OLD | NEW |
---|---|
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 #include <errno.h> | 6 #include <errno.h> |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <pthread.h> | 8 #include <pthread.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "nacl_io/kernel_handle.h" | 14 #include "nacl_io/kernel_handle.h" |
15 #include "nacl_io/kernel_intercept.h" | 15 #include "nacl_io/kernel_intercept.h" |
16 #include "nacl_io/kernel_proxy.h" | 16 #include "nacl_io/kernel_proxy.h" |
17 #include "nacl_io/mount.h" | 17 #include "nacl_io/mount.h" |
18 #include "nacl_io/mount_mem.h" | 18 #include "nacl_io/mount_mem.h" |
19 #include "nacl_io/osmman.h" | 19 #include "nacl_io/osmman.h" |
20 #include "nacl_io/path.h" | 20 #include "nacl_io/path.h" |
21 | 21 |
22 #include "gtest/gtest.h" | 22 #include "gtest/gtest.h" |
23 | 23 |
24 | |
25 class KernelProxyTest : public ::testing::Test { | 24 class KernelProxyTest : public ::testing::Test { |
26 public: | 25 public: |
27 KernelProxyTest() | 26 KernelProxyTest() : kp_(new KernelProxy) { |
28 : kp_(new KernelProxy) { | |
29 ki_init(kp_); | 27 ki_init(kp_); |
30 // Unmount the passthrough FS and mount a memfs. | 28 // Unmount the passthrough FS and mount a memfs. |
31 EXPECT_EQ(0, kp_->umount("/")); | 29 EXPECT_EQ(0, kp_->umount("/")); |
32 EXPECT_EQ(0, kp_->mount("", "/", "memfs", 0, NULL)); | 30 EXPECT_EQ(0, kp_->mount("", "/", "memfs", 0, NULL)); |
33 } | 31 } |
34 | 32 |
35 ~KernelProxyTest() { | 33 ~KernelProxyTest() { |
36 ki_uninit(); | 34 ki_uninit(); |
37 delete kp_; | 35 delete kp_; |
38 } | 36 } |
39 | 37 |
40 private: | 38 private: |
41 KernelProxy* kp_; | 39 KernelProxy* kp_; |
42 }; | 40 }; |
43 | 41 |
44 | |
45 TEST_F(KernelProxyTest, WorkingDirectory) { | 42 TEST_F(KernelProxyTest, WorkingDirectory) { |
46 char text[1024]; | 43 char text[1024]; |
47 | 44 |
48 text[0] = 0; | 45 text[0] = 0; |
49 ki_getcwd(text, sizeof(text)); | 46 ki_getcwd(text, sizeof(text)); |
50 EXPECT_STREQ("/", text); | 47 EXPECT_STREQ("/", text); |
51 | 48 |
52 char* alloc = ki_getwd(NULL); | 49 char* alloc = ki_getwd(NULL); |
53 EXPECT_EQ((char *) NULL, alloc); | 50 EXPECT_EQ((char*)NULL, alloc); |
54 EXPECT_EQ(EFAULT, errno); | 51 EXPECT_EQ(EFAULT, errno); |
55 | 52 |
56 text[0] = 0; | 53 text[0] = 0; |
57 alloc = ki_getwd(text); | 54 alloc = ki_getwd(text); |
58 EXPECT_STREQ("/", alloc); | 55 EXPECT_STREQ("/", alloc); |
59 | 56 |
60 EXPECT_EQ(-1, ki_chdir("/foo")); | 57 EXPECT_EQ(-1, ki_chdir("/foo")); |
61 EXPECT_EQ(ENOENT, errno); | 58 EXPECT_EQ(ENOENT, errno); |
62 | 59 |
63 EXPECT_EQ(0, ki_chdir("/")); | 60 EXPECT_EQ(0, ki_chdir("/")); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 | 103 |
107 // Write hello and world to same node with different descriptors | 104 // Write hello and world to same node with different descriptors |
108 // so that we overwrite each other | 105 // so that we overwrite each other |
109 EXPECT_EQ(5, ki_write(fd2, "WORLD", 5)); | 106 EXPECT_EQ(5, ki_write(fd2, "WORLD", 5)); |
110 EXPECT_EQ(5, ki_write(fd1, "HELLO", 5)); | 107 EXPECT_EQ(5, ki_write(fd1, "HELLO", 5)); |
111 | 108 |
112 fd3 = ki_open("/foo/bar", O_WRONLY); | 109 fd3 = ki_open("/foo/bar", O_WRONLY); |
113 EXPECT_NE(-1, fd3); | 110 EXPECT_NE(-1, fd3); |
114 | 111 |
115 len = ki_read(fd3, text, sizeof(text)); | 112 len = ki_read(fd3, text, sizeof(text)); |
116 if (len > -0) text[len] = 0; | 113 if (len > -0) |
noelallen1
2013/06/07 21:48:43
-0?
binji
2013/06/07 23:23:11
Done.
| |
114 text[len] = 0; | |
117 EXPECT_EQ(5, len); | 115 EXPECT_EQ(5, len); |
118 EXPECT_STREQ("HELLO", text); | 116 EXPECT_STREQ("HELLO", text); |
119 EXPECT_EQ(0, ki_close(fd1)); | 117 EXPECT_EQ(0, ki_close(fd1)); |
120 EXPECT_EQ(0, ki_close(fd2)); | 118 EXPECT_EQ(0, ki_close(fd2)); |
121 | 119 |
122 fd1 = ki_open("/foo/bar", O_WRONLY | O_APPEND); | 120 fd1 = ki_open("/foo/bar", O_WRONLY | O_APPEND); |
123 EXPECT_NE(-1, fd1); | 121 EXPECT_NE(-1, fd1); |
124 EXPECT_EQ(5, ki_write(fd1, "WORLD", 5)); | 122 EXPECT_EQ(5, ki_write(fd1, "WORLD", 5)); |
125 | 123 |
126 len = ki_read(fd3, text, sizeof(text)); | 124 len = ki_read(fd3, text, sizeof(text)); |
127 if (len >= 0) text[len] = 0; | 125 if (len >= 0) |
126 text[len] = 0; | |
128 | 127 |
129 EXPECT_EQ(5, len); | 128 EXPECT_EQ(5, len); |
130 EXPECT_STREQ("WORLD", text); | 129 EXPECT_STREQ("WORLD", text); |
131 | 130 |
132 fd2 = ki_open("/foo/bar", O_RDONLY); | 131 fd2 = ki_open("/foo/bar", O_RDONLY); |
133 EXPECT_NE(-1, fd2); | 132 EXPECT_NE(-1, fd2); |
134 len = ki_read(fd2, text, sizeof(text)); | 133 len = ki_read(fd2, text, sizeof(text)); |
135 if (len > 0) text[len] = 0; | 134 if (len > 0) |
135 text[len] = 0; | |
136 EXPECT_EQ(10, len); | 136 EXPECT_EQ(10, len); |
137 EXPECT_STREQ("HELLOWORLD", text); | 137 EXPECT_STREQ("HELLOWORLD", text); |
138 } | 138 } |
139 | 139 |
140 TEST_F(KernelProxyTest, MemMountLseek) { | 140 TEST_F(KernelProxyTest, MemMountLseek) { |
141 int fd = ki_open("/foo", O_CREAT | O_RDWR); | 141 int fd = ki_open("/foo", O_CREAT | O_RDWR); |
142 EXPECT_EQ(9, ki_write(fd, "Some text", 9)); | 142 EXPECT_EQ(9, ki_write(fd, "Some text", 9)); |
143 | 143 |
144 EXPECT_EQ(9, ki_lseek(fd, 0, SEEK_CUR)); | 144 EXPECT_EQ(9, ki_lseek(fd, 0, SEEK_CUR)); |
145 EXPECT_EQ(9, ki_lseek(fd, 0, SEEK_END)); | 145 EXPECT_EQ(9, ki_lseek(fd, 0, SEEK_END)); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 EXPECT_EQ(4, ki_write(dup_fd, "more", 4)); | 190 EXPECT_EQ(4, ki_write(dup_fd, "more", 4)); |
191 | 191 |
192 EXPECT_EQ(0, ki_close(dup2_fd)); | 192 EXPECT_EQ(0, ki_close(dup2_fd)); |
193 // fd, new_fd -> "/bar" | 193 // fd, new_fd -> "/bar" |
194 // dup_fd -> "/foo" | 194 // dup_fd -> "/foo" |
195 | 195 |
196 EXPECT_EQ(dup_fd, ki_dup2(fd, dup_fd)); | 196 EXPECT_EQ(dup_fd, ki_dup2(fd, dup_fd)); |
197 // fd, new_fd, dup_fd -> "/bar" | 197 // fd, new_fd, dup_fd -> "/bar" |
198 } | 198 } |
199 | 199 |
200 | |
201 StringMap_t g_StringMap; | 200 StringMap_t g_StringMap; |
202 | 201 |
203 class MountMockInit : public MountMem { | 202 class MountMockInit : public MountMem { |
204 public: | 203 public: |
205 virtual bool Init(int dev, StringMap_t& args, PepperInterface* ppapi) { | 204 virtual Error Init(int dev, StringMap_t& args, PepperInterface* ppapi) { |
206 g_StringMap = args; | 205 g_StringMap = args; |
207 if (args.find("false") != args.end()) | 206 if (args.find("false") != args.end()) |
208 return false; | 207 return EINVAL; |
209 return true; | 208 return 0; |
210 }; | 209 } |
210 ; | |
211 }; | 211 }; |
212 | 212 |
213 class KernelProxyMountMock : public KernelProxy { | 213 class KernelProxyMountMock : public KernelProxy { |
214 virtual void Init(PepperInterface* ppapi) { | 214 virtual void Init(PepperInterface* ppapi) { |
215 KernelProxy::Init(NULL); | 215 KernelProxy::Init(NULL); |
216 factories_["initfs"] = MountMockInit::Create<MountMockInit>; | 216 factories_["initfs"] = MountMockInit::Create<MountMockInit>; |
217 } | 217 } |
218 }; | 218 }; |
219 | 219 |
220 class KernelProxyMountTest : public ::testing::Test { | 220 class KernelProxyMountTest : public ::testing::Test { |
221 public: | 221 public: |
222 KernelProxyMountTest() | 222 KernelProxyMountTest() : kp_(new KernelProxyMountMock) { ki_init(kp_); } |
223 : kp_(new KernelProxyMountMock) { | |
224 ki_init(kp_); | |
225 } | |
226 | 223 |
227 ~KernelProxyMountTest() { | 224 ~KernelProxyMountTest() { |
228 ki_uninit(); | 225 ki_uninit(); |
229 delete kp_; | 226 delete kp_; |
230 } | 227 } |
231 | 228 |
232 private: | 229 private: |
233 KernelProxy* kp_; | 230 KernelProxy* kp_; |
234 }; | 231 }; |
235 | 232 |
236 TEST_F(KernelProxyMountTest, MountInit) { | 233 TEST_F(KernelProxyMountTest, MountInit) { |
237 int res1 = ki_mount("/", "/mnt1", "initfs", 0, "false,foo=bar"); | 234 int res1 = ki_mount("/", "/mnt1", "initfs", 0, "false,foo=bar"); |
238 | 235 |
239 EXPECT_EQ("bar", g_StringMap["foo"]); | 236 EXPECT_EQ("bar", g_StringMap["foo"]); |
240 EXPECT_EQ(-1, res1); | 237 EXPECT_EQ(-1, res1); |
241 EXPECT_EQ(EINVAL, errno); | 238 EXPECT_EQ(EINVAL, errno); |
242 | 239 |
243 int res2 = ki_mount("/", "/mnt2", "initfs", 0, "true,bar=foo,x=y"); | 240 int res2 = ki_mount("/", "/mnt2", "initfs", 0, "true,bar=foo,x=y"); |
244 EXPECT_NE(-1, res2); | 241 EXPECT_NE(-1, res2); |
245 EXPECT_EQ("y", g_StringMap["x"]); | 242 EXPECT_EQ("y", g_StringMap["x"]); |
246 } | 243 } |
247 | 244 |
248 | |
249 namespace { | 245 namespace { |
250 | 246 |
251 int g_MMapCount = 0; | 247 int g_MMapCount = 0; |
252 | 248 |
253 class MountNodeMockMMap : public MountNode { | 249 class MountNodeMockMMap : public MountNode { |
254 public: | 250 public: |
255 MountNodeMockMMap(Mount* mount) | 251 MountNodeMockMMap(Mount* mount) : MountNode(mount), node_mmap_count_(0) { |
256 : MountNode(mount), | 252 EXPECT_EQ(0, Init(0)); |
257 node_mmap_count_(0) { | |
258 Init(0); | |
259 } | 253 } |
260 | 254 |
261 virtual void* MMap(void* addr, size_t length, int prot, int flags, | 255 virtual Error MMap(void* addr, |
262 size_t offset) { | 256 size_t length, |
257 int prot, | |
258 int flags, | |
259 size_t offset, | |
260 void** out_addr) { | |
263 node_mmap_count_++; | 261 node_mmap_count_++; |
264 switch (g_MMapCount++) { | 262 switch (g_MMapCount++) { |
265 case 0: return reinterpret_cast<void*>(0x1000); | 263 case 0: |
266 case 1: return reinterpret_cast<void*>(0x2000); | 264 *out_addr = reinterpret_cast<void*>(0x1000); |
267 case 2: return reinterpret_cast<void*>(0x3000); | 265 break; |
268 default: return MAP_FAILED; | 266 case 1: |
267 *out_addr = reinterpret_cast<void*>(0x2000); | |
268 break; | |
269 case 2: | |
270 *out_addr = reinterpret_cast<void*>(0x3000); | |
271 break; | |
272 default: | |
273 return EPERM; | |
269 } | 274 } |
275 | |
276 return 0; | |
270 } | 277 } |
271 | 278 |
272 private: | 279 private: |
273 int node_mmap_count_; | 280 int node_mmap_count_; |
274 }; | 281 }; |
275 | 282 |
276 class MountMockMMap : public Mount { | 283 class MountMockMMap : public Mount { |
277 public: | 284 public: |
278 virtual MountNode* Open(const Path& path, int mode) { | 285 virtual Error Open(const Path& path, int mode, MountNode** out_node) { |
279 MountNodeMockMMap* node = new MountNodeMockMMap(this); | 286 MountNodeMockMMap* node = new MountNodeMockMMap(this); |
280 return node; | 287 *out_node = node; |
288 return 0; | |
281 } | 289 } |
282 | 290 |
283 virtual MountNode* OpenResource(const Path& path) { return NULL; } | 291 virtual Error OpenResource(const Path& path, MountNode** out_node) { |
284 virtual int Unlink(const Path& path) { return -1; } | 292 *out_node = NULL; |
285 virtual int Mkdir(const Path& path, int permissions) { return -1; } | 293 return ENOSYS; |
286 virtual int Rmdir(const Path& path) { return -1; } | 294 } |
287 virtual int Remove(const Path& path) { return -1; } | 295 virtual Error Unlink(const Path& path) { return ENOSYS; } |
296 virtual Error Mkdir(const Path& path, int permissions) { return ENOSYS; } | |
297 virtual Error Rmdir(const Path& path) { return ENOSYS; } | |
298 virtual Error Remove(const Path& path) { return ENOSYS; } | |
288 }; | 299 }; |
289 | 300 |
290 class KernelProxyMockMMap : public KernelProxy { | 301 class KernelProxyMockMMap : public KernelProxy { |
291 virtual void Init(PepperInterface* ppapi) { | 302 virtual void Init(PepperInterface* ppapi) { |
292 KernelProxy::Init(NULL); | 303 KernelProxy::Init(NULL); |
293 factories_["mmapfs"] = MountMockInit::Create<MountMockMMap>; | 304 factories_["mmapfs"] = MountMockInit::Create<MountMockMMap>; |
294 } | 305 } |
295 }; | 306 }; |
296 | 307 |
297 class KernelProxyMMapTest : public ::testing::Test { | 308 class KernelProxyMMapTest : public ::testing::Test { |
298 public: | 309 public: |
299 KernelProxyMMapTest() | 310 KernelProxyMMapTest() : kp_(new KernelProxyMockMMap) { ki_init(kp_); } |
300 : kp_(new KernelProxyMockMMap) { | |
301 ki_init(kp_); | |
302 } | |
303 | 311 |
304 ~KernelProxyMMapTest() { | 312 ~KernelProxyMMapTest() { |
305 ki_uninit(); | 313 ki_uninit(); |
306 delete kp_; | 314 delete kp_; |
307 } | 315 } |
308 | 316 |
309 private: | 317 private: |
310 KernelProxy* kp_; | 318 KernelProxy* kp_; |
311 }; | 319 }; |
312 | 320 |
(...skipping 17 matching lines...) Expand all Loading... | |
330 EXPECT_EQ(reinterpret_cast<void*>(0x3000), addr3); | 338 EXPECT_EQ(reinterpret_cast<void*>(0x3000), addr3); |
331 EXPECT_EQ(3, g_MMapCount); | 339 EXPECT_EQ(3, g_MMapCount); |
332 | 340 |
333 ki_close(fd); | 341 ki_close(fd); |
334 | 342 |
335 // We no longer track mmap'd regions, so munmap is a no-op. | 343 // We no longer track mmap'd regions, so munmap is a no-op. |
336 EXPECT_EQ(0, ki_munmap(reinterpret_cast<void*>(0x1000), 0x2800)); | 344 EXPECT_EQ(0, ki_munmap(reinterpret_cast<void*>(0x1000), 0x2800)); |
337 // We don't track regions, so the mmap count hasn't changed. | 345 // We don't track regions, so the mmap count hasn't changed. |
338 EXPECT_EQ(3, g_MMapCount); | 346 EXPECT_EQ(3, g_MMapCount); |
339 } | 347 } |
OLD | NEW |