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 <string>
18
19 #include <brillo/bind_lambda.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "tpm_manager/client/tpm_nvram_binder_proxy.h"
24 #include "tpm_manager/client/tpm_ownership_binder_proxy.h"
25 #include "tpm_manager/common/mock_tpm_nvram_interface.h"
26 #include "tpm_manager/common/mock_tpm_ownership_interface.h"
27 #include "tpm_manager/common/tpm_manager_constants.h"
28 #include "tpm_manager/server/binder_service.h"
29
30 using testing::_;
31 using testing::Invoke;
32 using testing::NiceMock;
33 using testing::Return;
34 using testing::StrictMock;
35 using testing::WithArgs;
36
37 namespace tpm_manager {
38
39 // A test fixture to exercise both proxy and service layers. Tpm*BinderProxy
40 // classes get coverage here and do not need additional unit tests.
41 class BinderServiceTest : public testing::Test {
42 public:
43 ~BinderServiceTest() override = default;
SetUp()44 void SetUp() override {
45 binder_service_.reset(
46 new BinderService(&mock_nvram_service_, &mock_ownership_service_));
47 binder_service_->InitForTesting();
48 nvram_proxy_.reset(
49 new TpmNvramBinderProxy(binder_service_->GetITpmNvram()));
50 ownership_proxy_.reset(
51 new TpmOwnershipBinderProxy(binder_service_->GetITpmOwnership()));
52 }
53
54 template <typename ResponseProtobufType>
GetCallback(ResponseProtobufType * proto)55 base::Callback<void(const ResponseProtobufType&)> GetCallback(
56 ResponseProtobufType* proto) {
57 return base::Bind(
58 [](ResponseProtobufType* proto, const ResponseProtobufType& response) {
59 *proto = response;
60 },
61 base::Unretained(proto));
62 }
63
64 protected:
65 StrictMock<MockTpmNvramInterface> mock_nvram_service_;
66 StrictMock<MockTpmOwnershipInterface> mock_ownership_service_;
67 std::unique_ptr<BinderService> binder_service_;
68 std::unique_ptr<TpmNvramBinderProxy> nvram_proxy_;
69 std::unique_ptr<TpmOwnershipBinderProxy> ownership_proxy_;
70 };
71
TEST_F(BinderServiceTest,CopyableCallback)72 TEST_F(BinderServiceTest, CopyableCallback) {
73 EXPECT_CALL(mock_ownership_service_, GetTpmStatus(_, _))
74 .WillOnce(WithArgs<1>(Invoke(
75 [](const TpmOwnershipInterface::GetTpmStatusCallback& callback) {
76 // Copy the callback, then call the original.
77 GetTpmStatusReply reply;
78 base::Closure copy = base::Bind(callback, reply);
79 callback.Run(reply);
80 })));
81 GetTpmStatusRequest request;
82 GetTpmStatusReply reply;
83 ownership_proxy_->GetTpmStatus(request,
84 GetCallback<GetTpmStatusReply>(&reply));
85 }
86
TEST_F(BinderServiceTest,GetTpmStatus)87 TEST_F(BinderServiceTest, GetTpmStatus) {
88 GetTpmStatusRequest request;
89 EXPECT_CALL(mock_ownership_service_, GetTpmStatus(_, _))
90 .WillOnce(Invoke(
91 [](const GetTpmStatusRequest& request,
92 const TpmOwnershipInterface::GetTpmStatusCallback& callback) {
93 GetTpmStatusReply reply;
94 reply.set_status(STATUS_SUCCESS);
95 reply.set_enabled(true);
96 reply.set_owned(true);
97 reply.set_dictionary_attack_counter(3);
98 reply.set_dictionary_attack_threshold(4);
99 reply.set_dictionary_attack_lockout_in_effect(true);
100 reply.set_dictionary_attack_lockout_seconds_remaining(5);
101 callback.Run(reply);
102 }));
103 GetTpmStatusReply reply;
104 ownership_proxy_->GetTpmStatus(request,
105 GetCallback<GetTpmStatusReply>(&reply));
106 EXPECT_EQ(STATUS_SUCCESS, reply.status());
107 EXPECT_TRUE(reply.enabled());
108 EXPECT_TRUE(reply.owned());
109 EXPECT_EQ(3, reply.dictionary_attack_counter());
110 EXPECT_EQ(4, reply.dictionary_attack_threshold());
111 EXPECT_TRUE(reply.dictionary_attack_lockout_in_effect());
112 EXPECT_EQ(5, reply.dictionary_attack_lockout_seconds_remaining());
113 }
114
TEST_F(BinderServiceTest,TakeOwnership)115 TEST_F(BinderServiceTest, TakeOwnership) {
116 EXPECT_CALL(mock_ownership_service_, TakeOwnership(_, _))
117 .WillOnce(Invoke(
118 [](const TakeOwnershipRequest& request,
119 const TpmOwnershipInterface::TakeOwnershipCallback& callback) {
120 TakeOwnershipReply reply;
121 reply.set_status(STATUS_SUCCESS);
122 callback.Run(reply);
123 }));
124 TakeOwnershipRequest request;
125 TakeOwnershipReply reply;
126 ownership_proxy_->TakeOwnership(request,
127 GetCallback<TakeOwnershipReply>(&reply));
128 EXPECT_EQ(STATUS_SUCCESS, reply.status());
129 }
130
TEST_F(BinderServiceTest,RemoveOwnerDependency)131 TEST_F(BinderServiceTest, RemoveOwnerDependency) {
132 std::string owner_dependency("owner_dependency");
133 RemoveOwnerDependencyRequest request;
134 request.set_owner_dependency(owner_dependency);
135 EXPECT_CALL(mock_ownership_service_, RemoveOwnerDependency(_, _))
136 .WillOnce(Invoke([&owner_dependency](
137 const RemoveOwnerDependencyRequest& request,
138 const TpmOwnershipInterface::RemoveOwnerDependencyCallback&
139 callback) {
140 EXPECT_TRUE(request.has_owner_dependency());
141 EXPECT_EQ(owner_dependency, request.owner_dependency());
142 RemoveOwnerDependencyReply reply;
143 reply.set_status(STATUS_SUCCESS);
144 callback.Run(reply);
145 }));
146 RemoveOwnerDependencyReply reply;
147 ownership_proxy_->RemoveOwnerDependency(
148 request, GetCallback<RemoveOwnerDependencyReply>(&reply));
149 EXPECT_EQ(STATUS_SUCCESS, reply.status());
150 }
151
TEST_F(BinderServiceTest,DefineSpace)152 TEST_F(BinderServiceTest, DefineSpace) {
153 uint32_t nvram_index = 5;
154 size_t nvram_length = 32;
155 DefineSpaceRequest request;
156 request.set_index(nvram_index);
157 request.set_size(nvram_length);
158 EXPECT_CALL(mock_nvram_service_, DefineSpace(_, _))
159 .WillOnce(Invoke([nvram_index, nvram_length](
160 const DefineSpaceRequest& request,
161 const TpmNvramInterface::DefineSpaceCallback& callback) {
162 EXPECT_TRUE(request.has_index());
163 EXPECT_EQ(nvram_index, request.index());
164 EXPECT_TRUE(request.has_size());
165 EXPECT_EQ(nvram_length, request.size());
166 DefineSpaceReply reply;
167 reply.set_result(NVRAM_RESULT_SUCCESS);
168 callback.Run(reply);
169 }));
170 DefineSpaceReply reply;
171 nvram_proxy_->DefineSpace(request, GetCallback<DefineSpaceReply>(&reply));
172 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
173 }
174
TEST_F(BinderServiceTest,DestroySpace)175 TEST_F(BinderServiceTest, DestroySpace) {
176 uint32_t nvram_index = 5;
177 DestroySpaceRequest request;
178 request.set_index(nvram_index);
179 EXPECT_CALL(mock_nvram_service_, DestroySpace(_, _))
180 .WillOnce(Invoke([nvram_index](
181 const DestroySpaceRequest& request,
182 const TpmNvramInterface::DestroySpaceCallback& callback) {
183 EXPECT_TRUE(request.has_index());
184 EXPECT_EQ(nvram_index, request.index());
185 DestroySpaceReply reply;
186 reply.set_result(NVRAM_RESULT_SUCCESS);
187 callback.Run(reply);
188 }));
189 DestroySpaceReply reply;
190 nvram_proxy_->DestroySpace(request, GetCallback<DestroySpaceReply>(&reply));
191 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
192 }
193
TEST_F(BinderServiceTest,WriteSpace)194 TEST_F(BinderServiceTest, WriteSpace) {
195 uint32_t nvram_index = 5;
196 std::string nvram_data("nvram_data");
197 WriteSpaceRequest request;
198 request.set_index(nvram_index);
199 request.set_data(nvram_data);
200 EXPECT_CALL(mock_nvram_service_, WriteSpace(_, _))
201 .WillOnce(Invoke([nvram_index, nvram_data](
202 const WriteSpaceRequest& request,
203 const TpmNvramInterface::WriteSpaceCallback& callback) {
204 EXPECT_TRUE(request.has_index());
205 EXPECT_EQ(nvram_index, request.index());
206 EXPECT_TRUE(request.has_data());
207 EXPECT_EQ(nvram_data, request.data());
208 WriteSpaceReply reply;
209 reply.set_result(NVRAM_RESULT_SUCCESS);
210 callback.Run(reply);
211 }));
212 WriteSpaceReply reply;
213 nvram_proxy_->WriteSpace(request, GetCallback<WriteSpaceReply>(&reply));
214 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
215 }
216
TEST_F(BinderServiceTest,ReadSpace)217 TEST_F(BinderServiceTest, ReadSpace) {
218 uint32_t nvram_index = 5;
219 std::string nvram_data("nvram_data");
220 ReadSpaceRequest request;
221 request.set_index(nvram_index);
222 EXPECT_CALL(mock_nvram_service_, ReadSpace(_, _))
223 .WillOnce(Invoke([nvram_index, nvram_data](
224 const ReadSpaceRequest& request,
225 const TpmNvramInterface::ReadSpaceCallback& callback) {
226 EXPECT_TRUE(request.has_index());
227 EXPECT_EQ(nvram_index, request.index());
228 ReadSpaceReply reply;
229 reply.set_result(NVRAM_RESULT_SUCCESS);
230 reply.set_data(nvram_data);
231 callback.Run(reply);
232 }));
233 ReadSpaceReply reply;
234 nvram_proxy_->ReadSpace(request, GetCallback<ReadSpaceReply>(&reply));
235 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
236 EXPECT_TRUE(reply.has_data());
237 EXPECT_EQ(nvram_data, reply.data());
238 }
239
TEST_F(BinderServiceTest,LockSpace)240 TEST_F(BinderServiceTest, LockSpace) {
241 uint32_t nvram_index = 5;
242 LockSpaceRequest request;
243 request.set_index(nvram_index);
244 request.set_lock_read(true);
245 request.set_lock_write(true);
246 EXPECT_CALL(mock_nvram_service_, LockSpace(_, _))
247 .WillOnce(Invoke(
248 [nvram_index](const LockSpaceRequest& request,
249 const TpmNvramInterface::LockSpaceCallback& callback) {
250 EXPECT_TRUE(request.has_index());
251 EXPECT_EQ(nvram_index, request.index());
252 EXPECT_TRUE(request.lock_read());
253 EXPECT_TRUE(request.lock_write());
254 LockSpaceReply reply;
255 reply.set_result(NVRAM_RESULT_SUCCESS);
256 callback.Run(reply);
257 }));
258 LockSpaceReply reply;
259 nvram_proxy_->LockSpace(request, GetCallback<LockSpaceReply>(&reply));
260 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
261 }
262
TEST_F(BinderServiceTest,ListSpaces)263 TEST_F(BinderServiceTest, ListSpaces) {
264 constexpr uint32_t nvram_index_list[] = {3, 4, 5};
265 ListSpacesRequest request;
266 EXPECT_CALL(mock_nvram_service_, ListSpaces(_, _))
267 .WillOnce(Invoke([nvram_index_list](
268 const ListSpacesRequest& request,
269 const TpmNvramInterface::ListSpacesCallback& callback) {
270 ListSpacesReply reply;
271 reply.set_result(NVRAM_RESULT_SUCCESS);
272 for (auto index : nvram_index_list) {
273 reply.add_index_list(index);
274 }
275 callback.Run(reply);
276 }));
277 ListSpacesReply reply;
278 nvram_proxy_->ListSpaces(request, GetCallback<ListSpacesReply>(&reply));
279 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
280 EXPECT_EQ(arraysize(nvram_index_list), reply.index_list_size());
281 for (size_t i = 0; i < 3; i++) {
282 EXPECT_EQ(nvram_index_list[i], reply.index_list(i));
283 }
284 }
285
TEST_F(BinderServiceTest,GetSpaceInfo)286 TEST_F(BinderServiceTest, GetSpaceInfo) {
287 uint32_t nvram_index = 5;
288 size_t nvram_size = 32;
289 GetSpaceInfoRequest request;
290 request.set_index(nvram_index);
291 EXPECT_CALL(mock_nvram_service_, GetSpaceInfo(_, _))
292 .WillOnce(Invoke([nvram_index, nvram_size](
293 const GetSpaceInfoRequest& request,
294 const TpmNvramInterface::GetSpaceInfoCallback& callback) {
295 EXPECT_TRUE(request.has_index());
296 EXPECT_EQ(nvram_index, request.index());
297 GetSpaceInfoReply reply;
298 reply.set_result(NVRAM_RESULT_SUCCESS);
299 reply.set_size(nvram_size);
300 reply.set_is_read_locked(true);
301 reply.set_is_write_locked(true);
302 callback.Run(reply);
303 }));
304 GetSpaceInfoReply reply;
305 nvram_proxy_->GetSpaceInfo(request, GetCallback<GetSpaceInfoReply>(&reply));
306 EXPECT_EQ(NVRAM_RESULT_SUCCESS, reply.result());
307 EXPECT_TRUE(reply.has_size());
308 EXPECT_EQ(nvram_size, reply.size());
309 EXPECT_TRUE(reply.is_read_locked());
310 EXPECT_TRUE(reply.is_write_locked());
311 }
312
313 } // namespace tpm_manager
314