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 <cutils/properties.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 constexpr char kTestAddrBad1[BluetoothAddress::kStringLength + 1] = 42 "bb:aa:dd:00:00:01"; 43 constexpr uint8_t kTestAddrBad1_bytes[BluetoothAddress::kBytes] = { 44 0xbb, 0xaa, 0xdd, 0x00, 0x00, 0x01}; 45 46 constexpr char kAddrPath[] = "/tmp/my_address_in_a_file.txt"; 47 48 class BluetoothAddressTest : public ::testing::Test { 49 public: 50 BluetoothAddressTest() {} 51 ~BluetoothAddressTest() {} 52 53 void FileWriteString(const char* path, const char* string); 54 }; 55 56 void BluetoothAddressTest::FileWriteString(const char* path, 57 const char* string) { 58 int fd = open(path, O_CREAT | O_RDWR); 59 EXPECT_TRUE(fd > 0) << "err = " << strerror(errno); 60 61 size_t length = strlen(string); 62 size_t bytes_written = write(fd, string, length); 63 64 EXPECT_EQ(length, bytes_written) << strerror(errno); 65 66 close(fd); 67 } 68 69 TEST_F(BluetoothAddressTest, string_to_bytes) { 70 uint8_t addr[BluetoothAddress::kBytes]; 71 72 // Malformed addresses 73 EXPECT_FALSE(BluetoothAddress::string_to_bytes("", addr)); 74 EXPECT_FALSE(BluetoothAddress::string_to_bytes("000000000000", addr)); 75 EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:0000", addr)); 76 EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:00:0", addr)); 77 EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:00:0;", addr)); 78 EXPECT_FALSE(BluetoothAddress::string_to_bytes("aB:cD:eF:Gh:iJ:Kl", addr)); 79 EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:000:00:00:0;", addr)); 80 EXPECT_FALSE(BluetoothAddress::string_to_bytes("12:34:56:78:90:12;", addr)); 81 EXPECT_FALSE(BluetoothAddress::string_to_bytes("12:34:56:78:90:123", addr)); 82 83 // Reasonable addresses 84 EXPECT_TRUE(BluetoothAddress::string_to_bytes("00:00:00:00:00:00", addr)); 85 EXPECT_TRUE(BluetoothAddress::string_to_bytes("a5:a5:a5:a5:a5:a5", addr)); 86 EXPECT_TRUE(BluetoothAddress::string_to_bytes("5A:5A:5A:5A:5A:5A", addr)); 87 EXPECT_TRUE(BluetoothAddress::string_to_bytes("AA:BB:CC:DD:EE:FF", addr)); 88 EXPECT_TRUE(BluetoothAddress::string_to_bytes("aa:bb:cc:dd:ee:ff", addr)); 89 90 // Compare the output to known bytes 91 uint8_t addrA[BluetoothAddress::kBytes]; 92 uint8_t addrB[BluetoothAddress::kBytes]; 93 94 // kTestAddr1 95 EXPECT_TRUE(BluetoothAddress::string_to_bytes(kTestAddr1, addrA)); 96 EXPECT_TRUE(memcmp(addrA, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); 97 98 // kZeros 99 EXPECT_TRUE(BluetoothAddress::string_to_bytes(kZeros, addrB)); 100 EXPECT_TRUE(memcmp(addrB, kZeros_bytes, BluetoothAddress::kBytes) == 0); 101 102 // kTestAddr1 != kZeros 103 EXPECT_FALSE(memcmp(addrA, addrB, BluetoothAddress::kBytes) == 0); 104 } 105 106 TEST_F(BluetoothAddressTest, bytes_to_string) { 107 char addrA[BluetoothAddress::kStringLength + 1] = ""; 108 char addrB[BluetoothAddress::kStringLength + 1] = ""; 109 110 // kTestAddr1 111 BluetoothAddress::bytes_to_string(kTestAddr1_bytes, addrA); 112 EXPECT_TRUE(memcmp(addrA, kTestAddr1, BluetoothAddress::kStringLength) == 0); 113 114 // kZeros 115 BluetoothAddress::bytes_to_string(kZeros_bytes, addrB); 116 EXPECT_TRUE(memcmp(addrB, kZeros, BluetoothAddress::kStringLength) == 0); 117 118 // kTestAddr1 != kZeros 119 EXPECT_FALSE(memcmp(addrA, addrB, BluetoothAddress::kStringLength) == 0); 120 } 121 122 TEST_F(BluetoothAddressTest, property_set) { 123 // Set the properties to empty strings. 124 property_set(PERSIST_BDADDR_PROPERTY, ""); 125 property_set(PROPERTY_BT_BDADDR_PATH, ""); 126 property_set(FACTORY_BDADDR_PROPERTY, ""); 127 128 // Get returns 0. 129 char prop[PROP_VALUE_MAX] = ""; 130 EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 0); 131 EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) == 0); 132 EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) == 0); 133 134 // Set the properties to known strings. 135 property_set(PERSIST_BDADDR_PROPERTY, "1"); 136 property_set(PROPERTY_BT_BDADDR_PATH, "22"); 137 property_set(FACTORY_BDADDR_PROPERTY, "333"); 138 139 // Get returns the correct length. 140 EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 1); 141 EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) == 2); 142 EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) == 3); 143 144 // Set the properties to empty strings again. 145 property_set(PERSIST_BDADDR_PROPERTY, ""); 146 property_set(PROPERTY_BT_BDADDR_PATH, ""); 147 property_set(FACTORY_BDADDR_PROPERTY, ""); 148 149 // Get returns 0. 150 EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 0); 151 EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) == 0); 152 EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) == 0); 153 } 154 155 TEST_F(BluetoothAddressTest, property_get) { 156 // Set the properties to known strings. 157 property_set(PERSIST_BDADDR_PROPERTY, PERSIST_BDADDR_PROPERTY); 158 property_set(PROPERTY_BT_BDADDR_PATH, PROPERTY_BT_BDADDR_PATH); 159 property_set(FACTORY_BDADDR_PROPERTY, FACTORY_BDADDR_PROPERTY); 160 161 // Get returns the same strings. 162 char prop[PROP_VALUE_MAX] = ""; 163 EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); 164 EXPECT_TRUE(strcmp(PERSIST_BDADDR_PROPERTY, prop) == 0); 165 166 EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); 167 EXPECT_TRUE(strcmp(PROPERTY_BT_BDADDR_PATH, prop) == 0); 168 169 EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); 170 EXPECT_TRUE(strcmp(FACTORY_BDADDR_PROPERTY, prop) == 0); 171 172 // Set a property to a different known string. 173 char prop2[PROP_VALUE_MAX] = "Erased"; 174 property_set(PERSIST_BDADDR_PROPERTY, prop2); 175 176 // Get returns the correct strings. 177 EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); 178 EXPECT_TRUE(strcmp(prop2, prop) == 0); 179 180 EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); 181 EXPECT_TRUE(strcmp(PROPERTY_BT_BDADDR_PATH, prop) == 0); 182 183 EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); 184 EXPECT_TRUE(strcmp(FACTORY_BDADDR_PROPERTY, prop) == 0); 185 186 // Set another property to prop2. 187 property_set(PROPERTY_BT_BDADDR_PATH, prop2); 188 189 EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); 190 EXPECT_TRUE(strcmp(prop2, prop) == 0); 191 192 EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); 193 EXPECT_TRUE(strcmp(prop2, prop) == 0); 194 195 EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); 196 EXPECT_TRUE(strcmp(FACTORY_BDADDR_PROPERTY, prop) == 0); 197 198 // Set the third property to prop2. 199 property_set(FACTORY_BDADDR_PROPERTY, prop2); 200 201 EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); 202 EXPECT_TRUE(strcmp(prop2, prop) == 0); 203 204 EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); 205 EXPECT_TRUE(strcmp(prop2, prop) == 0); 206 207 EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); 208 EXPECT_TRUE(strcmp(prop2, prop) == 0); 209 } 210 211 TEST_F(BluetoothAddressTest, get_local_address) { 212 EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, "") == 0); 213 EXPECT_TRUE(property_set(FACTORY_BDADDR_PROPERTY, "") == 0); 214 uint8_t address[BluetoothAddress::kBytes]; 215 216 // File contains a non-zero Address. 217 FileWriteString(kAddrPath, kTestAddr1); 218 EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, kAddrPath) == 0); 219 EXPECT_TRUE(BluetoothAddress::get_local_address(address)); 220 EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); 221 222 // File contains a zero address. A random address will be generated. 223 FileWriteString(kAddrPath, kZeros); 224 EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, kAddrPath) == 0); 225 EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0); 226 EXPECT_TRUE(BluetoothAddress::get_local_address(address)); 227 EXPECT_TRUE(memcmp(address, kZeros_bytes, BluetoothAddress::kBytes) != 0); 228 char prop[PROP_VALUE_MAX] = "Before reading"; 229 EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 230 BluetoothAddress::kStringLength); 231 char address_str[BluetoothAddress::kStringLength + 1]; 232 BluetoothAddress::bytes_to_string(address, address_str); 233 EXPECT_TRUE(memcmp(address_str, prop, BluetoothAddress::kStringLength) == 0); 234 235 // Factory property contains an address. 236 EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0); 237 EXPECT_TRUE(property_set(FACTORY_BDADDR_PROPERTY, kTestAddr1) == 0); 238 EXPECT_TRUE(BluetoothAddress::get_local_address(address)); 239 EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); 240 241 // Persistent property contains an address. 242 memcpy(address, kTestAddrBad1_bytes, BluetoothAddress::kBytes); 243 EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddr1) == 0); 244 EXPECT_TRUE(property_set(FACTORY_BDADDR_PROPERTY, "") == 0); 245 EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, "") == 0); 246 EXPECT_TRUE(BluetoothAddress::get_local_address(address)); 247 EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); 248 } 249 250 } // namespace implementation 251 } // namespace V1_0 252 } // namespace bluetooth 253 } // namespace hardware 254 } // namespace android 255