Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2017 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 <stdint.h>
     18 #include <string.h>
     19 
     20 #include <memory>
     21 #include <vector>
     22 
     23 #include <gtest/gtest.h>
     24 
     25 #include <unwindstack/Memory.h>
     26 
     27 #include "MemoryFake.h"
     28 
     29 namespace unwindstack {
     30 
     31 TEST(MemoryRangeTest, read) {
     32   std::vector<uint8_t> src(1024);
     33   memset(src.data(), 0x4c, 1024);
     34   MemoryFake* memory_fake = new MemoryFake;
     35   std::shared_ptr<Memory> process_memory(memory_fake);
     36   memory_fake->SetMemory(9001, src);
     37 
     38   MemoryRange range(process_memory, 9001, src.size(), 0);
     39 
     40   std::vector<uint8_t> dst(1024);
     41   ASSERT_TRUE(range.ReadFully(0, dst.data(), src.size()));
     42   for (size_t i = 0; i < 1024; i++) {
     43     ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i;
     44   }
     45 }
     46 
     47 TEST(MemoryRangeTest, read_near_limit) {
     48   std::vector<uint8_t> src(4096);
     49   memset(src.data(), 0x4c, 4096);
     50   MemoryFake* memory_fake = new MemoryFake;
     51   std::shared_ptr<Memory> process_memory(memory_fake);
     52   memory_fake->SetMemory(1000, src);
     53 
     54   MemoryRange range(process_memory, 1000, 1024, 0);
     55 
     56   std::vector<uint8_t> dst(1024);
     57   ASSERT_TRUE(range.ReadFully(1020, dst.data(), 4));
     58   for (size_t i = 0; i < 4; i++) {
     59     ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i;
     60   }
     61 
     62   // Verify that reads outside of the range will fail.
     63   ASSERT_FALSE(range.ReadFully(1020, dst.data(), 5));
     64   ASSERT_FALSE(range.ReadFully(1024, dst.data(), 1));
     65   ASSERT_FALSE(range.ReadFully(1024, dst.data(), 1024));
     66 
     67   // Verify that reading up to the end works.
     68   ASSERT_TRUE(range.ReadFully(1020, dst.data(), 4));
     69 }
     70 
     71 TEST(MemoryRangeTest, read_overflow) {
     72   std::vector<uint8_t> buffer(100);
     73 
     74   std::shared_ptr<Memory> process_memory(new MemoryFakeAlwaysReadZero);
     75   std::unique_ptr<MemoryRange> overflow(new MemoryRange(process_memory, 100, 200, 0));
     76   ASSERT_FALSE(overflow->ReadFully(UINT64_MAX - 10, buffer.data(), 100));
     77 }
     78 
     79 TEST(MemoryRangeTest, Read) {
     80   std::vector<uint8_t> src(4096);
     81   memset(src.data(), 0x4c, 4096);
     82   MemoryFake* memory_fake = new MemoryFake;
     83   std::shared_ptr<Memory> process_memory(memory_fake);
     84   memory_fake->SetMemory(1000, src);
     85 
     86   MemoryRange range(process_memory, 1000, 1024, 0);
     87   std::vector<uint8_t> dst(1024);
     88   ASSERT_EQ(4U, range.Read(1020, dst.data(), 1024));
     89   for (size_t i = 0; i < 4; i++) {
     90     ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i;
     91   }
     92 }
     93 
     94 }  // namespace unwindstack
     95