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 #include "url/gurl.h" |
| 12 |
| 13 namespace content { |
| 14 namespace { |
| 15 const url::Origin test_origin1(GURL("https://www.example1.com")); |
| 16 const url::Origin test_origin2(GURL("https://www.example2.com")); |
| 17 |
| 18 const std::string device_address1 = "00:00:00"; |
| 19 const std::string device_address2 = "11:11:11"; |
| 20 |
| 21 const std::vector<content::BluetoothScanFilter> filters = |
| 22 std::vector<BluetoothScanFilter>(); |
| 23 const std::vector<device::BluetoothUUID> optional_services = |
| 24 std::vector<device::BluetoothUUID>(); |
| 25 } // namespace |
| 26 |
| 27 class BluetoothAllowedDevicesMapTest : public testing::Test {}; |
| 28 |
| 29 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMap) { |
| 30 BluetoothAllowedDevicesMap allowed_devices_map; |
| 31 |
| 32 const std::string& device_id = allowed_devices_map.AddDevice( |
| 33 test_origin1, device_address1, filters, optional_services); |
| 34 |
| 35 // Test that we can retrieve the device address/id. |
| 36 EXPECT_EQ(device_id, |
| 37 allowed_devices_map.GetDeviceId(test_origin1, device_address1)); |
| 38 EXPECT_EQ(device_address1, |
| 39 allowed_devices_map.GetDeviceAddress(test_origin1, device_id)); |
| 40 } |
| 41 |
| 42 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMapTwice) { |
| 43 BluetoothAllowedDevicesMap allowed_devices_map; |
| 44 const std::string& device_id1 = allowed_devices_map.AddDevice( |
| 45 test_origin1, device_address1, filters, optional_services); |
| 46 const std::string& device_id2 = allowed_devices_map.AddDevice( |
| 47 test_origin1, device_address1, filters, optional_services); |
| 48 |
| 49 EXPECT_EQ(device_id1, device_id2); |
| 50 |
| 51 // Test that we can retrieve the device address/id. |
| 52 EXPECT_EQ(device_id1, |
| 53 allowed_devices_map.GetDeviceId(test_origin1, device_address1)); |
| 54 EXPECT_EQ(device_address1, |
| 55 allowed_devices_map.GetDeviceAddress(test_origin1, device_id1)); |
| 56 } |
| 57 |
| 58 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromSameOriginToMap) { |
| 59 BluetoothAllowedDevicesMap allowed_devices_map; |
| 60 const std::string& device_id1 = allowed_devices_map.AddDevice( |
| 61 test_origin1, device_address1, filters, optional_services); |
| 62 const std::string& device_id2 = allowed_devices_map.AddDevice( |
| 63 test_origin1, device_address2, filters, optional_services); |
| 64 |
| 65 EXPECT_NE(device_id1, device_id2); |
| 66 |
| 67 // Test that we can retrieve the device address/id. |
| 68 EXPECT_EQ(device_id1, |
| 69 allowed_devices_map.GetDeviceId(test_origin1, device_address1)); |
| 70 EXPECT_EQ(device_id2, |
| 71 allowed_devices_map.GetDeviceId(test_origin1, device_address2)); |
| 72 |
| 73 EXPECT_EQ(device_address1, |
| 74 allowed_devices_map.GetDeviceAddress(test_origin1, device_id1)); |
| 75 EXPECT_EQ(device_address2, |
| 76 allowed_devices_map.GetDeviceAddress(test_origin1, device_id2)); |
| 77 } |
| 78 |
| 79 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromTwoOriginsToMap) { |
| 80 BluetoothAllowedDevicesMap allowed_devices_map; |
| 81 const std::string& device_id1 = allowed_devices_map.AddDevice( |
| 82 test_origin1, device_address1, filters, optional_services); |
| 83 const std::string& device_id2 = allowed_devices_map.AddDevice( |
| 84 test_origin2, device_address2, filters, optional_services); |
| 85 |
| 86 EXPECT_NE(device_id1, device_id2); |
| 87 |
| 88 // Test that the wrong origin doesn't have access to the device. |
| 89 |
| 90 EXPECT_EQ(base::EmptyString(), |
| 91 allowed_devices_map.GetDeviceId(test_origin1, device_address2)); |
| 92 EXPECT_EQ(base::EmptyString(), |
| 93 allowed_devices_map.GetDeviceId(test_origin2, device_address1)); |
| 94 |
| 95 EXPECT_EQ(base::EmptyString(), |
| 96 allowed_devices_map.GetDeviceAddress(test_origin1, device_id2)); |
| 97 EXPECT_EQ(base::EmptyString(), |
| 98 allowed_devices_map.GetDeviceAddress(test_origin2, device_id1)); |
| 99 |
| 100 // Test that we can retrieve the device address/id. |
| 101 EXPECT_EQ(device_id1, |
| 102 allowed_devices_map.GetDeviceId(test_origin1, device_address1)); |
| 103 EXPECT_EQ(device_id2, |
| 104 allowed_devices_map.GetDeviceId(test_origin2, device_address2)); |
| 105 |
| 106 EXPECT_EQ(device_address1, |
| 107 allowed_devices_map.GetDeviceAddress(test_origin1, device_id1)); |
| 108 EXPECT_EQ(device_address2, |
| 109 allowed_devices_map.GetDeviceAddress(test_origin2, device_id2)); |
| 110 } |
| 111 |
| 112 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceFromTwoOriginsToMap) { |
| 113 BluetoothAllowedDevicesMap allowed_devices_map; |
| 114 const std::string& device_id1 = allowed_devices_map.AddDevice( |
| 115 test_origin1, device_address1, filters, optional_services); |
| 116 const std::string& device_id2 = allowed_devices_map.AddDevice( |
| 117 test_origin2, device_address1, filters, optional_services); |
| 118 |
| 119 EXPECT_NE(device_id1, device_id2); |
| 120 |
| 121 // Test that the wrong origin doesn't have access to the device. |
| 122 EXPECT_EQ(base::EmptyString(), |
| 123 allowed_devices_map.GetDeviceAddress(test_origin1, device_id2)); |
| 124 EXPECT_EQ(base::EmptyString(), |
| 125 allowed_devices_map.GetDeviceAddress(test_origin2, device_id1)); |
| 126 } |
| 127 |
| 128 TEST_F(BluetoothAllowedDevicesMapTest, AddRemoveAddDeviceToMap) { |
| 129 BluetoothAllowedDevicesMap allowed_devices_map; |
| 130 const std::string device_id_first_time = allowed_devices_map.AddDevice( |
| 131 test_origin1, device_address1, filters, optional_services); |
| 132 |
| 133 allowed_devices_map.RemoveDevice(test_origin1, device_address1); |
| 134 |
| 135 const std::string device_id_second_time = allowed_devices_map.AddDevice( |
| 136 test_origin1, device_address1, filters, optional_services); |
| 137 |
| 138 EXPECT_NE(device_id_first_time, device_id_second_time); |
| 139 } |
| 140 |
| 141 TEST_F(BluetoothAllowedDevicesMapTest, RemoveDeviceFromMap) { |
| 142 BluetoothAllowedDevicesMap allowed_devices_map; |
| 143 |
| 144 const std::string& device_id = allowed_devices_map.AddDevice( |
| 145 test_origin1, device_address1, filters, optional_services); |
| 146 |
| 147 allowed_devices_map.RemoveDevice(test_origin1, device_address1); |
| 148 |
| 149 EXPECT_EQ(base::EmptyString(), |
| 150 allowed_devices_map.GetDeviceId(test_origin1, device_id)); |
| 151 EXPECT_EQ(base::EmptyString(), allowed_devices_map.GetDeviceAddress( |
| 152 test_origin1, device_address1)); |
| 153 } |
| 154 |
| 155 TEST_F(BluetoothAllowedDevicesMapTest, CorrectIdFormat) { |
| 156 BluetoothAllowedDevicesMap allowed_devices_map; |
| 157 |
| 158 const std::string& device_id = allowed_devices_map.AddDevice( |
| 159 test_origin1, device_address1, filters, optional_services); |
| 160 |
| 161 EXPECT_TRUE(device_id.size() == 24) |
| 162 << "Expected Lenghth of a 128bit string encoded to Base64."; |
| 163 EXPECT_TRUE((device_id[22] == '=') && (device_id[23] == '=')) |
| 164 << "Expected padding characters for a 128bit string encoded to Base64."; |
| 165 } |
| 166 |
| 167 } // namespace content |
OLD | NEW |