Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: tests/nonsfi/file_descriptor_test.cc

Issue 744803003: Non-SFI mode: Add syscalls which are needed for nacl_helper_nonsfi's sandbox. (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 void test_openat(const char *test_directory) {
18 puts("test for openat()");
19 const char test_file_name[] = "test_openat_file.txt";
20 const char test_file_content[] = "Hello, World.\n";
21
22 // Create a test file under the |test_directory|.
23 {
24 char test_path[PATH_MAX];
25 snprintf(test_path, PATH_MAX, "%s/%s", test_directory, test_file_name);
26 FILE *fp = fopen(test_path, "w");
27 ASSERT_NE(fp, NULL);
28 fputs(test_file_content, fp);
29 fclose(fp);
30 }
31
32 int dirfd = open(test_directory, O_RDONLY | O_DIRECTORY);
33 ASSERT_GE(dirfd, 0);
34
35 int fd = openat(dirfd, test_file_name, O_RDONLY);
36 ASSERT_GE(fd, 0);
37
38 // Read the file content.
39 const int kBufLen = 80;
40 char buf[kBufLen];
41 size_t len = 0;
42 while (true) {
43 ssize_t rc = read(fd, buf + len, kBufLen - len);
44 ASSERT_GE(rc, 0);
45 if (rc == 0)
46 break; // EOF.
47 len += rc;
48 }
49 ASSERT_EQ(len, sizeof(test_file_content) - 1);
50 ASSERT_EQ(memcmp(buf, test_file_content, len), 0);
51
52 // Open for the non-directory fd.
53 int fd2 = openat(fd, test_file_name, O_RDONLY);
54 ASSERT_EQ(fd2, -1);
55 ASSERT_EQ(errno, ENOTDIR);
56 errno = 0;
57
58 int rc = close(fd);
59 ASSERT_EQ(rc, 0);
60
61 // Test for non-existing file.
62 fd = openat(dirfd, "non-existing-file", O_RDONLY);
63 ASSERT_EQ(fd, -1);
64 ASSERT_EQ(errno, ENOENT);
65 errno = 0;
66
67 rc = close(dirfd);
68 ASSERT_EQ(rc, 0);
69
70 // Test for invalid directory fd.
71 fd = openat(-1, test_file_name, O_RDONLY);
72 ASSERT_EQ(fd, -1);
73 ASSERT_EQ(errno, EBADF);
74 }
75
76 void test_pipe2() {
77 puts("test for pipe2()");
78
79 int fds[2];
80 int rc = pipe2(fds, O_NONBLOCK);
81 ASSERT_EQ(rc, 0);
82
83 // Make sure O_NOBLOCK works.
84 const int kBufSize = 80;
85 char buf[kBufSize];
86 rc = read(fds[0], buf, kBufSize);
87 ASSERT_EQ(rc, -1);
88 ASSERT_EQ(errno, EAGAIN);
89 errno = 0;
90
91 rc = close(fds[0]);
92 ASSERT_EQ(rc, 0);
93 rc = close(fds[1]);
94 ASSERT_EQ(rc, 0);
95 }
96
97 void test_fstatat(const char *test_directory) {
98 puts("test for pipe2()");
99 const char test_file_name[] = "test_fstatat_file.txt";
100 char test_path[PATH_MAX];
101 snprintf(test_path, PATH_MAX, "%s/%s", test_directory, test_file_name);
102
103 // Create an empty file.
104 {
105 FILE *fp = fopen(test_path, "w");
106 fclose(fp);
107 }
108
109 struct stat buf;
110 struct stat buf2;
111 int rc = stat(test_path, &buf);
112 ASSERT_EQ(rc, 0);
113 int dirfd = open(test_directory, O_RDONLY | O_DIRECTORY);
114 ASSERT_GE(dirfd, 0);
115 // Currently, no |flag| is defined. So, pass 0.
116 rc = fstatat(dirfd, test_file_name, &buf2, 0);
117 ASSERT_EQ(rc, 0);
118 ASSERT_EQ(buf.st_dev, buf2.st_dev);
119 ASSERT_EQ(buf.st_mode, buf2.st_mode);
120 ASSERT_EQ(buf.st_nlink, buf2.st_nlink);
121 ASSERT_EQ(buf.st_uid, buf2.st_uid);
122 ASSERT_EQ(buf.st_gid, buf2.st_gid);
123 ASSERT_EQ(buf.st_rdev, buf2.st_rdev);
124 ASSERT_EQ(buf.st_size, buf2.st_size);
125 ASSERT_EQ(buf.st_blksize, buf2.st_blksize);
126 ASSERT_EQ(buf.st_blocks, buf2.st_blocks);
127 ASSERT_EQ(buf.st_atime, buf2.st_atime);
128 ASSERT_EQ(buf.st_mtime, buf2.st_mtime);
129 ASSERT_EQ(buf.st_ctime, buf2.st_ctime);
130
131 // Test for non-existing file.
132 rc = fstatat(dirfd, "not-existing-file", &buf, 0);
133 ASSERT_EQ(rc, -1);
134 ASSERT_EQ(errno, ENOENT);
135 errno = 0;
136
137 rc = close(dirfd);
138 ASSERT_EQ(rc, 0);
139
140 // With invalid file descriptor.
141 rc = fstatat(-1, test_file_name, &buf2, 0);
142 ASSERT_EQ(rc, -1);
143 ASSERT_EQ(errno, EBADF);
144 errno = 0;
145
146 // Test with non-directory file descriptor.
147 int fd = open(test_path, O_RDONLY);
148 ASSERT_GE(fd, 0);
149 rc = fstatat(fd, test_file_name, &buf2, 0);
150 ASSERT_EQ(rc, -1);
151 ASSERT_EQ(errno, ENOTDIR);
152 errno = 0;
153
154 rc = close(fd);
155 ASSERT_EQ(rc, 0);
156 }
157
158 int main(int argc, char *argv[]) {
159 if (argc != 2) {
160 printf("Please specify the test directory name.\n");
161 exit(-1);
162 }
163
164 const char* test_directory = argv[1];
165 test_openat(test_directory);
166 test_pipe2();
167 test_fstatat(test_directory);
168
169 puts("PASSED");
170 return 0;
171 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698