• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
21 
22 #include <gtest/gtest.h>
23 
24 #include "Memory.h"
25 
26 #include "MemoryFake.h"
27 
28 class MemoryRangeTest : public ::testing::Test {
29  protected:
SetUp()30   void SetUp() override {
31     memory_ = new MemoryFake;
32   }
33 
34   MemoryFake* memory_;
35 };
36 
TEST_F(MemoryRangeTest,read)37 TEST_F(MemoryRangeTest, read) {
38   std::vector<uint8_t> src(1024);
39   memset(src.data(), 0x4c, 1024);
40   memory_->SetMemory(9001, src);
41 
42   MemoryRange range(memory_, 9001, 9001 + src.size());
43 
44   std::vector<uint8_t> dst(1024);
45   ASSERT_TRUE(range.Read(0, dst.data(), src.size()));
46   for (size_t i = 0; i < 1024; i++) {
47     ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i;
48   }
49 }
50 
TEST_F(MemoryRangeTest,read_near_limit)51 TEST_F(MemoryRangeTest, read_near_limit) {
52   std::vector<uint8_t> src(4096);
53   memset(src.data(), 0x4c, 4096);
54   memory_->SetMemory(1000, src);
55 
56   MemoryRange range(memory_, 1000, 2024);
57 
58   std::vector<uint8_t> dst(1024);
59   ASSERT_TRUE(range.Read(1020, dst.data(), 4));
60   for (size_t i = 0; i < 4; i++) {
61     ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i;
62   }
63 
64   // Verify that reads outside of the range will fail.
65   ASSERT_FALSE(range.Read(1020, dst.data(), 5));
66   ASSERT_FALSE(range.Read(1024, dst.data(), 1));
67   ASSERT_FALSE(range.Read(1024, dst.data(), 1024));
68 }
69 
TEST_F(MemoryRangeTest,read_string_past_end)70 TEST_F(MemoryRangeTest, read_string_past_end) {
71   std::string name("0123456789");
72   memory_->SetMemory(0, name);
73 
74   // Verify a read past the range fails.
75   MemoryRange range(memory_, 0, 5);
76   std::string dst_name;
77   ASSERT_FALSE(range.ReadString(0, &dst_name));
78 }
79 
TEST_F(MemoryRangeTest,read_string_to_end)80 TEST_F(MemoryRangeTest, read_string_to_end) {
81   std::string name("0123456789");
82   memory_->SetMemory(30, name);
83 
84   // Verify the range going to the end of the string works.
85   MemoryRange range(memory_, 30, 30 + name.size() + 1);
86   std::string dst_name;
87   ASSERT_TRUE(range.ReadString(0, &dst_name));
88   ASSERT_EQ("0123456789", dst_name);
89 }
90 
TEST_F(MemoryRangeTest,read_string_fencepost)91 TEST_F(MemoryRangeTest, read_string_fencepost) {
92   std::string name("0123456789");
93   memory_->SetMemory(10, name);
94 
95   // Verify the range set to one byte less than the end of the string fails.
96   MemoryRange range(memory_, 10, 10 + name.size());
97   std::string dst_name;
98   ASSERT_FALSE(range.ReadString(0, &dst_name));
99 }
100