• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using bluetooth::common::AddressObfuscator;
24 
25 constexpr AddressObfuscator::Octet32 kEmptyKey = {0};
26 
27 constexpr AddressObfuscator::Octet32 kTestKey1 = {
28     0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
29     0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
30     0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
31 };
32 static RawAddress kTestData1 = {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}};
33 constexpr AddressObfuscator::Octet32 kTestResultRaw1 = {
34     0x9b, 0x52, 0xb9, 0xb9, 0xb9, 0x34, 0x80, 0x1d, 0x98, 0x0b, 0x10,
35     0xbe, 0x45, 0xa2, 0x6d, 0xaa, 0x99, 0xc3, 0x04, 0x10, 0x08, 0x03,
36     0xb7, 0xb4, 0xa9, 0xde, 0xcf, 0x89, 0xe1, 0x5d, 0xd4, 0xaa};
37 static std::string kTestResult1(
38     reinterpret_cast<const char*>(kTestResultRaw1.data()),
39     kTestResultRaw1.size());
40 
41 constexpr AddressObfuscator::Octet32 kTestKey2 = {
42     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
43     0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
44     0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20};
45 
46 static RawAddress kTestData2_1 = {{0x9e, 0xf5, 0x3d, 0x6c, 0x2e, 0x33}};
47 constexpr AddressObfuscator::Octet32 kTestResultRaw2_1 = {
48     0xb4, 0xe2, 0xfc, 0xb9, 0x59, 0x0d, 0x1f, 0xcf, 0x68, 0x80, 0xb2,
49     0x3d, 0x08, 0x55, 0x4e, 0x64, 0xf5, 0x3b, 0x33, 0x0d, 0xb6, 0x31,
50     0x9a, 0xbc, 0x4e, 0xce, 0x61, 0xbd, 0x46, 0x66, 0x45, 0x94};
51 static std::string kTestResult2_1(
52     reinterpret_cast<const char*>(kTestResultRaw2_1.data()),
53     kTestResultRaw2_1.size());
54 
55 static RawAddress kTestData2_2 = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
56 constexpr AddressObfuscator::Octet32 kTestResultRaw2_2 = {
57     0xd4, 0xd8, 0x23, 0xc0, 0x24, 0xba, 0xde, 0xe3, 0x1c, 0xad, 0x84,
58     0x8b, 0x3d, 0xc6, 0xda, 0x93, 0x88, 0xb2, 0x5c, 0x60, 0x13, 0xe5,
59     0xe2, 0x3e, 0x75, 0x5f, 0xd7, 0x15, 0x56, 0xf7, 0xaf, 0x27};
60 static std::string kTestResult2_2(
61     reinterpret_cast<const char*>(kTestResultRaw2_2.data()),
62     kTestResultRaw2_2.size());
63 
64 static RawAddress kTestData2_3 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
65 constexpr AddressObfuscator::Octet32 kTestResultRaw2_3 = {
66     0x6f, 0x7c, 0x3d, 0x23, 0xcc, 0x7a, 0xf2, 0x68, 0xee, 0xe8, 0x6c,
67     0x0f, 0xb5, 0xe0, 0x0c, 0x88, 0xf6, 0x38, 0x71, 0x44, 0x88, 0x09,
68     0x45, 0x0a, 0xa2, 0xd7, 0xf6, 0x70, 0xba, 0x8c, 0xe9, 0x79};
69 static std::string kTestResult2_3(
70     reinterpret_cast<const char*>(kTestResultRaw2_3.data()),
71     kTestResultRaw2_3.size());
72 
TEST(AddressObfuscatorTest,test_invalid_key)73 TEST(AddressObfuscatorTest, test_invalid_key) {
74   EXPECT_FALSE(AddressObfuscator::IsSaltValid(kEmptyKey));
75 }
76 
TEST(AddressObfuscatorTest,test_valid_key)77 TEST(AddressObfuscatorTest, test_valid_key) {
78   EXPECT_TRUE(AddressObfuscator::IsSaltValid(kTestKey1));
79 }
80 
TEST(AddressObfuscatorTest,test_initialize_negative)81 TEST(AddressObfuscatorTest, test_initialize_negative) {
82   AddressObfuscator::GetInstance()->Initialize(kEmptyKey);
83   EXPECT_FALSE(AddressObfuscator::GetInstance()->IsInitialized());
84 }
85 
TEST(AddressObfuscatorTest,test_initialize_positive)86 TEST(AddressObfuscatorTest, test_initialize_positive) {
87   AddressObfuscator::GetInstance()->Initialize(kTestKey1);
88   EXPECT_TRUE(AddressObfuscator::GetInstance()->IsInitialized());
89 }
90 
TEST(AddressObfuscatorTest,test_obfuscate_address_key1)91 TEST(AddressObfuscatorTest, test_obfuscate_address_key1) {
92   AddressObfuscator::GetInstance()->Initialize(kTestKey1);
93   std::string result = AddressObfuscator::GetInstance()->Obfuscate(kTestData1);
94   EXPECT_EQ(result.size(), AddressObfuscator::kOctet32Length);
95   EXPECT_EQ(result, kTestResult1);
96 }
97 
TEST(AddressObfuscatorTest,test_obfuscate_address_key2)98 TEST(AddressObfuscatorTest, test_obfuscate_address_key2) {
99   AddressObfuscator::GetInstance()->Initialize(kTestKey2);
100   std::string result =
101       AddressObfuscator::GetInstance()->Obfuscate(kTestData2_1);
102   EXPECT_EQ(result.size(), AddressObfuscator::kOctet32Length);
103   EXPECT_EQ(result, kTestResult2_1);
104 }
105 
TEST(AddressObfuscatorTest,test_obfuscate_address_key2_empty_adddress)106 TEST(AddressObfuscatorTest, test_obfuscate_address_key2_empty_adddress) {
107   AddressObfuscator::GetInstance()->Initialize(kTestKey2);
108   std::string result =
109       AddressObfuscator::GetInstance()->Obfuscate(kTestData2_2);
110   EXPECT_EQ(result.size(), AddressObfuscator::kOctet32Length);
111   EXPECT_EQ(result, kTestResult2_2);
112 }
113 
TEST(AddressObfuscatorTest,test_obfuscate_address_key2_max_address)114 TEST(AddressObfuscatorTest, test_obfuscate_address_key2_max_address) {
115   AddressObfuscator::GetInstance()->Initialize(kTestKey2);
116   std::string result =
117       AddressObfuscator::GetInstance()->Obfuscate(kTestData2_3);
118   EXPECT_EQ(result.size(), AddressObfuscator::kOctet32Length);
119   EXPECT_EQ(result, kTestResult2_3);
120 }