Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(754)

Side by Side Diff: content/browser/bluetooth/bluetooth_allowed_devices_map_unittest.cc

Issue 1502663003: bluetooth: Implement allowed devices map (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Address jyasskin's comments Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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, AddTwoDevicesToMap) {
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_origin2, device_address2, filters, optional_services);
Jeffrey Yasskin 2016/01/13 02:31:36 Add a test with s/device_address2/device_address1/
ortuno 2016/01/13 21:34:31 Done.
63
64 EXPECT_NE(device_id1, device_id2);
65
66 // Test that the wrong origin doesn't have access to the device.
67
68 EXPECT_EQ(base::EmptyString(),
69 allowed_devices_map.GetDeviceId(test_origin1, device_address2));
70 EXPECT_EQ(base::EmptyString(),
71 allowed_devices_map.GetDeviceId(test_origin2, device_address1));
72
73 EXPECT_EQ(base::EmptyString(),
74 allowed_devices_map.GetDeviceAddress(test_origin1, device_id2));
75 EXPECT_EQ(base::EmptyString(),
76 allowed_devices_map.GetDeviceAddress(test_origin2, device_id1));
77
78 // Test that we can retrieve the device address/id.
79 EXPECT_EQ(device_id1,
80 allowed_devices_map.GetDeviceId(test_origin1, device_address1));
81 EXPECT_EQ(device_id2,
82 allowed_devices_map.GetDeviceId(test_origin2, device_address2));
83
84 EXPECT_EQ(device_address1,
85 allowed_devices_map.GetDeviceAddress(test_origin1, device_id1));
86 EXPECT_EQ(device_address2,
87 allowed_devices_map.GetDeviceAddress(test_origin2, device_id2));
88 }
89
90 TEST_F(BluetoothAllowedDevicesMapTest, AddRemoveAddDeviceToMap) {
91 BluetoothAllowedDevicesMap allowed_devices_map;
92 const std::string device_id_first_time = allowed_devices_map.AddDevice(
93 test_origin1, device_address1, filters, optional_services);
94
95 allowed_devices_map.RemoveDevice(test_origin1, device_address1);
96
97 const std::string device_id_second_time = allowed_devices_map.AddDevice(
98 test_origin1, device_address1, filters, optional_services);
99
100 EXPECT_NE(device_id_first_time, device_id_second_time);
101 }
102
103 TEST_F(BluetoothAllowedDevicesMapTest, RemoveDeviceFromMap) {
104 BluetoothAllowedDevicesMap allowed_devices_map;
105
106 const std::string& device_id = allowed_devices_map.AddDevice(
107 test_origin1, device_address1, filters, optional_services);
108
109 allowed_devices_map.RemoveDevice(test_origin1, device_address1);
110
111 EXPECT_EQ(base::EmptyString(),
112 allowed_devices_map.GetDeviceId(test_origin1, device_id));
113 EXPECT_EQ(base::EmptyString(), allowed_devices_map.GetDeviceAddress(
114 test_origin1, device_address1));
115 }
116
117 TEST_F(BluetoothAllowedDevicesMapTest, RemoveNonExistentDeviceFromMap) {
118 BluetoothAllowedDevicesMap allowed_devices_map;
119
120 const std::string& device_id = allowed_devices_map.AddDevice(
121 test_origin1, device_address1, filters, optional_services);
122
123 // Remove nonexistent devices.
124 allowed_devices_map.RemoveDevice(test_origin1, device_address2);
125 allowed_devices_map.RemoveDevice(test_origin2, device_address1);
126
127 // Test that we can retrieve the device address/id.
Jeffrey Yasskin 2016/01/13 02:31:36 "still retrieve the added device address/id"
ortuno 2016/01/13 21:34:31 Done.
128 EXPECT_EQ(device_id,
129 allowed_devices_map.GetDeviceId(test_origin1, device_address1));
130 EXPECT_EQ(device_address1,
131 allowed_devices_map.GetDeviceAddress(test_origin1, device_id));
132 }
133
134 TEST_F(BluetoothAllowedDevicesMapTest, CorrectIdFormat) {
135 BluetoothAllowedDevicesMap allowed_devices_map;
136
137 const std::string& device_id = allowed_devices_map.AddDevice(
138 test_origin1, device_address1, filters, optional_services);
139
140 EXPECT_TRUE(device_id.size() == 24)
141 << "Expected Lenghth of a 128bit string encoded to Base64.";
142 EXPECT_TRUE((device_id[22] == '=') && (device_id[23] == '='))
143 << "Expected padding characters for a 128bit string encoded to Base64.";
144 }
145
146 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698