Page MenuHomePhorge

No OneTemporary

Size
15 KB
Referenced Files
None
Subscribers
None
diff --git a/src/testfixtures/factory.cpp b/src/testfixtures/factory.cpp
index 086a7d2..064124e 100644
--- a/src/testfixtures/factory.cpp
+++ b/src/testfixtures/factory.cpp
@@ -1,319 +1,321 @@
/*
* This file is part of libkazv.
* SPDX-FileCopyrightText: 2023 tusooa <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "factory.hpp"
namespace Kazv::Factory
{
static std::string generateUserId()
{
static std::size_t next = 0;
++next;
return "@u" + std::to_string(next) + ":example.com";
}
static std::string generateEventId()
{
static std::size_t next = 0;
++next;
- return "$" + std::to_string(next);
+ std::stringstream s;
+ s << std::setw(40) << std::setfill('0') << next;
+ return "$" + s.str();
}
static std::string generateRoomId()
{
static std::size_t next = 0;
++next;
return "!" + std::to_string(next) + ":example.com";
}
static std::string generateDeviceId()
{
static std::size_t next = 0;
++next;
return "dev" + std::to_string(next);
}
ClientModel makeClient(const ComposedModifier<ClientModel> &mod)
{
ClientModel m;
m.serverUrl = "example.com";
m.userId = "@bob:example.com";
m.token = "exampletoken";
m.deviceId = "exampledevice";
m.loggedIn = true;
mod(m);
return m;
}
ComposedModifier<ClientModel> withRoom(RoomModel room)
{
return [room](ClientModel &m) {
m.roomList.rooms = m.roomList.rooms.set(room.roomId, room);
};
}
ComposedModifier<ClientModel> withAccountData(immer::flex_vector<Event> accountDataEvent)
{
return [accountDataEvent](ClientModel &m) {
m.accountData = merge(std::move(m.accountData), accountDataEvent, keyOfAccountData);
};
}
ComposedModifier<ClientModel> withCrypto(const Crypto &crypto)
{
return [crypto](ClientModel &m) {
m.crypto = crypto;
};
}
ComposedModifier<ClientModel> withDevice(std::string userId, DeviceKeyInfo info)
{
return [userId, info](ClientModel &m) {
m.deviceLists.deviceLists = std::move(m.deviceLists.deviceLists)
.update(userId, [=](auto deviceMap) {
return std::move(deviceMap).set(info.deviceId, info);
});
};
}
DeviceKeyInfo makeDeviceKeyInfo(const ComposedModifier<DeviceKeyInfo> &mod)
{
DeviceKeyInfo m;
mod(m);
if (m.deviceId.empty()) {
withDeviceId(generateDeviceId())(m);
}
if (m.ed25519Key.empty()) {
m.ed25519Key = m.deviceId + "ed25519";
}
if (m.curve25519Key.empty()) {
m.curve25519Key = m.deviceId + "curve25519";
}
return m;
}
ComposedModifier<DeviceKeyInfo> withDeviceId(std::string deviceId)
{
return [deviceId](DeviceKeyInfo &m) { m.deviceId = deviceId; };
}
ComposedModifier<DeviceKeyInfo> withDeviceDisplayName(std::string displayName)
{
return [displayName](DeviceKeyInfo &m) { m.displayName = displayName; };
}
ComposedModifier<DeviceKeyInfo> withDeviceTrustLevel(DeviceTrustLevel trustLevel)
{
return [trustLevel](DeviceKeyInfo &m) { m.trustLevel = trustLevel; };
}
Crypto makeCrypto(const ComposedModifier<Crypto> &mod)
{
auto m = Crypto(RandomTag{}, genRandomData(Crypto::constructRandomSize()));
mod(m);
return m;
}
RoomModel makeRoom(const ComposedModifier<RoomModel> &mod)
{
RoomModel room;
room.roomId = generateRoomId();
mod(room);
return room;
}
ComposedModifier<RoomModel> withRoomId(std::string id)
{
return [id](RoomModel &m) { m.roomId = id; };
}
ComposedModifier<RoomModel> withRoomAccountData(immer::flex_vector<Event> accountDataEvent)
{
return [accountDataEvent](RoomModel &m) {
m.accountData = merge(std::move(m.accountData), accountDataEvent, keyOfAccountData);
};
}
ComposedModifier<RoomModel> withRoomState(immer::flex_vector<Event> stateEvent)
{
return [stateEvent](RoomModel &m) {
m.stateEvents = merge(std::move(m.stateEvents), stateEvent, keyOfState);
};
}
ComposedModifier<RoomModel> withRoomTimeline(immer::flex_vector<Event> timelineEvents)
{
return [timelineEvents](RoomModel &m) {
m = RoomModel::update(m, AddToTimelineAction{timelineEvents, std::nullopt, std::nullopt, std::nullopt});
};
}
ComposedModifier<RoomModel> withRoomTimelineGaps(immer::map<std::string, std::string> timelineGaps)
{
return [timelineGaps](RoomModel &m) {
m.timelineGaps = timelineGaps;
};
}
ComposedModifier<RoomModel> withRoomMembership(RoomMembership membership)
{
return [membership](RoomModel &m) {
m.membership = membership;
};
}
ComposedModifier<RoomModel> withRoomEncrypted(bool encrypted)
{
return [encrypted](RoomModel &m) {
m.encrypted = encrypted;
};
}
Event makeEvent(const ComposedModifier<Event> &mod)
{
Event e = json{
{"type", "m.room.message"},
{"sender", "@foo:tusooa.xyz"},
{"content", {
{"msgtype", "m.text"},
{"body", "test"},
}},
{"origin_server_ts", 1000},
{"event_id", generateEventId()},
};
mod(e);
return e;
}
Event makeMemberEvent(const ComposedModifier<Event> &mod)
{
auto userId = generateUserId();
auto event = makeEvent(
withEventType("m.room.member")
| withStateKey(userId)
| withEventContent(json::object())
| withMembership("join")
| mod
);
if (event.sender().empty()) {
withEventSenderId(event.stateKey());
}
return event;
}
ComposedModifier<Event> withEventJson(const json &j)
{
return [j](Event &e) {
e = Event(j);
};
}
ComposedModifier<Event> withEventKV(const json::json_pointer &k, const json &v)
{
return [k, v](Event &e) {
auto j = e.originalJson().get();
j[k] = v;
withEventJson(j)(e);
};
}
ComposedModifier<Event> withEventId(std::string id)
{
return withEventKV("/event_id"_json_pointer, id);
}
ComposedModifier<Event> withEventType(std::string type)
{
return withEventKV("/type"_json_pointer, type);
}
ComposedModifier<Event> withEventContent(const json &content)
{
return withEventKV("/content"_json_pointer, content);
}
ComposedModifier<Event> withStateKey(std::string id)
{
return withEventKV("/state_key"_json_pointer, id);
}
ComposedModifier<Event> withMembership(std::string membership)
{
return withEventKV("/content/membership"_json_pointer, membership);
}
ComposedModifier<Event> withMemberDisplayName(std::string displayName)
{
return withEventKV("/content/displayname"_json_pointer, displayName);
}
ComposedModifier<Event> withMemberAvatarUrl(std::string avatarUrl)
{
return withEventKV("/content/avatar_url"_json_pointer, avatarUrl);
}
ComposedModifier<Event> withEventSenderId(std::string sender)
{
return withEventKV("/sender_key"_json_pointer, sender);
}
ComposedModifier<Event> withEventRelationship(std::string relType, std::string eventId)
{
return withEventKV("/content/m.relates_to"_json_pointer, {
{"rel_type", relType},
{"event_id", eventId},
});
}
ComposedModifier<Event> withEventReplyTo(std::string eventId)
{
return withEventKV("/content/m.relates_to"_json_pointer, {
{"m.in_reply_to", {{"event_id", eventId}}},
});
}
Response makeResponse(std::string jobId, const ComposedModifier<Response> &mod)
{
Response r;
(withResponseStatusCode(200)
| withResponseJsonBody(json::object())
| withResponseDataKV("-job-id", jobId)
| mod)(r);
return r;
}
ComposedModifier<Response> withResponseStatusCode(int code)
{
return withAttr(&Response::statusCode, code);
}
ComposedModifier<Response> withResponseJsonBody(const json &body)
{
return withAttr(&Response::body, JsonWrap(body));
}
ComposedModifier<Response> withResponseBytesBody(const Bytes &body)
{
return withAttr(&Response::body, body);
}
ComposedModifier<Response> withResponseFileBody(const FileDesc &body)
{
return withAttr(&Response::body, body);
}
ComposedModifier<Response> withResponseDataKV(std::string k, const json &v)
{
return [k, v](Response &m) {
auto data = m.extraData.get();
if (!data.is_object()) {
data = json::object();
}
data[k] = v;
m.extraData = data;
};
}
}
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 9308cc2..b21b9d6 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -1,81 +1,114 @@
-
include(CTest)
set(KAZVTEST_RESPATH ${CMAKE_CURRENT_SOURCE_DIR}/resources)
configure_file(kazvtest-respath.hpp.in kazvtest-respath.hpp)
-add_executable(kazvtest
- testmain.cpp
- basejobtest.cpp
+function(libkazv_add_tests)
+ set(options "")
+ set(oneValueArgs "")
+ set(multiValueArgs EXTRA_LINK_LIBRARIES EXTRA_INCLUDE_DIRECTORIES)
+ cmake_parse_arguments(PARSE_ARGV 0 libkazv_add_tests "${options}" "${oneValueArgs}" "${multiValueArgs}")
+ foreach(test_source ${libkazv_add_tests_UNPARSED_ARGUMENTS})
+ string(REGEX REPLACE "\\.cpp$" "" test_executable "${test_source}")
+ string(REGEX REPLACE "/|\\\\" "--" test_executable "${test_executable}")
+ message(STATUS "Test ${test_executable} added")
+ add_executable("${test_executable}" "${test_source}")
+ target_link_libraries("${test_executable}"
+ PRIVATE Catch2::Catch2WithMain
+ Threads::Threads
+ ${libkazv_add_tests_EXTRA_LINK_LIBRARIES}
+ )
+
+ target_include_directories(
+ "${test_executable}"
+ PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/..
+ ${libkazv_add_tests_EXTRA_INCLUDE_DIRECTORIES}
+ )
+
+ target_compile_definitions("${test_executable}" PRIVATE CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS)
+
+ add_test(NAME "${test_executable}" COMMAND "${test_executable}" "--allow-running-no-tests" "~[needs-internet]")
+ endforeach()
+endfunction()
+
+libkazv_add_tests(
event-test.cpp
cursorutiltest.cpp
base/serialization-test.cpp
base/types-test.cpp
base/immer-utils-test.cpp
base/json-utils-test.cpp
+ EXTRA_LINK_LIBRARIES kazvbase
+)
- client/client-test-util.cpp
+add_library(client-test-lib SHARED client/client-test-util.cpp)
+target_link_libraries(client-test-lib PUBLIC kazvjob kazvclient)
+libkazv_add_tests(
client/discovery-test.cpp
client/sync-test.cpp
client/content-test.cpp
client/paginate-test.cpp
client/util-test.cpp
client/serialization-test.cpp
client/encrypted-file-test.cpp
client/sdk-test.cpp
client/thread-safety-test.cpp
client/room-test.cpp
client/random-generator-test.cpp
client/profile-test.cpp
client/kick-test.cpp
client/ban-test.cpp
client/join-test.cpp
client/keys-test.cpp
client/device-ops-test.cpp
client/send-test.cpp
client/encryption-test.cpp
client/redact-test.cpp
client/tagging-test.cpp
client/account-data-test.cpp
client/room/room-actions-test.cpp
client/room/local-echo-test.cpp
client/room/event-relationships-test.cpp
client/room/member-membership-test.cpp
client/push-rules-desc-test.cpp
client/notification-handler-test.cpp
client/validator-test.cpp
client/power-levels-desc-test.cpp
client/client-test.cpp
client/create-room-test.cpp
client/device-list-tracker-benchmark-test.cpp
client/room/read-receipt-test.cpp
+ EXTRA_LINK_LIBRARIES kazvclient kazveventemitter kazvjob client-test-lib kazvtestfixtures
+ EXTRA_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/client
+)
+libkazv_add_tests(
+ basejobtest.cpp
kazvjobtest.cpp
- event-emitter-test.cpp
- crypto-test.cpp
- crypto/deterministic-test.cpp
- promise-test.cpp
- store-test.cpp
file-desc-test.cpp
- )
+ EXTRA_LINK_LIBRARIES kazvbase kazvjob
+)
-target_include_directories(
- kazvtest
- PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/client)
+libkazv_add_tests(
+ promise-test.cpp
+ EXTRA_LINK_LIBRARIES kazvbase kazvjob kazvstore
+)
-target_compile_definitions(kazvtest PRIVATE CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS)
+libkazv_add_tests(
+ event-emitter-test.cpp
+ EXTRA_LINK_LIBRARIES kazvbase kazveventemitter
+)
-target_link_libraries(kazvtest
- PRIVATE Catch2::Catch2WithMain
- PRIVATE kazv
- PRIVATE kazveventemitter
- PRIVATE kazvjob
- PRIVATE nlohmann_json::nlohmann_json
- PRIVATE immer
- PRIVATE lager
- PRIVATE zug
- PRIVATE kazvtestfixtures
+libkazv_add_tests(
+ crypto-test.cpp
+ crypto/deterministic-test.cpp
+ EXTRA_LINK_LIBRARIES kazvcrypto
)
-add_test(NAME kazvtest COMMAND kazvtest "~[needs-internet]")
+libkazv_add_tests(
+ store-test.cpp
+ EXTRA_LINK_LIBRARIES kazvstore kazvjob
+)
diff --git a/src/tests/crypto/deterministic-test.cpp b/src/tests/crypto/deterministic-test.cpp
index 70112e5..cc175c6 100644
--- a/src/tests/crypto/deterministic-test.cpp
+++ b/src/tests/crypto/deterministic-test.cpp
@@ -1,49 +1,49 @@
/*
* This file is part of libkazv.
* SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libkazv-config.hpp>
#include <catch2/catch_all.hpp>
#include <crypto.hpp>
#include <outbound-group-session.hpp>
-#include <random-generator.hpp>
+#include <client/random-generator.hpp>
using namespace Kazv;
TEST_CASE("Deterministic constructors of Crypto and OutboundGroupSession", "[crypto][deterministic]")
{
auto rg = RandomInterface{RandomDeviceGenerator{}};
auto random = rg.generateRange<RandomData>(Crypto::constructRandomSize());
Crypto crypto1(RandomTag{}, random);
Crypto crypto2(RandomTag{}, random);
REQUIRE(crypto1.toJson() == crypto2.toJson());
random = rg.generateRange<RandomData>(OutboundGroupSession::constructRandomSize());
auto creationTime = currentTimeMs();
OutboundGroupSession ogs1(RandomTag{}, random, creationTime);
OutboundGroupSession ogs2(RandomTag{}, random, creationTime);
REQUIRE(nlohmann::json(ogs1) == nlohmann::json(ogs2));
}
TEST_CASE("Deterministic generating of one-time keys", "[crypto][deterministic]")
{
auto rg = RandomInterface{RandomDeviceGenerator{}};
auto random = rg.generateRange<RandomData>(Crypto::constructRandomSize());
Crypto crypto1(RandomTag{}, random);
Crypto crypto2(crypto1);
auto numKeysToGen = 2;
random = rg.generateRange<RandomData>(Crypto::genOneTimeKeysRandomSize(numKeysToGen));
crypto1.genOneTimeKeysWithRandom(random, numKeysToGen);
crypto2.genOneTimeKeysWithRandom(random, numKeysToGen);
REQUIRE(crypto1.toJson() == crypto2.toJson());
}
diff --git a/src/tests/testmain.cpp b/src/tests/testmain.cpp
deleted file mode 100644
index 0304f60..0000000
--- a/src/tests/testmain.cpp
+++ /dev/null
@@ -1,10 +0,0 @@
-/*
- * This file is part of libkazv.
- * SPDX-FileCopyrightText: 2020 Tusooa Zhu
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
-
-#include <libkazv-config.hpp>
-
-
-#include <catch2/catch_all.hpp>

File Metadata

Mime Type
text/x-diff
Expires
Sun, Jan 19, 6:49 PM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55386
Default Alt Text
(15 KB)

Event Timeline