• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, public DatabaseIO {
41  public:
42   ~DatabaseImplTest() override = default;
SetUp()43   void SetUp() override {
44     database_.reset(new DatabaseImpl(&mock_crypto_utility_));
45     database_->set_io(this);
46     InitializeFakeData();
47     database_->Initialize();
48   }
49 
50   // Fake DatabaseIO::Read.
Read(std::string * data)51   bool Read(std::string* data) override {
52     if (fake_persistent_data_readable_) {
53       *data = fake_persistent_data_;
54     }
55     return fake_persistent_data_readable_;
56   }
57 
58   // Fake DatabaseIO::Write.
Write(const std::string & data)59   bool Write(const std::string& data) override {
60     if (fake_persistent_data_writable_) {
61       fake_persistent_data_ = data;
62     }
63     return fake_persistent_data_writable_;
64   }
65 
66   // Fake DatabaseIO::Watch.
Watch(const base::Closure & callback)67   void Watch(const base::Closure& callback) override {
68     fake_watch_callback_ = callback;
69   }
70 
71   // Initializes fake_persistent_data_ with a default value.
InitializeFakeData()72   void InitializeFakeData() {
73     AttestationDatabase proto;
74     proto.mutable_credentials()->set_conformance_credential(kFakeCredential);
75     proto.SerializeToString(&fake_persistent_data_);
76   }
77 
78  protected:
79   std::string fake_persistent_data_;
80   bool fake_persistent_data_readable_{true};
81   bool fake_persistent_data_writable_{true};
82   base::Closure fake_watch_callback_;
83   NiceMock<MockCryptoUtility> mock_crypto_utility_;
84   std::unique_ptr<DatabaseImpl> database_;
85 };
86 
TEST_F(DatabaseImplTest,ReadSuccess)87 TEST_F(DatabaseImplTest, ReadSuccess) {
88   database_->GetMutableProtobuf()->Clear();
89   EXPECT_TRUE(database_->Reload());
90   EXPECT_EQ(std::string(kFakeCredential),
91             database_->GetProtobuf().credentials().conformance_credential());
92 }
93 
TEST_F(DatabaseImplTest,ReadFailure)94 TEST_F(DatabaseImplTest, ReadFailure) {
95   fake_persistent_data_readable_ = false;
96   database_->GetMutableProtobuf()->Clear();
97   EXPECT_FALSE(database_->Reload());
98   EXPECT_FALSE(database_->GetProtobuf().has_credentials());
99 }
100 
TEST_F(DatabaseImplTest,DecryptFailure)101 TEST_F(DatabaseImplTest, DecryptFailure) {
102   EXPECT_CALL(mock_crypto_utility_, DecryptData(_, _, _))
103       .WillRepeatedly(Return(false));
104   database_->GetMutableProtobuf()->Clear();
105   EXPECT_FALSE(database_->Reload());
106   EXPECT_FALSE(database_->GetProtobuf().has_credentials());
107 }
108 
TEST_F(DatabaseImplTest,WriteSuccess)109 TEST_F(DatabaseImplTest, WriteSuccess) {
110   database_->GetMutableProtobuf()
111       ->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 
TEST_F(DatabaseImplTest,WriteFailure)119 TEST_F(DatabaseImplTest, WriteFailure) {
120   fake_persistent_data_writable_ = false;
121   database_->GetMutableProtobuf()
122       ->mutable_credentials()
123       ->set_platform_credential("test");
124   EXPECT_FALSE(database_->SaveChanges());
125 }
126 
TEST_F(DatabaseImplTest,EncryptFailure)127 TEST_F(DatabaseImplTest, EncryptFailure) {
128   EXPECT_CALL(mock_crypto_utility_, EncryptData(_, _, _, _))
129       .WillRepeatedly(Return(false));
130   database_->GetMutableProtobuf()
131       ->mutable_credentials()
132       ->set_platform_credential("test");
133   EXPECT_FALSE(database_->SaveChanges());
134 }
135 
TEST_F(DatabaseImplTest,IgnoreLegacyEncryptJunk)136 TEST_F(DatabaseImplTest, IgnoreLegacyEncryptJunk) {
137   // Legacy encryption scheme appended a SHA-1 hash before encrypting.
138   fake_persistent_data_ += std::string(20, 'A');
139   EXPECT_EQ(std::string(kFakeCredential),
140             database_->GetProtobuf().credentials().conformance_credential());
141 }
142 
TEST_F(DatabaseImplTest,Reload)143 TEST_F(DatabaseImplTest, Reload) {
144   AttestationDatabase proto;
145   proto.mutable_credentials()->set_platform_credential(kFakeCredential);
146   proto.SerializeToString(&fake_persistent_data_);
147   EXPECT_EQ(std::string(),
148             database_->GetProtobuf().credentials().platform_credential());
149   EXPECT_TRUE(database_->Reload());
150   EXPECT_EQ(std::string(kFakeCredential),
151             database_->GetProtobuf().credentials().platform_credential());
152 }
153 
TEST_F(DatabaseImplTest,AutoReload)154 TEST_F(DatabaseImplTest, AutoReload) {
155   AttestationDatabase proto;
156   proto.mutable_credentials()->set_platform_credential(kFakeCredential);
157   proto.SerializeToString(&fake_persistent_data_);
158   EXPECT_EQ(std::string(),
159             database_->GetProtobuf().credentials().platform_credential());
160   fake_watch_callback_.Run();
161   EXPECT_EQ(std::string(kFakeCredential),
162             database_->GetProtobuf().credentials().platform_credential());
163 }
164 
165 }  // namespace attestation
166