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>
18 #include "BionicDeathTest.h"
19 #include "TemporaryFile.h"
20
21 #include <errno.h>
22 #include <libgen.h>
23 #include <limits.h>
24 #include <pthread.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30
31 // The random number generator tests all set the seed, get four values, reset the seed and check
32 // that they get the first two values repeated, and then reset the seed and check two more values
33 // to rule out the possibility that we're just going round a cycle of four values.
34 // TODO: factor this out.
35
TEST(stdlib,drand48)36 TEST(stdlib, drand48) {
37 srand48(0x01020304);
38 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
39 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
40 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
41 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
42 srand48(0x01020304);
43 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
44 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
45 srand48(0x01020304);
46 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
47 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
48 }
49
TEST(stdlib,erand48)50 TEST(stdlib, erand48) {
51 const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 };
52 unsigned short xsubi[3];
53 memcpy(xsubi, seed, sizeof(seed));
54 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
55 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
56 EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi));
57 EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi));
58 memcpy(xsubi, seed, sizeof(seed));
59 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
60 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
61 memcpy(xsubi, seed, sizeof(seed));
62 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
63 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
64 }
65
TEST(stdlib,lcong48)66 TEST(stdlib, lcong48) {
67 unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e };
68 lcong48(p);
69 EXPECT_EQ(1531389981, lrand48());
70 EXPECT_EQ(1598801533, lrand48());
71 EXPECT_EQ(2080534853, lrand48());
72 EXPECT_EQ(1102488897, lrand48());
73 lcong48(p);
74 EXPECT_EQ(1531389981, lrand48());
75 EXPECT_EQ(1598801533, lrand48());
76 lcong48(p);
77 EXPECT_EQ(1531389981, lrand48());
78 EXPECT_EQ(1598801533, lrand48());
79 }
80
TEST(stdlib,lrand48)81 TEST(stdlib, lrand48) {
82 srand48(0x01020304);
83 EXPECT_EQ(1409163720, lrand48());
84 EXPECT_EQ(397769746, lrand48());
85 EXPECT_EQ(902267124, lrand48());
86 EXPECT_EQ(132366131, lrand48());
87 srand48(0x01020304);
88 EXPECT_EQ(1409163720, lrand48());
89 EXPECT_EQ(397769746, lrand48());
90 srand48(0x01020304);
91 EXPECT_EQ(1409163720, lrand48());
92 EXPECT_EQ(397769746, lrand48());
93 }
94
TEST(stdlib,random)95 TEST(stdlib, random) {
96 srandom(0x01020304);
97 EXPECT_EQ(55436735, random());
98 EXPECT_EQ(1399865117, random());
99 EXPECT_EQ(2032643283, random());
100 EXPECT_EQ(571329216, random());
101 srandom(0x01020304);
102 EXPECT_EQ(55436735, random());
103 EXPECT_EQ(1399865117, random());
104 srandom(0x01020304);
105 EXPECT_EQ(55436735, random());
106 EXPECT_EQ(1399865117, random());
107 }
108
TEST(stdlib,rand)109 TEST(stdlib, rand) {
110 srand(0x01020304);
111 EXPECT_EQ(55436735, rand());
112 EXPECT_EQ(1399865117, rand());
113 EXPECT_EQ(2032643283, rand());
114 EXPECT_EQ(571329216, rand());
115 srand(0x01020304);
116 EXPECT_EQ(55436735, rand());
117 EXPECT_EQ(1399865117, rand());
118 srand(0x01020304);
119 EXPECT_EQ(55436735, rand());
120 EXPECT_EQ(1399865117, rand());
121 }
122
TEST(stdlib,mrand48)123 TEST(stdlib, mrand48) {
124 srand48(0x01020304);
125 EXPECT_EQ(-1476639856, mrand48());
126 EXPECT_EQ(795539493, mrand48());
127 EXPECT_EQ(1804534249, mrand48());
128 EXPECT_EQ(264732262, mrand48());
129 srand48(0x01020304);
130 EXPECT_EQ(-1476639856, mrand48());
131 EXPECT_EQ(795539493, mrand48());
132 srand48(0x01020304);
133 EXPECT_EQ(-1476639856, mrand48());
134 EXPECT_EQ(795539493, mrand48());
135 }
136
TEST(stdlib,posix_memalign)137 TEST(stdlib, posix_memalign) {
138 void* p;
139
140 ASSERT_EQ(0, posix_memalign(&p, 512, 128));
141 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512);
142 free(p);
143
144 // Can't align to a non-power of 2.
145 ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128));
146 }
147
TEST(stdlib,realpath__NULL_filename)148 TEST(stdlib, realpath__NULL_filename) {
149 errno = 0;
150 char* p = realpath(NULL, NULL);
151 ASSERT_TRUE(p == NULL);
152 ASSERT_EQ(EINVAL, errno);
153 }
154
TEST(stdlib,realpath__empty_filename)155 TEST(stdlib, realpath__empty_filename) {
156 errno = 0;
157 char* p = realpath("", NULL);
158 ASSERT_TRUE(p == NULL);
159 ASSERT_EQ(ENOENT, errno);
160 }
161
TEST(stdlib,realpath__ENOENT)162 TEST(stdlib, realpath__ENOENT) {
163 errno = 0;
164 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
165 ASSERT_TRUE(p == NULL);
166 ASSERT_EQ(ENOENT, errno);
167 }
168
TEST(stdlib,realpath__component_after_non_directory)169 TEST(stdlib, realpath__component_after_non_directory) {
170 errno = 0;
171 char* p = realpath("/dev/null/.", NULL);
172 ASSERT_TRUE(p == NULL);
173 ASSERT_EQ(ENOTDIR, errno);
174
175 errno = 0;
176 p = realpath("/dev/null/..", NULL);
177 ASSERT_TRUE(p == NULL);
178 ASSERT_EQ(ENOTDIR, errno);
179 }
180
TEST(stdlib,realpath)181 TEST(stdlib, realpath) {
182 // Get the name of this executable.
183 char executable_path[PATH_MAX];
184 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
185 ASSERT_NE(rc, -1);
186 executable_path[rc] = '\0';
187
188 char buf[PATH_MAX + 1];
189 char* p = realpath("/proc/self/exe", buf);
190 ASSERT_STREQ(executable_path, p);
191
192 p = realpath("/proc/self/exe", NULL);
193 ASSERT_STREQ(executable_path, p);
194 free(p);
195 }
196
TEST(stdlib,qsort)197 TEST(stdlib, qsort) {
198 struct s {
199 char name[16];
200 static int comparator(const void* lhs, const void* rhs) {
201 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
202 }
203 };
204 s entries[3];
205 strcpy(entries[0].name, "charlie");
206 strcpy(entries[1].name, "bravo");
207 strcpy(entries[2].name, "alpha");
208
209 qsort(entries, 3, sizeof(s), s::comparator);
210 ASSERT_STREQ("alpha", entries[0].name);
211 ASSERT_STREQ("bravo", entries[1].name);
212 ASSERT_STREQ("charlie", entries[2].name);
213
214 qsort(entries, 3, sizeof(s), s::comparator);
215 ASSERT_STREQ("alpha", entries[0].name);
216 ASSERT_STREQ("bravo", entries[1].name);
217 ASSERT_STREQ("charlie", entries[2].name);
218 }
219
TestBug57421_child(void * arg)220 static void* TestBug57421_child(void* arg) {
221 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
222 pthread_join(main_thread, NULL);
223 char* value = getenv("ENVIRONMENT_VARIABLE");
224 if (value == NULL) {
225 setenv("ENVIRONMENT_VARIABLE", "value", 1);
226 }
227 return NULL;
228 }
229
TestBug57421_main()230 static void TestBug57421_main() {
231 pthread_t t;
232 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
233 pthread_exit(NULL);
234 }
235
236 // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
237 // run this test (which exits normally) in its own process.
238
239 class stdlib_DeathTest : public BionicDeathTest {};
240
TEST_F(stdlib_DeathTest,getenv_after_main_thread_exits)241 TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
242 // https://code.google.com/p/android/issues/detail?id=57421
243 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
244 }
245
TEST(stdlib,mkostemp64)246 TEST(stdlib, mkostemp64) {
247 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
248 int flags = fcntl(tf.fd, F_GETFD);
249 ASSERT_TRUE(flags != -1);
250 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
251 }
252
TEST(stdlib,mkostemp)253 TEST(stdlib, mkostemp) {
254 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
255 int flags = fcntl(tf.fd, F_GETFD);
256 ASSERT_TRUE(flags != -1);
257 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
258 }
259
TEST(stdlib,mkstemp64)260 TEST(stdlib, mkstemp64) {
261 TemporaryFile tf(mkstemp64);
262 struct stat64 sb;
263 ASSERT_EQ(0, fstat64(tf.fd, &sb));
264 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
265 }
266
TEST(stdlib,mkstemp)267 TEST(stdlib, mkstemp) {
268 TemporaryFile tf;
269 struct stat sb;
270 ASSERT_EQ(0, fstat(tf.fd, &sb));
271 }
272
TEST(stdlib,system)273 TEST(stdlib, system) {
274 int status;
275
276 status = system("exit 0");
277 ASSERT_TRUE(WIFEXITED(status));
278 ASSERT_EQ(0, WEXITSTATUS(status));
279
280 status = system("exit 1");
281 ASSERT_TRUE(WIFEXITED(status));
282 ASSERT_EQ(1, WEXITSTATUS(status));
283 }
284
TEST(stdlib,atof)285 TEST(stdlib, atof) {
286 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
287 }
288
TEST(stdlib,strtod)289 TEST(stdlib, strtod) {
290 ASSERT_DOUBLE_EQ(1.23, strtod("1.23", NULL));
291 }
292
TEST(stdlib,strtof)293 TEST(stdlib, strtof) {
294 ASSERT_FLOAT_EQ(1.23, strtof("1.23", NULL));
295 }
296
TEST(stdlib,strtold)297 TEST(stdlib, strtold) {
298 ASSERT_DOUBLE_EQ(1.23, strtold("1.23", NULL));
299 }
300
TEST(stdlib,strtof_2206701)301 TEST(stdlib, strtof_2206701) {
302 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", NULL));
303 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", NULL));
304 }
305
TEST(stdlib,strtod_largest_subnormal)306 TEST(stdlib, strtod_largest_subnormal) {
307 // This value has been known to cause javac and java to infinite loop.
308 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
309 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", NULL));
310 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", NULL));
311 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", NULL));
312 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", NULL));
313 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", NULL));
314 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", NULL));
315 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", NULL));
316 }
317
TEST(stdlib,quick_exit)318 TEST(stdlib, quick_exit) {
319 pid_t pid = fork();
320 ASSERT_NE(-1, pid) << strerror(errno);
321
322 if (pid == 0) {
323 quick_exit(99);
324 }
325
326 int status;
327 ASSERT_EQ(pid, waitpid(pid, &status, 0));
328 ASSERT_TRUE(WIFEXITED(status));
329 ASSERT_EQ(99, WEXITSTATUS(status));
330 }
331
332 static int quick_exit_status = 0;
333
quick_exit_1(void)334 static void quick_exit_1(void) {
335 ASSERT_EQ(quick_exit_status, 0);
336 quick_exit_status = 1;
337 }
338
quick_exit_2(void)339 static void quick_exit_2(void) {
340 ASSERT_EQ(quick_exit_status, 1);
341 }
342
not_run(void)343 static void not_run(void) {
344 FAIL();
345 }
346
TEST(stdlib,at_quick_exit)347 TEST(stdlib, at_quick_exit) {
348 pid_t pid = fork();
349 ASSERT_NE(-1, pid) << strerror(errno);
350
351 if (pid == 0) {
352 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
353 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
354 atexit(not_run);
355 quick_exit(99);
356 }
357
358 int status;
359 ASSERT_EQ(pid, waitpid(pid, &status, 0));
360 ASSERT_TRUE(WIFEXITED(status));
361 ASSERT_EQ(99, WEXITSTATUS(status));
362 }
363
TEST(unistd,_Exit)364 TEST(unistd, _Exit) {
365 int pid = fork();
366 ASSERT_NE(-1, pid) << strerror(errno);
367
368 if (pid == 0) {
369 _Exit(99);
370 }
371
372 int status;
373 ASSERT_EQ(pid, waitpid(pid, &status, 0));
374 ASSERT_TRUE(WIFEXITED(status));
375 ASSERT_EQ(99, WEXITSTATUS(status));
376 }
377
TEST(stdlib,pty_smoke)378 TEST(stdlib, pty_smoke) {
379 // getpt returns a pty with O_RDWR|O_NOCTTY.
380 int fd = getpt();
381 ASSERT_NE(-1, fd);
382
383 // grantpt is a no-op.
384 ASSERT_EQ(0, grantpt(fd));
385
386 // ptsname_r should start "/dev/pts/".
387 char name_r[128];
388 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
389 name_r[9] = 0;
390 ASSERT_STREQ("/dev/pts/", name_r);
391
392 close(fd);
393 }
394
TEST(stdlib,posix_openpt)395 TEST(stdlib, posix_openpt) {
396 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
397 ASSERT_NE(-1, fd);
398 close(fd);
399 }
400
TEST(stdlib,ptsname_r_ENOTTY)401 TEST(stdlib, ptsname_r_ENOTTY) {
402 errno = 0;
403 char buf[128];
404 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
405 ASSERT_EQ(ENOTTY, errno);
406 }
407
TEST(stdlib,ptsname_r_EINVAL)408 TEST(stdlib, ptsname_r_EINVAL) {
409 int fd = getpt();
410 ASSERT_NE(-1, fd);
411 errno = 0;
412 char* buf = NULL;
413 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
414 ASSERT_EQ(EINVAL, errno);
415 close(fd);
416 }
417
TEST(stdlib,ptsname_r_ERANGE)418 TEST(stdlib, ptsname_r_ERANGE) {
419 int fd = getpt();
420 ASSERT_NE(-1, fd);
421 errno = 0;
422 char buf[1];
423 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
424 ASSERT_EQ(ERANGE, errno);
425 close(fd);
426 }
427
TEST(stdlib,ttyname_r)428 TEST(stdlib, ttyname_r) {
429 int fd = getpt();
430 ASSERT_NE(-1, fd);
431
432 // ttyname_r returns "/dev/ptmx" for a pty.
433 char name_r[128];
434 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
435 ASSERT_STREQ("/dev/ptmx", name_r);
436
437 close(fd);
438 }
439
TEST(stdlib,ttyname_r_ENOTTY)440 TEST(stdlib, ttyname_r_ENOTTY) {
441 int fd = open("/dev/null", O_WRONLY);
442 errno = 0;
443 char buf[128];
444 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
445 ASSERT_EQ(ENOTTY, errno);
446 close(fd);
447 }
448
TEST(stdlib,ttyname_r_EINVAL)449 TEST(stdlib, ttyname_r_EINVAL) {
450 int fd = getpt();
451 ASSERT_NE(-1, fd);
452 errno = 0;
453 char* buf = NULL;
454 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
455 ASSERT_EQ(EINVAL, errno);
456 close(fd);
457 }
458
TEST(stdlib,ttyname_r_ERANGE)459 TEST(stdlib, ttyname_r_ERANGE) {
460 int fd = getpt();
461 ASSERT_NE(-1, fd);
462 errno = 0;
463 char buf[1];
464 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
465 ASSERT_EQ(ERANGE, errno);
466 close(fd);
467 }
468
TEST(stdlib,unlockpt_ENOTTY)469 TEST(stdlib, unlockpt_ENOTTY) {
470 int fd = open("/dev/null", O_WRONLY);
471 errno = 0;
472 ASSERT_EQ(-1, unlockpt(fd));
473 ASSERT_EQ(ENOTTY, errno);
474 close(fd);
475 }
476
TEST(stdlib,strtol_EINVAL)477 TEST(stdlib, strtol_EINVAL) {
478 errno = 0;
479 strtol("123", NULL, -1);
480 ASSERT_EQ(EINVAL, errno);
481 errno = 0;
482 strtol("123", NULL, 1);
483 ASSERT_EQ(EINVAL, errno);
484 errno = 0;
485 strtol("123", NULL, 37);
486 ASSERT_EQ(EINVAL, errno);
487 }
488
TEST(stdlib,strtoll_EINVAL)489 TEST(stdlib, strtoll_EINVAL) {
490 errno = 0;
491 strtoll("123", NULL, -1);
492 ASSERT_EQ(EINVAL, errno);
493 errno = 0;
494 strtoll("123", NULL, 1);
495 ASSERT_EQ(EINVAL, errno);
496 errno = 0;
497 strtoll("123", NULL, 37);
498 ASSERT_EQ(EINVAL, errno);
499 }
500
TEST(stdlib,strtoul_EINVAL)501 TEST(stdlib, strtoul_EINVAL) {
502 errno = 0;
503 strtoul("123", NULL, -1);
504 ASSERT_EQ(EINVAL, errno);
505 errno = 0;
506 strtoul("123", NULL, 1);
507 ASSERT_EQ(EINVAL, errno);
508 errno = 0;
509 strtoul("123", NULL, 37);
510 ASSERT_EQ(EINVAL, errno);
511 }
512
TEST(stdlib,strtoull_EINVAL)513 TEST(stdlib, strtoull_EINVAL) {
514 errno = 0;
515 strtoull("123", NULL, -1);
516 ASSERT_EQ(EINVAL, errno);
517 errno = 0;
518 strtoull("123", NULL, 1);
519 ASSERT_EQ(EINVAL, errno);
520 errno = 0;
521 strtoull("123", NULL, 37);
522 ASSERT_EQ(EINVAL, errno);
523 }
524