account_test.cpp (2617B)
1 #include <snitch/snitch.hpp> 2 #include "account.hpp" 3 #include "errors.hpp" 4 #include <botan/auto_rng.h> 5 #include <botan/pubkey.h> 6 7 using namespace spank_olm; 8 9 TEST_CASE("Account serialization and deserialization") 10 { 11 Botan::AutoSeeded_RNG rng; 12 Account account; 13 account.new_account(rng); 14 account.generate_one_time_keys(rng, 5); 15 account.generate_fallback_key(rng); 16 17 const auto serialized = account.pickle(); 18 const auto deserialized = Account::unpickle(serialized); 19 20 REQUIRE( 21 account.identity_keys->ed25519_key.public_key()->raw_public_key_bits() == deserialized.identity_keys-> 22 ed25519_key.public_key()->raw_public_key_bits()); 23 REQUIRE( 24 account.identity_keys->curve25519_key.public_key()->raw_public_key_bits() == deserialized.identity_keys-> 25 curve25519_key.public_key()->raw_public_key_bits()); 26 REQUIRE(account.one_time_keys.size() == deserialized.one_time_keys.size()); 27 REQUIRE(account.next_one_time_key_id == deserialized.next_one_time_key_id); 28 } 29 30 TEST_CASE("Account sign and verify") 31 { 32 Botan::AutoSeeded_RNG rng; 33 Account account; 34 account.new_account(rng); 35 36 const std::string message = "Test message"; 37 auto signature = account.sign(rng, message); 38 39 Botan::PK_Verifier verifier(account.identity_keys->ed25519_key, "Ed25519ph"); 40 verifier.update(message); 41 REQUIRE(verifier.check_signature(signature)); 42 } 43 44 TEST_CASE("Account generate and mark keys as published") 45 { 46 Botan::AutoSeeded_RNG rng; 47 Account account; 48 account.new_account(rng); 49 account.generate_one_time_keys(rng, 5); 50 51 REQUIRE(account.one_time_keys.size() == 5); 52 53 const auto published_count = account.mark_keys_as_published(); 54 REQUIRE(published_count == 5); 55 56 for (const auto& key : account.one_time_keys) 57 { 58 REQUIRE(key->published == true); 59 } 60 } 61 62 TEST_CASE("Account generate and forget fallback key") 63 { 64 Botan::AutoSeeded_RNG rng; 65 Account account; 66 account.new_account(rng); 67 account.generate_fallback_key(rng); 68 69 70 account.generate_fallback_key(rng); 71 72 account.forget_old_fallback_key(); 73 REQUIRE(account.prev_fallback_key == std::nullopt); 74 } 75 76 TEST_CASE("Account lookup and remove key") 77 { 78 Botan::AutoSeeded_RNG rng; 79 Account account; 80 account.new_account(rng); 81 account.generate_one_time_keys(rng, 1); 82 83 const auto key = account.one_time_keys[0].key.public_key(); 84 auto lookup_result = account.lookup_key(*key); 85 REQUIRE(lookup_result.has_value()); 86 87 account.remove_key(*key); 88 lookup_result = account.lookup_key(*key); 89 REQUIRE(!lookup_result.has_value()); 90 }