blob: 50afd05b6e4ba44f1d28f6df99d588e420209726 [file] [log] [blame]
Elliott Hughesa55f6302013-01-02 14:23:43 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
Yabin Cui9df70402014-11-05 18:01:01 -080018#include "BionicDeathTest.h"
Christopher Ferris13613132013-10-28 15:24:04 -070019#include "ScopedSignalHandler.h"
Elliott Hughesb4f76162013-09-19 16:27:24 -070020#include "TemporaryFile.h"
Elliott Hughesa55f6302013-01-02 14:23:43 -080021
Elliott Hughes915fefb2014-02-18 12:34:51 -080022#include <errno.h>
Colin Cross3d19a832014-02-14 18:56:23 -080023#include <fcntl.h>
Elliott Hughes21972b62014-07-28 12:24:22 -070024#include <limits.h>
Elliott Hughes428f5562013-02-05 16:10:59 -080025#include <stdint.h>
Yabin Cuib5e581a2014-11-08 14:58:12 -080026#include <sys/param.h>
Elliott Hughes7086ad62014-06-19 16:39:01 -070027#include <sys/syscall.h>
Elliott Hughes764a9932014-04-08 19:44:36 -070028#include <sys/types.h>
Derek Xued94e7f02014-09-25 11:12:01 +010029#include <sys/utsname.h>
Elliott Hughes764a9932014-04-08 19:44:36 -070030#include <sys/wait.h>
Yabin Cui9df70402014-11-05 18:01:01 -080031#include <unistd.h>
32
Elliott Hughes533dde42014-04-25 18:27:38 -070033static void* get_brk() {
34 return sbrk(0);
35}
Elliott Hughes428f5562013-02-05 16:10:59 -080036
Elliott Hughes533dde42014-04-25 18:27:38 -070037static void* page_align(uintptr_t addr) {
38 uintptr_t mask = sysconf(_SC_PAGE_SIZE) - 1;
39 return reinterpret_cast<void*>((addr + mask) & ~mask);
40}
41
42TEST(unistd, brk) {
43 void* initial_break = get_brk();
44
45 // The kernel aligns the break to a page.
46 void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 1);
Elliott Hughes428f5562013-02-05 16:10:59 -080047 ASSERT_EQ(0, brk(new_break));
Elliott Hughes533dde42014-04-25 18:27:38 -070048 ASSERT_GE(get_brk(), new_break);
Elliott Hughes428f5562013-02-05 16:10:59 -080049
Elliott Hughes533dde42014-04-25 18:27:38 -070050 new_break = page_align(reinterpret_cast<uintptr_t>(initial_break) + sysconf(_SC_PAGE_SIZE));
51 ASSERT_EQ(0, brk(new_break));
52 ASSERT_EQ(get_brk(), new_break);
53}
54
55TEST(unistd, brk_ENOMEM) {
56 ASSERT_EQ(-1, brk(reinterpret_cast<void*>(-1)));
57 ASSERT_EQ(ENOMEM, errno);
58}
59
Christopher Ferris738b0cc2014-05-21 19:03:34 -070060#if defined(__GLIBC__)
61#define SBRK_MIN INTPTR_MIN
62#define SBRK_MAX INTPTR_MAX
63#else
64#define SBRK_MIN PTRDIFF_MIN
65#define SBRK_MAX PTRDIFF_MAX
66#endif
67
Elliott Hughes533dde42014-04-25 18:27:38 -070068TEST(unistd, sbrk_ENOMEM) {
Christopher Ferris738b0cc2014-05-21 19:03:34 -070069#if defined(__BIONIC__) && !defined(__LP64__)
70 // There is no way to guarantee that all overflow conditions can be tested
71 // without manipulating the underlying values of the current break.
72 extern void* __bionic_brk;
73
74 class ScopedBrk {
75 public:
76 ScopedBrk() : saved_brk_(__bionic_brk) {}
77 virtual ~ScopedBrk() { __bionic_brk = saved_brk_; }
78
79 private:
80 void* saved_brk_;
81 };
82
83 ScopedBrk scope_brk;
84
85 // Set the current break to a point that will cause an overflow.
86 __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) + 2);
Elliott Hughes533dde42014-04-25 18:27:38 -070087
88 // Can't increase by so much that we'd overflow.
89 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MAX));
90 ASSERT_EQ(ENOMEM, errno);
91
Christopher Ferris738b0cc2014-05-21 19:03:34 -070092 // Set the current break to a point that will cause an overflow.
93 __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX));
Elliott Hughes533dde42014-04-25 18:27:38 -070094
Elliott Hughes533dde42014-04-25 18:27:38 -070095 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN));
96 ASSERT_EQ(ENOMEM, errno);
Christopher Ferris738b0cc2014-05-21 19:03:34 -070097
98 __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) - 1);
99
100 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN + 1));
101 ASSERT_EQ(ENOMEM, errno);
102#else
103 class ScopedBrk {
104 public:
105 ScopedBrk() : saved_brk_(get_brk()) {}
106 virtual ~ScopedBrk() { brk(saved_brk_); }
107
108 private:
109 void* saved_brk_;
110 };
111
112 ScopedBrk scope_brk;
113
114 uintptr_t cur_brk = reinterpret_cast<uintptr_t>(get_brk());
115 if (cur_brk < static_cast<uintptr_t>(-(SBRK_MIN+1))) {
116 // Do the overflow test for a max negative increment.
117 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MIN));
118#if defined(__BIONIC__)
119 // GLIBC does not set errno in overflow case.
120 ASSERT_EQ(ENOMEM, errno);
121#endif
122 }
123
124 uintptr_t overflow_brk = static_cast<uintptr_t>(SBRK_MAX) + 2;
125 if (cur_brk < overflow_brk) {
126 // Try and move the value to PTRDIFF_MAX + 2.
127 cur_brk = reinterpret_cast<uintptr_t>(sbrk(overflow_brk));
128 }
129 if (cur_brk >= overflow_brk) {
130 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MAX));
131#if defined(__BIONIC__)
132 // GLIBC does not set errno in overflow case.
133 ASSERT_EQ(ENOMEM, errno);
134#endif
135 }
Elliott Hughes533dde42014-04-25 18:27:38 -0700136#endif
Elliott Hughes428f5562013-02-05 16:10:59 -0800137}
Elliott Hughesb4f76162013-09-19 16:27:24 -0700138
139TEST(unistd, truncate) {
140 TemporaryFile tf;
141 ASSERT_EQ(0, close(tf.fd));
142 ASSERT_EQ(0, truncate(tf.filename, 123));
143
144 struct stat sb;
145 ASSERT_EQ(0, stat(tf.filename, &sb));
146 ASSERT_EQ(123, sb.st_size);
147}
148
149TEST(unistd, truncate64) {
150 TemporaryFile tf;
151 ASSERT_EQ(0, close(tf.fd));
152 ASSERT_EQ(0, truncate64(tf.filename, 123));
153
154 struct stat sb;
155 ASSERT_EQ(0, stat(tf.filename, &sb));
156 ASSERT_EQ(123, sb.st_size);
157}
158
159TEST(unistd, ftruncate) {
160 TemporaryFile tf;
161 ASSERT_EQ(0, ftruncate(tf.fd, 123));
162 ASSERT_EQ(0, close(tf.fd));
163
164 struct stat sb;
165 ASSERT_EQ(0, stat(tf.filename, &sb));
166 ASSERT_EQ(123, sb.st_size);
167}
168
169TEST(unistd, ftruncate64) {
170 TemporaryFile tf;
171 ASSERT_EQ(0, ftruncate64(tf.fd, 123));
172 ASSERT_EQ(0, close(tf.fd));
173
174 struct stat sb;
175 ASSERT_EQ(0, stat(tf.filename, &sb));
176 ASSERT_EQ(123, sb.st_size);
177}
Elliott Hughes11952072013-10-24 15:15:14 -0700178
Elliott Hughes1728b232014-05-14 10:02:03 -0700179static bool g_pause_test_flag = false;
Elliott Hughes11952072013-10-24 15:15:14 -0700180static void PauseTestSignalHandler(int) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700181 g_pause_test_flag = true;
Elliott Hughes11952072013-10-24 15:15:14 -0700182}
183
184TEST(unistd, pause) {
Christopher Ferris13613132013-10-28 15:24:04 -0700185 ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
186
Elliott Hughes11952072013-10-24 15:15:14 -0700187 alarm(1);
Elliott Hughes1728b232014-05-14 10:02:03 -0700188 ASSERT_FALSE(g_pause_test_flag);
Elliott Hughes11952072013-10-24 15:15:14 -0700189 ASSERT_EQ(-1, pause());
Elliott Hughes1728b232014-05-14 10:02:03 -0700190 ASSERT_TRUE(g_pause_test_flag);
Elliott Hughes11952072013-10-24 15:15:14 -0700191}
Colin Cross3d19a832014-02-14 18:56:23 -0800192
193TEST(unistd, read) {
194 int fd = open("/proc/version", O_RDONLY);
195 ASSERT_TRUE(fd != -1);
196
197 char buf[5];
198 ASSERT_EQ(5, read(fd, buf, 5));
199 ASSERT_EQ(buf[0], 'L');
200 ASSERT_EQ(buf[1], 'i');
201 ASSERT_EQ(buf[2], 'n');
202 ASSERT_EQ(buf[3], 'u');
203 ASSERT_EQ(buf[4], 'x');
204 close(fd);
205}
206
207TEST(unistd, read_EBADF) {
208 // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that
209 // our syscall stubs correctly return a 64-bit -1.
210 char buf[1];
211 ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
212 ASSERT_EQ(EBADF, errno);
213}
Elliott Hughesaedb00d2014-03-03 14:38:20 -0800214
Elliott Hughes21972b62014-07-28 12:24:22 -0700215TEST(unistd, syscall_long) {
216 // Check that syscall(3) correctly returns long results.
217 // https://code.google.com/p/android/issues/detail?id=73952
218 // We assume that the break is > 4GiB, but this is potentially flaky.
219 uintptr_t p = reinterpret_cast<uintptr_t>(sbrk(0));
220 ASSERT_EQ(p, static_cast<uintptr_t>(syscall(__NR_brk, 0)));
221}
222
Elliott Hughesaedb00d2014-03-03 14:38:20 -0800223TEST(unistd, alarm) {
224 ASSERT_EQ(0U, alarm(0));
225}
Elliott Hughes9f525642014-04-08 17:14:01 -0700226
227TEST(unistd, _exit) {
228 int pid = fork();
229 ASSERT_NE(-1, pid) << strerror(errno);
230
231 if (pid == 0) {
232 _exit(99);
233 }
234
235 int status;
236 ASSERT_EQ(pid, waitpid(pid, &status, 0));
237 ASSERT_TRUE(WIFEXITED(status));
238 ASSERT_EQ(99, WEXITSTATUS(status));
239}
Grigoriy Kraynovcbf6df02013-09-17 15:44:22 +0400240
241TEST(unistd, getenv_unsetenv) {
242 ASSERT_EQ(0, setenv("test-variable", "hello", 1));
243 ASSERT_STREQ("hello", getenv("test-variable"));
244 ASSERT_EQ(0, unsetenv("test-variable"));
245 ASSERT_TRUE(getenv("test-variable") == NULL);
246}
247
248TEST(unistd, unsetenv_EINVAL) {
Grigoriy Kraynovcbf6df02013-09-17 15:44:22 +0400249 EXPECT_EQ(-1, unsetenv(""));
250 EXPECT_EQ(EINVAL, errno);
251 EXPECT_EQ(-1, unsetenv("a=b"));
252 EXPECT_EQ(EINVAL, errno);
253}
254
255TEST(unistd, setenv_EINVAL) {
256 EXPECT_EQ(-1, setenv(NULL, "value", 0));
257 EXPECT_EQ(EINVAL, errno);
258 EXPECT_EQ(-1, setenv(NULL, "value", 1));
259 EXPECT_EQ(EINVAL, errno);
260 EXPECT_EQ(-1, setenv("", "value", 0));
261 EXPECT_EQ(EINVAL, errno);
262 EXPECT_EQ(-1, setenv("", "value", 1));
263 EXPECT_EQ(EINVAL, errno);
264 EXPECT_EQ(-1, setenv("a=b", "value", 0));
265 EXPECT_EQ(EINVAL, errno);
266 EXPECT_EQ(-1, setenv("a=b", "value", 1));
267 EXPECT_EQ(EINVAL, errno);
268}
269
270TEST(unistd, setenv) {
271 ASSERT_EQ(0, unsetenv("test-variable"));
272
273 char a[] = "a";
274 char b[] = "b";
275 char c[] = "c";
276
277 // New value.
278 EXPECT_EQ(0, setenv("test-variable", a, 0));
279 EXPECT_STREQ(a, getenv("test-variable"));
280
281 // Existing value, no overwrite.
282 EXPECT_EQ(0, setenv("test-variable", b, 0));
283 EXPECT_STREQ(a, getenv("test-variable"));
284
285 // Existing value, overwrite.
286 EXPECT_EQ(0, setenv("test-variable", c, 1));
287 EXPECT_STREQ(c, getenv("test-variable"));
288 // But the arrays backing the values are unchanged.
289 EXPECT_EQ('a', a[0]);
290 EXPECT_EQ('b', b[0]);
291 EXPECT_EQ('c', c[0]);
292
293 ASSERT_EQ(0, unsetenv("test-variable"));
294}
295
296TEST(unistd, putenv) {
297 ASSERT_EQ(0, unsetenv("a"));
298
299 char* s1 = strdup("a=b");
300 ASSERT_EQ(0, putenv(s1));
301
302 ASSERT_STREQ("b", getenv("a"));
303 s1[2] = 'c';
304 ASSERT_STREQ("c", getenv("a"));
305
306 char* s2 = strdup("a=b");
307 ASSERT_EQ(0, putenv(s2));
308
309 ASSERT_STREQ("b", getenv("a"));
310 ASSERT_EQ('c', s1[2]);
311
312 ASSERT_EQ(0, unsetenv("a"));
313 free(s1);
314 free(s2);
315}
316
317TEST(unistd, clearenv) {
318 extern char** environ;
319
320 // Guarantee that environ is not initially empty...
321 ASSERT_EQ(0, setenv("test-variable", "a", 1));
322
323 // Stash a copy.
324 std::vector<char*> old_environ;
325 for (size_t i = 0; environ[i] != NULL; ++i) {
326 old_environ.push_back(strdup(environ[i]));
327 }
328
329 ASSERT_EQ(0, clearenv());
330
331 EXPECT_TRUE(environ == NULL || environ[0] == NULL);
332 EXPECT_EQ(NULL, getenv("test-variable"));
333 EXPECT_EQ(0, setenv("test-variable", "post-clear", 1));
334 EXPECT_STREQ("post-clear", getenv("test-variable"));
335
336 // Put the old environment back.
337 for (size_t i = 0; i < old_environ.size(); ++i) {
338 EXPECT_EQ(0, putenv(old_environ[i]));
339 }
340
341 // Check it wasn't overwritten.
342 EXPECT_STREQ("a", getenv("test-variable"));
343
344 EXPECT_EQ(0, unsetenv("test-variable"));
345}
Elliott Hughesa62a28d2014-05-07 14:30:33 -0700346
347static void TestFsyncFunction(int (*fn)(int)) {
348 int fd;
349
350 // Can't sync an invalid fd.
351 errno = 0;
352 EXPECT_EQ(-1, fn(-1));
353 EXPECT_EQ(EBADF, errno);
354
355 // It doesn't matter whether you've opened a file for write or not.
356 TemporaryFile tf;
357 ASSERT_NE(-1, tf.fd);
358
359 EXPECT_EQ(0, fn(tf.fd));
360
361 ASSERT_NE(-1, fd = open(tf.filename, O_RDONLY));
362 EXPECT_EQ(0, fn(fd));
363 close(fd);
364
365 ASSERT_NE(-1, fd = open(tf.filename, O_RDWR));
366 EXPECT_EQ(0, fn(fd));
367 close(fd);
368
369 // The fd can even be a directory.
370 ASSERT_NE(-1, fd = open("/", O_RDONLY));
371 EXPECT_EQ(0, fn(fd));
372 close(fd);
373
374 // But some file systems may choose to be fussy...
375 errno = 0;
376 ASSERT_NE(-1, fd = open("/proc/version", O_RDONLY));
377 EXPECT_EQ(-1, fn(fd));
378 EXPECT_EQ(EINVAL, errno);
379 close(fd);
380}
381
382TEST(unistd, fdatasync) {
383 TestFsyncFunction(fdatasync);
384}
385
386TEST(unistd, fsync) {
387 TestFsyncFunction(fsync);
388}
Elliott Hughes7086ad62014-06-19 16:39:01 -0700389
Elliott Hughesfa9e16e2014-06-23 17:49:45 -0700390static void AssertGetPidCorrect() {
391 // The loop is just to make manual testing/debugging with strace easier.
392 pid_t getpid_syscall_result = syscall(__NR_getpid);
393 for (size_t i = 0; i < 128; ++i) {
394 ASSERT_EQ(getpid_syscall_result, getpid());
395 }
396}
397
Elliott Hughes7086ad62014-06-19 16:39:01 -0700398TEST(unistd, getpid_caching_and_fork) {
399 pid_t parent_pid = getpid();
400 ASSERT_EQ(syscall(__NR_getpid), parent_pid);
401
402 pid_t fork_result = fork();
403 ASSERT_NE(fork_result, -1);
404 if (fork_result == 0) {
405 // We're the child.
Elliott Hughesfa9e16e2014-06-23 17:49:45 -0700406 AssertGetPidCorrect();
Elliott Hughes7086ad62014-06-19 16:39:01 -0700407 ASSERT_EQ(parent_pid, getppid());
408 _exit(123);
409 } else {
410 // We're the parent.
411 ASSERT_EQ(parent_pid, getpid());
412
413 int status;
414 ASSERT_EQ(fork_result, waitpid(fork_result, &status, 0));
415 ASSERT_TRUE(WIFEXITED(status));
416 ASSERT_EQ(123, WEXITSTATUS(status));
417 }
418}
419
Elliott Hughesfa9e16e2014-06-23 17:49:45 -0700420static int GetPidCachingCloneStartRoutine(void*) {
421 AssertGetPidCorrect();
422 return 123;
Elliott Hughes7086ad62014-06-19 16:39:01 -0700423}
424
Elliott Hughesfa9e16e2014-06-23 17:49:45 -0700425TEST(unistd, getpid_caching_and_clone) {
426 pid_t parent_pid = getpid();
427 ASSERT_EQ(syscall(__NR_getpid), parent_pid);
428
429 void* child_stack[1024];
430 int clone_result = clone(GetPidCachingCloneStartRoutine, &child_stack[1024], CLONE_NEWNS | SIGCHLD, NULL);
Elliott Hughes2b3b2ec2014-08-21 19:23:53 -0700431 if (clone_result == -1 && errno == EPERM && getuid() != 0) {
432 GTEST_LOG_(INFO) << "This test only works if you have permission to CLONE_NEWNS; try running as root.\n";
433 return;
434 }
Elliott Hughesfa9e16e2014-06-23 17:49:45 -0700435 ASSERT_NE(clone_result, -1);
436
437 ASSERT_EQ(parent_pid, getpid());
438
439 int status;
440 ASSERT_EQ(clone_result, waitpid(clone_result, &status, 0));
441 ASSERT_TRUE(WIFEXITED(status));
442 ASSERT_EQ(123, WEXITSTATUS(status));
443}
444
445static void* GetPidCachingPthreadStartRoutine(void*) {
446 AssertGetPidCorrect();
Elliott Hughes7086ad62014-06-19 16:39:01 -0700447 return NULL;
448}
449
450TEST(unistd, getpid_caching_and_pthread_create) {
451 pid_t parent_pid = getpid();
452
453 pthread_t t;
Elliott Hughesfa9e16e2014-06-23 17:49:45 -0700454 ASSERT_EQ(0, pthread_create(&t, NULL, GetPidCachingPthreadStartRoutine, NULL));
Elliott Hughes7086ad62014-06-19 16:39:01 -0700455
456 ASSERT_EQ(parent_pid, getpid());
457
458 void* result;
459 ASSERT_EQ(0, pthread_join(t, &result));
460 ASSERT_EQ(NULL, result);
461}
Elliott Hughes60452a22014-09-22 14:41:30 -0700462
Yabin Cui9df70402014-11-05 18:01:01 -0800463class unistd_DeathTest : public BionicDeathTest {};
464
465TEST_F(unistd_DeathTest, abort) {
Elliott Hughes60452a22014-09-22 14:41:30 -0700466 ASSERT_EXIT(abort(), testing::KilledBySignal(SIGABRT), "");
467}
Elliott Hughesb86a4c72014-11-07 16:07:13 -0800468
469TEST(unistd, sethostname) {
470 // The permissions check happens before the argument check, so this will
471 // fail for a different reason if you're running as root than if you're
472 // not, but it'll fail either way. Checking that we have the symbol is about
473 // all we can do for sethostname(2).
474 ASSERT_EQ(-1, sethostname("", -1));
475}
Derek Xued94e7f02014-09-25 11:12:01 +0100476
477TEST(unistd, gethostname) {
478 char hostname[HOST_NAME_MAX + 1];
Derek Xued94e7f02014-09-25 11:12:01 +0100479 memset(hostname, 0, sizeof(hostname));
480
Yongqin Liu2f954ba2014-10-30 16:34:55 +0800481 // Can we get the hostname with a big buffer?
Derek Xued94e7f02014-09-25 11:12:01 +0100482 ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
Yongqin Liu2f954ba2014-10-30 16:34:55 +0800483
484 // Can we get the hostname with a right-sized buffer?
485 errno = 0;
486 ASSERT_EQ(0, gethostname(hostname, strlen(hostname) + 1));
487
488 // Does uname(2) agree?
Derek Xued94e7f02014-09-25 11:12:01 +0100489 utsname buf;
490 ASSERT_EQ(0, uname(&buf));
491 ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
492 ASSERT_GT(strlen(hostname), 0U);
493
Yongqin Liu2f954ba2014-10-30 16:34:55 +0800494 // Do we correctly detect truncation?
Derek Xued94e7f02014-09-25 11:12:01 +0100495 errno = 0;
496 ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
497 ASSERT_EQ(ENAMETOOLONG, errno);
Derek Xued94e7f02014-09-25 11:12:01 +0100498}
Yabin Cuib5e581a2014-11-08 14:58:12 -0800499
500TEST(unistd, pathconf_fpathconf) {
501 TemporaryFile tf;
502 long rc = 0L;
503 // As a file system's block size is always power of 2, the configure values
504 // for ALLOC and XFER should be power of 2 as well.
505 rc = pathconf(tf.filename, _PC_ALLOC_SIZE_MIN);
506 ASSERT_TRUE(rc > 0 && powerof2(rc));
507 rc = pathconf(tf.filename, _PC_REC_MIN_XFER_SIZE);
508 ASSERT_TRUE(rc > 0 && powerof2(rc));
509 rc = pathconf(tf.filename, _PC_REC_XFER_ALIGN);
510 ASSERT_TRUE(rc > 0 && powerof2(rc));
511
512 rc = fpathconf(tf.fd, _PC_ALLOC_SIZE_MIN);
513 ASSERT_TRUE(rc > 0 && powerof2(rc));
514 rc = fpathconf(tf.fd, _PC_REC_MIN_XFER_SIZE);
515 ASSERT_TRUE(rc > 0 && powerof2(rc));
516 rc = fpathconf(tf.fd, _PC_REC_XFER_ALIGN);
517 ASSERT_TRUE(rc > 0 && powerof2(rc));
518}
Derek Xuebc644762014-09-25 10:55:34 +0100519
520#define verifySysconf(name, ret) \
521{\
522 errno = 0;\
523 ret = sysconf(name);\
524 ASSERT_TRUE((0 == errno) && (-1 != ret)) << "name=" << #name << ", ret=" << ret << ", Error Message: " << strerror(errno);\
525}\
526
527TEST(unistd, sysconf) {
528 long ret;
529
530 verifySysconf(_SC_MONOTONIC_CLOCK, ret);
531 ASSERT_GT(ret, 0);
532 verifySysconf(_SC_ARG_MAX, ret);
533 ASSERT_GT(ret, 0);
534 verifySysconf(_SC_CHILD_MAX, ret);
535 ASSERT_GT(ret, 0);
536 verifySysconf(_SC_CLK_TCK, ret);
537 ASSERT_GT(ret, 0);
538 verifySysconf(_SC_LINE_MAX, ret);
539 ASSERT_GT(ret, 1);
540 verifySysconf(_SC_NGROUPS_MAX, ret);
541 ASSERT_GT(ret, 0);
542 verifySysconf(_SC_OPEN_MAX, ret);
543 ASSERT_GT(ret, 1);
544 verifySysconf(_SC_2_C_BIND, ret);
545 ASSERT_GT(ret, 0);
546 verifySysconf(_SC_2_C_VERSION, ret);
547 verifySysconf(_SC_JOB_CONTROL, ret);
548 ASSERT_EQ(1, ret);
549 verifySysconf(_SC_SAVED_IDS, ret);
550 ASSERT_EQ(1, ret);
551 verifySysconf(_SC_VERSION, ret);
552 verifySysconf(_SC_RE_DUP_MAX, ret);
553 verifySysconf(_SC_STREAM_MAX, ret);
554 ASSERT_GT(ret, 0);
555 verifySysconf(_SC_TZNAME_MAX, ret);
556 verifySysconf(_SC_XOPEN_VERSION, ret);
557 verifySysconf(_SC_ATEXIT_MAX, ret);
558 ASSERT_GT(ret, 1);
559 verifySysconf(_SC_IOV_MAX, ret);
560 ASSERT_GT(ret, 0);
561 verifySysconf(_SC_PAGESIZE, ret);
562 ASSERT_GE(ret, 1);
563 verifySysconf(_SC_PAGE_SIZE, ret);
564 ASSERT_GE(ret, 1);
565 verifySysconf(_SC_XOPEN_UNIX, ret);
566 verifySysconf(_SC_DELAYTIMER_MAX, ret);
567 ASSERT_GT(ret, 0);
568 verifySysconf(_SC_MQ_OPEN_MAX, ret);
569 verifySysconf(_SC_MQ_PRIO_MAX, ret);
570 ASSERT_GT(ret ,0);
571 verifySysconf(_SC_RTSIG_MAX, ret);
572 verifySysconf(_SC_SEM_NSEMS_MAX, ret);
573 verifySysconf(_SC_SEM_VALUE_MAX, ret);
574 verifySysconf(_SC_SIGQUEUE_MAX, ret);
575 ASSERT_GT(ret, 0);
576 verifySysconf(_SC_TIMER_MAX, ret);
577 verifySysconf(_SC_FSYNC, ret);
578 verifySysconf(_SC_MAPPED_FILES, ret);
579 verifySysconf(_SC_PRIORITY_SCHEDULING, ret);
580 verifySysconf(_SC_SEMAPHORES, ret);
581 verifySysconf(_SC_SYNCHRONIZED_IO, ret);
582 verifySysconf(_SC_TIMERS, ret);
583 verifySysconf(_SC_GETGR_R_SIZE_MAX, ret);
584 verifySysconf(_SC_GETPW_R_SIZE_MAX, ret);
585 verifySysconf(_SC_LOGIN_NAME_MAX, ret);
586 verifySysconf(_SC_THREAD_DESTRUCTOR_ITERATIONS, ret);
587 verifySysconf(_SC_THREAD_KEYS_MAX, ret);
588 verifySysconf(_SC_THREAD_STACK_MIN, ret);
589 verifySysconf(_SC_THREAD_THREADS_MAX, ret);
590 verifySysconf(_SC_TTY_NAME_MAX, ret);
591 ASSERT_GT(ret, 0);
592 verifySysconf(_SC_THREADS, ret);
593 verifySysconf(_SC_THREAD_PRIO_INHERIT, ret);
594 verifySysconf(_SC_THREAD_PRIO_PROTECT, ret);
595 verifySysconf(_SC_NPROCESSORS_CONF, ret);
596 verifySysconf(_SC_NPROCESSORS_ONLN, ret);
597 verifySysconf(_SC_PHYS_PAGES, ret);
598 verifySysconf(_SC_AVPHYS_PAGES, ret);
599
600 // TODO: keep following names check here
601 // so that we can enable them when the error fixed
602
603 /* when call sysconf with following name,
604 the system call will report error "Function not implemented"
605
606 verifySysconf(_SC_BC_BASE_MAX, ret);
607 verifySysconf(_SC_BC_DIM_MAX, ret);
608 verifySysconf(_SC_BC_SCALE_MAX, ret);
609 verifySysconf(_SC_BC_STRING_MAX, ret);
610 verifySysconf(_SC_COLL_WEIGHTS_MAX, ret);
611 verifySysconf(_SC_EXPR_NEST_MAX, ret);
612 verifySysconf(_SC_XOPEN_SHM, ret);
613 verifySysconf(_SC_XBS5_ILP32_OFF32, ret);
614 verifySysconf(_SC_XBS5_ILP32_OFFBIG, ret);
615 verifySysconf(_SC_XBS5_LP64_OFF64, ret);
616 verifySysconf(_SC_XBS5_LPBIG_OFFBIG, ret);
617 verifySysconf(_SC_AIO_LISTIO_MAX, ret);
618 verifySysconf(_SC_AIO_MAX, ret);
619 verifySysconf(_SC_AIO_PRIO_DELTA_MAX, ret);
620 verifySysconf(_SC_ASYNCHRONOUS_IO, ret);
621 verifySysconf(_SC_MEMLOCK, ret);
622 verifySysconf(_SC_MEMLOCK_RANGE, ret);
623 verifySysconf(_SC_MEMORY_PROTECTION, ret);
624 verifySysconf(_SC_MESSAGE_PASSING, ret);
625 verifySysconf(_SC_PRIORITIZED_IO, ret);
626 verifySysconf(_SC_SHARED_MEMORY_OBJECTS, ret);
627 verifySysconf(_SC_THREAD_SAFE_FUNCTIONS, ret);
628
629 */
630
631 /* when following names are checked,
632 the return value of sysconf is -1,
633 which means following name are invalid.
634
635 verifySysconf(_SC_2_C_DEV, ret);
636 verifySysconf(_SC_2_FORT_DEV, ret);
637 verifySysconf(_SC_2_FORT_RUN, ret);
638 verifySysconf(_SC_2_LOCALEDEF, ret);
639 verifySysconf(_SC_2_SW_DEV, ret);
640 verifySysconf(_SC_2_UPE, ret);
641 verifySysconf(_SC_2_VERSION, ret);
642 verifySysconf(_SC_XOPEN_CRYPT, ret);
643 verifySysconf(_SC_XOPEN_ENH_I18N, ret);
644 verifySysconf(_SC_XOPEN_XCU_VERSION, ret);
645 verifySysconf(_SC_XOPEN_REALTIME, ret);
646 verifySysconf(_SC_XOPEN_REALTIME_THREADS, ret);
647 verifySysconf(_SC_XOPEN_LEGACY, ret);
648 verifySysconf(_SC_THREAD_ATTR_STACKADDR, ret);
649 verifySysconf(_SC_THREAD_ATTR_STACKSIZE, ret);
650 verifySysconf(_SC_REALTIME_SIGNALS, ret);
651
652 */
653}