1 // Copyright (c) 2013 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.
5 // The linux/mac host build of nacl_io can't do wrapping of syscalls so all
6 // these tests must be disabled.
7 #if !defined(__linux__) && !defined(__APPLE__)
14 #include "gtest/gtest.h"
15 #include "mock_kernel_proxy.h"
16 #include "nacl_io/kernel_intercept.h"
17 #include "nacl_io/kernel_wrap.h"
18 #include "nacl_io/kernel_wrap_real.h"
19 #include "nacl_io/osmman.h"
20 #include "nacl_io/ossocket.h"
21 #include "nacl_io/ostermios.h"
22 #include "nacl_io/ostime.h"
23 #include "ppapi_simple/ps.h"
25 #if defined(__native_client__) && !defined(__GLIBC__)
27 // TODO(sbc): remove once this gets added to the newlib toolchain headers.
32 using namespace nacl_io
;
35 using ::testing::AnyNumber
;
36 using ::testing::DoAll
;
37 using ::testing::Invoke
;
38 using ::testing::Return
;
39 using ::testing::StrEq
;
43 #define COMPARE_FIELD(actual, expected, f) \
44 if (actual != expected) { \
45 *result_listener << "mismatch of field \"" f \
47 "expected: " << expected << " actual: " << actual; \
51 #define COMPARE_FIELD_SIMPLE(f) COMPARE_FIELD(arg->f, other->f, #f)
53 MATCHER_P(IsEqualToStatbuf
, other
, "") {
54 COMPARE_FIELD_SIMPLE(st_dev
);
55 COMPARE_FIELD_SIMPLE(st_ino
);
56 COMPARE_FIELD_SIMPLE(st_mode
);
57 COMPARE_FIELD_SIMPLE(st_nlink
);
58 COMPARE_FIELD_SIMPLE(st_uid
);
59 COMPARE_FIELD_SIMPLE(st_gid
);
60 COMPARE_FIELD_SIMPLE(st_rdev
);
61 COMPARE_FIELD_SIMPLE(st_size
);
62 COMPARE_FIELD_SIMPLE(st_atime
);
63 COMPARE_FIELD_SIMPLE(st_mtime
);
64 COMPARE_FIELD_SIMPLE(st_ctime
);
68 MATCHER_P(IsEqualToUtimbuf
, other
, "") {
69 COMPARE_FIELD(arg
[0].tv_sec
, other
->actime
, "actime");
70 COMPARE_FIELD(arg
[1].tv_sec
, other
->modtime
, "modtime");
74 MATCHER_P(IsEqualToTimeval
, other
, "") {
75 COMPARE_FIELD(arg
[0].tv_sec
, other
[0].tv_sec
, "[0].tv_sec");
76 COMPARE_FIELD(arg
[0].tv_nsec
, other
[0].tv_usec
* 1000, "[0].tv_usec");
77 COMPARE_FIELD(arg
[1].tv_sec
, other
[1].tv_sec
, "[1].tv_sec");
78 COMPARE_FIELD(arg
[1].tv_nsec
, other
[1].tv_usec
* 1000, "[1].tv_usec");
83 #undef COMPARE_FIELD_SIMPLE
86 ACTION_P(SetErrno
, value
) {
90 ACTION_P2(SetString
, target
, source
) {
91 strcpy(target
, source
);
94 ACTION_P(SetStat
, statbuf
) {
95 memset(arg1
, 0, sizeof(struct stat
));
96 arg1
->st_dev
= statbuf
->st_dev
;
97 arg1
->st_ino
= statbuf
->st_ino
;
98 arg1
->st_mode
= statbuf
->st_mode
;
99 arg1
->st_nlink
= statbuf
->st_nlink
;
100 arg1
->st_uid
= statbuf
->st_uid
;
101 arg1
->st_gid
= statbuf
->st_gid
;
102 arg1
->st_rdev
= statbuf
->st_rdev
;
103 arg1
->st_size
= statbuf
->st_size
;
104 arg1
->st_atime
= statbuf
->st_atime
;
105 arg1
->st_mtime
= statbuf
->st_mtime
;
106 arg1
->st_ctime
= statbuf
->st_ctime
;
109 void MakeDummyStatbuf(struct stat
* statbuf
) {
110 memset(&statbuf
[0], 0, sizeof(struct stat
));
113 statbuf
->st_mode
= 3;
114 statbuf
->st_nlink
= 4;
117 statbuf
->st_rdev
= 7;
118 statbuf
->st_size
= 8;
119 statbuf
->st_atime
= 9;
120 statbuf
->st_mtime
= 10;
121 statbuf
->st_ctime
= 11;
124 const mode_t kDummyMode
= 0xbeef;
125 const int kDummyErrno
= 0xfeeb;
126 const int kDummyInt
= 0xdedbeef;
127 const int kDummyInt2
= 0xcabba6e;
128 const int kDummyInt3
= 0xf00ba4;
129 const int kDummyInt4
= 0xabacdba;
130 const size_t kDummySizeT
= 0x60067e;
131 const char* kDummyConstChar
= "foobar";
132 const char* kDummyConstChar2
= "g00gl3";
133 const char* kDummyConstChar3
= "fr00gl3";
134 const void* kDummyVoidPtr
= "blahblah";
135 const uid_t kDummyUid
= 1001;
136 const gid_t kDummyGid
= 1002;
138 class KernelWrapTest
: public ::testing::Test
{
142 virtual void SetUp() {
143 // Initialize the global errno value to a consistent value rather than
144 // relying on its value from previous test runs.
147 // Initializing the KernelProxy opens stdin/stdout/stderr.
148 EXPECT_CALL(mock
, open(_
, _
, _
))
151 .WillOnce(Return(2));
153 ASSERT_EQ(0, ki_push_state_for_testing());
154 ASSERT_EQ(0, ki_init(&mock
));
156 // We allow write to be called any number of times, and it forwards to
157 // _real_write. This prevents an infinite loop writing output if there is a
159 ON_CALL(mock
, write(_
, _
, _
))
160 .WillByDefault(Invoke(this, &KernelWrapTest::DefaultWrite
));
161 EXPECT_CALL(mock
, write(_
, _
, _
)).Times(AnyNumber());
163 // Ignore calls to munmap. These can be generated from within the standard
164 // library malloc implementation so can be expected at pretty much any time.
165 // Returning zero is fine since the real munmap see also run.
166 // See kernel_wrap_newlib.cc.
167 ON_CALL(mock
, munmap(_
, _
))
168 .WillByDefault(Return(0));
169 EXPECT_CALL(mock
, munmap(_
, _
)).Times(AnyNumber());
173 // Uninitialize the kernel proxy so wrapped functions passthrough to their
174 // unwrapped versions.
178 MockKernelProxy mock
;
181 ssize_t
DefaultWrite(int fd
, const void* buf
, size_t count
) {
184 int rtn
= _real_write(fd
, buf
, count
, &nwrote
);
195 TEST_F(KernelWrapTest
, access
) {
196 EXPECT_CALL(mock
, access(kDummyConstChar
, kDummyInt
)) .WillOnce(Return(0));
197 EXPECT_EQ(0, access(kDummyConstChar
, kDummyInt
));
199 EXPECT_CALL(mock
, access(kDummyConstChar
, kDummyInt
))
200 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
201 EXPECT_EQ(-1, access(kDummyConstChar
, kDummyInt
));
202 EXPECT_EQ(kDummyErrno
, errno
);
206 TEST_F(KernelWrapTest
, chdir
) {
207 EXPECT_CALL(mock
, chdir(kDummyConstChar
)).WillOnce(Return(0));
208 EXPECT_EQ(0, chdir(kDummyConstChar
));
210 EXPECT_CALL(mock
, chdir(kDummyConstChar
))
211 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
212 EXPECT_EQ(-1, chdir(kDummyConstChar
));
213 ASSERT_EQ(kDummyErrno
, errno
);
216 TEST_F(KernelWrapTest
, chmod
) {
217 EXPECT_CALL(mock
, chmod(kDummyConstChar
, kDummyMode
))
218 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
219 EXPECT_EQ(-1, chmod(kDummyConstChar
, kDummyMode
));
220 ASSERT_EQ(kDummyErrno
, errno
);
223 TEST_F(KernelWrapTest
, chown
) {
224 EXPECT_CALL(mock
, chown(kDummyConstChar
, kDummyUid
, kDummyGid
))
225 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
226 EXPECT_EQ(-1, chown(kDummyConstChar
, kDummyUid
, kDummyGid
));
227 ASSERT_EQ(kDummyErrno
, errno
);
230 TEST_F(KernelWrapTest
, close
) {
231 // The way we wrap close does not support returning arbitrary values, so we
233 EXPECT_CALL(mock
, close(kDummyInt
))
234 .WillOnce(Return(0));
236 EXPECT_EQ(0, close(kDummyInt
));
238 EXPECT_CALL(mock
, close(kDummyInt
))
239 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
240 EXPECT_EQ(-1, close(kDummyInt
));
241 ASSERT_EQ(kDummyErrno
, errno
);
244 TEST_F(KernelWrapTest
, dup
) {
245 EXPECT_CALL(mock
, dup(kDummyInt
)).WillOnce(Return(kDummyInt2
));
246 EXPECT_EQ(kDummyInt2
, dup(kDummyInt
));
249 TEST_F(KernelWrapTest
, dup2
) {
250 // The way we wrap dup2 does not support returning aribtrary values, only -1
251 // or the value of the new fd.
252 EXPECT_CALL(mock
, dup2(kDummyInt
, kDummyInt2
))
253 .WillOnce(Return(kDummyInt2
))
254 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
256 EXPECT_EQ(kDummyInt2
, dup2(kDummyInt
, kDummyInt2
));
257 EXPECT_EQ(-1, dup2(kDummyInt
, kDummyInt2
));
258 ASSERT_EQ(kDummyErrno
, errno
);
261 TEST_F(KernelWrapTest
, fchdir
) {
262 EXPECT_CALL(mock
, fchdir(kDummyInt
))
263 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
265 EXPECT_EQ(-1, fchdir(kDummyInt
));
266 ASSERT_EQ(kDummyErrno
, errno
);
269 TEST_F(KernelWrapTest
, fchmod
) {
270 EXPECT_CALL(mock
, fchmod(kDummyInt
, kDummyMode
))
272 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
274 EXPECT_EQ(0, fchmod(kDummyInt
, kDummyMode
));
275 EXPECT_EQ(-1, fchmod(kDummyInt
, kDummyMode
));
276 ASSERT_EQ(kDummyErrno
, errno
);
279 TEST_F(KernelWrapTest
, fchown
) {
280 EXPECT_CALL(mock
, fchown(kDummyInt
, kDummyUid
, kDummyGid
))
281 .WillOnce(Return(kDummyInt
));
282 EXPECT_EQ(kDummyInt
, fchown(kDummyInt
, kDummyUid
, kDummyGid
));
285 TEST_F(KernelWrapTest
, fcntl
) {
286 char buffer
[] = "fcntl";
287 EXPECT_CALL(mock
, fcntl(kDummyInt
, kDummyInt2
, _
))
288 .WillOnce(Return(kDummyInt3
));
289 EXPECT_EQ(kDummyInt3
, fcntl(kDummyInt
, kDummyInt2
, buffer
));
292 TEST_F(KernelWrapTest
, fdatasync
) {
293 EXPECT_CALL(mock
, fdatasync(kDummyInt
)).WillOnce(Return(0))
294 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
296 EXPECT_EQ(0, fdatasync(kDummyInt
));
297 EXPECT_EQ(-1, fdatasync(kDummyInt
));
298 ASSERT_EQ(kDummyErrno
, errno
);
301 TEST_F(KernelWrapTest
, fstat
) {
302 // The way we wrap fstat does not support returning aribtrary values, only 0
304 struct stat in_statbuf
;
305 MakeDummyStatbuf(&in_statbuf
);
306 EXPECT_CALL(mock
, fstat(kDummyInt
, _
))
307 .WillOnce(DoAll(SetStat(&in_statbuf
), Return(0)))
308 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
309 struct stat out_statbuf
;
311 EXPECT_EQ(0, fstat(kDummyInt
, &out_statbuf
));
312 EXPECT_THAT(&in_statbuf
, IsEqualToStatbuf(&out_statbuf
));
314 EXPECT_EQ(-1, fstat(kDummyInt
, &out_statbuf
));
315 ASSERT_EQ(kDummyErrno
, errno
);
318 TEST_F(KernelWrapTest
, ftruncate
) {
319 EXPECT_CALL(mock
, ftruncate(kDummyInt
, kDummyInt2
))
320 .WillOnce(Return(kDummyInt3
));
321 EXPECT_EQ(kDummyInt3
, ftruncate(kDummyInt
, kDummyInt2
));
324 TEST_F(KernelWrapTest
, fsync
) {
325 EXPECT_CALL(mock
, fsync(kDummyInt
))
326 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
327 EXPECT_EQ(-1, fsync(kDummyInt
));
328 ASSERT_EQ(kDummyErrno
, errno
);
331 TEST_F(KernelWrapTest
, futimes
) {
332 struct timeval times
[2] = {{123, 234}, {345, 456}};
333 EXPECT_CALL(mock
, futimens(kDummyInt
, IsEqualToTimeval(times
)))
334 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
335 EXPECT_EQ(-1, futimes(kDummyInt
, times
));
336 ASSERT_EQ(kDummyErrno
, errno
);
339 TEST_F(KernelWrapTest
, getcwd
) {
340 char buffer
[PATH_MAX
];
341 char result
[PATH_MAX
];
342 memset(buffer
, 0, PATH_MAX
);
343 strcpy(result
, "getcwd_result");
344 EXPECT_CALL(mock
, getcwd(buffer
, kDummySizeT
))
345 .WillOnce(DoAll(SetString(buffer
, result
), Return(buffer
)));
346 EXPECT_STREQ(result
, getcwd(buffer
, kDummySizeT
));
349 TEST_F(KernelWrapTest
, getdents
) {
350 #if !defined( __GLIBC__) && !defined(__BIONIC__)
351 // TODO(sbc): Find a way to test the getdents wrapper under glibc.
352 // It looks like the only way to exercise it is to call readdir(2).
353 // There is an internal glibc function __getdents that will call the
354 // IRT but that cannot be accessed from here as glibc does not export it.
355 struct dirent dirent
;
356 EXPECT_CALL(mock
, getdents(kDummyInt
, &dirent
, kDummyInt2
))
357 .WillOnce(Return(kDummyInt2
));
358 EXPECT_EQ(kDummyInt2
, getdents(kDummyInt
, &dirent
, kDummyInt2
));
362 // gcc gives error: getwd is deprecated.
363 #if defined(__GNUC__)
364 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
366 TEST_F(KernelWrapTest
, getwd
) {
367 char result
[] = "getwd_result";
368 char buffer
[] = "getwd";
369 EXPECT_CALL(mock
, getwd(buffer
)).WillOnce(Return(result
));
370 EXPECT_EQ(result
, getwd(buffer
));
372 #if defined(__GNUC__)
373 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
376 TEST_F(KernelWrapTest
, ioctl
) {
377 char buffer
[] = "ioctl";
378 EXPECT_CALL(mock
, ioctl(kDummyInt
, kDummyInt2
, _
))
379 .WillOnce(Return(kDummyInt3
));
380 EXPECT_EQ(kDummyInt3
, ioctl(kDummyInt
, kDummyInt2
, buffer
));
383 #if !defined(__BIONIC__)
384 TEST_F(KernelWrapTest
, isatty
) {
385 EXPECT_CALL(mock
, isatty(kDummyInt
)).WillOnce(Return(kDummyInt2
));
386 EXPECT_EQ(kDummyInt2
, isatty(kDummyInt
));
388 // This test verifies that the IRT interception wrapper for isatty
389 // ignores the value of errno when isatty() returns 1. We had a bug
390 // where returning 1 from ki_isatty resulted in errno being returned
391 // by the IRT interface.
393 EXPECT_CALL(mock
, isatty(kDummyInt
)).WillOnce(Return(1));
394 EXPECT_EQ(1, isatty(kDummyInt
));
398 TEST_F(KernelWrapTest
, kill
) {
399 EXPECT_CALL(mock
, kill(kDummyInt
, kDummyInt2
)).WillOnce(Return(kDummyInt3
));
400 EXPECT_EQ(kDummyInt3
, kill(kDummyInt
, kDummyInt2
));
403 TEST_F(KernelWrapTest
, lchown
) {
404 EXPECT_CALL(mock
, lchown(kDummyConstChar
, kDummyUid
, kDummyGid
))
405 .WillOnce(Return(kDummyInt
));
406 EXPECT_EQ(kDummyInt
, lchown(kDummyConstChar
, kDummyUid
, kDummyGid
));
409 TEST_F(KernelWrapTest
, link
) {
410 EXPECT_CALL(mock
, link(kDummyConstChar
, kDummyConstChar2
))
411 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
412 EXPECT_EQ(-1, link(kDummyConstChar
, kDummyConstChar2
));
413 ASSERT_EQ(kDummyErrno
, errno
);
416 TEST_F(KernelWrapTest
, lseek
) {
417 EXPECT_CALL(mock
, lseek(kDummyInt
, kDummyInt2
, kDummyInt3
))
418 .WillOnce(Return(kDummyInt4
));
419 EXPECT_EQ(kDummyInt4
, lseek(kDummyInt
, kDummyInt2
, kDummyInt3
));
422 TEST_F(KernelWrapTest
, mkdir
) {
424 EXPECT_CALL(mock
, mkdir(kDummyConstChar
, 0777)).WillOnce(Return(kDummyInt2
));
425 EXPECT_EQ(kDummyInt2
, mkdir(kDummyConstChar
));
427 EXPECT_CALL(mock
, mkdir(kDummyConstChar
, kDummyMode
))
428 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
429 EXPECT_EQ(-1, mkdir(kDummyConstChar
, kDummyMode
));
430 ASSERT_EQ(kDummyErrno
, errno
);
434 TEST_F(KernelWrapTest
, mmap
) {
435 // We only wrap mmap if |flags| has the MAP_ANONYMOUS bit unset.
436 int flags
= kDummyInt2
& ~MAP_ANONYMOUS
;
438 const size_t kDummySizeT2
= 0xbadf00d;
441 void* kDummyVoidPtr1
= &dummy1
;
442 void* kDummyVoidPtr2
= &dummy2
;
449 kDummySizeT2
)).WillOnce(Return(kDummyVoidPtr2
));
450 EXPECT_EQ(kDummyVoidPtr2
,
459 TEST_F(KernelWrapTest
, mount
) {
461 mount(kDummyConstChar
,
465 kDummyVoidPtr
)).WillOnce(Return(kDummyInt2
));
466 EXPECT_EQ(kDummyInt2
,
467 mount(kDummyConstChar
,
474 TEST_F(KernelWrapTest
, munmap
) {
475 // The way we wrap munmap, calls the "real" mmap as well as the intercepted
476 // one. The result returned is from the "real" mmap.
478 void* kDummyVoidPtr
= &dummy1
;
479 size_t kDummySizeT
= sizeof(kDummyVoidPtr
);
480 EXPECT_CALL(mock
, munmap(kDummyVoidPtr
, kDummySizeT
));
481 munmap(kDummyVoidPtr
, kDummySizeT
);
484 TEST_F(KernelWrapTest
, open
) {
485 // We pass O_RDONLY because we do not want an error in flags translation
486 EXPECT_CALL(mock
, open(kDummyConstChar
, 0, 0))
487 .WillOnce(Return(kDummyInt2
))
488 .WillOnce(Return(kDummyInt2
));
490 EXPECT_EQ(kDummyInt2
, open(kDummyConstChar
, 0, 0));
491 EXPECT_EQ(kDummyInt2
, open(kDummyConstChar
, 0, 0));
494 TEST_F(KernelWrapTest
, pipe
) {
496 EXPECT_CALL(mock
, pipe(fds
)).WillOnce(Return(kDummyInt
));
497 EXPECT_EQ(kDummyInt
, pipe(fds
));
500 TEST_F(KernelWrapTest
, read
) {
502 void* dummy_void_ptr
= &dummy_value
;
503 EXPECT_CALL(mock
, read(kDummyInt
, dummy_void_ptr
, kDummyInt2
))
504 .WillOnce(Return(kDummyInt3
));
505 EXPECT_EQ(kDummyInt3
, read(kDummyInt
, dummy_void_ptr
, kDummyInt2
));
508 TEST_F(KernelWrapTest
, readlink
) {
511 EXPECT_CALL(mock
, readlink(kDummyConstChar
, buf
, 10))
512 .WillOnce(Return(kDummyInt
))
513 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
515 EXPECT_EQ(kDummyInt
, readlink(kDummyConstChar
, buf
, 10));
516 EXPECT_EQ(-1, readlink(kDummyConstChar
, buf
, 10));
517 ASSERT_EQ(kDummyErrno
, errno
);
520 TEST_F(KernelWrapTest
, remove
) {
521 // The remove syscall is not directly intercepted. Instead it is implemented
522 // in terms of unlink()/rmdir().
523 EXPECT_CALL(mock
, unlink(kDummyConstChar
))
524 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
525 EXPECT_EQ(-1, remove(kDummyConstChar
));
528 TEST_F(KernelWrapTest
, rename
) {
529 EXPECT_CALL(mock
, rename(kDummyConstChar
, kDummyConstChar2
))
531 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
533 EXPECT_EQ(0, rename(kDummyConstChar
, kDummyConstChar2
));
534 EXPECT_EQ(-1, rename(kDummyConstChar
, kDummyConstChar2
));
535 ASSERT_EQ(kDummyErrno
, errno
);
538 TEST_F(KernelWrapTest
, rmdir
) {
539 EXPECT_CALL(mock
, rmdir(kDummyConstChar
))
540 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
541 EXPECT_EQ(-1, rmdir(kDummyConstChar
));
542 ASSERT_EQ(kDummyErrno
, errno
);
545 static void new_handler(int) {}
547 TEST_F(KernelWrapTest
, sigaction
) {
548 struct sigaction action
;
549 struct sigaction oaction
;
550 EXPECT_CALL(mock
, sigaction(kDummyInt
, &action
, &oaction
))
551 .WillOnce(Return(0));
552 EXPECT_EQ(0, sigaction(kDummyInt
, &action
, &oaction
));
555 TEST_F(KernelWrapTest
, sigset
) {
556 EXPECT_CALL(mock
, sigaction(kDummyInt
, _
, _
))
557 .WillOnce(Return(0));
558 EXPECT_EQ(NULL
, sigset(kDummyInt
, new_handler
));
561 TEST_F(KernelWrapTest
, signal
) {
562 // KernelIntercept forwards calls to signal to KernelProxy::sigset.
563 EXPECT_CALL(mock
, sigaction(kDummyInt
, _
, _
))
564 .WillOnce(Return(0));
565 EXPECT_EQ(NULL
, signal(kDummyInt
, new_handler
));
568 TEST_F(KernelWrapTest
, stat
) {
569 // The way we wrap stat does not support returning aribtrary values, only 0
571 struct stat in_statbuf
;
572 MakeDummyStatbuf(&in_statbuf
);
573 EXPECT_CALL(mock
, stat(StrEq(kDummyConstChar
), _
))
574 .WillOnce(DoAll(SetStat(&in_statbuf
), Return(0)))
575 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
576 struct stat out_statbuf
;
578 EXPECT_EQ(0, stat(kDummyConstChar
, &out_statbuf
));
579 EXPECT_THAT(&in_statbuf
, IsEqualToStatbuf(&out_statbuf
));
581 EXPECT_EQ(-1, stat(kDummyConstChar
, &out_statbuf
));
582 ASSERT_EQ(kDummyErrno
, errno
);
585 TEST_F(KernelWrapTest
, symlink
) {
586 EXPECT_CALL(mock
, symlink(kDummyConstChar
, kDummyConstChar2
))
587 .WillOnce(Return(kDummyInt
));
588 EXPECT_EQ(kDummyInt
, symlink(kDummyConstChar
, kDummyConstChar2
));
592 TEST_F(KernelWrapTest
, tcflush
) {
593 EXPECT_CALL(mock
, tcflush(kDummyInt
, kDummyInt2
))
594 .WillOnce(Return(kDummyInt3
));
595 EXPECT_EQ(kDummyInt3
, tcflush(kDummyInt
, kDummyInt2
));
598 TEST_F(KernelWrapTest
, tcgetattr
) {
600 EXPECT_CALL(mock
, tcgetattr(kDummyInt
, &term
)).WillOnce(Return(kDummyInt2
));
601 EXPECT_EQ(kDummyInt2
, tcgetattr(kDummyInt
, &term
));
604 TEST_F(KernelWrapTest
, tcsetattr
) {
606 EXPECT_CALL(mock
, tcsetattr(kDummyInt
, kDummyInt2
, &term
))
607 .WillOnce(Return(kDummyInt3
));
608 EXPECT_EQ(kDummyInt3
, tcsetattr(kDummyInt
, kDummyInt2
, &term
));
612 TEST_F(KernelWrapTest
, umount
) {
613 EXPECT_CALL(mock
, umount(kDummyConstChar
)).WillOnce(Return(kDummyInt
));
614 EXPECT_EQ(kDummyInt
, umount(kDummyConstChar
));
617 TEST_F(KernelWrapTest
, truncate
) {
618 EXPECT_CALL(mock
, truncate(kDummyConstChar
, kDummyInt3
))
620 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
622 EXPECT_EQ(0, truncate(kDummyConstChar
, kDummyInt3
));
624 EXPECT_EQ(-1, truncate(kDummyConstChar
, kDummyInt3
));
627 TEST_F(KernelWrapTest
, lstat
) {
628 struct stat in_statbuf
;
629 MakeDummyStatbuf(&in_statbuf
);
630 EXPECT_CALL(mock
, lstat(StrEq(kDummyConstChar
), _
))
631 .WillOnce(DoAll(SetStat(&in_statbuf
), Return(0)))
632 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
633 struct stat out_statbuf
;
635 EXPECT_EQ(0, lstat(kDummyConstChar
, &out_statbuf
));
636 EXPECT_THAT(&in_statbuf
, IsEqualToStatbuf(&out_statbuf
));
638 EXPECT_EQ(-1, lstat(kDummyConstChar
, &out_statbuf
));
639 ASSERT_EQ(kDummyErrno
, errno
);
642 TEST_F(KernelWrapTest
, unlink
) {
643 EXPECT_CALL(mock
, unlink(kDummyConstChar
))
644 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
645 EXPECT_EQ(-1, unlink(kDummyConstChar
));
646 ASSERT_EQ(kDummyErrno
, errno
);
649 TEST_F(KernelWrapTest
, utime
) {
650 const struct utimbuf times
= {123, 456};
651 EXPECT_CALL(mock
, utimens(kDummyConstChar
, IsEqualToUtimbuf(×
)))
652 .WillOnce(Return(kDummyInt
));
653 EXPECT_EQ(kDummyInt
, utime(kDummyConstChar
, ×
));
656 TEST_F(KernelWrapTest
, utimes
) {
657 struct timeval times
[2] = {{123, 234}, {345, 456}};
658 EXPECT_CALL(mock
, utimens(kDummyConstChar
, IsEqualToTimeval(times
)))
659 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
660 EXPECT_EQ(-1, utimes(kDummyConstChar
, times
));
661 ASSERT_EQ(kDummyErrno
, errno
);
664 TEST_F(KernelWrapTest
, write
) {
665 EXPECT_CALL(mock
, write(kDummyInt
, kDummyVoidPtr
, kDummyInt2
))
666 .WillOnce(Return(kDummyInt3
));
667 EXPECT_EQ(kDummyInt3
, write(kDummyInt
, kDummyVoidPtr
, kDummyInt2
));
670 class KernelWrapTestUninit
: public ::testing::Test
{
672 ASSERT_EQ(0, ki_push_state_for_testing());
673 kernel_wrap_uninit();
678 ki_pop_state_for_testing();
682 TEST_F(KernelWrapTestUninit
, Mkdir_Uninitialised
) {
683 // If we are running within chrome we can't use these calls without
684 // nacl_io initialized.
685 if (PSGetInstanceId() != 0)
687 EXPECT_EQ(0, mkdir("./foo", S_IREAD
| S_IWRITE
));
688 EXPECT_EQ(0, rmdir("./foo"));
691 TEST_F(KernelWrapTestUninit
, Getcwd_Uninitialised
) {
692 // If we are running within chrome we can't use these calls without
693 // nacl_io initialized.
694 if (PSGetInstanceId() != 0)
697 ASSERT_NE((char*)NULL
, getcwd(dir
, PATH_MAX
));
698 // Verify that the CWD ends with 'nacl_io_test'
699 const char* suffix
= "nacl_io_test";
700 ASSERT_GT(strlen(dir
), strlen(suffix
));
701 ASSERT_EQ(0, strcmp(dir
+strlen(dir
)-strlen(suffix
), suffix
));
704 #if defined(PROVIDES_SOCKET_API) and !defined(__BIONIC__)
705 TEST_F(KernelWrapTest
, poll
) {
707 EXPECT_CALL(mock
, poll(&fds
, kDummyInt
, kDummyInt2
))
708 .WillOnce(Return(kDummyInt3
));
709 EXPECT_EQ(kDummyInt3
, poll(&fds
, kDummyInt
, kDummyInt2
));
712 TEST_F(KernelWrapTest
, select
) {
716 EXPECT_CALL(mock
, select(kDummyInt
, &readfds
, &writefds
, &exceptfds
, NULL
))
717 .WillOnce(Return(kDummyInt2
));
718 EXPECT_EQ(kDummyInt2
,
719 select(kDummyInt
, &readfds
, &writefds
, &exceptfds
, NULL
));
723 TEST_F(KernelWrapTest
, accept
) {
724 struct sockaddr addr
;
726 EXPECT_CALL(mock
, accept(kDummyInt
, &addr
, &len
))
727 .WillOnce(Return(kDummyInt2
));
728 EXPECT_EQ(kDummyInt2
, accept(kDummyInt
, &addr
, &len
));
731 TEST_F(KernelWrapTest
, bind
) {
732 // The way we wrap bind does not support returning arbitrary values, so we
734 struct sockaddr addr
;
735 EXPECT_CALL(mock
, bind(kDummyInt
, &addr
, kDummyInt2
))
737 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
738 EXPECT_EQ(0, bind(kDummyInt
, &addr
, kDummyInt2
));
739 EXPECT_EQ(-1, bind(kDummyInt
, &addr
, kDummyInt2
));
740 EXPECT_EQ(kDummyErrno
, errno
);
743 TEST_F(KernelWrapTest
, connect
) {
744 // The way we wrap connect does not support returning arbitrary values, so we
746 struct sockaddr addr
;
747 EXPECT_CALL(mock
, connect(kDummyInt
, &addr
, kDummyInt2
))
749 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
750 EXPECT_EQ(0, connect(kDummyInt
, &addr
, kDummyInt2
));
751 EXPECT_EQ(-1, connect(kDummyInt
, &addr
, kDummyInt2
));
752 EXPECT_EQ(kDummyErrno
, errno
);
755 TEST_F(KernelWrapTest
, gethostbyname
) {
756 struct hostent result
;
757 EXPECT_CALL(mock
, gethostbyname(kDummyConstChar
)).WillOnce(Return(&result
));
758 EXPECT_EQ(&result
, gethostbyname(kDummyConstChar
));
761 TEST_F(KernelWrapTest
, getpeername
) {
762 // The way we wrap getpeername does not support returning arbitrary values,
763 // so we test 0 and -1.
764 struct sockaddr addr
;
766 EXPECT_CALL(mock
, getpeername(kDummyInt
, &addr
, &len
))
768 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
769 EXPECT_EQ(0, getpeername(kDummyInt
, &addr
, &len
));
770 EXPECT_EQ(-1, getpeername(kDummyInt
, &addr
, &len
));
771 EXPECT_EQ(kDummyErrno
, errno
);
774 TEST_F(KernelWrapTest
, getsockname
) {
775 // The way we wrap getsockname does not support returning arbitrary values,
776 // so we test 0 and -1.
777 struct sockaddr addr
;
780 EXPECT_CALL(mock
, getsockname(kDummyInt
, &addr
, &len
))
782 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
783 EXPECT_EQ(0, getsockname(kDummyInt
, &addr
, &len
));
784 EXPECT_EQ(-1, getsockname(kDummyInt
, &addr
, &len
));
785 EXPECT_EQ(kDummyErrno
, errno
);
788 TEST_F(KernelWrapTest
, getsockopt
) {
789 // The way we wrap getsockname does not support returning arbitrary values,
790 // so we test 0 and -1.
792 void* dummy_void_ptr
= &dummy_val
;
795 mock
, getsockopt(kDummyInt
, kDummyInt2
, kDummyInt3
, dummy_void_ptr
, &len
))
797 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
800 getsockopt(kDummyInt
, kDummyInt2
, kDummyInt3
, dummy_void_ptr
, &len
));
803 getsockopt(kDummyInt
, kDummyInt2
, kDummyInt3
, dummy_void_ptr
, &len
));
804 EXPECT_EQ(kDummyErrno
, errno
);
807 TEST_F(KernelWrapTest
, listen
) {
808 // The way we wrap listen does not support returning arbitrary values, so we
810 EXPECT_CALL(mock
, listen(kDummyInt
, kDummyInt2
))
812 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
813 EXPECT_EQ(0, listen(kDummyInt
, kDummyInt2
));
814 EXPECT_EQ(-1, listen(kDummyInt
, kDummyInt2
));
815 EXPECT_EQ(kDummyErrno
, errno
);
818 TEST_F(KernelWrapTest
, recv
) {
820 void* dummy_void_ptr
= &dummy_val
;
821 EXPECT_CALL(mock
, recv(kDummyInt
, dummy_void_ptr
, kDummySizeT
, kDummyInt2
))
822 .WillOnce(Return(kDummyInt3
));
823 EXPECT_EQ(kDummyInt3
,
824 recv(kDummyInt
, dummy_void_ptr
, kDummySizeT
, kDummyInt2
));
827 TEST_F(KernelWrapTest
, recvfrom
) {
829 void* dummy_void_ptr
= &dummy_val
;
830 struct sockaddr addr
;
834 recvfrom(kDummyInt
, dummy_void_ptr
, kDummyInt2
, kDummyInt3
, &addr
, &len
))
835 .WillOnce(Return(kDummyInt4
));
838 recvfrom(kDummyInt
, dummy_void_ptr
, kDummyInt2
, kDummyInt3
, &addr
, &len
));
842 TEST_F(KernelWrapTest
, recvmsg
) {
844 EXPECT_CALL(mock
, recvmsg(kDummyInt
, &msg
, kDummyInt2
))
845 .WillOnce(Return(kDummyInt3
));
846 EXPECT_EQ(kDummyInt3
, recvmsg(kDummyInt
, &msg
, kDummyInt2
));
850 TEST_F(KernelWrapTest
, send
) {
851 EXPECT_CALL(mock
, send(kDummyInt
, kDummyVoidPtr
, kDummySizeT
, kDummyInt2
))
852 .WillOnce(Return(kDummyInt3
));
853 EXPECT_EQ(kDummyInt3
,
854 send(kDummyInt
, kDummyVoidPtr
, kDummySizeT
, kDummyInt2
));
857 TEST_F(KernelWrapTest
, sendto
) {
858 const socklen_t kDummySockLen
= 0x50cc5;
859 struct sockaddr addr
;
866 kDummySockLen
)).WillOnce(Return(kDummyInt4
));
867 EXPECT_EQ(kDummyInt4
,
876 TEST_F(KernelWrapTest
, sendmsg
) {
878 EXPECT_CALL(mock
, sendmsg(kDummyInt
, &msg
, kDummyInt2
))
879 .WillOnce(Return(kDummyInt3
));
880 EXPECT_EQ(kDummyInt3
, sendmsg(kDummyInt
, &msg
, kDummyInt2
));
883 TEST_F(KernelWrapTest
, setsockopt
) {
884 // The way we wrap setsockopt does not support returning arbitrary values, so
886 const socklen_t kDummySockLen
= 0x50cc5;
890 kDummyInt
, kDummyInt2
, kDummyInt3
, kDummyVoidPtr
, kDummySockLen
))
892 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
896 kDummyInt
, kDummyInt2
, kDummyInt3
, kDummyVoidPtr
, kDummySockLen
));
900 kDummyInt
, kDummyInt2
, kDummyInt3
, kDummyVoidPtr
, kDummySockLen
));
901 EXPECT_EQ(kDummyErrno
, errno
);
904 TEST_F(KernelWrapTest
, shutdown
) {
905 // The way we wrap shutdown does not support returning arbitrary values, so we
907 EXPECT_CALL(mock
, shutdown(kDummyInt
, kDummyInt2
))
909 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
910 EXPECT_EQ(0, shutdown(kDummyInt
, kDummyInt2
));
911 EXPECT_EQ(-1, shutdown(kDummyInt
, kDummyInt2
));
912 EXPECT_EQ(kDummyErrno
, errno
);
915 TEST_F(KernelWrapTest
, socket
) {
916 EXPECT_CALL(mock
, socket(kDummyInt
, kDummyInt2
, kDummyInt3
))
917 .WillOnce(Return(kDummyInt4
));
918 EXPECT_EQ(kDummyInt4
, socket(kDummyInt
, kDummyInt2
, kDummyInt3
));
921 TEST_F(KernelWrapTest
, socketpair
) {
922 // The way we wrap socketpair does not support returning arbitrary values,
923 // so we test 0 and -1.
925 EXPECT_CALL(mock
, socketpair(kDummyInt
, kDummyInt2
, kDummyInt3
, &dummy_val
))
927 .WillOnce(DoAll(SetErrno(kDummyErrno
), Return(-1)));
928 EXPECT_EQ(0, socketpair(kDummyInt
, kDummyInt2
, kDummyInt3
, &dummy_val
));
929 EXPECT_EQ(-1, socketpair(kDummyInt
, kDummyInt2
, kDummyInt3
, &dummy_val
));
930 EXPECT_EQ(kDummyErrno
, errno
);
933 #endif // PROVIDES_SOCKET_API