| OLD | NEW |
| (Empty) |
| 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 | |
| 3 * found in the LICENSE file. | |
| 4 */ | |
| 5 | |
| 6 #include <fcntl.h> | |
| 7 #include <pthread.h> | |
| 8 | |
| 9 #include <map> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "nacl_mounts/kernel_intercept.h" | |
| 13 #include "nacl_mounts/kernel_proxy.h" | |
| 14 #include "nacl_mounts/path.h" | |
| 15 | |
| 16 #include "gtest/gtest.h" | |
| 17 | |
| 18 class KernelProxyMock : public KernelProxy { | |
| 19 public: | |
| 20 KernelProxyMock() {} | |
| 21 virtual ~KernelProxyMock() {} | |
| 22 | |
| 23 virtual int chdir(const char* path) { | |
| 24 events.push_back("chdir"); | |
| 25 return 0; | |
| 26 } | |
| 27 virtual char* getcwd(char* buf, size_t size) { | |
| 28 events.push_back("getcwd"); | |
| 29 return buf; | |
| 30 } | |
| 31 virtual char* getwd(char* buf) { | |
| 32 events.push_back("getwd"); | |
| 33 return buf; | |
| 34 } | |
| 35 virtual int dup(int oldfd) { | |
| 36 events.push_back("dup"); | |
| 37 return oldfd; | |
| 38 } | |
| 39 virtual int chmod(const char *path, mode_t mode) { | |
| 40 events.push_back("chmod"); | |
| 41 return 0; | |
| 42 } | |
| 43 virtual int stat(const char *path, struct stat *buf) { | |
| 44 events.push_back("stat"); | |
| 45 return 0; | |
| 46 } | |
| 47 virtual int mkdir(const char *path, mode_t mode) { | |
| 48 events.push_back("mkdir"); | |
| 49 return 0; | |
| 50 } | |
| 51 virtual int rmdir(const char *path) { | |
| 52 events.push_back("rmdir"); | |
| 53 return 0; | |
| 54 } | |
| 55 virtual int mount(const char *source, const char *target, | |
| 56 const char *filesystemtype, unsigned long mountflags, const void *data) { | |
| 57 events.push_back("mount"); | |
| 58 return 0; | |
| 59 } | |
| 60 virtual int umount(const char *path) { | |
| 61 events.push_back("umount"); | |
| 62 return 0; | |
| 63 } | |
| 64 virtual int open(const char *path, int oflag) { | |
| 65 events.push_back("open"); | |
| 66 return 0; | |
| 67 } | |
| 68 virtual ssize_t read(int fd, void *buf, size_t nbyte) { | |
| 69 events.push_back("read"); | |
| 70 return 0; | |
| 71 } | |
| 72 virtual ssize_t write(int fd, const void *buf, size_t nbyte) { | |
| 73 events.push_back("write"); | |
| 74 return 0; | |
| 75 } | |
| 76 virtual int fstat(int fd, struct stat *buf) { | |
| 77 events.push_back("fstat"); | |
| 78 return 0; | |
| 79 } | |
| 80 virtual int getdents(int fd, void *buf, unsigned int count) { | |
| 81 events.push_back("getdents"); | |
| 82 return 0; | |
| 83 } | |
| 84 virtual int fsync(int fd) { | |
| 85 events.push_back("fsync"); | |
| 86 return 0; | |
| 87 } | |
| 88 virtual int isatty(int fd) { | |
| 89 events.push_back("isatty"); | |
| 90 return 0; | |
| 91 } | |
| 92 virtual int close(int fd) { | |
| 93 events.push_back("close"); | |
| 94 return 0; | |
| 95 } | |
| 96 virtual off_t lseek(int fd, off_t offset, int whence) { | |
| 97 events.push_back("lseek"); | |
| 98 return 0; | |
| 99 } | |
| 100 virtual int remove(const char* path) { | |
| 101 events.push_back("remove"); | |
| 102 return 0; | |
| 103 } | |
| 104 virtual int unlink(const char* path) { | |
| 105 events.push_back("unlink"); | |
| 106 return 0; | |
| 107 } | |
| 108 virtual int access(const char* path, int amode) { | |
| 109 events.push_back("access"); | |
| 110 return 0; | |
| 111 } | |
| 112 | |
| 113 std::string& LastStr() { | |
| 114 return events.back(); | |
| 115 } | |
| 116 | |
| 117 std::vector<std::string> events; | |
| 118 }; | |
| 119 | |
| 120 | |
| 121 TEST(KernelIntercept, SanityChecks) { | |
| 122 static KernelProxyMock* mock = new KernelProxyMock(); | |
| 123 ki_init(mock); | |
| 124 | |
| 125 ki_chdir("foo"); | |
| 126 EXPECT_EQ("chdir", mock->LastStr()); | |
| 127 | |
| 128 char getcwd_buffer[] = "foo"; | |
| 129 ki_getcwd(getcwd_buffer, 1); | |
| 130 EXPECT_EQ("getcwd", mock->LastStr()); | |
| 131 | |
| 132 char getwd_buffer[] = "foo"; | |
| 133 ki_getwd(getwd_buffer); | |
| 134 EXPECT_EQ("getwd", mock->LastStr()); | |
| 135 | |
| 136 ki_dup(1); | |
| 137 EXPECT_EQ("dup", mock->LastStr()); | |
| 138 | |
| 139 ki_chmod("foo", 0); | |
| 140 EXPECT_EQ("chmod", mock->LastStr()); | |
| 141 | |
| 142 ki_stat("foo", NULL); | |
| 143 EXPECT_EQ("stat", mock->LastStr()); | |
| 144 | |
| 145 ki_mkdir("foo", 0); | |
| 146 EXPECT_EQ("mkdir", mock->LastStr()); | |
| 147 | |
| 148 ki_rmdir("foo"); | |
| 149 EXPECT_EQ("rmdir", mock->LastStr()); | |
| 150 | |
| 151 ki_mount("foo", "bar", NULL, 0, NULL); | |
| 152 EXPECT_EQ("mount", mock->LastStr()); | |
| 153 | |
| 154 ki_umount("foo"); | |
| 155 EXPECT_EQ("umount", mock->LastStr()); | |
| 156 | |
| 157 ki_open("foo", 0); | |
| 158 EXPECT_EQ("open", mock->LastStr()); | |
| 159 | |
| 160 ki_read(1, NULL, 0); | |
| 161 EXPECT_EQ("read", mock->LastStr()); | |
| 162 | |
| 163 ki_write(1, NULL, 0); | |
| 164 EXPECT_EQ("write", mock->LastStr()); | |
| 165 | |
| 166 ki_fstat(1, NULL); | |
| 167 EXPECT_EQ("fstat", mock->LastStr()); | |
| 168 | |
| 169 ki_getdents(1, NULL, 0); | |
| 170 EXPECT_EQ("getdents", mock->LastStr()); | |
| 171 | |
| 172 ki_fsync(1); | |
| 173 EXPECT_EQ("fsync", mock->LastStr()); | |
| 174 | |
| 175 ki_isatty(1); | |
| 176 EXPECT_EQ("isatty", mock->LastStr()); | |
| 177 | |
| 178 ki_close(1); | |
| 179 EXPECT_EQ("close", mock->LastStr()); | |
| 180 | |
| 181 ki_remove("foo"); | |
| 182 EXPECT_EQ("fsync", mock->LastStr()); | |
| 183 | |
| 184 ki_unlink("foo"); | |
| 185 EXPECT_EQ("isatty", mock->LastStr()); | |
| 186 | |
| 187 ki_access("foo", 0); | |
| 188 EXPECT_EQ("close", mock->LastStr()); | |
| 189 } | |
| OLD | NEW |