Chromium Code Reviews| 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 = 28; | |
|
Sam Clegg
2015/07/28 00:14:13
sizeof(first)?
zhitingzhu
2015/07/28 02:05:41
Done.
| |
| 108 read_iov[1].iov_base = second; | |
| 109 read_iov[1].iov_len = 28; | |
| 110 read_iov[2].iov_base = third; | |
| 111 read_iov[2].iov_len = 12; | |
| 112 int i = 0; | |
| 113 ssize_t ret = 0; | |
| 114 char str1[] = "abcdefghijklmnopqrstuvwxyz\n"; | |
|
Sam Clegg
2015/07/28 00:14:13
Why not 'const char*'?
zhitingzhu
2015/07/28 02:05:41
Done.
| |
| 115 char str2[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"; | |
| 116 char str3[] = "0123456789\n"; | |
| 117 write_iov[0].iov_base = str1; | |
| 118 write_iov[0].iov_len = 28; | |
|
Sam Clegg
2015/07/28 00:14:13
sizeof?
zhitingzhu
2015/07/28 02:05:41
Done.
| |
| 119 write_iov[1].iov_base = str2; | |
| 120 write_iov[1].iov_len = 28; | |
| 121 write_iov[2].iov_base = str3; | |
| 122 write_iov[2].iov_len = 12; | |
| 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("abcdefghijklmnopqrstuvwxyz\n", | |
|
Sam Clegg
2015/07/28 00:14:13
Use str1 here?
zhitingzhu
2015/07/28 02:05:41
Done.
| |
| 133 (const char*)read_iov[0].iov_base); | |
| 134 ASSERT_EQ(28, read_iov[0].iov_len); | |
| 135 ASSERT_STREQ("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", | |
| 136 (const char*)read_iov[1].iov_base); | |
| 137 ASSERT_EQ(28, read_iov[1].iov_len); | |
| 138 ASSERT_STREQ("0123456789\n", (const char*)read_iov[2].iov_base); | |
| 139 ASSERT_EQ(12, read_iov[2].iov_len); | |
| 140 ASSERT_NE(close(fd), -1); | |
| 141 ASSERT_NE(-1, unlink("test.txt")); | |
| 142 } | |
| 143 #endif | |
| 144 | |
| 145 // No tests for funtions ended with at, e.g. openat, fts_* | |
| 146 // fchdir is not implemented in sel_ldr | |
| 147 #if 0 | |
| 148 TEST(TestOpenat, openat) { | |
| 149 ASSERT_NE(-1, mkdir("t1", S_IRWXU | S_IRWXG | S_IXGRP)); | |
| 150 DIR* dir = opendir("t1"); | |
| 151 ASSERT_NE((DIR*)NULL, dir); | |
| 152 int fd_t1 = dirfd(dir); | |
| 153 ASSERT_NE(-1, fd_t1); | |
| 154 int fd_t2 = openat(fd_t1, "test.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); | |
| 155 ASSERT_NE(-1, fd_t2); | |
| 156 ASSERT_EQ(4, write(fd_t2, "test", 4)); | |
| 157 ASSERT_NE(-1, close(fd_t2)); | |
| 158 ASSERT_NE(-1, close(fd_t1)); | |
| 159 ASSERT_NE(-1, unlink("t1/test.txt")); | |
| 160 ASSERT_NE(-1, rmdir("t1")); | |
| 161 } | |
| 162 #endif | |
| 163 | |
| 95 TEST(TestLockf, lockf) { | 164 TEST(TestLockf, lockf) { |
| 96 // The fcntl() method underlying lockf() is not implemented in NaCl. | 165 // The fcntl() method underlying lockf() is not implemented in NaCl. |
| 97 ASSERT_EQ(-1, lockf(1, F_LOCK, 1)); | 166 ASSERT_EQ(-1, lockf(1, F_LOCK, 1)); |
| 98 ASSERT_EQ(ENOSYS, errno); | 167 ASSERT_EQ(ENOSYS, errno); |
| 99 ASSERT_EQ(-1, lockf(1, F_TLOCK, 1)); | 168 ASSERT_EQ(-1, lockf(1, F_TLOCK, 1)); |
| 100 ASSERT_EQ(ENOSYS, errno); | 169 ASSERT_EQ(ENOSYS, errno); |
| 101 ASSERT_EQ(-1, lockf(1, F_ULOCK, 1)); | 170 ASSERT_EQ(-1, lockf(1, F_ULOCK, 1)); |
| 102 ASSERT_EQ(ENOSYS, errno); | 171 ASSERT_EQ(ENOSYS, errno); |
| 103 } | 172 } |
| 104 | 173 |
| 174 TEST(TestFlock, flock) { | |
| 175 // The fcntl() method underlying flock() is not implemented in NaCl. | |
| 176 ASSERT_EQ(-1, flock(1, LOCK_SH)); | |
| 177 ASSERT_EQ(ENOSYS, errno); | |
| 178 ASSERT_EQ(-1, flock(1, LOCK_EX)); | |
| 179 ASSERT_EQ(ENOSYS, errno); | |
| 180 ASSERT_EQ(-1, flock(1, LOCK_UN)); | |
| 181 ASSERT_EQ(ENOSYS, errno); | |
| 182 } | |
| 183 | |
| 184 // Can't tested err as it will let the test program fail. | |
|
Sam Clegg
2015/07/28 00:14:13
Can you elaborate or re-phrase this comment? Its
zhitingzhu
2015/07/28 02:05:41
Done.
| |
| 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 |