blob: 5b9442fcf7d2add2f9202a1613b466d7e2ae3986 [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>
Elliott Hughes33697a02016-01-26 13:04:57 -080018
Yabin Cui9df70402014-11-05 18:01:01 -080019#include "BionicDeathTest.h"
Elliott Hughes7f0849f2016-08-26 16:17:17 -070020#include "math_data_test.h"
Calin Juravlefe317a32014-02-21 15:11:03 +000021#include "TemporaryFile.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080022#include "utils.h"
Elliott Hughes774c7f52012-10-01 13:11:03 -070023
Elliott Hughesb16b7222013-02-04 13:18:00 -080024#include <errno.h>
Elliott Hughes7f0849f2016-08-26 16:17:17 -070025#include <fcntl.h>
Elliott Hughesf0777842013-03-01 16:59:46 -080026#include <libgen.h>
27#include <limits.h>
Elliott Hughes7f0849f2016-08-26 16:17:17 -070028#include <math.h>
Elliott Hughes877ec6d2013-11-15 17:40:18 -080029#include <pthread.h>
Elliott Hughesb16b7222013-02-04 13:18:00 -080030#include <stdint.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070031#include <stdlib.h>
Elliott Hughes40488562014-03-12 13:50:38 -070032#include <sys/types.h>
33#include <sys/wait.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070034
Elliott Hughes274afe82014-11-06 12:40:08 -080035// The random number generator tests all set the seed, get four values, reset the seed and check
36// that they get the first two values repeated, and then reset the seed and check two more values
37// to rule out the possibility that we're just going round a cycle of four values.
38// TODO: factor this out.
39
Elliott Hughes774c7f52012-10-01 13:11:03 -070040TEST(stdlib, drand48) {
41 srand48(0x01020304);
42 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
43 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
44 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
45 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080046 srand48(0x01020304);
47 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
48 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
49 srand48(0x01020304);
50 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
51 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
52}
53
54TEST(stdlib, erand48) {
55 const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 };
56 unsigned short xsubi[3];
57 memcpy(xsubi, seed, sizeof(seed));
58 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
59 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
60 EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi));
61 EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi));
62 memcpy(xsubi, seed, sizeof(seed));
63 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
64 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
65 memcpy(xsubi, seed, sizeof(seed));
66 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
67 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
68}
69
70TEST(stdlib, lcong48) {
71 unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e };
72 lcong48(p);
73 EXPECT_EQ(1531389981, lrand48());
74 EXPECT_EQ(1598801533, lrand48());
75 EXPECT_EQ(2080534853, lrand48());
76 EXPECT_EQ(1102488897, lrand48());
77 lcong48(p);
78 EXPECT_EQ(1531389981, lrand48());
79 EXPECT_EQ(1598801533, lrand48());
80 lcong48(p);
81 EXPECT_EQ(1531389981, lrand48());
82 EXPECT_EQ(1598801533, lrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -070083}
84
Elliott Hughesa0beeea2014-06-12 11:48:04 -070085TEST(stdlib, lrand48) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070086 srand48(0x01020304);
87 EXPECT_EQ(1409163720, lrand48());
88 EXPECT_EQ(397769746, lrand48());
89 EXPECT_EQ(902267124, lrand48());
90 EXPECT_EQ(132366131, lrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080091 srand48(0x01020304);
92 EXPECT_EQ(1409163720, lrand48());
93 EXPECT_EQ(397769746, lrand48());
94 srand48(0x01020304);
95 EXPECT_EQ(1409163720, lrand48());
96 EXPECT_EQ(397769746, lrand48());
Elliott Hughesa0beeea2014-06-12 11:48:04 -070097}
Elliott Hughes774c7f52012-10-01 13:11:03 -070098
Elliott Hughesa0beeea2014-06-12 11:48:04 -070099TEST(stdlib, random) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700100 srandom(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700101 EXPECT_EQ(55436735, random());
102 EXPECT_EQ(1399865117, random());
103 EXPECT_EQ(2032643283, random());
104 EXPECT_EQ(571329216, random());
Elliott Hughes274afe82014-11-06 12:40:08 -0800105 srandom(0x01020304);
106 EXPECT_EQ(55436735, random());
107 EXPECT_EQ(1399865117, random());
108 srandom(0x01020304);
109 EXPECT_EQ(55436735, random());
110 EXPECT_EQ(1399865117, random());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700111}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700112
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700113TEST(stdlib, rand) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700114 srand(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700115 EXPECT_EQ(55436735, rand());
116 EXPECT_EQ(1399865117, rand());
117 EXPECT_EQ(2032643283, rand());
118 EXPECT_EQ(571329216, rand());
Elliott Hughes274afe82014-11-06 12:40:08 -0800119 srand(0x01020304);
120 EXPECT_EQ(55436735, rand());
121 EXPECT_EQ(1399865117, rand());
122 srand(0x01020304);
123 EXPECT_EQ(55436735, rand());
124 EXPECT_EQ(1399865117, rand());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700125}
126
127TEST(stdlib, mrand48) {
128 srand48(0x01020304);
129 EXPECT_EQ(-1476639856, mrand48());
130 EXPECT_EQ(795539493, mrand48());
131 EXPECT_EQ(1804534249, mrand48());
132 EXPECT_EQ(264732262, mrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800133 srand48(0x01020304);
134 EXPECT_EQ(-1476639856, mrand48());
135 EXPECT_EQ(795539493, mrand48());
136 srand48(0x01020304);
137 EXPECT_EQ(-1476639856, mrand48());
138 EXPECT_EQ(795539493, mrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700139}
Elliott Hughesb16b7222013-02-04 13:18:00 -0800140
141TEST(stdlib, posix_memalign) {
142 void* p;
143
144 ASSERT_EQ(0, posix_memalign(&p, 512, 128));
145 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512);
146 free(p);
147
148 // Can't align to a non-power of 2.
149 ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128));
150}
Elliott Hughesf0777842013-03-01 16:59:46 -0800151
152TEST(stdlib, realpath__NULL_filename) {
153 errno = 0;
154 char* p = realpath(NULL, NULL);
155 ASSERT_TRUE(p == NULL);
156 ASSERT_EQ(EINVAL, errno);
157}
158
159TEST(stdlib, realpath__empty_filename) {
160 errno = 0;
161 char* p = realpath("", NULL);
162 ASSERT_TRUE(p == NULL);
163 ASSERT_EQ(ENOENT, errno);
164}
165
166TEST(stdlib, realpath__ENOENT) {
167 errno = 0;
168 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
169 ASSERT_TRUE(p == NULL);
170 ASSERT_EQ(ENOENT, errno);
171}
172
Elliott Hughes31e072f2014-09-30 16:15:42 -0700173TEST(stdlib, realpath__component_after_non_directory) {
174 errno = 0;
175 char* p = realpath("/dev/null/.", NULL);
176 ASSERT_TRUE(p == NULL);
177 ASSERT_EQ(ENOTDIR, errno);
178
179 errno = 0;
180 p = realpath("/dev/null/..", NULL);
181 ASSERT_TRUE(p == NULL);
182 ASSERT_EQ(ENOTDIR, errno);
183}
184
Elliott Hughesf0777842013-03-01 16:59:46 -0800185TEST(stdlib, realpath) {
186 // Get the name of this executable.
187 char executable_path[PATH_MAX];
188 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
189 ASSERT_NE(rc, -1);
190 executable_path[rc] = '\0';
191
192 char buf[PATH_MAX + 1];
193 char* p = realpath("/proc/self/exe", buf);
194 ASSERT_STREQ(executable_path, p);
195
196 p = realpath("/proc/self/exe", NULL);
197 ASSERT_STREQ(executable_path, p);
198 free(p);
199}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700200
201TEST(stdlib, qsort) {
202 struct s {
203 char name[16];
204 static int comparator(const void* lhs, const void* rhs) {
205 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
206 }
207 };
208 s entries[3];
209 strcpy(entries[0].name, "charlie");
210 strcpy(entries[1].name, "bravo");
211 strcpy(entries[2].name, "alpha");
212
213 qsort(entries, 3, sizeof(s), s::comparator);
214 ASSERT_STREQ("alpha", entries[0].name);
215 ASSERT_STREQ("bravo", entries[1].name);
216 ASSERT_STREQ("charlie", entries[2].name);
217
218 qsort(entries, 3, sizeof(s), s::comparator);
219 ASSERT_STREQ("alpha", entries[0].name);
220 ASSERT_STREQ("bravo", entries[1].name);
221 ASSERT_STREQ("charlie", entries[2].name);
222}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800223
224static void* TestBug57421_child(void* arg) {
225 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
226 pthread_join(main_thread, NULL);
227 char* value = getenv("ENVIRONMENT_VARIABLE");
228 if (value == NULL) {
229 setenv("ENVIRONMENT_VARIABLE", "value", 1);
230 }
231 return NULL;
232}
233
234static void TestBug57421_main() {
235 pthread_t t;
236 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
237 pthread_exit(NULL);
238}
239
240// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
241// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800242
243class stdlib_DeathTest : public BionicDeathTest {};
244
245TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800246 // https://code.google.com/p/android/issues/detail?id=57421
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800247 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
248}
Calin Juravlefe317a32014-02-21 15:11:03 +0000249
Elliott Hughes31165ed2014-09-23 17:34:29 -0700250TEST(stdlib, mkostemp64) {
251 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
252 int flags = fcntl(tf.fd, F_GETFD);
253 ASSERT_TRUE(flags != -1);
254 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
255}
256
257TEST(stdlib, mkostemp) {
258 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
259 int flags = fcntl(tf.fd, F_GETFD);
260 ASSERT_TRUE(flags != -1);
261 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
262}
263
264TEST(stdlib, mkstemp64) {
265 TemporaryFile tf(mkstemp64);
266 struct stat64 sb;
267 ASSERT_EQ(0, fstat64(tf.fd, &sb));
268 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
269}
270
Calin Juravlefe317a32014-02-21 15:11:03 +0000271TEST(stdlib, mkstemp) {
272 TemporaryFile tf;
273 struct stat sb;
274 ASSERT_EQ(0, fstat(tf.fd, &sb));
275}
276
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700277TEST(stdlib, system) {
278 int status;
279
280 status = system("exit 0");
281 ASSERT_TRUE(WIFEXITED(status));
282 ASSERT_EQ(0, WEXITSTATUS(status));
283
284 status = system("exit 1");
285 ASSERT_TRUE(WIFEXITED(status));
286 ASSERT_EQ(1, WEXITSTATUS(status));
287}
Elliott Hughes5a817382014-03-12 16:12:57 -0700288
289TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700290 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700291}
292
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700293template <typename T>
294static void CheckStrToFloat(T fn(const char* s, char** end)) {
295 FpUlpEq<0, T> pred;
296
297 EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr));
298 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr));
299 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr));
300
301 EXPECT_TRUE(isnan(fn("+nan", nullptr)));
302 EXPECT_TRUE(isnan(fn("nan", nullptr)));
303 EXPECT_TRUE(isnan(fn("-nan", nullptr)));
304
305 EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr)));
306 EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr)));
307 EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr)));
308
309 char* p;
310 EXPECT_TRUE(isnan(fn("+nanny", &p)));
311 EXPECT_STREQ("ny", p);
312 EXPECT_TRUE(isnan(fn("nanny", &p)));
313 EXPECT_STREQ("ny", p);
314 EXPECT_TRUE(isnan(fn("-nanny", &p)));
315 EXPECT_STREQ("ny", p);
316
317 EXPECT_EQ(0, fn("muppet", &p));
318 EXPECT_STREQ("muppet", p);
319 EXPECT_EQ(0, fn(" muppet", &p));
320 EXPECT_STREQ(" muppet", p);
321
322 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr));
323 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr));
324 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr));
325
326 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr));
327 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr));
328 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr));
329
330 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p));
331 EXPECT_STREQ("initude", p);
332 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p));
333 EXPECT_STREQ("initude", p);
334 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p));
335 EXPECT_STREQ("initude", p);
336
337 // Check case-insensitivity.
338 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr));
339 EXPECT_TRUE(isnan(fn("NaN", nullptr)));
340}
341
Elliott Hughes5a817382014-03-12 16:12:57 -0700342TEST(stdlib, strtod) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700343 CheckStrToFloat(strtod);
Elliott Hughes5a817382014-03-12 16:12:57 -0700344}
345
346TEST(stdlib, strtof) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700347 CheckStrToFloat(strtof);
Elliott Hughes5a817382014-03-12 16:12:57 -0700348}
349
350TEST(stdlib, strtold) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700351 CheckStrToFloat(strtold);
Elliott Hughes5a817382014-03-12 16:12:57 -0700352}
Elliott Hughes9f525642014-04-08 17:14:01 -0700353
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700354TEST(stdlib, strtof_2206701) {
355 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", NULL));
356 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", NULL));
357}
358
359TEST(stdlib, strtod_largest_subnormal) {
360 // This value has been known to cause javac and java to infinite loop.
361 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
362 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", NULL));
363 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", NULL));
364 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", NULL));
365 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", NULL));
366 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", NULL));
367 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", NULL));
368 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", NULL));
369}
370
Dan Albertb8425c52014-04-29 17:49:06 -0700371TEST(stdlib, quick_exit) {
372 pid_t pid = fork();
373 ASSERT_NE(-1, pid) << strerror(errno);
374
375 if (pid == 0) {
376 quick_exit(99);
377 }
378
Elliott Hughes33697a02016-01-26 13:04:57 -0800379 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700380}
381
382static int quick_exit_status = 0;
383
384static void quick_exit_1(void) {
385 ASSERT_EQ(quick_exit_status, 0);
386 quick_exit_status = 1;
387}
388
389static void quick_exit_2(void) {
390 ASSERT_EQ(quick_exit_status, 1);
391}
392
393static void not_run(void) {
394 FAIL();
395}
396
397TEST(stdlib, at_quick_exit) {
398 pid_t pid = fork();
399 ASSERT_NE(-1, pid) << strerror(errno);
400
401 if (pid == 0) {
402 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
403 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
404 atexit(not_run);
405 quick_exit(99);
406 }
407
Elliott Hughes33697a02016-01-26 13:04:57 -0800408 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700409}
410
Elliott Hughes9f525642014-04-08 17:14:01 -0700411TEST(unistd, _Exit) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800412 pid_t pid = fork();
Elliott Hughes9f525642014-04-08 17:14:01 -0700413 ASSERT_NE(-1, pid) << strerror(errno);
414
415 if (pid == 0) {
416 _Exit(99);
417 }
418
Elliott Hughes33697a02016-01-26 13:04:57 -0800419 AssertChildExited(pid, 99);
Elliott Hughes9f525642014-04-08 17:14:01 -0700420}
Elliott Hughes49167062014-07-25 17:24:00 -0700421
422TEST(stdlib, pty_smoke) {
423 // getpt returns a pty with O_RDWR|O_NOCTTY.
424 int fd = getpt();
425 ASSERT_NE(-1, fd);
426
427 // grantpt is a no-op.
428 ASSERT_EQ(0, grantpt(fd));
429
430 // ptsname_r should start "/dev/pts/".
431 char name_r[128];
432 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
433 name_r[9] = 0;
434 ASSERT_STREQ("/dev/pts/", name_r);
435
436 close(fd);
437}
438
439TEST(stdlib, posix_openpt) {
440 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
441 ASSERT_NE(-1, fd);
442 close(fd);
443}
444
445TEST(stdlib, ptsname_r_ENOTTY) {
446 errno = 0;
447 char buf[128];
448 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
449 ASSERT_EQ(ENOTTY, errno);
450}
451
452TEST(stdlib, ptsname_r_EINVAL) {
453 int fd = getpt();
454 ASSERT_NE(-1, fd);
455 errno = 0;
456 char* buf = NULL;
457 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
458 ASSERT_EQ(EINVAL, errno);
459 close(fd);
460}
461
462TEST(stdlib, ptsname_r_ERANGE) {
463 int fd = getpt();
464 ASSERT_NE(-1, fd);
465 errno = 0;
466 char buf[1];
467 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
468 ASSERT_EQ(ERANGE, errno);
469 close(fd);
470}
471
472TEST(stdlib, ttyname_r) {
473 int fd = getpt();
474 ASSERT_NE(-1, fd);
475
476 // ttyname_r returns "/dev/ptmx" for a pty.
477 char name_r[128];
478 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
479 ASSERT_STREQ("/dev/ptmx", name_r);
480
481 close(fd);
482}
483
484TEST(stdlib, ttyname_r_ENOTTY) {
485 int fd = open("/dev/null", O_WRONLY);
486 errno = 0;
487 char buf[128];
488 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
489 ASSERT_EQ(ENOTTY, errno);
490 close(fd);
491}
492
493TEST(stdlib, ttyname_r_EINVAL) {
494 int fd = getpt();
495 ASSERT_NE(-1, fd);
496 errno = 0;
497 char* buf = NULL;
498 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
499 ASSERT_EQ(EINVAL, errno);
500 close(fd);
501}
502
503TEST(stdlib, ttyname_r_ERANGE) {
504 int fd = getpt();
505 ASSERT_NE(-1, fd);
506 errno = 0;
507 char buf[1];
508 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
509 ASSERT_EQ(ERANGE, errno);
510 close(fd);
511}
512
513TEST(stdlib, unlockpt_ENOTTY) {
514 int fd = open("/dev/null", O_WRONLY);
515 errno = 0;
516 ASSERT_EQ(-1, unlockpt(fd));
517 ASSERT_EQ(ENOTTY, errno);
518 close(fd);
519}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700520
521TEST(stdlib, strtol_EINVAL) {
522 errno = 0;
523 strtol("123", NULL, -1);
524 ASSERT_EQ(EINVAL, errno);
525 errno = 0;
526 strtol("123", NULL, 1);
527 ASSERT_EQ(EINVAL, errno);
528 errno = 0;
529 strtol("123", NULL, 37);
530 ASSERT_EQ(EINVAL, errno);
531}
532
533TEST(stdlib, strtoll_EINVAL) {
534 errno = 0;
535 strtoll("123", NULL, -1);
536 ASSERT_EQ(EINVAL, errno);
537 errno = 0;
538 strtoll("123", NULL, 1);
539 ASSERT_EQ(EINVAL, errno);
540 errno = 0;
541 strtoll("123", NULL, 37);
542 ASSERT_EQ(EINVAL, errno);
543}
544
545TEST(stdlib, strtoul_EINVAL) {
546 errno = 0;
547 strtoul("123", NULL, -1);
548 ASSERT_EQ(EINVAL, errno);
549 errno = 0;
550 strtoul("123", NULL, 1);
551 ASSERT_EQ(EINVAL, errno);
552 errno = 0;
553 strtoul("123", NULL, 37);
554 ASSERT_EQ(EINVAL, errno);
555}
556
557TEST(stdlib, strtoull_EINVAL) {
558 errno = 0;
559 strtoull("123", NULL, -1);
560 ASSERT_EQ(EINVAL, errno);
561 errno = 0;
562 strtoull("123", NULL, 1);
563 ASSERT_EQ(EINVAL, errno);
564 errno = 0;
565 strtoull("123", NULL, 37);
566 ASSERT_EQ(EINVAL, errno);
567}
Elliott Hughesdf143f82016-04-04 17:34:04 -0700568
569TEST(stdlib, getsubopt) {
570 char* const tokens[] = {
571 const_cast<char*>("a"),
572 const_cast<char*>("b"),
573 const_cast<char*>("foo"),
574 nullptr
575 };
576 std::string input = "a,b,foo=bar,a,unknown";
577 char* subopts = &input[0];
578 char* value = nullptr;
579
580 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
581 ASSERT_EQ(nullptr, value);
582 ASSERT_EQ(1, getsubopt(&subopts, tokens, &value));
583 ASSERT_EQ(nullptr, value);
584 ASSERT_EQ(2, getsubopt(&subopts, tokens, &value));
585 ASSERT_STREQ("bar", value);
586 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
587 ASSERT_EQ(nullptr, value);
588
589 ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
590}
Elliott Hughes6f6f9052016-04-28 14:54:52 -0700591
592TEST(stdlib, mblen) {
593 // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
594 // respectively, do or do not have state-dependent encodings." We're always UTF-8.
595 EXPECT_EQ(0, mblen(nullptr, 1));
596
597 ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
598
599 // 1-byte UTF-8.
600 EXPECT_EQ(1, mblen("abcdef", 6));
601 // 2-byte UTF-8.
602 EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
603 // 3-byte UTF-8.
604 EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
605 // 4-byte UTF-8.
606 EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
607
608 // Illegal over-long sequence.
609 ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
610
611 // "mblen() shall ... return 0 (if s points to the null byte)".
612 EXPECT_EQ(0, mblen("", 1));
613}