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