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 19 #include "BionicDeathTest.h" 20 #include "math_data_test.h" 21 #include "TemporaryFile.h" 22 #include "utils.h" 23 24 #include <errno.h> 25 #include <fcntl.h> 26 #include <libgen.h> 27 #include <limits.h> 28 #include <math.h> 29 #include <pthread.h> 30 #include <stdint.h> 31 #include <stdlib.h> 32 #include <sys/types.h> 33 #include <sys/wait.h> 34 35 #include <limits> 36 #include <string> 37 38 #if defined(__BIONIC__) 39 #define ALIGNED_ALLOC_AVAILABLE 1 40 #elif defined(__GLIBC_PREREQ) 41 #if __GLIBC_PREREQ(2, 16) 42 #define ALIGNED_ALLOC_AVAILABLE 1 43 #endif 44 #endif 45 46 // The random number generator tests all set the seed, get four values, reset the seed and check 47 // that they get the first two values repeated, and then reset the seed and check two more values 48 // to rule out the possibility that we're just going round a cycle of four values. 49 // TODO: factor this out. 50 51 TEST(stdlib, drand48) { 52 srand48(0x01020304); 53 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); 54 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); 55 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48()); 56 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48()); 57 srand48(0x01020304); 58 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); 59 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); 60 srand48(0x01020304); 61 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); 62 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); 63 } 64 65 TEST(stdlib, erand48) { 66 const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 }; 67 unsigned short xsubi[3]; 68 memcpy(xsubi, seed, sizeof(seed)); 69 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi)); 70 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi)); 71 EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi)); 72 EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi)); 73 memcpy(xsubi, seed, sizeof(seed)); 74 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi)); 75 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi)); 76 memcpy(xsubi, seed, sizeof(seed)); 77 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi)); 78 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi)); 79 } 80 81 TEST(stdlib, lcong48) { 82 unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e }; 83 lcong48(p); 84 EXPECT_EQ(1531389981, lrand48()); 85 EXPECT_EQ(1598801533, lrand48()); 86 EXPECT_EQ(2080534853, lrand48()); 87 EXPECT_EQ(1102488897, lrand48()); 88 lcong48(p); 89 EXPECT_EQ(1531389981, lrand48()); 90 EXPECT_EQ(1598801533, lrand48()); 91 lcong48(p); 92 EXPECT_EQ(1531389981, lrand48()); 93 EXPECT_EQ(1598801533, lrand48()); 94 } 95 96 TEST(stdlib, lrand48) { 97 srand48(0x01020304); 98 EXPECT_EQ(1409163720, lrand48()); 99 EXPECT_EQ(397769746, lrand48()); 100 EXPECT_EQ(902267124, lrand48()); 101 EXPECT_EQ(132366131, lrand48()); 102 srand48(0x01020304); 103 EXPECT_EQ(1409163720, lrand48()); 104 EXPECT_EQ(397769746, lrand48()); 105 srand48(0x01020304); 106 EXPECT_EQ(1409163720, lrand48()); 107 EXPECT_EQ(397769746, lrand48()); 108 } 109 110 TEST(stdlib, random) { 111 srandom(0x01020304); 112 EXPECT_EQ(55436735, random()); 113 EXPECT_EQ(1399865117, random()); 114 EXPECT_EQ(2032643283, random()); 115 EXPECT_EQ(571329216, random()); 116 srandom(0x01020304); 117 EXPECT_EQ(55436735, random()); 118 EXPECT_EQ(1399865117, random()); 119 srandom(0x01020304); 120 EXPECT_EQ(55436735, random()); 121 EXPECT_EQ(1399865117, random()); 122 } 123 124 TEST(stdlib, rand) { 125 srand(0x01020304); 126 EXPECT_EQ(55436735, rand()); 127 EXPECT_EQ(1399865117, rand()); 128 EXPECT_EQ(2032643283, rand()); 129 EXPECT_EQ(571329216, rand()); 130 srand(0x01020304); 131 EXPECT_EQ(55436735, rand()); 132 EXPECT_EQ(1399865117, rand()); 133 srand(0x01020304); 134 EXPECT_EQ(55436735, rand()); 135 EXPECT_EQ(1399865117, rand()); 136 } 137 138 TEST(stdlib, mrand48) { 139 srand48(0x01020304); 140 EXPECT_EQ(-1476639856, mrand48()); 141 EXPECT_EQ(795539493, mrand48()); 142 EXPECT_EQ(1804534249, mrand48()); 143 EXPECT_EQ(264732262, mrand48()); 144 srand48(0x01020304); 145 EXPECT_EQ(-1476639856, mrand48()); 146 EXPECT_EQ(795539493, mrand48()); 147 srand48(0x01020304); 148 EXPECT_EQ(-1476639856, mrand48()); 149 EXPECT_EQ(795539493, mrand48()); 150 } 151 152 TEST(stdlib, jrand48_distribution) { 153 const int iterations = 4096; 154 const int pivot_low = 1536; 155 const int pivot_high = 2560; 156 157 unsigned short xsubi[3]; 158 int bits[32] = {}; 159 160 for (int iter = 0; iter < iterations; ++iter) { 161 long rand_val = jrand48(xsubi); 162 for (int bit = 0; bit < 32; ++bit) { 163 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01; 164 } 165 } 166 167 // Check that bit probability is uniform 168 for (int bit = 0; bit < 32; ++bit) { 169 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high)); 170 } 171 } 172 173 TEST(stdlib, mrand48_distribution) { 174 const int iterations = 4096; 175 const int pivot_low = 1536; 176 const int pivot_high = 2560; 177 178 int bits[32] = {}; 179 180 for (int iter = 0; iter < iterations; ++iter) { 181 long rand_val = mrand48(); 182 for (int bit = 0; bit < 32; ++bit) { 183 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01; 184 } 185 } 186 187 // Check that bit probability is uniform 188 for (int bit = 0; bit < 32; ++bit) { 189 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high)); 190 } 191 } 192 193 TEST(stdlib, posix_memalign_sweep) { 194 void* ptr; 195 196 // These should all fail. 197 for (size_t align = 0; align < sizeof(long); align++) { 198 ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256)) 199 << "Unexpected value at align " << align; 200 } 201 202 // Verify powers of 2 up to 2048 allocate, and verify that all other 203 // alignment values between the powers of 2 fail. 204 size_t last_align = sizeof(long); 205 for (size_t align = sizeof(long); align <= 2048; align <<= 1) { 206 // Try all of the non power of 2 values from the last until this value. 207 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) { 208 ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256)) 209 << "Unexpected success at align " << fail_align; 210 } 211 ASSERT_EQ(0, posix_memalign(&ptr, align, 256)) 212 << "Unexpected failure at align " << align; 213 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) 214 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align; 215 free(ptr); 216 last_align = align; 217 } 218 } 219 220 TEST(stdlib, posix_memalign_various_sizes) { 221 std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000}; 222 for (auto size : sizes) { 223 void* ptr; 224 ASSERT_EQ(0, posix_memalign(&ptr, 16, 1)) 225 << "posix_memalign failed at size " << size; 226 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf) 227 << "Pointer not aligned at size " << size << " ptr " << ptr; 228 free(ptr); 229 } 230 } 231 232 TEST(stdlib, posix_memalign_overflow) { 233 void* ptr; 234 ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX)); 235 } 236 237 TEST(stdlib, aligned_alloc_sweep) { 238 #if defined(ALIGNED_ALLOC_AVAILABLE) 239 // Verify powers of 2 up to 2048 allocate, and verify that all other 240 // alignment values between the powers of 2 fail. 241 size_t last_align = 1; 242 for (size_t align = 1; align <= 2048; align <<= 1) { 243 // Try all of the non power of 2 values from the last until this value. 244 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) { 245 ASSERT_TRUE(aligned_alloc(fail_align, 256) == nullptr) 246 << "Unexpected success at align " << fail_align; 247 ASSERT_EQ(EINVAL, errno) << "Unexpected errno at align " << fail_align; 248 } 249 void* ptr = aligned_alloc(align, 256); 250 ASSERT_TRUE(ptr != nullptr) << "Unexpected failure at align " << align; 251 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) 252 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align; 253 free(ptr); 254 last_align = align; 255 } 256 #else 257 GTEST_LOG_(INFO) << "This test requires a C library that has aligned_alloc.\n"; 258 #endif 259 } 260 261 TEST(stdlib, aligned_alloc_overflow) { 262 #if defined(ALIGNED_ALLOC_AVAILABLE) 263 ASSERT_TRUE(aligned_alloc(16, SIZE_MAX) == nullptr); 264 #else 265 GTEST_LOG_(INFO) << "This test requires a C library that has aligned_alloc.\n"; 266 #endif 267 } 268 269 TEST(stdlib, aligned_alloc_size_not_multiple_of_alignment) { 270 #if defined(ALIGNED_ALLOC_AVAILABLE) 271 for (size_t size = 1; size <= 2048; size++) { 272 void* ptr = aligned_alloc(2048, size); 273 ASSERT_TRUE(ptr != nullptr) << "Failed at size " << std::to_string(size); 274 free(ptr); 275 } 276 #else 277 GTEST_LOG_(INFO) << "This test requires a C library that has aligned_alloc.\n"; 278 #endif 279 } 280 281 TEST(stdlib, realpath__NULL_filename) { 282 errno = 0; 283 // Work around the compile-time error generated by FORTIFY here. 284 const char* path = NULL; 285 char* p = realpath(path, NULL); 286 ASSERT_TRUE(p == NULL); 287 ASSERT_EQ(EINVAL, errno); 288 } 289 290 TEST(stdlib, realpath__empty_filename) { 291 errno = 0; 292 char* p = realpath("", NULL); 293 ASSERT_TRUE(p == NULL); 294 ASSERT_EQ(ENOENT, errno); 295 } 296 297 TEST(stdlib, realpath__ENOENT) { 298 errno = 0; 299 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL); 300 ASSERT_TRUE(p == NULL); 301 ASSERT_EQ(ENOENT, errno); 302 } 303 304 TEST(stdlib, realpath__component_after_non_directory) { 305 errno = 0; 306 char* p = realpath("/dev/null/.", NULL); 307 ASSERT_TRUE(p == NULL); 308 ASSERT_EQ(ENOTDIR, errno); 309 310 errno = 0; 311 p = realpath("/dev/null/..", NULL); 312 ASSERT_TRUE(p == NULL); 313 ASSERT_EQ(ENOTDIR, errno); 314 } 315 316 TEST(stdlib, realpath) { 317 // Get the name of this executable. 318 char executable_path[PATH_MAX]; 319 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); 320 ASSERT_NE(rc, -1); 321 executable_path[rc] = '\0'; 322 323 char buf[PATH_MAX + 1]; 324 char* p = realpath("/proc/self/exe", buf); 325 ASSERT_STREQ(executable_path, p); 326 327 p = realpath("/proc/self/exe", NULL); 328 ASSERT_STREQ(executable_path, p); 329 free(p); 330 } 331 332 TEST(stdlib, qsort) { 333 struct s { 334 char name[16]; 335 static int comparator(const void* lhs, const void* rhs) { 336 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name); 337 } 338 }; 339 s entries[3]; 340 strcpy(entries[0].name, "charlie"); 341 strcpy(entries[1].name, "bravo"); 342 strcpy(entries[2].name, "alpha"); 343 344 qsort(entries, 3, sizeof(s), s::comparator); 345 ASSERT_STREQ("alpha", entries[0].name); 346 ASSERT_STREQ("bravo", entries[1].name); 347 ASSERT_STREQ("charlie", entries[2].name); 348 349 qsort(entries, 3, sizeof(s), s::comparator); 350 ASSERT_STREQ("alpha", entries[0].name); 351 ASSERT_STREQ("bravo", entries[1].name); 352 ASSERT_STREQ("charlie", entries[2].name); 353 } 354 355 static void* TestBug57421_child(void* arg) { 356 pthread_t main_thread = reinterpret_cast<pthread_t>(arg); 357 pthread_join(main_thread, NULL); 358 char* value = getenv("ENVIRONMENT_VARIABLE"); 359 if (value == NULL) { 360 setenv("ENVIRONMENT_VARIABLE", "value", 1); 361 } 362 return NULL; 363 } 364 365 static void TestBug57421_main() { 366 pthread_t t; 367 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self()))); 368 pthread_exit(NULL); 369 } 370 371 // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to 372 // run this test (which exits normally) in its own process. 373 374 class stdlib_DeathTest : public BionicDeathTest {}; 375 376 TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) { 377 // https://code.google.com/p/android/issues/detail?id=57421 378 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), ""); 379 } 380 381 TEST(stdlib, mkostemp64) { 382 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); }); 383 AssertCloseOnExec(tf.fd, true); 384 } 385 386 TEST(stdlib, mkostemp) { 387 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); }); 388 AssertCloseOnExec(tf.fd, true); 389 } 390 391 TEST(stdlib, mkstemp64) { 392 TemporaryFile tf(mkstemp64); 393 struct stat64 sb; 394 ASSERT_EQ(0, fstat64(tf.fd, &sb)); 395 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE); 396 } 397 398 TEST(stdlib, mkstemp) { 399 TemporaryFile tf; 400 struct stat sb; 401 ASSERT_EQ(0, fstat(tf.fd, &sb)); 402 } 403 404 TEST(stdlib, system) { 405 int status; 406 407 status = system("exit 0"); 408 ASSERT_TRUE(WIFEXITED(status)); 409 ASSERT_EQ(0, WEXITSTATUS(status)); 410 411 status = system("exit 1"); 412 ASSERT_TRUE(WIFEXITED(status)); 413 ASSERT_EQ(1, WEXITSTATUS(status)); 414 } 415 416 TEST(stdlib, atof) { 417 ASSERT_DOUBLE_EQ(1.23, atof("1.23")); 418 } 419 420 template <typename T> 421 static void CheckStrToFloat(T fn(const char* s, char** end)) { 422 FpUlpEq<0, T> pred; 423 424 EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr)); 425 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr)); 426 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr)); 427 428 const char* s = " \t\v\f\r\n9.0"; 429 char* p; 430 EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p)); 431 EXPECT_EQ(s + strlen(s), p); 432 433 EXPECT_TRUE(isnan(fn("+nan", nullptr))); 434 EXPECT_TRUE(isnan(fn("nan", nullptr))); 435 EXPECT_TRUE(isnan(fn("-nan", nullptr))); 436 437 EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr))); 438 EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr))); 439 EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr))); 440 441 EXPECT_TRUE(isnan(fn("+nanny", &p))); 442 EXPECT_STREQ("ny", p); 443 EXPECT_TRUE(isnan(fn("nanny", &p))); 444 EXPECT_STREQ("ny", p); 445 EXPECT_TRUE(isnan(fn("-nanny", &p))); 446 EXPECT_STREQ("ny", p); 447 448 EXPECT_EQ(0, fn("muppet", &p)); 449 EXPECT_STREQ("muppet", p); 450 EXPECT_EQ(0, fn(" muppet", &p)); 451 EXPECT_STREQ(" muppet", p); 452 453 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr)); 454 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr)); 455 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr)); 456 457 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr)); 458 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr)); 459 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr)); 460 461 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p)); 462 EXPECT_STREQ("initude", p); 463 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p)); 464 EXPECT_STREQ("initude", p); 465 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p)); 466 EXPECT_STREQ("initude", p); 467 468 // Check case-insensitivity. 469 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr)); 470 EXPECT_TRUE(isnan(fn("NaN", nullptr))); 471 } 472 473 TEST(stdlib, strtod) { 474 CheckStrToFloat(strtod); 475 } 476 477 TEST(stdlib, strtof) { 478 CheckStrToFloat(strtof); 479 } 480 481 TEST(stdlib, strtold) { 482 CheckStrToFloat(strtold); 483 } 484 485 TEST(stdlib, strtof_2206701) { 486 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", NULL)); 487 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", NULL)); 488 } 489 490 TEST(stdlib, strtod_largest_subnormal) { 491 // This value has been known to cause javac and java to infinite loop. 492 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ 493 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", NULL)); 494 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", NULL)); 495 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", NULL)); 496 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", NULL)); 497 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", NULL)); 498 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", NULL)); 499 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", NULL)); 500 } 501 502 TEST(stdlib, quick_exit) { 503 pid_t pid = fork(); 504 ASSERT_NE(-1, pid) << strerror(errno); 505 506 if (pid == 0) { 507 quick_exit(99); 508 } 509 510 AssertChildExited(pid, 99); 511 } 512 513 static int quick_exit_status = 0; 514 515 static void quick_exit_1(void) { 516 ASSERT_EQ(quick_exit_status, 0); 517 quick_exit_status = 1; 518 } 519 520 static void quick_exit_2(void) { 521 ASSERT_EQ(quick_exit_status, 1); 522 } 523 524 static void not_run(void) { 525 FAIL(); 526 } 527 528 TEST(stdlib, at_quick_exit) { 529 pid_t pid = fork(); 530 ASSERT_NE(-1, pid) << strerror(errno); 531 532 if (pid == 0) { 533 ASSERT_EQ(at_quick_exit(quick_exit_2), 0); 534 ASSERT_EQ(at_quick_exit(quick_exit_1), 0); 535 atexit(not_run); 536 quick_exit(99); 537 } 538 539 AssertChildExited(pid, 99); 540 } 541 542 TEST(unistd, _Exit) { 543 pid_t pid = fork(); 544 ASSERT_NE(-1, pid) << strerror(errno); 545 546 if (pid == 0) { 547 _Exit(99); 548 } 549 550 AssertChildExited(pid, 99); 551 } 552 553 TEST(stdlib, pty_smoke) { 554 // getpt returns a pty with O_RDWR|O_NOCTTY. 555 int fd = getpt(); 556 ASSERT_NE(-1, fd); 557 558 // grantpt is a no-op. 559 ASSERT_EQ(0, grantpt(fd)); 560 561 // ptsname_r should start "/dev/pts/". 562 char name_r[128]; 563 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r))); 564 name_r[9] = 0; 565 ASSERT_STREQ("/dev/pts/", name_r); 566 567 close(fd); 568 } 569 570 TEST(stdlib, posix_openpt) { 571 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC); 572 ASSERT_NE(-1, fd); 573 close(fd); 574 } 575 576 TEST(stdlib, ptsname_r_ENOTTY) { 577 errno = 0; 578 char buf[128]; 579 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf))); 580 ASSERT_EQ(ENOTTY, errno); 581 } 582 583 TEST(stdlib, ptsname_r_EINVAL) { 584 int fd = getpt(); 585 ASSERT_NE(-1, fd); 586 errno = 0; 587 char* buf = NULL; 588 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128)); 589 ASSERT_EQ(EINVAL, errno); 590 close(fd); 591 } 592 593 TEST(stdlib, ptsname_r_ERANGE) { 594 int fd = getpt(); 595 ASSERT_NE(-1, fd); 596 errno = 0; 597 char buf[1]; 598 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf))); 599 ASSERT_EQ(ERANGE, errno); 600 close(fd); 601 } 602 603 TEST(stdlib, ttyname) { 604 int fd = getpt(); 605 ASSERT_NE(-1, fd); 606 607 // ttyname returns "/dev/ptmx" for a pty. 608 ASSERT_STREQ("/dev/ptmx", ttyname(fd)); 609 610 close(fd); 611 } 612 613 TEST(stdlib, ttyname_r) { 614 int fd = getpt(); 615 ASSERT_NE(-1, fd); 616 617 // ttyname_r returns "/dev/ptmx" for a pty. 618 char name_r[128]; 619 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r))); 620 ASSERT_STREQ("/dev/ptmx", name_r); 621 622 close(fd); 623 } 624 625 TEST(stdlib, ttyname_r_ENOTTY) { 626 int fd = open("/dev/null", O_WRONLY); 627 errno = 0; 628 char buf[128]; 629 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf))); 630 ASSERT_EQ(ENOTTY, errno); 631 close(fd); 632 } 633 634 TEST(stdlib, ttyname_r_EINVAL) { 635 int fd = getpt(); 636 ASSERT_NE(-1, fd); 637 errno = 0; 638 char* buf = NULL; 639 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128)); 640 ASSERT_EQ(EINVAL, errno); 641 close(fd); 642 } 643 644 TEST(stdlib, ttyname_r_ERANGE) { 645 int fd = getpt(); 646 ASSERT_NE(-1, fd); 647 errno = 0; 648 char buf[1]; 649 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf))); 650 ASSERT_EQ(ERANGE, errno); 651 close(fd); 652 } 653 654 TEST(stdlib, unlockpt_ENOTTY) { 655 int fd = open("/dev/null", O_WRONLY); 656 errno = 0; 657 ASSERT_EQ(-1, unlockpt(fd)); 658 ASSERT_EQ(ENOTTY, errno); 659 close(fd); 660 } 661 662 TEST(stdlib, getsubopt) { 663 char* const tokens[] = { 664 const_cast<char*>("a"), 665 const_cast<char*>("b"), 666 const_cast<char*>("foo"), 667 nullptr 668 }; 669 std::string input = "a,b,foo=bar,a,unknown"; 670 char* subopts = &input[0]; 671 char* value = nullptr; 672 673 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value)); 674 ASSERT_EQ(nullptr, value); 675 ASSERT_EQ(1, getsubopt(&subopts, tokens, &value)); 676 ASSERT_EQ(nullptr, value); 677 ASSERT_EQ(2, getsubopt(&subopts, tokens, &value)); 678 ASSERT_STREQ("bar", value); 679 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value)); 680 ASSERT_EQ(nullptr, value); 681 682 ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value)); 683 } 684 685 TEST(stdlib, mblen) { 686 // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings, 687 // respectively, do or do not have state-dependent encodings." We're always UTF-8. 688 EXPECT_EQ(0, mblen(nullptr, 1)); 689 690 ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8")); 691 692 // 1-byte UTF-8. 693 EXPECT_EQ(1, mblen("abcdef", 6)); 694 // 2-byte UTF-8. 695 EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6)); 696 // 3-byte UTF-8. 697 EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6)); 698 // 4-byte UTF-8. 699 EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6)); 700 701 // Illegal over-long sequence. 702 ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6)); 703 704 // "mblen() shall ... return 0 (if s points to the null byte)". 705 EXPECT_EQ(0, mblen("", 1)); 706 } 707 708 template <typename T> 709 static void CheckStrToInt(T fn(const char* s, char** end, int base)) { 710 char* end_p; 711 712 // Negative base => invalid. 713 errno = 0; 714 ASSERT_EQ(T(0), fn("123", &end_p, -1)); 715 ASSERT_EQ(EINVAL, errno); 716 717 // Base 1 => invalid (base 0 means "please guess"). 718 errno = 0; 719 ASSERT_EQ(T(0), fn("123", &end_p, 1)); 720 ASSERT_EQ(EINVAL, errno); 721 722 // Base > 36 => invalid. 723 errno = 0; 724 ASSERT_EQ(T(0), fn("123", &end_p, 37)); 725 ASSERT_EQ(EINVAL, errno); 726 727 // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'. 728 ASSERT_EQ(T(0), fn("0xy", &end_p, 16)); 729 ASSERT_EQ('x', *end_p); 730 731 if (std::numeric_limits<T>::is_signed) { 732 // Minimum (such as -128). 733 std::string min{std::to_string(std::numeric_limits<T>::min())}; 734 end_p = nullptr; 735 errno = 0; 736 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0)); 737 ASSERT_EQ(0, errno); 738 ASSERT_EQ('\0', *end_p); 739 // Too negative (such as -129). 740 min.back() = (min.back() + 1); 741 end_p = nullptr; 742 errno = 0; 743 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0)); 744 ASSERT_EQ(ERANGE, errno); 745 ASSERT_EQ('\0', *end_p); 746 } 747 748 // Maximum (such as 127). 749 std::string max{std::to_string(std::numeric_limits<T>::max())}; 750 end_p = nullptr; 751 errno = 0; 752 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0)); 753 ASSERT_EQ(0, errno); 754 ASSERT_EQ('\0', *end_p); 755 // Too positive (such as 128). 756 max.back() = (max.back() + 1); 757 end_p = nullptr; 758 errno = 0; 759 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0)); 760 ASSERT_EQ(ERANGE, errno); 761 ASSERT_EQ('\0', *end_p); 762 763 // In case of overflow, strto* leaves us pointing past the end of the number, 764 // not at the digit that overflowed. 765 end_p = nullptr; 766 errno = 0; 767 ASSERT_EQ(std::numeric_limits<T>::max(), 768 fn("99999999999999999999999999999999999999999999999999999abc", &end_p, 0)); 769 ASSERT_EQ(ERANGE, errno); 770 ASSERT_STREQ("abc", end_p); 771 if (std::numeric_limits<T>::is_signed) { 772 end_p = nullptr; 773 errno = 0; 774 ASSERT_EQ(std::numeric_limits<T>::min(), 775 fn("-99999999999999999999999999999999999999999999999999999abc", &end_p, 0)); 776 ASSERT_EQ(ERANGE, errno); 777 ASSERT_STREQ("abc", end_p); 778 } 779 } 780 781 TEST(stdlib, strtol_smoke) { 782 CheckStrToInt(strtol); 783 } 784 785 TEST(stdlib, strtoll_smoke) { 786 CheckStrToInt(strtoll); 787 } 788 789 TEST(stdlib, strtoul_smoke) { 790 CheckStrToInt(strtoul); 791 } 792 793 TEST(stdlib, strtoull_smoke) { 794 CheckStrToInt(strtoull); 795 } 796 797 TEST(stdlib, strtoimax_smoke) { 798 CheckStrToInt(strtoimax); 799 } 800 801 TEST(stdlib, strtoumax_smoke) { 802 CheckStrToInt(strtoumax); 803 } 804 805 TEST(stdlib, abs) { 806 ASSERT_EQ(INT_MAX, abs(-INT_MAX)); 807 ASSERT_EQ(INT_MAX, abs(INT_MAX)); 808 } 809 810 TEST(stdlib, labs) { 811 ASSERT_EQ(LONG_MAX, labs(-LONG_MAX)); 812 ASSERT_EQ(LONG_MAX, labs(LONG_MAX)); 813 } 814 815 TEST(stdlib, llabs) { 816 ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX)); 817 ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX)); 818 } 819