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