File indexing completed on 2024-11-24 04:43:52

0001 /*
0002     SPDX-FileCopyrightText: 2017-2018 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KWallet>
0008 #include <QTest>
0009 #include <functional>
0010 
0011 #include "ewssettings.h"
0012 #include "ewssettings_ut_mock.h"
0013 
0014 Q_LOGGING_CATEGORY(EWSRES_LOG, "org.kde.pim.ews", QtInfoMsg)
0015 
0016 static const QString accessTokenMapKey = QStringLiteral("access-token");
0017 static const QString refreshTokenMapKey = QStringLiteral("refresh-token");
0018 
0019 class UtEwsSettings : public QObject
0020 {
0021     Q_OBJECT
0022 private Q_SLOTS:
0023     void readNoPassword();
0024     void readNullWallet();
0025     void readTimeout();
0026     void readTimeoutInterrupted();
0027     void readValidPassword();
0028     void writeNullPassword();
0029     void writeNullWallet();
0030     void writeTimeout();
0031     void writeValidPassword();
0032     void readValidMap();
0033     void writeValidMap();
0034 };
0035 
0036 namespace KWallet
0037 {
0038 class MyWallet : public Wallet
0039 {
0040     Q_OBJECT
0041 public:
0042     MyWallet();
0043     ~MyWallet() override;
0044 
0045     void doOpen(bool success);
0046 
0047     bool hasFolder(const QString &folder) override;
0048     bool setFolder(const QString &folder) override;
0049     bool createFolder(const QString &folder) override;
0050     int readPassword(const QString &key, QString &value) override;
0051     int writePassword(const QString &key, const QString &value) override;
0052     int readMap(const QString &key, QMap<QString, QString> &value) override;
0053     int writeMap(const QString &key, const QMap<QString, QString> &value) override;
0054 
0055     std::function<bool(const QString &)> hasFolderCallback;
0056     std::function<bool(const QString &)> setFolderCallback;
0057     std::function<bool(const QString &)> createFolderCallback;
0058     std::function<int(const QString &, QString &)> readPasswordCallback;
0059     std::function<int(const QString &, const QString &)> writePasswordCallback;
0060     std::function<int(const QString &, QMap<QString, QString> &)> readMapCallback;
0061     std::function<int(const QString &, const QMap<QString, QString> &)> writeMapCallback;
0062 };
0063 
0064 static std::function<void()> errorCallback;
0065 static std::function<Wallet *(MyWallet *wallet)> openWalletCallback;
0066 
0067 void reportError()
0068 {
0069     if (errorCallback) {
0070         errorCallback();
0071     }
0072 }
0073 
0074 Wallet *openWallet(MyWallet *wallet)
0075 {
0076     if (openWalletCallback) {
0077         return openWalletCallback(wallet);
0078     } else {
0079         qDebug() << "Wallet open callback not registered!";
0080         errorCallback();
0081         return wallet;
0082     }
0083 }
0084 
0085 static const QString networkWallet = QStringLiteral("test_network_wallet");
0086 inline const QString Wallet::NetworkWallet()
0087 {
0088     return networkWallet;
0089 }
0090 
0091 inline Wallet *Wallet::openWallet(const QString &name, WId, OpenType ot)
0092 {
0093     qDebug() << "intercepted openWallet";
0094     if (name != networkWallet) {
0095         qDebug() << "Incorrect wallet name";
0096         reportError();
0097     }
0098 
0099     if (ot != Asynchronous) {
0100         qDebug() << "Unsupported open type";
0101         reportError();
0102     }
0103 
0104     auto wallet = new MyWallet();
0105     return KWallet::openWallet(wallet);
0106 }
0107 
0108 MyWallet::MyWallet()
0109     : Wallet(0, networkWallet)
0110 {
0111 }
0112 
0113 MyWallet::~MyWallet() = default;
0114 
0115 void MyWallet::doOpen(bool success)
0116 {
0117     Q_EMIT walletOpened(success);
0118 }
0119 
0120 bool MyWallet::hasFolder(const QString &folder)
0121 {
0122     if (hasFolderCallback) {
0123         return hasFolderCallback(folder);
0124     } else {
0125         qWarning() << "hasFolder() callback not set!";
0126         reportError();
0127         return false;
0128     }
0129 }
0130 
0131 bool MyWallet::setFolder(const QString &folder)
0132 {
0133     if (setFolderCallback) {
0134         return setFolderCallback(folder);
0135     } else {
0136         qWarning() << "setFolder() callback not set!";
0137         reportError();
0138         return false;
0139     }
0140 }
0141 
0142 bool MyWallet::createFolder(const QString &folder)
0143 {
0144     if (createFolderCallback) {
0145         return createFolderCallback(folder);
0146     } else {
0147         qWarning() << "createFolder() callback not set!";
0148         reportError();
0149         return false;
0150     }
0151 }
0152 
0153 int MyWallet::readPassword(const QString &key, QString &value)
0154 {
0155     if (readPasswordCallback) {
0156         return readPasswordCallback(key, value);
0157     } else {
0158         qWarning() << "readPasswordCallback() callback not set!";
0159         reportError();
0160         return 0;
0161     }
0162 }
0163 
0164 int MyWallet::writePassword(const QString &key, const QString &value)
0165 {
0166     if (writePasswordCallback) {
0167         return writePasswordCallback(key, value);
0168     } else {
0169         qWarning() << "writePasswordCallback() callback not set!";
0170         reportError();
0171         return 0;
0172     }
0173 }
0174 
0175 int MyWallet::readMap(const QString &key, QMap<QString, QString> &value)
0176 {
0177     if (readMapCallback) {
0178         return readMapCallback(key, value);
0179     } else {
0180         qWarning() << "readMapCallback() callback not set!";
0181         reportError();
0182         return 0;
0183     }
0184 }
0185 
0186 int MyWallet::writeMap(const QString &key, const QMap<QString, QString> &value)
0187 {
0188     if (writeMapCallback) {
0189         return writeMapCallback(key, value);
0190     } else {
0191         qWarning() << "writeMapCallback() callback not set!";
0192         reportError();
0193         return 0;
0194     }
0195 }
0196 }
0197 
0198 void UtEwsSettings::readNoPassword()
0199 {
0200     KWallet::MyWallet *wallet = nullptr;
0201     KWallet::openWalletCallback = [&wallet](KWallet::MyWallet *w) {
0202         wallet = w;
0203         return w;
0204     };
0205 
0206     QEventLoop loop;
0207     bool error = false;
0208     KWallet::errorCallback = [&]() {
0209         if (loop.isRunning()) {
0210             loop.exit(1);
0211         } else {
0212             error = true;
0213         }
0214     };
0215 
0216     bool hasFolderCalled = false;
0217     QString password;
0218     EwsSettings settings(0);
0219     connect(&settings, &EwsSettings::passwordRequestFinished, this, [&](const QString &p) {
0220         password = p;
0221         loop.exit(0);
0222     });
0223     QTimer::singleShot(100, this, [&]() {
0224         settings.requestPassword();
0225         if (!wallet) {
0226             qDebug() << "Wallet is null";
0227             loop.exit(1);
0228             return;
0229         }
0230         wallet->hasFolderCallback = [&hasFolderCalled](const QString &) {
0231             hasFolderCalled = true;
0232             return false;
0233         };
0234         wallet->createFolderCallback = [](const QString &) {
0235             return false;
0236         };
0237         wallet->doOpen(true);
0238     });
0239     QTimer timeoutTimer;
0240     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0241         qDebug() << "Test timeout";
0242         loop.exit(1);
0243     });
0244     timeoutTimer.setSingleShot(true);
0245     timeoutTimer.start(2000);
0246 
0247     QVERIFY(error != true);
0248 
0249     QVERIFY(loop.exec() == 0);
0250 
0251     QVERIFY(password.isNull());
0252     QVERIFY(hasFolderCalled);
0253 }
0254 
0255 void UtEwsSettings::readNullWallet()
0256 {
0257     KWallet::openWalletCallback = [](KWallet::MyWallet *) {
0258         return nullptr;
0259     };
0260 
0261     QEventLoop loop;
0262     bool error = false;
0263     KWallet::errorCallback = [&]() {
0264         if (loop.isRunning()) {
0265             loop.exit(1);
0266         } else {
0267             error = true;
0268         }
0269     };
0270 
0271     QString password;
0272     EwsSettings settings(0);
0273     connect(&settings, &EwsSettings::passwordRequestFinished, this, [&](const QString &p) {
0274         password = p;
0275         loop.exit(0);
0276     });
0277     QTimer::singleShot(100, [&]() {
0278         settings.requestPassword();
0279     });
0280     QTimer timeoutTimer;
0281     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0282         qDebug() << "Test timeout";
0283         loop.exit(1);
0284     });
0285     timeoutTimer.setSingleShot(true);
0286     timeoutTimer.start(2000);
0287 
0288     QVERIFY(error != true);
0289 
0290     QVERIFY(loop.exec() == 0);
0291 
0292     QVERIFY(password.isNull());
0293 }
0294 
0295 void UtEwsSettings::readTimeout()
0296 {
0297     KWallet::MyWallet *wallet = nullptr;
0298     KWallet::openWalletCallback = [&wallet](KWallet::MyWallet *w) {
0299         wallet = w;
0300         return w;
0301     };
0302 
0303     QEventLoop loop;
0304     bool error = false;
0305     KWallet::errorCallback = [&]() {
0306         if (loop.isRunning()) {
0307             loop.exit(1);
0308         } else {
0309             error = true;
0310         }
0311     };
0312 
0313     bool hasFolderCalled = false;
0314     QString password;
0315     EwsSettings settings(0);
0316     connect(&settings, &EwsSettings::passwordRequestFinished, this, [&](const QString &p) {
0317         password = p;
0318         loop.exit(0);
0319     });
0320     QTimer::singleShot(100, this, [&]() {
0321         settings.requestPassword();
0322         if (!wallet) {
0323             qDebug() << "Wallet is null";
0324             loop.exit(1);
0325             return;
0326         }
0327         wallet->hasFolderCallback = [&hasFolderCalled](const QString &) {
0328             hasFolderCalled = true;
0329             return false;
0330         };
0331         wallet->createFolderCallback = [](const QString &) {
0332             return false;
0333         };
0334     });
0335     QTimer timeoutTimer;
0336     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0337         qDebug() << "Test timeout";
0338         loop.exit(1);
0339     });
0340     timeoutTimer.setSingleShot(true);
0341     timeoutTimer.start(5000);
0342 
0343     QVERIFY(error != true);
0344 
0345     QVERIFY(loop.exec() == 0);
0346 
0347     QVERIFY(password.isNull());
0348     QVERIFY(!hasFolderCalled);
0349 }
0350 
0351 void UtEwsSettings::readTimeoutInterrupted()
0352 {
0353     KWallet::MyWallet *wallet = nullptr;
0354     KWallet::openWalletCallback = [&wallet](KWallet::MyWallet *w) {
0355         wallet = w;
0356         return w;
0357     };
0358 
0359     QEventLoop loop;
0360     bool error = false;
0361     KWallet::errorCallback = [&]() {
0362         if (loop.isRunning()) {
0363             loop.exit(1);
0364         } else {
0365             error = true;
0366         }
0367     };
0368 
0369     bool hasFolderCalled = false;
0370     QString password;
0371     EwsSettings settings(0);
0372     bool testSecondSignal = false;
0373     connect(&settings, &EwsSettings::passwordRequestFinished, this, [&](const QString &p) {
0374         if (!testSecondSignal) {
0375             password = p;
0376             loop.exit(0);
0377         } else {
0378             loop.exit(1);
0379         }
0380     });
0381     QTimer::singleShot(100, this, [&]() {
0382         settings.requestPassword();
0383         if (!wallet) {
0384             qDebug() << "Wallet is null";
0385             loop.exit(1);
0386             return;
0387         }
0388         wallet->hasFolderCallback = [&hasFolderCalled](const QString &) {
0389             hasFolderCalled = true;
0390             return false;
0391         };
0392         wallet->createFolderCallback = [](const QString &) {
0393             return false;
0394         };
0395     });
0396     QTimer::singleShot(1000, this, [&]() {
0397         settings.setTestPassword(QStringLiteral("foo"));
0398     });
0399     QTimer timeoutTimer;
0400     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0401         qDebug() << "Test timeout";
0402         loop.exit(2);
0403     });
0404     timeoutTimer.setSingleShot(true);
0405     timeoutTimer.start(5000);
0406 
0407     QVERIFY(error != true);
0408 
0409     QVERIFY(loop.exec() == 0);
0410 
0411     QVERIFY(password == QLatin1StringView("foo"));
0412     QVERIFY(!hasFolderCalled);
0413 
0414     // Check for second passwordRequestFinished signal
0415     QVERIFY(loop.exec() == 2);
0416 }
0417 
0418 void UtEwsSettings::readValidPassword()
0419 {
0420     KWallet::MyWallet *wallet = nullptr;
0421     KWallet::openWalletCallback = [&wallet](KWallet::MyWallet *w) {
0422         wallet = w;
0423         return w;
0424     };
0425 
0426     QEventLoop loop;
0427     bool error = false;
0428     KWallet::errorCallback = [&]() {
0429         if (loop.isRunning()) {
0430             loop.exit(1);
0431         } else {
0432             error = true;
0433         }
0434     };
0435 
0436     bool hasFolderCalled = false;
0437     bool setFolderCalled = false;
0438     QString password;
0439     EwsSettings settings(0);
0440     connect(&settings, &EwsSettings::passwordRequestFinished, this, [&](const QString &p) {
0441         password = p;
0442         loop.exit(0);
0443     });
0444     QTimer::singleShot(100, this, [&]() {
0445         settings.requestPassword();
0446         if (!wallet) {
0447             qDebug() << "Wallet is null";
0448             loop.exit(1);
0449             return;
0450         }
0451         wallet->hasFolderCallback = [&hasFolderCalled](const QString &) {
0452             hasFolderCalled = true;
0453             return true;
0454         };
0455         wallet->createFolderCallback = [](const QString &) {
0456             return false;
0457         };
0458         wallet->setFolderCallback = [&setFolderCalled](const QString &) {
0459             setFolderCalled = true;
0460             return true;
0461         };
0462         wallet->readPasswordCallback = [](const QString &, QString &password) {
0463             password = QStringLiteral("foo");
0464             return true;
0465         };
0466         wallet->doOpen(true);
0467     });
0468     QTimer timeoutTimer;
0469     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0470         qDebug() << "Test timeout";
0471         loop.exit(1);
0472     });
0473     timeoutTimer.setSingleShot(true);
0474     timeoutTimer.start(2000);
0475 
0476     QVERIFY(error != true);
0477 
0478     QVERIFY(loop.exec() == 0);
0479 
0480     QVERIFY(password == QLatin1StringView("foo"));
0481     QVERIFY(hasFolderCalled);
0482     QVERIFY(setFolderCalled);
0483 }
0484 
0485 void UtEwsSettings::writeNullPassword()
0486 {
0487     KWallet::MyWallet *wallet = nullptr;
0488     KWallet::openWalletCallback = [&wallet](KWallet::MyWallet *w) {
0489         wallet = w;
0490         return w;
0491     };
0492 
0493     QEventLoop loop;
0494     bool error = false;
0495     KWallet::errorCallback = [&]() {
0496         if (loop.isRunning()) {
0497             loop.exit(1);
0498         } else {
0499             error = true;
0500         }
0501     };
0502 
0503     EwsSettings settings(0);
0504     QTimer::singleShot(100, this, [&]() {
0505         settings.setPassword(QString());
0506         if (wallet) {
0507             qDebug() << "Wallet is not null";
0508             loop.exit(1);
0509             return;
0510         }
0511         loop.exit(0);
0512     });
0513     QTimer timeoutTimer;
0514     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0515         qDebug() << "Test timeout";
0516         loop.exit(1);
0517     });
0518     timeoutTimer.setSingleShot(true);
0519     timeoutTimer.start(2000);
0520 
0521     QVERIFY(error != true);
0522 
0523     QVERIFY(loop.exec() == 0);
0524 }
0525 
0526 void UtEwsSettings::writeNullWallet()
0527 {
0528     KWallet::MyWallet *wallet = nullptr;
0529     KWallet::openWalletCallback = [](KWallet::MyWallet *) {
0530         return nullptr;
0531     };
0532 
0533     QEventLoop loop;
0534     bool error = false;
0535     KWallet::errorCallback = [&]() {
0536         if (loop.isRunning()) {
0537             loop.exit(1);
0538         } else {
0539             error = true;
0540         }
0541     };
0542 
0543     EwsSettings settings(0);
0544     QTimer::singleShot(100, this, [&]() {
0545         settings.setPassword(QStringLiteral("foo"));
0546         if (wallet) {
0547             qDebug() << "Wallet is not null";
0548             loop.exit(1);
0549             return;
0550         }
0551         loop.exit(0);
0552     });
0553     QTimer timeoutTimer;
0554     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0555         qDebug() << "Test timeout";
0556         loop.exit(1);
0557     });
0558     timeoutTimer.setSingleShot(true);
0559     timeoutTimer.start(2000);
0560 
0561     QVERIFY(error != true);
0562 
0563     QVERIFY(loop.exec() == 0);
0564 }
0565 
0566 void UtEwsSettings::writeTimeout()
0567 {
0568     KWallet::MyWallet *wallet = nullptr;
0569     KWallet::openWalletCallback = [&wallet](KWallet::MyWallet *w) {
0570         wallet = w;
0571         return w;
0572     };
0573 
0574     QEventLoop loop;
0575     bool error = false;
0576     KWallet::errorCallback = [&]() {
0577         if (loop.isRunning()) {
0578             loop.exit(1);
0579         } else {
0580             error = true;
0581         }
0582     };
0583 
0584     bool hasFolderCalled = false;
0585     bool createFolderCalled = false;
0586     bool setFolderCalled = false;
0587     QString password;
0588     EwsSettings settings(0);
0589     QTimer::singleShot(100, this, [&]() {
0590         settings.setPassword(QStringLiteral("foo"));
0591         if (!wallet) {
0592             qDebug() << "Wallet is null";
0593             loop.exit(1);
0594             return;
0595         }
0596         wallet->hasFolderCallback = [&hasFolderCalled](const QString &) {
0597             hasFolderCalled = true;
0598             return false;
0599         };
0600         wallet->createFolderCallback = [&createFolderCalled](const QString &) {
0601             createFolderCalled = true;
0602             return true;
0603         };
0604         wallet->setFolderCallback = [&setFolderCalled](const QString &) {
0605             setFolderCalled = true;
0606             return false;
0607         };
0608         wallet->writePasswordCallback = [&](const QString &, const QString &p) {
0609             password = p;
0610             loop.exit(0);
0611             return true;
0612         };
0613     });
0614     QTimer timeoutTimer;
0615     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0616         qDebug() << "Test timeout";
0617         loop.exit(2);
0618     });
0619     timeoutTimer.setSingleShot(true);
0620     timeoutTimer.start(5000);
0621 
0622     QVERIFY(error != true);
0623 
0624     QVERIFY(loop.exec() == 2);
0625 
0626     QVERIFY(password.isNull());
0627     QVERIFY(!hasFolderCalled);
0628     QVERIFY(!setFolderCalled);
0629     QVERIFY(!createFolderCalled);
0630 }
0631 
0632 void UtEwsSettings::writeValidPassword()
0633 {
0634     KWallet::MyWallet *wallet = nullptr;
0635     KWallet::openWalletCallback = [&wallet](KWallet::MyWallet *w) {
0636         wallet = w;
0637         return w;
0638     };
0639 
0640     QEventLoop loop;
0641     bool error = false;
0642     KWallet::errorCallback = [&]() {
0643         if (loop.isRunning()) {
0644             loop.exit(1);
0645         } else {
0646             error = true;
0647         }
0648     };
0649 
0650     bool hasFolderCalled = false;
0651     bool createFolderCalled = false;
0652     bool setFolderCalled = false;
0653     QString password;
0654     EwsSettings settings(0);
0655     QTimer::singleShot(100, this, [&]() {
0656         settings.setPassword(QStringLiteral("foo"));
0657         if (!wallet) {
0658             qDebug() << "Wallet is null";
0659             loop.exit(1);
0660             return;
0661         }
0662         wallet->hasFolderCallback = [&hasFolderCalled](const QString &) {
0663             hasFolderCalled = true;
0664             return false;
0665         };
0666         wallet->createFolderCallback = [&createFolderCalled](const QString &) {
0667             createFolderCalled = true;
0668             return true;
0669         };
0670         wallet->setFolderCallback = [&setFolderCalled](const QString &) {
0671             setFolderCalled = true;
0672             return false;
0673         };
0674         wallet->writePasswordCallback = [&](const QString &, const QString &p) {
0675             password = p;
0676             loop.exit(0);
0677             return true;
0678         };
0679         wallet->doOpen(true);
0680     });
0681     QTimer timeoutTimer;
0682     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0683         qDebug() << "Test timeout";
0684         loop.exit(1);
0685     });
0686     timeoutTimer.setSingleShot(true);
0687     timeoutTimer.start(2000);
0688 
0689     QVERIFY(error != true);
0690 
0691     QVERIFY(loop.exec() == 0);
0692 
0693     QVERIFY(password == QLatin1StringView("foo"));
0694     QVERIFY(hasFolderCalled);
0695     QVERIFY(setFolderCalled);
0696     QVERIFY(createFolderCalled);
0697 }
0698 
0699 void UtEwsSettings::readValidMap()
0700 {
0701     KWallet::MyWallet *wallet = nullptr;
0702     KWallet::openWalletCallback = [&wallet](KWallet::MyWallet *w) {
0703         wallet = w;
0704         return w;
0705     };
0706 
0707     QEventLoop loop;
0708     bool error = false;
0709     KWallet::errorCallback = [&]() {
0710         if (loop.isRunning()) {
0711             loop.exit(1);
0712         } else {
0713             error = true;
0714         }
0715     };
0716 
0717     bool hasFolderCalled = false;
0718     bool setFolderCalled = false;
0719     QMap<QString, QString> map;
0720     const QMap<QString, QString> expectedMap = {
0721         {accessTokenMapKey, QStringLiteral("afoo")},
0722         {refreshTokenMapKey, QStringLiteral("rfoo")},
0723     };
0724     EwsSettings settings(0);
0725     connect(&settings, &EwsSettings::mapRequestFinished, this, [&](const QMap<QString, QString> &m) {
0726         map = m;
0727         loop.exit(0);
0728     });
0729     QTimer::singleShot(100, this, [&]() {
0730         settings.requestMap();
0731         if (!wallet) {
0732             qDebug() << "Wallet is null";
0733             loop.exit(1);
0734             return;
0735         }
0736         wallet->hasFolderCallback = [&hasFolderCalled](const QString &) {
0737             hasFolderCalled = true;
0738             return true;
0739         };
0740         wallet->createFolderCallback = [](const QString &) {
0741             return false;
0742         };
0743         wallet->setFolderCallback = [&setFolderCalled](const QString &) {
0744             setFolderCalled = true;
0745             return true;
0746         };
0747         wallet->readMapCallback = [](const QString &, QMap<QString, QString> &map) {
0748             map[accessTokenMapKey] = QStringLiteral("afoo");
0749             map[refreshTokenMapKey] = QStringLiteral("rfoo");
0750             return true;
0751         };
0752         wallet->doOpen(true);
0753     });
0754     QTimer timeoutTimer;
0755     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0756         qDebug() << "Test timeout";
0757         loop.exit(1);
0758     });
0759     timeoutTimer.setSingleShot(true);
0760     timeoutTimer.start(2000);
0761 
0762     QVERIFY(error != true);
0763 
0764     QVERIFY(loop.exec() == 0);
0765 
0766     QVERIFY(map == expectedMap);
0767     QVERIFY(hasFolderCalled);
0768     QVERIFY(setFolderCalled);
0769 }
0770 
0771 void UtEwsSettings::writeValidMap()
0772 {
0773     KWallet::MyWallet *wallet = nullptr;
0774     KWallet::openWalletCallback = [&wallet](KWallet::MyWallet *w) {
0775         wallet = w;
0776         return w;
0777     };
0778 
0779     QEventLoop loop;
0780     bool error = false;
0781     KWallet::errorCallback = [&]() {
0782         if (loop.isRunning()) {
0783             loop.exit(1);
0784         } else {
0785             error = true;
0786         }
0787     };
0788 
0789     bool hasFolderCalled = false;
0790     bool createFolderCalled = false;
0791     bool setFolderCalled = false;
0792     const QMap<QString, QString> expectedMap = {{accessTokenMapKey, QStringLiteral("afoo")}, {refreshTokenMapKey, QStringLiteral("rfoo")}};
0793     QMap<QString, QString> map;
0794     EwsSettings settings(0);
0795     QTimer::singleShot(100, this, [&]() {
0796         settings.setMap(expectedMap);
0797         if (!wallet) {
0798             qDebug() << "Wallet is null";
0799             loop.exit(1);
0800             return;
0801         }
0802         wallet->hasFolderCallback = [&hasFolderCalled](const QString &) {
0803             hasFolderCalled = true;
0804             return false;
0805         };
0806         wallet->createFolderCallback = [&createFolderCalled](const QString &) {
0807             createFolderCalled = true;
0808             return true;
0809         };
0810         wallet->setFolderCallback = [&setFolderCalled](const QString &) {
0811             setFolderCalled = true;
0812             return false;
0813         };
0814         wallet->writeMapCallback = [&](const QString &, const QMap<QString, QString> &m) {
0815             map = m;
0816             loop.exit(0);
0817             return true;
0818         };
0819         wallet->doOpen(true);
0820     });
0821     QTimer timeoutTimer;
0822     connect(&timeoutTimer, &QTimer::timeout, this, [&]() {
0823         qDebug() << "Test timeout";
0824         loop.exit(1);
0825     });
0826     timeoutTimer.setSingleShot(true);
0827     timeoutTimer.start(2000);
0828 
0829     QVERIFY(error != true);
0830 
0831     QVERIFY(loop.exec() == 0);
0832 
0833     QVERIFY(map == expectedMap);
0834     QVERIFY(hasFolderCalled);
0835     QVERIFY(setFolderCalled);
0836     QVERIFY(createFolderCalled);
0837 }
0838 
0839 QTEST_MAIN(UtEwsSettings)
0840 
0841 #include "ewssettings_ut.moc"