aboutsummaryrefslogtreecommitdiff
path: root/nearby/presence/np_cpp_ffi/tests/v1_private_identity_tests.cc
diff options
context:
space:
mode:
Diffstat (limited to 'nearby/presence/np_cpp_ffi/tests/v1_private_identity_tests.cc')
-rw-r--r--nearby/presence/np_cpp_ffi/tests/v1_private_identity_tests.cc130
1 files changed, 130 insertions, 0 deletions
diff --git a/nearby/presence/np_cpp_ffi/tests/v1_private_identity_tests.cc b/nearby/presence/np_cpp_ffi/tests/v1_private_identity_tests.cc
new file mode 100644
index 0000000..8cd0bd0
--- /dev/null
+++ b/nearby/presence/np_cpp_ffi/tests/v1_private_identity_tests.cc
@@ -0,0 +1,130 @@
+// Copyright 2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "nearby_protocol.h"
+#include "np_cpp_test.h"
+#include "gtest/gtest.h"
+#include <algorithm>
+
+// All canned data output from np_adv/tests/examples_v1.rs
+static nearby_protocol::RawAdvertisementPayload
+ V1AdvPrivateIdentity(nearby_protocol::ByteBuffer<255>(
+ {105,
+ {
+ 0x20, 0x67, 0x91, 0x10, 0x08, 0xAD, 0x69, 0x46, 0x04, 0x5D, 0xAE,
+ 0x6D, 0xB7, 0xF7, 0x5C, 0xD3, 0xB8, 0xAC, 0xF0, 0xBF, 0x75, 0x90,
+ 0x01, 0xBE, 0x73, 0x33, 0xA4, 0x76, 0x84, 0x4A, 0x09, 0x0F, 0x2B,
+ 0x99, 0x47, 0xDF, 0x8B, 0x46, 0xCA, 0x16, 0xCE, 0x13, 0xB5, 0x6E,
+ 0x53, 0xAE, 0x28, 0x56, 0x44, 0x0E, 0xA6, 0x8D, 0xEB, 0xA1, 0x11,
+ 0xAF, 0x4E, 0x1B, 0xE0, 0x8E, 0xF5, 0xBA, 0x90, 0x4F, 0x2E, 0x94,
+ 0xFC, 0xDE, 0xA6, 0x7F, 0x5D, 0xC8, 0x37, 0xB7, 0xEF, 0xCA, 0xAC,
+ 0x8B, 0x9F, 0x1B, 0xD4, 0xC6, 0x11, 0x85, 0xD3, 0x67, 0x39, 0x32,
+ 0xD1, 0x82, 0xCA, 0x4E, 0xB9, 0x46, 0x03, 0x83, 0x68, 0x56, 0x0B,
+ 0xCC, 0xFD, 0x7A, 0x2A, 0xBE, 0x07,
+ }}));
+
+static std::array<uint8_t, 32> key_seed = {
+ 0x31, 0x43, 0x63, 0x1E, 0xCA, 0xE8, 0x97, 0x4B, 0x96, 0x50, 0xCC,
+ 0x1C, 0x48, 0x25, 0x0E, 0x81, 0x58, 0x06, 0x81, 0x51, 0xF9, 0xEB,
+ 0x25, 0x23, 0x03, 0xD4, 0x97, 0x6D, 0x95, 0x19, 0x91, 0x39,
+};
+
+static std::array<uint8_t, 32> expected_unsigned_metadata_key_hmac = {0};
+
+static std::array<uint8_t, 32> expected_signed_metadata_key_hmac = {
+ 0x1C, 0xBC, 0xEB, 0xDC, 0x17, 0xB5, 0x91, 0xE5, 0x07, 0x9D, 0x70,
+ 0xC1, 0xE8, 0x4B, 0xCC, 0xDB, 0x4B, 0x0F, 0x76, 0x83, 0x59, 0x62,
+ 0x0A, 0x2D, 0x55, 0x0B, 0x3B, 0x36, 0xA4, 0x92, 0x8B, 0x13,
+};
+
+static std::array<uint8_t, 32> public_key = {
+ 0x6D, 0x0D, 0xB6, 0x09, 0x10, 0xB1, 0x4E, 0xC4, 0x7E, 0x10, 0x16,
+ 0x14, 0x9C, 0x9F, 0xF2, 0x14, 0x0F, 0xEC, 0x53, 0x76, 0xE3, 0x07,
+ 0xD9, 0xD3, 0x9E, 0xAE, 0xE7, 0x45, 0x2C, 0x03, 0xEC, 0x6D,
+};
+
+static uint8_t encrypted_metadata[] = {
+ 0x09, 0xB8, 0xC6, 0x6B, 0x71, 0x43, 0x55, 0x4C, 0xB9, 0x9D, 0xBF,
+ 0xE4, 0xAF, 0x3E, 0xA2, 0x56, 0x0E, 0x6C, 0xBC, 0xDC, 0x3F, 0x3F,
+ 0x0D, 0x28, 0xD4, 0x50, 0xA9, 0xEA, 0xC3, 0x60, 0xB0, 0x81, 0x31,
+ 0xE2, 0x67, 0xB5, 0xC8, 0x15, 0x0C, 0xCA, 0x0B, 0x9B, 0x2C, 0x80,
+ 0xC1, 0xB1, 0xF6, 0x5F, 0xE1, 0x51, 0xF9, 0xE2, 0x23, 0x56, 0xD4,
+ 0x0B, 0x89, 0xA7, 0xF3, 0x4D, 0xE8, 0x79, 0x26, 0x44, 0x7E, 0x62,
+ 0xDE, 0x53, 0x13, 0x15, 0x3D, 0xFC, 0x04, 0x2E, 0x2D, 0x08, 0x43,
+ 0x2E, 0xE1, 0x96, 0xE8, 0x0F, 0xD0, 0xFC, 0xDE, 0x03, 0x86, 0x23,
+ 0xB6, 0x98, 0x85, 0x27, 0x67, 0xD8, 0x1D, 0xC3, 0xE2, 0xE0, 0xA4,
+ 0x32, 0x1A, 0x5F, 0x51, 0x0B, 0xA8, 0xD8, 0xA7, 0x23, 0xA4, 0x57,
+};
+
+TEST_F(NpCppTest, V1PrivateIdentitySimpleCase) {
+ auto slab_result = nearby_protocol::CredentialSlab::TryCreate();
+ ASSERT_TRUE(slab_result.ok());
+
+ std::span<uint8_t> metadata_span(encrypted_metadata);
+ nearby_protocol::MatchedCredentialData match_data(123, metadata_span);
+
+ nearby_protocol::V1MatchableCredential v1_cred(
+ key_seed, expected_unsigned_metadata_key_hmac,
+ expected_signed_metadata_key_hmac, public_key, match_data);
+
+ auto add_result = slab_result->AddV1Credential(v1_cred);
+ ASSERT_EQ(add_result, absl::OkStatus());
+
+ auto book_result =
+ nearby_protocol::CredentialBook::TryCreateFromSlab(*slab_result);
+ ASSERT_TRUE(book_result.ok());
+
+ auto deserialize_result =
+ nearby_protocol::Deserializer::DeserializeAdvertisement(
+ V1AdvPrivateIdentity, *book_result);
+ ASSERT_EQ(deserialize_result.GetKind(),
+ nearby_protocol::DeserializeAdvertisementResultKind::V1);
+
+ auto v1_adv = deserialize_result.IntoV1();
+ ASSERT_EQ(v1_adv.GetNumUndecryptableSections(), 0);
+ ASSERT_EQ(v1_adv.GetNumLegibleSections(), 1);
+
+ auto section = v1_adv.TryGetSection(0);
+ ASSERT_TRUE(section.ok());
+ ASSERT_EQ(section->GetIdentityKind(),
+ nearby_protocol::DeserializedV1IdentityKind::Decrypted);
+ ASSERT_EQ(section->NumberOfDataElements(), 1);
+
+ auto metadata = section->DecryptMetadata();
+ ASSERT_TRUE(metadata.ok());
+ ASSERT_EQ(
+ std::string("{\"uuid\":\"378845e1-2616-420d-86f5-674177a7504d\","
+ "\"display_name\":\"Alice\",\"location\":\"Wonderland\"}"),
+ std::string(metadata->begin(), metadata->end()));
+
+ auto identity_details = section->GetIdentityDetails();
+ ASSERT_TRUE(identity_details.ok());
+ ASSERT_EQ(identity_details->cred_id, 123);
+ ASSERT_EQ(identity_details->verification_mode,
+ nearby_protocol::V1VerificationMode::Signature);
+ ASSERT_EQ(identity_details->identity_type,
+ nearby_protocol::EncryptedIdentityType::Private);
+
+ auto de = section->TryGetDataElement(0);
+ ASSERT_TRUE(de.ok());
+ ASSERT_EQ(de->GetDataElementTypeCode(), 5);
+ ASSERT_EQ(de->GetPayload().ToVector(), std::vector<uint8_t>{7});
+
+ auto offset = de->GetOffset();
+ auto derived_salt = section->DeriveSaltForOffset(offset);
+ ASSERT_TRUE(derived_salt.ok());
+ std::array<uint8_t, 16> expected =
+ {94, 154, 245, 152, 164, 22, 131, 157, 8, 79, 28, 77, 236, 57, 17, 97};
+ ASSERT_EQ(*derived_salt, expected);
+} \ No newline at end of file