Changeset View
Changeset View
Standalone View
Standalone View
src/tests/crypto/key-export-test.cpp
| /* | /* | ||||
| * This file is part of libkazv. | * This file is part of libkazv. | ||||
| * SPDX-FileCopyrightText: 2025 tusooa <tusooa@kazv.moe> | * SPDX-FileCopyrightText: 2025 tusooa <tusooa@kazv.moe> | ||||
| * SPDX-License-Identifier: AGPL-3.0-or-later | * SPDX-License-Identifier: AGPL-3.0-or-later | ||||
| */ | */ | ||||
| #include <libkazv-config.hpp> | #include <libkazv-config.hpp> | ||||
| #include <catch2/catch_test_macros.hpp> | #include <catch2/catch_test_macros.hpp> | ||||
| #include <key-export.hpp> | #include <key-export.hpp> | ||||
| #include <crypto.hpp> | #include <crypto.hpp> | ||||
| #include <crypto-util.hpp> | |||||
| using namespace Kazv; | using namespace Kazv; | ||||
| static const std::string password = "test"; | static const std::string password = "test"; | ||||
| static const std::string salt = "\x5e\x91\xe3\x75\xcc\x7b\xaf\x04\x91\xfe\xe3\x07\x89\x58\x1c\xb8"; | static const std::string salt = "\x5e\x91\xe3\x75\xcc\x7b\xaf\x04\x91\xfe\xe3\x07\x89\x58\x1c\xb8"; | ||||
| static const std::size_t iterations = 1000000; | static const std::size_t iterations = 1000000; | ||||
| static const ByteArray iv(16, 0); | static const ByteArray iv(16, 0); | ||||
| static const ByteArray expEncryptionKey = {5, 60, 69, 180, 233, 146, 45, 161, 158, 115, 248, 156, 6, 242, 138, 45, 103, 156, 226, 255, 176, 37, 30, 182, 138, 0, 240, 157, 217, 116, 141, 128}; | static const ByteArray expEncryptionKey = {5, 60, 69, 180, 233, 146, 45, 161, 158, 115, 248, 156, 6, 242, 138, 45, 103, 156, 226, 255, 176, 37, 30, 182, 138, 0, 240, 157, 217, 116, 141, 128}; | ||||
| ▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | auto plainText = R"({ | ||||
| "type": "m.room.message", | "type": "m.room.message", | ||||
| "room_id": "!room:example.org" | "room_id": "!room:example.org" | ||||
| })"_json; | })"_json; | ||||
| auto encryptedContent = a.encryptMegOlm(plainText); | auto encryptedContent = a.encryptMegOlm(plainText); | ||||
| auto sessionId = encryptedContent["session_id"].template get<std::string>(); | auto sessionId = encryptedContent["session_id"].template get<std::string>(); | ||||
| auto exportJson = nlohmann::json{ | auto exportJson = nlohmann::json{ | ||||
| { | { | ||||
| {"algorithm", "m.megolm.v1.aes-sha2"}, | {"algorithm", "m.megolm.v1.aes-sha2"}, | ||||
| {"forwarding_curve25519_key_chain", {}}, | {"forwarding_curve25519_key_chain", nlohmann::json::array()}, | ||||
| {"room_id", roomId}, | {"room_id", roomId}, | ||||
| {"sender_claimed_keys", { | {"sender_claimed_keys", { | ||||
| {"ed25519", a.ed25519IdentityKey()}, | {"ed25519", a.ed25519IdentityKey()}, | ||||
| }}, | }}, | ||||
| {"sender_key", a.curve25519IdentityKey()}, | {"sender_key", a.curve25519IdentityKey()}, | ||||
| {"session_id", sessionId}, | {"session_id", sessionId}, | ||||
| {"session_key", sessionKey}, | {"session_key", sessionKey}, | ||||
| }, | }, | ||||
| }; | }; | ||||
| Crypto b(RandomTag{}, genRandomData(Crypto::constructRandomSize())); | Crypto b(RandomTag{}, genRandomData(Crypto::constructRandomSize())); | ||||
| WHEN("importing one") | WHEN("importing one") | ||||
| { | { | ||||
| auto res = b.importInboundGroupSessions(exportJson); | auto res = b.importInboundGroupSessions(exportJson); | ||||
| REQUIRE(res == 1); | REQUIRE(res == 1); | ||||
| auto encryptedEvent = nlohmann::json{ | |||||
| {"content", encryptedContent}, | |||||
| {"algorithm", CryptoConstants::megOlmAlgo}, | |||||
| {"room_id", roomId}, | |||||
| {"event_id", "!abc"}, | |||||
| {"origin_server_ts", 1000}, | |||||
| }; | |||||
| auto decrypted = b.decrypt(encryptedEvent); | |||||
| REQUIRE(decrypted.has_value()); | |||||
| REQUIRE(nlohmann::json::parse(decrypted.value()) == plainText); | |||||
| } | } | ||||
| WHEN("importing empty array") | WHEN("importing empty array") | ||||
| { | { | ||||
| auto res = b.importInboundGroupSessions(nlohmann::json::array()); | auto res = b.importInboundGroupSessions(nlohmann::json::array()); | ||||
| REQUIRE(res == 0); | REQUIRE(res == 0); | ||||
| } | } | ||||
| Show All 23 Lines | |||||