Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F112270
D118.1732286625.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
D118.1732286625.diff
View Options
diff --git a/src/matrix-sdk.hpp b/src/matrix-sdk.hpp
--- a/src/matrix-sdk.hpp
+++ b/src/matrix-sdk.hpp
@@ -30,9 +30,11 @@
class MatrixDeviceList;
class MatrixPromise;
class MatrixSdkTest;
+class MatrixSdkSessionsTest;
class MatrixEvent;
class MatrixStickerPackList;
class MatrixUserGivenAttrsMap;
+class KazvSessionLockGuard;
struct MatrixSdkPrivate;
@@ -233,8 +235,11 @@
private: // Testing
friend MatrixSdkTest;
+ friend MatrixSdkSessionsTest;
friend MatrixSdk *makeTestSdk(Kazv::SdkModel model);
+ void setUserDataDir(const std::string &userDataDir);
+
explicit MatrixSdk(Kazv::SdkModel model, bool testing = false, QObject *parent = 0);
void startThread();
};
diff --git a/src/matrix-sdk.cpp b/src/matrix-sdk.cpp
--- a/src/matrix-sdk.cpp
+++ b/src/matrix-sdk.cpp
@@ -83,6 +83,7 @@
MatrixSdkPrivate(MatrixSdk *q, bool testing);
MatrixSdkPrivate(MatrixSdk *q, bool testing, SdkModel model);
bool testing;
+ std::string userDataDir;
RandomInterface randomGenerator;
QThread *thread;
QObject *obj;
@@ -106,6 +107,8 @@
serializeClientToFile(clientOnSecondaryRoot);
}
}
+
+ void serializeClientToFile(Client c);
};
// Cleaning up notes:
@@ -148,7 +151,7 @@
}
};
-static void serializeClientToFile(Client c)
+void MatrixSdkPrivate::serializeClientToFile(Client c)
{
using namespace Kazv::CursorOp;
auto userId = +c.userId();
@@ -159,7 +162,7 @@
}
using StdPath = std::filesystem::path;
- auto userDataDir = StdPath(kazvUserDataDir().toStdString());
+ auto userDataDir = StdPath(this->userDataDir);
auto encodedUserId = encodeBase64(userId, Base64Opts::urlSafe);
auto sessionDir = userDataDir / "sessions"
/ encodedUserId / deviceId;
@@ -200,6 +203,7 @@
MatrixSdkPrivate::MatrixSdkPrivate(MatrixSdk *q, bool testing)
: testing(testing)
+ , userDataDir{kazvUserDataDir().toStdString()}
, randomGenerator(QtRandAdapter{})
, thread(new QThread())
, obj(new QObject())
@@ -222,6 +226,7 @@
MatrixSdkPrivate::MatrixSdkPrivate(MatrixSdk *q, bool testing, SdkModel model)
: testing(testing)
+ , userDataDir{kazvUserDataDir().toStdString()}
, randomGenerator(QtRandAdapter{})
, thread(new QThread())
, obj(new QObject())
@@ -371,6 +376,7 @@
void MatrixSdk::emplace(std::optional<SdkModel> model)
{
auto testing = m_d->testing;
+ auto userDataDir = m_d->userDataDir;
if (m_d) {
cleanupDPointer(std::move(m_d));
@@ -379,6 +385,7 @@
m_d = (model.has_value()
? std::make_unique<MatrixSdkPrivate>(this, testing, std::move(model.value()))
: std::make_unique<MatrixSdkPrivate>(this, testing));
+ m_d->userDataDir = userDataDir;
// Re-initialize lager-qt cursors and watchable connections
init();
@@ -393,7 +400,7 @@
QStringList MatrixSdk::allSessions() const
{
using StdPath = std::filesystem::path;
- auto userDataDir = StdPath(kazvUserDataDir().toStdString());
+ auto userDataDir = StdPath(m_d->userDataDir);
auto allSessionsDir = userDataDir / "sessions";
QStringList sessionNames;
@@ -485,7 +492,7 @@
};
qDebug() << "in loadSession(), sessionName=" << sessionName;
- auto userDataDir = StdPath(kazvUserDataDir().toStdString());
+ auto userDataDir = StdPath(m_d->userDataDir);
auto parts = sessionName.split('/');
if (parts.size() == 2) {
auto userId = parts[0].toStdString();
@@ -647,4 +654,9 @@
return new MatrixPromise(m_d->clientOnSecondaryRoot.setAccountData(event));
}
+void MatrixSdk::setUserDataDir(const std::string &userDataDir)
+{
+ m_d->userDataDir = userDataDir;
+}
+
#include "matrix-sdk.moc"
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -1,9 +1,12 @@
+set(KAZV_TEST_TEMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/test-temp)
+configure_file(test-temp.hpp.in test-temp.hpp)
+
set(kazvtestlib_SRCS
test-model.cpp
)
add_library(kazvtestlib STATIC ${kazvtestlib_SRCS})
-
+target_include_directories(kazvtestlib PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(kazvtestlib PUBLIC kazvprivlib libkazv::kazvtestfixtures)
ecm_add_tests(
@@ -12,6 +15,7 @@
qt-json-test.cpp
device-mgmt-test.cpp
matrix-sdk-test.cpp
+ matrix-sdk-sessions-test.cpp
matrix-promise-test.cpp
matrix-room-timeline-test.cpp
matrix-room-member-list-model-test.cpp
diff --git a/src/tests/matrix-sdk-sessions-test.cpp b/src/tests/matrix-sdk-sessions-test.cpp
new file mode 100644
--- /dev/null
+++ b/src/tests/matrix-sdk-sessions-test.cpp
@@ -0,0 +1,106 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <kazv-defs.hpp>
+#include <memory>
+#include <filesystem>
+#include <QObject>
+#include <QtTest>
+#include <crypto/base64.hpp>
+#include <matrix-sdk.hpp>
+#include "test-temp.hpp"
+
+using namespace Kazv;
+namespace Fs = std::filesystem;
+using StdPath = Fs::path;
+
+class MatrixSdkSessionsTest : public QObject
+{
+ Q_OBJECT
+
+private:
+ std::string m_userDataDir;
+
+ void clearDir();
+ void createSession(std::string userId, std::string deviceId);
+
+ template<class ...Args>
+ std::unique_ptr<MatrixSdk> makeMatrixSdk(Args &&...args)
+ {
+ std::unique_ptr<MatrixSdk> sdk(new MatrixSdk(std::forward<Args>(args)...));
+ sdk->setUserDataDir(m_userDataDir);
+ return sdk;
+ }
+
+private Q_SLOTS:
+ void initTestCase();
+ void init();
+ void cleanup();
+
+ void testListSessions();
+ void testLoadSession();
+};
+
+void MatrixSdkSessionsTest::initTestCase()
+{
+ auto dir = StdPath(kazvTestTempDir()) / "sessions-test";
+ m_userDataDir = dir.string();
+}
+
+void MatrixSdkSessionsTest::clearDir()
+{
+ QVERIFY(!m_userDataDir.empty());
+ std::error_code ec;
+ Fs::remove_all(StdPath(m_userDataDir), ec);
+ QVERIFY(!ec);
+}
+
+void MatrixSdkSessionsTest::init() { clearDir(); }
+
+void MatrixSdkSessionsTest::cleanup() { clearDir(); }
+
+void MatrixSdkSessionsTest::createSession(std::string userId, std::string deviceId)
+{
+ auto sessionsDir = StdPath(m_userDataDir) / "sessions";
+ SdkModel model;
+ model.client.userId = userId;
+ model.client.deviceId = deviceId;
+ auto sdk = makeMatrixSdk(model, /* testing = */ false);
+ sdk->serializeToFile();
+}
+
+void MatrixSdkSessionsTest::testListSessions()
+{
+ createSession("@mew:example.com", "device1");
+ createSession("@mew:example.com", "device2");
+ createSession("@mew2:example.com", "device3");
+
+ auto sdk = makeMatrixSdk();
+ QSet<QString> expected{
+ QStringLiteral("@mew:example.com/device1"),
+ QStringLiteral("@mew:example.com/device2"),
+ QStringLiteral("@mew2:example.com/device3"),
+ };
+
+ auto sessionList = sdk->allSessions();
+ auto sessionSet = QSet<QString>(sessionList.begin(), sessionList.end());
+ QCOMPARE(sessionSet, expected);
+}
+
+void MatrixSdkSessionsTest::testLoadSession()
+{
+ createSession("@mew:example.com", "device1");
+
+ auto sdk = makeMatrixSdk();
+ auto res = sdk->loadSession("@mew:example.com/device1");
+ QVERIFY(res);
+ QCOMPARE(sdk->userId(), QStringLiteral("@mew:example.com"));
+ QCOMPARE(sdk->deviceId(), QStringLiteral("device1"));
+}
+
+QTEST_MAIN(MatrixSdkSessionsTest)
+
+#include "matrix-sdk-sessions-test.moc"
diff --git a/src/tests/test-temp.hpp.in b/src/tests/test-temp.hpp.in
new file mode 100644
--- /dev/null
+++ b/src/tests/test-temp.hpp.in
@@ -0,0 +1,14 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#pragma once
+#include <kazv-defs.hpp>
+#include <string>
+
+inline std::string kazvTestTempDir()
+{
+ return R"xx(${KAZV_TEST_TEMP_DIR})xx";
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Nov 22, 6:43 AM (18 h, 27 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
38883
Default Alt Text
D118.1732286625.diff (7 KB)
Attached To
Mode
D118: Add tests for MatrixSdk session management
Attached
Detach File
Event Timeline
Log In to Comment