Debian Codegraph @

tests/click-hooks/online-accounts-hooks2/tst_online_accounts_hooks2.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-ui
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 <Accounts/Account>
22 #include <Accounts/Manager>
23 #include <QDebug>
24 #include <QDir>
25 #include <QDomDocument>
26 #include <QDomElement>
27 #include <QFile>
28 #include <QProcess>
29 #include <QRegularExpression>
30 #include <QSignalSpy>
31 #include <QTest>
32 #include <QTextStream>
33 #include <SignOn/Identity>
34 #include <SignOn/IdentityInfo>
35 #include <libqtdbusmock/DBusMock.h>
36 #include "fake_signond.h"
37
38 #define TEST_DIR TMP_DIR "/hooks-test2"
39
40 // map file name -> contents
41 typedef QHash<QString,QString> FileData;
42
43 class OnlineAccountsHooksTest: public QObject
44 {
45 Q_OBJECT
46
47 public:
48 OnlineAccountsHooksTest();
49
50 private Q_SLOTS:
51 void initTestCase();
52 void cleanup() {
53 if (!QTest::currentTestFailed()) {
54 clearHooksDir();
55 clearInstallDir();
56 clearPackageDir();
57 }
58 }
59 void testNoFiles();
60 void testValidHooks_data();
61 void testValidHooks();
62 void testRemoval();
63 void testRemovalWithAcl();
64 void testTimestampRemoval();
65
66 private:
67 void clearHooksDir();
68 void clearInstallDir();
69 void clearPackageDir();
70 bool runHookProcess();
71 bool runXmlDiff(const QString &generated, const QString &expected);
72 void writeHookFile(const QString &name, const QString &contents);
73 void writeInstalledFile(const QString &name, const QString &contents);
74 void writePackageFile(const QString &name,
75 const QString &contents = QString());
76 QStringList findGeneratedFiles() const;
77
78 private:
79 QtDBusTest::DBusTestRunner m_dbus;
80 QtDBusMock::DBusMock m_mock;
81 FakeSignond m_signond;
82 QByteArray m_busAddress;
83 QDir m_testDir;
84 QDir m_hooksDir;
85 QDir m_installDir;
86 QDir m_packageDir;
87 };
88
89 OnlineAccountsHooksTest::OnlineAccountsHooksTest():
90 QObject(0),
91 m_dbus(),
92 m_mock(m_dbus),
93 m_signond(&m_mock),
94 m_testDir(TEST_DIR),
95 m_hooksDir(TEST_DIR "/lomiri-online-accounts-hooks2"),
96 m_installDir(TEST_DIR "/accounts"),
97 m_packageDir(TEST_DIR "/package")
98 {
99 m_busAddress = qgetenv("DBUS_SESSION_BUS_ADDRESS");
100 }
101
102 void OnlineAccountsHooksTest::clearHooksDir()
103 {
104 m_hooksDir.removeRecursively();
105 m_hooksDir.mkpath(".");
106 }
107
108 void OnlineAccountsHooksTest::clearInstallDir()
109 {
110 m_installDir.removeRecursively();
111 m_installDir.mkpath(".");
112 }
113
114 void OnlineAccountsHooksTest::clearPackageDir()
115 {
116 m_packageDir.removeRecursively();
117 m_packageDir.mkpath(".");
118 }
119
120 bool OnlineAccountsHooksTest::runHookProcess()
121 {
122 QProcess process;
123 process.setProcessChannelMode(QProcess::ForwardedChannels);
124 process.start(HOOK_PROCESS);
125 if (!process.waitForFinished()) return false;
126
127 return process.exitCode() == EXIT_SUCCESS;
128 }
129
130 bool OnlineAccountsHooksTest::runXmlDiff(const QString &generated,
131 const QString &expected)
132 {
133 QProcess process;
134 process.setProcessChannelMode(QProcess::ForwardedChannels);
135 QStringList args;
136 args.append(generated);
137 args.append(expected);
138 process.start("xmldiff", args);
139 if (!process.waitForFinished()) return false;
140
141 int exitCode = process.exitCode();
142 if (exitCode != EXIT_SUCCESS) {
143 process.start("cat", args);
144 process.waitForFinished();
145 }
146 return exitCode == EXIT_SUCCESS;
147 }
148
149 void OnlineAccountsHooksTest::writeHookFile(const QString &name,
150 const QString &contents)
151 {
152 QFile file(m_hooksDir.filePath(name));
153 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
154 qWarning() << "Could not write file" << name;
155 return;
156 }
157
158 file.write(contents.toUtf8());
159 }
160
161 void OnlineAccountsHooksTest::writeInstalledFile(const QString &name,
162 const QString &contents)
163 {
164 QFileInfo fileInfo(name);
165 m_installDir.mkpath(fileInfo.path());
166 QFile file(m_installDir.filePath(name));
167 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
168 qWarning() << "Could not write file" << name;
169 return;
170 }
171
172 file.write(contents.toUtf8());
173 }
174
175 void OnlineAccountsHooksTest::writePackageFile(const QString &name,
176 const QString &contents)
177 {
178 QFileInfo fileInfo(name);
179 m_packageDir.mkpath(fileInfo.path());
180 QFile file(m_packageDir.filePath(name));
181 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
182 qWarning() << "Could not write file" << name;
183 return;
184 }
185
186 file.write(contents.toUtf8());
187 }
188
189 static QStringList findFiles(const QDir &dir)
190 {
191 QStringList files = dir.entryList(QDir::Files);
192
193 QStringList dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
194 Q_FOREACH(const QString &d, dirs) {
195 QStringList dirFiles = findFiles(QDir(dir.filePath(d)));
196 Q_FOREACH(const QString &file, dirFiles) {
197 files.append(dir.relativeFilePath(d + "/" + file));
198 }
199 }
200
201 return files;
202 }
203
204 QStringList OnlineAccountsHooksTest::findGeneratedFiles() const
205 {
206 return findFiles(m_installDir);
207 }
208
209 void OnlineAccountsHooksTest::initTestCase()
210 {
211 qputenv("XDG_DATA_HOME", TEST_DIR);
212 qputenv("OAH_CLICK_DIR", m_packageDir.path().toUtf8());
213 qputenv("ACCOUNTS", TEST_DIR);
214 qputenv("SSO_USE_PEER_BUS", "0");
215
216 // The hook must be able to run without a D-Bus session
217 qunsetenv("DBUS_SESSION_BUS_ADDRESS");
218
219 clearHooksDir();
220 clearInstallDir();
221 clearPackageDir();
222 }
223
224 void OnlineAccountsHooksTest::testNoFiles()
225 {
226 QVERIFY(runHookProcess());
227 QVERIFY(m_hooksDir.entryList(QDir::Files).isEmpty());
228 QVERIFY(findGeneratedFiles().isEmpty());
229 }
230
231 void OnlineAccountsHooksTest::testValidHooks_data()
232 {
233 QTest::addColumn<QString>("hookName");
234 QTest::addColumn<QString>("contents");
235 QTest::addColumn<FileData>("packageFiles");
236 QTest::addColumn<FileData>("expectedFiles");
237
238 FileData files;
239 FileData package;
240 QTest::newRow("empty file") <<
241 "com.lomiri.test_MyApp_0.1.accounts" <<
242 "" <<
243 package <<
244 files;
245
246 QTest::newRow("empty dictionary") <<
247 "com.lomiri.test_MyApp_0.1.accounts" <<
248 "{}" <<
249 package <<
250 files;
251
252 files["applications/com.lomiri.test_MyApp.application"] =
253 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
254 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
255 "<application id=\"com.lomiri.test_MyApp\">\n"
256 " <profile>com.lomiri.test_MyApp_0.2</profile>\n"
257 " <package-dir>" TEST_DIR "/package</package-dir>\n"
258 " <desktop-entry>com.lomiri.test_MyApp_0.2</desktop-entry>\n"
259 " <services>\n"
260 " <service id=\"com.lomiri.test_MyApp_google\">\n"
261 " <description>Publish to Picasa</description>\n"
262 " </service>\n"
263 " </services>\n"
264 "</application>\n";
265 files["services/com.lomiri.test_MyApp_google.service"] =
266 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
267 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
268 "<service id=\"com.lomiri.test_MyApp_google\">\n"
269 " <type>com.lomiri.test_MyApp</type>\n"
270 " <provider>google</provider>\n"
271 " <name>Picasa</name>\n"
272 " <profile>com.lomiri.test_MyApp_0.2</profile>\n"
273 " <template>\n"
274 " <setting name=\"Key\">value</setting>\n"
275 " <group name=\"a\">\n"
276 " <group name=\"b\">\n"
277 " <group name=\"c\">\n"
278 " <setting name=\"Key3\">value3</setting>\n"
279 " <setting name=\"Key4\">value4</setting>\n"
280 " </group>\n"
281 " </group>\n"
282 " </group>\n"
283 " <group name=\"other\">\n"
284 " <setting name=\"key\">value5</setting>\n"
285 " </group>\n"
286 " <group name=\"subgroup\">\n"
287 " <setting name=\"Key2\">value2</setting>\n"
288 " </group>\n"
289 " </template>\n"
290 "</service>\n";
291 QTest::newRow("one service") <<
292 "com.lomiri.test_MyApp_0.2.accounts" <<
293 "{"
294 " \"services\": ["
295 " {"
296 " \"provider\": \"google\","
297 " \"description\": \"Publish to Picasa\","
298 " \"name\": \"Picasa\","
299 " \"settings\": {"
300 " \"Key\": \"value\","
301 " \"subgroup\": {"
302 " \"Key2\": \"value2\""
303 " },"
304 " \"a/b/c\": {"
305 " \"Key3\": \"value3\","
306 " \"Key4\": \"value4\""
307 " },"
308 " \"other/key\": \"value5\""
309 " }"
310 " }"
311 " ]"
312 "}" <<
313 package <<
314 files;
315
316 files["applications/com.lomiri.test_MyApp.application"] =
317 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
318 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
319 "<application id=\"com.lomiri.test_MyApp\">\n"
320 " <profile>com.lomiri.test_MyApp_0.2</profile>\n"
321 " <package-dir>" TEST_DIR "/package</package-dir>\n"
322 " <desktop-entry>com.lomiri.test_MyApp_0.2</desktop-entry>\n"
323 " <services>\n"
324 " <service id=\"com.lomiri.test_MyApp_google\">\n"
325 " <description>.</description>\n"
326 " </service>\n"
327 " </services>\n"
328 "</application>\n";
329 files["services/com.lomiri.test_MyApp_google.service"] =
330 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
331 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
332 "<service id=\"com.lomiri.test_MyApp_google\">\n"
333 " <type>com.lomiri.test_MyApp</type>\n"
334 " <provider>google</provider>\n"
335 " <name>.</name>\n"
336 " <profile>com.lomiri.test_MyApp_0.2</profile>\n"
337 "</service>\n";
338 QTest::newRow("minimal") <<
339 "com.lomiri.test_MyApp_0.2.accounts" <<
340 "{"
341 " \"services\": ["
342 " {"
343 " \"provider\": \"google\""
344 " }"
345 " ]"
346 "}" <<
347 package <<
348 files;
349
350 files.clear();
351 files["applications/com.lomiri.test_MyApp.application"] =
352 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
353 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
354 "<application id=\"com.lomiri.test_MyApp\">\n"
355 " <profile>com.lomiri.test_MyApp_0.3</profile>\n"
356 " <package-dir>" TEST_DIR "/package</package-dir>\n"
357 " <desktop-entry>com.lomiri.test_MyApp_0.3</desktop-entry>\n"
358 " <translations>my-app</translations>\n"
359 " <services>\n"
360 " <service id=\"com.lomiri.test_MyApp_facebook\">\n"
361 " <description>Publish to Facebook</description>\n"
362 " </service>\n"
363 " <service id=\"com.lomiri.test_MyApp_google\">\n"
364 " <description>Publish to Picasa</description>\n"
365 " </service>\n"
366 " </services>\n"
367 "</application>\n";
368 files["services/com.lomiri.test_MyApp_google.service"] =
369 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
370 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
371 "<service id=\"com.lomiri.test_MyApp_google\">\n"
372 " <type>com.lomiri.test_MyApp</type>\n"
373 " <provider>google</provider>\n"
374 " <name>Picasa</name>\n"
375 " <profile>com.lomiri.test_MyApp_0.3</profile>\n"
376 " <translations>my-app</translations>\n"
377 "</service>\n";
378 files["services/com.lomiri.test_MyApp_facebook.service"] =
379 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
380 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
381 "<service id=\"com.lomiri.test_MyApp_facebook\">\n"
382 " <type>com.lomiri.test_MyApp</type>\n"
383 " <provider>facebook</provider>\n"
384 " <name>Facebook photos</name>\n"
385 " <profile>com.lomiri.test_MyApp_0.3</profile>\n"
386 " <translations>my-app</translations>\n"
387 "</service>\n";
388 QTest::newRow("two services") <<
389 "com.lomiri.test_MyApp_0.3.accounts" <<
390 "{"
391 " \"translations\": \"my-app\","
392 " \"services\": ["
393 " {"
394 " \"provider\": \"facebook\","
395 " \"description\": \"Publish to Facebook\","
396 " \"name\": \"Facebook photos\""
397 " },"
398 " {"
399 " \"provider\": \"google\","
400 " \"description\": \"Publish to Picasa\","
401 " \"name\": \"Picasa\""
402 " }"
403 " ]"
404 "}" <<
405 package <<
406 files;
407
408 files.clear();
409 files["applications/com.lomiri.test_MyApp2.application"] =
410 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
411 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
412 "<application id=\"com.lomiri.test_MyApp2\">\n"
413 " <profile>com.lomiri.test_MyApp2_0.2</profile>\n"
414 " <package-dir>" TEST_DIR "/package</package-dir>\n"
415 " <desktop-entry>com.lomiri.test_MyApp2_0.2</desktop-entry>\n"
416 " <services>\n"
417 " <service id=\"com.lomiri.test_MyApp2_google\">\n"
418 " <description>Publish to Picasa</description>\n"
419 " </service>\n"
420 " </services>\n"
421 "</application>\n";
422 files["services/com.lomiri.test_MyApp2_google.service"] =
423 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
424 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
425 "<service id=\"com.lomiri.test_MyApp2_google\">\n"
426 " <type>com.lomiri.test_MyApp2</type>\n"
427 " <provider>google</provider>\n"
428 " <name>Picasa</name>\n"
429 " <profile>com.lomiri.test_MyApp2_0.2</profile>\n"
430 " <template>\n"
431 " <group name=\"auth\">\n"
432 " <setting name=\"method\">oauth2</setting>\n"
433 " <setting name=\"mechanism\">web_server</setting>\n"
434 " <group name=\"oauth2\">\n"
435 " <group name=\"web_server\">\n"
436 " <setting name=\"ClientId\">foo</setting>\n"
437 " <setting name=\"ClientSecret\">bar</setting>\n"
438 " <setting name=\"Scopes\" type=\"as\">['one scope','and another']</setting>\n"
439 " <setting name=\"UseSSL\" type=\"b\">false</setting>\n"
440 " </group>\n"
441 " </group>\n"
442 " </group>\n"
443 " </template>\n"
444 "</service>\n";
445 QTest::newRow("one service, with auth data") <<
446 "com.lomiri.test_MyApp2_0.2.accounts" <<
447 "{"
448 " \"services\": ["
449 " {"
450 " \"provider\": \"google\","
451 " \"description\": \"Publish to Picasa\","
452 " \"name\": \"Picasa\","
453 " \"auth\": {"
454 " \"oauth2/web_server\": {"
455 " \"ClientId\": \"foo\","
456 " \"ClientSecret\": \"bar\","
457 " \"UseSSL\": false,"
458 " \"Scopes\": [\"one scope\",\"and another\"]"
459 " }"
460 " }"
461 " }"
462 " ]"
463 "}" <<
464 package <<
465 files;
466
467 package["myapp/Main.qml"] = "Something here";
468 package["google.svg"] = "icon contents";
469 files.clear();
470 files["applications/com.lomiri.test_MyApp.application"] =
471 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
472 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
473 "<application id=\"com.lomiri.test_MyApp\">\n"
474 " <profile>com.lomiri.test_MyApp_0.2</profile>\n"
475 " <package-dir>" TEST_DIR "/package</package-dir>\n"
476 " <desktop-entry>com.lomiri.test_MyApp_0.2</desktop-entry>\n"
477 " <services>\n"
478 " <service id=\"com.lomiri.test_MyApp_google\">\n"
479 " <description>.</description>\n"
480 " </service>\n"
481 " </services>\n"
482 "</application>\n";
483 files["services/com.lomiri.test_MyApp_google.service"] =
484 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
485 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
486 "<service id=\"com.lomiri.test_MyApp_google\">\n"
487 " <type>com.lomiri.test_MyApp</type>\n"
488 " <provider>google</provider>\n"
489 " <name>.</name>\n"
490 " <profile>com.lomiri.test_MyApp_0.2</profile>\n"
491 "</service>\n";
492 files["providers/com.lomiri.test_MyApp.provider"] =
493 QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
494 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
495 "<provider id=\"com.lomiri.test_MyApp\">\n"
496 " <name>Google</name>\n"
497 " <icon>%1/google.svg</icon>\n"
498 " <profile>com.lomiri.test_MyApp_0.2</profile>\n"
499 " <package-dir>" TEST_DIR "/package</package-dir>\n"
500 "</provider>\n").arg(m_packageDir.path());
501 files["qml-plugins/com.lomiri.test_MyApp/Main.qml"] =
502 "Something here";
503 QTest::newRow("account plugin, icon file") <<
504 "com.lomiri.test_MyApp_0.2.accounts" <<
505 "{"
506 " \"services\": ["
507 " {"
508 " \"provider\": \"google\""
509 " }"
510 " ],"
511 " \"plugin\": {"
512 " \"name\": \"Google\","
513 " \"icon\": \"google.svg\","
514 " \"qml\": \"myapp\""
515 " }"
516 "}" <<
517 package <<
518 files;
519
520 files.clear();
521 package.clear();
522 package["myapp/Main.qml"] = "Something herez";
523 files["applications/com.lomiri.test_MyApp.application"] =
524 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
525 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
526 "<application id=\"com.lomiri.test_MyApp\">\n"
527 " <profile>com.lomiri.test_MyApp_0.3</profile>\n"
528 " <package-dir>" TEST_DIR "/package</package-dir>\n"
529 " <desktop-entry>com.lomiri.test_MyApp_0.3</desktop-entry>\n"
530 " <services>\n"
531 " <service id=\"com.lomiri.test_MyApp_abc\">\n"
532 " <description>.</description>\n"
533 " </service>\n"
534 " </services>\n"
535 "</application>\n";
536 files["services/com.lomiri.test_MyApp_abc.service"] =
537 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
538 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
539 "<service id=\"com.lomiri.test_MyApp_abc\">\n"
540 " <type>com.lomiri.test_MyApp</type>\n"
541 " <provider>abc</provider>\n"
542 " <name>.</name>\n"
543 " <profile>com.lomiri.test_MyApp_0.3</profile>\n"
544 "</service>\n";
545 files["providers/com.lomiri.test_MyApp.provider"] =
546 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
547 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
548 "<provider id=\"com.lomiri.test_MyApp\">\n"
549 " <name>A B C</name>\n"
550 " <icon>abc.svg</icon>\n"
551 " <profile>com.lomiri.test_MyApp_0.3</profile>\n"
552 " <package-dir>" TEST_DIR "/package</package-dir>\n"
553 "</provider>\n";
554 files["qml-plugins/com.lomiri.test_MyApp/Main.qml"] =
555 "Something herez";
556 QTest::newRow("account plugin, stock icon") <<
557 "com.lomiri.test_MyApp_0.3.accounts" <<
558 "{"
559 " \"services\": ["
560 " {"
561 " \"provider\": \"abc\""
562 " }"
563 " ],"
564 " \"plugin\": {"
565 " \"name\": \"A B C\","
566 " \"icon\": \"abc.svg\","
567 " \"qml\": \"myapp\""
568 " }"
569 "}" <<
570 package <<
571 files;
572
573 files.clear();
574 files["applications/com.lomiri.test_MyScope.application"] =
575 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
576 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
577 "<application id=\"com.lomiri.test_MyScope\">\n"
578 " <profile>com.lomiri.test_MyScope_0.3</profile>\n"
579 " <package-dir>" TEST_DIR "/package</package-dir>\n"
580 " <desktop-entry>com.lomiri.test_MyScope</desktop-entry>\n"
581 " <services>\n"
582 " <service id=\"com.lomiri.test_MyScope_google\">\n"
583 " <description>.</description>\n"
584 " </service>\n"
585 " </services>\n"
586 "</application>\n";
587 files["services/com.lomiri.test_MyScope_google.service"] =
588 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
589 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
590 "<service id=\"com.lomiri.test_MyScope_google\">\n"
591 " <type>com.lomiri.test_MyScope</type>\n"
592 " <provider>google</provider>\n"
593 " <name>.</name>\n"
594 " <profile>com.lomiri.test_MyScope_0.3</profile>\n"
595 "</service>\n";
596 QTest::newRow("minimal") <<
597 "com.lomiri.test_MyScope_0.3.accounts" <<
598 "{"
599 " \"scope\": true,"
600 " \"services\": ["
601 " {"
602 " \"provider\": \"google\""
603 " }"
604 " ]"
605 "}" <<
606 package <<
607 files;
608 }
609
610 void OnlineAccountsHooksTest::testValidHooks()
611 {
612 QFETCH(QString, hookName);
613 QFETCH(QString, contents);
614 QFETCH(FileData, packageFiles);
615 QFETCH(FileData, expectedFiles);
616
617 writeHookFile(hookName, contents);
618 for (FileData::const_iterator i = packageFiles.constBegin();
619 i != packageFiles.constEnd(); i++) {
620 writePackageFile(i.key(), i.value());
621 }
622 QVERIFY(runHookProcess());
623
624 QStringList genFiles = findGeneratedFiles();
625 QStringList expFilesKeys = expectedFiles.keys();
626 QCOMPARE(QSet<QString>(genFiles.constBegin(), genFiles.constEnd()), QSet<QString>(expFilesKeys.constBegin(), expFilesKeys.constEnd()));
627
628 QStringList xmlSuffixes;
629 xmlSuffixes << "provider" << "service" << "application";
630 for (FileData::const_iterator i = expectedFiles.constBegin();
631 i != expectedFiles.constEnd(); i++) {
632 QFileInfo generatedFile(m_installDir.filePath(i.key()));
633 if (xmlSuffixes.contains(generatedFile.suffix())) {
634 /* Dump the expected XML into a file */
635 QString expectedXml = generatedFile.filePath() + ".expected";
636 QFile file(expectedXml);
637 QVERIFY(file.open(QIODevice::WriteOnly));
638 QByteArray expectedContents = i.value().toUtf8();
639 QCOMPARE(file.write(expectedContents), expectedContents.length());
640 file.close();
641
642 QVERIFY(runXmlDiff(generatedFile.filePath(), expectedXml));
643 } else {
644 // direct comparison
645 QFile file(generatedFile.filePath());
646 QVERIFY(file.open(QIODevice::ReadOnly));
647 QByteArray expectedContents = i.value().toUtf8();
648 QCOMPARE(file.readAll(), expectedContents);
649 }
650 }
651 }
652
653 void OnlineAccountsHooksTest::testRemoval()
654 {
655 QString stillInstalled("applications/com.lomiri.test_StillInstalled.application");
656 writeInstalledFile(stillInstalled,
657 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
658 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
659 "<application>\n"
660 " <description>My application</description>\n"
661 " <service-types>\n"
662 " <service-type>some type</service-type>\n"
663 " </service-types>\n"
664 " <profile>com-lomiri.test_StillInstalled_2.0</profile>\n"
665 "</application>");
666 QVERIFY(m_installDir.exists(stillInstalled));
667
668 QString myApp("applications/com.lomiri.test_MyApp.application");
669 writeInstalledFile(myApp,
670 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
671 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
672 "<application>\n"
673 " <description>My application</description>\n"
674 " <services>\n"
675 " <service id=\"com-lomiri.test_MyApp_example\">\n"
676 " <description>Publish somewhere</description>\n"
677 " </service>\n"
678 " </services>\n"
679 " <profile>com-lomiri.test_MyApp_3.0</profile>\n"
680 "</application>");
681 QVERIFY(m_installDir.exists(myApp));
682
683 QString myService("services/com.lomiri.test_MyApp_example.service");
684 writeInstalledFile(myService,
685 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
686 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
687 "<service>\n"
688 " <name>Hello world</name>\n"
689 " <provider>example</provider>\n"
690 " <description>My application</description>\n"
691 " <profile>com-lomiri.test_MyApp_3.0</profile>\n"
692 "</service>");
693 QVERIFY(m_installDir.exists(myService));
694
695 QString noProfile("applications/com.lomiri.test_NoProfile.application");
696 writeInstalledFile(noProfile,
697 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
698 "<application>\n"
699 " <description>My application</description>\n"
700 " <service-types>\n"
701 " <service-type>some type</service-type>\n"
702 " </service-types>\n"
703 "</application>");
704 QVERIFY(m_installDir.exists(noProfile));
705
706 QString notOurs("applications/com.lomiri.test_NotOurs.application");
707 writeInstalledFile(notOurs,
708 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
709 "<!--this file is auto-generated by lomiri-online-accounts-hooks; do not modify-->\n"
710 "<application>\n"
711 " <description>My application</description>\n"
712 " <service-types>\n"
713 " <service-type>some type</service-type>\n"
714 " </service-types>\n"
715 " <profile>com-lomiri.test_NotOurs_0.1.0</profile>\n"
716 "</application>");
717 QVERIFY(m_installDir.exists(notOurs));
718
719 writeHookFile("com-lomiri.test_StillInstalled_2.0.accounts",
720 "{"
721 " \"services\": ["
722 " {"
723 " \"provider\": \"example\","
724 " \"description\": \"Publish somewhere\","
725 " \"name\": \"Hello world\""
726 " }"
727 " ]"
728 "}");
729
730 QVERIFY(runHookProcess());
731
732 QVERIFY(m_installDir.exists(stillInstalled));
733 QVERIFY(!m_installDir.exists(myApp));
734 QVERIFY(!m_installDir.exists(myService));
735 QVERIFY(m_installDir.exists(noProfile));
736 QVERIFY(m_installDir.exists(notOurs));
737 }
738
739 void OnlineAccountsHooksTest::testRemovalWithAcl()
740 {
741 qputenv("DBUS_SESSION_BUS_ADDRESS", m_busAddress);
742 m_dbus.startServices();
743
744 QString myApp("applications/com.lomiri.test_MyAcl.application");
745 writeInstalledFile(myApp,
746 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
747 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
748 "<application id=\"com-lomiri.test_MyAcl\">\n"
749 " <description>My application</description>\n"
750 " <services>\n"
751 " <service id=\"com-lomiri.test_MyAcl_example\">\n"
752 " <description>Publish somewhere</description>\n"
753 " </service>\n"
754 " </services>\n"
755 " <profile>com-lomiri.test_MyAcl_3.0</profile>\n"
756 "</application>");
757 QVERIFY(m_installDir.exists(myApp));
758
759 QString myService("services/com.lomiri.test_MyAcl_example.service");
760 writeInstalledFile(myService,
761 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
762 "<!--this file is auto-generated by lomiri-online-accounts-hooks2; do not modify-->\n"
763 "<service id=\"com-lomiri.test_MyAcl_example\">\n"
764 " <name>Hello world</name>\n"
765 " <type>com-lomiri.test_MyAcl</type>\n"
766 " <provider>example</provider>\n"
767 " <description>My application</description>\n"
768 " <profile>com-lomiri.test_MyAcl_3.0</profile>\n"
769 "</service>");
770 QVERIFY(m_installDir.exists(myService));
771
772 /* Create an account, enable the app and add it to the ACL */
773 Accounts::Manager manager;
774 Accounts::Service service =
775 manager.service("com.lomiri.test_MyAcl_example");
776 QVERIFY(service.isValid());
777 Accounts::Account *account = manager.createAccount("example");
778 account->setDisplayName("Example account");
779 account->setEnabled(true);
780 account->setCredentialsId(25);
781 account->selectService(service);
782 account->setEnabled(true);
783 account->syncAndBlock();
784 Accounts::AccountId accountId = account->id();
785 QVERIFY(accountId > 0);
786
787 QVariantMap initialInfo;
788 QStringList acls;
789 acls << "one" << "com-lomiri.test_MyAcl_0.1" << "two_click";
790 QVariantList initialAccessControlList;
791 for (const QString &acl: acls) {
792 initialAccessControlList.append(acl);
793 }
794 initialInfo["ACL"] = initialAccessControlList;
795 initialInfo["Id"] = 25;
796 m_signond.addIdentity(25, initialInfo);
797
798 /* Now run the hook process; it should delete the .service and .application
799 * files, and also disable the service and remove the app from the ACL */
800 QVERIFY(runHookProcess());
801
802 QVERIFY(!m_installDir.exists(myApp));
803 QVERIFY(!m_installDir.exists(myService));
804 QTRY_COMPARE(account->isEnabled(), false);
805
806 SignOn::Identity *identity = SignOn::Identity::existingIdentity(25, this);
807 QSignalSpy gotInfo(identity, SIGNAL(info(const SignOn::IdentityInfo&)));
808 identity->queryInfo();
809 QTRY_COMPARE(gotInfo.count(), 1);
810
811 SignOn::IdentityInfo info =
812 gotInfo.at(0).at(0).value<SignOn::IdentityInfo>();
813 QStringList infoAcl = info.accessControlList();
814 QStringList expectedAcl;
815 expectedAcl << "one" << "two_click";
816 QCOMPARE(QSet<QString>(infoAcl.constBegin(), infoAcl.constEnd()), QSet<QString>(expectedAcl.constBegin(), expectedAcl.constEnd()));
817 }
818
819 void OnlineAccountsHooksTest::testTimestampRemoval()
820 {
821 QString stillInstalled("com-lomiri.test_MyApp_2.0.accounts");
822 writeHookFile(stillInstalled, "{}");
823 QString stillInstalledTimestamp("com-lomiri.test_MyApp_2.0.accounts.processed");
824 writeHookFile(stillInstalledTimestamp, "");
825 QString oldTimestamp("com-lomiri.test_MyApp_1.0.accounts.processed");
826 writeHookFile(oldTimestamp, "");
827 QString staleTimestamp1("com-lomiri.app_MyOtherApp_2.0.accounts.processed");
828 writeHookFile(staleTimestamp1, "");
829 QString staleTimestamp2("com-lomiri.app_MyOtherApp_1.0.accounts.processed");
830 writeHookFile(staleTimestamp2, "");
831
832 QVERIFY(runHookProcess());
833
834 QVERIFY(m_hooksDir.exists(stillInstalled));
835 QVERIFY(m_hooksDir.exists(stillInstalledTimestamp));
836 QVERIFY(!m_hooksDir.exists(oldTimestamp));
837 QVERIFY(!m_hooksDir.exists(staleTimestamp1));
838 QVERIFY(!m_hooksDir.exists(staleTimestamp2));
839 }
840
841 QTEST_GUILESS_MAIN(OnlineAccountsHooksTest);
842
843 #include "tst_online_accounts_hooks2.moc"