1 // Copyright (c) 2014 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.
11 #include "gtest/gtest.h"
13 #include "nacl_io/kernel_intercept.h"
14 #include "nacl_io/kernel_proxy.h"
16 using namespace nacl_io
;
20 class SyscallsTest
: public ::testing::Test
{
25 ASSERT_EQ(0, ki_push_state_for_testing());
26 ASSERT_EQ(0, ki_init(&kp_
));
27 // Unmount the passthrough FS and mount a memfs.
28 EXPECT_EQ(0, kp_
.umount("/"));
29 EXPECT_EQ(0, kp_
.mount("", "/", "memfs", 0, NULL
));
32 void TearDown() { ki_uninit(); }
40 #if defined(__native_client__) || defined(STANDALONE)
42 // The Linux standalone test is unique in that it calls the real Linux
43 // functions (realpath, mkdir, chdir, etc.), not the nacl_io functions. This is
44 // done to show that the tests match the behavior for a real implementation.
46 TEST_F(SyscallsTest
, Realpath
) {
47 char buffer
[PATH_MAX
];
50 #if defined(__native_client__)
51 ASSERT_EQ(0, mkdir("/tmp", S_IRUSR
| S_IWUSR
));
54 result
= mkdir("/tmp/bar", S_IRUSR
| S_IWUSR
);
55 #if defined(__native_client__)
59 ASSERT_EQ(EEXIST
, errno
);
65 int fd
= open("/tmp/file", O_CREAT
| O_RDWR
, 0644);
67 ASSERT_EQ(0, close(fd
));
69 // Test absolute paths.
70 EXPECT_STREQ("/", realpath("/", buffer
));
71 EXPECT_STREQ("/", realpath("/tmp/..", buffer
));
72 EXPECT_STREQ("/tmp", realpath("/tmp", buffer
));
73 EXPECT_STREQ("/tmp", realpath("/tmp/", buffer
));
74 EXPECT_STREQ("/tmp", realpath("/tmp/bar/..", buffer
));
75 EXPECT_STREQ("/tmp", realpath("/tmp/bar/../bar/../../tmp", buffer
));
76 EXPECT_STREQ("/tmp", realpath("/tmp/././", buffer
));
77 EXPECT_STREQ("/tmp", realpath("///tmp", buffer
));
78 EXPECT_STREQ("/tmp/bar", realpath("/tmp/bar", buffer
));
80 EXPECT_EQ(NULL
, realpath("/blah", buffer
));
81 EXPECT_EQ(ENOENT
, errno
);
83 EXPECT_EQ(NULL
, realpath("/blah/blah", buffer
));
84 EXPECT_EQ(ENOENT
, errno
);
86 EXPECT_EQ(NULL
, realpath("/tmp/baz/..", buffer
));
87 EXPECT_EQ(ENOENT
, errno
);
89 EXPECT_EQ(NULL
, realpath("/tmp/file/", buffer
));
90 EXPECT_EQ(ENOTDIR
, errno
);
92 EXPECT_EQ(NULL
, realpath(NULL
, buffer
));
93 EXPECT_EQ(EINVAL
, errno
);
95 // Test relative paths.
96 EXPECT_EQ(0, chdir("/tmp"));
98 EXPECT_STREQ("/", realpath("..", buffer
));
99 EXPECT_STREQ("/tmp", realpath(".", buffer
));
100 EXPECT_STREQ("/tmp", realpath("./", buffer
));
101 EXPECT_STREQ("/tmp", realpath("bar/..", buffer
));
102 EXPECT_STREQ("/tmp", realpath("bar/../../tmp", buffer
));
103 EXPECT_STREQ("/tmp", realpath(".///", buffer
));
104 EXPECT_STREQ("/tmp/bar", realpath("bar", buffer
));
106 // Test when resolved_path is allocated.
107 char* allocated
= realpath("/tmp", NULL
);
108 EXPECT_STREQ("/tmp", allocated
);
112 #endif // defined(__native_client__) || defined(STANDALONE)