Debian Codegraph @

tests/online-accounts-service/tst_signonui_service/tst_signonui_service.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This file is part of lomiri-online-accounts-service
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 3, as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranties of
14 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <online-accounts-common/globals.h>
22 #include "request-manager-mock.h"
23 #include "signonui-service.h"
24
25 #include <QDebug>
26 #include <QSignalSpy>
27 #include <QString>
28 #include <QTemporaryDir>
29 #include <QTest>
30 #include <sys/time.h>
31
32 using namespace SignOnUi;
33
34 class ServiceTest: public QObject
35 {
36 Q_OBJECT
37
38 public:
39 ServiceTest();
40
41 private:
42 void writeFile(const QString &name, const QByteArray &contents);
43 void setFileDate(const QString &name, qint64 timestamp);
44
45 private Q_SLOTS:
46 void testCookies_data();
47 void testCookies();
48
49 private:
50 OnlineAccountsUi::RequestManager m_requestManager;
51 Service m_service;
52 };
53
54 ServiceTest::ServiceTest():
55 QObject(0)
56 {
57 }
58
59 void ServiceTest::writeFile(const QString &name, const QByteArray &contents)
60 {
61 QFile file(name);
62 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
63 qWarning() << "Could not write file" << name;
64 return;
65 }
66
67 file.write(contents);
68 }
69
70 void ServiceTest::setFileDate(const QString &name, qint64 timestamp)
71 {
72 struct timeval times[2];
73
74 times[0].tv_sec = times[1].tv_sec = timestamp;
75 times[0].tv_usec = times[1].tv_usec = 0;
76 utimes(name.toUtf8().constData(), times);
77 }
78
79 void ServiceTest::testCookies_data()
80 {
81 QTest::addColumn<QString>("contents");
82 QTest::addColumn<qint64>("expectedTimestamp");
83 QTest::addColumn<RawCookies>("expectedCookies");
84
85 RawCookies cookies;
86
87 QTest::newRow("empty") <<
88 "" <<
89 qint64(1406204196) <<
90 cookies;
91
92 QTest::newRow("invalid-cookies") <<
93 "[\n"
94 " {\"path\": \"/\"},\n"
95 " {\"name\": \"C1\", \"path\": \"/\"},\n"
96 " {\"value\": \"else\", \"domain\": \"foo.com\"}\n"
97 "]" <<
98 qint64(1406104196) <<
99 cookies;
100 cookies.clear();
101
102 cookies.append("C1=something");
103 cookies.append("C2=else; domain=foo.com");
104 cookies.append("C3=yes; HttpOnly");
105 cookies.append("C4=OK; path=/");
106 cookies.append("C5=no; expires=Sat, 20-Aug-2016 16:33:43 GMT");
107 QTest::newRow("few-cookies") <<
108 "[\n"
109 " {\"name\": \"C1\", \"value\": \"something\"},\n"
110 " {\"name\": \"C2\", \"value\": \"else\", \"domain\": \"foo.com\"},\n"
111 " {\"name\": \"C3\", \"value\": \"yes\", \"httponly\": \"true\"},\n"
112 " {\"name\": \"C4\", \"value\": \"OK\", \"path\": \"/\"},\n"
113 " {\"name\": \"C5\", \"value\": \"no\", \"expirationdate\": \"2016-08-20T16:33:43Z\"}\n"
114 "]" <<
115 qint64(1406104196) <<
116 cookies;
117 }
118
119 void ServiceTest::testCookies()
120 {
121 QFETCH(QString, contents);
122 QFETCH(qint64, expectedTimestamp);
123 QFETCH(RawCookies, expectedCookies);
124
125 QTemporaryDir tempDir;
126 QVERIFY(tempDir.isValid());
127 qputenv("XDG_CACHE_HOME", tempDir.path().toUtf8());
128 quint32 id = 47;
129
130 QString subDir =
131 QString("lomiri-online-accounts-ui/id-%2").arg(id);
132 QDir identityDir(tempDir.path());
133 identityDir.mkpath(subDir);
134 identityDir.cd(subDir);
135
136 QString cookieFile(identityDir.filePath("cookies.json"));
137 writeFile(cookieFile, contents.toUtf8());
138 setFileDate(cookieFile, expectedTimestamp);
139
140 RawCookies cookies;
141 qint64 timestamp = 0;
142 m_service.cookiesForIdentity(id, cookies, timestamp);
143 QCOMPARE(cookies, expectedCookies);
144 /* Be more tolerant about the timestamp: divide by 10, to allow a 10
145 * seconds gap */
146 QCOMPARE(timestamp / 10, expectedTimestamp / 10);
147 }
148
149 QTEST_MAIN(ServiceTest);
150
151 #include "tst_signonui_service.moc"