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