• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/device.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include "storage/classic_device.h"
23 #include "storage/le_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, smaller_address.ToString());
94     LeDevice device2(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
95     ASSERT_TRUE(device1 < device2);
96   }
97 
98   {
99     LeDevice device1(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
100     LeDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
101     ASSERT_FALSE(device1 < device2);
102   }
103 
104   {
105     LeDevice device1(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
106     LeDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
107     ASSERT_TRUE(device1 < device2);
108   }
109 
110   {
111     LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
112     LeDevice device2(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
113     ASSERT_TRUE(device1 < device2);
114   }
115 
116   {
117     LeDevice device1(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
118     LeDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
119     ASSERT_FALSE(device1 < device2);
120   }
121 
122   {
123     LeDevice device1(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
124     LeDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
125     ASSERT_FALSE(device1 < device2);
126   }
127 
128   {
129     LeDevice device1(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
130     LeDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
131     ASSERT_TRUE(device1 < device2);
132   }
133 
134   {
135     LeDevice device1(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
136     LeDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
137     ASSERT_FALSE(device1 < device2);
138   }
139 
140   {
141     LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
142     LeDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
143     ASSERT_TRUE(device1 < device2);
144   }
145 
146   {
147     LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
148     LeDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
149     ASSERT_TRUE(device1 < device2);
150   }
151 
152   {
153     LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
154     LeDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
155     ASSERT_TRUE(device1 < device2);
156   }
157 
158   {
159     LeDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
160     LeDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
161     ASSERT_TRUE(device1 < device2);
162   }
163 }
164