• 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 
TEST(MemoryLocalTest,read)26 TEST(MemoryLocalTest, read) {
27   std::vector<uint8_t> src(1024);
28   memset(src.data(), 0x4c, 1024);
29 
30   MemoryLocal local;
31 
32   std::vector<uint8_t> dst(1024);
33   ASSERT_TRUE(local.Read(reinterpret_cast<uint64_t>(src.data()), dst.data(), 1024));
34   ASSERT_EQ(0, memcmp(src.data(), dst.data(), 1024));
35   for (size_t i = 0; i < 1024; i++) {
36     ASSERT_EQ(0x4cU, dst[i]);
37   }
38 
39   memset(src.data(), 0x23, 512);
40   ASSERT_TRUE(local.Read(reinterpret_cast<uint64_t>(src.data()), dst.data(), 1024));
41   ASSERT_EQ(0, memcmp(src.data(), dst.data(), 1024));
42   for (size_t i = 0; i < 512; i++) {
43     ASSERT_EQ(0x23U, dst[i]);
44   }
45   for (size_t i = 512; i < 1024; i++) {
46     ASSERT_EQ(0x4cU, dst[i]);
47   }
48 }
49 
TEST(MemoryLocalTest,read_string)50 TEST(MemoryLocalTest, read_string) {
51   std::string name("string_in_memory");
52 
53   MemoryLocal local;
54 
55   std::vector<uint8_t> dst(1024);
56   std::string dst_name;
57   ASSERT_TRUE(local.ReadString(reinterpret_cast<uint64_t>(name.c_str()), &dst_name));
58   ASSERT_EQ("string_in_memory", dst_name);
59 
60   ASSERT_TRUE(local.ReadString(reinterpret_cast<uint64_t>(&name[7]), &dst_name));
61   ASSERT_EQ("in_memory", dst_name);
62 
63   ASSERT_TRUE(local.ReadString(reinterpret_cast<uint64_t>(&name[7]), &dst_name, 10));
64   ASSERT_EQ("in_memory", dst_name);
65 
66   ASSERT_FALSE(local.ReadString(reinterpret_cast<uint64_t>(&name[7]), &dst_name, 9));
67 }
68 
TEST(MemoryLocalTest,read_illegal)69 TEST(MemoryLocalTest, read_illegal) {
70   MemoryLocal local;
71 
72   std::vector<uint8_t> dst(100);
73   ASSERT_FALSE(local.Read(0, dst.data(), 1));
74   ASSERT_FALSE(local.Read(0, dst.data(), 100));
75 }
76