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 "tpm_manager/server/mock_tpm_nvram.h"
18
19 namespace tpm_manager {
20
21 using testing::_;
22 using testing::Invoke;
23 using testing::Return;
24
MockTpmNvram()25 MockTpmNvram::MockTpmNvram() {
26 ON_CALL(*this, DefineSpace(_, _, _, _, _))
27 .WillByDefault(Invoke(this, &MockTpmNvram::FakeDefineSpace));
28 ON_CALL(*this, DestroySpace(_))
29 .WillByDefault(Invoke(this, &MockTpmNvram::FakeDestroySpace));
30 ON_CALL(*this, WriteSpace(_, _, _))
31 .WillByDefault(Invoke(this, &MockTpmNvram::FakeWriteSpace));
32 ON_CALL(*this, ReadSpace(_, _, _))
33 .WillByDefault(Invoke(this, &MockTpmNvram::FakeReadSpace));
34 ON_CALL(*this, LockSpace(_, _, _, _))
35 .WillByDefault(Invoke(this, &MockTpmNvram::FakeLockSpace));
36 ON_CALL(*this, ListSpaces(_))
37 .WillByDefault(Invoke(this, &MockTpmNvram::FakeListSpaces));
38 ON_CALL(*this, GetSpaceInfo(_, _, _, _, _, _))
39 .WillByDefault(Invoke(this, &MockTpmNvram::FakeGetSpaceInfo));
40 }
41
~MockTpmNvram()42 MockTpmNvram::~MockTpmNvram() {}
43
FakeDefineSpace(uint32_t index,size_t size,const std::vector<NvramSpaceAttribute> & attributes,const std::string & authorization_value,NvramSpacePolicy policy)44 NvramResult MockTpmNvram::FakeDefineSpace(
45 uint32_t index,
46 size_t size,
47 const std::vector<NvramSpaceAttribute>& attributes,
48 const std::string& authorization_value,
49 NvramSpacePolicy policy) {
50 if (size == 0) {
51 return NVRAM_RESULT_INVALID_PARAMETER;
52 }
53 if (nvram_map_.count(index) != 0) {
54 return NVRAM_RESULT_SPACE_ALREADY_EXISTS;
55 }
56 NvSpace ns;
57 ns.data.resize(size, '\xff');
58 ns.read_locked = false;
59 ns.write_locked = false;
60 ns.attributes = attributes;
61 ns.authorization_value = authorization_value;
62 ns.policy = policy;
63 nvram_map_[index] = ns;
64 return NVRAM_RESULT_SUCCESS;
65 }
66
FakeDestroySpace(uint32_t index)67 NvramResult MockTpmNvram::FakeDestroySpace(uint32_t index) {
68 if (nvram_map_.count(index) == 0) {
69 return NVRAM_RESULT_SPACE_DOES_NOT_EXIST;
70 }
71 nvram_map_.erase(index);
72 return NVRAM_RESULT_SUCCESS;
73 }
74
FakeWriteSpace(uint32_t index,const std::string & data,const std::string & authorization_value)75 NvramResult MockTpmNvram::FakeWriteSpace(
76 uint32_t index,
77 const std::string& data,
78 const std::string& authorization_value) {
79 if (nvram_map_.count(index) == 0) {
80 return NVRAM_RESULT_SPACE_DOES_NOT_EXIST;
81 }
82 if (nvram_map_[index].authorization_value != authorization_value) {
83 return NVRAM_RESULT_ACCESS_DENIED;
84 }
85 if (nvram_map_[index].write_locked) {
86 return NVRAM_RESULT_OPERATION_DISABLED;
87 }
88 std::string& space_data = nvram_map_[index].data;
89 size_t size = space_data.size();
90 if (data.size() > size) {
91 return NVRAM_RESULT_INVALID_PARAMETER;
92 }
93 space_data = data;
94 space_data.resize(size);
95 return NVRAM_RESULT_SUCCESS;
96 }
97
FakeReadSpace(uint32_t index,std::string * data,const std::string & authorization_value)98 NvramResult MockTpmNvram::FakeReadSpace(
99 uint32_t index,
100 std::string* data,
101 const std::string& authorization_value) {
102 if (nvram_map_.count(index) == 0) {
103 return NVRAM_RESULT_SPACE_DOES_NOT_EXIST;
104 }
105 if (nvram_map_[index].authorization_value != authorization_value) {
106 return NVRAM_RESULT_ACCESS_DENIED;
107 }
108 if (nvram_map_[index].read_locked) {
109 return NVRAM_RESULT_OPERATION_DISABLED;
110 }
111 *data = nvram_map_[index].data;
112 return NVRAM_RESULT_SUCCESS;
113 }
114
FakeLockSpace(uint32_t index,bool lock_read,bool lock_write,const std::string & authorization_value)115 NvramResult MockTpmNvram::FakeLockSpace(
116 uint32_t index,
117 bool lock_read,
118 bool lock_write,
119 const std::string& authorization_value) {
120 if (nvram_map_.count(index) == 0) {
121 return NVRAM_RESULT_SPACE_DOES_NOT_EXIST;
122 }
123 if (nvram_map_[index].authorization_value != authorization_value) {
124 return NVRAM_RESULT_ACCESS_DENIED;
125 }
126 if (lock_read) {
127 nvram_map_[index].read_locked = true;
128 }
129 if (lock_write) {
130 nvram_map_[index].write_locked = true;
131 }
132 return NVRAM_RESULT_SUCCESS;
133 }
134
FakeListSpaces(std::vector<uint32_t> * index_list)135 NvramResult MockTpmNvram::FakeListSpaces(std::vector<uint32_t>* index_list) {
136 for (auto iter : nvram_map_) {
137 index_list->push_back(iter.first);
138 }
139 return NVRAM_RESULT_SUCCESS;
140 }
141
FakeGetSpaceInfo(uint32_t index,size_t * size,bool * is_read_locked,bool * is_write_locked,std::vector<NvramSpaceAttribute> * attributes,NvramSpacePolicy * policy)142 NvramResult MockTpmNvram::FakeGetSpaceInfo(
143 uint32_t index,
144 size_t* size,
145 bool* is_read_locked,
146 bool* is_write_locked,
147 std::vector<NvramSpaceAttribute>* attributes,
148 NvramSpacePolicy* policy) {
149 if (nvram_map_.count(index) == 0) {
150 return NVRAM_RESULT_SPACE_DOES_NOT_EXIST;
151 }
152 NvSpace& space = nvram_map_[index];
153 *size = space.data.size();
154 *is_read_locked = space.read_locked;
155 *is_write_locked = space.write_locked;
156 *attributes = space.attributes;
157 *policy = space.policy;
158 return NVRAM_RESULT_SUCCESS;
159 }
160
161 } // namespace tpm_manager
162