Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 17 | #include <errno.h> |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 19 | #include <libgen.h> |
| 20 | #include <limits.h> |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 21 | #include <math.h> |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 22 | #include <pthread.h> |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 23 | #include <stdint.h> |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 25 | #include <sys/cdefs.h> |
Elliott Hughes | 4048856 | 2014-03-12 13:50:38 -0700 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 28 | #include <unistd.h> |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 30 | #include <limits> |
Elliott Hughes | f61a06e | 2017-12-19 16:11:37 -0800 | [diff] [blame] | 31 | #include <string> |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 32 | |
Elliott Hughes | 22fb267 | 2019-11-01 08:07:25 -0700 | [diff] [blame] | 33 | #include <android-base/file.h> |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 34 | #include <android-base/macros.h> |
Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 35 | #include <android-base/silent_death_test.h> |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 36 | #include <gtest/gtest.h> |
| 37 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 38 | #include "math_data_test.h" |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 39 | #include "utils.h" |
| 40 | |
Elliott Hughes | 22fb267 | 2019-11-01 08:07:25 -0700 | [diff] [blame] | 41 | using namespace std::string_literals; |
| 42 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 43 | template <typename T = int (*)(char*)> |
| 44 | class GenericTemporaryFile { |
| 45 | public: |
| 46 | explicit GenericTemporaryFile(T mk_fn = mkstemp) : mk_fn_(mk_fn) { |
| 47 | // Since we might be running on the host or the target, and if we're |
| 48 | // running on the host we might be running under bionic or glibc, |
| 49 | // let's just try both possible temporary directories and take the |
| 50 | // first one that works. |
| 51 | init("/data/local/tmp"); |
| 52 | if (fd == -1) { |
| 53 | init("/tmp"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | ~GenericTemporaryFile() { |
| 58 | close(fd); |
| 59 | unlink(path); |
| 60 | } |
| 61 | |
| 62 | int fd; |
| 63 | char path[1024]; |
| 64 | |
| 65 | private: |
| 66 | T mk_fn_; |
| 67 | |
| 68 | void init(const char* tmp_dir) { |
| 69 | snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir); |
| 70 | fd = mk_fn_(path); |
| 71 | } |
| 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(GenericTemporaryFile); |
| 74 | }; |
| 75 | |
| 76 | typedef GenericTemporaryFile<> MyTemporaryFile; |
| 77 | |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 78 | // The random number generator tests all set the seed, get four values, reset the seed and check |
| 79 | // that they get the first two values repeated, and then reset the seed and check two more values |
| 80 | // to rule out the possibility that we're just going round a cycle of four values. |
| 81 | // TODO: factor this out. |
| 82 | |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 83 | TEST(stdlib, drand48) { |
| 84 | srand48(0x01020304); |
| 85 | EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); |
| 86 | EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); |
| 87 | EXPECT_DOUBLE_EQ(0.42015087072844537, drand48()); |
| 88 | EXPECT_DOUBLE_EQ(0.061637783047395089, drand48()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 89 | srand48(0x01020304); |
| 90 | EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); |
| 91 | EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); |
| 92 | srand48(0x01020304); |
| 93 | EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); |
| 94 | EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); |
| 95 | } |
| 96 | |
| 97 | TEST(stdlib, erand48) { |
| 98 | const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 }; |
| 99 | unsigned short xsubi[3]; |
| 100 | memcpy(xsubi, seed, sizeof(seed)); |
| 101 | EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi)); |
| 102 | EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi)); |
| 103 | EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi)); |
| 104 | EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi)); |
| 105 | memcpy(xsubi, seed, sizeof(seed)); |
| 106 | EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi)); |
| 107 | EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi)); |
| 108 | memcpy(xsubi, seed, sizeof(seed)); |
| 109 | EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi)); |
| 110 | EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi)); |
| 111 | } |
| 112 | |
| 113 | TEST(stdlib, lcong48) { |
| 114 | unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e }; |
| 115 | lcong48(p); |
| 116 | EXPECT_EQ(1531389981, lrand48()); |
| 117 | EXPECT_EQ(1598801533, lrand48()); |
| 118 | EXPECT_EQ(2080534853, lrand48()); |
| 119 | EXPECT_EQ(1102488897, lrand48()); |
| 120 | lcong48(p); |
| 121 | EXPECT_EQ(1531389981, lrand48()); |
| 122 | EXPECT_EQ(1598801533, lrand48()); |
| 123 | lcong48(p); |
| 124 | EXPECT_EQ(1531389981, lrand48()); |
| 125 | EXPECT_EQ(1598801533, lrand48()); |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 128 | TEST(stdlib, lrand48) { |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 129 | srand48(0x01020304); |
| 130 | EXPECT_EQ(1409163720, lrand48()); |
| 131 | EXPECT_EQ(397769746, lrand48()); |
| 132 | EXPECT_EQ(902267124, lrand48()); |
| 133 | EXPECT_EQ(132366131, lrand48()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 134 | srand48(0x01020304); |
| 135 | EXPECT_EQ(1409163720, lrand48()); |
| 136 | EXPECT_EQ(397769746, lrand48()); |
| 137 | srand48(0x01020304); |
| 138 | EXPECT_EQ(1409163720, lrand48()); |
| 139 | EXPECT_EQ(397769746, lrand48()); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 140 | } |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 141 | |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 142 | TEST(stdlib, random) { |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 143 | srandom(0x01020304); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 144 | EXPECT_EQ(55436735, random()); |
| 145 | EXPECT_EQ(1399865117, random()); |
| 146 | EXPECT_EQ(2032643283, random()); |
| 147 | EXPECT_EQ(571329216, random()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 148 | srandom(0x01020304); |
| 149 | EXPECT_EQ(55436735, random()); |
| 150 | EXPECT_EQ(1399865117, random()); |
| 151 | srandom(0x01020304); |
| 152 | EXPECT_EQ(55436735, random()); |
| 153 | EXPECT_EQ(1399865117, random()); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 154 | } |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 155 | |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 156 | TEST(stdlib, rand) { |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 157 | srand(0x01020304); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 158 | EXPECT_EQ(55436735, rand()); |
| 159 | EXPECT_EQ(1399865117, rand()); |
| 160 | EXPECT_EQ(2032643283, rand()); |
| 161 | EXPECT_EQ(571329216, rand()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 162 | srand(0x01020304); |
| 163 | EXPECT_EQ(55436735, rand()); |
| 164 | EXPECT_EQ(1399865117, rand()); |
| 165 | srand(0x01020304); |
| 166 | EXPECT_EQ(55436735, rand()); |
| 167 | EXPECT_EQ(1399865117, rand()); |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST(stdlib, mrand48) { |
| 171 | srand48(0x01020304); |
| 172 | EXPECT_EQ(-1476639856, mrand48()); |
| 173 | EXPECT_EQ(795539493, mrand48()); |
| 174 | EXPECT_EQ(1804534249, mrand48()); |
| 175 | EXPECT_EQ(264732262, mrand48()); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 176 | srand48(0x01020304); |
| 177 | EXPECT_EQ(-1476639856, mrand48()); |
| 178 | EXPECT_EQ(795539493, mrand48()); |
| 179 | srand48(0x01020304); |
| 180 | EXPECT_EQ(-1476639856, mrand48()); |
| 181 | EXPECT_EQ(795539493, mrand48()); |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 182 | } |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 183 | |
Aleksandra Tsvetkova | 608b451 | 2015-02-27 15:01:59 +0300 | [diff] [blame] | 184 | TEST(stdlib, jrand48_distribution) { |
| 185 | const int iterations = 4096; |
| 186 | const int pivot_low = 1536; |
| 187 | const int pivot_high = 2560; |
| 188 | |
| 189 | unsigned short xsubi[3]; |
| 190 | int bits[32] = {}; |
| 191 | |
| 192 | for (int iter = 0; iter < iterations; ++iter) { |
| 193 | long rand_val = jrand48(xsubi); |
| 194 | for (int bit = 0; bit < 32; ++bit) { |
| 195 | bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Check that bit probability is uniform |
| 200 | for (int bit = 0; bit < 32; ++bit) { |
| 201 | EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | TEST(stdlib, mrand48_distribution) { |
| 206 | const int iterations = 4096; |
| 207 | const int pivot_low = 1536; |
| 208 | const int pivot_high = 2560; |
| 209 | |
| 210 | int bits[32] = {}; |
| 211 | |
| 212 | for (int iter = 0; iter < iterations; ++iter) { |
| 213 | long rand_val = mrand48(); |
| 214 | for (int bit = 0; bit < 32; ++bit) { |
| 215 | bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Check that bit probability is uniform |
| 220 | for (int bit = 0; bit < 32; ++bit) { |
| 221 | EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high)); |
| 222 | } |
| 223 | } |
| 224 | |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 225 | TEST(stdlib, posix_memalign_sweep) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 226 | SKIP_WITH_HWASAN; |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 227 | void* ptr; |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 228 | |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 229 | // These should all fail. |
| 230 | for (size_t align = 0; align < sizeof(long); align++) { |
| 231 | ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256)) |
| 232 | << "Unexpected value at align " << align; |
| 233 | } |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 234 | |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 235 | // Verify powers of 2 up to 2048 allocate, and verify that all other |
| 236 | // alignment values between the powers of 2 fail. |
| 237 | size_t last_align = sizeof(long); |
| 238 | for (size_t align = sizeof(long); align <= 2048; align <<= 1) { |
| 239 | // Try all of the non power of 2 values from the last until this value. |
| 240 | for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) { |
| 241 | ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256)) |
| 242 | << "Unexpected success at align " << fail_align; |
| 243 | } |
| 244 | ASSERT_EQ(0, posix_memalign(&ptr, align, 256)) |
| 245 | << "Unexpected failure at align " << align; |
| 246 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 247 | << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align; |
| 248 | free(ptr); |
| 249 | last_align = align; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | TEST(stdlib, posix_memalign_various_sizes) { |
| 254 | std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000}; |
| 255 | for (auto size : sizes) { |
| 256 | void* ptr; |
| 257 | ASSERT_EQ(0, posix_memalign(&ptr, 16, 1)) |
| 258 | << "posix_memalign failed at size " << size; |
| 259 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf) |
| 260 | << "Pointer not aligned at size " << size << " ptr " << ptr; |
| 261 | free(ptr); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | TEST(stdlib, posix_memalign_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 266 | SKIP_WITH_HWASAN; |
Christopher Ferris | 3a32d95 | 2017-06-15 13:30:44 -0700 | [diff] [blame] | 267 | void* ptr; |
| 268 | ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX)); |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 269 | } |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 270 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 271 | TEST(stdlib, aligned_alloc_sweep) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 272 | SKIP_WITH_HWASAN; |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 273 | // Verify powers of 2 up to 2048 allocate, and verify that all other |
| 274 | // alignment values between the powers of 2 fail. |
| 275 | size_t last_align = 1; |
| 276 | for (size_t align = 1; align <= 2048; align <<= 1) { |
| 277 | // Try all of the non power of 2 values from the last until this value. |
| 278 | for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) { |
Christopher Ferris | a22f5d5 | 2019-03-01 16:40:59 -0800 | [diff] [blame] | 279 | ASSERT_TRUE(aligned_alloc(fail_align, fail_align) == nullptr) |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 280 | << "Unexpected success at align " << fail_align; |
| 281 | ASSERT_EQ(EINVAL, errno) << "Unexpected errno at align " << fail_align; |
| 282 | } |
Christopher Ferris | a22f5d5 | 2019-03-01 16:40:59 -0800 | [diff] [blame] | 283 | void* ptr = aligned_alloc(align, 2 * align); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 284 | ASSERT_TRUE(ptr != nullptr) << "Unexpected failure at align " << align; |
| 285 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 286 | << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align; |
| 287 | free(ptr); |
| 288 | last_align = align; |
| 289 | } |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | TEST(stdlib, aligned_alloc_overflow) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 293 | SKIP_WITH_HWASAN; |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 294 | ASSERT_TRUE(aligned_alloc(16, SIZE_MAX) == nullptr); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | TEST(stdlib, aligned_alloc_size_not_multiple_of_alignment) { |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 298 | SKIP_WITH_HWASAN; |
Christopher Ferris | a22f5d5 | 2019-03-01 16:40:59 -0800 | [diff] [blame] | 299 | |
| 300 | ASSERT_TRUE(aligned_alloc(2048, 1) == nullptr); |
| 301 | ASSERT_TRUE(aligned_alloc(4, 3) == nullptr); |
| 302 | ASSERT_TRUE(aligned_alloc(4, 7) == nullptr); |
| 303 | ASSERT_TRUE(aligned_alloc(16, 8) == nullptr); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 306 | TEST(stdlib, realpath__NULL_filename) { |
| 307 | errno = 0; |
George Burgess IV | 95bd488 | 2017-08-14 14:48:55 -0700 | [diff] [blame] | 308 | // Work around the compile-time error generated by FORTIFY here. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 309 | const char* path = nullptr; |
| 310 | char* p = realpath(path, nullptr); |
| 311 | ASSERT_TRUE(p == nullptr); |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 312 | ASSERT_EQ(EINVAL, errno); |
| 313 | } |
| 314 | |
| 315 | TEST(stdlib, realpath__empty_filename) { |
| 316 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 317 | char* p = realpath("", nullptr); |
| 318 | ASSERT_TRUE(p == nullptr); |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 319 | ASSERT_EQ(ENOENT, errno); |
| 320 | } |
| 321 | |
| 322 | TEST(stdlib, realpath__ENOENT) { |
| 323 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 324 | char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", nullptr); |
| 325 | ASSERT_TRUE(p == nullptr); |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 326 | ASSERT_EQ(ENOENT, errno); |
| 327 | } |
| 328 | |
Elliott Hughes | 22fb267 | 2019-11-01 08:07:25 -0700 | [diff] [blame] | 329 | TEST(stdlib, realpath__ELOOP) { |
| 330 | TemporaryDir td; |
| 331 | std::string link = std::string(td.path) + "/loop"; |
| 332 | ASSERT_EQ(0, symlink(link.c_str(), link.c_str())); |
| 333 | |
| 334 | errno = 0; |
| 335 | char* p = realpath(link.c_str(), nullptr); |
| 336 | ASSERT_TRUE(p == nullptr); |
| 337 | ASSERT_EQ(ELOOP, errno); |
| 338 | } |
| 339 | |
Elliott Hughes | 31e072f | 2014-09-30 16:15:42 -0700 | [diff] [blame] | 340 | TEST(stdlib, realpath__component_after_non_directory) { |
| 341 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 342 | char* p = realpath("/dev/null/.", nullptr); |
| 343 | ASSERT_TRUE(p == nullptr); |
Elliott Hughes | 31e072f | 2014-09-30 16:15:42 -0700 | [diff] [blame] | 344 | ASSERT_EQ(ENOTDIR, errno); |
| 345 | |
| 346 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 347 | p = realpath("/dev/null/..", nullptr); |
| 348 | ASSERT_TRUE(p == nullptr); |
Elliott Hughes | 31e072f | 2014-09-30 16:15:42 -0700 | [diff] [blame] | 349 | ASSERT_EQ(ENOTDIR, errno); |
| 350 | } |
| 351 | |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 352 | TEST(stdlib, realpath) { |
| 353 | // Get the name of this executable. |
| 354 | char executable_path[PATH_MAX]; |
| 355 | int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); |
| 356 | ASSERT_NE(rc, -1); |
| 357 | executable_path[rc] = '\0'; |
| 358 | |
| 359 | char buf[PATH_MAX + 1]; |
| 360 | char* p = realpath("/proc/self/exe", buf); |
| 361 | ASSERT_STREQ(executable_path, p); |
| 362 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 363 | p = realpath("/proc/self/exe", nullptr); |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 364 | ASSERT_STREQ(executable_path, p); |
| 365 | free(p); |
| 366 | } |
Elliott Hughes | 0b25f63 | 2013-04-11 18:08:34 -0700 | [diff] [blame] | 367 | |
Elliott Hughes | 22fb267 | 2019-11-01 08:07:25 -0700 | [diff] [blame] | 368 | TEST(stdlib, realpath__dot) { |
| 369 | char* p = realpath("/proc/./version", nullptr); |
| 370 | ASSERT_STREQ("/proc/version", p); |
| 371 | free(p); |
| 372 | } |
| 373 | |
| 374 | TEST(stdlib, realpath__dot_dot) { |
| 375 | char* p = realpath("/dev/../proc/version", nullptr); |
| 376 | ASSERT_STREQ("/proc/version", p); |
| 377 | free(p); |
| 378 | } |
| 379 | |
| 380 | TEST(stdlib, realpath__deleted) { |
| 381 | TemporaryDir td; |
| 382 | |
| 383 | // Create a file "A". |
| 384 | std::string A_path = td.path + "/A"s; |
| 385 | ASSERT_TRUE(android::base::WriteStringToFile("test\n", A_path)); |
| 386 | |
| 387 | // Get an O_PATH fd for it. |
| 388 | android::base::unique_fd fd(open(A_path.c_str(), O_PATH)); |
| 389 | ASSERT_NE(fd, -1); |
| 390 | |
| 391 | // Create a file "A (deleted)". |
| 392 | android::base::unique_fd fd2(open((td.path + "/A (deleted)"s).c_str(), |
| 393 | O_CREAT | O_TRUNC | O_WRONLY, 0644)); |
| 394 | ASSERT_NE(fd2, -1); |
| 395 | |
| 396 | // Delete "A". |
| 397 | ASSERT_EQ(0, unlink(A_path.c_str())); |
| 398 | |
| 399 | // Now realpath() on the O_PATH fd, and check we *don't* get "A (deleted)". |
| 400 | std::string path = android::base::StringPrintf("/proc/%d/fd/%d", static_cast<int>(getpid()), |
| 401 | fd.get()); |
| 402 | errno = 0; |
| 403 | char* result = realpath(path.c_str(), nullptr); |
| 404 | ASSERT_EQ(nullptr, result) << result; |
| 405 | ASSERT_EQ(ENOENT, errno); |
| 406 | free(result); |
| 407 | } |
| 408 | |
Elliott Hughes | 0b25f63 | 2013-04-11 18:08:34 -0700 | [diff] [blame] | 409 | TEST(stdlib, qsort) { |
| 410 | struct s { |
| 411 | char name[16]; |
| 412 | static int comparator(const void* lhs, const void* rhs) { |
| 413 | return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name); |
| 414 | } |
| 415 | }; |
| 416 | s entries[3]; |
| 417 | strcpy(entries[0].name, "charlie"); |
| 418 | strcpy(entries[1].name, "bravo"); |
| 419 | strcpy(entries[2].name, "alpha"); |
| 420 | |
| 421 | qsort(entries, 3, sizeof(s), s::comparator); |
| 422 | ASSERT_STREQ("alpha", entries[0].name); |
| 423 | ASSERT_STREQ("bravo", entries[1].name); |
| 424 | ASSERT_STREQ("charlie", entries[2].name); |
| 425 | |
| 426 | qsort(entries, 3, sizeof(s), s::comparator); |
| 427 | ASSERT_STREQ("alpha", entries[0].name); |
| 428 | ASSERT_STREQ("bravo", entries[1].name); |
| 429 | ASSERT_STREQ("charlie", entries[2].name); |
| 430 | } |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 431 | |
| 432 | static void* TestBug57421_child(void* arg) { |
| 433 | pthread_t main_thread = reinterpret_cast<pthread_t>(arg); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 434 | pthread_join(main_thread, nullptr); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 435 | char* value = getenv("ENVIRONMENT_VARIABLE"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 436 | if (value == nullptr) { |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 437 | setenv("ENVIRONMENT_VARIABLE", "value", 1); |
| 438 | } |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 439 | return nullptr; |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static void TestBug57421_main() { |
| 443 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 444 | ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug57421_child, reinterpret_cast<void*>(pthread_self()))); |
| 445 | pthread_exit(nullptr); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 449 | // run this test (which exits normally) in its own process. |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 450 | |
Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 451 | using stdlib_DeathTest = SilentDeathTest; |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 452 | |
| 453 | TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) { |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 454 | // https://code.google.com/p/android/issues/detail?id=57421 |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 455 | ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), ""); |
| 456 | } |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 457 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 458 | TEST(stdlib, mkostemp64_smoke) { |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 459 | MyTemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); }); |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 460 | ASSERT_TRUE(CloseOnExec(tf.fd)); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | TEST(stdlib, mkostemp) { |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 464 | MyTemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); }); |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 465 | ASSERT_TRUE(CloseOnExec(tf.fd)); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 468 | TEST(stdlib, mkstemp64_smoke) { |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 469 | MyTemporaryFile tf(mkstemp64); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 470 | struct stat64 sb; |
| 471 | ASSERT_EQ(0, fstat64(tf.fd, &sb)); |
| 472 | ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE); |
| 473 | } |
| 474 | |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 475 | TEST(stdlib, mkstemp) { |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 476 | MyTemporaryFile tf(mkstemp); |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 477 | struct stat sb; |
| 478 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 479 | } |
| 480 | |
Elliott Hughes | 3cdf573 | 2014-03-11 12:54:44 -0700 | [diff] [blame] | 481 | TEST(stdlib, system) { |
| 482 | int status; |
| 483 | |
| 484 | status = system("exit 0"); |
| 485 | ASSERT_TRUE(WIFEXITED(status)); |
| 486 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 487 | |
| 488 | status = system("exit 1"); |
| 489 | ASSERT_TRUE(WIFEXITED(status)); |
| 490 | ASSERT_EQ(1, WEXITSTATUS(status)); |
| 491 | } |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 492 | |
Elliott Hughes | bbbe27f | 2021-03-08 14:07:01 -0800 | [diff] [blame] | 493 | TEST(stdlib, system_NULL) { |
| 494 | // "The system() function shall always return non-zero when command is NULL." |
| 495 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/system.html |
| 496 | ASSERT_NE(0, system(nullptr)); |
| 497 | } |
| 498 | |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 499 | TEST(stdlib, atof) { |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 500 | ASSERT_DOUBLE_EQ(1.23, atof("1.23")); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 503 | template <typename T> |
| 504 | static void CheckStrToFloat(T fn(const char* s, char** end)) { |
| 505 | FpUlpEq<0, T> pred; |
| 506 | |
| 507 | EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr)); |
| 508 | EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr)); |
| 509 | EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr)); |
| 510 | |
Dan Albert | f634655 | 2016-12-02 12:02:03 -0800 | [diff] [blame] | 511 | const char* s = " \t\v\f\r\n9.0"; |
| 512 | char* p; |
| 513 | EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p)); |
| 514 | EXPECT_EQ(s + strlen(s), p); |
| 515 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 516 | EXPECT_TRUE(isnan(fn("+nan", nullptr))); |
| 517 | EXPECT_TRUE(isnan(fn("nan", nullptr))); |
| 518 | EXPECT_TRUE(isnan(fn("-nan", nullptr))); |
| 519 | |
| 520 | EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr))); |
| 521 | EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr))); |
| 522 | EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr))); |
| 523 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 524 | EXPECT_TRUE(isnan(fn("+nanny", &p))); |
| 525 | EXPECT_STREQ("ny", p); |
| 526 | EXPECT_TRUE(isnan(fn("nanny", &p))); |
| 527 | EXPECT_STREQ("ny", p); |
| 528 | EXPECT_TRUE(isnan(fn("-nanny", &p))); |
| 529 | EXPECT_STREQ("ny", p); |
| 530 | |
| 531 | EXPECT_EQ(0, fn("muppet", &p)); |
| 532 | EXPECT_STREQ("muppet", p); |
| 533 | EXPECT_EQ(0, fn(" muppet", &p)); |
| 534 | EXPECT_STREQ(" muppet", p); |
| 535 | |
| 536 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr)); |
| 537 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr)); |
| 538 | EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr)); |
| 539 | |
| 540 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr)); |
| 541 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr)); |
| 542 | EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr)); |
| 543 | |
| 544 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p)); |
| 545 | EXPECT_STREQ("initude", p); |
| 546 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p)); |
| 547 | EXPECT_STREQ("initude", p); |
| 548 | EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p)); |
| 549 | EXPECT_STREQ("initude", p); |
| 550 | |
| 551 | // Check case-insensitivity. |
| 552 | EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr)); |
| 553 | EXPECT_TRUE(isnan(fn("NaN", nullptr))); |
| 554 | } |
| 555 | |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 556 | TEST(stdlib, strtod) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 557 | CheckStrToFloat(strtod); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | TEST(stdlib, strtof) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 561 | CheckStrToFloat(strtof); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | TEST(stdlib, strtold) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 565 | CheckStrToFloat(strtold); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 566 | } |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 567 | |
Elliott Hughes | 89aaaff | 2014-10-28 17:54:23 -0700 | [diff] [blame] | 568 | TEST(stdlib, strtof_2206701) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 569 | ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", nullptr)); |
| 570 | ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", nullptr)); |
Elliott Hughes | 89aaaff | 2014-10-28 17:54:23 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | TEST(stdlib, strtod_largest_subnormal) { |
| 574 | // This value has been known to cause javac and java to infinite loop. |
| 575 | // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 576 | ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", nullptr)); |
| 577 | ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", nullptr)); |
| 578 | ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", nullptr)); |
| 579 | ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", nullptr)); |
| 580 | ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", nullptr)); |
| 581 | ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", nullptr)); |
| 582 | ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", nullptr)); |
Elliott Hughes | 89aaaff | 2014-10-28 17:54:23 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 585 | TEST(stdlib, quick_exit) { |
| 586 | pid_t pid = fork(); |
| 587 | ASSERT_NE(-1, pid) << strerror(errno); |
| 588 | |
| 589 | if (pid == 0) { |
| 590 | quick_exit(99); |
| 591 | } |
| 592 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 593 | AssertChildExited(pid, 99); |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static int quick_exit_status = 0; |
| 597 | |
| 598 | static void quick_exit_1(void) { |
| 599 | ASSERT_EQ(quick_exit_status, 0); |
| 600 | quick_exit_status = 1; |
| 601 | } |
| 602 | |
| 603 | static void quick_exit_2(void) { |
| 604 | ASSERT_EQ(quick_exit_status, 1); |
| 605 | } |
| 606 | |
| 607 | static void not_run(void) { |
| 608 | FAIL(); |
| 609 | } |
| 610 | |
| 611 | TEST(stdlib, at_quick_exit) { |
| 612 | pid_t pid = fork(); |
| 613 | ASSERT_NE(-1, pid) << strerror(errno); |
| 614 | |
| 615 | if (pid == 0) { |
| 616 | ASSERT_EQ(at_quick_exit(quick_exit_2), 0); |
| 617 | ASSERT_EQ(at_quick_exit(quick_exit_1), 0); |
| 618 | atexit(not_run); |
| 619 | quick_exit(99); |
| 620 | } |
| 621 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 622 | AssertChildExited(pid, 99); |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 625 | TEST(unistd, _Exit) { |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 626 | pid_t pid = fork(); |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 627 | ASSERT_NE(-1, pid) << strerror(errno); |
| 628 | |
| 629 | if (pid == 0) { |
| 630 | _Exit(99); |
| 631 | } |
| 632 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 633 | AssertChildExited(pid, 99); |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 634 | } |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 635 | |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 636 | #if defined(ANDROID_HOST_MUSL) |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 637 | // musl doesn't have getpt |
| 638 | int getpt() { |
| 639 | return posix_openpt(O_RDWR|O_NOCTTY); |
| 640 | } |
| 641 | #endif |
| 642 | |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 643 | TEST(stdlib, pty_smoke) { |
| 644 | // getpt returns a pty with O_RDWR|O_NOCTTY. |
| 645 | int fd = getpt(); |
| 646 | ASSERT_NE(-1, fd); |
| 647 | |
| 648 | // grantpt is a no-op. |
| 649 | ASSERT_EQ(0, grantpt(fd)); |
| 650 | |
| 651 | // ptsname_r should start "/dev/pts/". |
| 652 | char name_r[128]; |
| 653 | ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r))); |
| 654 | name_r[9] = 0; |
| 655 | ASSERT_STREQ("/dev/pts/", name_r); |
| 656 | |
| 657 | close(fd); |
| 658 | } |
| 659 | |
| 660 | TEST(stdlib, posix_openpt) { |
| 661 | int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC); |
| 662 | ASSERT_NE(-1, fd); |
| 663 | close(fd); |
| 664 | } |
| 665 | |
| 666 | TEST(stdlib, ptsname_r_ENOTTY) { |
| 667 | errno = 0; |
| 668 | char buf[128]; |
| 669 | ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf))); |
| 670 | ASSERT_EQ(ENOTTY, errno); |
| 671 | } |
| 672 | |
| 673 | TEST(stdlib, ptsname_r_EINVAL) { |
| 674 | int fd = getpt(); |
| 675 | ASSERT_NE(-1, fd); |
| 676 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 677 | char* buf = nullptr; |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 678 | ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128)); |
| 679 | ASSERT_EQ(EINVAL, errno); |
| 680 | close(fd); |
| 681 | } |
| 682 | |
| 683 | TEST(stdlib, ptsname_r_ERANGE) { |
| 684 | int fd = getpt(); |
| 685 | ASSERT_NE(-1, fd); |
| 686 | errno = 0; |
| 687 | char buf[1]; |
| 688 | ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf))); |
| 689 | ASSERT_EQ(ERANGE, errno); |
| 690 | close(fd); |
| 691 | } |
| 692 | |
Elliott Hughes | 728cde5 | 2017-11-08 21:53:50 -0800 | [diff] [blame] | 693 | TEST(stdlib, ttyname) { |
| 694 | int fd = getpt(); |
| 695 | ASSERT_NE(-1, fd); |
| 696 | |
| 697 | // ttyname returns "/dev/ptmx" for a pty. |
| 698 | ASSERT_STREQ("/dev/ptmx", ttyname(fd)); |
| 699 | |
| 700 | close(fd); |
| 701 | } |
| 702 | |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 703 | TEST(stdlib, ttyname_r) { |
| 704 | int fd = getpt(); |
| 705 | ASSERT_NE(-1, fd); |
| 706 | |
| 707 | // ttyname_r returns "/dev/ptmx" for a pty. |
| 708 | char name_r[128]; |
| 709 | ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r))); |
| 710 | ASSERT_STREQ("/dev/ptmx", name_r); |
| 711 | |
| 712 | close(fd); |
| 713 | } |
| 714 | |
| 715 | TEST(stdlib, ttyname_r_ENOTTY) { |
| 716 | int fd = open("/dev/null", O_WRONLY); |
| 717 | errno = 0; |
| 718 | char buf[128]; |
| 719 | ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf))); |
| 720 | ASSERT_EQ(ENOTTY, errno); |
| 721 | close(fd); |
| 722 | } |
| 723 | |
| 724 | TEST(stdlib, ttyname_r_EINVAL) { |
| 725 | int fd = getpt(); |
| 726 | ASSERT_NE(-1, fd); |
| 727 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 728 | char* buf = nullptr; |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 729 | ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128)); |
| 730 | ASSERT_EQ(EINVAL, errno); |
| 731 | close(fd); |
| 732 | } |
| 733 | |
| 734 | TEST(stdlib, ttyname_r_ERANGE) { |
| 735 | int fd = getpt(); |
| 736 | ASSERT_NE(-1, fd); |
| 737 | errno = 0; |
| 738 | char buf[1]; |
| 739 | ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf))); |
| 740 | ASSERT_EQ(ERANGE, errno); |
| 741 | close(fd); |
| 742 | } |
| 743 | |
| 744 | TEST(stdlib, unlockpt_ENOTTY) { |
| 745 | int fd = open("/dev/null", O_WRONLY); |
| 746 | errno = 0; |
| 747 | ASSERT_EQ(-1, unlockpt(fd)); |
| 748 | ASSERT_EQ(ENOTTY, errno); |
| 749 | close(fd); |
| 750 | } |
Elliott Hughes | b05ec5a | 2014-09-23 14:53:10 -0700 | [diff] [blame] | 751 | |
Elliott Hughes | df143f8 | 2016-04-04 17:34:04 -0700 | [diff] [blame] | 752 | TEST(stdlib, getsubopt) { |
| 753 | char* const tokens[] = { |
| 754 | const_cast<char*>("a"), |
| 755 | const_cast<char*>("b"), |
| 756 | const_cast<char*>("foo"), |
| 757 | nullptr |
| 758 | }; |
| 759 | std::string input = "a,b,foo=bar,a,unknown"; |
| 760 | char* subopts = &input[0]; |
| 761 | char* value = nullptr; |
| 762 | |
| 763 | ASSERT_EQ(0, getsubopt(&subopts, tokens, &value)); |
| 764 | ASSERT_EQ(nullptr, value); |
| 765 | ASSERT_EQ(1, getsubopt(&subopts, tokens, &value)); |
| 766 | ASSERT_EQ(nullptr, value); |
| 767 | ASSERT_EQ(2, getsubopt(&subopts, tokens, &value)); |
| 768 | ASSERT_STREQ("bar", value); |
| 769 | ASSERT_EQ(0, getsubopt(&subopts, tokens, &value)); |
| 770 | ASSERT_EQ(nullptr, value); |
| 771 | |
| 772 | ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value)); |
| 773 | } |
Elliott Hughes | 6f6f905 | 2016-04-28 14:54:52 -0700 | [diff] [blame] | 774 | |
| 775 | TEST(stdlib, mblen) { |
| 776 | // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings, |
| 777 | // respectively, do or do not have state-dependent encodings." We're always UTF-8. |
| 778 | EXPECT_EQ(0, mblen(nullptr, 1)); |
| 779 | |
| 780 | ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8")); |
| 781 | |
| 782 | // 1-byte UTF-8. |
| 783 | EXPECT_EQ(1, mblen("abcdef", 6)); |
| 784 | // 2-byte UTF-8. |
| 785 | EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6)); |
| 786 | // 3-byte UTF-8. |
| 787 | EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6)); |
| 788 | // 4-byte UTF-8. |
| 789 | EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6)); |
| 790 | |
| 791 | // Illegal over-long sequence. |
| 792 | ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6)); |
| 793 | |
| 794 | // "mblen() shall ... return 0 (if s points to the null byte)". |
| 795 | EXPECT_EQ(0, mblen("", 1)); |
| 796 | } |
Elliott Hughes | f826a37 | 2017-07-13 09:35:15 -0700 | [diff] [blame] | 797 | |
| 798 | template <typename T> |
| 799 | static void CheckStrToInt(T fn(const char* s, char** end, int base)) { |
| 800 | char* end_p; |
| 801 | |
| 802 | // Negative base => invalid. |
| 803 | errno = 0; |
| 804 | ASSERT_EQ(T(0), fn("123", &end_p, -1)); |
| 805 | ASSERT_EQ(EINVAL, errno); |
| 806 | |
| 807 | // Base 1 => invalid (base 0 means "please guess"). |
| 808 | errno = 0; |
| 809 | ASSERT_EQ(T(0), fn("123", &end_p, 1)); |
| 810 | ASSERT_EQ(EINVAL, errno); |
| 811 | |
| 812 | // Base > 36 => invalid. |
| 813 | errno = 0; |
| 814 | ASSERT_EQ(T(0), fn("123", &end_p, 37)); |
| 815 | ASSERT_EQ(EINVAL, errno); |
| 816 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 817 | // Both leading + or - are always allowed (even for the strtou* family). |
| 818 | ASSERT_EQ(T(-123), fn("-123", &end_p, 10)); |
| 819 | ASSERT_EQ(T(123), fn("+123", &end_p, 10)); |
| 820 | |
Elliott Hughes | f826a37 | 2017-07-13 09:35:15 -0700 | [diff] [blame] | 821 | // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'. |
| 822 | ASSERT_EQ(T(0), fn("0xy", &end_p, 16)); |
| 823 | ASSERT_EQ('x', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 824 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 825 | // Hexadecimal (both the 0x and the digits) is case-insensitive. |
| 826 | ASSERT_EQ(T(0xab), fn("0xab", &end_p, 0)); |
| 827 | ASSERT_EQ(T(0xab), fn("0Xab", &end_p, 0)); |
| 828 | ASSERT_EQ(T(0xab), fn("0xAB", &end_p, 0)); |
| 829 | ASSERT_EQ(T(0xab), fn("0XAB", &end_p, 0)); |
| 830 | ASSERT_EQ(T(0xab), fn("0xAb", &end_p, 0)); |
| 831 | ASSERT_EQ(T(0xab), fn("0XAb", &end_p, 0)); |
| 832 | |
| 833 | // Octal lives! (Sadly.) |
| 834 | ASSERT_EQ(T(0666), fn("0666", &end_p, 0)); |
| 835 | |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 836 | if (std::numeric_limits<T>::is_signed) { |
| 837 | // Minimum (such as -128). |
Elliott Hughes | f61a06e | 2017-12-19 16:11:37 -0800 | [diff] [blame] | 838 | std::string min{std::to_string(std::numeric_limits<T>::min())}; |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 839 | end_p = nullptr; |
| 840 | errno = 0; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 841 | ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0)); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 842 | ASSERT_EQ(0, errno); |
| 843 | ASSERT_EQ('\0', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 844 | // Too negative (such as -129). |
| 845 | min.back() = (min.back() + 1); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 846 | end_p = nullptr; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 847 | errno = 0; |
| 848 | ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0)); |
| 849 | ASSERT_EQ(ERANGE, errno); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 850 | ASSERT_EQ('\0', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | // Maximum (such as 127). |
Elliott Hughes | f61a06e | 2017-12-19 16:11:37 -0800 | [diff] [blame] | 854 | std::string max{std::to_string(std::numeric_limits<T>::max())}; |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 855 | end_p = nullptr; |
| 856 | errno = 0; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 857 | ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0)); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 858 | ASSERT_EQ(0, errno); |
| 859 | ASSERT_EQ('\0', *end_p); |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 860 | // Too positive (such as 128). |
| 861 | max.back() = (max.back() + 1); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 862 | end_p = nullptr; |
Elliott Hughes | 1921dce | 2017-12-19 10:27:27 -0800 | [diff] [blame] | 863 | errno = 0; |
| 864 | ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0)); |
| 865 | ASSERT_EQ(ERANGE, errno); |
Elliott Hughes | cb239bd | 2017-12-20 17:37:11 -0800 | [diff] [blame] | 866 | ASSERT_EQ('\0', *end_p); |
| 867 | |
| 868 | // In case of overflow, strto* leaves us pointing past the end of the number, |
| 869 | // not at the digit that overflowed. |
| 870 | end_p = nullptr; |
| 871 | errno = 0; |
| 872 | ASSERT_EQ(std::numeric_limits<T>::max(), |
| 873 | fn("99999999999999999999999999999999999999999999999999999abc", &end_p, 0)); |
| 874 | ASSERT_EQ(ERANGE, errno); |
| 875 | ASSERT_STREQ("abc", end_p); |
| 876 | if (std::numeric_limits<T>::is_signed) { |
| 877 | end_p = nullptr; |
| 878 | errno = 0; |
| 879 | ASSERT_EQ(std::numeric_limits<T>::min(), |
| 880 | fn("-99999999999999999999999999999999999999999999999999999abc", &end_p, 0)); |
| 881 | ASSERT_EQ(ERANGE, errno); |
| 882 | ASSERT_STREQ("abc", end_p); |
| 883 | } |
Elliott Hughes | f826a37 | 2017-07-13 09:35:15 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | TEST(stdlib, strtol_smoke) { |
| 887 | CheckStrToInt(strtol); |
| 888 | } |
| 889 | |
| 890 | TEST(stdlib, strtoll_smoke) { |
| 891 | CheckStrToInt(strtoll); |
| 892 | } |
| 893 | |
| 894 | TEST(stdlib, strtoul_smoke) { |
| 895 | CheckStrToInt(strtoul); |
| 896 | } |
| 897 | |
| 898 | TEST(stdlib, strtoull_smoke) { |
| 899 | CheckStrToInt(strtoull); |
| 900 | } |
| 901 | |
| 902 | TEST(stdlib, strtoimax_smoke) { |
| 903 | CheckStrToInt(strtoimax); |
| 904 | } |
| 905 | |
| 906 | TEST(stdlib, strtoumax_smoke) { |
| 907 | CheckStrToInt(strtoumax); |
| 908 | } |
Elliott Hughes | 00d8a8b | 2017-09-07 16:42:13 -0700 | [diff] [blame] | 909 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 910 | TEST(stdlib, atoi) { |
| 911 | // Implemented using strtol in bionic, so extensive testing unnecessary. |
| 912 | ASSERT_EQ(123, atoi("123four")); |
| 913 | ASSERT_EQ(0, atoi("hello")); |
| 914 | } |
| 915 | |
| 916 | TEST(stdlib, atol) { |
| 917 | // Implemented using strtol in bionic, so extensive testing unnecessary. |
| 918 | ASSERT_EQ(123L, atol("123four")); |
| 919 | ASSERT_EQ(0L, atol("hello")); |
| 920 | } |
| 921 | |
Elliott Hughes | 00d8a8b | 2017-09-07 16:42:13 -0700 | [diff] [blame] | 922 | TEST(stdlib, abs) { |
| 923 | ASSERT_EQ(INT_MAX, abs(-INT_MAX)); |
| 924 | ASSERT_EQ(INT_MAX, abs(INT_MAX)); |
| 925 | } |
| 926 | |
| 927 | TEST(stdlib, labs) { |
| 928 | ASSERT_EQ(LONG_MAX, labs(-LONG_MAX)); |
| 929 | ASSERT_EQ(LONG_MAX, labs(LONG_MAX)); |
| 930 | } |
| 931 | |
| 932 | TEST(stdlib, llabs) { |
| 933 | ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX)); |
| 934 | ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX)); |
| 935 | } |
Elliott Hughes | 2d0b28b | 2018-10-23 11:23:00 -0700 | [diff] [blame] | 936 | |
| 937 | TEST(stdlib, getloadavg) { |
| 938 | double load[3]; |
| 939 | |
| 940 | // The second argument should have been size_t. |
| 941 | ASSERT_EQ(-1, getloadavg(load, -1)); |
| 942 | ASSERT_EQ(-1, getloadavg(load, INT_MIN)); |
| 943 | |
| 944 | // Zero is a no-op. |
| 945 | ASSERT_EQ(0, getloadavg(load, 0)); |
| 946 | |
| 947 | // The Linux kernel doesn't support more than 3 (but you can ask for fewer). |
| 948 | ASSERT_EQ(1, getloadavg(load, 1)); |
| 949 | ASSERT_EQ(2, getloadavg(load, 2)); |
| 950 | ASSERT_EQ(3, getloadavg(load, 3)); |
| 951 | ASSERT_EQ(3, getloadavg(load, 4)); |
| 952 | ASSERT_EQ(3, getloadavg(load, INT_MAX)); |
| 953 | |
| 954 | // Read /proc/loadavg and check that it's "close enough". |
Elliott Hughes | 2d0b28b | 2018-10-23 11:23:00 -0700 | [diff] [blame] | 955 | double expected[3]; |
| 956 | std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/loadavg", "re"), fclose}; |
| 957 | ASSERT_EQ(3, fscanf(fp.get(), "%lf %lf %lf", &expected[0], &expected[1], &expected[2])); |
Elliott Hughes | 72a54a4 | 2018-12-18 14:47:25 -0800 | [diff] [blame] | 958 | load[0] = load[1] = load[2] = nan(""); |
Elliott Hughes | 2d0b28b | 2018-10-23 11:23:00 -0700 | [diff] [blame] | 959 | ASSERT_EQ(3, getloadavg(load, 3)); |
| 960 | |
Elliott Hughes | 72a54a4 | 2018-12-18 14:47:25 -0800 | [diff] [blame] | 961 | // Check that getloadavg(3) at least overwrote the NaNs. |
Elliott Hughes | 2d0b28b | 2018-10-23 11:23:00 -0700 | [diff] [blame] | 962 | ASSERT_FALSE(isnan(load[0])); |
Elliott Hughes | 72a54a4 | 2018-12-18 14:47:25 -0800 | [diff] [blame] | 963 | ASSERT_FALSE(isnan(load[1])); |
| 964 | ASSERT_FALSE(isnan(load[2])); |
| 965 | // And that the difference between /proc/loadavg and getloadavg(3) is "small". |
| 966 | ASSERT_TRUE(fabs(expected[0] - load[0]) < 0.5) << expected[0] << ' ' << load[0]; |
| 967 | ASSERT_TRUE(fabs(expected[1] - load[1]) < 0.5) << expected[1] << ' ' << load[1]; |
| 968 | ASSERT_TRUE(fabs(expected[2] - load[2]) < 0.5) << expected[2] << ' ' << load[2]; |
Elliott Hughes | 2d0b28b | 2018-10-23 11:23:00 -0700 | [diff] [blame] | 969 | } |
Elliott Hughes | 75064c1 | 2020-01-22 20:46:12 -0800 | [diff] [blame] | 970 | |
| 971 | TEST(stdlib, getprogname) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 972 | #if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL) |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 973 | GTEST_SKIP() << "glibc and musl don't have getprogname()"; |
Elliott Hughes | 75064c1 | 2020-01-22 20:46:12 -0800 | [diff] [blame] | 974 | #else |
| 975 | // You should always have a name. |
| 976 | ASSERT_TRUE(getprogname() != nullptr); |
| 977 | // The name should never have a slash in it. |
| 978 | ASSERT_TRUE(strchr(getprogname(), '/') == nullptr); |
| 979 | #endif |
| 980 | } |
| 981 | |
| 982 | TEST(stdlib, setprogname) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 983 | #if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL) |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 984 | GTEST_SKIP() << "glibc and musl don't have setprogname()"; |
Elliott Hughes | 75064c1 | 2020-01-22 20:46:12 -0800 | [diff] [blame] | 985 | #else |
| 986 | // setprogname() only takes the basename of what you give it. |
| 987 | setprogname("/usr/bin/muppet"); |
| 988 | ASSERT_STREQ("muppet", getprogname()); |
| 989 | #endif |
| 990 | } |