| 1 | /* |
| 2 | * Copyright (C) 2013 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/debug.h> |
| 22 | #include "libaccounts-service.h" |
| 23 | |
| 24 | #include <Accounts/Account> |
| 25 | #include <Accounts/Manager> |
| 26 | #include <Accounts/Service> |
| 27 | #include <QDBusConnection> |
| 28 | #include <QDebug> |
| 29 | #include <QProcess> |
| 30 | #include <QSignalSpy> |
| 31 | #include <QString> |
| 32 | #include <QTest> |
| 33 | |
| 34 | using namespace OnlineAccountsUi; |
| 35 | |
| 36 | #define TEST_SERVICE_NAME \ |
| 37 | "com.lomiri.OnlineAccountsUi.LibaccountsService.Test" |
| 38 | #define TEST_OBJECT_PATH "/" |
| 39 | |
| 40 | class LibaccountsServiceTest: public QObject |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | |
| 44 | public: |
| 45 | LibaccountsServiceTest(); |
| 46 | |
| 47 | private: |
| 48 | QProcess *requestStore(const QStringList &args, bool showError = false) { |
| 49 | QString command = QStringLiteral("gdbus"); |
| 50 | QStringList combinedArgs = { "call", "--session", |
| 51 | "--dest", TEST_SERVICE_NAME, |
| 52 | "--object-path", TEST_OBJECT_PATH, |
| 53 | "--method", "com.google.code.AccountsSSO.Accounts.Manager.store" }; |
| 54 | combinedArgs.append(args); |
| 55 | QProcess *process = new QProcess(this); |
| 56 | if (showError) { |
| 57 | process->setProcessChannelMode(QProcess::ForwardedErrorChannel); |
| 58 | } |
| 59 | process->start(command, combinedArgs); |
| 60 | process->waitForStarted(); |
| 61 | return process; |
| 62 | } |
| 63 | |
| 64 | private Q_SLOTS: |
| 65 | void init(); |
| 66 | void testProfile_data(); |
| 67 | void testProfile(); |
| 68 | void testFailure(); |
| 69 | void testAccount_data(); |
| 70 | void testAccount(); |
| 71 | void testSettings_data(); |
| 72 | void testSettings(); |
| 73 | |
| 74 | private: |
| 75 | LibaccountsService m_service; |
| 76 | }; |
| 77 | |
| 78 | /* Mocking libaccounts-qt { */ |
| 79 | class ManagerController { |
| 80 | public: |
| 81 | ManagerController(): lastLoadedAccount(0) { m_instance = this; } |
| 82 | ~ManagerController() { m_instance = 0; } |
| 83 | static ManagerController *instance() { return m_instance; } |
| 84 | |
| 85 | void setServices(const QStringList &services) { m_services = services; } |
| 86 | |
| 87 | public: |
| 88 | Accounts::Account *lastLoadedAccount; |
| 89 | |
| 90 | private: |
| 91 | friend class Accounts::Manager; |
| 92 | QStringList m_services; |
| 93 | static ManagerController *m_instance; |
| 94 | }; |
| 95 | |
| 96 | ManagerController *ManagerController::m_instance = 0; |
| 97 | |
| 98 | typedef QHash<QString,QVariantMap> ServiceSettings; |
| 99 | typedef QHash<QString,QSet<QString> > RemovedKeys; |
| 100 | |
| 101 | class AccountController: public QObject { |
| 102 | Q_OBJECT |
| 103 | public: |
| 104 | AccountController(Accounts::Account *account): |
| 105 | QObject(account), |
| 106 | m_id(0), |
| 107 | m_wasDeleted(false), |
| 108 | m_syncWasCalled(false), |
| 109 | m_account(account) { |
| 110 | m_controllers[account] = this; |
| 111 | } |
| 112 | |
| 113 | static AccountController *mock(Accounts::Account *account) { |
| 114 | return m_controllers[account]; |
| 115 | } |
| 116 | |
| 117 | bool syncWasCalled() const { return m_syncWasCalled; } |
| 118 | void doSync(Accounts::Error error = Accounts::Error()) { |
| 119 | if (error.type() == Accounts::Error::NoError) { |
| 120 | QMetaObject::invokeMethod(m_account, "synced", Qt::QueuedConnection); |
| 121 | } else { |
| 122 | QMetaObject::invokeMethod(m_account, "error", Qt::QueuedConnection, |
| 123 | Q_ARG(Accounts::Error, error)); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | protected: |
| 128 | void syncCalled() { m_syncWasCalled = true; } |
| 129 | |
| 130 | public: |
| 131 | quint32 m_id; |
| 132 | QString m_provider; |
| 133 | ServiceSettings m_serviceSettings; |
| 134 | RemovedKeys m_removedKeys; |
| 135 | bool m_wasDeleted; |
| 136 | bool m_syncWasCalled; |
| 137 | private: |
| 138 | friend class Accounts::Account; |
| 139 | static QHash<Accounts::Account *,AccountController*> m_controllers; |
| 140 | Accounts::Account *m_account; |
| 141 | }; |
| 142 | |
| 143 | QHash<Accounts::Account *,AccountController*> AccountController::m_controllers; |
| 144 | |
| 145 | struct _AgService { |
| 146 | _AgService(const QString &name): name(name) {} |
| 147 | QString name; |
| 148 | }; |
| 149 | |
| 150 | namespace Accounts { |
| 151 | |
| 152 | Service::Service(): |
| 153 | m_service(0), |
| 154 | m_tags(0) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | Service::Service(AgService *service, ReferenceMode): |
| 159 | m_service(service), |
| 160 | m_tags(0) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | Service::Service(const Service &other): |
| 165 | m_service(other.m_service ? new _AgService(other.m_service->name) : 0), |
| 166 | m_tags(0) |
| 167 | { |
| 168 | } |
| 169 | |
| 170 | Service &Service::operator=(const Service &other) |
| 171 | { |
| 172 | delete m_service; |
| 173 | m_service = other.m_service ? new _AgService(other.m_service->name) : 0; |
| 174 | return *this; |
| 175 | } |
| 176 | |
| 177 | Service::~Service() |
| 178 | { |
| 179 | delete m_service; |
| 180 | } |
| 181 | |
| 182 | bool Service::isValid() const |
| 183 | { |
| 184 | return m_service != 0; |
| 185 | } |
| 186 | |
| 187 | class Account::Private { |
| 188 | public: |
| 189 | Private() {} |
| 190 | |
| 191 | QString m_selectedService; |
| 192 | AccountController *m_controller; |
| 193 | }; |
| 194 | |
| 195 | Account::Account(Private *d, QObject *parent): |
| 196 | QObject(parent), |
| 197 | d(d) |
| 198 | { |
| 199 | d->m_controller = new AccountController(this); |
| 200 | } |
| 201 | |
| 202 | Account::~Account() |
| 203 | { |
| 204 | delete d; |
| 205 | } |
| 206 | |
| 207 | AccountId Account::id() const |
| 208 | { |
| 209 | return d->m_controller->m_id; |
| 210 | } |
| 211 | |
| 212 | void Account::selectService(const Service &service) |
| 213 | { |
| 214 | d->m_selectedService = service.m_service ? |
| 215 | service.m_service->name : QString(); |
| 216 | } |
| 217 | |
| 218 | void Account::setValue(const QString &key, const QVariant &value) |
| 219 | { |
| 220 | QVariantMap &settings = |
| 221 | d->m_controller->m_serviceSettings[d->m_selectedService]; |
| 222 | settings.insert(key, value); |
| 223 | } |
| 224 | |
| 225 | void Account::remove(const QString &key) |
| 226 | { |
| 227 | QSet<QString> &removedKeys = |
| 228 | d->m_controller->m_removedKeys[d->m_selectedService]; |
| 229 | removedKeys.insert(key); |
| 230 | } |
| 231 | |
| 232 | void Account::sync() |
| 233 | { |
| 234 | d->m_controller->syncCalled(); |
| 235 | } |
| 236 | |
| 237 | void Account::remove() |
| 238 | { |
| 239 | d->m_controller->m_wasDeleted = true; |
| 240 | } |
| 241 | |
| 242 | Watch::~Watch() |
| 243 | { |
| 244 | } |
| 245 | |
| 246 | class Manager::Private { |
| 247 | public: |
| 248 | Private() {} |
| 249 | |
| 250 | ManagerController m_controller; |
| 251 | }; |
| 252 | |
| 253 | Manager::Manager(QObject *parent): |
| 254 | QObject(parent), |
| 255 | d(new Private()) |
| 256 | { |
| 257 | } |
| 258 | |
| 259 | Manager::~Manager() |
| 260 | { |
| 261 | delete d; |
| 262 | } |
| 263 | |
| 264 | Account *Manager::account(const AccountId &id) const |
| 265 | { |
| 266 | Account::Private *accountD = new Account::Private(); |
| 267 | d->m_controller.lastLoadedAccount = |
| 268 | new Account(accountD, const_cast<Manager*>(this)); |
| 269 | accountD->m_controller->m_id = id; |
| 270 | return d->m_controller.lastLoadedAccount; |
| 271 | } |
| 272 | |
| 273 | Account *Manager::createAccount(const QString &providerName) |
| 274 | { |
| 275 | Account::Private *accountD = new Account::Private(); |
| 276 | d->m_controller.lastLoadedAccount = new Account(accountD, this); |
| 277 | accountD->m_controller->m_provider = providerName; |
| 278 | return d->m_controller.lastLoadedAccount; |
| 279 | } |
| 280 | |
| 281 | Service Manager::service(const QString &serviceName) const |
| 282 | { |
| 283 | if (d->m_controller.m_services.contains(serviceName)) { |
| 284 | return Service(new _AgService(serviceName)); |
| 285 | } else { |
| 286 | return Service(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | } // namespace |
| 291 | |
| 292 | /* } mocking libaccounts-qt */ |
| 293 | |
| 294 | /* mocking utils.cpp { */ |
| 295 | namespace OnlineAccountsUi { |
| 296 | |
| 297 | static QString staticApparmorProfile; |
| 298 | |
| 299 | QString apparmorProfileOfPeer(const QDBusMessage &) |
| 300 | { |
| 301 | return staticApparmorProfile; |
| 302 | } |
| 303 | |
| 304 | void setApparmorProfile(const QString &profile) |
| 305 | { |
| 306 | staticApparmorProfile = profile; |
| 307 | } |
| 308 | |
| 309 | } // namespace |
| 310 | /* } mocking utils.cpp */ |
| 311 | |
| 312 | Q_DECLARE_METATYPE(QProcess::ExitStatus) |
| 313 | |
| 314 | LibaccountsServiceTest::LibaccountsServiceTest(): |
| 315 | QObject(0) |
| 316 | { |
| 317 | QDBusConnection conn = QDBusConnection::sessionBus(); |
| 318 | conn.registerService(TEST_SERVICE_NAME); |
| 319 | conn.registerObject(TEST_OBJECT_PATH, &m_service, |
| 320 | QDBusConnection::ExportAllContents); |
| 321 | |
| 322 | qRegisterMetaType<QProcess::ExitStatus>(); |
| 323 | setLoggingLevel(2); |
| 324 | } |
| 325 | |
| 326 | void LibaccountsServiceTest::init() |
| 327 | { |
| 328 | ManagerController *mc = ManagerController::instance(); |
| 329 | mc->lastLoadedAccount = 0; |
| 330 | } |
| 331 | |
| 332 | void LibaccountsServiceTest::testProfile_data() |
| 333 | { |
| 334 | QTest::addColumn<QString>("profile"); |
| 335 | QTest::addColumn<QString>("provider"); |
| 336 | QTest::addColumn<bool>("mustPass"); |
| 337 | |
| 338 | QTest::newRow("non click, mismatch") << |
| 339 | "appProfile" << "OneProvider" << false; |
| 340 | |
| 341 | QTest::newRow("non click, match") << |
| 342 | "theProfile" << "theProfile" << true; |
| 343 | |
| 344 | QTest::newRow("click, mismatch") << |
| 345 | "com.lomiri.package_app_0.1" << "com.lomiri.package_other" << false; |
| 346 | |
| 347 | QTest::newRow("click, match") << |
| 348 | "com.lomiri.package_app_0.2" << "com.lomiri.package_app" << true; |
| 349 | } |
| 350 | |
| 351 | void LibaccountsServiceTest::testProfile() |
| 352 | { |
| 353 | QFETCH(QString, profile); |
| 354 | QFETCH(QString, provider); |
| 355 | QFETCH(bool, mustPass); |
| 356 | |
| 357 | setApparmorProfile(profile); |
| 358 | QStringList params = { "0", "true", "false", provider, "[]" }; |
| 359 | QProcess *client = requestStore(params); |
| 360 | QSignalSpy finished(client, SIGNAL(finished(int,QProcess::ExitStatus))); |
| 361 | |
| 362 | if (mustPass) { |
| 363 | ManagerController *mc = ManagerController::instance(); |
| 364 | QTRY_VERIFY(mc->lastLoadedAccount != 0); |
| 365 | AccountController *ac = AccountController::mock(mc->lastLoadedAccount); |
| 366 | QTRY_COMPARE(ac->syncWasCalled(), true); |
| 367 | ac->doSync(); |
| 368 | } |
| 369 | |
| 370 | finished.wait(); |
| 371 | |
| 372 | QByteArray stdErr = client->readAllStandardError(); |
| 373 | QCOMPARE(stdErr.contains("Profile/provider mismatch"), !mustPass); |
| 374 | } |
| 375 | |
| 376 | void LibaccountsServiceTest::testFailure() |
| 377 | { |
| 378 | setApparmorProfile("MyProvider"); |
| 379 | QStringList params = { "0", "true", "false", "MyProvider", "[]" }; |
| 380 | QProcess *client = requestStore(params); |
| 381 | QSignalSpy finished(client, SIGNAL(finished(int,QProcess::ExitStatus))); |
| 382 | |
| 383 | ManagerController *mc = ManagerController::instance(); |
| 384 | QTRY_VERIFY(mc->lastLoadedAccount != 0); |
| 385 | AccountController *ac = AccountController::mock(mc->lastLoadedAccount); |
| 386 | QTRY_COMPARE(ac->syncWasCalled(), true); |
| 387 | |
| 388 | // return an error |
| 389 | Accounts::Error error(Accounts::Error::Database, "hi there"); |
| 390 | ac->doSync(error); |
| 391 | finished.wait(); |
| 392 | |
| 393 | QVERIFY(client->readAllStandardError().contains("hi there")); |
| 394 | } |
| 395 | |
| 396 | void LibaccountsServiceTest::testAccount_data() |
| 397 | { |
| 398 | QTest::addColumn<QStringList>("params"); |
| 399 | QTest::addColumn<QString>("provider"); |
| 400 | QTest::addColumn<quint32>("accountId"); |
| 401 | QTest::addColumn<bool>("deleted"); |
| 402 | |
| 403 | QTest::newRow("new account") << |
| 404 | (QStringList({ "0", "true", "false", "Cool" })) << |
| 405 | "Cool" << quint32(0) << false; |
| 406 | |
| 407 | QTest::newRow("existing account") << |
| 408 | (QStringList({ "5", "false", "false", "Bad" })) << |
| 409 | "Bad" << quint32(5) << false; |
| 410 | |
| 411 | QTest::newRow("deleting account") << |
| 412 | (QStringList({ "7", "false", "true", "Die" })) << |
| 413 | "Die" << quint32(7) << true; |
| 414 | } |
| 415 | |
| 416 | void LibaccountsServiceTest::testAccount() |
| 417 | { |
| 418 | QFETCH(QStringList, params); |
| 419 | QFETCH(QString, provider); |
| 420 | QFETCH(quint32, accountId); |
| 421 | QFETCH(bool, deleted); |
| 422 | |
| 423 | setApparmorProfile(provider); |
| 424 | |
| 425 | ManagerController *mc = ManagerController::instance(); |
| 426 | |
| 427 | params.append("[]"); |
| 428 | QProcess *client = requestStore(params, true); |
| 429 | QSignalSpy finished(client, SIGNAL(finished(int,QProcess::ExitStatus))); |
| 430 | |
| 431 | QTRY_VERIFY(mc->lastLoadedAccount != 0); |
| 432 | AccountController *ac = AccountController::mock(mc->lastLoadedAccount); |
| 433 | QTRY_COMPARE(ac->syncWasCalled(), true); |
| 434 | |
| 435 | QCOMPARE(ac->m_id, accountId); |
| 436 | if (accountId == 0) { |
| 437 | QCOMPARE(ac->m_provider, provider); |
| 438 | } |
| 439 | QCOMPARE(ac->m_wasDeleted, deleted); |
| 440 | |
| 441 | ac->doSync(); |
| 442 | |
| 443 | finished.wait(); |
| 444 | } |
| 445 | |
| 446 | void LibaccountsServiceTest::testSettings_data() |
| 447 | { |
| 448 | QTest::addColumn<QString>("clientSettings"); |
| 449 | QTest::addColumn<ServiceSettings>("settings"); |
| 450 | QTest::addColumn<RemovedKeys>("removedKeys"); |
| 451 | |
| 452 | QHash<QString,QVariantMap> settings; |
| 453 | QHash<QString,QSet<QString> > removedKeys; |
| 454 | |
| 455 | QTest::newRow("no services") << |
| 456 | "[]" << |
| 457 | settings << removedKeys; |
| 458 | QTest::newRow("no settings") << |
| 459 | "[('cool', 'type', 3, {}, [])]" << |
| 460 | settings << removedKeys; |
| 461 | |
| 462 | settings["cool"].insert("enabled", false); |
| 463 | settings["cool"].insert("name", QString("Bob")); |
| 464 | QTest::newRow("some keys changed") << |
| 465 | "[('cool', 'type', 3, {'enabled': <false>, 'name': <'Bob'>}, [])]" << |
| 466 | settings << removedKeys; |
| 467 | settings.clear(); |
| 468 | |
| 469 | removedKeys["cool"].insert("enabled"); |
| 470 | removedKeys["cool"].insert("id"); |
| 471 | QTest::newRow("some keys removed") << |
| 472 | "[('cool', 'type', 3, {}, ['enabled','id'])]" << |
| 473 | settings << removedKeys; |
| 474 | removedKeys.clear(); |
| 475 | |
| 476 | settings["cool"].insert("enabled", true); |
| 477 | settings["cool"].insert("name", QString("Tom")); |
| 478 | removedKeys["cool"].insert("id"); |
| 479 | settings["bad"].insert("port", quint32(4000)); |
| 480 | removedKeys["bad"].insert("name"); |
| 481 | QTest::newRow("two services, lots of changes") << |
| 482 | "[" |
| 483 | "('cool', 'type', 3, {'enabled': <true>, 'name': <'Tom'>}, ['id'])," |
| 484 | "('bad', 'btype', 2, {'port': <uint32 4000>}, ['name'])" |
| 485 | "]" << |
| 486 | settings << removedKeys; |
| 487 | settings.clear(); |
| 488 | removedKeys.clear(); |
| 489 | |
| 490 | QTest::newRow("invalid service") << |
| 491 | "[('findme', 'type', 3, {}, ['enabled','id'])]" << |
| 492 | settings << removedKeys; |
| 493 | } |
| 494 | |
| 495 | void LibaccountsServiceTest::testSettings() |
| 496 | { |
| 497 | QFETCH(QString, clientSettings); |
| 498 | QFETCH(ServiceSettings, settings); |
| 499 | QFETCH(RemovedKeys, removedKeys); |
| 500 | |
| 501 | setApparmorProfile("MyProvider"); |
| 502 | |
| 503 | ManagerController *mc = ManagerController::instance(); |
| 504 | mc->setServices(QStringList() << "cool" << "bad"); |
| 505 | |
| 506 | QStringList params = { "0", "true", "false", "MyProvider", clientSettings }; |
| 507 | QProcess *client = requestStore(params, true); |
| 508 | QSignalSpy finished(client, SIGNAL(finished(int,QProcess::ExitStatus))); |
| 509 | |
| 510 | QTRY_VERIFY(mc->lastLoadedAccount != 0); |
| 511 | AccountController *ac = AccountController::mock(mc->lastLoadedAccount); |
| 512 | QTRY_COMPARE(ac->syncWasCalled(), true); |
| 513 | |
| 514 | QCOMPARE(ac->m_serviceSettings, settings); |
| 515 | QCOMPARE(ac->m_removedKeys, removedKeys); |
| 516 | |
| 517 | ac->doSync(); |
| 518 | |
| 519 | finished.wait(); |
| 520 | } |
| 521 | |
| 522 | QTEST_MAIN(LibaccountsServiceTest); |
| 523 | |
| 524 | #include "tst_libaccounts_service.moc" |