1723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris/*
2723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris * Copyright (C) 2017 The Android Open Source Project
3723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris *
4723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris * Licensed under the Apache License, Version 2.0 (the "License");
5723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris * you may not use this file except in compliance with the License.
6723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris * You may obtain a copy of the License at
7723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris *
8723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris *      http://www.apache.org/licenses/LICENSE-2.0
9723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris *
10723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris * Unless required by applicable law or agreed to in writing, software
11723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris * distributed under the License is distributed on an "AS IS" BASIS,
12723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris * See the License for the specific language governing permissions and
14723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris * limitations under the License.
15723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris */
16723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
17723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris#include <stdint.h>
18723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris#include <string.h>
19723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
20723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris#include <vector>
21723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
22723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris#include <gtest/gtest.h>
23723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
24723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris#include "Memory.h"
25723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
263958f8060ac0adccd977c0fab7a53d45f3fce58dChristopher FerrisTEST(MemoryLocalTest, read) {
27723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  std::vector<uint8_t> src(1024);
28723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  memset(src.data(), 0x4c, 1024);
29723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
30723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  MemoryLocal local;
31723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
32723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  std::vector<uint8_t> dst(1024);
33723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_TRUE(local.Read(reinterpret_cast<uint64_t>(src.data()), dst.data(), 1024));
34723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_EQ(0, memcmp(src.data(), dst.data(), 1024));
35723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  for (size_t i = 0; i < 1024; i++) {
36723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris    ASSERT_EQ(0x4cU, dst[i]);
37723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  }
38723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
39723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  memset(src.data(), 0x23, 512);
40723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_TRUE(local.Read(reinterpret_cast<uint64_t>(src.data()), dst.data(), 1024));
41723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_EQ(0, memcmp(src.data(), dst.data(), 1024));
42723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  for (size_t i = 0; i < 512; i++) {
43723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris    ASSERT_EQ(0x23U, dst[i]);
44723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  }
45723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  for (size_t i = 512; i < 1024; i++) {
46723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris    ASSERT_EQ(0x4cU, dst[i]);
47723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  }
48723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris}
49723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
503958f8060ac0adccd977c0fab7a53d45f3fce58dChristopher FerrisTEST(MemoryLocalTest, read_string) {
51723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  std::string name("string_in_memory");
52723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
53723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  MemoryLocal local;
54723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
55723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  std::vector<uint8_t> dst(1024);
56723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  std::string dst_name;
57723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_TRUE(local.ReadString(reinterpret_cast<uint64_t>(name.c_str()), &dst_name));
58723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_EQ("string_in_memory", dst_name);
59723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
60723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_TRUE(local.ReadString(reinterpret_cast<uint64_t>(&name[7]), &dst_name));
61723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_EQ("in_memory", dst_name);
62723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
63723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_TRUE(local.ReadString(reinterpret_cast<uint64_t>(&name[7]), &dst_name, 10));
64723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_EQ("in_memory", dst_name);
65723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
66723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_FALSE(local.ReadString(reinterpret_cast<uint64_t>(&name[7]), &dst_name, 9));
67723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris}
68723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
693958f8060ac0adccd977c0fab7a53d45f3fce58dChristopher FerrisTEST(MemoryLocalTest, read_illegal) {
70723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  MemoryLocal local;
71723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris
72723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  std::vector<uint8_t> dst(100);
73723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_FALSE(local.Read(0, dst.data(), 1));
74723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris  ASSERT_FALSE(local.Read(0, dst.data(), 100));
75723cf9b6e61744f7a20a807e67ab50adb9db5d42Christopher Ferris}
76