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 <string> | |
7 #include <vector> | |
8 #include "gtest/gtest.h" | |
9 #include "nacl_mounts/kernel_proxy.h" | |
10 #include "nacl_mounts/kernel_intercept.h" | |
11 #include "nacl_mounts/kernel_wrap.h" | |
12 #include "kernel_proxy_mock.h" | |
13 | |
14 using ::testing::StrEq; | |
15 using ::testing::_; | |
16 | |
17 namespace { | |
18 | |
19 #define COMPARE_FIELD(f) \ | |
20 if (arg->f != statbuf->f) { \ | |
21 *result_listener << "mismatch of field \""#f"\". " \ | |
22 "expected: " << statbuf->f << \ | |
23 " actual: " << arg->f; \ | |
24 return false; \ | |
25 } | |
26 | |
27 MATCHER_P(IsEqualToStatbuf, statbuf, "") { | |
28 COMPARE_FIELD(st_dev); | |
29 COMPARE_FIELD(st_ino); | |
30 COMPARE_FIELD(st_mode); | |
31 COMPARE_FIELD(st_nlink); | |
32 COMPARE_FIELD(st_uid); | |
33 COMPARE_FIELD(st_gid); | |
34 COMPARE_FIELD(st_rdev); | |
35 COMPARE_FIELD(st_size); | |
36 COMPARE_FIELD(st_atime); | |
37 COMPARE_FIELD(st_mtime); | |
38 COMPARE_FIELD(st_ctime); | |
39 return true; | |
40 } | |
41 | |
42 #undef COMPARE_FIELD | |
43 | |
44 ACTION_P(SetStat, statbuf) { | |
45 memset(arg1, 0, sizeof(struct stat)); | |
46 arg1->st_dev = statbuf->st_dev; | |
47 arg1->st_ino = statbuf->st_ino; | |
48 arg1->st_mode = statbuf->st_mode; | |
49 arg1->st_nlink = statbuf->st_nlink; | |
50 arg1->st_uid = statbuf->st_uid; | |
51 arg1->st_gid = statbuf->st_gid; | |
52 arg1->st_rdev = statbuf->st_rdev; | |
53 arg1->st_size = statbuf->st_size; | |
54 arg1->st_atime = statbuf->st_atime; | |
55 arg1->st_mtime = statbuf->st_mtime; | |
56 arg1->st_ctime = statbuf->st_ctime; | |
57 return 0; | |
58 } | |
59 | |
60 void MakeDummyStatbuf(struct stat* statbuf) { | |
61 memset(&statbuf[0], 0, sizeof(struct stat)); | |
62 statbuf->st_dev = 1; | |
63 statbuf->st_ino = 2; | |
64 statbuf->st_mode = 3; | |
65 statbuf->st_nlink = 4; | |
66 statbuf->st_uid = 5; | |
67 statbuf->st_gid = 6; | |
68 statbuf->st_rdev = 7; | |
69 statbuf->st_size = 8; | |
70 statbuf->st_atime = 9; | |
71 statbuf->st_mtime = 10; | |
72 statbuf->st_ctime = 11; | |
73 } | |
74 | |
75 class KernelWrapTest : public ::testing::Test { | |
76 public: | |
77 KernelWrapTest() { | |
78 ki_init(&mock); | |
79 } | |
80 | |
81 ~KernelWrapTest() { | |
82 ki_uninit(); | |
83 } | |
84 | |
85 KernelProxyMock mock; | |
86 }; | |
87 | |
88 } // namespace | |
89 | |
90 | |
91 TEST_F(KernelWrapTest, access) { | |
92 EXPECT_CALL(mock, access(StrEq("access"), 12)).Times(1); | |
93 access("access", 12); | |
94 } | |
95 | |
96 TEST_F(KernelWrapTest, chdir) { | |
97 EXPECT_CALL(mock, chdir(StrEq("chdir"))).Times(1); | |
98 chdir("chdir"); | |
99 } | |
100 | |
101 TEST_F(KernelWrapTest, chmod) { | |
102 EXPECT_CALL(mock, chmod(StrEq("chmod"), 23)).Times(1); | |
103 chmod("chmod", 23); | |
104 } | |
105 | |
106 TEST_F(KernelWrapTest, close) { | |
107 EXPECT_CALL(mock, close(34)).Times(1); | |
108 close(34); | |
109 } | |
110 | |
111 TEST_F(KernelWrapTest, dup) { | |
112 EXPECT_CALL(mock, dup(123)).Times(1); | |
113 dup(123); | |
114 } | |
115 | |
116 TEST_F(KernelWrapTest, dup2) { | |
117 EXPECT_CALL(mock, dup2(123, 234)).Times(1); | |
118 dup2(123, 234); | |
119 } | |
120 | |
121 TEST_F(KernelWrapTest, fstat) { | |
122 struct stat in_statbuf; | |
123 MakeDummyStatbuf(&in_statbuf); | |
124 EXPECT_CALL(mock, fstat(234, _)) | |
125 .Times(1) | |
126 .WillOnce(SetStat(&in_statbuf)); | |
127 struct stat out_statbuf; | |
128 fstat(234, &out_statbuf); | |
129 EXPECT_THAT(&in_statbuf, IsEqualToStatbuf(&out_statbuf)); | |
130 } | |
131 | |
132 TEST_F(KernelWrapTest, fsync) { | |
133 EXPECT_CALL(mock, fsync(345)).Times(1); | |
134 fsync(345); | |
135 } | |
136 | |
137 TEST_F(KernelWrapTest, getcwd) { | |
138 EXPECT_CALL(mock, getcwd(StrEq("getcwd"), 1)).Times(1); | |
139 char buffer[] = "getcwd"; | |
140 getcwd(buffer, 1); | |
141 } | |
142 | |
143 TEST_F(KernelWrapTest, getdents) { | |
144 EXPECT_CALL(mock, getdents(456, NULL, 567)).Times(1); | |
145 getdents(456, NULL, 567); | |
146 } | |
147 | |
148 // gcc gives error: getwd is deprecated. | |
149 #if defined(__GNUC__) | |
150 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
151 #endif | |
152 TEST_F(KernelWrapTest, getwd) { | |
153 EXPECT_CALL(mock, getwd(StrEq("getwd"))).Times(1); | |
154 char buffer[] = "getwd"; | |
155 getwd(buffer); | |
156 } | |
157 #if defined(__GNUC__) | |
158 #pragma GCC diagnostic warning "-Wdeprecated-declarations" | |
159 #endif | |
160 | |
161 TEST_F(KernelWrapTest, isatty) { | |
162 EXPECT_CALL(mock, isatty(678)).Times(1); | |
163 isatty(678); | |
164 } | |
165 | |
166 TEST_F(KernelWrapTest, lseek) { | |
167 EXPECT_CALL(mock, lseek(789, 891, 912)).Times(1); | |
168 lseek(789, 891, 912); | |
169 } | |
170 | |
171 TEST_F(KernelWrapTest, mkdir) { | |
172 #if defined(WIN32) | |
173 EXPECT_CALL(mock, mkdir(StrEq("mkdir"), 0777)).Times(1); | |
174 mkdir("mkdir"); | |
175 #else | |
176 EXPECT_CALL(mock, mkdir(StrEq("mkdir"), 1234)).Times(1); | |
177 mkdir("mkdir", 1234); | |
178 #endif | |
179 } | |
180 | |
181 TEST_F(KernelWrapTest, mount) { | |
182 EXPECT_CALL(mock, | |
183 mount(StrEq("mount1"), StrEq("mount2"), StrEq("mount3"), 2345, NULL)) | |
184 .Times(1); | |
185 mount("mount1", "mount2", "mount3", 2345, NULL); | |
186 } | |
187 | |
188 TEST_F(KernelWrapTest, open) { | |
189 EXPECT_CALL(mock, open(StrEq("open"), 3456)).Times(1); | |
190 open("open", 3456); | |
191 } | |
192 | |
193 TEST_F(KernelWrapTest, read) { | |
194 EXPECT_CALL(mock, read(4567, NULL, 5678)).Times(1); | |
195 read(4567, NULL, 5678); | |
196 } | |
197 | |
198 TEST_F(KernelWrapTest, remove) { | |
199 EXPECT_CALL(mock, remove(StrEq("remove"))).Times(1); | |
200 remove("remove"); | |
201 } | |
202 | |
203 TEST_F(KernelWrapTest, rmdir) { | |
204 EXPECT_CALL(mock, rmdir(StrEq("rmdir"))).Times(1); | |
205 rmdir("rmdir"); | |
206 } | |
207 | |
208 TEST_F(KernelWrapTest, stat) { | |
209 struct stat in_statbuf; | |
210 MakeDummyStatbuf(&in_statbuf); | |
211 EXPECT_CALL(mock, stat(StrEq("stat"), _)) | |
212 .Times(1) | |
213 .WillOnce(SetStat(&in_statbuf)); | |
214 struct stat out_statbuf; | |
215 stat("stat", &out_statbuf); | |
216 EXPECT_THAT(&in_statbuf, IsEqualToStatbuf(&out_statbuf)); | |
217 } | |
218 | |
219 TEST_F(KernelWrapTest, umount) { | |
220 EXPECT_CALL(mock, umount(StrEq("umount"))).Times(1); | |
221 umount("umount"); | |
222 } | |
223 | |
224 TEST_F(KernelWrapTest, unlink) { | |
225 EXPECT_CALL(mock, unlink(StrEq("unlink"))).Times(1); | |
226 unlink("unlink"); | |
227 } | |
228 | |
229 TEST_F(KernelWrapTest, write) { | |
230 EXPECT_CALL(mock, write(6789, NULL, 7891)).Times(1); | |
231 write(6789, NULL, 7891); | |
232 } | |
OLD | NEW |