1 /******************************************************************************
2  *
3  *  Copyright 2017 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 <gtest/gtest.h>
20 
21 #include "raw_address.h"
22 
23 static const char* test_addr = "12:34:56:78:9a:bc";
24 static const char* test_addr2 = "cb:a9:87:65:43:21";
25 
TEST(RawAddressUnittest,test_constructor_array)26 TEST(RawAddressUnittest, test_constructor_array) {
27   RawAddress bdaddr({0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc});
28 
29   ASSERT_EQ(0x12, bdaddr.address[0]);
30   ASSERT_EQ(0x34, bdaddr.address[1]);
31   ASSERT_EQ(0x56, bdaddr.address[2]);
32   ASSERT_EQ(0x78, bdaddr.address[3]);
33   ASSERT_EQ(0x9A, bdaddr.address[4]);
34   ASSERT_EQ(0xBC, bdaddr.address[5]);
35 
36   std::string ret = bdaddr.ToString();
37 
38   ASSERT_STREQ(test_addr, ret.c_str());
39 }
40 
TEST(RawAddressUnittest,test_is_empty)41 TEST(RawAddressUnittest, test_is_empty) {
42   RawAddress empty;
43   RawAddress::FromString("00:00:00:00:00:00", empty);
44   ASSERT_TRUE(empty.IsEmpty());
45 
46   RawAddress not_empty;
47   RawAddress::FromString("00:00:00:00:00:01", not_empty);
48   ASSERT_FALSE(not_empty.IsEmpty());
49 }
50 
TEST(RawAddressUnittest,test_to_from_str)51 TEST(RawAddressUnittest, test_to_from_str) {
52   RawAddress bdaddr;
53   RawAddress::FromString(test_addr, bdaddr);
54 
55   ASSERT_EQ(0x12, bdaddr.address[0]);
56   ASSERT_EQ(0x34, bdaddr.address[1]);
57   ASSERT_EQ(0x56, bdaddr.address[2]);
58   ASSERT_EQ(0x78, bdaddr.address[3]);
59   ASSERT_EQ(0x9A, bdaddr.address[4]);
60   ASSERT_EQ(0xBC, bdaddr.address[5]);
61 
62   std::string ret = bdaddr.ToString();
63 
64   ASSERT_STREQ(test_addr, ret.c_str());
65 }
66 
TEST(RawAddressUnittest,test_from_octets)67 TEST(RawAddressUnittest, test_from_octets) {
68   static const uint8_t test_addr_array[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc};
69 
70   RawAddress bdaddr;
71   size_t expected_result = RawAddress::kLength;
72   ASSERT_EQ(expected_result, bdaddr.FromOctets(test_addr_array));
73 
74   ASSERT_EQ(0x12, bdaddr.address[0]);
75   ASSERT_EQ(0x34, bdaddr.address[1]);
76   ASSERT_EQ(0x56, bdaddr.address[2]);
77   ASSERT_EQ(0x78, bdaddr.address[3]);
78   ASSERT_EQ(0x9A, bdaddr.address[4]);
79   ASSERT_EQ(0xBC, bdaddr.address[5]);
80 
81   std::string ret = bdaddr.ToString();
82 
83   ASSERT_STREQ(test_addr, ret.c_str());
84 }
85 
TEST(RawAddressTest,test_equals)86 TEST(RawAddressTest, test_equals) {
87   RawAddress bdaddr1;
88   RawAddress bdaddr2;
89   RawAddress bdaddr3;
90   RawAddress::FromString(test_addr, bdaddr1);
91   RawAddress::FromString(test_addr, bdaddr2);
92   EXPECT_TRUE(bdaddr1 == bdaddr2);
93   EXPECT_FALSE(bdaddr1 != bdaddr2);
94   EXPECT_TRUE(bdaddr1 == bdaddr1);
95   EXPECT_FALSE(bdaddr1 != bdaddr1);
96 
97   RawAddress::FromString(test_addr2, bdaddr3);
98   EXPECT_FALSE(bdaddr2 == bdaddr3);
99   EXPECT_TRUE(bdaddr2 != bdaddr3);
100 }
101 
TEST(RawAddressTest,test_less_than)102 TEST(RawAddressTest, test_less_than) {
103   RawAddress bdaddr1;
104   RawAddress bdaddr2;
105   RawAddress bdaddr3;
106   RawAddress::FromString(test_addr, bdaddr1);
107   RawAddress::FromString(test_addr, bdaddr2);
108   EXPECT_FALSE(bdaddr1 < bdaddr2);
109   EXPECT_FALSE(bdaddr1 < bdaddr1);
110 
111   RawAddress::FromString(test_addr2, bdaddr3);
112   EXPECT_TRUE(bdaddr2 < bdaddr3);
113   EXPECT_FALSE(bdaddr3 < bdaddr2);
114 }
115 
TEST(RawAddressTest,test_more_than)116 TEST(RawAddressTest, test_more_than) {
117   RawAddress bdaddr1;
118   RawAddress bdaddr2;
119   RawAddress bdaddr3;
120   RawAddress::FromString(test_addr, bdaddr1);
121   RawAddress::FromString(test_addr, bdaddr2);
122   EXPECT_FALSE(bdaddr1 > bdaddr2);
123   EXPECT_FALSE(bdaddr1 > bdaddr1);
124 
125   RawAddress::FromString(test_addr2, bdaddr3);
126   EXPECT_FALSE(bdaddr2 > bdaddr3);
127   EXPECT_TRUE(bdaddr3 > bdaddr2);
128 }
129 
TEST(RawAddressTest,test_less_than_or_equal)130 TEST(RawAddressTest, test_less_than_or_equal) {
131   RawAddress bdaddr1;
132   RawAddress bdaddr2;
133   RawAddress bdaddr3;
134   RawAddress::FromString(test_addr, bdaddr1);
135   RawAddress::FromString(test_addr, bdaddr2);
136   EXPECT_TRUE(bdaddr1 <= bdaddr2);
137   EXPECT_TRUE(bdaddr1 <= bdaddr1);
138 
139   RawAddress::FromString(test_addr2, bdaddr3);
140   EXPECT_TRUE(bdaddr2 <= bdaddr3);
141   EXPECT_FALSE(bdaddr3 <= bdaddr2);
142 }
143 
TEST(RawAddressTest,test_more_than_or_equal)144 TEST(RawAddressTest, test_more_than_or_equal) {
145   RawAddress bdaddr1;
146   RawAddress bdaddr2;
147   RawAddress bdaddr3;
148   RawAddress::FromString(test_addr, bdaddr1);
149   RawAddress::FromString(test_addr, bdaddr2);
150   EXPECT_TRUE(bdaddr1 >= bdaddr2);
151   EXPECT_TRUE(bdaddr1 >= bdaddr1);
152 
153   RawAddress::FromString(test_addr2, bdaddr3);
154   EXPECT_FALSE(bdaddr2 >= bdaddr3);
155   EXPECT_TRUE(bdaddr3 >= bdaddr2);
156 }
157 
TEST(RawAddressTest,test_copy)158 TEST(RawAddressTest, test_copy) {
159   RawAddress bdaddr1;
160   RawAddress bdaddr2;
161   RawAddress::FromString(test_addr, bdaddr1);
162   bdaddr2 = bdaddr1;
163 
164   EXPECT_TRUE(bdaddr1 == bdaddr2);
165 }
166 
TEST(RawAddressTest,IsValidAddress)167 TEST(RawAddressTest, IsValidAddress) {
168   EXPECT_FALSE(RawAddress::IsValidAddress(""));
169   EXPECT_FALSE(RawAddress::IsValidAddress("000000000000"));
170   EXPECT_FALSE(RawAddress::IsValidAddress("00:00:00:00:0000"));
171   EXPECT_FALSE(RawAddress::IsValidAddress("00:00:00:00:00:0"));
172   EXPECT_FALSE(RawAddress::IsValidAddress("00:00:00:00:00:0;"));
173   EXPECT_TRUE(RawAddress::IsValidAddress("00:00:00:00:00:00"));
174   EXPECT_TRUE(RawAddress::IsValidAddress("AB:cd:00:00:00:00"));
175   EXPECT_FALSE(RawAddress::IsValidAddress("aB:cD:eF:Gh:iJ:Kl"));
176 }
177 
TEST(RawAddressTest,BdAddrFromString)178 TEST(RawAddressTest, BdAddrFromString) {
179   RawAddress addr;
180   memset(&addr, 0, sizeof(addr));
181 
182   EXPECT_TRUE(RawAddress::FromString("00:00:00:00:00:00", addr));
183   const RawAddress result0 = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
184   EXPECT_EQ(0, memcmp(&addr, &result0, sizeof(addr)));
185 
186   EXPECT_TRUE(RawAddress::FromString("ab:01:4C:d5:21:9f", addr));
187   const RawAddress result1 = {{0xab, 0x01, 0x4c, 0xd5, 0x21, 0x9f}};
188   EXPECT_EQ(0, memcmp(&addr, &result1, sizeof(addr)));
189 }
190