blob: 70b23bd75e70902c2af9f5bd4bf87e77c81e08c9 [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>
Christopher Ferris13613132013-10-28 15:24:04 -070018#include "ScopedSignalHandler.h"
Elliott Hughesb4f76162013-09-19 16:27:24 -070019#include "TemporaryFile.h"
Elliott Hughesa55f6302013-01-02 14:23:43 -080020
Elliott Hughes915fefb2014-02-18 12:34:51 -080021#include <errno.h>
Colin Cross3d19a832014-02-14 18:56:23 -080022#include <fcntl.h>
Elliott Hughes428f5562013-02-05 16:10:59 -080023#include <stdint.h>
Elliott Hughesa55f6302013-01-02 14:23:43 -080024#include <unistd.h>
Elliott Hughes764a9932014-04-08 19:44:36 -070025#include <sys/types.h>
26#include <sys/wait.h>
Elliott Hughesa55f6302013-01-02 14:23:43 -080027
28TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) {
29 ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0);
30}
Elliott Hughes428f5562013-02-05 16:10:59 -080031
32TEST(unistd, sbrk) {
33 void* initial_break = sbrk(0);
34
35 void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 2000);
36 ASSERT_EQ(0, brk(new_break));
37
38 void* final_break = sbrk(0);
39 ASSERT_EQ(final_break, new_break);
40}
Elliott Hughesb4f76162013-09-19 16:27:24 -070041
42TEST(unistd, truncate) {
43 TemporaryFile tf;
44 ASSERT_EQ(0, close(tf.fd));
45 ASSERT_EQ(0, truncate(tf.filename, 123));
46
47 struct stat sb;
48 ASSERT_EQ(0, stat(tf.filename, &sb));
49 ASSERT_EQ(123, sb.st_size);
50}
51
52TEST(unistd, truncate64) {
53 TemporaryFile tf;
54 ASSERT_EQ(0, close(tf.fd));
55 ASSERT_EQ(0, truncate64(tf.filename, 123));
56
57 struct stat sb;
58 ASSERT_EQ(0, stat(tf.filename, &sb));
59 ASSERT_EQ(123, sb.st_size);
60}
61
62TEST(unistd, ftruncate) {
63 TemporaryFile tf;
64 ASSERT_EQ(0, ftruncate(tf.fd, 123));
65 ASSERT_EQ(0, close(tf.fd));
66
67 struct stat sb;
68 ASSERT_EQ(0, stat(tf.filename, &sb));
69 ASSERT_EQ(123, sb.st_size);
70}
71
72TEST(unistd, ftruncate64) {
73 TemporaryFile tf;
74 ASSERT_EQ(0, ftruncate64(tf.fd, 123));
75 ASSERT_EQ(0, close(tf.fd));
76
77 struct stat sb;
78 ASSERT_EQ(0, stat(tf.filename, &sb));
79 ASSERT_EQ(123, sb.st_size);
80}
Elliott Hughes11952072013-10-24 15:15:14 -070081
82static bool gPauseTestFlag = false;
83static void PauseTestSignalHandler(int) {
84 gPauseTestFlag = true;
85}
86
87TEST(unistd, pause) {
Christopher Ferris13613132013-10-28 15:24:04 -070088 ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
89
Elliott Hughes11952072013-10-24 15:15:14 -070090 alarm(1);
91 ASSERT_FALSE(gPauseTestFlag);
92 ASSERT_EQ(-1, pause());
93 ASSERT_TRUE(gPauseTestFlag);
94}
Colin Cross3d19a832014-02-14 18:56:23 -080095
96TEST(unistd, read) {
97 int fd = open("/proc/version", O_RDONLY);
98 ASSERT_TRUE(fd != -1);
99
100 char buf[5];
101 ASSERT_EQ(5, read(fd, buf, 5));
102 ASSERT_EQ(buf[0], 'L');
103 ASSERT_EQ(buf[1], 'i');
104 ASSERT_EQ(buf[2], 'n');
105 ASSERT_EQ(buf[3], 'u');
106 ASSERT_EQ(buf[4], 'x');
107 close(fd);
108}
109
110TEST(unistd, read_EBADF) {
111 // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that
112 // our syscall stubs correctly return a 64-bit -1.
113 char buf[1];
114 ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
115 ASSERT_EQ(EBADF, errno);
116}
Elliott Hughesaedb00d2014-03-03 14:38:20 -0800117
118TEST(unistd, alarm) {
119 ASSERT_EQ(0U, alarm(0));
120}
Elliott Hughes9f525642014-04-08 17:14:01 -0700121
122TEST(unistd, _exit) {
123 int pid = fork();
124 ASSERT_NE(-1, pid) << strerror(errno);
125
126 if (pid == 0) {
127 _exit(99);
128 }
129
130 int status;
131 ASSERT_EQ(pid, waitpid(pid, &status, 0));
132 ASSERT_TRUE(WIFEXITED(status));
133 ASSERT_EQ(99, WEXITSTATUS(status));
134}
Grigoriy Kraynovcbf6df02013-09-17 15:44:22 +0400135
136TEST(unistd, getenv_unsetenv) {
137 ASSERT_EQ(0, setenv("test-variable", "hello", 1));
138 ASSERT_STREQ("hello", getenv("test-variable"));
139 ASSERT_EQ(0, unsetenv("test-variable"));
140 ASSERT_TRUE(getenv("test-variable") == NULL);
141}
142
143TEST(unistd, unsetenv_EINVAL) {
144 EXPECT_EQ(-1, unsetenv(NULL));
145 EXPECT_EQ(EINVAL, errno);
146 EXPECT_EQ(-1, unsetenv(""));
147 EXPECT_EQ(EINVAL, errno);
148 EXPECT_EQ(-1, unsetenv("a=b"));
149 EXPECT_EQ(EINVAL, errno);
150}
151
152TEST(unistd, setenv_EINVAL) {
153 EXPECT_EQ(-1, setenv(NULL, "value", 0));
154 EXPECT_EQ(EINVAL, errno);
155 EXPECT_EQ(-1, setenv(NULL, "value", 1));
156 EXPECT_EQ(EINVAL, errno);
157 EXPECT_EQ(-1, setenv("", "value", 0));
158 EXPECT_EQ(EINVAL, errno);
159 EXPECT_EQ(-1, setenv("", "value", 1));
160 EXPECT_EQ(EINVAL, errno);
161 EXPECT_EQ(-1, setenv("a=b", "value", 0));
162 EXPECT_EQ(EINVAL, errno);
163 EXPECT_EQ(-1, setenv("a=b", "value", 1));
164 EXPECT_EQ(EINVAL, errno);
165}
166
167TEST(unistd, setenv) {
168 ASSERT_EQ(0, unsetenv("test-variable"));
169
170 char a[] = "a";
171 char b[] = "b";
172 char c[] = "c";
173
174 // New value.
175 EXPECT_EQ(0, setenv("test-variable", a, 0));
176 EXPECT_STREQ(a, getenv("test-variable"));
177
178 // Existing value, no overwrite.
179 EXPECT_EQ(0, setenv("test-variable", b, 0));
180 EXPECT_STREQ(a, getenv("test-variable"));
181
182 // Existing value, overwrite.
183 EXPECT_EQ(0, setenv("test-variable", c, 1));
184 EXPECT_STREQ(c, getenv("test-variable"));
185 // But the arrays backing the values are unchanged.
186 EXPECT_EQ('a', a[0]);
187 EXPECT_EQ('b', b[0]);
188 EXPECT_EQ('c', c[0]);
189
190 ASSERT_EQ(0, unsetenv("test-variable"));
191}
192
193TEST(unistd, putenv) {
194 ASSERT_EQ(0, unsetenv("a"));
195
196 char* s1 = strdup("a=b");
197 ASSERT_EQ(0, putenv(s1));
198
199 ASSERT_STREQ("b", getenv("a"));
200 s1[2] = 'c';
201 ASSERT_STREQ("c", getenv("a"));
202
203 char* s2 = strdup("a=b");
204 ASSERT_EQ(0, putenv(s2));
205
206 ASSERT_STREQ("b", getenv("a"));
207 ASSERT_EQ('c', s1[2]);
208
209 ASSERT_EQ(0, unsetenv("a"));
210 free(s1);
211 free(s2);
212}
213
214TEST(unistd, clearenv) {
215 extern char** environ;
216
217 // Guarantee that environ is not initially empty...
218 ASSERT_EQ(0, setenv("test-variable", "a", 1));
219
220 // Stash a copy.
221 std::vector<char*> old_environ;
222 for (size_t i = 0; environ[i] != NULL; ++i) {
223 old_environ.push_back(strdup(environ[i]));
224 }
225
226 ASSERT_EQ(0, clearenv());
227
228 EXPECT_TRUE(environ == NULL || environ[0] == NULL);
229 EXPECT_EQ(NULL, getenv("test-variable"));
230 EXPECT_EQ(0, setenv("test-variable", "post-clear", 1));
231 EXPECT_STREQ("post-clear", getenv("test-variable"));
232
233 // Put the old environment back.
234 for (size_t i = 0; i < old_environ.size(); ++i) {
235 EXPECT_EQ(0, putenv(old_environ[i]));
236 }
237
238 // Check it wasn't overwritten.
239 EXPECT_STREQ("a", getenv("test-variable"));
240
241 EXPECT_EQ(0, unsetenv("test-variable"));
242}