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