|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the FOO module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtCore/QString> |
|
44 #include <QtTest/QtTest> |
|
45 #include <QtCore/QCoreApplication> |
|
46 #include <QtNetwork/QAuthenticator> |
|
47 |
|
48 #include <private/qauthenticator_p.h> |
|
49 |
|
50 class tst_QAuthenticator : public QObject |
|
51 { |
|
52 Q_OBJECT |
|
53 |
|
54 public: |
|
55 tst_QAuthenticator(); |
|
56 |
|
57 private Q_SLOTS: |
|
58 void basicAuth(); |
|
59 void basicAuth_data(); |
|
60 |
|
61 void ntlmAuth_data(); |
|
62 void ntlmAuth(); |
|
63 }; |
|
64 |
|
65 tst_QAuthenticator::tst_QAuthenticator() |
|
66 { |
|
67 } |
|
68 |
|
69 void tst_QAuthenticator::basicAuth_data() |
|
70 { |
|
71 QTest::addColumn<QString>("data"); |
|
72 QTest::addColumn<QString>("realm"); |
|
73 QTest::addColumn<QString>("user"); |
|
74 QTest::addColumn<QString>("password"); |
|
75 QTest::addColumn<QByteArray>("expectedReply"); |
|
76 |
|
77 QTest::newRow("just-user") << "" << "" << "foo" << "" << QByteArray("foo:").toBase64(); |
|
78 QTest::newRow("user-password") << "" << "" << "foo" << "bar" << QByteArray("foo:bar").toBase64(); |
|
79 QTest::newRow("user-password-realm") << "realm=\"secure area\"" << "secure area" << "foo" << "bar" << QByteArray("foo:bar").toBase64(); |
|
80 } |
|
81 |
|
82 void tst_QAuthenticator::basicAuth() |
|
83 { |
|
84 QFETCH(QString, data); |
|
85 QFETCH(QString, realm); |
|
86 QFETCH(QString, user); |
|
87 QFETCH(QString, password); |
|
88 QFETCH(QByteArray, expectedReply); |
|
89 |
|
90 QAuthenticator auth; |
|
91 auth.detach(); |
|
92 QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(auth); |
|
93 QVERIFY(priv->phase == QAuthenticatorPrivate::Start); |
|
94 |
|
95 QList<QPair<QByteArray, QByteArray> > headers; |
|
96 headers << qMakePair(QByteArray("WWW-Authenticate"), "Basic " + data.toUtf8()); |
|
97 priv->parseHttpResponse(headers, /*isProxy = */ false); |
|
98 |
|
99 QCOMPARE(auth.realm(), realm); |
|
100 QCOMPARE(auth.option("realm").toString(), realm); |
|
101 |
|
102 auth.setUser(user); |
|
103 auth.setPassword(password); |
|
104 |
|
105 QVERIFY(priv->phase == QAuthenticatorPrivate::Start); |
|
106 |
|
107 QCOMPARE(priv->calculateResponse("GET", "/").constData(), ("Basic " + expectedReply).constData()); |
|
108 } |
|
109 |
|
110 void tst_QAuthenticator::ntlmAuth_data() |
|
111 { |
|
112 QTest::addColumn<QString>("data"); |
|
113 QTest::addColumn<QString>("realm"); |
|
114 |
|
115 QTest::newRow("no-realm") << "TlRMTVNTUAACAAAAHAAcADAAAAAFAoEATFZ3OLRQADIAAAAAAAAAAJYAlgBMAAAAUQBUAC0AVABFAFMAVAAtAEQATwBNAEEASQBOAAIAHABRAFQALQBUAEUAUwBUAC0ARABPAE0AQQBJAE4AAQAcAFEAVAAtAFQARQBTAFQALQBTAEUAUgBWAEUAUgAEABYAcQB0AC0AdABlAHMAdAAtAG4AZQB0AAMANABxAHQALQB0AGUAcwB0AC0AcwBlAHIAdgBlAHIALgBxAHQALQB0AGUAcwB0AC0AbgBlAHQAAAAAAA==" << ""; |
|
116 QTest::newRow("with-realm") << "TlRMTVNTUAACAAAADAAMADgAAAAFAoECWCZkccFFAzwAAAAAAAAAAL4AvgBEAAAABQLODgAAAA9NAEcARABOAE8ASwACAAwATQBHAEQATgBPAEsAAQAcAE4ATwBLAC0AQQBNAFMAUwBTAEYARQAtADAAMQAEACAAbQBnAGQAbgBvAGsALgBuAG8AawBpAGEALgBjAG8AbQADAD4AbgBvAGsALQBhAG0AcwBzAHMAZgBlAC0AMAAxAC4AbQBnAGQAbgBvAGsALgBuAG8AawBpAGEALgBjAG8AbQAFACAAbQBnAGQAbgBvAGsALgBuAG8AawBpAGEALgBjAG8AbQAAAAAA" << "NOE"; |
|
117 } |
|
118 |
|
119 void tst_QAuthenticator::ntlmAuth() |
|
120 { |
|
121 QFETCH(QString, data); |
|
122 QFETCH(QString, realm); |
|
123 |
|
124 QAuthenticator auth; |
|
125 auth.detach(); |
|
126 QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(auth); |
|
127 QVERIFY(priv->phase == QAuthenticatorPrivate::Start); |
|
128 |
|
129 QList<QPair<QByteArray, QByteArray> > headers; |
|
130 |
|
131 // NTLM phase 1: negotiate |
|
132 // This phase of NTLM contains no information, other than what we're willing to negotiate |
|
133 // Current implementation uses flags: |
|
134 // NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_REQUEST_TARGET |
|
135 headers << qMakePair<QByteArray, QByteArray>("WWW-Authenticate", "NTLM"); |
|
136 priv->parseHttpResponse(headers, /*isProxy = */ false); |
|
137 QCOMPARE(priv->calculateResponse("GET", "/").constData(), "NTLM TlRMTVNTUAABAAAABQIAAAAAAAAAAAAAAAAAAAAAAAA="); |
|
138 |
|
139 // NTLM phase 2: challenge |
|
140 headers.clear(); |
|
141 headers << qMakePair(QByteArray("WWW-Authenticate"), "NTLM " + data.toUtf8()); |
|
142 priv->parseHttpResponse(headers, /*isProxy = */ false); |
|
143 |
|
144 QEXPECT_FAIL("with-realm", "NTLM authentication code doesn't extract the realm", Continue); |
|
145 QCOMPARE(auth.realm(), realm); |
|
146 |
|
147 auth.setUser("unimportant"); |
|
148 auth.setPassword("unimportant"); |
|
149 |
|
150 QVERIFY(!priv->calculateResponse("GET", "/").isEmpty()); |
|
151 } |
|
152 |
|
153 QTEST_MAIN(tst_QAuthenticator); |
|
154 |
|
155 #include "tst_qauthenticator.moc" |