1 /*!
2  * \brief Unit tests for \ref UrlUtil
3  *
4  * \copyright Copyright (c) 2014-2021 Governikus GmbH & Co. KG, Germany
5  */
6 
7 #include "UrlUtil.h"
8 #include <QSslCertificate>
9 #include <QtTest>
10 
11 using namespace governikus;
12 
13 
14 class test_UrlUtil
15 	: public QObject
16 {
17 	Q_OBJECT
18 
19 	private Q_SLOTS:
isMatchingSameOriginPolicy_data()20 		void isMatchingSameOriginPolicy_data()
21 		{
22 			QTest::addColumn<QUrl>("url1");
23 			QTest::addColumn<QUrl>("url2");
24 			QTest::addColumn<bool>("valid");
25 
26 			QTest::newRow("none_80") << QUrl("http://www.web.de/index.html") << QUrl("http://www.web.de:80") << true;
27 			QTest::newRow("8080_8080") << QUrl("http://www.web.de:8080/index.html") << QUrl("http://www.web.de:8080") << true;
28 			QTest::newRow("none_443") << QUrl("https://www.web.de/index.html") << QUrl("https://www.web.de:443") << true;
29 			QTest::newRow("8443_8443") << QUrl("https://www.web.de:8443/index.html") << QUrl("https://www.web.de:8443") << true;
30 
31 			QTest::newRow("false_8443_8444") << QUrl("https://www.web.de:8443/index.html") << QUrl("https://www.web.de:8444") << false;
32 			QTest::newRow("false_999_999") << QUrl("https://www.web.de:999/index.html") << QUrl("http://www.web.de:999") << false;
33 			QTest::newRow("false_different_domain") << QUrl("http://www.google.de:999/index.html") << QUrl("http://www.web.de:999") << false;
34 
35 			QTest::newRow("no_scheme_https_with_port") << QUrl("de.dummy.cz") << QUrl("https://de.dummy.cz:443") << false;
36 			QTest::newRow("no_scheme_https_without_port") << QUrl("de.dummy.cz") << QUrl("https://de.dummy.cz") << false;
37 
38 			QTest::newRow("no_scheme_http_with_port") << QUrl("de.dummy.cz") << QUrl("http://de.dummy.cz:80") << false;
39 			QTest::newRow("no_scheme_http_without_port") << QUrl("de.dummy.cz") << QUrl("http://de.dummy.cz") << false;
40 		}
41 
42 
isMatchingSameOriginPolicy()43 		void isMatchingSameOriginPolicy()
44 		{
45 			QFETCH(QUrl, url1);
46 			QFETCH(QUrl, url2);
47 			QFETCH(bool, valid);
48 
49 			QCOMPARE(UrlUtil::isMatchingSameOriginPolicy(url1, url2), valid);
50 		}
51 
52 
majorMinor()53 		void majorMinor()
54 		{
55 			const QString URL_PREFIX("https://www.der-pott-kocht.net:8443/index.html");
56 			const QUrl url(URL_PREFIX);
57 
58 			// Ok
59 			QCOMPARE(UrlUtil::addMajorMinor(url, GlobalStatus(ECardApiResult(ECardApiResult::Major::Ok, ECardApiResult::Minor::null))).toString(),
60 					URL_PREFIX + "?ResultMajor=ok");
61 
62 			// General server error
63 			QCOMPARE(UrlUtil::addMajorMinor(url, GlobalStatus(ECardApiResult(ECardApiResult::Major::Error, ECardApiResult::Minor::AL_Unknown_Error, QString(), ECardApiResult::Origin::Server))).toString(),
64 					URL_PREFIX + "?ResultMajor=error&ResultMinor=serverError");
65 
66 			// Minors defined in TR-03112-1 and TR-03124-1 2.5.4.2
67 			QCOMPARE(UrlUtil::addMajorMinor(url, GlobalStatus(ECardApiResult(ECardApiResult::Major::Error, ECardApiResult::Minor::AL_Communication_Error))).toString(),
68 					URL_PREFIX + "?ResultMajor=error&ResultMinor=communicationError");
69 			QCOMPARE(UrlUtil::addMajorMinor(url, GlobalStatus(ECardApiResult(ECardApiResult::Major::Error, ECardApiResult::Minor::DP_Trusted_Channel_Establishment_Failed))).toString(),
70 					URL_PREFIX + "?ResultMajor=error&ResultMinor=trustedChannelEstablishmentFailed");
71 			QCOMPARE(UrlUtil::addMajorMinor(url, GlobalStatus(ECardApiResult(ECardApiResult::Major::Error, ECardApiResult::Minor::SAL_Cancellation_by_User))).toString(),
72 					URL_PREFIX + "?ResultMajor=error&ResultMinor=cancellationByUser");
73 
74 			// No difference between client and server origin
75 			QCOMPARE(UrlUtil::addMajorMinor(url, GlobalStatus(ECardApiResult(ECardApiResult::Major::Error, ECardApiResult::Minor::SAL_Cancellation_by_User, QString(), ECardApiResult::Origin::Server))).toString(),
76 					URL_PREFIX + "?ResultMajor=error&ResultMinor=cancellationByUser");
77 
78 			// General client error
79 			QCOMPARE(UrlUtil::addMajorMinor(url, GlobalStatus(ECardApiResult(ECardApiResult::Major::Error, ECardApiResult::Minor::AL_Not_Initialized))).toString(),
80 					URL_PREFIX + "?ResultMajor=error&ResultMinor=clientError");
81 		}
82 
83 
84 };
85 
86 QTEST_GUILESS_MAIN(test_UrlUtil)
87 #include "test_UrlUtil.moc"
88