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 "native_client/src/include/nacl_assert.h" | |
16 | |
17 static ssize_t read_from_fd(int fd, char* buf, size_t buf_len) { | |
Mark Seaborn
2014/12/08 22:09:03
Use " *" spacing
hidehiko
2014/12/09 08:38:49
Acknowledged.
| |
18 size_t len = 0; | |
19 while (len < buf_len) { | |
20 ssize_t rc = read(fd, buf + len, buf_len - len); | |
21 if (rc < 0) | |
22 return -1; | |
23 if (rc == 0) | |
24 break; // EOF. | |
25 len += rc; | |
26 } | |
27 return len; | |
28 } | |
29 | |
30 void test_openat(const char *test_directory) { | |
31 puts("test for openat()"); | |
32 const char test_file_name[] = "test_openat_file.txt"; | |
33 const char test_file_content[] = "Hello, World.\n"; | |
34 | |
35 char test_path[PATH_MAX]; | |
36 snprintf(test_path, PATH_MAX, "%s/%s", test_directory, test_file_name); | |
Mark Seaborn
2014/12/08 22:09:03
How about using std::string instead?
hidehiko
2014/12/09 08:38:49
Done.
| |
37 | |
38 // Create a test file under the |test_directory|. | |
39 { | |
40 FILE *fp = fopen(test_path, "w"); | |
41 ASSERT_NE(fp, NULL); | |
42 fputs(test_file_content, fp); | |
43 fclose(fp); | |
Mark Seaborn
2014/12/08 22:09:03
Nit: check for error
hidehiko
2014/12/09 08:38:49
Done.
| |
44 } | |
45 | |
46 int dirfd = open(test_directory, O_RDONLY | O_DIRECTORY); | |
47 ASSERT_GE(dirfd, 0); | |
48 | |
49 int fd = openat(dirfd, test_file_name, O_RDONLY); | |
50 ASSERT_GE(fd, 0); | |
51 | |
52 // Read the file content. | |
53 const int kBufLen = 80; | |
54 char buf[kBufLen]; | |
55 ssize_t len = read_from_fd(fd, buf, kBufLen); | |
56 ASSERT_EQ(len, sizeof(test_file_content) - 1); | |
57 ASSERT_EQ(memcmp(buf, test_file_content, len), 0); | |
58 | |
59 // Open for the non-directory fd. | |
Mark Seaborn
2014/12/08 22:09:02
Grammar nit: remove "for"
hidehiko
2014/12/09 08:38:49
Done.
| |
60 int fd2 = openat(fd, test_file_name, O_RDONLY); | |
61 ASSERT_EQ(fd2, -1); | |
62 ASSERT_EQ(errno, ENOTDIR); | |
63 errno = 0; | |
64 | |
65 int rc = close(fd); | |
66 ASSERT_EQ(rc, 0); | |
67 | |
68 // Test for AT_FDCWD. | |
69 { | |
70 char original_directory[PATH_MAX]; | |
71 ASSERT_NE(getcwd(original_directory, PATH_MAX), NULL); | |
72 ASSERT_EQ(chdir(test_directory), 0); | |
73 | |
74 fd = openat(AT_FDCWD, test_file_name, O_RDONLY); | |
75 ASSERT_GE(fd, 0); | |
76 len = read_from_fd(fd, buf, kBufLen); | |
77 ASSERT_EQ(len, sizeof(test_file_content) - 1); | |
78 ASSERT_EQ(memcmp(buf, test_file_content, len), 0); | |
79 | |
80 ASSERT_EQ(chdir(original_directory), 0); | |
81 } | |
82 | |
83 // Test for non-existing file. | |
84 fd = openat(dirfd, "non-existing-file", O_RDONLY); | |
85 ASSERT_EQ(fd, -1); | |
86 ASSERT_EQ(errno, ENOENT); | |
87 errno = 0; | |
88 | |
89 rc = close(dirfd); | |
90 ASSERT_EQ(rc, 0); | |
91 | |
92 // Test for invalid directory fd. | |
93 fd = openat(-1, test_file_name, O_RDONLY); | |
94 ASSERT_EQ(fd, -1); | |
95 ASSERT_EQ(errno, EBADF); | |
96 } | |
97 | |
98 void test_fstatat(const char *test_directory) { | |
99 puts("test for fstatat()"); | |
100 const char test_file_name[] = "test_fstatat_file.txt"; | |
101 char test_path[PATH_MAX]; | |
102 snprintf(test_path, PATH_MAX, "%s/%s", test_directory, test_file_name); | |
Mark Seaborn
2014/12/08 22:09:03
Use std::string?
hidehiko
2014/12/09 08:38:49
Done.
| |
103 | |
104 // Create an empty file. | |
105 { | |
106 FILE *fp = fopen(test_path, "w"); | |
Mark Seaborn
2014/12/08 22:09:03
Check for error
hidehiko
2014/12/09 08:38:49
Done.
| |
107 fclose(fp); | |
108 } | |
109 | |
110 struct stat buf; | |
111 struct stat buf2; | |
112 int rc = stat(test_path, &buf); | |
113 ASSERT_EQ(rc, 0); | |
114 int dirfd = open(test_directory, O_RDONLY | O_DIRECTORY); | |
115 ASSERT_GE(dirfd, 0); | |
116 // Currently, no |flag| is defined. So, pass 0. | |
117 rc = fstatat(dirfd, test_file_name, &buf2, 0); | |
118 ASSERT_EQ(rc, 0); | |
119 ASSERT_EQ(buf.st_dev, buf2.st_dev); | |
120 ASSERT_EQ(buf.st_mode, buf2.st_mode); | |
121 ASSERT_EQ(buf.st_nlink, buf2.st_nlink); | |
122 ASSERT_EQ(buf.st_uid, buf2.st_uid); | |
123 ASSERT_EQ(buf.st_gid, buf2.st_gid); | |
124 ASSERT_EQ(buf.st_rdev, buf2.st_rdev); | |
125 ASSERT_EQ(buf.st_size, buf2.st_size); | |
126 ASSERT_EQ(buf.st_blksize, buf2.st_blksize); | |
127 ASSERT_EQ(buf.st_blocks, buf2.st_blocks); | |
128 ASSERT_EQ(buf.st_atime, buf2.st_atime); | |
129 ASSERT_EQ(buf.st_mtime, buf2.st_mtime); | |
130 ASSERT_EQ(buf.st_ctime, buf2.st_ctime); | |
131 | |
132 // Test for AT_FDCWD. | |
133 { | |
134 char original_directory[PATH_MAX]; | |
135 ASSERT_NE(getcwd(original_directory, PATH_MAX), NULL); | |
136 ASSERT_EQ(chdir(test_directory), 0); | |
137 | |
138 rc = fstatat(AT_FDCWD, test_file_name, &buf2, 0); | |
139 ASSERT_EQ(rc, 0); | |
140 ASSERT_EQ(buf.st_dev, buf2.st_dev); | |
141 ASSERT_EQ(buf.st_mode, buf2.st_mode); | |
142 ASSERT_EQ(buf.st_nlink, buf2.st_nlink); | |
143 ASSERT_EQ(buf.st_uid, buf2.st_uid); | |
144 ASSERT_EQ(buf.st_gid, buf2.st_gid); | |
145 ASSERT_EQ(buf.st_rdev, buf2.st_rdev); | |
146 ASSERT_EQ(buf.st_size, buf2.st_size); | |
147 ASSERT_EQ(buf.st_blksize, buf2.st_blksize); | |
148 ASSERT_EQ(buf.st_blocks, buf2.st_blocks); | |
149 ASSERT_EQ(buf.st_atime, buf2.st_atime); | |
150 ASSERT_EQ(buf.st_mtime, buf2.st_mtime); | |
151 ASSERT_EQ(buf.st_ctime, buf2.st_ctime); | |
152 | |
153 ASSERT_EQ(chdir(original_directory), 0); | |
154 } | |
155 | |
156 // Test for non-existing file. | |
157 rc = fstatat(dirfd, "not-existing-file", &buf, 0); | |
158 ASSERT_EQ(rc, -1); | |
159 ASSERT_EQ(errno, ENOENT); | |
160 errno = 0; | |
161 | |
162 rc = close(dirfd); | |
163 ASSERT_EQ(rc, 0); | |
164 | |
165 // With invalid file descriptor. | |
166 rc = fstatat(-1, test_file_name, &buf2, 0); | |
167 ASSERT_EQ(rc, -1); | |
168 ASSERT_EQ(errno, EBADF); | |
169 errno = 0; | |
170 | |
171 // Test with non-directory file descriptor. | |
172 int fd = open(test_path, O_RDONLY); | |
173 ASSERT_GE(fd, 0); | |
174 rc = fstatat(fd, test_file_name, &buf2, 0); | |
175 ASSERT_EQ(rc, -1); | |
176 ASSERT_EQ(errno, ENOTDIR); | |
177 errno = 0; | |
178 | |
179 rc = close(fd); | |
180 ASSERT_EQ(rc, 0); | |
181 } | |
182 | |
183 int main(int argc, char *argv[]) { | |
184 if (argc != 2) { | |
185 printf("Please specify the test directory name.\n"); | |
186 exit(-1); | |
187 } | |
188 | |
189 const char* test_directory = argv[1]; | |
Mark Seaborn
2014/12/08 22:09:03
Use " *" spacing
hidehiko
2014/12/09 08:38:49
Done.
| |
190 test_openat(test_directory); | |
191 test_fstatat(test_directory); | |
192 | |
193 puts("PASSED"); | |
194 return 0; | |
195 } | |
OLD | NEW |