1 // 2 // Copyright (C) 2015 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 <memory> 18 #include <string> 19 20 #include <gmock/gmock.h> 21 #include <gtest/gtest.h> 22 23 #include "attestation/common/mock_crypto_utility.h" 24 #include "attestation/server/database_impl.h" 25 26 using testing::_; 27 using testing::Invoke; 28 using testing::NiceMock; 29 using testing::Return; 30 using testing::WithArgs; 31 32 namespace { 33 34 const char kFakeCredential[] = "1234"; 35 36 } // namespace 37 38 namespace attestation { 39 40 class DatabaseImplTest : public testing::Test, 41 public DatabaseIO { 42 public: 43 ~DatabaseImplTest() override = default; 44 void SetUp() override { 45 database_.reset(new DatabaseImpl(&mock_crypto_utility_)); 46 database_->set_io(this); 47 InitializeFakeData(); 48 database_->Initialize(); 49 } 50 51 // Fake DatabaseIO::Read. 52 bool Read(std::string* data) override { 53 if (fake_persistent_data_readable_) { 54 *data = fake_persistent_data_; 55 } 56 return fake_persistent_data_readable_; 57 } 58 59 // Fake DatabaseIO::Write. 60 bool Write(const std::string& data) override { 61 if (fake_persistent_data_writable_) { 62 fake_persistent_data_ = data; 63 } 64 return fake_persistent_data_writable_; 65 } 66 67 // Fake DatabaseIO::Watch. 68 void Watch(const base::Closure& callback) override { 69 fake_watch_callback_ = callback; 70 } 71 72 // Initializes fake_persistent_data_ with a default value. 73 void InitializeFakeData() { 74 AttestationDatabase proto; 75 proto.mutable_credentials()->set_conformance_credential(kFakeCredential); 76 proto.SerializeToString(&fake_persistent_data_); 77 } 78 79 protected: 80 std::string fake_persistent_data_; 81 bool fake_persistent_data_readable_{true}; 82 bool fake_persistent_data_writable_{true}; 83 base::Closure fake_watch_callback_; 84 NiceMock<MockCryptoUtility> mock_crypto_utility_; 85 std::unique_ptr<DatabaseImpl> database_; 86 }; 87 88 TEST_F(DatabaseImplTest, ReadSuccess) { 89 database_->GetMutableProtobuf()->Clear(); 90 EXPECT_TRUE(database_->Reload()); 91 EXPECT_EQ(std::string(kFakeCredential), 92 database_->GetProtobuf().credentials().conformance_credential()); 93 } 94 95 TEST_F(DatabaseImplTest, ReadFailure) { 96 fake_persistent_data_readable_ = false; 97 database_->GetMutableProtobuf()->Clear(); 98 EXPECT_FALSE(database_->Reload()); 99 EXPECT_FALSE(database_->GetProtobuf().has_credentials()); 100 } 101 102 TEST_F(DatabaseImplTest, DecryptFailure) { 103 EXPECT_CALL(mock_crypto_utility_, DecryptData(_, _, _)) 104 .WillRepeatedly(Return(false)); 105 database_->GetMutableProtobuf()->Clear(); 106 EXPECT_FALSE(database_->Reload()); 107 EXPECT_FALSE(database_->GetProtobuf().has_credentials()); 108 } 109 110 TEST_F(DatabaseImplTest, WriteSuccess) { 111 database_->GetMutableProtobuf()->mutable_credentials()-> 112 set_platform_credential("test"); 113 std::string expected_data; 114 database_->GetProtobuf().SerializeToString(&expected_data); 115 EXPECT_TRUE(database_->SaveChanges()); 116 EXPECT_EQ(expected_data, fake_persistent_data_); 117 } 118 119 TEST_F(DatabaseImplTest, WriteFailure) { 120 fake_persistent_data_writable_ = false; 121 database_->GetMutableProtobuf()->mutable_credentials()-> 122 set_platform_credential("test"); 123 EXPECT_FALSE(database_->SaveChanges()); 124 } 125 126 TEST_F(DatabaseImplTest, EncryptFailure) { 127 EXPECT_CALL(mock_crypto_utility_, EncryptData(_, _, _, _)) 128 .WillRepeatedly(Return(false)); 129 database_->GetMutableProtobuf()->mutable_credentials()-> 130 set_platform_credential("test"); 131 EXPECT_FALSE(database_->SaveChanges()); 132 } 133 134 TEST_F(DatabaseImplTest, IgnoreLegacyEncryptJunk) { 135 // Legacy encryption scheme appended a SHA-1 hash before encrypting. 136 fake_persistent_data_ += std::string(20, 'A'); 137 EXPECT_EQ(std::string(kFakeCredential), 138 database_->GetProtobuf().credentials().conformance_credential()); 139 } 140 141 TEST_F(DatabaseImplTest, Reload) { 142 AttestationDatabase proto; 143 proto.mutable_credentials()->set_platform_credential(kFakeCredential); 144 proto.SerializeToString(&fake_persistent_data_); 145 EXPECT_EQ(std::string(), 146 database_->GetProtobuf().credentials().platform_credential()); 147 EXPECT_TRUE(database_->Reload()); 148 EXPECT_EQ(std::string(kFakeCredential), 149 database_->GetProtobuf().credentials().platform_credential()); 150 } 151 152 TEST_F(DatabaseImplTest, AutoReload) { 153 AttestationDatabase proto; 154 proto.mutable_credentials()->set_platform_credential(kFakeCredential); 155 proto.SerializeToString(&fake_persistent_data_); 156 EXPECT_EQ(std::string(), 157 database_->GetProtobuf().credentials().platform_credential()); 158 fake_watch_callback_.Run(); 159 EXPECT_EQ(std::string(kFakeCredential), 160 database_->GetProtobuf().credentials().platform_credential()); 161 } 162 163 } // namespace attestation 164