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