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 <unwindstack/Memory.h>
     27 
     28 namespace unwindstack {
     29 
     30 class MemoryFake : public Memory {
     31  public:
     32   MemoryFake() = default;
     33   virtual ~MemoryFake() = default;
     34 
     35   size_t Read(uint64_t addr, void* buffer, size_t size) override;
     36 
     37   void SetMemory(uint64_t addr, const void* memory, size_t length);
     38 
     39   void SetData8(uint64_t addr, uint8_t value) {
     40     SetMemory(addr, &value, sizeof(value));
     41   }
     42 
     43   void SetData16(uint64_t addr, uint16_t value) {
     44     SetMemory(addr, &value, sizeof(value));
     45   }
     46 
     47   void SetData32(uint64_t addr, uint32_t value) {
     48     SetMemory(addr, &value, sizeof(value));
     49   }
     50 
     51   void SetData64(uint64_t addr, uint64_t value) {
     52     SetMemory(addr, &value, sizeof(value));
     53   }
     54 
     55   void SetMemory(uint64_t addr, std::vector<uint8_t> values) {
     56     SetMemory(addr, values.data(), values.size());
     57   }
     58 
     59   void SetMemory(uint64_t addr, std::string string) {
     60     SetMemory(addr, string.c_str(), string.size() + 1);
     61   }
     62 
     63   void Clear() { data_.clear(); }
     64 
     65  private:
     66   std::unordered_map<uint64_t, uint8_t> data_;
     67 };
     68 
     69 class MemoryFakeAlwaysReadZero : public Memory {
     70  public:
     71   MemoryFakeAlwaysReadZero() = default;
     72   virtual ~MemoryFakeAlwaysReadZero() = default;
     73 
     74   size_t Read(uint64_t, void* buffer, size_t size) override {
     75     memset(buffer, 0, size);
     76     return size;
     77   }
     78 };
     79 
     80 }  // namespace unwindstack
     81 
     82 #endif  // _LIBUNWINDSTACK_TESTS_MEMORY_FAKE_H
     83