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