1 /*
2 * Copyright 2020 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include "common/byte_array.h"
21 #include "hci/link_key.h"
22 #include "storage/classic_device.h"
23 #include "storage/mutation.h"
24
25 using bluetooth::common::ByteArray;
26 using bluetooth::hci::Address;
27 using bluetooth::hci::DeviceType;
28 using bluetooth::hci::kExampleLinkKey;
29 using bluetooth::storage::ClassicDevice;
30 using bluetooth::storage::ConfigCache;
31 using bluetooth::storage::Device;
32 using bluetooth::storage::Mutation;
33 using ::testing::Eq;
34 using ::testing::Optional;
35
TEST(ClassicDeviceTest,create_new_le_device)36 TEST(ClassicDeviceTest, create_new_le_device) {
37 ConfigCache config(10, Device::kLinkKeyProperties);
38 ConfigCache memory_only_config(10, {});
39 bluetooth::hci::Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
40 ClassicDevice device(&config, &memory_only_config, address.ToString());
41 ASSERT_FALSE(device.GetLinkKey());
42 }
43
TEST(ClassicDeviceTest,set_property)44 TEST(ClassicDeviceTest, set_property) {
45 ConfigCache config(10, Device::kLinkKeyProperties);
46 ConfigCache memory_only_config(10, {});
47 Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
48 ClassicDevice device(&config, &memory_only_config, address.ToString());
49 ASSERT_FALSE(device.GetLinkKey());
50 Mutation mutation(&config, &memory_only_config);
51 mutation.Add(device.SetLinkKey(kExampleLinkKey));
52 mutation.Commit();
53 ASSERT_THAT(device.GetLinkKey(), Optional(Eq(kExampleLinkKey)));
54 }
55
TEST(ClassicDeviceTest,equality_test)56 TEST(ClassicDeviceTest, equality_test) {
57 ConfigCache config(10, Device::kLinkKeyProperties);
58 ConfigCache memory_only_config(10, {});
59 bluetooth::hci::Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
60 ClassicDevice device1(&config, &memory_only_config, address.ToString());
61 ClassicDevice device2(&config, &memory_only_config, address.ToString());
62 ASSERT_EQ(device1, device2);
63 bluetooth::hci::Address address3 = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x07}};
64 ClassicDevice device3(&config, &memory_only_config, address3.ToString());
65 ASSERT_NE(device1, device3);
66 }
67
TEST(ClassicDeviceTest,operator_less_than)68 TEST(ClassicDeviceTest, operator_less_than) {
69 ConfigCache config1(10, Device::kLinkKeyProperties);
70 ConfigCache config2(10, Device::kLinkKeyProperties);
71 ASSERT_NE(&config1, &config2);
72 ConfigCache* smaller_config_ptr = &config1;
73 ConfigCache* larger_config_ptr = &config2;
74 if (&config2 < &config1) {
75 smaller_config_ptr = &config2;
76 larger_config_ptr = &config1;
77 }
78
79 ConfigCache memory_only_config1(10, {});
80 ConfigCache memory_only_config2(10, {});
81 ASSERT_NE(&memory_only_config1, &memory_only_config2);
82 ConfigCache* smaller_memory_only_config_ptr = &memory_only_config1;
83 ConfigCache* larger_memory_only_config_ptr = &memory_only_config2;
84 if (&memory_only_config2 < &memory_only_config1) {
85 smaller_memory_only_config_ptr = &memory_only_config2;
86 larger_memory_only_config_ptr = &memory_only_config1;
87 }
88
89 bluetooth::hci::Address smaller_address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
90 bluetooth::hci::Address larger_address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x07}};
91
92 {
93 ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
94 ClassicDevice device2(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
95 ASSERT_TRUE(device1 < device2);
96 }
97
98 {
99 ClassicDevice device1(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
100 ClassicDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
101 ASSERT_FALSE(device1 < device2);
102 }
103
104 {
105 ClassicDevice device1(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
106 ClassicDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
107 ASSERT_TRUE(device1 < device2);
108 }
109
110 {
111 ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
112 ClassicDevice device2(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
113 ASSERT_TRUE(device1 < device2);
114 }
115
116 {
117 ClassicDevice device1(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
118 ClassicDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
119 ASSERT_FALSE(device1 < device2);
120 }
121
122 {
123 ClassicDevice device1(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
124 ClassicDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
125 ASSERT_FALSE(device1 < device2);
126 }
127
128 {
129 ClassicDevice device1(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
130 ClassicDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
131 ASSERT_TRUE(device1 < device2);
132 }
133
134 {
135 ClassicDevice device1(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
136 ClassicDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
137 ASSERT_FALSE(device1 < device2);
138 }
139
140 {
141 ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
142 ClassicDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
143 ASSERT_TRUE(device1 < device2);
144 }
145
146 {
147 ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
148 ClassicDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
149 ASSERT_TRUE(device1 < device2);
150 }
151
152 {
153 ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
154 ClassicDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
155 ASSERT_TRUE(device1 < device2);
156 }
157
158 {
159 ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
160 ClassicDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
161 ASSERT_TRUE(device1 < device2);
162 }
163 }
164