Chromium Code Reviews| 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 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_fstatat(const char *test_directory) { | |
| 77 puts("test for pipe2()"); | |
|
hamaji
2014/12/03 09:01:13
s/pipe2/fstatat/ ?
And we don't have a test for p
hidehiko
2014/12/03 17:34:49
Done.
Note: pipe2() was gone.
| |
| 78 const char test_file_name[] = "test_fstatat_file.txt"; | |
| 79 char test_path[PATH_MAX]; | |
| 80 snprintf(test_path, PATH_MAX, "%s/%s", test_directory, test_file_name); | |
| 81 | |
| 82 // Create an empty file. | |
| 83 { | |
| 84 FILE *fp = fopen(test_path, "w"); | |
| 85 fclose(fp); | |
| 86 } | |
| 87 | |
| 88 struct stat buf; | |
| 89 struct stat buf2; | |
| 90 int rc = stat(test_path, &buf); | |
| 91 ASSERT_EQ(rc, 0); | |
| 92 int dirfd = open(test_directory, O_RDONLY | O_DIRECTORY); | |
| 93 ASSERT_GE(dirfd, 0); | |
| 94 // Currently, no |flag| is defined. So, pass 0. | |
| 95 rc = fstatat(dirfd, test_file_name, &buf2, 0); | |
| 96 ASSERT_EQ(rc, 0); | |
| 97 ASSERT_EQ(buf.st_dev, buf2.st_dev); | |
| 98 ASSERT_EQ(buf.st_mode, buf2.st_mode); | |
| 99 ASSERT_EQ(buf.st_nlink, buf2.st_nlink); | |
| 100 ASSERT_EQ(buf.st_uid, buf2.st_uid); | |
| 101 ASSERT_EQ(buf.st_gid, buf2.st_gid); | |
| 102 ASSERT_EQ(buf.st_rdev, buf2.st_rdev); | |
| 103 ASSERT_EQ(buf.st_size, buf2.st_size); | |
| 104 ASSERT_EQ(buf.st_blksize, buf2.st_blksize); | |
| 105 ASSERT_EQ(buf.st_blocks, buf2.st_blocks); | |
| 106 ASSERT_EQ(buf.st_atime, buf2.st_atime); | |
| 107 ASSERT_EQ(buf.st_mtime, buf2.st_mtime); | |
| 108 ASSERT_EQ(buf.st_ctime, buf2.st_ctime); | |
| 109 | |
| 110 // Test for non-existing file. | |
| 111 rc = fstatat(dirfd, "not-existing-file", &buf, 0); | |
| 112 ASSERT_EQ(rc, -1); | |
| 113 ASSERT_EQ(errno, ENOENT); | |
| 114 errno = 0; | |
| 115 | |
| 116 rc = close(dirfd); | |
| 117 ASSERT_EQ(rc, 0); | |
| 118 | |
| 119 // With invalid file descriptor. | |
| 120 rc = fstatat(-1, test_file_name, &buf2, 0); | |
| 121 ASSERT_EQ(rc, -1); | |
| 122 ASSERT_EQ(errno, EBADF); | |
| 123 errno = 0; | |
| 124 | |
| 125 // Test with non-directory file descriptor. | |
| 126 int fd = open(test_path, O_RDONLY); | |
| 127 ASSERT_GE(fd, 0); | |
| 128 rc = fstatat(fd, test_file_name, &buf2, 0); | |
| 129 ASSERT_EQ(rc, -1); | |
| 130 ASSERT_EQ(errno, ENOTDIR); | |
| 131 errno = 0; | |
| 132 | |
| 133 rc = close(fd); | |
| 134 ASSERT_EQ(rc, 0); | |
| 135 } | |
| 136 | |
| 137 int main(int argc, char *argv[]) { | |
| 138 if (argc != 2) { | |
| 139 printf("Please specify the test directory name.\n"); | |
| 140 exit(-1); | |
| 141 } | |
| 142 | |
| 143 const char* test_directory = argv[1]; | |
| 144 test_openat(test_directory); | |
| 145 test_fstatat(test_directory); | |
| 146 | |
| 147 puts("PASSED"); | |
| 148 return 0; | |
| 149 } | |
| OLD | NEW |