ssu
Loading...
Searching...
No Matches
urlresolvertest.cpp
Go to the documentation of this file.
1
7
8#include "urlresolvertest.h"
9
10#include <QtXml/QDomDocument>
11
12#include "constants.h"
13#include "libssu/sandbox_p.h"
14#include "testutils/process.h"
15
16void UrlResolverTest::initTestCase()
17{
18 // test will fail if executed without proper installation of libssu and repos
19 QString arch = "";
20#ifdef TARGET_ARCH
21 arch = TARGET_ARCH;
22#else
23#warning "TARGET_ARCH not defined"
24#warning "In-tree tests will fail without using run-tests script"
25#endif
26 rndRepos["nemo"] = QString("https://packages.example.com/nemo/latest/platform/%1/").arg(arch);
27 rndRepos["mer-core"] = QString("https://packages.example.com/mer/latest/builds/%1/packages/").arg(arch);
28 /*
29 rndRepos["non-oss"] = "";
30 */
31 releaseRepos["nemo"] = QString("https://packages.example.com/releases/1.2.3/nemo/platform/%1/").arg(arch);
32 releaseRepos["mer-core"] = QString("https://packages.example.com/1.2.3/mer/%1/packages/").arg(arch);
33 releaseRepos["jolla"] = QString("https://packages.example.com/releases/1.2.3/jolla/%1/").arg(arch);
34 releaseRepos["major"] = QString("https://packages.example.com/releases/1/major/%1/").arg(arch);
35 releaseRepos["minor"] = QString("https://packages.example.com/releases/2/minor/%1/").arg(arch);
36 releaseRepos["majmin"] = QString("https://packages.example.com/releases/1.2/majmin/%1/").arg(arch);
37}
38
39void UrlResolverTest::cleanupTestCase()
40{
41
42}
43
44void UrlResolverTest::checkFlavour()
45{
46 ssu.setFlavour("testing");
47 QCOMPARE(ssu.flavour(), QString("testing"));
48 ssu.setFlavour("release");
49 QCOMPARE(ssu.flavour(), QString("release"));
50 Ssu ssu2;
51 QCOMPARE(ssu2.flavour(), QString("release"));
52
53}
54
55void UrlResolverTest::checkRelease()
56{
57 ssu.setRelease("0.1");
58 QCOMPARE(ssu.release(), QString("0.1"));
59 ssu.setRelease("0.2", true);
60 QCOMPARE(ssu.release(), QString("0.1"));
61 QCOMPARE(ssu.release(true), QString("0.2"));
62 Ssu ssu2;
63 QCOMPARE(ssu2.release(), QString("0.1"));
64 QCOMPARE(ssu2.release(true), QString("0.2"));
65 ssu.setRelease("latest", true);
66}
67
68void UrlResolverTest::checkDomain()
69{
70 QString credentialsUrl;
71 QString registerUrl;
72
73 // domain not defined -> default
74 ssu.setDomain("");
75 credentialsUrl = ssu.repoUrl("credentials-url");
76 QCOMPARE(credentialsUrl, QString("https://ssu.testing.com/ssu/device/%1/credentials.xml"));
77
78 // domain defined but not matching block -> default
79 ssu.setDomain("nevermind");
80 credentialsUrl = ssu.repoUrl("credentials-url");
81 QCOMPARE(credentialsUrl, QString("https://ssu.testing.com/ssu/device/%1/credentials.xml"));
82
83 ssu.setDomain("example");
84 QCOMPARE(ssu.domain(), QString("example"));
85 credentialsUrl = ssu.repoUrl("credentials-url");
86 QCOMPARE(credentialsUrl, QString("https://ssu.example.com/ssu/device/%1/credentials.xml"));
87 registerUrl = ssu.repoUrl("register-url");
88 QCOMPARE(registerUrl, QString("https://ssu.example.com/ssu/device/%1/register.xml"));
89
90}
91void UrlResolverTest::checkCleanUrl()
92{
93 QHashIterator<QString, QString> i(rndRepos);
94 while (i.hasNext()) {
95 i.next();
96 QString url = ssu.repoUrl(i.key(), true);
97 QVERIFY(!url.contains("%("));
98 }
99
100}
101
102void UrlResolverTest::simpleRepoUrlLookup()
103{
104 QHashIterator<QString, QString> i(rndRepos);
105 while (i.hasNext()) {
106 QString url;
107 i.next();
108 url = ssu.repoUrl(i.key(), true);
109 QCOMPARE(url, i.value());
110 url = ssu.repoUrl(i.key(), false);
111 QVERIFY(url.compare(i.value()) != 0);
112 }
113 /*
114 QCOMPARE(ssu.repoUrl("jolla", 0), QString(""));
115 QCOMPARE(ssu.repoUrl("non-oss", 1), QString(""));
116 */
117}
118
119void UrlResolverTest::checkReleaseRepoUrls()
120{
121 ssu.setRelease("1.2.3");
122 QHashIterator<QString, QString> i(releaseRepos);
123 while (i.hasNext()) {
124 QString url;
125 i.next();
126 url = ssu.repoUrl(i.key(), false);
127 QCOMPARE(url, i.value());
128 url = ssu.repoUrl(i.key());
129 QCOMPARE(url, i.value());
130 }
131}
132
133void UrlResolverTest::checkRegisterDevice()
134{
135 QDomDocument doc("foo");
136
137 QDomElement root = doc.createElement("foo");
138 doc.appendChild(root);
139
140 QDomElement certificate = doc.createElement("certificate");
141 root.appendChild(certificate);
142
143 QVERIFY2(!ssu.registerDevice(&doc),
144 "Ssu::registerDevice() should fail when 'certificate' is empty");
145
146 QFile certificateFile(QString("%1/mycert.crt").arg(LOCATE_DATA_PATH));
147 QVERIFY(certificateFile.open(QIODevice::ReadOnly));
148
149 certificate.appendChild(doc.createTextNode(certificateFile.readAll()));
150
151 QDomElement privateKey = doc.createElement("privateKey");
152 root.appendChild(privateKey);
153
154 QVERIFY2(!ssu.registerDevice(&doc),
155 "Ssu::registerDevice() should fail when 'privateKey' is empty");
156
157 QFile privateKeyFile(QString("%1/mykey.key").arg(LOCATE_DATA_PATH));
158 QVERIFY(privateKeyFile.open(QIODevice::ReadOnly));
159
160 privateKey.appendChild(doc.createTextNode(privateKeyFile.readAll()));
161
162 QDomElement user = doc.createElement("user");
163 root.appendChild(user);
164 user.appendChild(doc.createTextNode("john.doe"));
165
166 QSignalSpy registrationStatusChanged_spy(&ssu, SIGNAL(registrationStatusChanged()));
167
168 QVERIFY(ssu.registerDevice(&doc));
169
170 QVERIFY(registrationStatusChanged_spy.count() == 1);
171 QVERIFY(ssu.isRegistered());
172
173 ssu.unregister();
174
175 QVERIFY(registrationStatusChanged_spy.count() == 2);
176 QVERIFY(!ssu.isRegistered());
177}
178
179void UrlResolverTest::checkSetCredentials()
180{
181 QDomDocument doc("foo");
182
183 QDomElement root = doc.createElement("foo");
184 doc.appendChild(root);
185
186 QDomElement credentials1 = doc.createElement("credentials");
187 root.appendChild(credentials1);
188
189 QVERIFY2(!ssu.setCredentials(&doc),
190 "Ssu::setCredentials() should fail when 'scope' is not defined");
191
192 credentials1.setAttribute("scope", "utscope1");
193
194 QVERIFY2(!ssu.setCredentials(&doc),
195 "Ssu::setCredentials() should fail when username/password is missing");
196
197 QDomElement username1 = doc.createElement("username");
198 credentials1.appendChild(username1);
199 username1.appendChild(doc.createTextNode("john.doe1"));
200
201 QVERIFY2(!ssu.setCredentials(&doc),
202 "Ssu::setCredentials() should fail when password is missing");
203
204 QDomElement password1 = doc.createElement("password");
205 credentials1.appendChild(password1);
206 password1.appendChild(doc.createTextNode("SeCrEt1"));
207
208 QVERIFY2(ssu.setCredentials(&doc),
209 qPrintable(QString("setCredentials() failed: %1").arg(ssu.lastError())));
210
211 QVERIFY2(ssu.lastCredentialsUpdate() > QDateTime::currentDateTime().addSecs(-5) &&
212 ssu.lastCredentialsUpdate() <= QDateTime::currentDateTime(),
213 "Ssu::lastCredentialsUpdate was not updated");
214
215 //QVERIFY(ssu.credentialScopes().contains("utscope1"));
216 QCOMPARE(ssu.credentials("utscope1").first, QString("john.doe1"));
217 QCOMPARE(ssu.credentials("utscope1").second, QString("SeCrEt1"));
218
219
220 QDomElement credentials2 = doc.createElement("credentials");
221 root.appendChild(credentials2);
222 credentials2.setAttribute("scope", "utscope2");
223
224 QDomElement username2 = doc.createElement("username");
225 credentials2.appendChild(username2);
226 username2.appendChild(doc.createTextNode("john.doe2"));
227
228 QDomElement password2 = doc.createElement("password");
229 credentials2.appendChild(password2);
230 password2.appendChild(doc.createTextNode("SeCrEt2"));
231
232 QVERIFY2(ssu.setCredentials(&doc),
233 qPrintable(QString("setCredentials() failed: %1").arg(ssu.lastError())));
234
235 QVERIFY2(ssu.lastCredentialsUpdate() > QDateTime::currentDateTime().addSecs(-5) &&
236 ssu.lastCredentialsUpdate() <= QDateTime::currentDateTime(),
237 "Ssu::lastCredentialsUpdate was not updated");
238
239 //QVERIFY(ssu.credentialScopes().contains("utscope1"));
240 QCOMPARE(ssu.credentials("utscope1").first, QString("john.doe1"));
241 QCOMPARE(ssu.credentials("utscope1").second, QString("SeCrEt1"));
242
243 //QVERIFY(ssu.credentialScopes().contains("utscope2"));
244 QCOMPARE(ssu.credentials("utscope2").first, QString("john.doe2"));
245 QCOMPARE(ssu.credentials("utscope2").second, QString("SeCrEt2"));
246}
247
248void UrlResolverTest::checkStoreAuthorizedKeys()
249{
250 QVERIFY(QDir().mkpath(Sandbox::map(QDir::homePath())));
251
252 QByteArray testData("# test data\n");
253 ssu.storeAuthorizedKeys(testData);
254
255 QFile authorizedKeys(Sandbox::map(QDir::home().filePath(".ssh/authorized_keys")));
256 QVERIFY(authorizedKeys.open(QIODevice::ReadOnly));
257
258 QVERIFY(authorizedKeys.readAll().split('\n').contains(testData.trimmed()));
259
260 QByteArray testData2("# test data2\n");
261 ssu.storeAuthorizedKeys(testData2);
262
263 QEXPECT_FAIL("", "Ssu::storeAuthorizedKeys() does not modify existing authorized_keys", Continue);
264 authorizedKeys.seek(0);
265 QVERIFY(authorizedKeys.readAll().split('\n').contains(testData2.trimmed()));
266
267 const QFile::Permissions go_rwx =
268 QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup |
269 QFile::ReadOther | QFile::WriteOther | QFile::ExeOther;
270 QVERIFY((QFileInfo(Sandbox::map(QDir::home().filePath(".ssh"))).permissions() & go_rwx) == 0);
271}
272
273void UrlResolverTest::checkVerifyResponse()
274{
275 QDomDocument doc("foo");
276
277 QDomElement root = doc.createElement("foo");
278 doc.appendChild(root);
279
280 QDomElement action = doc.createElement("action");
281 root.appendChild(action);
282 action.appendChild(doc.createTextNode("register"));
283
284 QDomElement deviceId = doc.createElement("deviceId");
285 root.appendChild(deviceId);
286 deviceId.appendChild(doc.createTextNode("deadbeef-dead-beef-dead"));
287
288 QDomElement protocolVersion = doc.createElement("protocolVersion");
289 root.appendChild(protocolVersion);
290
291 QDomText protocolVersionText = doc.createTextNode(SSU_PROTOCOL_VERSION);
292 protocolVersion.appendChild(protocolVersionText);
293
294 QVERIFY(ssu.verifyResponse(&doc));
295
296 protocolVersionText.setData(SSU_PROTOCOL_VERSION ".invalid");
297
298 QVERIFY2(!ssu.verifyResponse(&doc),
299 "Ssu::verifyResponse() should fail when protocolVersion does not match SSU_PROTOCOL_VERSION");
300}
Q_INVOKABLE QString flavour()
See SsuCoreConfig::flavour.
Definition ssu.cpp:156
Q_INVOKABLE QString release(bool rnd=false)
See SsuCoreConfig::release.
Definition ssu.cpp:191
#define SSU_PROTOCOL_VERSION
The ssu protocol version used by the ssu client libraries.
Definition constants.h:24