OLD | NEW |
1 /* Copyright 2014 The Native Client Authors. All rights reserved. | 1 /* Copyright 2014 The Native Client Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. */ | 3 * found in the LICENSE file. */ |
4 | 4 |
5 #include <endian.h> | 5 #include <endian.h> |
6 #include <errno.h> | 6 #include <errno.h> |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <netinet/in.h> | 8 #include <netinet/in.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <sys/types.h> | 11 #include <sys/types.h> |
| 12 #include <sys/uio.h> |
| 13 #include <fcntl.h> |
| 14 #include <dirent.h> |
| 15 #include <sys/file.h> |
| 16 #include <err.h> |
| 17 #include <time.h> |
12 | 18 |
13 #ifndef __GLIBC__ | 19 #ifndef __GLIBC__ |
14 #include <sys/endian.h> | 20 #include <sys/endian.h> |
15 #endif | 21 #endif |
16 | 22 |
17 #include "gtest/gtest.h" | 23 #include "gtest/gtest.h" |
18 | 24 |
19 TEST(TestMktemp, mkdtemp_errors) { | 25 TEST(TestMktemp, mkdtemp_errors) { |
20 char small[] = "small"; | 26 char small[] = "small"; |
21 char missing_template[] = "missing_template"; | 27 char missing_template[] = "missing_template"; |
22 char short_template_XXX[] = "short_template_XXX"; | 28 char short_template_XXX[] = "short_template_XXX"; |
23 char not_XXXXXX_suffix[] = "not_XXXXXX_suffix"; | 29 char not_XXXXXX_suffix[] = "not_XXXXXX_suffix"; |
24 ASSERT_EQ((char*)NULL, mkdtemp(small)); | 30 ASSERT_EQ((char*)NULL, mkdtemp(small)); |
25 ASSERT_EQ(EINVAL, errno); | 31 ASSERT_EQ(EINVAL, errno); |
26 ASSERT_EQ((char*)NULL, mkdtemp(missing_template)); | 32 ASSERT_EQ((char*)NULL, mkdtemp(missing_template)); |
27 ASSERT_EQ(EINVAL, errno); | 33 ASSERT_EQ(EINVAL, errno); |
28 ASSERT_EQ((char*)NULL, mkdtemp(short_template_XXX)); | 34 ASSERT_EQ((char*)NULL, mkdtemp(short_template_XXX)); |
29 ASSERT_EQ(EINVAL, errno); | 35 ASSERT_EQ(EINVAL, errno); |
30 ASSERT_EQ((char*)NULL, mkdtemp(not_XXXXXX_suffix)); | 36 ASSERT_EQ((char*)NULL, mkdtemp(not_XXXXXX_suffix)); |
31 ASSERT_EQ(EINVAL, errno); | 37 ASSERT_EQ(EINVAL, errno); |
32 | |
33 } | 38 } |
34 | 39 |
35 TEST(TestMktemp, mkdtemp) { | 40 TEST(TestMktemp, mkdtemp) { |
36 char tempdir[] = "tempfile_XXXXXX"; | 41 char tempdir[] = "tempfile_XXXXXX"; |
37 ASSERT_NE((char*)NULL, mkdtemp(tempdir)); | 42 ASSERT_NE((char*)NULL, mkdtemp(tempdir)); |
38 // Check that tempname starts with the correct prefix but has been | 43 // Check that tempname starts with the correct prefix but has been |
39 // modified from the original. | 44 // modified from the original. |
40 ASSERT_EQ(0, strncmp("tempfile_", tempdir, strlen("tempfile_"))); | 45 ASSERT_EQ(0, strncmp("tempfile_", tempdir, strlen("tempfile_"))); |
41 ASSERT_STRNE("tempfile_XXXXXX", tempdir); | 46 ASSERT_STRNE("tempfile_XXXXXX", tempdir); |
42 | 47 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 int n32 = htonl(num32); | 90 int n32 = htonl(num32); |
86 int h16 = ntohs(num16); | 91 int h16 = ntohs(num16); |
87 int h32 = ntohl(num32); | 92 int h32 = ntohl(num32); |
88 ASSERT_EQ(n16, htobe16(num16)); | 93 ASSERT_EQ(n16, htobe16(num16)); |
89 ASSERT_EQ(n32, htobe32(num32)); | 94 ASSERT_EQ(n32, htobe32(num32)); |
90 ASSERT_EQ(h16, betoh16(num16)); | 95 ASSERT_EQ(h16, betoh16(num16)); |
91 ASSERT_EQ(h32, betoh32(num32)); | 96 ASSERT_EQ(h32, betoh32(num32)); |
92 } | 97 } |
93 #endif | 98 #endif |
94 | 99 |
| 100 // readv is not implemented in glibc. |
| 101 #ifndef __GLIBC__ |
| 102 TEST(TestReadv, readv_writev) { |
| 103 struct iovec write_iov[3]; |
| 104 struct iovec read_iov[3]; |
| 105 char first[28], second[28], third[12]; |
| 106 read_iov[0].iov_base = first; |
| 107 read_iov[0].iov_len = sizeof(first); |
| 108 read_iov[1].iov_base = second; |
| 109 read_iov[1].iov_len = sizeof(second); |
| 110 read_iov[2].iov_base = third; |
| 111 read_iov[2].iov_len = sizeof(third); |
| 112 int i = 0; |
| 113 ssize_t ret = 0; |
| 114 const char* str1 = "abcdefghijklmnopqrstuvwxyz\n"; |
| 115 const char* str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"; |
| 116 const char* str3 = "0123456789\n"; |
| 117 write_iov[0].iov_base = (void*)str1; |
| 118 write_iov[0].iov_len = strlen(str1) + 1; |
| 119 write_iov[1].iov_base = (void*)str2; |
| 120 write_iov[1].iov_len = strlen(str2) + 1; |
| 121 write_iov[2].iov_base = (void*)str3; |
| 122 write_iov[2].iov_len = strlen(str3) + 1; |
| 123 int fd = open("test.txt", O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR); |
| 124 ASSERT_NE(fd, -1); |
| 125 ret = writev(fd, write_iov, 3); |
| 126 ASSERT_NE(ret, -1); |
| 127 ASSERT_NE(close(fd), -1); |
| 128 fd = open("test.txt", O_RDONLY); |
| 129 ASSERT_NE(fd, -1); |
| 130 ret = readv(fd, read_iov, 3); |
| 131 ASSERT_NE(ret, -1); |
| 132 ASSERT_STREQ(str1, (const char*)read_iov[0].iov_base); |
| 133 ASSERT_EQ(28, read_iov[0].iov_len); |
| 134 ASSERT_STREQ(str2, (const char*)read_iov[1].iov_base); |
| 135 ASSERT_EQ(28, read_iov[1].iov_len); |
| 136 ASSERT_STREQ(str3, (const char*)read_iov[2].iov_base); |
| 137 ASSERT_EQ(12, read_iov[2].iov_len); |
| 138 ASSERT_NE(close(fd), -1); |
| 139 ASSERT_NE(-1, unlink("test.txt")); |
| 140 } |
| 141 #endif |
| 142 |
| 143 // No tests for funtions ended with at, e.g. openat, fts_* |
| 144 // fchdir is not implemented in sel_ldr |
| 145 #if 0 |
| 146 TEST(TestOpenat, openat) { |
| 147 ASSERT_NE(-1, mkdir("t1", S_IRWXU | S_IRWXG | S_IXGRP)); |
| 148 DIR* dir = opendir("t1"); |
| 149 ASSERT_NE((DIR*)NULL, dir); |
| 150 int fd_t1 = dirfd(dir); |
| 151 ASSERT_NE(-1, fd_t1); |
| 152 int fd_t2 = openat(fd_t1, "test.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); |
| 153 ASSERT_NE(-1, fd_t2); |
| 154 ASSERT_EQ(4, write(fd_t2, "test", 4)); |
| 155 ASSERT_NE(-1, close(fd_t2)); |
| 156 ASSERT_NE(-1, close(fd_t1)); |
| 157 ASSERT_NE(-1, unlink("t1/test.txt")); |
| 158 ASSERT_NE(-1, rmdir("t1")); |
| 159 } |
| 160 #endif |
| 161 |
95 TEST(TestLockf, lockf) { | 162 TEST(TestLockf, lockf) { |
96 // The fcntl() method underlying lockf() is not implemented in NaCl. | 163 // The fcntl() method underlying lockf() is not implemented in NaCl. |
97 ASSERT_EQ(-1, lockf(1, F_LOCK, 1)); | 164 ASSERT_EQ(-1, lockf(1, F_LOCK, 1)); |
98 ASSERT_EQ(ENOSYS, errno); | 165 ASSERT_EQ(ENOSYS, errno); |
99 ASSERT_EQ(-1, lockf(1, F_TLOCK, 1)); | 166 ASSERT_EQ(-1, lockf(1, F_TLOCK, 1)); |
100 ASSERT_EQ(ENOSYS, errno); | 167 ASSERT_EQ(ENOSYS, errno); |
101 ASSERT_EQ(-1, lockf(1, F_ULOCK, 1)); | 168 ASSERT_EQ(-1, lockf(1, F_ULOCK, 1)); |
102 ASSERT_EQ(ENOSYS, errno); | 169 ASSERT_EQ(ENOSYS, errno); |
103 } | 170 } |
104 | 171 |
| 172 TEST(TestFlock, flock) { |
| 173 // The fcntl() method underlying flock() is not implemented in NaCl. |
| 174 ASSERT_EQ(-1, flock(1, LOCK_SH)); |
| 175 ASSERT_EQ(ENOSYS, errno); |
| 176 ASSERT_EQ(-1, flock(1, LOCK_EX)); |
| 177 ASSERT_EQ(ENOSYS, errno); |
| 178 ASSERT_EQ(-1, flock(1, LOCK_UN)); |
| 179 ASSERT_EQ(ENOSYS, errno); |
| 180 } |
| 181 |
| 182 /* Calling err will cause the test program exit with failure code if errno |
| 183 * is set. No tests for err. |
| 184 */ |
| 185 |
| 186 TEST(TestTimegm, timegm) { |
| 187 struct tm tmp; |
| 188 tmp.tm_sec = 1; |
| 189 tmp.tm_min = 0; |
| 190 tmp.tm_hour = 0; |
| 191 tmp.tm_mday = 3; |
| 192 tmp.tm_mon = 4 - 1; |
| 193 tmp.tm_year = 2015 - 1900; |
| 194 tmp.tm_isdst = -1; |
| 195 time_t tt = timegm(&tmp); |
| 196 ASSERT_EQ(1428019201, tt); |
| 197 } |
| 198 |
105 int main(int argc, char** argv) { | 199 int main(int argc, char** argv) { |
106 setenv("TERM", "xterm-256color", 0); | 200 setenv("TERM", "xterm-256color", 0); |
107 ::testing::InitGoogleTest(&argc, argv); | 201 ::testing::InitGoogleTest(&argc, argv); |
108 return RUN_ALL_TESTS(); | 202 return RUN_ALL_TESTS(); |
109 } | 203 } |
OLD | NEW |