imcplugin demo: Extend to support stat() call
[nativeclient.git] / service_runtime / sel_mem_test.cc
blob621f7c4ab980a99077669eaa170a197cfb2ba3bb
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "native_client/service_runtime/sel_mem.h"
34 #include "third_party/gtest/files/include/gtest/gtest.h"
36 namespace {
38 TEST(SelMemTest, AddTest) {
39 struct NaClVmmap mem_map;
40 int start_page_num = 32;
41 int ret_code;
43 ret_code = NaClVmmapCtor(&mem_map);
44 EXPECT_EQ(1, ret_code);
46 for (int i = 1; i <= 5; ++i) {
47 ret_code = NaClVmmapAdd(&mem_map,
48 start_page_num*i,
50 PROT_READ | PROT_EXEC,
51 (struct NaClMemObj *) NULL);
52 EXPECT_EQ(1, ret_code);
53 EXPECT_EQ(i, static_cast<int>(mem_map.nvalid));
54 EXPECT_EQ(5, static_cast<int>(mem_map.size));
57 // no checks for start_page_num ..
58 ret_code = NaClVmmapAdd(&mem_map,
59 start_page_num,
61 PROT_READ,
62 (struct NaClMemObj *) NULL);
63 EXPECT_EQ(6, static_cast<int>(mem_map.nvalid));
64 EXPECT_EQ(10, static_cast<int>(mem_map.size));
66 NaClVmmapDtor(&mem_map);
69 TEST(SelMemTest, UpdateTest) {
70 struct NaClVmmap mem_map;
72 NaClVmmapCtor(&mem_map);
74 // 1st region
75 NaClVmmapUpdate(&mem_map,
76 32,
77 12,
78 PROT_READ | PROT_EXEC,
79 (struct NaClMemObj *) NULL,
80 0);
81 EXPECT_EQ(1, static_cast<int>(mem_map.nvalid));
83 // no overlap
84 NaClVmmapUpdate(&mem_map,
85 64,
86 10,
87 PROT_READ,
88 (struct NaClMemObj *) NULL,
89 0);
90 // vmmap is [32, 44], [64, 74]
91 EXPECT_EQ(2, static_cast<int>(mem_map.nvalid));
93 // new mapping overlaps end and start of existing mappings
94 NaClVmmapUpdate(&mem_map,
95 42,
96 24,
97 PROT_READ,
98 (struct NaClMemObj *) NULL,
99 0);
100 // vmmap is [32, 41], [42, 66], [67, 74]
101 EXPECT_EQ(3, static_cast<int>(mem_map.nvalid));
103 // new mapping is in the middle of existing mapping
104 NaClVmmapUpdate(&mem_map,
107 PROT_READ | PROT_EXEC,
108 (struct NaClMemObj *) NULL,
110 // vmmap is [32, 35], [34, 36], [37, 41], [42, 66], [67, 74]
111 EXPECT_EQ(5, static_cast<int>(mem_map.nvalid));
113 // new mapping covers all of the existing mapping
114 NaClVmmapUpdate(&mem_map,
117 PROT_READ | PROT_EXEC,
118 (struct NaClMemObj *) NULL,
120 // vmmap is [32, 36], [37, 41], [42, 66], [67, 74]
121 EXPECT_EQ(4, static_cast<int>(mem_map.nvalid));
123 // remove existing mappings
124 NaClVmmapUpdate(&mem_map,
127 PROT_READ | PROT_EXEC,
128 (struct NaClMemObj *) NULL,
130 // vmmap is [32, 36], [37, 39], [71, 74]
131 EXPECT_EQ(3, static_cast<int>(mem_map.nvalid));
133 NaClVmmapDtor(&mem_map);
136 TEST(SelMemTest, FindPageTest) {
137 struct NaClVmmap mem_map;
138 int ret_code;
140 ret_code = NaClVmmapCtor(&mem_map);
141 EXPECT_EQ(1, ret_code);
143 struct NaClVmmapEntry const *entry;
144 entry = NaClVmmapFindPage(&mem_map, 32);
145 EXPECT_TRUE(NULL == entry);
147 int start_page_num = 32;
148 for (int i = 1; i <= 6; ++i) {
149 ret_code = NaClVmmapAdd(&mem_map,
150 start_page_num*i,
151 2*i,
152 PROT_READ | PROT_EXEC,
153 (struct NaClMemObj *) NULL);
154 EXPECT_EQ(1, ret_code);
155 EXPECT_EQ(i, static_cast<int>(mem_map.nvalid));
157 // vmmap is [32, 34], [64, 68], [96, 102], [128, 136],
158 // [160, 170], [192, 204]
160 entry = NaClVmmapFindPage(&mem_map, 16);
161 EXPECT_TRUE(NULL == entry);
163 entry = NaClVmmapFindPage(&mem_map, 32);
164 EXPECT_TRUE(NULL != entry);
166 entry = NaClVmmapFindPage(&mem_map, 34);
167 EXPECT_TRUE(NULL == entry);
169 entry = NaClVmmapFindPage(&mem_map, 202);
170 EXPECT_TRUE(NULL != entry);
172 NaClVmmapDtor(&mem_map);
175 TEST(SelMemTest, FindSpaceTest) {
176 struct NaClVmmap mem_map;
177 int ret_code;
179 ret_code = NaClVmmapCtor(&mem_map);
180 EXPECT_EQ(1, ret_code);
182 // no entry
183 ret_code = NaClVmmapFindSpace(&mem_map, 32);
184 EXPECT_EQ(0, ret_code);
186 NaClVmmapAdd(&mem_map,
189 PROT_READ | PROT_EXEC,
190 (struct NaClMemObj *) NULL);
191 EXPECT_EQ(1, static_cast<int>(mem_map.nvalid));
192 // one entry only
193 ret_code = NaClVmmapFindSpace(&mem_map, 2);
194 EXPECT_EQ(0, ret_code);
196 NaClVmmapAdd(&mem_map,
199 PROT_READ | PROT_EXEC,
200 (struct NaClMemObj *) NULL);
201 EXPECT_EQ(2, static_cast<int>(mem_map.nvalid));
203 // the space is [32, 42], [64, 74]
204 ret_code = NaClVmmapFindSpace(&mem_map, 32);
205 EXPECT_EQ(0, ret_code);
207 ret_code = NaClVmmapFindSpace(&mem_map, 2);
208 EXPECT_EQ(62, ret_code);
210 NaClVmmapAdd(&mem_map,
213 PROT_READ | PROT_EXEC,
214 (struct NaClMemObj *) NULL);
215 EXPECT_EQ(3, static_cast<int>(mem_map.nvalid));
217 // vmmap is [32, 42], [64, 74], [96, 106]
218 // the search is from high address down
219 ret_code = NaClVmmapFindSpace(&mem_map, 22);
220 EXPECT_EQ(74, ret_code);
222 NaClVmmapDtor(&mem_map);