1 //
2 // Copyright 2016 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <gtest/gtest.h>
20
21 #include <string>
22 #include <vector>
23 using std::vector;
24
25 #include "bluetooth_address.h"
26
27 namespace android {
28 namespace hardware {
29 namespace bluetooth {
30 namespace V1_0 {
31 namespace implementation {
32
33 constexpr char kTestAddr1[BluetoothAddress::kStringLength + 1] =
34 "12:34:56:78:9a:bc";
35 constexpr uint8_t kTestAddr1_bytes[BluetoothAddress::kBytes] = {
36 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc};
37 constexpr char kZeros[BluetoothAddress::kStringLength + 1] =
38 "00:00:00:00:00:00";
39 constexpr uint8_t kZeros_bytes[BluetoothAddress::kBytes] = {0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00};
41
42 class BluetoothAddressTest : public ::testing::Test {
43 public:
BluetoothAddressTest()44 BluetoothAddressTest() {}
~BluetoothAddressTest()45 ~BluetoothAddressTest() {}
46
47 void FileWriteString(const char* path, const char* string);
48 };
49
FileWriteString(const char * path,const char * string)50 void BluetoothAddressTest::FileWriteString(const char* path,
51 const char* string) {
52 int fd = open(path, O_CREAT | O_RDWR, 0600);
53 EXPECT_TRUE(fd > 0) << "err = " << strerror(errno);
54
55 size_t length = strlen(string);
56 size_t bytes_written = write(fd, string, length);
57
58 EXPECT_EQ(length, bytes_written) << strerror(errno);
59
60 close(fd);
61 }
62
TEST_F(BluetoothAddressTest,string_to_bytes)63 TEST_F(BluetoothAddressTest, string_to_bytes) {
64 uint8_t addr[BluetoothAddress::kBytes];
65
66 // Malformed addresses
67 EXPECT_FALSE(BluetoothAddress::string_to_bytes("", addr));
68 EXPECT_FALSE(BluetoothAddress::string_to_bytes("000000000000", addr));
69 EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:0000", addr));
70 EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:00:0", addr));
71 EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:00:0;", addr));
72 EXPECT_FALSE(BluetoothAddress::string_to_bytes("aB:cD:eF:Gh:iJ:Kl", addr));
73 EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:000:00:00:0;", addr));
74 EXPECT_FALSE(BluetoothAddress::string_to_bytes("12:34:56:78:90:12;", addr));
75 EXPECT_FALSE(BluetoothAddress::string_to_bytes("12:34:56:78:90:123", addr));
76
77 // Reasonable addresses
78 EXPECT_TRUE(BluetoothAddress::string_to_bytes("00:00:00:00:00:00", addr));
79 EXPECT_TRUE(BluetoothAddress::string_to_bytes("a5:a5:a5:a5:a5:a5", addr));
80 EXPECT_TRUE(BluetoothAddress::string_to_bytes("5A:5A:5A:5A:5A:5A", addr));
81 EXPECT_TRUE(BluetoothAddress::string_to_bytes("AA:BB:CC:DD:EE:FF", addr));
82 EXPECT_TRUE(BluetoothAddress::string_to_bytes("aa:bb:cc:dd:ee:ff", addr));
83
84 // Compare the output to known bytes
85 uint8_t addrA[BluetoothAddress::kBytes];
86 uint8_t addrB[BluetoothAddress::kBytes];
87
88 // kTestAddr1
89 EXPECT_TRUE(BluetoothAddress::string_to_bytes(kTestAddr1, addrA));
90 EXPECT_TRUE(memcmp(addrA, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0);
91
92 // kZeros
93 EXPECT_TRUE(BluetoothAddress::string_to_bytes(kZeros, addrB));
94 EXPECT_TRUE(memcmp(addrB, kZeros_bytes, BluetoothAddress::kBytes) == 0);
95
96 // kTestAddr1 != kZeros
97 EXPECT_FALSE(memcmp(addrA, addrB, BluetoothAddress::kBytes) == 0);
98 }
99
TEST_F(BluetoothAddressTest,bytes_to_string)100 TEST_F(BluetoothAddressTest, bytes_to_string) {
101 char addrA[BluetoothAddress::kStringLength + 1] = "";
102 char addrB[BluetoothAddress::kStringLength + 1] = "";
103
104 // kTestAddr1
105 BluetoothAddress::bytes_to_string(kTestAddr1_bytes, addrA);
106 EXPECT_TRUE(memcmp(addrA, kTestAddr1, BluetoothAddress::kStringLength) == 0);
107
108 // kZeros
109 BluetoothAddress::bytes_to_string(kZeros_bytes, addrB);
110 EXPECT_TRUE(memcmp(addrB, kZeros, BluetoothAddress::kStringLength) == 0);
111
112 // kTestAddr1 != kZeros
113 EXPECT_FALSE(memcmp(addrA, addrB, BluetoothAddress::kStringLength) == 0);
114 }
115
116 } // namespace implementation
117 } // namespace V1_0
118 } // namespace bluetooth
119 } // namespace hardware
120 } // namespace android
121