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