diff options
author | Better Together Rust Devs <no-reply@google.com> | 2024-01-10 21:07:56 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2024-01-10 21:07:56 +0000 |
commit | 524a5e276bc40a675f207ec5d65eae68abf7099f (patch) | |
tree | 130a48b0d282dd0c937e3a60bbc796ba57d12073 /nearby/presence/np_cpp_ffi/tests/credential_slab_tests.cc | |
parent | 9cb9ac223ef2b89e8ba2ced1f352a650fd77112b (diff) | |
parent | 78251ce4fcad128d3ca1c23458786323e1b7f54c (diff) | |
download | beto-rust-524a5e276bc40a675f207ec5d65eae68abf7099f.tar.gz |
Project import generated by Copybara. am: 45cc997519 am: 78251ce4fc
Original change: https://android-review.googlesource.com/c/platform/external/rust/beto-rust/+/2903117
Change-Id: I7b0230c1cb073fb83e694eb9e971f75a3c5f62f1
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'nearby/presence/np_cpp_ffi/tests/credential_slab_tests.cc')
-rw-r--r-- | nearby/presence/np_cpp_ffi/tests/credential_slab_tests.cc | 178 |
1 files changed, 174 insertions, 4 deletions
diff --git a/nearby/presence/np_cpp_ffi/tests/credential_slab_tests.cc b/nearby/presence/np_cpp_ffi/tests/credential_slab_tests.cc index f715361..c8ddb3c 100644 --- a/nearby/presence/np_cpp_ffi/tests/credential_slab_tests.cc +++ b/nearby/presence/np_cpp_ffi/tests/credential_slab_tests.cc @@ -13,8 +13,8 @@ // limitations under the License. #include "nearby_protocol.h" -#include "shared_test_util.h" #include "np_cpp_test.h" +#include "shared_test_util.h" #include "gtest/gtest.h" @@ -41,16 +41,186 @@ TEST_F(NpCppTest, TestSlabMoveConstructor) { // credential-book. nearby_protocol::CredentialSlab next_slab(std::move(slab)); - auto maybe_book = nearby_protocol::CredentialBook::TryCreateFromSlab(next_slab); + auto maybe_book = + nearby_protocol::CredentialBook::TryCreateFromSlab(next_slab); ASSERT_TRUE(maybe_book.ok()); // Now, both slabs should be moved-out-of, since `TryCreateFromSlab` takes // ownership. Verify that this is the case, and attempts to re-use the slabs // result in an assert failure. ASSERT_DEATH([[maybe_unused]] auto failure = - nearby_protocol::CredentialBook::TryCreateFromSlab(slab), //NOLINT(bugprone-use-after-move) + nearby_protocol::CredentialBook::TryCreateFromSlab( + slab), // NOLINT(bugprone-use-after-move) + ""); + ASSERT_DEATH( + [[maybe_unused]] auto failure = + nearby_protocol::CredentialBook::TryCreateFromSlab(next_slab), + ""); +} + +TEST_F(NpCppTest, TestSlabDestructor) { + { + auto slab1_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab1_result.ok()); + + auto slab2_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab2_result.ok()); + + auto slab3_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab3_result.ok()); + + auto slab4_result = nearby_protocol::CredentialSlab::TryCreate(); + + ASSERT_FALSE(slab4_result.ok()); + ASSERT_TRUE(absl::IsResourceExhausted(slab4_result.status())); + } + + // Now that the above variables have gone out of scope we should verify that + // the destructor succeeded in cleaning up those resources + auto slab_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab_result.ok()); +} + +TEST_F(NpCppTest, TestSlabMoveAssignment) { + auto slab_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab_result.ok()); + + // create a second slab + auto other_slab_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(other_slab_result.ok()); + + // move assignment should override currently assigned slab with new one, + // freeing the existing one. + auto other_slab = std::move(slab_result.value()); + auto maybe_book = + nearby_protocol::CredentialBook::TryCreateFromSlab(other_slab); + ASSERT_TRUE(maybe_book.ok()); + + // The old object should now lead to use after moved assert failure + ASSERT_DEATH([[maybe_unused]] auto failure = + nearby_protocol::CredentialBook::TryCreateFromSlab( + slab_result.value()), // NOLINT(bugprone-use-after-move) ""); + + // moving again should still lead to a use after moved assert failure + auto another_moved_book = std::move(slab_result.value()); ASSERT_DEATH([[maybe_unused]] auto failure = - nearby_protocol::CredentialBook::TryCreateFromSlab(next_slab), + nearby_protocol::CredentialBook::TryCreateFromSlab( + another_moved_book), // NOLINT(bugprone-use-after-move) ""); } + +TEST_F(NpCppTest, TestAddV0Credential) { + auto slab_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab_result.ok()); + + uint8_t metadata[] = {1, 2, 3}; + std::span<uint8_t> metadata_span(metadata); + + nearby_protocol::MatchedCredentialData match_data(111, metadata_span); + std::array<uint8_t, 32> key_seed{1, 2, 3}; + std::array<uint8_t, 32> legacy_metadata_key_hmac{1, 2, 3}; + + nearby_protocol::V0MatchableCredential v0_cred( + key_seed, legacy_metadata_key_hmac, match_data); + auto add_result = slab_result.value().AddV0Credential(v0_cred); + ASSERT_EQ(add_result, absl::OkStatus()); +} + +TEST_F(NpCppTest, TestAddV0CredentialAfterMoved) { + auto slab_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab_result.ok()); + + // creating a book will move the slab + auto maybe_book = + nearby_protocol::CredentialBook::TryCreateFromSlab(slab_result.value()); + ASSERT_TRUE(maybe_book.ok()); + + uint8_t metadata[] = {1, 2, 3}; + std::span<uint8_t> metadata_span(metadata); + nearby_protocol::MatchedCredentialData match_data(111, metadata_span); + std::array<uint8_t, 32> key_seed{1, 2, 3}; + std::array<uint8_t, 32> legacy_metadata_key_hmac{1, 2, 3}; + nearby_protocol::V0MatchableCredential v0_cred( + key_seed, legacy_metadata_key_hmac, match_data); + + ASSERT_DEATH([[maybe_unused]] auto add_result = + slab_result.value().AddV0Credential(v0_cred); + , ""); +} + +TEST_F(NpCppTest, TestAddV1Credential) { + auto slab_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab_result.ok()); + + uint8_t metadata[] = {1, 2, 3}; + std::span<uint8_t> metadata_span(metadata); + nearby_protocol::MatchedCredentialData match_data(111, metadata_span); + std::array<uint8_t, 32> key_seed{1, 2, 3}; + std::array<uint8_t, 32> expected_unsigned_metadata_key_hmac{1, 2, 3}; + std::array<uint8_t, 32> expected_signed_metadata_key_hmac{1, 2, 3}; + std::array<uint8_t, 32> pub_key{1, 2, 3}; + nearby_protocol::V1MatchableCredential v1_cred( + key_seed, expected_unsigned_metadata_key_hmac, + expected_signed_metadata_key_hmac, pub_key, match_data); + + auto add_result = slab_result.value().AddV1Credential(v1_cred); + ASSERT_EQ(add_result, absl::OkStatus()); +} + +TEST_F(NpCppTest, TestAddV1CredentialAfterMoved) { + auto slab_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab_result.ok()); + + // creating a book will move the slab + auto maybe_book = + nearby_protocol::CredentialBook::TryCreateFromSlab(slab_result.value()); + ASSERT_TRUE(maybe_book.ok()); + + uint8_t metadata[] = {1, 2, 3}; + std::span<uint8_t> metadata_span(metadata); + nearby_protocol::MatchedCredentialData match_data(111, metadata_span); + std::array<uint8_t, 32> key_seed{1, 2, 3}; + std::array<uint8_t, 32> expected_unsigned_metadata_key_hmac{1, 2, 3}; + std::array<uint8_t, 32> expected_signed_metadata_key_hmac{1, 2, 3}; + std::array<uint8_t, 32> pub_key{1, 2, 3}; + nearby_protocol::V1MatchableCredential v1_cred( + key_seed, expected_unsigned_metadata_key_hmac, + expected_signed_metadata_key_hmac, pub_key, match_data); + + ASSERT_DEATH([[maybe_unused]] auto add_result = + slab_result.value().AddV1Credential(v1_cred); + , ""); +} + +// make sure the book can be populated with many credentials +TEST_F(NpCppTest, TestAddManyCredentials) { + auto slab_result = nearby_protocol::CredentialSlab::TryCreate(); + ASSERT_TRUE(slab_result.ok()); + + // Should be able to load the slab up with many credentials + for (int i = 0; i < 500; i++) { + uint8_t metadata[] = {1, 2, 3}; + std::span<uint8_t> metadata_span(metadata); + nearby_protocol::MatchedCredentialData match_data(111, metadata_span); + std::array<uint8_t, 32> key_seed{1, 2, 3}; + std::array<uint8_t, 32> legacy_metadata_key_hmac{1, 2, 3}; + nearby_protocol::V0MatchableCredential v0_cred( + key_seed, legacy_metadata_key_hmac, match_data); + auto add_result = slab_result->AddV0Credential(v0_cred); + ASSERT_EQ(add_result, absl::OkStatus()); + + std::array<uint8_t, 32> v1_key_seed{1, 2, 3}; + std::array<uint8_t, 32> v1_expected_unsigned_metadata_key_hmac{1, 2, 3}; + std::array<uint8_t, 32> v1_expected_signed_metadata_key_hmac{1, 2, 3}; + std::array<uint8_t, 32> v1_pub_key{1, 2, 3}; + nearby_protocol::V1MatchableCredential v1_cred( + v1_key_seed, v1_expected_unsigned_metadata_key_hmac, + v1_expected_signed_metadata_key_hmac, v1_pub_key, match_data); + + auto add_v1_result = slab_result->AddV1Credential(v1_cred); + ASSERT_EQ(add_v1_result, absl::OkStatus()); + } + ASSERT_TRUE( + nearby_protocol::CredentialBook::TryCreateFromSlab(*slab_result).ok()); +} |