blob: e814ef714395d30e9d6f77881a0766240dc34efd [file] [log] [blame]
Elliott Hughes774c7f52012-10-01 13:11:03 -07001/*
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>
Calin Juravlefe317a32014-02-21 15:11:03 +000018#include "TemporaryFile.h"
Elliott Hughes774c7f52012-10-01 13:11:03 -070019
Elliott Hughesb16b7222013-02-04 13:18:00 -080020#include <errno.h>
Elliott Hughesf0777842013-03-01 16:59:46 -080021#include <libgen.h>
22#include <limits.h>
Elliott Hughes877ec6d2013-11-15 17:40:18 -080023#include <pthread.h>
Elliott Hughesb16b7222013-02-04 13:18:00 -080024#include <stdint.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070025#include <stdlib.h>
Calin Juravlefe317a32014-02-21 15:11:03 +000026#include <fcntl.h>
Elliott Hughes40488562014-03-12 13:50:38 -070027#include <sys/types.h>
28#include <sys/wait.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070029
30TEST(stdlib, drand48) {
31 srand48(0x01020304);
32 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
33 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
34 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
35 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
36}
37
Elliott Hughesa0beeea2014-06-12 11:48:04 -070038TEST(stdlib, lrand48) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070039 srand48(0x01020304);
40 EXPECT_EQ(1409163720, lrand48());
41 EXPECT_EQ(397769746, lrand48());
42 EXPECT_EQ(902267124, lrand48());
43 EXPECT_EQ(132366131, lrand48());
Elliott Hughesa0beeea2014-06-12 11:48:04 -070044}
Elliott Hughes774c7f52012-10-01 13:11:03 -070045
Elliott Hughesa0beeea2014-06-12 11:48:04 -070046TEST(stdlib, random) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070047 srandom(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -070048 EXPECT_EQ(55436735, random());
49 EXPECT_EQ(1399865117, random());
50 EXPECT_EQ(2032643283, random());
51 EXPECT_EQ(571329216, random());
52}
Elliott Hughes774c7f52012-10-01 13:11:03 -070053
Elliott Hughesa0beeea2014-06-12 11:48:04 -070054TEST(stdlib, rand) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070055 srand(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -070056 EXPECT_EQ(55436735, rand());
57 EXPECT_EQ(1399865117, rand());
58 EXPECT_EQ(2032643283, rand());
59 EXPECT_EQ(571329216, rand());
Elliott Hughes774c7f52012-10-01 13:11:03 -070060}
61
62TEST(stdlib, mrand48) {
63 srand48(0x01020304);
64 EXPECT_EQ(-1476639856, mrand48());
65 EXPECT_EQ(795539493, mrand48());
66 EXPECT_EQ(1804534249, mrand48());
67 EXPECT_EQ(264732262, mrand48());
68}
Elliott Hughesb16b7222013-02-04 13:18:00 -080069
70TEST(stdlib, posix_memalign) {
71 void* p;
72
73 ASSERT_EQ(0, posix_memalign(&p, 512, 128));
74 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512);
75 free(p);
76
77 // Can't align to a non-power of 2.
78 ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128));
79}
Elliott Hughesf0777842013-03-01 16:59:46 -080080
81TEST(stdlib, realpath__NULL_filename) {
82 errno = 0;
83 char* p = realpath(NULL, NULL);
84 ASSERT_TRUE(p == NULL);
85 ASSERT_EQ(EINVAL, errno);
86}
87
88TEST(stdlib, realpath__empty_filename) {
89 errno = 0;
90 char* p = realpath("", NULL);
91 ASSERT_TRUE(p == NULL);
92 ASSERT_EQ(ENOENT, errno);
93}
94
95TEST(stdlib, realpath__ENOENT) {
96 errno = 0;
97 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
98 ASSERT_TRUE(p == NULL);
99 ASSERT_EQ(ENOENT, errno);
100}
101
102TEST(stdlib, realpath) {
103 // Get the name of this executable.
104 char executable_path[PATH_MAX];
105 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
106 ASSERT_NE(rc, -1);
107 executable_path[rc] = '\0';
108
109 char buf[PATH_MAX + 1];
110 char* p = realpath("/proc/self/exe", buf);
111 ASSERT_STREQ(executable_path, p);
112
113 p = realpath("/proc/self/exe", NULL);
114 ASSERT_STREQ(executable_path, p);
115 free(p);
116}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700117
118TEST(stdlib, qsort) {
119 struct s {
120 char name[16];
121 static int comparator(const void* lhs, const void* rhs) {
122 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
123 }
124 };
125 s entries[3];
126 strcpy(entries[0].name, "charlie");
127 strcpy(entries[1].name, "bravo");
128 strcpy(entries[2].name, "alpha");
129
130 qsort(entries, 3, sizeof(s), s::comparator);
131 ASSERT_STREQ("alpha", entries[0].name);
132 ASSERT_STREQ("bravo", entries[1].name);
133 ASSERT_STREQ("charlie", entries[2].name);
134
135 qsort(entries, 3, sizeof(s), s::comparator);
136 ASSERT_STREQ("alpha", entries[0].name);
137 ASSERT_STREQ("bravo", entries[1].name);
138 ASSERT_STREQ("charlie", entries[2].name);
139}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800140
141static void* TestBug57421_child(void* arg) {
142 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
143 pthread_join(main_thread, NULL);
144 char* value = getenv("ENVIRONMENT_VARIABLE");
145 if (value == NULL) {
146 setenv("ENVIRONMENT_VARIABLE", "value", 1);
147 }
148 return NULL;
149}
150
151static void TestBug57421_main() {
152 pthread_t t;
153 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
154 pthread_exit(NULL);
155}
156
157// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
158// run this test (which exits normally) in its own process.
159TEST(stdlib_DeathTest, getenv_after_main_thread_exits) {
160 // https://code.google.com/p/android/issues/detail?id=57421
161 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
162 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
163}
Calin Juravlefe317a32014-02-21 15:11:03 +0000164
Elliott Hughes31165ed2014-09-23 17:34:29 -0700165TEST(stdlib, mkostemp64) {
166 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
167 int flags = fcntl(tf.fd, F_GETFD);
168 ASSERT_TRUE(flags != -1);
169 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
170}
171
172TEST(stdlib, mkostemp) {
173 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
174 int flags = fcntl(tf.fd, F_GETFD);
175 ASSERT_TRUE(flags != -1);
176 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
177}
178
179TEST(stdlib, mkstemp64) {
180 TemporaryFile tf(mkstemp64);
181 struct stat64 sb;
182 ASSERT_EQ(0, fstat64(tf.fd, &sb));
183 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
184}
185
Calin Juravlefe317a32014-02-21 15:11:03 +0000186TEST(stdlib, mkstemp) {
187 TemporaryFile tf;
188 struct stat sb;
189 ASSERT_EQ(0, fstat(tf.fd, &sb));
190}
191
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700192TEST(stdlib, system) {
193 int status;
194
195 status = system("exit 0");
196 ASSERT_TRUE(WIFEXITED(status));
197 ASSERT_EQ(0, WEXITSTATUS(status));
198
199 status = system("exit 1");
200 ASSERT_TRUE(WIFEXITED(status));
201 ASSERT_EQ(1, WEXITSTATUS(status));
202}
Elliott Hughes5a817382014-03-12 16:12:57 -0700203
204TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700205 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700206}
207
208TEST(stdlib, strtod) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700209 ASSERT_DOUBLE_EQ(1.23, strtod("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700210}
211
212TEST(stdlib, strtof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700213 ASSERT_FLOAT_EQ(1.23, strtof("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700214}
215
216TEST(stdlib, strtold) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700217 ASSERT_DOUBLE_EQ(1.23, strtold("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700218}
Elliott Hughes9f525642014-04-08 17:14:01 -0700219
Dan Albertb8425c52014-04-29 17:49:06 -0700220TEST(stdlib, quick_exit) {
221 pid_t pid = fork();
222 ASSERT_NE(-1, pid) << strerror(errno);
223
224 if (pid == 0) {
225 quick_exit(99);
226 }
227
228 int status;
229 ASSERT_EQ(pid, waitpid(pid, &status, 0));
230 ASSERT_TRUE(WIFEXITED(status));
231 ASSERT_EQ(99, WEXITSTATUS(status));
232}
233
234static int quick_exit_status = 0;
235
236static void quick_exit_1(void) {
237 ASSERT_EQ(quick_exit_status, 0);
238 quick_exit_status = 1;
239}
240
241static void quick_exit_2(void) {
242 ASSERT_EQ(quick_exit_status, 1);
243}
244
245static void not_run(void) {
246 FAIL();
247}
248
249TEST(stdlib, at_quick_exit) {
250 pid_t pid = fork();
251 ASSERT_NE(-1, pid) << strerror(errno);
252
253 if (pid == 0) {
254 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
255 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
256 atexit(not_run);
257 quick_exit(99);
258 }
259
260 int status;
261 ASSERT_EQ(pid, waitpid(pid, &status, 0));
262 ASSERT_TRUE(WIFEXITED(status));
263 ASSERT_EQ(99, WEXITSTATUS(status));
264}
265
Elliott Hughes9f525642014-04-08 17:14:01 -0700266TEST(unistd, _Exit) {
267 int pid = fork();
268 ASSERT_NE(-1, pid) << strerror(errno);
269
270 if (pid == 0) {
271 _Exit(99);
272 }
273
274 int status;
275 ASSERT_EQ(pid, waitpid(pid, &status, 0));
276 ASSERT_TRUE(WIFEXITED(status));
277 ASSERT_EQ(99, WEXITSTATUS(status));
278}
Elliott Hughes49167062014-07-25 17:24:00 -0700279
280TEST(stdlib, pty_smoke) {
281 // getpt returns a pty with O_RDWR|O_NOCTTY.
282 int fd = getpt();
283 ASSERT_NE(-1, fd);
284
285 // grantpt is a no-op.
286 ASSERT_EQ(0, grantpt(fd));
287
288 // ptsname_r should start "/dev/pts/".
289 char name_r[128];
290 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
291 name_r[9] = 0;
292 ASSERT_STREQ("/dev/pts/", name_r);
293
294 close(fd);
295}
296
297TEST(stdlib, posix_openpt) {
298 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
299 ASSERT_NE(-1, fd);
300 close(fd);
301}
302
303TEST(stdlib, ptsname_r_ENOTTY) {
304 errno = 0;
305 char buf[128];
306 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
307 ASSERT_EQ(ENOTTY, errno);
308}
309
310TEST(stdlib, ptsname_r_EINVAL) {
311 int fd = getpt();
312 ASSERT_NE(-1, fd);
313 errno = 0;
314 char* buf = NULL;
315 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
316 ASSERT_EQ(EINVAL, errno);
317 close(fd);
318}
319
320TEST(stdlib, ptsname_r_ERANGE) {
321 int fd = getpt();
322 ASSERT_NE(-1, fd);
323 errno = 0;
324 char buf[1];
325 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
326 ASSERT_EQ(ERANGE, errno);
327 close(fd);
328}
329
330TEST(stdlib, ttyname_r) {
331 int fd = getpt();
332 ASSERT_NE(-1, fd);
333
334 // ttyname_r returns "/dev/ptmx" for a pty.
335 char name_r[128];
336 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
337 ASSERT_STREQ("/dev/ptmx", name_r);
338
339 close(fd);
340}
341
342TEST(stdlib, ttyname_r_ENOTTY) {
343 int fd = open("/dev/null", O_WRONLY);
344 errno = 0;
345 char buf[128];
346 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
347 ASSERT_EQ(ENOTTY, errno);
348 close(fd);
349}
350
351TEST(stdlib, ttyname_r_EINVAL) {
352 int fd = getpt();
353 ASSERT_NE(-1, fd);
354 errno = 0;
355 char* buf = NULL;
356 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
357 ASSERT_EQ(EINVAL, errno);
358 close(fd);
359}
360
361TEST(stdlib, ttyname_r_ERANGE) {
362 int fd = getpt();
363 ASSERT_NE(-1, fd);
364 errno = 0;
365 char buf[1];
366 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
367 ASSERT_EQ(ERANGE, errno);
368 close(fd);
369}
370
371TEST(stdlib, unlockpt_ENOTTY) {
372 int fd = open("/dev/null", O_WRONLY);
373 errno = 0;
374 ASSERT_EQ(-1, unlockpt(fd));
375 ASSERT_EQ(ENOTTY, errno);
376 close(fd);
377}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700378
379TEST(stdlib, strtol_EINVAL) {
380 errno = 0;
381 strtol("123", NULL, -1);
382 ASSERT_EQ(EINVAL, errno);
383 errno = 0;
384 strtol("123", NULL, 1);
385 ASSERT_EQ(EINVAL, errno);
386 errno = 0;
387 strtol("123", NULL, 37);
388 ASSERT_EQ(EINVAL, errno);
389}
390
391TEST(stdlib, strtoll_EINVAL) {
392 errno = 0;
393 strtoll("123", NULL, -1);
394 ASSERT_EQ(EINVAL, errno);
395 errno = 0;
396 strtoll("123", NULL, 1);
397 ASSERT_EQ(EINVAL, errno);
398 errno = 0;
399 strtoll("123", NULL, 37);
400 ASSERT_EQ(EINVAL, errno);
401}
402
403TEST(stdlib, strtoul_EINVAL) {
404 errno = 0;
405 strtoul("123", NULL, -1);
406 ASSERT_EQ(EINVAL, errno);
407 errno = 0;
408 strtoul("123", NULL, 1);
409 ASSERT_EQ(EINVAL, errno);
410 errno = 0;
411 strtoul("123", NULL, 37);
412 ASSERT_EQ(EINVAL, errno);
413}
414
415TEST(stdlib, strtoull_EINVAL) {
416 errno = 0;
417 strtoull("123", NULL, -1);
418 ASSERT_EQ(EINVAL, errno);
419 errno = 0;
420 strtoull("123", NULL, 1);
421 ASSERT_EQ(EINVAL, errno);
422 errno = 0;
423 strtoull("123", NULL, 37);
424 ASSERT_EQ(EINVAL, errno);
425}