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 "security/record/security_record_storage.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include "security/test/fake_storage_module.h"
22 
23 namespace bluetooth {
24 namespace security {
25 namespace record {
26 namespace {
27 
28 class DISABLED_SecurityRecordStorageTest : public ::testing::Test {
29  protected:
SetUp()30   void SetUp() override {
31     // Make Fake storage module
32     storage_module_ = new FakeStorageModule();
33 
34     // Inject
35     fake_registry_.InjectTestModule(&storage::StorageModule::Factory, storage_module_);
36 
37     // Make storage
38     record_storage_ = new record::SecurityRecordStorage(storage_module_, handler_);
39   }
40 
TearDown()41   void TearDown() override {
42     synchronize();
43     fake_registry_.StopAll();
44     delete record_storage_;
45   }
46 
synchronize()47   void synchronize() {
48     fake_registry_.SynchronizeModuleHandler(&FakeStorageModule::Factory, std::chrono::milliseconds(20));
49   }
50 
51   TestModuleRegistry fake_registry_;
52   os::Thread& thread_ = fake_registry_.GetTestThread();
53   os::Handler* handler_ = nullptr;
54   FakeStorageModule* storage_module_;
55   record::SecurityRecordStorage* record_storage_;
56 };
57 
TEST_F(DISABLED_SecurityRecordStorageTest,setup_teardown)58 TEST_F(DISABLED_SecurityRecordStorageTest, setup_teardown) {}
59 
TEST_F(DISABLED_SecurityRecordStorageTest,store_security_record)60 TEST_F(DISABLED_SecurityRecordStorageTest, store_security_record) {
61   hci::AddressWithType remote(
62       hci::Address({0x01, 0x02, 0x03, 0x04, 0x05, 0x06}), hci::AddressType::PUBLIC_DEVICE_ADDRESS);
63   std::array<uint8_t, 16> link_key = {
64       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0};
65   std::shared_ptr<record::SecurityRecord> record = std::make_shared<record::SecurityRecord>(remote);
66 
67   record->SetLinkKey(link_key, hci::KeyType::DEBUG_COMBINATION);
68   std::set<std::shared_ptr<record::SecurityRecord>> record_set;
69   record_set.insert(record);
70   record_storage_->SaveSecurityRecords(&record_set);
71 
72   auto device = storage_module_->GetDeviceByClassicMacAddress(remote.GetAddress());
73   ASSERT_TRUE(device.GetDeviceType());
74   ASSERT_EQ(device.Classic().GetLinkKeyType(), record->GetKeyType());
75   int i = 0;
76   for (i = 0; i < 16; ++i) {
77     ASSERT_EQ(link_key[i], device.Classic().GetLinkKey()->bytes[i]);
78   }
79 }
80 
TEST_F(DISABLED_SecurityRecordStorageTest,store_le_security_record)81 TEST_F(DISABLED_SecurityRecordStorageTest, store_le_security_record) {
82   hci::AddressWithType identity_address(
83       hci::Address({0x01, 0x02, 0x03, 0x04, 0x05, 0x06}), hci::AddressType::RANDOM_DEVICE_ADDRESS);
84   std::array<uint8_t, 16> remote_ltk{
85       0x07, 0x0c, 0x0e, 0x16, 0x18, 0x55, 0xc6, 0x72, 0x64, 0x5a, 0xd8, 0xb1, 0xf6, 0x93, 0x94, 0xa7};
86   uint16_t remote_ediv = 0x28;
87   std::array<uint8_t, 8> remote_rand{0x48, 0xac, 0x91, 0xf4, 0xef, 0x6d, 0x41, 0x10};
88   std::array<uint8_t, 16> remote_irk{
89       0x66, 0x90, 0x40, 0x76, 0x27, 0x69, 0x57, 0x71, 0x0d, 0x39, 0xf7, 0x80, 0x9e, 0x2f, 0x49, 0xcf};
90   std::array<uint8_t, 16> remote_signature_key{
91       0x08, 0x83, 0xae, 0x44, 0xd6, 0x77, 0x9e, 0x90, 0x1d, 0x25, 0xcd, 0xd7, 0xb6, 0xf4, 0x57, 0x85};
92   std::shared_ptr<record::SecurityRecord> record = std::make_shared<record::SecurityRecord>(identity_address);
93 
94   record->identity_address_ = identity_address;
95   record->remote_ltk = remote_ltk;
96   record->key_size = 16;
97   record->security_level = 2;
98   record->remote_ediv = remote_ediv;
99   record->remote_rand = remote_rand;
100   record->remote_irk = remote_irk;
101   record->remote_signature_key = remote_signature_key;
102 
103   std::set<std::shared_ptr<record::SecurityRecord>> record_set;
104   record_set.insert(record);
105   record_storage_->SaveSecurityRecords(&record_set);
106 
107   auto device = storage_module_->GetDeviceByClassicMacAddress(identity_address.GetAddress());
108   ASSERT_EQ(hci::DeviceType::LE, device.GetDeviceType());
109   ASSERT_EQ(device.Le().GetAddressType(), identity_address.GetAddressType());
110 
111   // IRK, address type, and address glued together
112   ASSERT_EQ(*device.Le().GetPeerId(), "66904076276957710d39f7809e2f49cf01010203040506");
113 
114   // LTK, RAND, EDIV and sec level glued together
115   ASSERT_EQ(*device.Le().GetPeerEncryptionKeys(), "070c0e161855c672645ad8b1f69394a748ac91f4ef6d411028000210");
116 
117   // Counter, signature key, and security level glued together
118   ASSERT_EQ(device.Le().GetPeerSignatureResolvingKeys(), "000000000883ae44d6779e901d25cdd7b6f4578502");
119 }
120 
TEST_F(DISABLED_SecurityRecordStorageTest,load_security_record)121 TEST_F(DISABLED_SecurityRecordStorageTest, load_security_record) {
122   hci::AddressWithType remote(
123       hci::Address({0x01, 0x02, 0x03, 0x04, 0x05, 0x06}), hci::AddressType::PUBLIC_DEVICE_ADDRESS);
124   std::array<uint8_t, 16> link_key = {
125       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0};
126   std::shared_ptr<record::SecurityRecord> record = std::make_shared<record::SecurityRecord>(remote);
127 
128   record->SetLinkKey(link_key, hci::KeyType::DEBUG_COMBINATION);
129   std::set<std::shared_ptr<record::SecurityRecord>> record_set;
130   record_set.insert(record);
131   record_storage_->SaveSecurityRecords(&record_set);
132 
133   auto device = storage_module_->GetDeviceByClassicMacAddress(remote.GetAddress());
134   ASSERT_TRUE(device.GetDeviceType());
135 
136   ASSERT_EQ(device.Classic().GetLinkKeyType(), record->GetKeyType());
137   int i = 0;
138   for (i = 0; i < 16; ++i) {
139     ASSERT_EQ(link_key[i], device.Classic().GetLinkKey()->bytes[i]);
140   }
141 
142   record_set.clear();
143   record_storage_->LoadSecurityRecords(&record_set);
144   record = *record_set.begin();
145   link_key = record->GetLinkKey();
146 
147   ASSERT_EQ(device.Classic().GetLinkKeyType(), record->GetKeyType());
148   ASSERT_TRUE(device.GetDeviceType());
149   for (i = 0; i < 16; ++i) {
150     ASSERT_EQ(link_key[i], device.Classic().GetLinkKey()->bytes[i]);
151   }
152 }
153 
TEST_F(DISABLED_SecurityRecordStorageTest,dont_save_temporary_records)154 TEST_F(DISABLED_SecurityRecordStorageTest, dont_save_temporary_records) {
155   hci::AddressWithType remote(
156       hci::Address({0x01, 0x02, 0x03, 0x04, 0x05, 0x06}), hci::AddressType::PUBLIC_DEVICE_ADDRESS);
157   std::array<uint8_t, 16> link_key = {
158       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0};
159   std::shared_ptr<record::SecurityRecord> record = std::make_shared<record::SecurityRecord>(remote);
160 
161   record->SetLinkKey(link_key, hci::KeyType::DEBUG_COMBINATION);
162   record->SetIsTemporary(true);
163   std::set<std::shared_ptr<record::SecurityRecord>> record_set;
164   record_set.insert(record);
165   record_storage_->SaveSecurityRecords(&record_set);
166 
167   auto device = storage_module_->GetDeviceByClassicMacAddress(remote.GetAddress());
168   ASSERT_FALSE(device.GetDeviceType());
169 
170   record_set.clear();
171   record_storage_->LoadSecurityRecords(&record_set);
172   ASSERT_EQ(record_set.size(), 0);
173 }
174 
TEST_F(DISABLED_SecurityRecordStorageTest,test_remove)175 TEST_F(DISABLED_SecurityRecordStorageTest, test_remove) {
176   hci::AddressWithType remote(
177       hci::Address({0x01, 0x02, 0x03, 0x04, 0x05, 0x06}), hci::AddressType::PUBLIC_DEVICE_ADDRESS);
178   std::array<uint8_t, 16> link_key = {
179       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0};
180   std::shared_ptr<record::SecurityRecord> record = std::make_shared<record::SecurityRecord>(remote);
181 
182   record->SetLinkKey(link_key, hci::KeyType::DEBUG_COMBINATION);
183   std::set<std::shared_ptr<record::SecurityRecord>> record_set;
184   record_set.insert(record);
185   record_storage_->SaveSecurityRecords(&record_set);
186 
187   auto device = storage_module_->GetDeviceByClassicMacAddress(remote.GetAddress());
188   ASSERT_TRUE(device.GetDeviceType());
189 
190   ASSERT_EQ(device.Classic().GetLinkKeyType(), record->GetKeyType());
191   int i = 0;
192   for (i = 0; i < 16; ++i) {
193     ASSERT_EQ(link_key[i], device.Classic().GetLinkKey()->bytes[i]);
194   }
195 
196   record_storage_->RemoveDevice(remote);
197 
198   record_set.clear();
199   record_storage_->LoadSecurityRecords(&record_set);
200   ASSERT_EQ(record_set.size(), 0);
201 }
202 
203 }  // namespace
204 }  // namespace record
205 }  // namespace security
206 }  // namespace bluetooth
207