Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2016 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 #ifndef _LIBUNWINDSTACK_TESTS_MEMORY_FAKE_H
     18 #define _LIBUNWINDSTACK_TESTS_MEMORY_FAKE_H
     19 
     20 #include <stdint.h>
     21 
     22 #include <string>
     23 #include <vector>
     24 #include <unordered_map>
     25 
     26 #include "Memory.h"
     27 
     28 class MemoryFake : public Memory {
     29  public:
     30   MemoryFake() = default;
     31   virtual ~MemoryFake() = default;
     32 
     33   bool Read(uint64_t addr, void* buffer, size_t size) override;
     34 
     35   void SetMemory(uint64_t addr, const void* memory, size_t length);
     36 
     37   void SetData8(uint64_t addr, uint8_t value) {
     38     SetMemory(addr, &value, sizeof(value));
     39   }
     40 
     41   void SetData16(uint64_t addr, uint16_t value) {
     42     SetMemory(addr, &value, sizeof(value));
     43   }
     44 
     45   void SetData32(uint64_t addr, uint32_t value) {
     46     SetMemory(addr, &value, sizeof(value));
     47   }
     48 
     49   void SetData64(uint64_t addr, uint64_t value) {
     50     SetMemory(addr, &value, sizeof(value));
     51   }
     52 
     53   void SetMemory(uint64_t addr, std::vector<uint8_t> values) {
     54     SetMemory(addr, values.data(), values.size());
     55   }
     56 
     57   void SetMemory(uint64_t addr, std::string string) {
     58     SetMemory(addr, string.c_str(), string.size() + 1);
     59   }
     60 
     61   void Clear() { data_.clear(); }
     62 
     63  private:
     64   std::unordered_map<uint64_t, uint8_t> data_;
     65 };
     66 
     67 class MemoryFakeAlwaysReadZero : public Memory {
     68  public:
     69   MemoryFakeAlwaysReadZero() = default;
     70   virtual ~MemoryFakeAlwaysReadZero() = default;
     71 
     72   bool Read(uint64_t, void* buffer, size_t size) override {
     73     memset(buffer, 0, size);
     74     return true;
     75   }
     76 };
     77 
     78 #endif  // _LIBUNWINDSTACK_TESTS_MEMORY_FAKE_H
     79