OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" |
| 6 |
| 7 #include "base/strings/string_util.h" |
| 8 #include "content/common/bluetooth/bluetooth_scan_filter.h" |
| 9 #include "device/bluetooth/bluetooth_uuid.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace content { |
| 13 namespace { |
| 14 const std::string& test_origin1 = "test_origin1"; |
| 15 const std::string& test_origin2 = "test_origin2"; |
| 16 |
| 17 const std::string& device_address1 = "00:00:00"; |
| 18 const std::string& device_address2 = "11:11:11"; |
| 19 |
| 20 const std::vector<content::BluetoothScanFilter> filters = |
| 21 std::vector<BluetoothScanFilter>(); |
| 22 const std::vector<device::BluetoothUUID> optional_services = |
| 23 std::vector<device::BluetoothUUID>(); |
| 24 } // namespace |
| 25 |
| 26 class BluetoothAllowedDevicesMapTest : public testing::Test {}; |
| 27 |
| 28 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMap) { |
| 29 BluetoothAllowedDevicesMap allowed_devices_map; |
| 30 |
| 31 const std::string& device_id = allowed_devices_map.AddDevice( |
| 32 test_origin1, device_address1, filters, optional_services); |
| 33 |
| 34 // Test that we can retrieve the device address/id. |
| 35 EXPECT_EQ(device_id, |
| 36 allowed_devices_map.GetDeviceId(test_origin1, device_address1)); |
| 37 EXPECT_EQ(device_address1, |
| 38 allowed_devices_map.GetDeviceAddress(test_origin1, device_id)); |
| 39 } |
| 40 |
| 41 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMapTwice) { |
| 42 BluetoothAllowedDevicesMap allowed_devices_map; |
| 43 const std::string& device_id1 = allowed_devices_map.AddDevice( |
| 44 test_origin1, device_address1, filters, optional_services); |
| 45 const std::string& device_id2 = allowed_devices_map.AddDevice( |
| 46 test_origin1, device_address1, filters, optional_services); |
| 47 |
| 48 EXPECT_EQ(device_id1, device_id2); |
| 49 |
| 50 // Test that we can retrieve the device address/id. |
| 51 EXPECT_EQ(device_id1, |
| 52 allowed_devices_map.GetDeviceId(test_origin1, device_address1)); |
| 53 EXPECT_EQ(device_address1, |
| 54 allowed_devices_map.GetDeviceAddress(test_origin1, device_id1)); |
| 55 } |
| 56 |
| 57 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromSameOriginToMap) { |
| 58 BluetoothAllowedDevicesMap allowed_devices_map; |
| 59 const std::string& device_id1 = allowed_devices_map.AddDevice( |
| 60 test_origin1, device_address1, filters, optional_services); |
| 61 const std::string& device_id2 = allowed_devices_map.AddDevice( |
| 62 test_origin1, device_address2, filters, optional_services); |
| 63 |
| 64 EXPECT_NE(device_id1, device_id2); |
| 65 |
| 66 // Test that we can retrieve the device address/id. |
| 67 EXPECT_EQ(device_id1, |
| 68 allowed_devices_map.GetDeviceId(test_origin1, device_address1)); |
| 69 EXPECT_EQ(device_id2, |
| 70 allowed_devices_map.GetDeviceId(test_origin1, device_address2)); |
| 71 |
| 72 EXPECT_EQ(device_address1, |
| 73 allowed_devices_map.GetDeviceAddress(test_origin1, device_id1)); |
| 74 EXPECT_EQ(device_address2, |
| 75 allowed_devices_map.GetDeviceAddress(test_origin1, device_id2)); |
| 76 } |
| 77 |
| 78 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromTwoOriginsToMap) { |
| 79 BluetoothAllowedDevicesMap allowed_devices_map; |
| 80 const std::string& device_id1 = allowed_devices_map.AddDevice( |
| 81 test_origin1, device_address1, filters, optional_services); |
| 82 const std::string& device_id2 = allowed_devices_map.AddDevice( |
| 83 test_origin2, device_address2, filters, optional_services); |
| 84 |
| 85 EXPECT_NE(device_id1, device_id2); |
| 86 |
| 87 // Test that the wrong origin doesn't have access to the device. |
| 88 |
| 89 EXPECT_EQ(base::EmptyString(), |
| 90 allowed_devices_map.GetDeviceId(test_origin1, device_address2)); |
| 91 EXPECT_EQ(base::EmptyString(), |
| 92 allowed_devices_map.GetDeviceId(test_origin2, device_address1)); |
| 93 |
| 94 EXPECT_EQ(base::EmptyString(), |
| 95 allowed_devices_map.GetDeviceAddress(test_origin1, device_id2)); |
| 96 EXPECT_EQ(base::EmptyString(), |
| 97 allowed_devices_map.GetDeviceAddress(test_origin2, device_id1)); |
| 98 |
| 99 // Test that we can retrieve the device address/id. |
| 100 EXPECT_EQ(device_id1, |
| 101 allowed_devices_map.GetDeviceId(test_origin1, device_address1)); |
| 102 EXPECT_EQ(device_id2, |
| 103 allowed_devices_map.GetDeviceId(test_origin2, device_address2)); |
| 104 |
| 105 EXPECT_EQ(device_address1, |
| 106 allowed_devices_map.GetDeviceAddress(test_origin1, device_id1)); |
| 107 EXPECT_EQ(device_address2, |
| 108 allowed_devices_map.GetDeviceAddress(test_origin2, device_id2)); |
| 109 } |
| 110 |
| 111 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceFromTwoOriginsToMap) { |
| 112 BluetoothAllowedDevicesMap allowed_devices_map; |
| 113 const std::string& device_id1 = allowed_devices_map.AddDevice( |
| 114 test_origin1, device_address1, filters, optional_services); |
| 115 const std::string& device_id2 = allowed_devices_map.AddDevice( |
| 116 test_origin2, device_address1, filters, optional_services); |
| 117 |
| 118 EXPECT_NE(device_id1, device_id2); |
| 119 |
| 120 // Test that the wrong origin doesn't have access to the device. |
| 121 EXPECT_EQ(base::EmptyString(), |
| 122 allowed_devices_map.GetDeviceAddress(test_origin1, device_id2)); |
| 123 EXPECT_EQ(base::EmptyString(), |
| 124 allowed_devices_map.GetDeviceAddress(test_origin2, device_id1)); |
| 125 } |
| 126 |
| 127 TEST_F(BluetoothAllowedDevicesMapTest, AddRemoveAddDeviceToMap) { |
| 128 BluetoothAllowedDevicesMap allowed_devices_map; |
| 129 const std::string device_id_first_time = allowed_devices_map.AddDevice( |
| 130 test_origin1, device_address1, filters, optional_services); |
| 131 |
| 132 allowed_devices_map.RemoveDevice(test_origin1, device_address1); |
| 133 |
| 134 const std::string device_id_second_time = allowed_devices_map.AddDevice( |
| 135 test_origin1, device_address1, filters, optional_services); |
| 136 |
| 137 EXPECT_NE(device_id_first_time, device_id_second_time); |
| 138 } |
| 139 |
| 140 TEST_F(BluetoothAllowedDevicesMapTest, RemoveDeviceFromMap) { |
| 141 BluetoothAllowedDevicesMap allowed_devices_map; |
| 142 |
| 143 const std::string& device_id = allowed_devices_map.AddDevice( |
| 144 test_origin1, device_address1, filters, optional_services); |
| 145 |
| 146 allowed_devices_map.RemoveDevice(test_origin1, device_address1); |
| 147 |
| 148 EXPECT_EQ(base::EmptyString(), |
| 149 allowed_devices_map.GetDeviceId(test_origin1, device_id)); |
| 150 EXPECT_EQ(base::EmptyString(), allowed_devices_map.GetDeviceAddress( |
| 151 test_origin1, device_address1)); |
| 152 } |
| 153 |
| 154 TEST_F(BluetoothAllowedDevicesMapTest, RemoveNonExistentDeviceFromMap) { |
| 155 BluetoothAllowedDevicesMap allowed_devices_map; |
| 156 |
| 157 const std::string& device_id = allowed_devices_map.AddDevice( |
| 158 test_origin1, device_address1, filters, optional_services); |
| 159 |
| 160 // Remove nonexistent devices. |
| 161 allowed_devices_map.RemoveDevice(test_origin1, device_address2); |
| 162 allowed_devices_map.RemoveDevice(test_origin2, device_address1); |
| 163 |
| 164 // Test that we can still retrieve the device address/id. |
| 165 EXPECT_EQ(device_id, |
| 166 allowed_devices_map.GetDeviceId(test_origin1, device_address1)); |
| 167 EXPECT_EQ(device_address1, |
| 168 allowed_devices_map.GetDeviceAddress(test_origin1, device_id)); |
| 169 } |
| 170 |
| 171 TEST_F(BluetoothAllowedDevicesMapTest, CorrectIdFormat) { |
| 172 BluetoothAllowedDevicesMap allowed_devices_map; |
| 173 |
| 174 const std::string& device_id = allowed_devices_map.AddDevice( |
| 175 test_origin1, device_address1, filters, optional_services); |
| 176 |
| 177 EXPECT_TRUE(device_id.size() == 24) |
| 178 << "Expected Lenghth of a 128bit string encoded to Base64."; |
| 179 EXPECT_TRUE((device_id[22] == '=') && (device_id[23] == '=')) |
| 180 << "Expected padding characters for a 128bit string encoded to Base64."; |
| 181 } |
| 182 |
| 183 } // namespace content |
OLD | NEW |