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 "attestation/common/mock_tpm_utility.h"
18
19 using ::testing::_;
20 using ::testing::Invoke;
21 using ::testing::Return;
22 using ::testing::WithArgs;
23
24 namespace {
25
26 class TransformString {
27 public:
TransformString(std::string method)28 explicit TransformString(std::string method) : method_(method) {}
operator ()(const std::string & in,std::string * out)29 bool operator()(const std::string& in, std::string* out) {
30 *out = attestation::MockTpmUtility::Transform(method_, in);
31 return true;
32 }
33
34 private:
35 std::string method_;
36 };
37
38 class UntransformString {
39 public:
UntransformString(std::string method)40 explicit UntransformString(std::string method) : method_(method) {}
operator ()(const std::string & in,std::string * out)41 bool operator()(const std::string& in, std::string* out) {
42 std::string suffix = "_fake_transform_" + method_;
43 auto position = in.find(suffix);
44 if (position == std::string::npos) {
45 return false;
46 }
47 *out = in.substr(0, position);
48 return true;
49 }
50
51 private:
52 std::string method_;
53 };
54
55 } // namespace
56
57 namespace attestation {
58
MockTpmUtility()59 MockTpmUtility::MockTpmUtility() {
60 ON_CALL(*this, IsTpmReady()).WillByDefault(Return(true));
61 ON_CALL(*this, ActivateIdentity(_, _, _, _, _, _))
62 .WillByDefault(Return(true));
63 ON_CALL(*this, CreateCertifiedKey(_, _, _, _, _, _, _, _, _))
64 .WillByDefault(Return(true));
65 ON_CALL(*this, SealToPCR0(_, _))
66 .WillByDefault(Invoke(TransformString("SealToPCR0")));
67 ON_CALL(*this, Unseal(_, _))
68 .WillByDefault(Invoke(UntransformString("SealToPCR0")));
69 ON_CALL(*this, Unbind(_, _, _))
70 .WillByDefault(WithArgs<1, 2>(Invoke(TransformString("Unbind"))));
71 ON_CALL(*this, Sign(_, _, _))
72 .WillByDefault(WithArgs<1, 2>(Invoke(TransformString("Sign"))));
73 }
74
~MockTpmUtility()75 MockTpmUtility::~MockTpmUtility() {}
76
77 // static
Transform(const std::string & method,const std::string & input)78 std::string MockTpmUtility::Transform(const std::string& method,
79 const std::string& input) {
80 return input + "_fake_transform_" + method;
81 }
82
83 } // namespace attestation
84