1 /******************************************************************************
2 *
3 * Copyright 2018 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "address_obfuscator.h"
20
21 #include <gtest/gtest.h>
22
23 #include "types/raw_address.h"
24
25 using bluetooth::common::AddressObfuscator;
26
27 constexpr AddressObfuscator::Octet32 kEmptyKey = {0};
28
29 constexpr AddressObfuscator::Octet32 kTestKey1 = {
30 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
31 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
32 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
33 };
34 static RawAddress kTestData1 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
35 constexpr AddressObfuscator::Octet32 kTestResultRaw1 = {
36 0x9b, 0x52, 0xb9, 0xb9, 0xb9, 0x34, 0x80, 0x1d, 0x98, 0x0b, 0x10,
37 0xbe, 0x45, 0xa2, 0x6d, 0xaa, 0x99, 0xc3, 0x04, 0x10, 0x08, 0x03,
38 0xb7, 0xb4, 0xa9, 0xde, 0xcf, 0x89, 0xe1, 0x5d, 0xd4, 0xaa};
39 static std::string kTestResult1(
40 reinterpret_cast<const char*>(kTestResultRaw1.data()),
41 kTestResultRaw1.size());
42
43 constexpr AddressObfuscator::Octet32 kTestKey2 = {
44 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
45 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
46 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20};
47
48 static RawAddress kTestData2_1 = {{0x9e, 0xf5, 0x3d, 0x6c, 0x2e, 0x33}};
49 constexpr AddressObfuscator::Octet32 kTestResultRaw2_1 = {
50 0xb4, 0xe2, 0xfc, 0xb9, 0x59, 0x0d, 0x1f, 0xcf, 0x68, 0x80, 0xb2,
51 0x3d, 0x08, 0x55, 0x4e, 0x64, 0xf5, 0x3b, 0x33, 0x0d, 0xb6, 0x31,
52 0x9a, 0xbc, 0x4e, 0xce, 0x61, 0xbd, 0x46, 0x66, 0x45, 0x94};
53 static std::string kTestResult2_1(
54 reinterpret_cast<const char*>(kTestResultRaw2_1.data()),
55 kTestResultRaw2_1.size());
56
57 static RawAddress kTestData2_2 = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
58 constexpr AddressObfuscator::Octet32 kTestResultRaw2_2 = {
59 0xd4, 0xd8, 0x23, 0xc0, 0x24, 0xba, 0xde, 0xe3, 0x1c, 0xad, 0x84,
60 0x8b, 0x3d, 0xc6, 0xda, 0x93, 0x88, 0xb2, 0x5c, 0x60, 0x13, 0xe5,
61 0xe2, 0x3e, 0x75, 0x5f, 0xd7, 0x15, 0x56, 0xf7, 0xaf, 0x27};
62 static std::string kTestResult2_2(
63 reinterpret_cast<const char*>(kTestResultRaw2_2.data()),
64 kTestResultRaw2_2.size());
65
66 static RawAddress kTestData2_3 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
67 constexpr AddressObfuscator::Octet32 kTestResultRaw2_3 = {
68 0x6f, 0x7c, 0x3d, 0x23, 0xcc, 0x7a, 0xf2, 0x68, 0xee, 0xe8, 0x6c,
69 0x0f, 0xb5, 0xe0, 0x0c, 0x88, 0xf6, 0x38, 0x71, 0x44, 0x88, 0x09,
70 0x45, 0x0a, 0xa2, 0xd7, 0xf6, 0x70, 0xba, 0x8c, 0xe9, 0x79};
71 static std::string kTestResult2_3(
72 reinterpret_cast<const char*>(kTestResultRaw2_3.data()),
73 kTestResultRaw2_3.size());
74
TEST(AddressObfuscatorTest,test_invalid_key)75 TEST(AddressObfuscatorTest, test_invalid_key) {
76 EXPECT_FALSE(AddressObfuscator::IsSaltValid(kEmptyKey));
77 }
78
TEST(AddressObfuscatorTest,test_valid_key)79 TEST(AddressObfuscatorTest, test_valid_key) {
80 EXPECT_TRUE(AddressObfuscator::IsSaltValid(kTestKey1));
81 }
82
TEST(AddressObfuscatorTest,test_initialize_negative)83 TEST(AddressObfuscatorTest, test_initialize_negative) {
84 AddressObfuscator::GetInstance()->Initialize(kEmptyKey);
85 EXPECT_FALSE(AddressObfuscator::GetInstance()->IsInitialized());
86 }
87
TEST(AddressObfuscatorTest,test_initialize_positive)88 TEST(AddressObfuscatorTest, test_initialize_positive) {
89 AddressObfuscator::GetInstance()->Initialize(kTestKey1);
90 EXPECT_TRUE(AddressObfuscator::GetInstance()->IsInitialized());
91 }
92
TEST(AddressObfuscatorTest,test_obfuscate_address_key1)93 TEST(AddressObfuscatorTest, test_obfuscate_address_key1) {
94 AddressObfuscator::GetInstance()->Initialize(kTestKey1);
95 std::string result = AddressObfuscator::GetInstance()->Obfuscate(kTestData1);
96 EXPECT_EQ(result.size(), AddressObfuscator::kOctet32Length);
97 EXPECT_EQ(result, kTestResult1);
98 }
99
TEST(AddressObfuscatorTest,test_obfuscate_address_key2)100 TEST(AddressObfuscatorTest, test_obfuscate_address_key2) {
101 AddressObfuscator::GetInstance()->Initialize(kTestKey2);
102 std::string result =
103 AddressObfuscator::GetInstance()->Obfuscate(kTestData2_1);
104 EXPECT_EQ(result.size(), AddressObfuscator::kOctet32Length);
105 EXPECT_EQ(result, kTestResult2_1);
106 }
107
TEST(AddressObfuscatorTest,test_obfuscate_address_key2_empty_adddress)108 TEST(AddressObfuscatorTest, test_obfuscate_address_key2_empty_adddress) {
109 AddressObfuscator::GetInstance()->Initialize(kTestKey2);
110 std::string result =
111 AddressObfuscator::GetInstance()->Obfuscate(kTestData2_2);
112 EXPECT_EQ(result.size(), AddressObfuscator::kOctet32Length);
113 EXPECT_EQ(result, kTestResult2_2);
114 }
115
TEST(AddressObfuscatorTest,test_obfuscate_address_key2_max_address)116 TEST(AddressObfuscatorTest, test_obfuscate_address_key2_max_address) {
117 AddressObfuscator::GetInstance()->Initialize(kTestKey2);
118 std::string result =
119 AddressObfuscator::GetInstance()->Obfuscate(kTestData2_3);
120 EXPECT_EQ(result.size(), AddressObfuscator::kOctet32Length);
121 EXPECT_EQ(result, kTestResult2_3);
122 }
123