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 <base/run_loop.h>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include "tpm_manager/server/mock_local_data_store.h"
22 #include "tpm_manager/server/mock_tpm_initializer.h"
23 #include "tpm_manager/server/mock_tpm_nvram.h"
24 #include "tpm_manager/server/mock_tpm_status.h"
25 #include "tpm_manager/server/tpm_manager_service.h"
26
27 using testing::_;
28 using testing::AtLeast;
29 using testing::Invoke;
30 using testing::NiceMock;
31 using testing::Return;
32 using testing::SaveArg;
33 using testing::SetArgPointee;
34
35 namespace {
36
37 const char kOwnerPassword[] = "owner";
38 const char kOwnerDependency[] = "owner_dependency";
39 const char kOtherDependency[] = "other_dependency";
40
41 }
42
43 namespace tpm_manager {
44
45 // A test fixture that takes care of message loop management and configuring a
46 // TpmManagerService instance with mock dependencies.
47 class TpmManagerServiceTest : public testing::Test {
48 public:
49 ~TpmManagerServiceTest() override = default;
SetUp()50 void SetUp() override {
51 service_.reset(new TpmManagerService(true /*wait_for_ownership*/,
52 &mock_local_data_store_,
53 &mock_tpm_status_,
54 &mock_tpm_initializer_,
55 &mock_tpm_nvram_));
56 SetupService();
57 }
58
59 protected:
Run()60 void Run() {
61 run_loop_.Run();
62 }
63
RunServiceWorkerAndQuit()64 void RunServiceWorkerAndQuit() {
65 // Run out the service worker loop by posting a new command and waiting for
66 // the response.
67 auto callback = [this](const GetTpmStatusReply& reply) {
68 Quit();
69 };
70 GetTpmStatusRequest request;
71 service_->GetTpmStatus(request, base::Bind(callback));
72 Run();
73 }
74
Quit()75 void Quit() {
76 run_loop_.Quit();
77 }
78
SetupService()79 void SetupService() {
80 CHECK(service_->Initialize());
81 }
82
83 NiceMock<MockLocalDataStore> mock_local_data_store_;
84 NiceMock<MockTpmInitializer> mock_tpm_initializer_;
85 NiceMock<MockTpmNvram> mock_tpm_nvram_;
86 NiceMock<MockTpmStatus> mock_tpm_status_;
87 std::unique_ptr<TpmManagerService> service_;
88
89 private:
90 base::MessageLoop message_loop_;
91 base::RunLoop run_loop_;
92 };
93
94 // Tests must call SetupService().
95 class TpmManagerServiceTest_NoWaitForOwnership : public TpmManagerServiceTest {
96 public:
97 ~TpmManagerServiceTest_NoWaitForOwnership() override = default;
SetUp()98 void SetUp() override {
99 service_.reset(new TpmManagerService(false /*wait_for_ownership*/,
100 &mock_local_data_store_,
101 &mock_tpm_status_,
102 &mock_tpm_initializer_,
103 &mock_tpm_nvram_));
104 }
105 };
106
TEST_F(TpmManagerServiceTest_NoWaitForOwnership,AutoInitialize)107 TEST_F(TpmManagerServiceTest_NoWaitForOwnership, AutoInitialize) {
108 // Make sure InitializeTpm doesn't get multiple calls.
109 EXPECT_CALL(mock_tpm_initializer_, InitializeTpm()).Times(1);
110 SetupService();
111 RunServiceWorkerAndQuit();
112 }
113
TEST_F(TpmManagerServiceTest_NoWaitForOwnership,AutoInitializeNoTpm)114 TEST_F(TpmManagerServiceTest_NoWaitForOwnership, AutoInitializeNoTpm) {
115 EXPECT_CALL(mock_tpm_status_, IsTpmEnabled()).WillRepeatedly(Return(false));
116 EXPECT_CALL(mock_tpm_initializer_, InitializeTpm()).Times(0);
117 SetupService();
118 RunServiceWorkerAndQuit();
119 }
120
TEST_F(TpmManagerServiceTest_NoWaitForOwnership,AutoInitializeFailure)121 TEST_F(TpmManagerServiceTest_NoWaitForOwnership, AutoInitializeFailure) {
122 EXPECT_CALL(mock_tpm_initializer_, InitializeTpm())
123 .WillRepeatedly(Return(false));
124 SetupService();
125 RunServiceWorkerAndQuit();
126 }
127
TEST_F(TpmManagerServiceTest_NoWaitForOwnership,TakeOwnershipAfterAutoInitialize)128 TEST_F(TpmManagerServiceTest_NoWaitForOwnership,
129 TakeOwnershipAfterAutoInitialize) {
130 EXPECT_CALL(mock_tpm_initializer_, InitializeTpm()).Times(AtLeast(2));
131 SetupService();
132 auto callback = [this](const TakeOwnershipReply& reply) {
133 EXPECT_EQ(STATUS_SUCCESS, reply.status());
134 Quit();
135 };
136 TakeOwnershipRequest request;
137 service_->TakeOwnership(request, base::Bind(callback));
138 Run();
139 }
140
TEST_F(TpmManagerServiceTest,NoAutoInitialize)141 TEST_F(TpmManagerServiceTest, NoAutoInitialize) {
142 EXPECT_CALL(mock_tpm_initializer_, InitializeTpm()).Times(0);
143 RunServiceWorkerAndQuit();
144 }
145
TEST_F(TpmManagerServiceTest,GetTpmStatusSuccess)146 TEST_F(TpmManagerServiceTest, GetTpmStatusSuccess) {
147 EXPECT_CALL(mock_tpm_status_, GetDictionaryAttackInfo(_, _, _, _))
148 .WillRepeatedly(Invoke([](int* counter, int* threshold, bool* lockout,
149 int* seconds_remaining) {
150 *counter = 5;
151 *threshold = 6;
152 *lockout = true;
153 *seconds_remaining = 7;
154 return true;
155 }));
156 LocalData local_data;
157 local_data.set_owner_password(kOwnerPassword);
158 EXPECT_CALL(mock_local_data_store_, Read(_))
159 .WillRepeatedly(DoAll(SetArgPointee<0>(local_data), Return(true)));
160
161 auto callback = [this](const GetTpmStatusReply& reply) {
162 EXPECT_EQ(STATUS_SUCCESS, reply.status());
163 EXPECT_TRUE(reply.enabled());
164 EXPECT_TRUE(reply.owned());
165 EXPECT_EQ(kOwnerPassword, reply.local_data().owner_password());
166 EXPECT_EQ(5, reply.dictionary_attack_counter());
167 EXPECT_EQ(6, reply.dictionary_attack_threshold());
168 EXPECT_TRUE(reply.dictionary_attack_lockout_in_effect());
169 EXPECT_EQ(7, reply.dictionary_attack_lockout_seconds_remaining());
170 Quit();
171 };
172 GetTpmStatusRequest request;
173 service_->GetTpmStatus(request, base::Bind(callback));
174 Run();
175 }
176
TEST_F(TpmManagerServiceTest,GetTpmStatusLocalDataFailure)177 TEST_F(TpmManagerServiceTest, GetTpmStatusLocalDataFailure) {
178 EXPECT_CALL(mock_local_data_store_, Read(_))
179 .WillRepeatedly(Return(false));
180 auto callback = [this](const GetTpmStatusReply& reply) {
181 EXPECT_EQ(STATUS_SUCCESS, reply.status());
182 EXPECT_TRUE(reply.enabled());
183 EXPECT_TRUE(reply.owned());
184 EXPECT_FALSE(reply.has_local_data());
185 EXPECT_TRUE(reply.has_dictionary_attack_counter());
186 EXPECT_TRUE(reply.has_dictionary_attack_threshold());
187 EXPECT_TRUE(reply.has_dictionary_attack_lockout_in_effect());
188 EXPECT_TRUE(reply.has_dictionary_attack_lockout_seconds_remaining());
189 Quit();
190 };
191 GetTpmStatusRequest request;
192 service_->GetTpmStatus(request, base::Bind(callback));
193 Run();
194 }
195
TEST_F(TpmManagerServiceTest,GetTpmStatusNoTpm)196 TEST_F(TpmManagerServiceTest, GetTpmStatusNoTpm) {
197 EXPECT_CALL(mock_tpm_status_, IsTpmEnabled()).WillRepeatedly(Return(false));
198 EXPECT_CALL(mock_tpm_status_, GetDictionaryAttackInfo(_, _, _, _))
199 .WillRepeatedly(Return(false));
200 auto callback = [this](const GetTpmStatusReply& reply) {
201 EXPECT_EQ(STATUS_SUCCESS, reply.status());
202 EXPECT_FALSE(reply.enabled());
203 EXPECT_TRUE(reply.owned());
204 EXPECT_TRUE(reply.has_local_data());
205 EXPECT_FALSE(reply.has_dictionary_attack_counter());
206 EXPECT_FALSE(reply.has_dictionary_attack_threshold());
207 EXPECT_FALSE(reply.has_dictionary_attack_lockout_in_effect());
208 EXPECT_FALSE(reply.has_dictionary_attack_lockout_seconds_remaining());
209 Quit();
210 };
211 GetTpmStatusRequest request;
212 service_->GetTpmStatus(request, base::Bind(callback));
213 Run();
214 }
215
TEST_F(TpmManagerServiceTest,TakeOwnershipSuccess)216 TEST_F(TpmManagerServiceTest, TakeOwnershipSuccess) {
217 // Make sure InitializeTpm doesn't get multiple calls.
218 EXPECT_CALL(mock_tpm_initializer_, InitializeTpm()).Times(1);
219 auto callback = [this](const TakeOwnershipReply& reply) {
220 EXPECT_EQ(STATUS_SUCCESS, reply.status());
221 Quit();
222 };
223 TakeOwnershipRequest request;
224 service_->TakeOwnership(request, base::Bind(callback));
225 Run();
226 }
227
TEST_F(TpmManagerServiceTest,TakeOwnershipFailure)228 TEST_F(TpmManagerServiceTest, TakeOwnershipFailure) {
229 EXPECT_CALL(mock_tpm_initializer_, InitializeTpm())
230 .WillRepeatedly(Return(false));
231 auto callback = [this](const TakeOwnershipReply& reply) {
232 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
233 Quit();
234 };
235 TakeOwnershipRequest request;
236 service_->TakeOwnership(request, base::Bind(callback));
237 Run();
238 }
239
TEST_F(TpmManagerServiceTest,TakeOwnershipNoTpm)240 TEST_F(TpmManagerServiceTest, TakeOwnershipNoTpm) {
241 EXPECT_CALL(mock_tpm_status_, IsTpmEnabled()).WillRepeatedly(Return(false));
242 auto callback = [this](const TakeOwnershipReply& reply) {
243 EXPECT_EQ(STATUS_NOT_AVAILABLE, reply.status());
244 Quit();
245 };
246 TakeOwnershipRequest request;
247 service_->TakeOwnership(request, base::Bind(callback));
248 Run();
249 }
250
TEST_F(TpmManagerServiceTest,RemoveOwnerDependencyReadFailure)251 TEST_F(TpmManagerServiceTest, RemoveOwnerDependencyReadFailure) {
252 EXPECT_CALL(mock_local_data_store_, Read(_))
253 .WillRepeatedly(Return(false));
254 auto callback = [this](const RemoveOwnerDependencyReply& reply) {
255 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
256 Quit();
257 };
258 RemoveOwnerDependencyRequest request;
259 request.set_owner_dependency(kOwnerDependency);
260 service_->RemoveOwnerDependency(request, base::Bind(callback));
261 Run();
262 }
263
TEST_F(TpmManagerServiceTest,RemoveOwnerDependencyWriteFailure)264 TEST_F(TpmManagerServiceTest, RemoveOwnerDependencyWriteFailure) {
265 EXPECT_CALL(mock_local_data_store_, Write(_))
266 .WillRepeatedly(Return(false));
267 auto callback = [this](const RemoveOwnerDependencyReply& reply) {
268 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
269 Quit();
270 };
271 RemoveOwnerDependencyRequest request;
272 request.set_owner_dependency(kOwnerDependency);
273 service_->RemoveOwnerDependency(request, base::Bind(callback));
274 Run();
275 }
276
TEST_F(TpmManagerServiceTest,RemoveOwnerDependencyNotCleared)277 TEST_F(TpmManagerServiceTest, RemoveOwnerDependencyNotCleared) {
278 LocalData local_data;
279 local_data.set_owner_password(kOwnerPassword);
280 local_data.add_owner_dependency(kOwnerDependency);
281 local_data.add_owner_dependency(kOtherDependency);
282 EXPECT_CALL(mock_local_data_store_, Read(_))
283 .WillOnce(DoAll(SetArgPointee<0>(local_data),
284 Return(true)));
285 EXPECT_CALL(mock_local_data_store_, Write(_))
286 .WillOnce(DoAll(SaveArg<0>(&local_data),
287 Return(true)));
288 auto callback = [this, &local_data](const RemoveOwnerDependencyReply& reply) {
289 EXPECT_EQ(STATUS_SUCCESS, reply.status());
290 EXPECT_EQ(1, local_data.owner_dependency_size());
291 EXPECT_EQ(kOtherDependency, local_data.owner_dependency(0));
292 EXPECT_TRUE(local_data.has_owner_password());
293 EXPECT_EQ(kOwnerPassword, local_data.owner_password());
294 Quit();
295 };
296 RemoveOwnerDependencyRequest request;
297 request.set_owner_dependency(kOwnerDependency);
298 service_->RemoveOwnerDependency(request, base::Bind(callback));
299 Run();
300 }
301
TEST_F(TpmManagerServiceTest,RemoveOwnerDependencyCleared)302 TEST_F(TpmManagerServiceTest, RemoveOwnerDependencyCleared) {
303 LocalData local_data;
304 local_data.set_owner_password(kOwnerPassword);
305 local_data.add_owner_dependency(kOwnerDependency);
306 EXPECT_CALL(mock_local_data_store_, Read(_))
307 .WillOnce(DoAll(SetArgPointee<0>(local_data),
308 Return(true)));
309 EXPECT_CALL(mock_local_data_store_, Write(_))
310 .WillOnce(DoAll(SaveArg<0>(&local_data),
311 Return(true)));
312 auto callback = [this, &local_data](const RemoveOwnerDependencyReply& reply) {
313 EXPECT_EQ(STATUS_SUCCESS, reply.status());
314 EXPECT_EQ(0, local_data.owner_dependency_size());
315 EXPECT_FALSE(local_data.has_owner_password());
316 Quit();
317 };
318 RemoveOwnerDependencyRequest request;
319 request.set_owner_dependency(kOwnerDependency);
320 service_->RemoveOwnerDependency(request, base::Bind(callback));
321 Run();
322 }
323
TEST_F(TpmManagerServiceTest,RemoveOwnerDependencyNotPresent)324 TEST_F(TpmManagerServiceTest, RemoveOwnerDependencyNotPresent) {
325 LocalData local_data;
326 local_data.set_owner_password(kOwnerPassword);
327 local_data.add_owner_dependency(kOwnerDependency);
328 EXPECT_CALL(mock_local_data_store_, Read(_))
329 .WillOnce(DoAll(SetArgPointee<0>(local_data),
330 Return(true)));
331 EXPECT_CALL(mock_local_data_store_, Write(_))
332 .WillOnce(DoAll(SaveArg<0>(&local_data),
333 Return(true)));
334 auto callback = [this, &local_data](const RemoveOwnerDependencyReply& reply) {
335 EXPECT_EQ(STATUS_SUCCESS, reply.status());
336 EXPECT_EQ(1, local_data.owner_dependency_size());
337 EXPECT_EQ(kOwnerDependency, local_data.owner_dependency(0));
338 EXPECT_TRUE(local_data.has_owner_password());
339 EXPECT_EQ(kOwnerPassword, local_data.owner_password());
340 Quit();
341 };
342 RemoveOwnerDependencyRequest request;
343 request.set_owner_dependency(kOtherDependency);
344 service_->RemoveOwnerDependency(request, base::Bind(callback));
345 Run();
346 }
347
TEST_F(TpmManagerServiceTest,DefineNvramFailure)348 TEST_F(TpmManagerServiceTest, DefineNvramFailure) {
349 uint32_t nvram_index = 5;
350 size_t nvram_length = 32;
351 EXPECT_CALL(mock_tpm_nvram_, DefineNvram(nvram_index, nvram_length))
352 .WillRepeatedly(Return(false));
353 auto callback = [this](const DefineNvramReply& reply) {
354 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
355 Quit();
356 };
357 DefineNvramRequest request;
358 request.set_index(nvram_index);
359 request.set_length(nvram_length);
360 service_->DefineNvram(request, base::Bind(callback));
361 Run();
362 }
363
TEST_F(TpmManagerServiceTest,DefineNvramSuccess)364 TEST_F(TpmManagerServiceTest, DefineNvramSuccess) {
365 uint32_t nvram_index = 5;
366 uint32_t nvram_length = 32;
367 auto define_callback = [this](const DefineNvramReply& reply) {
368 EXPECT_EQ(STATUS_SUCCESS, reply.status());
369 };
370 auto is_defined_callback = [this](const IsNvramDefinedReply& reply) {
371 EXPECT_EQ(STATUS_SUCCESS, reply.status());
372 EXPECT_EQ(true, reply.is_defined());
373 };
374 auto size_callback = [this, nvram_length](const GetNvramSizeReply& reply) {
375 EXPECT_EQ(STATUS_SUCCESS, reply.status());
376 EXPECT_EQ(nvram_length, reply.size());
377 };
378 DefineNvramRequest define_request;
379 define_request.set_index(nvram_index);
380 define_request.set_length(nvram_length);
381 service_->DefineNvram(define_request, base::Bind(define_callback));
382 IsNvramDefinedRequest is_defined_request;
383 is_defined_request.set_index(nvram_index);
384 service_->IsNvramDefined(is_defined_request, base::Bind(is_defined_callback));
385 GetNvramSizeRequest size_request;
386 size_request.set_index(nvram_index);
387 service_->GetNvramSize(size_request, base::Bind(size_callback));
388 RunServiceWorkerAndQuit();
389 }
390
TEST_F(TpmManagerServiceTest,DestroyUnitializedNvram)391 TEST_F(TpmManagerServiceTest, DestroyUnitializedNvram) {
392 auto callback = [this](const DestroyNvramReply& reply) {
393 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
394 Quit();
395 };
396 DestroyNvramRequest request;
397 service_->DestroyNvram(request, base::Bind(callback));
398 Run();
399 }
400
TEST_F(TpmManagerServiceTest,DestroyNvramSuccess)401 TEST_F(TpmManagerServiceTest, DestroyNvramSuccess) {
402 uint32_t nvram_index = 5;
403 uint32_t nvram_length = 32;
404 auto define_callback = [this](const DefineNvramReply& reply) {
405 EXPECT_EQ(STATUS_SUCCESS, reply.status());
406 };
407 auto destroy_callback = [this](const DestroyNvramReply& reply) {
408 EXPECT_EQ(STATUS_SUCCESS, reply.status());
409 };
410 DefineNvramRequest define_request;
411 define_request.set_index(nvram_index);
412 define_request.set_length(nvram_length);
413 service_->DefineNvram(define_request, base::Bind(define_callback));
414 DestroyNvramRequest destroy_request;
415 destroy_request.set_index(nvram_index);
416 service_->DestroyNvram(destroy_request, base::Bind(destroy_callback));
417 RunServiceWorkerAndQuit();
418 }
419
TEST_F(TpmManagerServiceTest,DoubleDestroyNvram)420 TEST_F(TpmManagerServiceTest, DoubleDestroyNvram) {
421 uint32_t nvram_index = 5;
422 uint32_t nvram_length = 32;
423 auto define_callback = [this](const DefineNvramReply& reply) {
424 EXPECT_EQ(STATUS_SUCCESS, reply.status());
425 };
426 auto destroy_callback_success = [this](const DestroyNvramReply& reply) {
427 EXPECT_EQ(STATUS_SUCCESS, reply.status());
428 };
429 auto destroy_callback_failure = [this](const DestroyNvramReply& reply) {
430 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
431 };
432 DefineNvramRequest define_request;
433 define_request.set_index(nvram_index);
434 define_request.set_length(nvram_length);
435 service_->DefineNvram(define_request, base::Bind(define_callback));
436 DestroyNvramRequest destroy_request;
437 destroy_request.set_index(nvram_index);
438 service_->DestroyNvram(destroy_request, base::Bind(destroy_callback_success));
439 service_->DestroyNvram(destroy_request, base::Bind(destroy_callback_failure));
440 RunServiceWorkerAndQuit();
441 }
442
TEST_F(TpmManagerServiceTest,WriteUninitializedNvram)443 TEST_F(TpmManagerServiceTest, WriteUninitializedNvram) {
444 auto callback = [this](const WriteNvramReply& reply) {
445 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
446 Quit();
447 };
448 WriteNvramRequest request;
449 service_->WriteNvram(request, base::Bind(callback));
450 Run();
451 }
452
TEST_F(TpmManagerServiceTest,WriteNvramIncorrectSize)453 TEST_F(TpmManagerServiceTest, WriteNvramIncorrectSize) {
454 uint32_t nvram_index = 5;
455 std::string nvram_data("nvram_data");
456 auto define_callback = [this](const DefineNvramReply& reply) {
457 EXPECT_EQ(STATUS_SUCCESS, reply.status());
458 };
459 auto write_callback = [this](const WriteNvramReply& reply) {
460 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
461 };
462 DefineNvramRequest define_request;
463 define_request.set_index(nvram_index);
464 define_request.set_length(nvram_data.size() - 1);
465 service_->DefineNvram(define_request, base::Bind(define_callback));
466 WriteNvramRequest write_request;
467 write_request.set_index(nvram_index);
468 write_request.set_data(nvram_data);
469 service_->WriteNvram(write_request, base::Bind(write_callback));
470 RunServiceWorkerAndQuit();
471 }
472
TEST_F(TpmManagerServiceTest,DoubleWrite)473 TEST_F(TpmManagerServiceTest, DoubleWrite) {
474 uint32_t nvram_index = 5;
475 std::string nvram_data("nvram_data");
476 auto define_callback = [this](const DefineNvramReply& reply) {
477 EXPECT_EQ(STATUS_SUCCESS, reply.status());
478 };
479 auto write_callback_success = [this](const WriteNvramReply& reply) {
480 EXPECT_EQ(STATUS_SUCCESS, reply.status());
481 };
482 auto write_callback_failure = [this](const WriteNvramReply& reply) {
483 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
484 };
485 DefineNvramRequest define_request;
486 define_request.set_index(nvram_index);
487 define_request.set_length(nvram_data.size());
488 service_->DefineNvram(define_request, base::Bind(define_callback));
489 WriteNvramRequest write_request;
490 write_request.set_index(nvram_index);
491 write_request.set_data(nvram_data);
492 service_->WriteNvram(write_request, base::Bind(write_callback_success));
493 service_->WriteNvram(write_request, base::Bind(write_callback_failure));
494 RunServiceWorkerAndQuit();
495 }
496
TEST_F(TpmManagerServiceTest,ReadUninitializedNvram)497 TEST_F(TpmManagerServiceTest, ReadUninitializedNvram) {
498 auto callback = [this](const ReadNvramReply& reply) {
499 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
500 Quit();
501 };
502 ReadNvramRequest request;
503 service_->ReadNvram(request, base::Bind(callback));
504 Run();
505 }
506
TEST_F(TpmManagerServiceTest,ReadUnwrittenNvram)507 TEST_F(TpmManagerServiceTest, ReadUnwrittenNvram) {
508 uint32_t nvram_index = 5;
509 uint32_t nvram_length = 32;
510 auto define_callback = [this](const DefineNvramReply& reply) {
511 EXPECT_EQ(STATUS_SUCCESS, reply.status());
512 };
513 auto read_callback = [this](const ReadNvramReply& reply) {
514 EXPECT_EQ(STATUS_UNEXPECTED_DEVICE_ERROR, reply.status());
515 };
516 DefineNvramRequest define_request;
517 define_request.set_index(nvram_index);
518 define_request.set_length(nvram_length);
519 service_->DefineNvram(define_request, base::Bind(define_callback));
520 ReadNvramRequest read_request;
521 read_request.set_index(nvram_index);
522 service_->ReadNvram(read_request, base::Bind(read_callback));
523 RunServiceWorkerAndQuit();
524 }
525
TEST_F(TpmManagerServiceTest,ReadWriteNvramSuccess)526 TEST_F(TpmManagerServiceTest, ReadWriteNvramSuccess) {
527 uint32_t nvram_index = 5;
528 std::string nvram_data("nvram_data");
529 auto define_callback = [this](const DefineNvramReply& reply) {
530 EXPECT_EQ(STATUS_SUCCESS, reply.status());
531 };
532 auto write_callback = [this](const WriteNvramReply& reply) {
533 EXPECT_EQ(STATUS_SUCCESS, reply.status());
534 };
535 auto read_callback = [this, nvram_data](const ReadNvramReply& reply) {
536 EXPECT_EQ(STATUS_SUCCESS, reply.status());
537 EXPECT_EQ(nvram_data, reply.data());
538 };
539 auto locked_callback = [this](const IsNvramLockedReply& reply) {
540 EXPECT_EQ(STATUS_SUCCESS, reply.status());
541 EXPECT_EQ(true, reply.is_locked());
542 };
543 DefineNvramRequest define_request;
544 define_request.set_index(nvram_index);
545 define_request.set_length(nvram_data.size());
546 service_->DefineNvram(define_request, base::Bind(define_callback));
547 WriteNvramRequest write_request;
548 write_request.set_index(nvram_index);
549 write_request.set_data(nvram_data);
550 service_->WriteNvram(write_request, base::Bind(write_callback));
551 ReadNvramRequest read_request;
552 read_request.set_index(nvram_index);
553 service_->ReadNvram(read_request, base::Bind(read_callback));
554 IsNvramLockedRequest locked_request;
555 locked_request.set_index(nvram_index);
556 service_->IsNvramLocked(locked_request, base::Bind(locked_callback));
557 RunServiceWorkerAndQuit();
558 }
559
560 } // namespace tpm_manager
561