]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - tests/unistd_test.cpp
Merge "Allow stdatomic.h to be included from mingw prebuilt."
[android-sdk/platform-bionic.git] / tests / unistd_test.cpp
1 /*
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  */
17 #include <gtest/gtest.h>
18 #include "BionicDeathTest.h"
19 #include "ScopedSignalHandler.h"
20 #include "TemporaryFile.h"
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdint.h>
26 #include <sys/syscall.h>
27 #include <sys/types.h>
28 #include <sys/utsname.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
32 TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) {
33   ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0);
34 }
36 static void* get_brk() {
37   return sbrk(0);
38 }
40 static void* page_align(uintptr_t addr) {
41   uintptr_t mask = sysconf(_SC_PAGE_SIZE) - 1;
42   return reinterpret_cast<void*>((addr + mask) & ~mask);
43 }
45 TEST(unistd, brk) {
46   void* initial_break = get_brk();
48   // The kernel aligns the break to a page.
49   void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 1);
50   ASSERT_EQ(0, brk(new_break));
51   ASSERT_GE(get_brk(), new_break);
53   new_break = page_align(reinterpret_cast<uintptr_t>(initial_break) + sysconf(_SC_PAGE_SIZE));
54   ASSERT_EQ(0, brk(new_break));
55   ASSERT_EQ(get_brk(), new_break);
56 }
58 TEST(unistd, brk_ENOMEM) {
59   ASSERT_EQ(-1, brk(reinterpret_cast<void*>(-1)));
60   ASSERT_EQ(ENOMEM, errno);
61 }
63 #if defined(__GLIBC__)
64 #define SBRK_MIN INTPTR_MIN
65 #define SBRK_MAX INTPTR_MAX
66 #else
67 #define SBRK_MIN PTRDIFF_MIN
68 #define SBRK_MAX PTRDIFF_MAX
69 #endif
71 TEST(unistd, sbrk_ENOMEM) {
72 #if defined(__BIONIC__) && !defined(__LP64__)
73   // There is no way to guarantee that all overflow conditions can be tested
74   // without manipulating the underlying values of the current break.
75   extern void* __bionic_brk;
77   class ScopedBrk {
78   public:
79     ScopedBrk() : saved_brk_(__bionic_brk) {}
80     virtual ~ScopedBrk() { __bionic_brk = saved_brk_; }
82   private:
83     void* saved_brk_;
84   };
86   ScopedBrk scope_brk;
88   // Set the current break to a point that will cause an overflow.
89   __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) + 2);
91   // Can't increase by so much that we'd overflow.
92   ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MAX));
93   ASSERT_EQ(ENOMEM, errno);
95   // Set the current break to a point that will cause an overflow.
96   __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX));
98   ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN));
99   ASSERT_EQ(ENOMEM, errno);
101   __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) - 1);
103   ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN + 1));
104   ASSERT_EQ(ENOMEM, errno);
105 #else
106   class ScopedBrk {
107   public:
108     ScopedBrk() : saved_brk_(get_brk()) {}
109     virtual ~ScopedBrk() { brk(saved_brk_); }
111   private:
112     void* saved_brk_;
113   };
115   ScopedBrk scope_brk;
117   uintptr_t cur_brk = reinterpret_cast<uintptr_t>(get_brk());
118   if (cur_brk < static_cast<uintptr_t>(-(SBRK_MIN+1))) {
119     // Do the overflow test for a max negative increment.
120     ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MIN));
121 #if defined(__BIONIC__)
122     // GLIBC does not set errno in overflow case.
123     ASSERT_EQ(ENOMEM, errno);
124 #endif
125   }
127   uintptr_t overflow_brk = static_cast<uintptr_t>(SBRK_MAX) + 2;
128   if (cur_brk < overflow_brk) {
129     // Try and move the value to PTRDIFF_MAX + 2.
130     cur_brk = reinterpret_cast<uintptr_t>(sbrk(overflow_brk));
131   }
132   if (cur_brk >= overflow_brk) {
133     ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MAX));
134 #if defined(__BIONIC__)
135     // GLIBC does not set errno in overflow case.
136     ASSERT_EQ(ENOMEM, errno);
137 #endif
138   }
139 #endif
142 TEST(unistd, truncate) {
143   TemporaryFile tf;
144   ASSERT_EQ(0, close(tf.fd));
145   ASSERT_EQ(0, truncate(tf.filename, 123));
147   struct stat sb;
148   ASSERT_EQ(0, stat(tf.filename, &sb));
149   ASSERT_EQ(123, sb.st_size);
152 TEST(unistd, truncate64) {
153   TemporaryFile tf;
154   ASSERT_EQ(0, close(tf.fd));
155   ASSERT_EQ(0, truncate64(tf.filename, 123));
157   struct stat sb;
158   ASSERT_EQ(0, stat(tf.filename, &sb));
159   ASSERT_EQ(123, sb.st_size);
162 TEST(unistd, ftruncate) {
163   TemporaryFile tf;
164   ASSERT_EQ(0, ftruncate(tf.fd, 123));
165   ASSERT_EQ(0, close(tf.fd));
167   struct stat sb;
168   ASSERT_EQ(0, stat(tf.filename, &sb));
169   ASSERT_EQ(123, sb.st_size);
172 TEST(unistd, ftruncate64) {
173   TemporaryFile tf;
174   ASSERT_EQ(0, ftruncate64(tf.fd, 123));
175   ASSERT_EQ(0, close(tf.fd));
177   struct stat sb;
178   ASSERT_EQ(0, stat(tf.filename, &sb));
179   ASSERT_EQ(123, sb.st_size);
182 static bool g_pause_test_flag = false;
183 static void PauseTestSignalHandler(int) {
184   g_pause_test_flag = true;
187 TEST(unistd, pause) {
188   ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
190   alarm(1);
191   ASSERT_FALSE(g_pause_test_flag);
192   ASSERT_EQ(-1, pause());
193   ASSERT_TRUE(g_pause_test_flag);
196 TEST(unistd, read) {
197   int fd = open("/proc/version", O_RDONLY);
198   ASSERT_TRUE(fd != -1);
200   char buf[5];
201   ASSERT_EQ(5, read(fd, buf, 5));
202   ASSERT_EQ(buf[0], 'L');
203   ASSERT_EQ(buf[1], 'i');
204   ASSERT_EQ(buf[2], 'n');
205   ASSERT_EQ(buf[3], 'u');
206   ASSERT_EQ(buf[4], 'x');
207   close(fd);
210 TEST(unistd, read_EBADF) {
211   // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that
212   // our syscall stubs correctly return a 64-bit -1.
213   char buf[1];
214   ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
215   ASSERT_EQ(EBADF, errno);
218 TEST(unistd, syscall_long) {
219   // Check that syscall(3) correctly returns long results.
220   // https://code.google.com/p/android/issues/detail?id=73952
221   // We assume that the break is > 4GiB, but this is potentially flaky.
222   uintptr_t p = reinterpret_cast<uintptr_t>(sbrk(0));
223   ASSERT_EQ(p, static_cast<uintptr_t>(syscall(__NR_brk, 0)));
226 TEST(unistd, alarm) {
227   ASSERT_EQ(0U, alarm(0));
230 TEST(unistd, _exit) {
231   int pid = fork();
232   ASSERT_NE(-1, pid) << strerror(errno);
234   if (pid == 0) {
235     _exit(99);
236   }
238   int status;
239   ASSERT_EQ(pid, waitpid(pid, &status, 0));
240   ASSERT_TRUE(WIFEXITED(status));
241   ASSERT_EQ(99, WEXITSTATUS(status));
244 TEST(unistd, getenv_unsetenv) {
245   ASSERT_EQ(0, setenv("test-variable", "hello", 1));
246   ASSERT_STREQ("hello", getenv("test-variable"));
247   ASSERT_EQ(0, unsetenv("test-variable"));
248   ASSERT_TRUE(getenv("test-variable") == NULL);
251 TEST(unistd, unsetenv_EINVAL) {
252   EXPECT_EQ(-1, unsetenv(""));
253   EXPECT_EQ(EINVAL, errno);
254   EXPECT_EQ(-1, unsetenv("a=b"));
255   EXPECT_EQ(EINVAL, errno);
258 TEST(unistd, setenv_EINVAL) {
259   EXPECT_EQ(-1, setenv(NULL, "value", 0));
260   EXPECT_EQ(EINVAL, errno);
261   EXPECT_EQ(-1, setenv(NULL, "value", 1));
262   EXPECT_EQ(EINVAL, errno);
263   EXPECT_EQ(-1, setenv("", "value", 0));
264   EXPECT_EQ(EINVAL, errno);
265   EXPECT_EQ(-1, setenv("", "value", 1));
266   EXPECT_EQ(EINVAL, errno);
267   EXPECT_EQ(-1, setenv("a=b", "value", 0));
268   EXPECT_EQ(EINVAL, errno);
269   EXPECT_EQ(-1, setenv("a=b", "value", 1));
270   EXPECT_EQ(EINVAL, errno);
273 TEST(unistd, setenv) {
274   ASSERT_EQ(0, unsetenv("test-variable"));
276   char a[] = "a";
277   char b[] = "b";
278   char c[] = "c";
280   // New value.
281   EXPECT_EQ(0, setenv("test-variable", a, 0));
282   EXPECT_STREQ(a, getenv("test-variable"));
284   // Existing value, no overwrite.
285   EXPECT_EQ(0, setenv("test-variable", b, 0));
286   EXPECT_STREQ(a, getenv("test-variable"));
288   // Existing value, overwrite.
289   EXPECT_EQ(0, setenv("test-variable", c, 1));
290   EXPECT_STREQ(c, getenv("test-variable"));
291   // But the arrays backing the values are unchanged.
292   EXPECT_EQ('a', a[0]);
293   EXPECT_EQ('b', b[0]);
294   EXPECT_EQ('c', c[0]);
296   ASSERT_EQ(0, unsetenv("test-variable"));
299 TEST(unistd, putenv) {
300   ASSERT_EQ(0, unsetenv("a"));
302   char* s1 = strdup("a=b");
303   ASSERT_EQ(0, putenv(s1));
305   ASSERT_STREQ("b", getenv("a"));
306   s1[2] = 'c';
307   ASSERT_STREQ("c", getenv("a"));
309   char* s2 = strdup("a=b");
310   ASSERT_EQ(0, putenv(s2));
312   ASSERT_STREQ("b", getenv("a"));
313   ASSERT_EQ('c', s1[2]);
315   ASSERT_EQ(0, unsetenv("a"));
316   free(s1);
317   free(s2);
320 TEST(unistd, clearenv) {
321   extern char** environ;
323   // Guarantee that environ is not initially empty...
324   ASSERT_EQ(0, setenv("test-variable", "a", 1));
326   // Stash a copy.
327   std::vector<char*> old_environ;
328   for (size_t i = 0; environ[i] != NULL; ++i) {
329     old_environ.push_back(strdup(environ[i]));
330   }
332   ASSERT_EQ(0, clearenv());
334   EXPECT_TRUE(environ == NULL || environ[0] == NULL);
335   EXPECT_EQ(NULL, getenv("test-variable"));
336   EXPECT_EQ(0, setenv("test-variable", "post-clear", 1));
337   EXPECT_STREQ("post-clear", getenv("test-variable"));
339   // Put the old environment back.
340   for (size_t i = 0; i < old_environ.size(); ++i) {
341     EXPECT_EQ(0, putenv(old_environ[i]));
342   }
344   // Check it wasn't overwritten.
345   EXPECT_STREQ("a", getenv("test-variable"));
347   EXPECT_EQ(0, unsetenv("test-variable"));
350 static void TestFsyncFunction(int (*fn)(int)) {
351   int fd;
353   // Can't sync an invalid fd.
354   errno = 0;
355   EXPECT_EQ(-1, fn(-1));
356   EXPECT_EQ(EBADF, errno);
358   // It doesn't matter whether you've opened a file for write or not.
359   TemporaryFile tf;
360   ASSERT_NE(-1, tf.fd);
362   EXPECT_EQ(0, fn(tf.fd));
364   ASSERT_NE(-1, fd = open(tf.filename, O_RDONLY));
365   EXPECT_EQ(0, fn(fd));
366   close(fd);
368   ASSERT_NE(-1, fd = open(tf.filename, O_RDWR));
369   EXPECT_EQ(0, fn(fd));
370   close(fd);
372   // The fd can even be a directory.
373   ASSERT_NE(-1, fd = open("/", O_RDONLY));
374   EXPECT_EQ(0, fn(fd));
375   close(fd);
377   // But some file systems may choose to be fussy...
378   errno = 0;
379   ASSERT_NE(-1, fd = open("/proc/version", O_RDONLY));
380   EXPECT_EQ(-1, fn(fd));
381   EXPECT_EQ(EINVAL, errno);
382   close(fd);
385 TEST(unistd, fdatasync) {
386   TestFsyncFunction(fdatasync);
389 TEST(unistd, fsync) {
390   TestFsyncFunction(fsync);
393 static void AssertGetPidCorrect() {
394   // The loop is just to make manual testing/debugging with strace easier.
395   pid_t getpid_syscall_result = syscall(__NR_getpid);
396   for (size_t i = 0; i < 128; ++i) {
397     ASSERT_EQ(getpid_syscall_result, getpid());
398   }
401 TEST(unistd, getpid_caching_and_fork) {
402   pid_t parent_pid = getpid();
403   ASSERT_EQ(syscall(__NR_getpid), parent_pid);
405   pid_t fork_result = fork();
406   ASSERT_NE(fork_result, -1);
407   if (fork_result == 0) {
408     // We're the child.
409     AssertGetPidCorrect();
410     ASSERT_EQ(parent_pid, getppid());
411     _exit(123);
412   } else {
413     // We're the parent.
414     ASSERT_EQ(parent_pid, getpid());
416     int status;
417     ASSERT_EQ(fork_result, waitpid(fork_result, &status, 0));
418     ASSERT_TRUE(WIFEXITED(status));
419     ASSERT_EQ(123, WEXITSTATUS(status));
420   }
423 static int GetPidCachingCloneStartRoutine(void*) {
424   AssertGetPidCorrect();
425   return 123;
428 TEST(unistd, getpid_caching_and_clone) {
429   pid_t parent_pid = getpid();
430   ASSERT_EQ(syscall(__NR_getpid), parent_pid);
432   void* child_stack[1024];
433   int clone_result = clone(GetPidCachingCloneStartRoutine, &child_stack[1024], CLONE_NEWNS | SIGCHLD, NULL);
434   if (clone_result == -1 && errno == EPERM && getuid() != 0) {
435     GTEST_LOG_(INFO) << "This test only works if you have permission to CLONE_NEWNS; try running as root.\n";
436     return;
437   }
438   ASSERT_NE(clone_result, -1);
440   ASSERT_EQ(parent_pid, getpid());
442   int status;
443   ASSERT_EQ(clone_result, waitpid(clone_result, &status, 0));
444   ASSERT_TRUE(WIFEXITED(status));
445   ASSERT_EQ(123, WEXITSTATUS(status));
448 static void* GetPidCachingPthreadStartRoutine(void*) {
449   AssertGetPidCorrect();
450   return NULL;
453 TEST(unistd, getpid_caching_and_pthread_create) {
454   pid_t parent_pid = getpid();
456   pthread_t t;
457   ASSERT_EQ(0, pthread_create(&t, NULL, GetPidCachingPthreadStartRoutine, NULL));
459   ASSERT_EQ(parent_pid, getpid());
461   void* result;
462   ASSERT_EQ(0, pthread_join(t, &result));
463   ASSERT_EQ(NULL, result);
466 class unistd_DeathTest : public BionicDeathTest {};
468 TEST_F(unistd_DeathTest, abort) {
469   ASSERT_EXIT(abort(), testing::KilledBySignal(SIGABRT), "");
472 TEST(unistd, sethostname) {
473   // The permissions check happens before the argument check, so this will
474   // fail for a different reason if you're running as root than if you're
475   // not, but it'll fail either way. Checking that we have the symbol is about
476   // all we can do for sethostname(2).
477   ASSERT_EQ(-1, sethostname("", -1));
480 TEST(unistd, gethostname) {
481   char hostname[HOST_NAME_MAX + 1];
482   memset(hostname, 0, sizeof(hostname));
484   // Can we get the hostname with a big buffer?
485   ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
487   // Can we get the hostname with a right-sized buffer?
488   errno = 0;
489   ASSERT_EQ(0, gethostname(hostname, strlen(hostname) + 1));
491   // Does uname(2) agree?
492   utsname buf;
493   ASSERT_EQ(0, uname(&buf));
494   ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
495   ASSERT_GT(strlen(hostname), 0U);
497   // Do we correctly detect truncation?
498   errno = 0;
499   ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
500   ASSERT_EQ(ENAMETOOLONG, errno);