Home | History | Annotate | Download | only in cryptohome
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chromeos/cryptohome/mock_async_method_caller.h"
      6 
      7 using ::testing::Invoke;
      8 using ::testing::WithArgs;
      9 using ::testing::_;
     10 
     11 namespace cryptohome {
     12 
     13 const char MockAsyncMethodCaller::kFakeAttestationEnrollRequest[] = "enrollreq";
     14 const char MockAsyncMethodCaller::kFakeAttestationCertRequest[] = "certreq";
     15 const char MockAsyncMethodCaller::kFakeAttestationCert[] = "cert";
     16 const char MockAsyncMethodCaller::kFakeSanitizedUsername[] = "01234567890ABC";
     17 const char MockAsyncMethodCaller::kFakeChallengeResponse[] =
     18     "challenge_response";
     19 
     20 MockAsyncMethodCaller::MockAsyncMethodCaller()
     21     : success_(false), return_code_(cryptohome::MOUNT_ERROR_NONE) {
     22 }
     23 
     24 MockAsyncMethodCaller::~MockAsyncMethodCaller() {}
     25 
     26 void MockAsyncMethodCaller::SetUp(bool success, MountError return_code) {
     27   success_ = success;
     28   return_code_ = return_code;
     29   ON_CALL(*this, AsyncCheckKey(_, _, _))
     30       .WillByDefault(
     31           WithArgs<2>(Invoke(this, &MockAsyncMethodCaller::DoCallback)));
     32   ON_CALL(*this, AsyncMigrateKey(_, _, _, _))
     33       .WillByDefault(
     34           WithArgs<3>(Invoke(this, &MockAsyncMethodCaller::DoCallback)));
     35   ON_CALL(*this, AsyncMount(_, _, _, _))
     36       .WillByDefault(
     37           WithArgs<3>(Invoke(this, &MockAsyncMethodCaller::DoCallback)));
     38   ON_CALL(*this, AsyncAddKey(_, _, _, _))
     39       .WillByDefault(
     40           WithArgs<3>(Invoke(this, &MockAsyncMethodCaller::DoCallback)));
     41   ON_CALL(*this, AsyncMountGuest(_))
     42       .WillByDefault(
     43           WithArgs<0>(Invoke(this, &MockAsyncMethodCaller::DoCallback)));
     44   ON_CALL(*this, AsyncRemove(_, _))
     45       .WillByDefault(
     46           WithArgs<1>(Invoke(this, &MockAsyncMethodCaller::DoCallback)));
     47   ON_CALL(*this, AsyncTpmAttestationCreateEnrollRequest(_))
     48       .WillByDefault(
     49           WithArgs<0>(Invoke(this,
     50                              &MockAsyncMethodCaller::FakeCreateEnrollRequest)));
     51   ON_CALL(*this, AsyncTpmAttestationEnroll(_, _))
     52       .WillByDefault(
     53           WithArgs<1>(Invoke(this, &MockAsyncMethodCaller::DoCallback)));
     54   ON_CALL(*this, AsyncTpmAttestationCreateCertRequest(_, _))
     55       .WillByDefault(
     56           WithArgs<1>(Invoke(this,
     57                              &MockAsyncMethodCaller::FakeCreateCertRequest)));
     58   ON_CALL(*this, AsyncTpmAttestationFinishCertRequest(_, _, _, _))
     59       .WillByDefault(
     60           WithArgs<3>(Invoke(this,
     61                              &MockAsyncMethodCaller::FakeFinishCertRequest)));
     62   ON_CALL(*this, AsyncGetSanitizedUsername(_, _))
     63       .WillByDefault(
     64           WithArgs<1>(Invoke(this,
     65                              &MockAsyncMethodCaller::
     66                                  FakeGetSanitizedUsername)));
     67   ON_CALL(*this, TpmAttestationSignEnterpriseChallenge(_, _, _, _, _, _, _))
     68       .WillByDefault(
     69           WithArgs<6>(Invoke(this,
     70                              &MockAsyncMethodCaller::FakeEnterpriseChallenge)));
     71   ON_CALL(*this, TpmAttestationRegisterKey(_, _, _))
     72       .WillByDefault(
     73           WithArgs<2>(Invoke(this, &MockAsyncMethodCaller::DoCallback)));
     74 }
     75 
     76 void MockAsyncMethodCaller::DoCallback(Callback callback) {
     77   callback.Run(success_, return_code_);
     78 }
     79 
     80 void MockAsyncMethodCaller::FakeCreateEnrollRequest(
     81     const DataCallback& callback) {
     82   callback.Run(success_, kFakeAttestationEnrollRequest);
     83 }
     84 
     85 void MockAsyncMethodCaller::FakeCreateCertRequest(
     86     const DataCallback& callback) {
     87   callback.Run(success_, kFakeAttestationCertRequest);
     88 }
     89 
     90 void MockAsyncMethodCaller::FakeFinishCertRequest(
     91     const DataCallback& callback) {
     92   callback.Run(success_, kFakeAttestationCert);
     93 }
     94 
     95 void MockAsyncMethodCaller::FakeGetSanitizedUsername(
     96     const DataCallback& callback) {
     97   callback.Run(success_, kFakeSanitizedUsername);
     98 }
     99 
    100 void MockAsyncMethodCaller::FakeEnterpriseChallenge(
    101     const DataCallback& callback) {
    102   callback.Run(success_, kFakeChallengeResponse);
    103 }
    104 
    105 }  // namespace cryptohome
    106