OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2014 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 #include <errno.h> | |
8 #include <fcntl.h> | |
9 #include <limits.h> | |
10 #include <stdio.h> | |
11 #include <string.h> | |
12 #include <sys/stat.h> | |
13 #include <unistd.h> | |
14 | |
15 #include <string> | |
16 | |
17 #include "native_client/src/include/nacl_assert.h" | |
18 | |
19 // Reads the whole file content from fd. On failure, returns an empty string. | |
20 static std::string read_from_fd(int fd) { | |
21 std::string content; | |
22 | |
23 const size_t kBufLen = 80; | |
24 char buf[kBufLen]; | |
25 while (true) { | |
26 ssize_t rc = read(fd, buf, kBufLen); | |
Mark Seaborn
2015/01/08 01:10:55
"rc" -> "bytes_read" for readability?
hidehiko
2015/01/08 07:30:00
Done.
| |
27 if (rc < 0) | |
28 return ""; // On error. | |
29 | |
30 if (rc == 0) | |
31 return content; // EOF | |
32 | |
33 content.append(buf, rc); | |
34 } | |
35 } | |
36 | |
37 void test_openat(const char *test_directory) { | |
38 puts("test for openat()"); | |
39 const char kTestFileName[] = "test_openat_file.txt"; | |
40 const char kTestFileContent[] = "Hello, World.\n"; | |
41 const std::string kTestPath = | |
42 std::string(test_directory) + "/" + kTestFileName; | |
43 | |
44 // Create a test file under the |test_directory|. | |
45 { | |
46 FILE *fp = fopen(kTestPath.c_str(), "w"); | |
47 ASSERT_NE(fp, NULL); | |
48 fputs(kTestFileContent, fp); | |
49 ASSERT_EQ(0, fclose(fp)); | |
50 } | |
51 | |
52 int dirfd = open(test_directory, O_RDONLY | O_DIRECTORY); | |
53 ASSERT_GE(dirfd, 0); | |
54 | |
55 int fd = openat(dirfd, kTestFileName, O_RDONLY); | |
56 ASSERT_GE(fd, 0); | |
57 | |
58 // Read the file content. | |
59 { | |
60 std::string content = read_from_fd(fd); | |
61 ASSERT(content == kTestFileContent); | |
62 } | |
63 | |
64 // Open the non-directory fd. | |
65 int fd2 = openat(fd, kTestFileName, O_RDONLY); | |
66 ASSERT_EQ(fd2, -1); | |
67 ASSERT_EQ(errno, ENOTDIR); | |
68 errno = 0; | |
69 | |
70 int rc = close(fd); | |
71 ASSERT_EQ(rc, 0); | |
72 | |
73 // Test for AT_FDCWD. | |
74 { | |
75 char original_directory[PATH_MAX]; | |
76 ASSERT_NE(getcwd(original_directory, PATH_MAX), NULL); | |
77 ASSERT_EQ(chdir(test_directory), 0); | |
78 | |
79 fd = openat(AT_FDCWD, kTestFileName, O_RDONLY); | |
80 ASSERT_GE(fd, 0); | |
81 std::string content = read_from_fd(fd); | |
82 ASSERT(content == kTestFileContent); | |
83 | |
84 ASSERT_EQ(chdir(original_directory), 0); | |
85 } | |
86 | |
87 // Test for non-existing file. | |
88 fd = openat(dirfd, "non-existing-file", O_RDONLY); | |
89 ASSERT_EQ(fd, -1); | |
90 ASSERT_EQ(errno, ENOENT); | |
91 errno = 0; | |
92 | |
93 rc = close(dirfd); | |
94 ASSERT_EQ(rc, 0); | |
95 | |
96 // Test for invalid directory fd. | |
97 fd = openat(-1, kTestFileName, O_RDONLY); | |
98 ASSERT_EQ(fd, -1); | |
99 ASSERT_EQ(errno, EBADF); | |
100 } | |
101 | |
102 void test_fstatat(const char *test_directory) { | |
103 puts("test for fstatat()"); | |
104 const char kTestFileName[] = "test_fstatat_file.txt"; | |
105 const std::string kTestPath = | |
106 std::string(test_directory) + "/" + kTestFileName; | |
107 | |
108 // Create an empty file. | |
109 { | |
110 FILE *fp = fopen(kTestPath.c_str(), "w"); | |
111 ASSERT_NE(fp, NULL); | |
112 ASSERT_EQ(0, fclose(fp)); | |
113 } | |
114 | |
115 struct stat buf; | |
116 struct stat buf2; | |
117 int rc = stat(kTestPath.c_str(), &buf); | |
118 ASSERT_EQ(rc, 0); | |
119 int dirfd = open(test_directory, O_RDONLY | O_DIRECTORY); | |
120 ASSERT_GE(dirfd, 0); | |
121 // Currently, no |flag| is defined. So, pass 0. | |
122 rc = fstatat(dirfd, kTestFileName, &buf2, 0); | |
123 ASSERT_EQ(rc, 0); | |
124 ASSERT_EQ(buf.st_dev, buf2.st_dev); | |
125 ASSERT_EQ(buf.st_mode, buf2.st_mode); | |
126 ASSERT_EQ(buf.st_nlink, buf2.st_nlink); | |
127 ASSERT_EQ(buf.st_uid, buf2.st_uid); | |
128 ASSERT_EQ(buf.st_gid, buf2.st_gid); | |
129 ASSERT_EQ(buf.st_rdev, buf2.st_rdev); | |
130 ASSERT_EQ(buf.st_size, buf2.st_size); | |
131 ASSERT_EQ(buf.st_blksize, buf2.st_blksize); | |
132 ASSERT_EQ(buf.st_blocks, buf2.st_blocks); | |
133 ASSERT_EQ(buf.st_atime, buf2.st_atime); | |
134 ASSERT_EQ(buf.st_mtime, buf2.st_mtime); | |
135 ASSERT_EQ(buf.st_ctime, buf2.st_ctime); | |
136 | |
137 // Test for AT_FDCWD. | |
138 { | |
139 char original_directory[PATH_MAX]; | |
140 ASSERT_NE(getcwd(original_directory, PATH_MAX), NULL); | |
141 ASSERT_EQ(chdir(test_directory), 0); | |
142 | |
143 rc = fstatat(AT_FDCWD, kTestFileName, &buf2, 0); | |
144 ASSERT_EQ(rc, 0); | |
145 ASSERT_EQ(buf.st_dev, buf2.st_dev); | |
146 ASSERT_EQ(buf.st_mode, buf2.st_mode); | |
147 ASSERT_EQ(buf.st_nlink, buf2.st_nlink); | |
148 ASSERT_EQ(buf.st_uid, buf2.st_uid); | |
149 ASSERT_EQ(buf.st_gid, buf2.st_gid); | |
150 ASSERT_EQ(buf.st_rdev, buf2.st_rdev); | |
151 ASSERT_EQ(buf.st_size, buf2.st_size); | |
152 ASSERT_EQ(buf.st_blksize, buf2.st_blksize); | |
153 ASSERT_EQ(buf.st_blocks, buf2.st_blocks); | |
154 ASSERT_EQ(buf.st_atime, buf2.st_atime); | |
155 ASSERT_EQ(buf.st_mtime, buf2.st_mtime); | |
156 ASSERT_EQ(buf.st_ctime, buf2.st_ctime); | |
157 | |
158 ASSERT_EQ(chdir(original_directory), 0); | |
159 } | |
160 | |
161 // Test for non-existing file. | |
162 rc = fstatat(dirfd, "not-existing-file", &buf, 0); | |
163 ASSERT_EQ(rc, -1); | |
164 ASSERT_EQ(errno, ENOENT); | |
165 errno = 0; | |
166 | |
167 rc = close(dirfd); | |
168 ASSERT_EQ(rc, 0); | |
169 | |
170 // With invalid file descriptor. | |
171 rc = fstatat(-1, kTestFileName, &buf2, 0); | |
172 ASSERT_EQ(rc, -1); | |
173 ASSERT_EQ(errno, EBADF); | |
174 errno = 0; | |
175 | |
176 // Test with non-directory file descriptor. | |
177 int fd = open(kTestPath.c_str(), O_RDONLY); | |
178 ASSERT_GE(fd, 0); | |
179 rc = fstatat(fd, kTestFileName, &buf2, 0); | |
180 ASSERT_EQ(rc, -1); | |
181 ASSERT_EQ(errno, ENOTDIR); | |
182 errno = 0; | |
183 | |
184 rc = close(fd); | |
185 ASSERT_EQ(rc, 0); | |
186 } | |
187 | |
188 int main(int argc, char *argv[]) { | |
189 if (argc != 2) { | |
190 printf("Please specify the test directory name.\n"); | |
191 exit(-1); | |
192 } | |
193 | |
194 const char *test_directory = argv[1]; | |
195 test_openat(test_directory); | |
196 test_fstatat(test_directory); | |
197 | |
198 puts("PASSED"); | |
199 return 0; | |
200 } | |
OLD | NEW |