Home | History | Annotate | Download | only in server
      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 
     25 MockTpmNvram::MockTpmNvram() {
     26   ON_CALL(*this, DefineNvram(_, _))
     27       .WillByDefault(Invoke(this, &MockTpmNvram::FakeDefineNvram));
     28   ON_CALL(*this, DestroyNvram(_))
     29       .WillByDefault(Invoke(this, &MockTpmNvram::FakeDestroyNvram));
     30   ON_CALL(*this, WriteNvram(_, _))
     31       .WillByDefault(Invoke(this, &MockTpmNvram::FakeWriteNvram));
     32   ON_CALL(*this, ReadNvram(_, _))
     33       .WillByDefault(Invoke(this, &MockTpmNvram::FakeReadNvram));
     34   ON_CALL(*this, IsNvramDefined(_, _))
     35       .WillByDefault(Invoke(this, &MockTpmNvram::FakeIsNvramDefined));
     36   ON_CALL(*this, IsNvramLocked(_, _))
     37       .WillByDefault(Invoke(this, &MockTpmNvram::FakeIsNvramLocked));
     38   ON_CALL(*this, GetNvramSize(_, _))
     39       .WillByDefault(Invoke(this, &MockTpmNvram::FakeGetNvramSize));
     40 }
     41 
     42 MockTpmNvram::~MockTpmNvram() {}
     43 
     44 bool MockTpmNvram::FakeDefineNvram(uint32_t index, size_t length) {
     45   if (length == 0) {
     46     return false;
     47   }
     48   NvSpace ns;
     49   ns.data.resize(length, '\xff');
     50   ns.written = false;
     51   nvram_map_[index] = ns;
     52   return true;
     53 }
     54 
     55 bool MockTpmNvram::FakeDestroyNvram(uint32_t index) {
     56   auto it = nvram_map_.find(index);
     57   if (it == nvram_map_.end()) {
     58     return false;
     59   }
     60   nvram_map_.erase(it);
     61   return true;
     62 }
     63 
     64 bool MockTpmNvram::FakeWriteNvram(uint32_t index, const std::string& data) {
     65   auto it = nvram_map_.find(index);
     66   if (it == nvram_map_.end()) {
     67     return false;
     68   }
     69   NvSpace& nv = it->second;
     70   if (nv.written || nv.data.size() < data.size()) {
     71     return false;
     72   }
     73   nv.data.replace(0, data.size(), data);
     74   nv.written = true;
     75   return true;
     76 }
     77 
     78 bool MockTpmNvram::FakeReadNvram(uint32_t index, std::string* data) {
     79   auto it = nvram_map_.find(index);
     80   if (it == nvram_map_.end()) {
     81     return false;
     82   }
     83   const NvSpace& nv = it->second;
     84   if (!nv.written) {
     85     return false;
     86   }
     87   data->assign(nv.data);
     88   return true;
     89 }
     90 
     91 bool MockTpmNvram::FakeIsNvramDefined(uint32_t index, bool* defined) {
     92   *defined = (nvram_map_.find(index) != nvram_map_.end());
     93   return true;
     94 }
     95 
     96 bool MockTpmNvram::FakeIsNvramLocked(uint32_t index, bool* locked) {
     97   bool defined;
     98   if (!IsNvramDefined(index, &defined) || !defined) {
     99     return false;
    100   }
    101   *locked = nvram_map_[index].written;
    102   return true;
    103 }
    104 
    105 bool MockTpmNvram::FakeGetNvramSize(uint32_t index, size_t* size) {
    106   bool defined;
    107   if (!IsNvramDefined(index, &defined) || !defined) {
    108     return false;
    109   }
    110   *size = nvram_map_[index].data.size();
    111   return true;
    112 }
    113 
    114 }  // namespace tpm_manager
    115