| 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/mount.h> | |
| 10 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 11 #include <unistd.h> | |
| 12 | 10 |
| 13 #include <map> | 11 #include <map> |
| 14 #include <string> | 12 #include <string> |
| 15 | 13 |
| 16 #include "nacl_mounts/kernel_handle.h" | 14 #include "nacl_mounts/kernel_handle.h" |
| 17 #include "nacl_mounts/kernel_intercept.h" | 15 #include "nacl_mounts/kernel_intercept.h" |
| 18 #include "nacl_mounts/kernel_proxy.h" | 16 #include "nacl_mounts/kernel_proxy.h" |
| 19 #include "nacl_mounts/mount.h" | 17 #include "nacl_mounts/mount.h" |
| 20 #include "nacl_mounts/mount_mem.h" | 18 #include "nacl_mounts/mount_mem.h" |
| 21 #include "nacl_mounts/path.h" | 19 #include "nacl_mounts/path.h" |
| 22 | 20 |
| 23 #define __STDC__ 1 | |
| 24 #include "gtest/gtest.h" | 21 #include "gtest/gtest.h" |
| 25 | 22 |
| 26 | 23 |
| 27 TEST(KernelProxy, WorkingDirectory) { | 24 TEST(KernelProxy, WorkingDirectory) { |
| 28 char text[1024]; | 25 char text[1024]; |
| 29 | 26 |
| 30 ki_init(new KernelProxy()); | 27 ki_init(new KernelProxy()); |
| 31 | 28 |
| 32 text[0] = 0; | 29 text[0] = 0; |
| 33 getcwd(text, sizeof(text)); | 30 ki_getcwd(text, sizeof(text)); |
| 34 EXPECT_STREQ("/", text); | 31 EXPECT_STREQ("/", text); |
| 35 | 32 |
| 36 char* alloc = getwd(NULL); | 33 char* alloc = ki_getwd(NULL); |
| 37 EXPECT_EQ((char *) NULL, alloc); | 34 EXPECT_EQ((char *) NULL, alloc); |
| 38 EXPECT_EQ(EFAULT, errno); | 35 EXPECT_EQ(EFAULT, errno); |
| 39 | 36 |
| 40 text[0] = 0; | 37 text[0] = 0; |
| 41 alloc = getwd(text); | 38 alloc = ki_getwd(text); |
| 42 EXPECT_STREQ("/", alloc); | 39 EXPECT_STREQ("/", alloc); |
| 43 | 40 |
| 44 EXPECT_EQ(-1, chdir("/foo")); | 41 EXPECT_EQ(-1, ki_chdir("/foo")); |
| 45 EXPECT_EQ(EEXIST, errno); | 42 EXPECT_EQ(EEXIST, errno); |
| 46 | 43 |
| 47 EXPECT_EQ(0, chdir("/")); | 44 EXPECT_EQ(0, ki_chdir("/")); |
| 48 | 45 |
| 49 EXPECT_EQ(0, mkdir("/foo", S_IREAD | S_IWRITE)); | 46 EXPECT_EQ(0, ki_mkdir("/foo", S_IREAD | S_IWRITE)); |
| 50 EXPECT_EQ(-1, mkdir("/foo", S_IREAD | S_IWRITE)); | 47 EXPECT_EQ(-1, ki_mkdir("/foo", S_IREAD | S_IWRITE)); |
| 51 EXPECT_EQ(EEXIST, errno); | 48 EXPECT_EQ(EEXIST, errno); |
| 52 | 49 |
| 53 memset(text, 0, sizeof(text)); | 50 memset(text, 0, sizeof(text)); |
| 54 EXPECT_EQ(0, chdir("foo")); | 51 EXPECT_EQ(0, ki_chdir("foo")); |
| 55 EXPECT_EQ(text, getcwd(text, sizeof(text))); | 52 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); |
| 56 EXPECT_STREQ("/foo", text); | 53 EXPECT_STREQ("/foo", text); |
| 57 | 54 |
| 58 memset(text, 0, sizeof(text)); | 55 memset(text, 0, sizeof(text)); |
| 59 EXPECT_EQ(-1, chdir("foo")); | 56 EXPECT_EQ(-1, ki_chdir("foo")); |
| 60 EXPECT_EQ(EEXIST, errno); | 57 EXPECT_EQ(EEXIST, errno); |
| 61 EXPECT_EQ(0, chdir("..")); | 58 EXPECT_EQ(0, ki_chdir("..")); |
| 62 EXPECT_EQ(0, chdir("/foo")); | 59 EXPECT_EQ(0, ki_chdir("/foo")); |
| 63 EXPECT_EQ(text, getcwd(text, sizeof(text))); | 60 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); |
| 64 EXPECT_STREQ("/foo", text); | 61 EXPECT_STREQ("/foo", text); |
| 65 } | 62 } |
| 66 | 63 |
| 67 TEST(KernelProxy, MemMountIO) { | 64 TEST(KernelProxy, MemMountIO) { |
| 68 char text[1024]; | 65 char text[1024]; |
| 69 int fd1, fd2, fd3; | 66 int fd1, fd2, fd3; |
| 70 int len; | 67 int len; |
| 71 | 68 |
| 72 ki_init(new KernelProxy()); | 69 ki_init(new KernelProxy()); |
| 73 | 70 |
| 74 // Create "/foo" | 71 // Create "/foo" |
| 75 EXPECT_EQ(0, mkdir("/foo", S_IREAD | S_IWRITE)); | 72 EXPECT_EQ(0, ki_mkdir("/foo", S_IREAD | S_IWRITE)); |
| 76 | 73 |
| 77 // Fail to open "/foo/bar" | 74 // Fail to open "/foo/bar" |
| 78 EXPECT_EQ(-1, open("/foo/bar", O_RDONLY)); | 75 EXPECT_EQ(-1, ki_open("/foo/bar", O_RDONLY)); |
| 79 EXPECT_EQ(ENOENT, errno); | 76 EXPECT_EQ(ENOENT, errno); |
| 80 | 77 |
| 81 // Create bar "/foo/bar" | 78 // Create bar "/foo/bar" |
| 82 fd1 = open("/foo/bar", O_RDONLY | O_CREAT); | 79 fd1 = ki_open("/foo/bar", O_RDONLY | O_CREAT); |
| 83 EXPECT_NE(-1, fd1); | 80 EXPECT_NE(-1, fd1); |
| 84 | 81 |
| 85 // Open (optionally create) bar "/foo/bar" | 82 // Open (optionally create) bar "/foo/bar" |
| 86 fd2 = open("/foo/bar", O_RDONLY | O_CREAT); | 83 fd2 = ki_open("/foo/bar", O_RDONLY | O_CREAT); |
| 87 EXPECT_NE(-1, fd2); | 84 EXPECT_NE(-1, fd2); |
| 88 | 85 |
| 89 // Fail to exclusively create bar "/foo/bar" | 86 // Fail to exclusively create bar "/foo/bar" |
| 90 EXPECT_EQ(-1, open("/foo/bar", O_RDONLY | O_CREAT | O_EXCL)); | 87 EXPECT_EQ(-1, ki_open("/foo/bar", O_RDONLY | O_CREAT | O_EXCL)); |
| 91 EXPECT_EQ(EEXIST, errno); | 88 EXPECT_EQ(EEXIST, errno); |
| 92 | 89 |
| 93 // Write hello and world to same node with different descriptors | 90 // Write hello and world to same node with different descriptors |
| 94 // so that we overwrite each other | 91 // so that we overwrite each other |
| 95 EXPECT_EQ(5, write(fd2, "WORLD", 5)); | 92 EXPECT_EQ(5, ki_write(fd2, "WORLD", 5)); |
| 96 EXPECT_EQ(5, write(fd1, "HELLO", 5)); | 93 EXPECT_EQ(5, ki_write(fd1, "HELLO", 5)); |
| 97 | 94 |
| 98 fd3 = open("/foo/bar", O_WRONLY); | 95 fd3 = ki_open("/foo/bar", O_WRONLY); |
| 99 EXPECT_NE(-1, fd3); | 96 EXPECT_NE(-1, fd3); |
| 100 | 97 |
| 101 len = read(fd3, text, sizeof(text)); | 98 len = ki_read(fd3, text, sizeof(text)); |
| 102 if (len > -0) text[len] = 0; | 99 if (len > -0) text[len] = 0; |
| 103 EXPECT_EQ(5, len); | 100 EXPECT_EQ(5, len); |
| 104 EXPECT_STREQ("HELLO", text); | 101 EXPECT_STREQ("HELLO", text); |
| 105 EXPECT_EQ(0, close(fd1)); | 102 EXPECT_EQ(0, ki_close(fd1)); |
| 106 EXPECT_EQ(0, close(fd2)); | 103 EXPECT_EQ(0, ki_close(fd2)); |
| 107 | 104 |
| 108 fd1 = open("/foo/bar", O_WRONLY | O_APPEND); | 105 fd1 = ki_open("/foo/bar", O_WRONLY | O_APPEND); |
| 109 EXPECT_NE(-1, fd1); | 106 EXPECT_NE(-1, fd1); |
| 110 EXPECT_EQ(5, write(fd1, "WORLD", 5)); | 107 EXPECT_EQ(5, ki_write(fd1, "WORLD", 5)); |
| 111 | 108 |
| 112 len = read(fd3, text, sizeof(text)); | 109 len = ki_read(fd3, text, sizeof(text)); |
| 113 if (len >= 0) text[len] = 0; | 110 if (len >= 0) text[len] = 0; |
| 114 | 111 |
| 115 EXPECT_EQ(5, len); | 112 EXPECT_EQ(5, len); |
| 116 EXPECT_STREQ("WORLD", text); | 113 EXPECT_STREQ("WORLD", text); |
| 117 | 114 |
| 118 fd2 = open("/foo/bar", O_RDONLY); | 115 fd2 = ki_open("/foo/bar", O_RDONLY); |
| 119 EXPECT_NE(-1, fd2); | 116 EXPECT_NE(-1, fd2); |
| 120 len = read(fd2, text, sizeof(text)); | 117 len = ki_read(fd2, text, sizeof(text)); |
| 121 if (len > 0) text[len] = 0; | 118 if (len > 0) text[len] = 0; |
| 122 EXPECT_EQ(10, len); | 119 EXPECT_EQ(10, len); |
| 123 EXPECT_STREQ("HELLOWORLD", text); | 120 EXPECT_STREQ("HELLOWORLD", text); |
| 124 } | 121 } |
| 125 | 122 |
| 126 StringMap_t g_StringMap; | 123 StringMap_t g_StringMap; |
| 127 | 124 |
| 128 class MountMockInit : public MountFactory<MountMockInit, MountMem> { | 125 class MountMockInit : public MountFactory<MountMockInit, MountMem> { |
| 129 public: | 126 public: |
| 130 bool Init(int dev, StringMap_t& args) { | 127 bool Init(int dev, StringMap_t& args) { |
| 131 g_StringMap = args; | 128 g_StringMap = args; |
| 132 if (args.find("false") != args.end()) | 129 if (args.find("false") != args.end()) |
| 133 return false; | 130 return false; |
| 134 return true; | 131 return true; |
| 135 }; | 132 }; |
| 136 }; | 133 }; |
| 137 | 134 |
| 138 class KernelProxyMountMock : public KernelProxy { | 135 class KernelProxyMountMock : public KernelProxy { |
| 139 void Init() { | 136 void Init() { |
| 140 KernelProxy::Init(); | 137 KernelProxy::Init(); |
| 141 factories_["initfs"] = MountMockInit::Create; | 138 factories_["initfs"] = MountMockInit::Create; |
| 142 } | 139 } |
| 143 }; | 140 }; |
| 144 | 141 |
| 145 TEST(KernelProxy, MountInit) { | 142 TEST(KernelProxy, MountInit) { |
| 146 ki_init(new KernelProxyMountMock()); | 143 ki_init(new KernelProxyMountMock()); |
| 147 int res1 = mount("/", "/mnt1", "initfs", 0, "false,foo=bar"); | 144 int res1 = ki_mount("/", "/mnt1", "initfs", 0, "false,foo=bar"); |
| 148 | 145 |
| 149 EXPECT_EQ("bar", g_StringMap["foo"]); | 146 EXPECT_EQ("bar", g_StringMap["foo"]); |
| 150 EXPECT_EQ(-1, res1); | 147 EXPECT_EQ(-1, res1); |
| 151 EXPECT_EQ(EINVAL, errno); | 148 EXPECT_EQ(EINVAL, errno); |
| 152 | 149 |
| 153 int res2 = mount("/", "/mnt2", "initfs", 0, "true,bar=foo,x=y"); | 150 int res2 = ki_mount("/", "/mnt2", "initfs", 0, "true,bar=foo,x=y"); |
| 154 EXPECT_NE(-1, res2); | 151 EXPECT_NE(-1, res2); |
| 155 EXPECT_EQ("y", g_StringMap["x"]); | 152 EXPECT_EQ("y", g_StringMap["x"]); |
| 156 } | 153 } |
| OLD | NEW |