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 "storage/le_device.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "storage/classic_device.h"
23 #include "storage/device.h"
24 #include "storage/mutation.h"
25
26 using bluetooth::hci::Address;
27 using bluetooth::hci::AddressType;
28 using bluetooth::hci::DeviceType;
29 using bluetooth::storage::ConfigCache;
30 using bluetooth::storage::Device;
31 using bluetooth::storage::LeDevice;
32 using bluetooth::storage::Mutation;
33 using ::testing::Eq;
34 using ::testing::Optional;
35
TEST(LeDeviceTest,create_new_le_device)36 TEST(LeDeviceTest, 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 LeDevice device(&config, &memory_only_config, address.ToString());
41 ASSERT_FALSE(device.GetAddressType());
42 }
43
TEST(LeDeviceTest,set_property)44 TEST(LeDeviceTest, set_property) {
45 ConfigCache config(10, Device::kLinkKeyProperties);
46 ConfigCache memory_only_config(10, {});
47 Address address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
48 LeDevice device(&config, &memory_only_config, address.ToString());
49 ASSERT_FALSE(device.GetAddressType());
50 Mutation mutation(&config, &memory_only_config);
51 mutation.Add(device.SetAddressType(AddressType::RANDOM_DEVICE_ADDRESS));
52 mutation.Commit();
53 ASSERT_THAT(device.GetAddressType(), Optional(Eq(AddressType::RANDOM_DEVICE_ADDRESS)));
54 }
55
TEST(LeDeviceTest,equality_test)56 TEST(LeDeviceTest, 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 LeDevice device1(&config, &memory_only_config, address.ToString());
61 LeDevice device2(&config, &memory_only_config, address.ToString());
62 ASSERT_EQ(device1, device2);
63 bluetooth::hci::Address address3 = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x07}};
64 LeDevice device3(&config, &memory_only_config, address3.ToString());
65 ASSERT_NE(device1, device3);
66 }
67
TEST(LeDeviceTest,operator_less_than)68 TEST(LeDeviceTest, 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 LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr,
94 smaller_address.ToString());
95 LeDevice device2(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
96 ASSERT_TRUE(device1 < device2);
97 }
98
99 {
100 LeDevice device1(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
101 LeDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
102 ASSERT_FALSE(device1 < device2);
103 }
104
105 {
106 LeDevice device1(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
107 LeDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
108 ASSERT_TRUE(device1 < device2);
109 }
110
111 {
112 LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
113 LeDevice device2(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
114 ASSERT_TRUE(device1 < device2);
115 }
116
117 {
118 LeDevice device1(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
119 LeDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
120 ASSERT_FALSE(device1 < device2);
121 }
122
123 {
124 LeDevice device1(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
125 LeDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr,
126 smaller_address.ToString());
127 ASSERT_FALSE(device1 < device2);
128 }
129
130 {
131 LeDevice device1(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
132 LeDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
133 ASSERT_TRUE(device1 < device2);
134 }
135
136 {
137 LeDevice device1(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
138 LeDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
139 ASSERT_FALSE(device1 < device2);
140 }
141
142 {
143 LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr,
144 smaller_address.ToString());
145 LeDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
146 ASSERT_TRUE(device1 < device2);
147 }
148
149 {
150 LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr,
151 smaller_address.ToString());
152 LeDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
153 ASSERT_TRUE(device1 < device2);
154 }
155
156 {
157 LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr,
158 smaller_address.ToString());
159 LeDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
160 ASSERT_TRUE(device1 < device2);
161 }
162
163 {
164 LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr,
165 smaller_address.ToString());
166 LeDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
167 ASSERT_TRUE(device1 < device2);
168 }
169 }
170