source: calamares/trunk/fuentes/src/modules/locale/GeoIPTests.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 7.5 KB
Line 
1/* === This file is part of Calamares - <http://github.com/calamares> ===
2 *
3 *   Copyright 2018, Adriaan de Groot <groot@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "GeoIPTests.h"
20
21#include "GeoIPJSON.h"
22#ifdef HAVE_XML
23#include "GeoIPXML.h"
24#endif
25
26#include <QNetworkAccessManager>
27#include <QNetworkReply>
28#include <QNetworkRequest>
29
30#include <QtTest/QtTest>
31
32QTEST_GUILESS_MAIN( GeoIPTests )
33
34GeoIPTests::GeoIPTests()
35{
36}
37
38GeoIPTests::~GeoIPTests()
39{
40}
41
42void
43GeoIPTests::initTestCase()
44{
45}
46
47static const char json_data_attribute[] =
48    "{\"time_zone\":\"Europe/Amsterdam\"}";
49
50void
51GeoIPTests::testJSON()
52{
53    GeoIPJSON handler;
54    auto tz = handler.processReply( json_data_attribute );
55
56    QCOMPARE( tz.first, QLatin1String( "Europe" ) );
57    QCOMPARE( tz.second, QLatin1String( "Amsterdam" ) );
58
59    // JSON is quite tolerant
60    tz = handler.processReply( "time_zone: \"Europe/Brussels\"" );
61    QCOMPARE( tz.second, QLatin1String( "Brussels" ) );
62
63    tz = handler.processReply( "time_zone: America/New_York\n" );
64    QCOMPARE( tz.first, QLatin1String( "America" ) );
65}
66
67void GeoIPTests::testJSONalt()
68{
69    GeoIPJSON handler( "zona_de_hora" );
70
71    auto tz = handler.processReply( json_data_attribute );
72    QCOMPARE( tz.first, QString() );  // Not found
73
74    tz = handler.processReply( "tarifa: 12\nzona_de_hora: Europe/Madrid" );
75    QCOMPARE( tz.first, QLatin1String( "Europe" ) );
76    QCOMPARE( tz.second, QLatin1String( "Madrid" ) );
77}
78
79void
80GeoIPTests::testJSONbad()
81{
82    static const char data[] = "time_zone: 1";
83
84    GeoIPJSON handler;
85    auto tz = handler.processReply( data );
86
87    tz = handler.processReply( data );
88    QCOMPARE( tz.first, QString() );
89
90    tz = handler.processReply( "" );
91    QCOMPARE( tz.first, QString() );
92
93    tz = handler.processReply( "<html><body>404 Forbidden</body></html>" );
94    QCOMPARE( tz.first, QString() );
95
96    tz = handler.processReply( "{ time zone = 'America/LosAngeles'}" );
97    QCOMPARE( tz.first, QString() );
98}
99
100
101static const char xml_data_ubiquity[] =
102        R"(<Response>
103  <Ip>85.150.1.1</Ip>
104  <Status>OK</Status>
105  <CountryCode>NL</CountryCode>
106  <CountryCode3>NLD</CountryCode3>
107  <CountryName>Netherlands</CountryName>
108  <RegionCode>None</RegionCode>
109  <RegionName>None</RegionName>
110  <City>None</City>
111  <ZipPostalCode/>
112  <Latitude>50.0</Latitude>
113  <Longitude>4.0</Longitude>
114  <AreaCode>0</AreaCode>
115  <TimeZone>Europe/Amsterdam</TimeZone>
116</Response>)";
117
118void
119GeoIPTests::testXML()
120{
121#ifdef HAVE_XML
122    GeoIPXML handler;
123    auto tz = handler.processReply( xml_data_ubiquity );
124
125    QCOMPARE( tz.first, QLatin1String( "Europe" ) );
126    QCOMPARE( tz.second, QLatin1String( "Amsterdam" ) );
127#endif
128}
129
130void
131GeoIPTests::testXML2()
132{
133    static const char data[] =
134        "<Response><TimeZone>America/North Dakota/Beulah</TimeZone></Response>";  // With a space!
135
136#ifdef HAVE_XML
137    GeoIPXML handler;
138    auto tz = handler.processReply( data );
139
140    QCOMPARE( tz.first, QLatin1String( "America" ) );
141    QCOMPARE( tz.second, QLatin1String( "North_Dakota/Beulah" ) );  // Without space
142#endif
143}
144
145
146void GeoIPTests::testXMLalt()
147{
148#ifdef HAVE_XML
149    GeoIPXML handler( "ZT" );
150
151    auto tz = handler.processReply( "<A><B/><C><ZT>Moon/Dark_side</ZT></C></A>" );
152    QCOMPARE( tz.first, QLatin1String( "Moon" ) );
153    QCOMPARE( tz.second, QLatin1String( "Dark_side" ) );
154#endif
155}
156
157void
158GeoIPTests::testXMLbad()
159{
160#ifdef HAVE_XML
161    GeoIPXML handler;
162    auto tz = handler.processReply( "{time_zone: \"Europe/Paris\"}" );
163    QCOMPARE( tz.first, QString() );
164
165    tz = handler.processReply( "<Response></Response>" );
166    QCOMPARE( tz.first, QString() );
167
168    tz = handler.processReply( "fnord<html/>" );
169    QCOMPARE( tz.first, QString() );
170#endif
171}
172
173void GeoIPTests::testSplitTZ()
174{
175    auto tz = GeoIP::splitTZString( QLatin1String("Moon/Dark_side") );
176    QCOMPARE( tz.first, QLatin1String("Moon") );
177    QCOMPARE( tz.second, QLatin1String("Dark_side") );
178
179    // Some providers return weirdly escaped data
180    tz = GeoIP::splitTZString( QLatin1String("America\\/NewYork") );
181    QCOMPARE( tz.first, QLatin1String("America") );
182    QCOMPARE( tz.second, QLatin1String("NewYork") );  // That's not actually the zone name
183
184    // Check that bogus data fails
185    tz = GeoIP::splitTZString( QString() );
186    QCOMPARE( tz.first, QString() );
187
188    tz = GeoIP::splitTZString( QLatin1String("America.NewYork") );
189    QCOMPARE( tz.first, QString() );
190
191    // Check that three-level is split properly and space is replaced
192    tz = GeoIP::splitTZString( QLatin1String("America/North Dakota/Beulah") );
193    QCOMPARE( tz.first, QLatin1String("America") );
194    QCOMPARE( tz.second, QLatin1String("North_Dakota/Beulah") );
195}
196
197
198static QByteArray
199synchronous_get( const char* urlstring )
200{
201    QUrl url( urlstring );
202    QNetworkAccessManager manager;
203    QEventLoop loop;
204
205    qDebug() << "Fetching" << url;
206
207    QObject::connect( &manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit );
208
209    QNetworkRequest request( url );
210    QNetworkReply* reply = manager.get( request );
211    loop.exec();
212    reply->deleteLater();
213    return reply->readAll();
214}
215
216#define CHECK_GET(t, selector, url) \
217    { \
218        auto tz = GeoIP##t( selector ).processReply( synchronous_get( url ) ); \
219        QCOMPARE( default_tz, tz ); \
220    }
221
222void GeoIPTests::testGet()
223{
224    if ( !QProcessEnvironment::systemEnvironment().contains( QLatin1String("TEST_HTTP_GET") ) )
225    {
226        qDebug() << "Skipping HTTP GET tests";
227        return;
228    }
229
230    GeoIPJSON default_handler;
231    // Call the KDE service the definitive source.
232    auto default_tz = default_handler.processReply( synchronous_get( "https://geoip.kde.org/v1/calamares" ) );
233
234    // This is bogus, because the test isn't always run by me
235    // QCOMPARE( default_tz.first, QLatin1String("Europe") );
236    // QCOMPARE( default_tz.second, QLatin1String("Amsterdam") );
237    QVERIFY( !default_tz.first.isEmpty() );
238    QVERIFY( !default_tz.second.isEmpty() );
239
240    // Each expansion of CHECK_GET does a synchronous GET, then checks that
241    // the TZ data is the same as the default_tz; this is fragile if the
242    // services don't agree on the location of where the test is run.
243    CHECK_GET( JSON, QString(), "https://geoip.kde.org/v1/calamares" )     // Check it's consistent
244    CHECK_GET( JSON, QString(), "http://freegeoip.net/json/" )             // Original FreeGeoIP service
245    CHECK_GET( JSON, QLatin1String("timezone"), "https://ipapi.co/json" )  // Different JSON
246    CHECK_GET( JSON, QLatin1String("timezone"), "http://ip-api.com/json" )
247
248    CHECK_GET( JSON, QLatin1String("location.time_zone"), "http://geoip.nekudo.com/api/" )  // 2-level JSON
249
250    CHECK_GET( JSON, QLatin1String("Location.TimeZone"), "https://geoip.kde.org/debug" )  // 2-level JSON
251
252#ifdef HAVE_XML
253    CHECK_GET( XML, QString(), "http://geoip.ubuntu.com/lookup" )  // Ubiquity's XML format
254    CHECK_GET( XML, QString(),  "https://geoip.kde.org/v1/ubiquity" )  // Temporary KDE service
255#endif
256}
Note: See TracBrowser for help on using the repository browser.