Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 18 | #include "ScopedSignalHandler.h" |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 19 | #include "TemporaryFile.h" |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 20 | |
Elliott Hughes | 915fefb | 2014-02-18 12:34:51 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Colin Cross | 3d19a83 | 2014-02-14 18:56:23 -0800 | [diff] [blame] | 22 | #include <fcntl.h> |
Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 23 | #include <stdint.h> |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
| 26 | TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) { |
| 27 | ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0); |
| 28 | } |
Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 29 | |
| 30 | TEST(unistd, sbrk) { |
| 31 | void* initial_break = sbrk(0); |
| 32 | |
| 33 | void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 2000); |
| 34 | ASSERT_EQ(0, brk(new_break)); |
| 35 | |
| 36 | void* final_break = sbrk(0); |
| 37 | ASSERT_EQ(final_break, new_break); |
| 38 | } |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 39 | |
| 40 | TEST(unistd, truncate) { |
| 41 | TemporaryFile tf; |
| 42 | ASSERT_EQ(0, close(tf.fd)); |
| 43 | ASSERT_EQ(0, truncate(tf.filename, 123)); |
| 44 | |
| 45 | struct stat sb; |
| 46 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
| 47 | ASSERT_EQ(123, sb.st_size); |
| 48 | } |
| 49 | |
| 50 | TEST(unistd, truncate64) { |
| 51 | TemporaryFile tf; |
| 52 | ASSERT_EQ(0, close(tf.fd)); |
| 53 | ASSERT_EQ(0, truncate64(tf.filename, 123)); |
| 54 | |
| 55 | struct stat sb; |
| 56 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
| 57 | ASSERT_EQ(123, sb.st_size); |
| 58 | } |
| 59 | |
| 60 | TEST(unistd, ftruncate) { |
| 61 | TemporaryFile tf; |
| 62 | ASSERT_EQ(0, ftruncate(tf.fd, 123)); |
| 63 | ASSERT_EQ(0, close(tf.fd)); |
| 64 | |
| 65 | struct stat sb; |
| 66 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
| 67 | ASSERT_EQ(123, sb.st_size); |
| 68 | } |
| 69 | |
| 70 | TEST(unistd, ftruncate64) { |
| 71 | TemporaryFile tf; |
| 72 | ASSERT_EQ(0, ftruncate64(tf.fd, 123)); |
| 73 | ASSERT_EQ(0, close(tf.fd)); |
| 74 | |
| 75 | struct stat sb; |
| 76 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
| 77 | ASSERT_EQ(123, sb.st_size); |
| 78 | } |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 79 | |
| 80 | static bool gPauseTestFlag = false; |
| 81 | static void PauseTestSignalHandler(int) { |
| 82 | gPauseTestFlag = true; |
| 83 | } |
| 84 | |
| 85 | TEST(unistd, pause) { |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 86 | ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler); |
| 87 | |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 88 | alarm(1); |
| 89 | ASSERT_FALSE(gPauseTestFlag); |
| 90 | ASSERT_EQ(-1, pause()); |
| 91 | ASSERT_TRUE(gPauseTestFlag); |
| 92 | } |
Colin Cross | 3d19a83 | 2014-02-14 18:56:23 -0800 | [diff] [blame] | 93 | |
| 94 | TEST(unistd, read) { |
| 95 | int fd = open("/proc/version", O_RDONLY); |
| 96 | ASSERT_TRUE(fd != -1); |
| 97 | |
| 98 | char buf[5]; |
| 99 | ASSERT_EQ(5, read(fd, buf, 5)); |
| 100 | ASSERT_EQ(buf[0], 'L'); |
| 101 | ASSERT_EQ(buf[1], 'i'); |
| 102 | ASSERT_EQ(buf[2], 'n'); |
| 103 | ASSERT_EQ(buf[3], 'u'); |
| 104 | ASSERT_EQ(buf[4], 'x'); |
| 105 | close(fd); |
| 106 | } |
| 107 | |
| 108 | TEST(unistd, read_EBADF) { |
| 109 | // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that |
| 110 | // our syscall stubs correctly return a 64-bit -1. |
| 111 | char buf[1]; |
| 112 | ASSERT_EQ(-1, read(-1, buf, sizeof(buf))); |
| 113 | ASSERT_EQ(EBADF, errno); |
| 114 | } |
Elliott Hughes | aedb00d | 2014-03-03 14:38:20 -0800 | [diff] [blame] | 115 | |
| 116 | TEST(unistd, alarm) { |
| 117 | ASSERT_EQ(0U, alarm(0)); |
| 118 | } |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame^] | 119 | |
| 120 | TEST(unistd, _exit) { |
| 121 | int pid = fork(); |
| 122 | ASSERT_NE(-1, pid) << strerror(errno); |
| 123 | |
| 124 | if (pid == 0) { |
| 125 | _exit(99); |
| 126 | } |
| 127 | |
| 128 | int status; |
| 129 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 130 | ASSERT_TRUE(WIFEXITED(status)); |
| 131 | ASSERT_EQ(99, WEXITSTATUS(status)); |
| 132 | } |