blob: ea88f39b75b8ac5cecb1761ad951ec81e5253bef [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
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700232TEST(stdlib, strtof_2206701) {
233 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", NULL));
234 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", NULL));
235}
236
237TEST(stdlib, strtod_largest_subnormal) {
238 // This value has been known to cause javac and java to infinite loop.
239 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
240 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", NULL));
241 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", NULL));
242 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", NULL));
243 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", NULL));
244 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", NULL));
245 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", NULL));
246 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", NULL));
247}
248
Dan Albertb8425c52014-04-29 17:49:06 -0700249TEST(stdlib, quick_exit) {
250 pid_t pid = fork();
251 ASSERT_NE(-1, pid) << strerror(errno);
252
253 if (pid == 0) {
254 quick_exit(99);
255 }
256
257 int status;
258 ASSERT_EQ(pid, waitpid(pid, &status, 0));
259 ASSERT_TRUE(WIFEXITED(status));
260 ASSERT_EQ(99, WEXITSTATUS(status));
261}
262
263static int quick_exit_status = 0;
264
265static void quick_exit_1(void) {
266 ASSERT_EQ(quick_exit_status, 0);
267 quick_exit_status = 1;
268}
269
270static void quick_exit_2(void) {
271 ASSERT_EQ(quick_exit_status, 1);
272}
273
274static void not_run(void) {
275 FAIL();
276}
277
278TEST(stdlib, at_quick_exit) {
279 pid_t pid = fork();
280 ASSERT_NE(-1, pid) << strerror(errno);
281
282 if (pid == 0) {
283 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
284 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
285 atexit(not_run);
286 quick_exit(99);
287 }
288
289 int status;
290 ASSERT_EQ(pid, waitpid(pid, &status, 0));
291 ASSERT_TRUE(WIFEXITED(status));
292 ASSERT_EQ(99, WEXITSTATUS(status));
293}
294
Elliott Hughes9f525642014-04-08 17:14:01 -0700295TEST(unistd, _Exit) {
296 int pid = fork();
297 ASSERT_NE(-1, pid) << strerror(errno);
298
299 if (pid == 0) {
300 _Exit(99);
301 }
302
303 int status;
304 ASSERT_EQ(pid, waitpid(pid, &status, 0));
305 ASSERT_TRUE(WIFEXITED(status));
306 ASSERT_EQ(99, WEXITSTATUS(status));
307}
Elliott Hughes49167062014-07-25 17:24:00 -0700308
309TEST(stdlib, pty_smoke) {
310 // getpt returns a pty with O_RDWR|O_NOCTTY.
311 int fd = getpt();
312 ASSERT_NE(-1, fd);
313
314 // grantpt is a no-op.
315 ASSERT_EQ(0, grantpt(fd));
316
317 // ptsname_r should start "/dev/pts/".
318 char name_r[128];
319 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
320 name_r[9] = 0;
321 ASSERT_STREQ("/dev/pts/", name_r);
322
323 close(fd);
324}
325
326TEST(stdlib, posix_openpt) {
327 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
328 ASSERT_NE(-1, fd);
329 close(fd);
330}
331
332TEST(stdlib, ptsname_r_ENOTTY) {
333 errno = 0;
334 char buf[128];
335 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
336 ASSERT_EQ(ENOTTY, errno);
337}
338
339TEST(stdlib, ptsname_r_EINVAL) {
340 int fd = getpt();
341 ASSERT_NE(-1, fd);
342 errno = 0;
343 char* buf = NULL;
344 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
345 ASSERT_EQ(EINVAL, errno);
346 close(fd);
347}
348
349TEST(stdlib, ptsname_r_ERANGE) {
350 int fd = getpt();
351 ASSERT_NE(-1, fd);
352 errno = 0;
353 char buf[1];
354 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
355 ASSERT_EQ(ERANGE, errno);
356 close(fd);
357}
358
359TEST(stdlib, ttyname_r) {
360 int fd = getpt();
361 ASSERT_NE(-1, fd);
362
363 // ttyname_r returns "/dev/ptmx" for a pty.
364 char name_r[128];
365 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
366 ASSERT_STREQ("/dev/ptmx", name_r);
367
368 close(fd);
369}
370
371TEST(stdlib, ttyname_r_ENOTTY) {
372 int fd = open("/dev/null", O_WRONLY);
373 errno = 0;
374 char buf[128];
375 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
376 ASSERT_EQ(ENOTTY, errno);
377 close(fd);
378}
379
380TEST(stdlib, ttyname_r_EINVAL) {
381 int fd = getpt();
382 ASSERT_NE(-1, fd);
383 errno = 0;
384 char* buf = NULL;
385 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
386 ASSERT_EQ(EINVAL, errno);
387 close(fd);
388}
389
390TEST(stdlib, ttyname_r_ERANGE) {
391 int fd = getpt();
392 ASSERT_NE(-1, fd);
393 errno = 0;
394 char buf[1];
395 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
396 ASSERT_EQ(ERANGE, errno);
397 close(fd);
398}
399
400TEST(stdlib, unlockpt_ENOTTY) {
401 int fd = open("/dev/null", O_WRONLY);
402 errno = 0;
403 ASSERT_EQ(-1, unlockpt(fd));
404 ASSERT_EQ(ENOTTY, errno);
405 close(fd);
406}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700407
408TEST(stdlib, strtol_EINVAL) {
409 errno = 0;
410 strtol("123", NULL, -1);
411 ASSERT_EQ(EINVAL, errno);
412 errno = 0;
413 strtol("123", NULL, 1);
414 ASSERT_EQ(EINVAL, errno);
415 errno = 0;
416 strtol("123", NULL, 37);
417 ASSERT_EQ(EINVAL, errno);
418}
419
420TEST(stdlib, strtoll_EINVAL) {
421 errno = 0;
422 strtoll("123", NULL, -1);
423 ASSERT_EQ(EINVAL, errno);
424 errno = 0;
425 strtoll("123", NULL, 1);
426 ASSERT_EQ(EINVAL, errno);
427 errno = 0;
428 strtoll("123", NULL, 37);
429 ASSERT_EQ(EINVAL, errno);
430}
431
432TEST(stdlib, strtoul_EINVAL) {
433 errno = 0;
434 strtoul("123", NULL, -1);
435 ASSERT_EQ(EINVAL, errno);
436 errno = 0;
437 strtoul("123", NULL, 1);
438 ASSERT_EQ(EINVAL, errno);
439 errno = 0;
440 strtoul("123", NULL, 37);
441 ASSERT_EQ(EINVAL, errno);
442}
443
444TEST(stdlib, strtoull_EINVAL) {
445 errno = 0;
446 strtoull("123", NULL, -1);
447 ASSERT_EQ(EINVAL, errno);
448 errno = 0;
449 strtoull("123", NULL, 1);
450 ASSERT_EQ(EINVAL, errno);
451 errno = 0;
452 strtoull("123", NULL, 37);
453 ASSERT_EQ(EINVAL, errno);
454}