Make some tests and benchmarks pass with QT_NO_QSTRINGBUILDER
[profile/ivi/qtbase.git] / tests / auto / network / kernel / qauthenticator / tst_qauthenticator.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the FOO module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
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, QByteArray>(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(), QByteArray("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, QByteArray>(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"