blob: 9ad96fd81f1747714a79ea2f5a4c569eab95fbd6 [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
Elliott Hughes31e072f2014-09-30 16:15:42 -0700102TEST(stdlib, realpath__component_after_non_directory) {
103 errno = 0;
104 char* p = realpath("/dev/null/.", NULL);
105 ASSERT_TRUE(p == NULL);
106 ASSERT_EQ(ENOTDIR, errno);
107
108 errno = 0;
109 p = realpath("/dev/null/..", NULL);
110 ASSERT_TRUE(p == NULL);
111 ASSERT_EQ(ENOTDIR, errno);
112}
113
Elliott Hughesf0777842013-03-01 16:59:46 -0800114TEST(stdlib, realpath) {
115 // Get the name of this executable.
116 char executable_path[PATH_MAX];
117 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
118 ASSERT_NE(rc, -1);
119 executable_path[rc] = '\0';
120
121 char buf[PATH_MAX + 1];
122 char* p = realpath("/proc/self/exe", buf);
123 ASSERT_STREQ(executable_path, p);
124
125 p = realpath("/proc/self/exe", NULL);
126 ASSERT_STREQ(executable_path, p);
127 free(p);
128}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700129
130TEST(stdlib, qsort) {
131 struct s {
132 char name[16];
133 static int comparator(const void* lhs, const void* rhs) {
134 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
135 }
136 };
137 s entries[3];
138 strcpy(entries[0].name, "charlie");
139 strcpy(entries[1].name, "bravo");
140 strcpy(entries[2].name, "alpha");
141
142 qsort(entries, 3, sizeof(s), s::comparator);
143 ASSERT_STREQ("alpha", entries[0].name);
144 ASSERT_STREQ("bravo", entries[1].name);
145 ASSERT_STREQ("charlie", entries[2].name);
146
147 qsort(entries, 3, sizeof(s), s::comparator);
148 ASSERT_STREQ("alpha", entries[0].name);
149 ASSERT_STREQ("bravo", entries[1].name);
150 ASSERT_STREQ("charlie", entries[2].name);
151}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800152
153static void* TestBug57421_child(void* arg) {
154 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
155 pthread_join(main_thread, NULL);
156 char* value = getenv("ENVIRONMENT_VARIABLE");
157 if (value == NULL) {
158 setenv("ENVIRONMENT_VARIABLE", "value", 1);
159 }
160 return NULL;
161}
162
163static void TestBug57421_main() {
164 pthread_t t;
165 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
166 pthread_exit(NULL);
167}
168
169// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
170// run this test (which exits normally) in its own process.
171TEST(stdlib_DeathTest, getenv_after_main_thread_exits) {
172 // https://code.google.com/p/android/issues/detail?id=57421
173 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
174 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
175}
Calin Juravlefe317a32014-02-21 15:11:03 +0000176
Elliott Hughes31165ed2014-09-23 17:34:29 -0700177TEST(stdlib, mkostemp64) {
178 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
179 int flags = fcntl(tf.fd, F_GETFD);
180 ASSERT_TRUE(flags != -1);
181 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
182}
183
184TEST(stdlib, mkostemp) {
185 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
186 int flags = fcntl(tf.fd, F_GETFD);
187 ASSERT_TRUE(flags != -1);
188 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
189}
190
191TEST(stdlib, mkstemp64) {
192 TemporaryFile tf(mkstemp64);
193 struct stat64 sb;
194 ASSERT_EQ(0, fstat64(tf.fd, &sb));
195 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
196}
197
Calin Juravlefe317a32014-02-21 15:11:03 +0000198TEST(stdlib, mkstemp) {
199 TemporaryFile tf;
200 struct stat sb;
201 ASSERT_EQ(0, fstat(tf.fd, &sb));
202}
203
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700204TEST(stdlib, system) {
205 int status;
206
207 status = system("exit 0");
208 ASSERT_TRUE(WIFEXITED(status));
209 ASSERT_EQ(0, WEXITSTATUS(status));
210
211 status = system("exit 1");
212 ASSERT_TRUE(WIFEXITED(status));
213 ASSERT_EQ(1, WEXITSTATUS(status));
214}
Elliott Hughes5a817382014-03-12 16:12:57 -0700215
216TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700217 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700218}
219
220TEST(stdlib, strtod) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700221 ASSERT_DOUBLE_EQ(1.23, strtod("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700222}
223
224TEST(stdlib, strtof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700225 ASSERT_FLOAT_EQ(1.23, strtof("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700226}
227
228TEST(stdlib, strtold) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700229 ASSERT_DOUBLE_EQ(1.23, strtold("1.23", NULL));
Elliott Hughes5a817382014-03-12 16:12:57 -0700230}
Elliott Hughes9f525642014-04-08 17:14:01 -0700231
Dan Albertb8425c52014-04-29 17:49:06 -0700232TEST(stdlib, quick_exit) {
233 pid_t pid = fork();
234 ASSERT_NE(-1, pid) << strerror(errno);
235
236 if (pid == 0) {
237 quick_exit(99);
238 }
239
240 int status;
241 ASSERT_EQ(pid, waitpid(pid, &status, 0));
242 ASSERT_TRUE(WIFEXITED(status));
243 ASSERT_EQ(99, WEXITSTATUS(status));
244}
245
246static int quick_exit_status = 0;
247
248static void quick_exit_1(void) {
249 ASSERT_EQ(quick_exit_status, 0);
250 quick_exit_status = 1;
251}
252
253static void quick_exit_2(void) {
254 ASSERT_EQ(quick_exit_status, 1);
255}
256
257static void not_run(void) {
258 FAIL();
259}
260
261TEST(stdlib, at_quick_exit) {
262 pid_t pid = fork();
263 ASSERT_NE(-1, pid) << strerror(errno);
264
265 if (pid == 0) {
266 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
267 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
268 atexit(not_run);
269 quick_exit(99);
270 }
271
272 int status;
273 ASSERT_EQ(pid, waitpid(pid, &status, 0));
274 ASSERT_TRUE(WIFEXITED(status));
275 ASSERT_EQ(99, WEXITSTATUS(status));
276}
277
Elliott Hughes9f525642014-04-08 17:14:01 -0700278TEST(unistd, _Exit) {
279 int pid = fork();
280 ASSERT_NE(-1, pid) << strerror(errno);
281
282 if (pid == 0) {
283 _Exit(99);
284 }
285
286 int status;
287 ASSERT_EQ(pid, waitpid(pid, &status, 0));
288 ASSERT_TRUE(WIFEXITED(status));
289 ASSERT_EQ(99, WEXITSTATUS(status));
290}
Elliott Hughes49167062014-07-25 17:24:00 -0700291
292TEST(stdlib, pty_smoke) {
293 // getpt returns a pty with O_RDWR|O_NOCTTY.
294 int fd = getpt();
295 ASSERT_NE(-1, fd);
296
297 // grantpt is a no-op.
298 ASSERT_EQ(0, grantpt(fd));
299
300 // ptsname_r should start "/dev/pts/".
301 char name_r[128];
302 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
303 name_r[9] = 0;
304 ASSERT_STREQ("/dev/pts/", name_r);
305
306 close(fd);
307}
308
309TEST(stdlib, posix_openpt) {
310 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
311 ASSERT_NE(-1, fd);
312 close(fd);
313}
314
315TEST(stdlib, ptsname_r_ENOTTY) {
316 errno = 0;
317 char buf[128];
318 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
319 ASSERT_EQ(ENOTTY, errno);
320}
321
322TEST(stdlib, ptsname_r_EINVAL) {
323 int fd = getpt();
324 ASSERT_NE(-1, fd);
325 errno = 0;
326 char* buf = NULL;
327 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
328 ASSERT_EQ(EINVAL, errno);
329 close(fd);
330}
331
332TEST(stdlib, ptsname_r_ERANGE) {
333 int fd = getpt();
334 ASSERT_NE(-1, fd);
335 errno = 0;
336 char buf[1];
337 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
338 ASSERT_EQ(ERANGE, errno);
339 close(fd);
340}
341
342TEST(stdlib, ttyname_r) {
343 int fd = getpt();
344 ASSERT_NE(-1, fd);
345
346 // ttyname_r returns "/dev/ptmx" for a pty.
347 char name_r[128];
348 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
349 ASSERT_STREQ("/dev/ptmx", name_r);
350
351 close(fd);
352}
353
354TEST(stdlib, ttyname_r_ENOTTY) {
355 int fd = open("/dev/null", O_WRONLY);
356 errno = 0;
357 char buf[128];
358 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
359 ASSERT_EQ(ENOTTY, errno);
360 close(fd);
361}
362
363TEST(stdlib, ttyname_r_EINVAL) {
364 int fd = getpt();
365 ASSERT_NE(-1, fd);
366 errno = 0;
367 char* buf = NULL;
368 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
369 ASSERT_EQ(EINVAL, errno);
370 close(fd);
371}
372
373TEST(stdlib, ttyname_r_ERANGE) {
374 int fd = getpt();
375 ASSERT_NE(-1, fd);
376 errno = 0;
377 char buf[1];
378 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
379 ASSERT_EQ(ERANGE, errno);
380 close(fd);
381}
382
383TEST(stdlib, unlockpt_ENOTTY) {
384 int fd = open("/dev/null", O_WRONLY);
385 errno = 0;
386 ASSERT_EQ(-1, unlockpt(fd));
387 ASSERT_EQ(ENOTTY, errno);
388 close(fd);
389}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700390
391TEST(stdlib, strtol_EINVAL) {
392 errno = 0;
393 strtol("123", NULL, -1);
394 ASSERT_EQ(EINVAL, errno);
395 errno = 0;
396 strtol("123", NULL, 1);
397 ASSERT_EQ(EINVAL, errno);
398 errno = 0;
399 strtol("123", NULL, 37);
400 ASSERT_EQ(EINVAL, errno);
401}
402
403TEST(stdlib, strtoll_EINVAL) {
404 errno = 0;
405 strtoll("123", NULL, -1);
406 ASSERT_EQ(EINVAL, errno);
407 errno = 0;
408 strtoll("123", NULL, 1);
409 ASSERT_EQ(EINVAL, errno);
410 errno = 0;
411 strtoll("123", NULL, 37);
412 ASSERT_EQ(EINVAL, errno);
413}
414
415TEST(stdlib, strtoul_EINVAL) {
416 errno = 0;
417 strtoul("123", NULL, -1);
418 ASSERT_EQ(EINVAL, errno);
419 errno = 0;
420 strtoul("123", NULL, 1);
421 ASSERT_EQ(EINVAL, errno);
422 errno = 0;
423 strtoul("123", NULL, 37);
424 ASSERT_EQ(EINVAL, errno);
425}
426
427TEST(stdlib, strtoull_EINVAL) {
428 errno = 0;
429 strtoull("123", NULL, -1);
430 ASSERT_EQ(EINVAL, errno);
431 errno = 0;
432 strtoull("123", NULL, 1);
433 ASSERT_EQ(EINVAL, errno);
434 errno = 0;
435 strtoull("123", NULL, 37);
436 ASSERT_EQ(EINVAL, errno);
437}