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