1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the FOO module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtCore/QString>
44 #include <QtTest/QtTest>
45 #include <QtCore/QCoreApplication>
46 #include <QtNetwork/QAuthenticator>
48 #include <private/qauthenticator_p.h>
50 class tst_QAuthenticator : public QObject
59 void basicAuth_data();
65 tst_QAuthenticator::tst_QAuthenticator()
69 void tst_QAuthenticator::basicAuth_data()
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");
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();
82 void tst_QAuthenticator::basicAuth()
84 QFETCH(QString, data);
85 QFETCH(QString, realm);
86 QFETCH(QString, user);
87 QFETCH(QString, password);
88 QFETCH(QByteArray, expectedReply);
92 QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(auth);
93 QVERIFY(priv->phase == QAuthenticatorPrivate::Start);
95 QList<QPair<QByteArray, QByteArray> > headers;
96 headers << qMakePair<QByteArray, QByteArray>(QByteArray("WWW-Authenticate"), "Basic " + data.toUtf8());
97 priv->parseHttpResponse(headers, /*isProxy = */ false);
99 QCOMPARE(auth.realm(), realm);
100 QCOMPARE(auth.option("realm").toString(), realm);
103 auth.setPassword(password);
105 QVERIFY(priv->phase == QAuthenticatorPrivate::Start);
107 QCOMPARE(priv->calculateResponse("GET", "/").constData(), QByteArray("Basic " + expectedReply).constData());
110 void tst_QAuthenticator::ntlmAuth_data()
112 QTest::addColumn<QString>("data");
113 QTest::addColumn<QString>("realm");
115 QTest::newRow("no-realm") << "TlRMTVNTUAACAAAAHAAcADAAAAAFAoEATFZ3OLRQADIAAAAAAAAAAJYAlgBMAAAAUQBUAC0AVABFAFMAVAAtAEQATwBNAEEASQBOAAIAHABRAFQALQBUAEUAUwBUAC0ARABPAE0AQQBJAE4AAQAcAFEAVAAtAFQARQBTAFQALQBTAEUAUgBWAEUAUgAEABYAcQB0AC0AdABlAHMAdAAtAG4AZQB0AAMANABxAHQALQB0AGUAcwB0AC0AcwBlAHIAdgBlAHIALgBxAHQALQB0AGUAcwB0AC0AbgBlAHQAAAAAAA==" << "";
116 QTest::newRow("with-realm") << "TlRMTVNTUAACAAAADAAMADgAAAAFAoECWCZkccFFAzwAAAAAAAAAAL4AvgBEAAAABQLODgAAAA9NAEcARABOAE8ASwACAAwATQBHAEQATgBPAEsAAQAcAE4ATwBLAC0AQQBNAFMAUwBTAEYARQAtADAAMQAEACAAbQBnAGQAbgBvAGsALgBuAG8AawBpAGEALgBjAG8AbQADAD4AbgBvAGsALQBhAG0AcwBzAHMAZgBlAC0AMAAxAC4AbQBnAGQAbgBvAGsALgBuAG8AawBpAGEALgBjAG8AbQAFACAAbQBnAGQAbgBvAGsALgBuAG8AawBpAGEALgBjAG8AbQAAAAAA" << "NOE";
119 void tst_QAuthenticator::ntlmAuth()
121 QFETCH(QString, data);
122 QFETCH(QString, realm);
126 QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(auth);
127 QVERIFY(priv->phase == QAuthenticatorPrivate::Start);
129 QList<QPair<QByteArray, QByteArray> > headers;
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=");
139 // NTLM phase 2: challenge
141 headers << qMakePair<QByteArray, QByteArray>(QByteArray("WWW-Authenticate"), "NTLM " + data.toUtf8());
142 priv->parseHttpResponse(headers, /*isProxy = */ false);
144 QEXPECT_FAIL("with-realm", "NTLM authentication code doesn't extract the realm", Continue);
145 QCOMPARE(auth.realm(), realm);
147 auth.setUser("unimportant");
148 auth.setPassword("unimportant");
150 QVERIFY(!priv->calculateResponse("GET", "/").isEmpty());
153 QTEST_MAIN(tst_QAuthenticator);
155 #include "tst_qauthenticator.moc"