File indexing completed on 2024-05-05 05:29:37

0001 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0002 // SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org>
0003 
0004 #include <QSignalSpy>
0005 #include <QStandardPaths>
0006 #include <QTest>
0007 
0008 #include <sentrydsns.h>
0009 
0010 #include "sentryfilereply.h"
0011 
0012 using namespace Qt::StringLiterals;
0013 
0014 class FileConnection : public SentryConnection
0015 {
0016 public:
0017     using SentryConnection::SentryConnection;
0018 
0019     SentryReply *get(const QNetworkRequest &request) override
0020     {
0021         if (request.url() == QUrl("https://autoconfig.kde.org/drkonqi/sentry/0/dsns.json"_L1)) {
0022             return new FileReply(QFINDTESTDATA("data/dsns.json"));
0023         }
0024 
0025         qWarning() << "unhandled request" << request.url();
0026         Q_ASSERT(false);
0027         return nullptr;
0028     }
0029 
0030     SentryReply *post(const QNetworkRequest &request, const QByteArray &) override
0031     {
0032         qWarning() << "unhandled request" << request.url();
0033         Q_ASSERT(false);
0034         return nullptr;
0035     }
0036 };
0037 
0038 class NoopFileConnection : public FileConnection
0039 {
0040 public:
0041     using FileConnection::FileConnection;
0042 
0043     SentryReply *get(const QNetworkRequest &) override
0044     {
0045         auto reply = new FileReply("/dev/null"_L1);
0046         reply->m_error = QNetworkReply::HostNotFoundError;
0047         reply->m_errorString = "Yada yada"_L1;
0048         return reply;
0049     }
0050 };
0051 
0052 class SentryDSNsTest : public QObject
0053 {
0054     Q_OBJECT
0055 private Q_SLOTS:
0056     void initTestCase()
0057     {
0058         QStandardPaths::setTestModeEnabled(true);
0059     }
0060 
0061     void testKWrite()
0062     {
0063         SentryDSNs dsns(std::make_shared<FileConnection>(), SentryDSNs::Cache::No);
0064         QSignalSpy spy(&dsns, &SentryDSNs::loaded);
0065         dsns.load();
0066         spy.wait();
0067         const auto context = dsns.context("kwrite"_L1);
0068         QCOMPARE(context.index, "3"_L1);
0069         QCOMPARE(context.key, "ba18a003921d4ac281851019f69050b4"_L1);
0070         QCOMPARE(context.project, "kwrite"_L1);
0071         QCOMPARE(context.dsnUrl(), QUrl("https://ba18a003921d4ac281851019f69050b4@crash-reports.kde.org/api/3"_L1));
0072         QCOMPARE(context.envelopeUrl(), QUrl("https://ba18a003921d4ac281851019f69050b4@crash-reports.kde.org/api/3/envelope/"_L1));
0073     }
0074 
0075     void testFallthrough()
0076     {
0077         SentryDSNs dsns(std::make_shared<FileConnection>(), SentryDSNs::Cache::No);
0078         QSignalSpy spy(&dsns, &SentryDSNs::loaded);
0079         dsns.load();
0080         spy.wait();
0081         const auto context = dsns.context("does_not_exist"_L1);
0082         QCOMPARE(context.index, "12"_L1);
0083         QCOMPARE(context.key, "asdf"_L1);
0084         QCOMPARE(context.project, "fallthrough"_L1);
0085     }
0086 
0087     void testBuiltinFallthrough()
0088     {
0089         SentryDSNs dsns(std::make_shared<NoopFileConnection>(), SentryDSNs::Cache::No);
0090         QSignalSpy spy(&dsns, &SentryDSNs::loaded);
0091         dsns.load();
0092         spy.wait();
0093         const auto context = dsns.context("does_not_exist"_L1);
0094         QCOMPARE(context.index, "11"_L1);
0095         QCOMPARE(context.key, "456f53a71a074438bbb786d6add63241"_L1);
0096         QCOMPARE(context.project, "fallthrough"_L1);
0097     }
0098 };
0099 
0100 QTEST_GUILESS_MAIN(SentryDSNsTest)
0101 
0102 #include "sentrydsnstest.moc"