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 namespace testing {
26
27 using bluetooth::common::ByteArray;
28 using bluetooth::hci::Address;
29 using bluetooth::hci::DeviceType;
30 using bluetooth::hci::kExampleLinkKey;
31 using bluetooth::storage::ClassicDevice;
32 using bluetooth::storage::ConfigCache;
33 using bluetooth::storage::Device;
34 using bluetooth::storage::Mutation;
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
68 } // namespace testing