1 /* === This file is part of Calamares - <https://calamares.io> ===
2 *
3 * SPDX-FileCopyrightText: 2018 Adriaan de Groot <groot@kde.org>
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * Calamares is Free Software: see the License-Identifier above.
7 *
8 */
9
10 #include "GeoIPTests.h"
11
12 #include "GeoIPFixed.h"
13 #include "GeoIPJSON.h"
14 #ifdef QT_XML_LIB
15 #include "GeoIPXML.h"
16 #endif
17 #include "Handler.h"
18
19 #include "network/Manager.h"
20
21 #include <QtTest/QtTest>
22
23 QTEST_GUILESS_MAIN( GeoIPTests )
24
25 using namespace CalamaresUtils::GeoIP;
26
GeoIPTests()27 GeoIPTests::GeoIPTests() {}
28
~GeoIPTests()29 GeoIPTests::~GeoIPTests() {}
30
31 void
initTestCase()32 GeoIPTests::initTestCase()
33 {
34 }
35
36 static const char json_data_attribute[] = "{\"time_zone\":\"Europe/Amsterdam\"}";
37
38 void
testJSON()39 GeoIPTests::testJSON()
40 {
41 GeoIPJSON handler;
42 auto tz = handler.processReply( json_data_attribute );
43
44 QCOMPARE( tz.first, QStringLiteral( "Europe" ) );
45 QCOMPARE( tz.second, QStringLiteral( "Amsterdam" ) );
46
47 // JSON is quite tolerant
48 tz = handler.processReply( "time_zone: \"Europe/Brussels\"" );
49 QCOMPARE( tz.second, QStringLiteral( "Brussels" ) );
50
51 tz = handler.processReply( "time_zone: America/New_York\n" );
52 QCOMPARE( tz.first, QStringLiteral( "America" ) );
53 }
54
55 void
testJSONalt()56 GeoIPTests::testJSONalt()
57 {
58 GeoIPJSON handler( "zona_de_hora" );
59
60 auto tz = handler.processReply( json_data_attribute );
61 QCOMPARE( tz.first, QString() ); // Not found
62
63 tz = handler.processReply( "tarifa: 12\nzona_de_hora: Europe/Madrid" );
64 QCOMPARE( tz.first, QStringLiteral( "Europe" ) );
65 QCOMPARE( tz.second, QStringLiteral( "Madrid" ) );
66 }
67
68 void
testJSONbad()69 GeoIPTests::testJSONbad()
70 {
71 static const char data[] = "time_zone: 1";
72
73 GeoIPJSON handler;
74 auto tz = handler.processReply( data );
75
76 tz = handler.processReply( data );
77 QCOMPARE( tz.first, QString() );
78
79 tz = handler.processReply( "" );
80 QCOMPARE( tz.first, QString() );
81
82 tz = handler.processReply( "<html><body>404 Forbidden</body></html>" );
83 QCOMPARE( tz.first, QString() );
84
85 tz = handler.processReply( "{ time zone = 'America/LosAngeles'}" );
86 QCOMPARE( tz.first, QString() );
87 }
88
89
90 static const char xml_data_ubiquity[] =
91 R"(<Response>
92 <Ip>85.150.1.1</Ip>
93 <Status>OK</Status>
94 <CountryCode>NL</CountryCode>
95 <CountryCode3>NLD</CountryCode3>
96 <CountryName>Netherlands</CountryName>
97 <RegionCode>None</RegionCode>
98 <RegionName>None</RegionName>
99 <City>None</City>
100 <ZipPostalCode/>
101 <Latitude>50.0</Latitude>
102 <Longitude>4.0</Longitude>
103 <AreaCode>0</AreaCode>
104 <TimeZone>Europe/Amsterdam</TimeZone>
105 </Response>)";
106
107 void
testXML()108 GeoIPTests::testXML()
109 {
110 #ifdef QT_XML_LIB
111 GeoIPXML handler;
112 auto tz = handler.processReply( xml_data_ubiquity );
113
114 QCOMPARE( tz.first, QStringLiteral( "Europe" ) );
115 QCOMPARE( tz.second, QStringLiteral( "Amsterdam" ) );
116 #endif
117 }
118
119 void
testXML2()120 GeoIPTests::testXML2()
121 {
122 static const char data[]
123 = "<Response><TimeZone>America/North Dakota/Beulah</TimeZone></Response>"; // With a space!
124
125 #ifdef QT_XML_LIB
126 GeoIPXML handler;
127 auto tz = handler.processReply( data );
128
129 QCOMPARE( tz.first, QStringLiteral( "America" ) );
130 QCOMPARE( tz.second, QStringLiteral( "North_Dakota/Beulah" ) ); // Without space
131 #endif
132 }
133
134
135 void
testXMLalt()136 GeoIPTests::testXMLalt()
137 {
138 #ifdef QT_XML_LIB
139 GeoIPXML handler( "ZT" );
140
141 auto tz = handler.processReply( "<A><B/><C><ZT>Moon/Dark_side</ZT></C></A>" );
142 QCOMPARE( tz.first, QStringLiteral( "Moon" ) );
143 QCOMPARE( tz.second, QStringLiteral( "Dark_side" ) );
144 #endif
145 }
146
147 void
testXMLbad()148 GeoIPTests::testXMLbad()
149 {
150 #ifdef QT_XML_LIB
151 GeoIPXML handler;
152 auto tz = handler.processReply( "{time_zone: \"Europe/Paris\"}" );
153 QCOMPARE( tz.first, QString() );
154
155 tz = handler.processReply( "<Response></Response>" );
156 QCOMPARE( tz.first, QString() );
157
158 tz = handler.processReply( "fnord<html/>" );
159 QCOMPARE( tz.first, QString() );
160 #endif
161 }
162
163 void
testSplitTZ()164 GeoIPTests::testSplitTZ()
165 {
166 using namespace CalamaresUtils::GeoIP;
167 auto tz = splitTZString( QStringLiteral( "Moon/Dark_side" ) );
168 QCOMPARE( tz.first, QStringLiteral( "Moon" ) );
169 QCOMPARE( tz.second, QStringLiteral( "Dark_side" ) );
170
171 // Some providers return weirdly escaped data
172 tz = splitTZString( QStringLiteral( "America\\/NewYork" ) );
173 QCOMPARE( tz.first, QStringLiteral( "America" ) );
174 QCOMPARE( tz.second, QStringLiteral( "NewYork" ) ); // That's not actually the zone name
175
176 // Check that bogus data fails
177 tz = splitTZString( QString() );
178 QCOMPARE( tz.first, QString() );
179
180 tz = splitTZString( QStringLiteral( "America.NewYork" ) );
181 QCOMPARE( tz.first, QString() );
182
183 // Check that three-level is split properly and space is replaced
184 tz = splitTZString( QStringLiteral( "America/North Dakota/Beulah" ) );
185 QCOMPARE( tz.first, QStringLiteral( "America" ) );
186 QCOMPARE( tz.second, QStringLiteral( "North_Dakota/Beulah" ) );
187 }
188
189
190 #define CHECK_GET( t, selector, url ) \
191 { \
192 auto tz = GeoIP##t( selector ) \
193 .processReply( CalamaresUtils::Network::Manager::instance().synchronousGet( QUrl( url ) ) ); \
194 qDebug() << tz; \
195 QCOMPARE( default_tz, tz ); \
196 auto tz2 = CalamaresUtils::GeoIP::Handler( "" #t, url, selector ).get(); \
197 qDebug() << tz2; \
198 QCOMPARE( default_tz, tz2 ); \
199 }
200
201 void
testGet()202 GeoIPTests::testGet()
203 {
204 if ( !QProcessEnvironment::systemEnvironment().contains( QStringLiteral( "TEST_HTTP_GET" ) ) )
205 {
206 qDebug() << "Skipping HTTP GET tests, set TEST_HTTP_GET environment variable to enable";
207 return;
208 }
209
210 GeoIPJSON default_handler;
211 // Call the KDE service the definitive source.
212 auto default_tz = default_handler.processReply(
213 CalamaresUtils::Network::Manager::instance().synchronousGet( QUrl( "https://geoip.kde.org/v1/calamares" ) ) );
214
215 // This is bogus, because the test isn't always run by me
216 // QCOMPARE( default_tz.first, QStringLiteral("Europe") );
217 // QCOMPARE( default_tz.second, QStringLiteral("Amsterdam") );
218 QVERIFY( !default_tz.first.isEmpty() );
219 QVERIFY( !default_tz.second.isEmpty() );
220
221 // Each expansion of CHECK_GET does a synchronous GET, then checks that
222 // the TZ data is the same as the default_tz; this is fragile if the
223 // services don't agree on the location of where the test is run.
224 CHECK_GET( JSON, QString(), "https://geoip.kde.org/v1/calamares" ) // Check it's consistent
225 CHECK_GET( JSON, QStringLiteral( "timezone" ), "https://ipapi.co/json" ) // Different JSON
226 CHECK_GET( JSON, QStringLiteral( "timezone" ), "http://ip-api.com/json" )
227
228 CHECK_GET( JSON, QStringLiteral( "Location.TimeZone" ), "https://geoip.kde.org/debug" ) // 2-level JSON
229
230 #ifdef QT_XML_LIB
231 CHECK_GET( XML, QString(), "http://geoip.ubuntu.com/lookup" ) // Ubiquity's XML format
232 CHECK_GET( XML, QString(), "https://geoip.kde.org/v1/ubiquity" ) // Temporary KDE service
233 #endif
234 }
235
236 void
testFixed()237 GeoIPTests::testFixed()
238 {
239 {
240 GeoIPFixed f;
241 auto tz = f.processReply( QByteArray() );
242 QCOMPARE( tz.first, QStringLiteral( "Europe" ) );
243 QCOMPARE( tz.second, QStringLiteral( "Amsterdam" ) );
244
245 QCOMPARE( f.processReply( xml_data_ubiquity ), tz );
246 QCOMPARE( f.processReply( QByteArray( "derp" ) ), tz );
247 }
248 {
249 GeoIPFixed f( QStringLiteral( "America/Vancouver" ) );
250 auto tz = f.processReply( QByteArray() );
251 QCOMPARE( tz.first, QStringLiteral( "America" ) );
252 QCOMPARE( tz.second, QStringLiteral( "Vancouver" ) );
253
254 QCOMPARE( f.processReply( xml_data_ubiquity ), tz );
255 QCOMPARE( f.processReply( QByteArray( "derp" ) ), tz );
256 }
257 {
258 GeoIPFixed f( QStringLiteral( "America/North Dakota/Beulah" ) );
259 auto tz = f.processReply( QByteArray() );
260 QCOMPARE( tz.first, QStringLiteral( "America" ) );
261 QCOMPARE( tz.second, QStringLiteral( "North_Dakota/Beulah" ) );
262
263 QCOMPARE( f.processReply( xml_data_ubiquity ), tz );
264 QCOMPARE( f.processReply( QByteArray( "derp" ) ), tz );
265 }
266 }
267