1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "host-common/HostmemIdMapping.h"
16
17 #include <gtest/gtest.h>
18
19 using android::emulation::HostmemIdMapping;
20
21 // Tests creation and destruction.
TEST(HostmemIdMapping,Basic)22 TEST(HostmemIdMapping, Basic) {
23 HostmemIdMapping m;
24 }
25
26 // Tests basic operations on an entry: add, remove, get entry info
TEST(HostmemIdMapping,BasicEntry)27 TEST(HostmemIdMapping, BasicEntry) {
28 HostmemIdMapping m;
29 {
30 MemEntry entry{
31 .hva = (void *)(uintptr_t)0,
32 .size = 1,
33 .register_fixed = 0,
34 .fixed_id = 0,
35 .caching = MAP_CACHE_NONE,
36 };
37 auto id = m.add(&entry);
38 EXPECT_EQ(HostmemIdMapping::kInvalidHostmemId, id);
39 }
40 {
41 MemEntry entry{
42 .hva = (void *)(uintptr_t) 1,
43 .size = 0,
44 .register_fixed = 0,
45 .fixed_id = 0,
46 .caching = MAP_CACHE_NONE,
47 };
48 auto id = m.add(&entry);
49 EXPECT_EQ(HostmemIdMapping::kInvalidHostmemId, id);
50 }
51 {
52 MemEntry inputEntry{
53 .hva = (void *)(uintptr_t) 1,
54 .size = 2,
55 .register_fixed = 0,
56 .fixed_id = 0,
57 .caching = MAP_CACHE_NONE,
58 };
59 auto id = m.add(&inputEntry);
60 EXPECT_NE(HostmemIdMapping::kInvalidHostmemId, id);
61
62 auto entry = m.get(id);
63 EXPECT_EQ(id, entry.id);
64 EXPECT_EQ(1, (uint64_t)(uintptr_t)entry.hva);
65 EXPECT_EQ(2, entry.size);
66
67 m.remove(id);
68
69 entry = m.get(id);
70
71 EXPECT_EQ(HostmemIdMapping::kInvalidHostmemId, entry.id);
72 EXPECT_EQ(0, (uint64_t)(uintptr_t)entry.hva);
73 EXPECT_EQ(0, entry.size);
74 }
75 }
76
77 // Tests the clear() method.
TEST(HostmemIdMapping,Clear)78 TEST(HostmemIdMapping, Clear) {
79 HostmemIdMapping m;
80 MemEntry entry1{
81 .hva = (void *)(uintptr_t) 1,
82 .size = 2,
83 .register_fixed = 0,
84 .fixed_id = 0,
85 .caching = MAP_CACHE_NONE,
86 };
87 auto id1 = m.add(&entry1);
88 MemEntry entry2{
89 .hva = (void *)(uintptr_t) 3,
90 .size = 4,
91 .register_fixed = 0,
92 .fixed_id = 0,
93 .caching = MAP_CACHE_NONE,
94 };
95 auto id2 = m.add(&entry2);
96
97 m.clear();
98
99 auto entry = m.get(id1);
100 EXPECT_EQ(HostmemIdMapping::kInvalidHostmemId, entry.id);
101 EXPECT_EQ(0, (uint64_t)(uintptr_t)entry.hva);
102 EXPECT_EQ(0, entry.size);
103
104 entry = m.get(id2);
105 EXPECT_EQ(HostmemIdMapping::kInvalidHostmemId, entry.id);
106 EXPECT_EQ(0, (uint64_t)(uintptr_t)entry.hva);
107 EXPECT_EQ(0, entry.size);
108 }
109
110