1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef NEARBY_PRESENCE_NP_CPP_FFI_TESTS_SHARED_TEST_UTILS_H_
16 #define NEARBY_PRESENCE_NP_CPP_FFI_TESTS_SHARED_TEST_UTILS_H_
17
18 #include "nearby_protocol.h"
19
20 // Plaintext advertisement bytes
21 constexpr std::array<uint8_t, 2> V0AdvEmptyBytes{0x00, 0x03};
22 constexpr std::array<uint8_t, 2> V1AdvEmptyBytes{0x00, 0x00};
23 constexpr std::array<uint8_t, 3> V0AdvPlaintextBytes{0x00, 0x15, 0x03};
24 constexpr std::array<uint8_t, 6> V0AdvPlaintextMultiDeBytes{
25 0x00, 0x15, 0x05, 0x26, 0x40, 0x40,
26 };
27 constexpr std::array<uint8_t, 5> V1AdvPlaintextBytes{0x20, // Version header
28 0x00, // format
29 0x02, // section len
30 0x15, // Tx power value 3
31 0x03};
32
33 // V0 encrypted advertisement data - ripped out of np_adv/tests/examples_v0.rs
34 constexpr std::array<uint8_t, 19> V0AdvEncryptedBytes{
35 0x04, 0x22, 0x22, 0xD8, 0x22, 0x12, 0xEF, 0x16, 0xDB, 0xF8,
36 0x72, 0xF2, 0xA3, 0xA7, 0xC0, 0xFA, 0x52, 0x48, 0xEC};
37 inline std::vector<uint8_t> V0AdvEncryptedMetadata = {
38 0x8D, 0xD4, 0x7A, 0x58, 0x47, 0x30, 0x65, 0x89, 0x06, 0x16, 0x04, 0xCA,
39 0x68, 0xCE, 0xB4, 0x18, 0xA7, 0x40, 0xB8, 0x36, 0xB0, 0x94, 0xB4, 0xC0,
40 0x6C, 0xAF, 0x7C, 0x4A, 0x3B, 0x68, 0x28, 0x54, 0x8F, 0x36, 0xD5, 0x68,
41 0x12, 0x6C, 0xC2, 0x63, 0x31, 0x4A, 0x48, 0xE4, 0x3B, 0xB3, 0xCF, 0xD4,
42 0x10, 0x49, 0x59, 0xE7, 0x2B, 0xDE, 0xEB, 0x78, 0x44, 0x68,
43 };
44 inline std::string ExpectedV0DecryptedMetadata(
45 R"({"name":"Alice","email":"alice@gmail.com"})");
46 constexpr std::array<uint8_t, 32> V0AdvKeySeed = {
47 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
48 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
49 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
50 };
51 constexpr std::array<uint8_t, 32> V0AdvLegacyIdentityTokenHmac = {
52 0x09, 0xFE, 0x9E, 0x81, 0xB7, 0x3E, 0x5E, 0xCC, 0x76, 0x59, 0x57,
53 0x71, 0xE0, 0x1F, 0xFB, 0x34, 0x38, 0xE7, 0x5F, 0x24, 0xA7, 0x69,
54 0x56, 0xA0, 0xB8, 0xEA, 0x67, 0xD1, 0x1C, 0x3E, 0x36, 0xFD};
55
56 // V1 encrypted advertisement data - ripped out of np_adv/tests/examples_v1.rs
57 constexpr std::array<uint8_t, 101> V1AdvEncryptedBytes{
58 0x20, 0x03, 0x9C, 0xED, 0x67, 0x93, 0x04, 0x6A, 0xBB, 0x1B, 0x1C, 0x40,
59 0x45, 0x1F, 0x34, 0x6C, 0x03, 0xF3, 0xAA, 0x4F, 0x01, 0x8B, 0x7A, 0x5F,
60 0x8A, 0x7B, 0x06, 0x29, 0xA4, 0x12, 0x27, 0x12, 0x63, 0xB4, 0x42, 0x76,
61 0xDE, 0xE9, 0x01, 0x6D, 0xF5, 0x70, 0x1C, 0x09, 0xDF, 0xBE, 0x2E, 0x32,
62 0xE5, 0x64, 0x8A, 0xDF, 0xB0, 0xB5, 0xE0, 0xC1, 0xD9, 0x76, 0x99, 0x4C,
63 0x71, 0xFB, 0xB2, 0xBF, 0xA2, 0xB9, 0xC2, 0xEA, 0x95, 0xE6, 0x6B, 0xFD,
64 0xD7, 0x93, 0x4D, 0xB5, 0x2D, 0xC2, 0x8D, 0x1E, 0x31, 0x32, 0x00, 0x88,
65 0x38, 0xBA, 0x73, 0x39, 0x5E, 0x23, 0xC3, 0x6C, 0x2D, 0x50, 0x22, 0x5B,
66 0xB5, 0xF4, 0x9C, 0x96, 0x37};
67 constexpr std::array<uint8_t, 32> V1AdvKeySeed = {
68 0xF1, 0xEF, 0x9E, 0x34, 0xDC, 0x28, 0xBC, 0x37, 0x5B, 0x6E, 0x4A,
69 0xC4, 0x52, 0xE6, 0x9C, 0xD3, 0x6D, 0xA9, 0xAB, 0x21, 0x5B, 0x02,
70 0xB9, 0x10, 0x27, 0xC3, 0xA9, 0x53, 0xB5, 0x29, 0x4C, 0x36};
71 constexpr std::array<uint8_t, 32>
72 V1AdvExpectedMicExtendedSaltIdentityTokenHmac = {0};
73 constexpr std::array<uint8_t, 32> V1AdvExpectedSignatureIdentityTokenHmac = {
74 0x10, 0x05, 0x45, 0x27, 0x8F, 0xE6, 0x86, 0x5B, 0x51, 0xB0, 0x39,
75 0xEA, 0xB8, 0xD7, 0x5F, 0x61, 0x41, 0xDD, 0x92, 0x62, 0xDD, 0x1A,
76 0x9D, 0xDA, 0x98, 0xDA, 0xF2, 0xDC, 0xA9, 0xD2, 0x45, 0x50};
77 constexpr std::array<uint8_t, 32> V1AdvPublicKey = {
78 0x39, 0xA0, 0x74, 0x81, 0x70, 0x46, 0xFF, 0x72, 0x59, 0xC7, 0x78,
79 0x6C, 0x30, 0x7B, 0xC8, 0x71, 0x26, 0x34, 0xFF, 0x13, 0x61, 0x8C,
80 0xE6, 0x22, 0x46, 0x62, 0x56, 0xFD, 0xC1, 0x7A, 0x01, 0xAE};
81 inline std::vector<uint8_t> V1AdvEncryptedMetadata = {
82 0x7B, 0xE9, 0x66, 0x00, 0x9E, 0x14, 0x50, 0xDE, 0x96, 0xCB, 0x79,
83 0x38, 0x93, 0xC5, 0x15, 0xE9, 0xC2, 0x6B, 0xE8, 0x03, 0x9F, 0x6C,
84 0xCA, 0x94, 0xAD, 0x24, 0x13, 0x27, 0xC1, 0xDE, 0xBC, 0xC2, 0x29,
85 0x88, 0xC0, 0xA5, 0x3B, 0xCA, 0x98, 0x4A, 0x42, 0xC3, 0xDE, 0xF5,
86 0x1C, 0xAE, 0xFF, 0xC0, 0x02, 0xD2, 0xD2, 0x97, 0x8B, 0x52, 0x93,
87 0x71, 0x07, 0x39, 0x87, 0x89, 0xCB, 0xCD, 0x79, 0x0A, 0x19, 0x0E,
88 0x73, 0xEB, 0x24, 0xFA, 0x8F, 0x4C, 0xA7, 0xF3, 0x95, 0x0B, 0xED,
89 0xEF, 0x27, 0x49, 0x8D, 0xE8, 0x7B, 0x88, 0x33, 0x16, 0x83, 0xF6,
90 0xF1, 0xE6, 0x43, 0x22, 0x70, 0x70, 0xFD, 0x9B, 0xA6, 0x52, 0x35,
91 0x0B, 0xBF, 0xBA, 0x0C, 0x20, 0xA3, 0x0C, 0xE7, 0xC7, 0xD0, 0x70};
92 inline std::string ExpectedV1DecryptedMetadata(
93 "{\"uuid\":\"378845e1-2616-420d-86f5-674177a7504d\","
94 "\"display_name\":\"Alice\",\"location\":\"Wonderland\"}");
95
96 // Data suitable for use directly through the C API (bypassing the C++ wrapper)
97 constexpr np_ffi::internal::RawAdvertisementPayload V0AdvMultiDeInternals{
98 {7, {0x00, 0x03, 0x15, 0x05, 0x26, 0x00, 0x46}}};
99
100 // Wrapper types directly usable by C++ API
101 constexpr nearby_protocol::ByteBuffer<255> V0AdvEmptyBuffer(V0AdvEmptyBytes);
102 constexpr nearby_protocol::RawAdvertisementPayload V0AdvEmpty(V0AdvEmptyBuffer);
103
104 constexpr nearby_protocol::ByteBuffer<255> V0AdvPlaintextBuffer(
105 V0AdvPlaintextBytes);
106 constexpr nearby_protocol::RawAdvertisementPayload V0AdvPlaintext(
107 V0AdvPlaintextBuffer);
108
109 constexpr nearby_protocol::ByteBuffer<255> V0AdvPlaintextMultiDeBuffer(
110 V0AdvPlaintextMultiDeBytes);
111 constexpr nearby_protocol::RawAdvertisementPayload V0AdvPlaintextMultiDe(
112 V0AdvPlaintextMultiDeBuffer);
113
114 constexpr nearby_protocol::ByteBuffer<255> V1AdvPlaintextBuffer(
115 V1AdvPlaintextBytes);
116 constexpr nearby_protocol::RawAdvertisementPayload V1AdvPlaintext(
117 V1AdvPlaintextBuffer);
118
119 constexpr nearby_protocol::ByteBuffer<255> V0AdvEncryptedBuffer(
120 V0AdvEncryptedBytes);
121 constexpr nearby_protocol::RawAdvertisementPayload V0AdvEncryptedPayload(
122 V0AdvEncryptedBuffer);
123
124 constexpr nearby_protocol::ByteBuffer<255> V1AdvEncryptedBuffer(
125 V1AdvEncryptedBytes);
126 constexpr nearby_protocol::RawAdvertisementPayload V1AdvEncrypted(
127 V1AdvEncryptedBuffer);
128
129 void test_panic_handler(nearby_protocol::PanicReason reason);
130
131 nearby_protocol::V0MatchableCredential GenerateRandomCredentialV0();
132
133 nearby_protocol::V1MatchableCredential GenerateRandomCredentialV1();
134
135 template <uint32_t N>
create_random_array()136 std::array<uint8_t, N> create_random_array() {
137 std::array<uint8_t, N> array{};
138 std::generate(array.begin(), array.end(), []() { return rand(); });
139 return array;
140 }
141
142 std::string generate_hex_string(size_t length);
143
144 #endif // NEARBY_PRESENCE_NP_CPP_FFI_TESTS_SHARED_TEST_UTILS_H_
145