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 static ssize_t read_from_fd(int fd, char* buf, size_t buf_len) { | |
| 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); | |
| 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); | |
| 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. | |
| 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 fd = openat(AT_FDCWD, test_path, O_RDONLY); | |
|
hamaji
2014/12/08 07:38:39
Doesn't this test pass even if AT_FDCWD == dirfd?
hidehiko
2014/12/08 12:39:39
Done.
| |
| 70 ASSERT_GE(fd, 0); | |
| 71 len = read_from_fd(fd, buf, kBufLen); | |
| 72 ASSERT_EQ(len, sizeof(test_file_content) - 1); | |
| 73 ASSERT_EQ(memcmp(buf, test_file_content, len), 0); | |
| 74 | |
| 75 // Test for non-existing file. | |
| 76 fd = openat(dirfd, "non-existing-file", O_RDONLY); | |
| 77 ASSERT_EQ(fd, -1); | |
| 78 ASSERT_EQ(errno, ENOENT); | |
| 79 errno = 0; | |
| 80 | |
| 81 rc = close(dirfd); | |
| 82 ASSERT_EQ(rc, 0); | |
| 83 | |
| 84 // Test for invalid directory fd. | |
| 85 fd = openat(-1, test_file_name, O_RDONLY); | |
| 86 ASSERT_EQ(fd, -1); | |
| 87 ASSERT_EQ(errno, EBADF); | |
| 88 } | |
| 89 | |
| 90 void test_fstatat(const char *test_directory) { | |
| 91 puts("test for fstatat()"); | |
| 92 const char test_file_name[] = "test_fstatat_file.txt"; | |
| 93 char test_path[PATH_MAX]; | |
| 94 snprintf(test_path, PATH_MAX, "%s/%s", test_directory, test_file_name); | |
| 95 | |
| 96 // Create an empty file. | |
| 97 { | |
| 98 FILE *fp = fopen(test_path, "w"); | |
| 99 fclose(fp); | |
| 100 } | |
| 101 | |
| 102 struct stat buf; | |
| 103 struct stat buf2; | |
| 104 int rc = stat(test_path, &buf); | |
| 105 ASSERT_EQ(rc, 0); | |
| 106 int dirfd = open(test_directory, O_RDONLY | O_DIRECTORY); | |
| 107 ASSERT_GE(dirfd, 0); | |
| 108 // Currently, no |flag| is defined. So, pass 0. | |
| 109 rc = fstatat(dirfd, test_file_name, &buf2, 0); | |
| 110 ASSERT_EQ(rc, 0); | |
| 111 ASSERT_EQ(buf.st_dev, buf2.st_dev); | |
| 112 ASSERT_EQ(buf.st_mode, buf2.st_mode); | |
| 113 ASSERT_EQ(buf.st_nlink, buf2.st_nlink); | |
| 114 ASSERT_EQ(buf.st_uid, buf2.st_uid); | |
| 115 ASSERT_EQ(buf.st_gid, buf2.st_gid); | |
| 116 ASSERT_EQ(buf.st_rdev, buf2.st_rdev); | |
| 117 ASSERT_EQ(buf.st_size, buf2.st_size); | |
| 118 ASSERT_EQ(buf.st_blksize, buf2.st_blksize); | |
| 119 ASSERT_EQ(buf.st_blocks, buf2.st_blocks); | |
| 120 ASSERT_EQ(buf.st_atime, buf2.st_atime); | |
| 121 ASSERT_EQ(buf.st_mtime, buf2.st_mtime); | |
| 122 ASSERT_EQ(buf.st_ctime, buf2.st_ctime); | |
| 123 | |
| 124 // Test for AT_FDCWD. | |
| 125 rc = fstatat(AT_FDCWD, test_path, &buf2, 0); | |
| 126 ASSERT_EQ(rc, 0); | |
| 127 ASSERT_EQ(buf.st_dev, buf2.st_dev); | |
| 128 ASSERT_EQ(buf.st_mode, buf2.st_mode); | |
| 129 ASSERT_EQ(buf.st_nlink, buf2.st_nlink); | |
| 130 ASSERT_EQ(buf.st_uid, buf2.st_uid); | |
| 131 ASSERT_EQ(buf.st_gid, buf2.st_gid); | |
| 132 ASSERT_EQ(buf.st_rdev, buf2.st_rdev); | |
| 133 ASSERT_EQ(buf.st_size, buf2.st_size); | |
| 134 ASSERT_EQ(buf.st_blksize, buf2.st_blksize); | |
| 135 ASSERT_EQ(buf.st_blocks, buf2.st_blocks); | |
| 136 ASSERT_EQ(buf.st_atime, buf2.st_atime); | |
| 137 ASSERT_EQ(buf.st_mtime, buf2.st_mtime); | |
| 138 ASSERT_EQ(buf.st_ctime, buf2.st_ctime); | |
| 139 | |
| 140 // Test for non-existing file. | |
| 141 rc = fstatat(dirfd, "not-existing-file", &buf, 0); | |
| 142 ASSERT_EQ(rc, -1); | |
| 143 ASSERT_EQ(errno, ENOENT); | |
| 144 errno = 0; | |
| 145 | |
| 146 rc = close(dirfd); | |
| 147 ASSERT_EQ(rc, 0); | |
| 148 | |
| 149 // With invalid file descriptor. | |
| 150 rc = fstatat(-1, test_file_name, &buf2, 0); | |
| 151 ASSERT_EQ(rc, -1); | |
| 152 ASSERT_EQ(errno, EBADF); | |
| 153 errno = 0; | |
| 154 | |
| 155 // Test with non-directory file descriptor. | |
| 156 int fd = open(test_path, O_RDONLY); | |
| 157 ASSERT_GE(fd, 0); | |
| 158 rc = fstatat(fd, test_file_name, &buf2, 0); | |
| 159 ASSERT_EQ(rc, -1); | |
| 160 ASSERT_EQ(errno, ENOTDIR); | |
| 161 errno = 0; | |
| 162 | |
| 163 rc = close(fd); | |
| 164 ASSERT_EQ(rc, 0); | |
| 165 } | |
| 166 | |
| 167 int main(int argc, char *argv[]) { | |
| 168 if (argc != 2) { | |
| 169 printf("Please specify the test directory name.\n"); | |
| 170 exit(-1); | |
| 171 } | |
| 172 | |
| 173 const char* test_directory = argv[1]; | |
| 174 test_openat(test_directory); | |
| 175 test_fstatat(test_directory); | |
| 176 | |
| 177 puts("PASSED"); | |
| 178 return 0; | |
| 179 } | |
| OLD | NEW |