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