1 /******************************************************************************
2  *
3  *  Copyright 2019 The Android Open Source Project
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 "hci/address.h"
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 #include <cstdint>
25 #include <string>
26 #include <unordered_map>
27 
28 using bluetooth::hci::Address;
29 
30 static const char* test_addr = "bc:9a:78:56:34:12";
31 static const char* test_addr2 = "21:43:65:87:a9:cb";
32 
TEST(AddressUnittest,test_constructor_array)33 TEST(AddressUnittest, test_constructor_array) {
34   Address bdaddr({0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc});
35 
36   ASSERT_EQ(0x12, bdaddr.address[0]);
37   ASSERT_EQ(0x34, bdaddr.address[1]);
38   ASSERT_EQ(0x56, bdaddr.address[2]);
39   ASSERT_EQ(0x78, bdaddr.address[3]);
40   ASSERT_EQ(0x9A, bdaddr.address[4]);
41   ASSERT_EQ(0xBC, bdaddr.address[5]);
42 
43   std::string ret = bdaddr.ToString();
44 
45   ASSERT_STREQ(test_addr, ret.c_str());
46 }
47 
TEST(AddressUnittest,test_is_empty)48 TEST(AddressUnittest, test_is_empty) {
49   Address empty;
50   Address::FromString("00:00:00:00:00:00", empty);
51   ASSERT_TRUE(empty.IsEmpty());
52 
53   Address not_empty;
54   Address::FromString("00:00:00:00:00:01", not_empty);
55   ASSERT_FALSE(not_empty.IsEmpty());
56 }
57 
TEST(AddressUnittest,test_to_from_str)58 TEST(AddressUnittest, test_to_from_str) {
59   Address bdaddr;
60   Address::FromString(test_addr, bdaddr);
61 
62   ASSERT_EQ(0x12, bdaddr.address[0]);
63   ASSERT_EQ(0x34, bdaddr.address[1]);
64   ASSERT_EQ(0x56, bdaddr.address[2]);
65   ASSERT_EQ(0x78, bdaddr.address[3]);
66   ASSERT_EQ(0x9A, bdaddr.address[4]);
67   ASSERT_EQ(0xBC, bdaddr.address[5]);
68 
69   std::string ret = bdaddr.ToString();
70 
71   ASSERT_STREQ(test_addr, ret.c_str());
72 }
73 
TEST(AddressUnittest,test_from_octets)74 TEST(AddressUnittest, test_from_octets) {
75   static const uint8_t test_addr_array[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc};
76 
77   Address bdaddr;
78   size_t expected_result = Address::kLength;
79   ASSERT_EQ(expected_result, bdaddr.FromOctets(test_addr_array));
80 
81   ASSERT_EQ(0x12, bdaddr.address[0]);
82   ASSERT_EQ(0x34, bdaddr.address[1]);
83   ASSERT_EQ(0x56, bdaddr.address[2]);
84   ASSERT_EQ(0x78, bdaddr.address[3]);
85   ASSERT_EQ(0x9A, bdaddr.address[4]);
86   ASSERT_EQ(0xBC, bdaddr.address[5]);
87 
88   std::string ret = bdaddr.ToString();
89 
90   ASSERT_STREQ(test_addr, ret.c_str());
91 }
92 
TEST(AddressTest,test_equals)93 TEST(AddressTest, test_equals) {
94   Address bdaddr1;
95   Address bdaddr2;
96   Address bdaddr3;
97   Address::FromString(test_addr, bdaddr1);
98   Address::FromString(test_addr, bdaddr2);
99   ASSERT_TRUE(bdaddr1 == bdaddr2);
100   ASSERT_FALSE(bdaddr1 != bdaddr2);
101   ASSERT_TRUE(bdaddr1 == bdaddr1);
102   ASSERT_FALSE(bdaddr1 != bdaddr1);
103 
104   Address::FromString(test_addr2, bdaddr3);
105   ASSERT_FALSE(bdaddr2 == bdaddr3);
106   ASSERT_TRUE(bdaddr2 != bdaddr3);
107 }
108 
TEST(AddressTest,test_less_than)109 TEST(AddressTest, test_less_than) {
110   Address bdaddr1;
111   Address bdaddr2;
112   Address bdaddr3;
113   Address::FromString(test_addr, bdaddr1);
114   Address::FromString(test_addr, bdaddr2);
115   ASSERT_FALSE(bdaddr1 < bdaddr2);
116   ASSERT_FALSE(bdaddr1 < bdaddr1);
117 
118   Address::FromString(test_addr2, bdaddr3);
119   ASSERT_TRUE(bdaddr2 < bdaddr3);
120   ASSERT_FALSE(bdaddr3 < bdaddr2);
121 }
122 
TEST(AddressTest,test_more_than)123 TEST(AddressTest, test_more_than) {
124   Address bdaddr1;
125   Address bdaddr2;
126   Address bdaddr3;
127   Address::FromString(test_addr, bdaddr1);
128   Address::FromString(test_addr, bdaddr2);
129   ASSERT_FALSE(bdaddr1 > bdaddr2);
130   ASSERT_FALSE(bdaddr1 > bdaddr1);
131 
132   Address::FromString(test_addr2, bdaddr3);
133   ASSERT_FALSE(bdaddr2 > bdaddr3);
134   ASSERT_TRUE(bdaddr3 > bdaddr2);
135 }
136 
TEST(AddressTest,test_less_than_or_equal)137 TEST(AddressTest, test_less_than_or_equal) {
138   Address bdaddr1;
139   Address bdaddr2;
140   Address bdaddr3;
141   Address::FromString(test_addr, bdaddr1);
142   Address::FromString(test_addr, bdaddr2);
143   ASSERT_TRUE(bdaddr1 <= bdaddr2);
144   ASSERT_TRUE(bdaddr1 <= bdaddr1);
145 
146   Address::FromString(test_addr2, bdaddr3);
147   ASSERT_TRUE(bdaddr2 <= bdaddr3);
148   ASSERT_FALSE(bdaddr3 <= bdaddr2);
149 }
150 
TEST(AddressTest,test_more_than_or_equal)151 TEST(AddressTest, test_more_than_or_equal) {
152   Address bdaddr1;
153   Address bdaddr2;
154   Address bdaddr3;
155   Address::FromString(test_addr, bdaddr1);
156   Address::FromString(test_addr, bdaddr2);
157   ASSERT_TRUE(bdaddr1 >= bdaddr2);
158   ASSERT_TRUE(bdaddr1 >= bdaddr1);
159 
160   Address::FromString(test_addr2, bdaddr3);
161   ASSERT_FALSE(bdaddr2 >= bdaddr3);
162   ASSERT_TRUE(bdaddr3 >= bdaddr2);
163 }
164 
TEST(AddressTest,test_copy)165 TEST(AddressTest, test_copy) {
166   Address bdaddr1;
167   Address bdaddr2;
168   Address::FromString(test_addr, bdaddr1);
169   bdaddr2 = bdaddr1;
170 
171   ASSERT_TRUE(bdaddr1 == bdaddr2);
172 }
173 
TEST(AddressTest,IsValidAddress)174 TEST(AddressTest, IsValidAddress) {
175   ASSERT_FALSE(Address::IsValidAddress(""));
176   ASSERT_FALSE(Address::IsValidAddress("000000000000"));
177   ASSERT_FALSE(Address::IsValidAddress("00:00:00:00:0000"));
178   ASSERT_FALSE(Address::IsValidAddress("00:00:00:00:00:0"));
179   ASSERT_FALSE(Address::IsValidAddress("00:00:00:00:00:0;"));
180   ASSERT_TRUE(Address::IsValidAddress("00:00:00:00:00:00"));
181   ASSERT_TRUE(Address::IsValidAddress("AB:cd:00:00:00:00"));
182   ASSERT_FALSE(Address::IsValidAddress("aB:cD:eF:Gh:iJ:Kl"));
183 }
184 
TEST(AddressTest,BdAddrFromString)185 TEST(AddressTest, BdAddrFromString) {
186   Address addr = {};
187 
188   ASSERT_TRUE(Address::FromString("00:00:00:00:00:00", addr));
189   const Address result0 = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
190   ASSERT_EQ(0, memcmp(addr.data(), result0.data(), Address::kLength));
191 
192   ASSERT_TRUE(Address::FromString("ab:01:4C:d5:21:9f", addr));
193   const Address result1 = {{0x9f, 0x21, 0xd5, 0x4c, 0x01, 0xab}};
194   ASSERT_EQ("ab:01:4c:d5:21:9f", addr.ToString());
195   ASSERT_EQ("ab:01:4c:d5:21:9f", result1.ToString());
196   ASSERT_THAT(addr.address, testing::ElementsAre(0x9f, 0x21, 0xd5, 0x4c, 0x01, 0xab));
197   ASSERT_EQ(0, memcmp(addr.data(), result1.data(), Address::kLength));
198 }
199 
TEST(AddressTest,BdAddrFromStringToStringEquivalent)200 TEST(AddressTest, BdAddrFromStringToStringEquivalent) {
201   std::string address = "c1:c2:c3:d1:d2:d3";
202   Address addr;
203 
204   ASSERT_TRUE(Address::FromString(address, addr));
205   ASSERT_EQ(addr.ToString(), address);
206 }
207 
TEST(AddressTest,BdAddrSameValueSameOrder)208 TEST(AddressTest, BdAddrSameValueSameOrder) {
209   Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
210   Address addr2{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
211   // Test if two addresses with same byte value have the same hash
212   struct std::hash<bluetooth::hci::Address> hasher;
213   ASSERT_EQ(hasher(addr1), hasher(addr2));
214   // Test if two addresses with the same hash and the same value, they will
215   // still map to the same value
216   std::unordered_map<Address, int> data = {};
217   data[addr1] = 5;
218   data[addr2] = 8;
219   ASSERT_EQ(data[addr1], data[addr2]);
220 }
221 
TEST(AddressTest,BdAddrHashDifferentForDifferentAddressesZeroAddr)222 TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesZeroAddr) {
223   Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
224   struct std::hash<Address> hasher;
225   ASSERT_NE(hasher(addr1), hasher(Address::kEmpty));
226 }
227 
TEST(AddressTest,BdAddrHashDifferentForDifferentAddressesFullAddr)228 TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesFullAddr) {
229   Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
230   struct std::hash<Address> hasher;
231   ASSERT_NE(hasher(addr1), hasher(Address::kAny));
232 }
233 
TEST(AddressTest,BdAddrHashDifferentForDifferentAddressesZeroAndFullAddr)234 TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesZeroAndFullAddr) {
235   struct std::hash<Address> hasher;
236   ASSERT_NE(hasher(Address::kEmpty), hasher(Address::kAny));
237 }
238 
TEST(AddressTest,ToStringForLoggingTestOutputUnderDebuggablePropAndInitFlag)239 TEST(AddressTest, ToStringForLoggingTestOutputUnderDebuggablePropAndInitFlag) {
240   Address addr{{0xab, 0x55, 0x44, 0x33, 0x22, 0x11}};
241   const std::string redacted_loggable_str = "xx:xx:xx:xx:55:ab";
242   const std::string loggable_str = "11:22:33:44:55:ab";
243 
244   std::string ret1 = addr.ToStringForLogging();
245   ASSERT_STREQ(ret1.c_str(), loggable_str.c_str());
246   std::string ret2 = addr.ToRedactedStringForLogging();
247   ASSERT_STREQ(ret2.c_str(), redacted_loggable_str.c_str());
248 }
249 
TEST(AddressTest,Inequalities)250 TEST(AddressTest, Inequalities) {
251   Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
252   Address addr2{{0x02, 0x03, 0x04, 0x05, 0x06, 0x07}};
253   ASSERT_TRUE(addr1 < addr2);
254   ASSERT_TRUE(addr2 > addr1);
255 
256   ASSERT_TRUE(addr1 <= addr1);
257   ASSERT_TRUE(addr2 <= addr2);
258 }
259