Changeset View
Changeset View
Standalone View
Standalone View
src/tests/client/encryption-test.cpp
| /* | /* | ||||
| * This file is part of libkazv. | * This file is part of libkazv. | ||||
| * SPDX-FileCopyrightText: 2021-2023 tusooa <tusooa@kazv.moe> | * SPDX-FileCopyrightText: 2021-2023 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_all.hpp> | #include <catch2/catch_all.hpp> | ||||
| #include <client/actions/encryption.hpp> | #include <client/actions/encryption.hpp> | ||||
| #include <client-model.hpp> | #include <client-model.hpp> | ||||
| #include "key-export.hpp" | |||||
| #include "client-test-util.hpp" | #include "client-test-util.hpp" | ||||
| #include "action-mock-utils.hpp" | |||||
| #include "factory.hpp" | #include "factory.hpp" | ||||
| using namespace Kazv::Factory; | using namespace Kazv::Factory; | ||||
| namespace | namespace | ||||
| { | { | ||||
| struct CreateE2EESessionResult | struct CreateE2EESessionResult | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 363 Lines • ▼ Show 20 Lines | TEST_CASE("tryDecryptEvents() will update room.undecryptedEvents", "[client][encryption]") | ||||
| REQUIRE(!decryptedEvent3.decrypted()); | REQUIRE(!decryptedEvent3.decrypted()); | ||||
| REQUIRE(nextClient.roomList.rooms[roomId].undecryptedEvents | REQUIRE(nextClient.roomList.rooms[roomId].undecryptedEvents | ||||
| == | == | ||||
| immer::map<std::string, immer::flex_vector<std::string>>{ | immer::map<std::string, immer::flex_vector<std::string>>{ | ||||
| {encrypted.originalJson().get()["content"]["session_id"], {encrypted2.id()}}, | {encrypted.originalJson().get()["content"]["session_id"], {encrypted2.id()}}, | ||||
| {encrypted3.originalJson().get()["content"]["session_id"], {encrypted3.id()}}, | {encrypted3.originalJson().get()["content"]["session_id"], {encrypted3.id()}}, | ||||
| }); | }); | ||||
| } | |||||
| static const std::string password = "test"; | |||||
| static const std::string backupFile = R"(-----BEGIN MEGOLM SESSION DATA----- | |||||
| AV6R43XMe68Ekf7jB4lYHLgAAAAAAAAAAAAAAAAAAAAAAA9CQG628kByLP7LApTtbvhnpgFnUUJ | |||||
| +tRMkpw4zcGoTOJya9/lawfRWKjd8LZeuHKdNLkEhfIAE16Xmqv+uU8oEASPxjLDOMjsgBKLMRx | |||||
| /iwUR7Aoe4wjuwEcdEEOW+T6ffjUz5LmEJcI14qZ1wXUPk1pnNmz+4nX8+a9UxgEpAN7vsmilwz | |||||
| P4PXNubhvGsqtZpy44pP6Td0alYgwVfTXqWB1KokMjuQE+2q6/Jb6U/z5D5nv8ArcJL04cD0U6r | |||||
| ySsRWI9Jra2OcKFQxgLeVpRAiP6/sRyl9k1n6eiSOfmGkZ+qnvOfZsQh7Wupgh6zRe8LNEtrYZh | |||||
| FpSaCE+0U8I5hZJrWNBDFfHg+rtzB4BEk0YwpD3rVcWEsk8kKqHmEulEqIXckd1SbSG7y7H1ADB | |||||
| 7mjAY7qWetMizPXD+I8MDUnU1TF3Jv3CIZfZY7BHh2WukmiORlpN4H5s/Wwq2oCIk7qXhCHFvaF | |||||
| uj+XytIz6TmkEVZfXK9zqUCwCU+VYSGl9GVAezO8CZ6aEJes95yYqRxfADdJG2Vtd0oXwrpR0xV | |||||
| 1GO+0JJ3xKicVX6U77iMtJbL1Lge32QvbAcv8o6mcaW28xeeYPrccMIRa3vLtuSDDqKC79S9bIP | |||||
| 2U5F+MHn+5dqMeXcG9K2hS91gsBQMAvX6 | |||||
| -----END MEGOLM SESSION DATA-----)"; | |||||
| TEST_CASE("import keys", "[client][encryption]") | |||||
| { | |||||
| auto u = makeMockSdkUtil(makeClient( | |||||
| withCrypto(makeCrypto()) | |||||
| )); | |||||
| auto c = u.sdk.client(); | |||||
| SECTION("success") | |||||
| { | |||||
| c.importFromKeyBackupFile(backupFile, password) | |||||
| .then([&u](auto stat) { | |||||
| REQUIRE(stat); | |||||
| // the example json in the spec is not a valid key | |||||
| REQUIRE(stat.dataJson("imported") == 0); | |||||
| u.io.stop(); | |||||
| }); | |||||
| u.io.run(); | |||||
| } | |||||
| SECTION("failure") | |||||
| { | |||||
| c.importFromKeyBackupFile(backupFile, "wrongpass") | |||||
| .then([&u](auto stat) { | |||||
| REQUIRE(!stat); | |||||
| REQUIRE(stat.dataStr("error") == DecryptKeyExportErrorCodes::HMAC_FAILED); | |||||
| u.io.stop(); | |||||
| }); | |||||
| u.io.run(); | |||||
| } | |||||
| } | } | ||||