blob: 465e61a7f40a4c1a01fa9451c92f08315981c972 [file] [log] [blame]
Elliott Hughes774c7f52012-10-01 13:11:03 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughesb16b7222013-02-04 13:18:00 -080017#include <errno.h>
Elliott Hughes7f0849f2016-08-26 16:17:17 -070018#include <fcntl.h>
Elliott Hughesf0777842013-03-01 16:59:46 -080019#include <libgen.h>
20#include <limits.h>
Elliott Hughes7f0849f2016-08-26 16:17:17 -070021#include <math.h>
Elliott Hughes877ec6d2013-11-15 17:40:18 -080022#include <pthread.h>
Elliott Hughesb16b7222013-02-04 13:18:00 -080023#include <stdint.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070024#include <stdlib.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070025#include <sys/cdefs.h>
Elliott Hughes40488562014-03-12 13:50:38 -070026#include <sys/types.h>
27#include <sys/wait.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080028#include <unistd.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070029
Elliott Hughes1921dce2017-12-19 10:27:27 -080030#include <limits>
Elliott Hughesf61a06e2017-12-19 16:11:37 -080031#include <string>
Elliott Hughes1921dce2017-12-19 10:27:27 -080032
Elliott Hughes22fb2672019-11-01 08:07:25 -070033#include <android-base/file.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080034#include <android-base/macros.h>
Elliott Hughes141b9172021-04-09 17:13:09 -070035#include <android-base/silent_death_test.h>
Florian Mayer750dcd32022-04-15 15:54:47 -070036#include <android-base/test_utils.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080037#include <gtest/gtest.h>
38
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080039#include "math_data_test.h"
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080040#include "utils.h"
41
Elliott Hughes22fb2672019-11-01 08:07:25 -070042using namespace std::string_literals;
43
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080044template <typename T = int (*)(char*)>
45class GenericTemporaryFile {
46 public:
47 explicit GenericTemporaryFile(T mk_fn = mkstemp) : mk_fn_(mk_fn) {
48 // Since we might be running on the host or the target, and if we're
49 // running on the host we might be running under bionic or glibc,
50 // let's just try both possible temporary directories and take the
51 // first one that works.
52 init("/data/local/tmp");
53 if (fd == -1) {
54 init("/tmp");
55 }
56 }
57
58 ~GenericTemporaryFile() {
59 close(fd);
60 unlink(path);
61 }
62
63 int fd;
64 char path[1024];
65
66 private:
67 T mk_fn_;
68
69 void init(const char* tmp_dir) {
70 snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir);
71 fd = mk_fn_(path);
72 }
73
74 DISALLOW_COPY_AND_ASSIGN(GenericTemporaryFile);
75};
76
77typedef GenericTemporaryFile<> MyTemporaryFile;
78
Elliott Hughes274afe82014-11-06 12:40:08 -080079// The random number generator tests all set the seed, get four values, reset the seed and check
80// that they get the first two values repeated, and then reset the seed and check two more values
81// to rule out the possibility that we're just going round a cycle of four values.
82// TODO: factor this out.
83
Elliott Hughes774c7f52012-10-01 13:11:03 -070084TEST(stdlib, drand48) {
85 srand48(0x01020304);
86 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
87 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
88 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
89 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080090 srand48(0x01020304);
91 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
92 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
93 srand48(0x01020304);
94 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
95 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
96}
97
98TEST(stdlib, erand48) {
99 const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 };
100 unsigned short xsubi[3];
101 memcpy(xsubi, seed, sizeof(seed));
102 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
103 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
104 EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi));
105 EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi));
106 memcpy(xsubi, seed, sizeof(seed));
107 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
108 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
109 memcpy(xsubi, seed, sizeof(seed));
110 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
111 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
112}
113
114TEST(stdlib, lcong48) {
115 unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e };
116 lcong48(p);
117 EXPECT_EQ(1531389981, lrand48());
118 EXPECT_EQ(1598801533, lrand48());
119 EXPECT_EQ(2080534853, lrand48());
120 EXPECT_EQ(1102488897, lrand48());
121 lcong48(p);
122 EXPECT_EQ(1531389981, lrand48());
123 EXPECT_EQ(1598801533, lrand48());
124 lcong48(p);
125 EXPECT_EQ(1531389981, lrand48());
126 EXPECT_EQ(1598801533, lrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700127}
128
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700129TEST(stdlib, lrand48) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700130 srand48(0x01020304);
131 EXPECT_EQ(1409163720, lrand48());
132 EXPECT_EQ(397769746, lrand48());
133 EXPECT_EQ(902267124, lrand48());
134 EXPECT_EQ(132366131, lrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800135 srand48(0x01020304);
136 EXPECT_EQ(1409163720, lrand48());
137 EXPECT_EQ(397769746, lrand48());
138 srand48(0x01020304);
139 EXPECT_EQ(1409163720, lrand48());
140 EXPECT_EQ(397769746, lrand48());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700141}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700142
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700143TEST(stdlib, random) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700144 srandom(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700145 EXPECT_EQ(55436735, random());
146 EXPECT_EQ(1399865117, random());
147 EXPECT_EQ(2032643283, random());
148 EXPECT_EQ(571329216, random());
Elliott Hughes274afe82014-11-06 12:40:08 -0800149 srandom(0x01020304);
150 EXPECT_EQ(55436735, random());
151 EXPECT_EQ(1399865117, random());
152 srandom(0x01020304);
153 EXPECT_EQ(55436735, random());
154 EXPECT_EQ(1399865117, random());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700155}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700156
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700157TEST(stdlib, rand) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700158 srand(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700159 EXPECT_EQ(55436735, rand());
160 EXPECT_EQ(1399865117, rand());
161 EXPECT_EQ(2032643283, rand());
162 EXPECT_EQ(571329216, rand());
Elliott Hughes274afe82014-11-06 12:40:08 -0800163 srand(0x01020304);
164 EXPECT_EQ(55436735, rand());
165 EXPECT_EQ(1399865117, rand());
166 srand(0x01020304);
167 EXPECT_EQ(55436735, rand());
168 EXPECT_EQ(1399865117, rand());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700169}
170
171TEST(stdlib, mrand48) {
172 srand48(0x01020304);
173 EXPECT_EQ(-1476639856, mrand48());
174 EXPECT_EQ(795539493, mrand48());
175 EXPECT_EQ(1804534249, mrand48());
176 EXPECT_EQ(264732262, mrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800177 srand48(0x01020304);
178 EXPECT_EQ(-1476639856, mrand48());
179 EXPECT_EQ(795539493, mrand48());
180 srand48(0x01020304);
181 EXPECT_EQ(-1476639856, mrand48());
182 EXPECT_EQ(795539493, mrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700183}
Elliott Hughesb16b7222013-02-04 13:18:00 -0800184
Aleksandra Tsvetkova608b4512015-02-27 15:01:59 +0300185TEST(stdlib, jrand48_distribution) {
186 const int iterations = 4096;
187 const int pivot_low = 1536;
188 const int pivot_high = 2560;
189
190 unsigned short xsubi[3];
191 int bits[32] = {};
192
193 for (int iter = 0; iter < iterations; ++iter) {
194 long rand_val = jrand48(xsubi);
195 for (int bit = 0; bit < 32; ++bit) {
196 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
197 }
198 }
199
200 // Check that bit probability is uniform
201 for (int bit = 0; bit < 32; ++bit) {
202 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
203 }
204}
205
206TEST(stdlib, mrand48_distribution) {
207 const int iterations = 4096;
208 const int pivot_low = 1536;
209 const int pivot_high = 2560;
210
211 int bits[32] = {};
212
213 for (int iter = 0; iter < iterations; ++iter) {
214 long rand_val = mrand48();
215 for (int bit = 0; bit < 32; ++bit) {
216 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
217 }
218 }
219
220 // Check that bit probability is uniform
221 for (int bit = 0; bit < 32; ++bit) {
222 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
223 }
224}
225
Christopher Ferris3a32d952017-06-15 13:30:44 -0700226TEST(stdlib, posix_memalign_sweep) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800227 SKIP_WITH_HWASAN;
Christopher Ferris3a32d952017-06-15 13:30:44 -0700228 void* ptr;
Elliott Hughesb16b7222013-02-04 13:18:00 -0800229
Christopher Ferris3a32d952017-06-15 13:30:44 -0700230 // These should all fail.
231 for (size_t align = 0; align < sizeof(long); align++) {
232 ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256))
233 << "Unexpected value at align " << align;
234 }
Elliott Hughesb16b7222013-02-04 13:18:00 -0800235
Christopher Ferris3a32d952017-06-15 13:30:44 -0700236 // Verify powers of 2 up to 2048 allocate, and verify that all other
237 // alignment values between the powers of 2 fail.
238 size_t last_align = sizeof(long);
239 for (size_t align = sizeof(long); align <= 2048; align <<= 1) {
240 // Try all of the non power of 2 values from the last until this value.
241 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
242 ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256))
243 << "Unexpected success at align " << fail_align;
244 }
245 ASSERT_EQ(0, posix_memalign(&ptr, align, 256))
246 << "Unexpected failure at align " << align;
247 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
248 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
249 free(ptr);
250 last_align = align;
251 }
252}
253
254TEST(stdlib, posix_memalign_various_sizes) {
255 std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000};
256 for (auto size : sizes) {
257 void* ptr;
258 ASSERT_EQ(0, posix_memalign(&ptr, 16, 1))
259 << "posix_memalign failed at size " << size;
260 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf)
261 << "Pointer not aligned at size " << size << " ptr " << ptr;
262 free(ptr);
263 }
264}
265
266TEST(stdlib, posix_memalign_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800267 SKIP_WITH_HWASAN;
Christopher Ferris3a32d952017-06-15 13:30:44 -0700268 void* ptr;
269 ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX));
Elliott Hughesb16b7222013-02-04 13:18:00 -0800270}
Elliott Hughesf0777842013-03-01 16:59:46 -0800271
Christopher Ferriscae21a92018-02-05 18:14:55 -0800272TEST(stdlib, aligned_alloc_sweep) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800273 SKIP_WITH_HWASAN;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800274 // Verify powers of 2 up to 2048 allocate, and verify that all other
275 // alignment values between the powers of 2 fail.
276 size_t last_align = 1;
277 for (size_t align = 1; align <= 2048; align <<= 1) {
278 // Try all of the non power of 2 values from the last until this value.
279 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800280 ASSERT_TRUE(aligned_alloc(fail_align, fail_align) == nullptr)
Christopher Ferriscae21a92018-02-05 18:14:55 -0800281 << "Unexpected success at align " << fail_align;
282 ASSERT_EQ(EINVAL, errno) << "Unexpected errno at align " << fail_align;
283 }
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800284 void* ptr = aligned_alloc(align, 2 * align);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800285 ASSERT_TRUE(ptr != nullptr) << "Unexpected failure at align " << align;
286 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
287 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
288 free(ptr);
289 last_align = align;
290 }
Christopher Ferriscae21a92018-02-05 18:14:55 -0800291}
292
293TEST(stdlib, aligned_alloc_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800294 SKIP_WITH_HWASAN;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800295 ASSERT_TRUE(aligned_alloc(16, SIZE_MAX) == nullptr);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800296}
297
298TEST(stdlib, aligned_alloc_size_not_multiple_of_alignment) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800299 SKIP_WITH_HWASAN;
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800300
301 ASSERT_TRUE(aligned_alloc(2048, 1) == nullptr);
302 ASSERT_TRUE(aligned_alloc(4, 3) == nullptr);
303 ASSERT_TRUE(aligned_alloc(4, 7) == nullptr);
304 ASSERT_TRUE(aligned_alloc(16, 8) == nullptr);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800305}
306
Elliott Hughesf0777842013-03-01 16:59:46 -0800307TEST(stdlib, realpath__NULL_filename) {
308 errno = 0;
George Burgess IV95bd4882017-08-14 14:48:55 -0700309 // Work around the compile-time error generated by FORTIFY here.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700310 const char* path = nullptr;
311 char* p = realpath(path, nullptr);
312 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800313 ASSERT_EQ(EINVAL, errno);
314}
315
316TEST(stdlib, realpath__empty_filename) {
317 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700318 char* p = realpath("", nullptr);
319 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800320 ASSERT_EQ(ENOENT, errno);
321}
322
323TEST(stdlib, realpath__ENOENT) {
324 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700325 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", nullptr);
326 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800327 ASSERT_EQ(ENOENT, errno);
328}
329
Elliott Hughes22fb2672019-11-01 08:07:25 -0700330TEST(stdlib, realpath__ELOOP) {
331 TemporaryDir td;
332 std::string link = std::string(td.path) + "/loop";
333 ASSERT_EQ(0, symlink(link.c_str(), link.c_str()));
334
335 errno = 0;
336 char* p = realpath(link.c_str(), nullptr);
337 ASSERT_TRUE(p == nullptr);
338 ASSERT_EQ(ELOOP, errno);
339}
340
Elliott Hughes31e072f2014-09-30 16:15:42 -0700341TEST(stdlib, realpath__component_after_non_directory) {
342 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700343 char* p = realpath("/dev/null/.", nullptr);
344 ASSERT_TRUE(p == nullptr);
Elliott Hughes31e072f2014-09-30 16:15:42 -0700345 ASSERT_EQ(ENOTDIR, errno);
346
347 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700348 p = realpath("/dev/null/..", nullptr);
349 ASSERT_TRUE(p == nullptr);
Elliott Hughes31e072f2014-09-30 16:15:42 -0700350 ASSERT_EQ(ENOTDIR, errno);
351}
352
Elliott Hughesf0777842013-03-01 16:59:46 -0800353TEST(stdlib, realpath) {
354 // Get the name of this executable.
355 char executable_path[PATH_MAX];
356 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
357 ASSERT_NE(rc, -1);
358 executable_path[rc] = '\0';
359
360 char buf[PATH_MAX + 1];
361 char* p = realpath("/proc/self/exe", buf);
362 ASSERT_STREQ(executable_path, p);
363
Yi Kong32bc0fc2018-08-02 17:31:13 -0700364 p = realpath("/proc/self/exe", nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800365 ASSERT_STREQ(executable_path, p);
366 free(p);
367}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700368
Elliott Hughes22fb2672019-11-01 08:07:25 -0700369TEST(stdlib, realpath__dot) {
370 char* p = realpath("/proc/./version", nullptr);
371 ASSERT_STREQ("/proc/version", p);
372 free(p);
373}
374
375TEST(stdlib, realpath__dot_dot) {
376 char* p = realpath("/dev/../proc/version", nullptr);
377 ASSERT_STREQ("/proc/version", p);
378 free(p);
379}
380
381TEST(stdlib, realpath__deleted) {
382 TemporaryDir td;
383
384 // Create a file "A".
385 std::string A_path = td.path + "/A"s;
386 ASSERT_TRUE(android::base::WriteStringToFile("test\n", A_path));
387
388 // Get an O_PATH fd for it.
389 android::base::unique_fd fd(open(A_path.c_str(), O_PATH));
390 ASSERT_NE(fd, -1);
391
392 // Create a file "A (deleted)".
393 android::base::unique_fd fd2(open((td.path + "/A (deleted)"s).c_str(),
394 O_CREAT | O_TRUNC | O_WRONLY, 0644));
395 ASSERT_NE(fd2, -1);
396
397 // Delete "A".
398 ASSERT_EQ(0, unlink(A_path.c_str()));
399
400 // Now realpath() on the O_PATH fd, and check we *don't* get "A (deleted)".
401 std::string path = android::base::StringPrintf("/proc/%d/fd/%d", static_cast<int>(getpid()),
402 fd.get());
403 errno = 0;
404 char* result = realpath(path.c_str(), nullptr);
405 ASSERT_EQ(nullptr, result) << result;
406 ASSERT_EQ(ENOENT, errno);
407 free(result);
408}
409
Elliott Hughes0b25f632013-04-11 18:08:34 -0700410TEST(stdlib, qsort) {
411 struct s {
412 char name[16];
413 static int comparator(const void* lhs, const void* rhs) {
414 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
415 }
416 };
417 s entries[3];
418 strcpy(entries[0].name, "charlie");
419 strcpy(entries[1].name, "bravo");
420 strcpy(entries[2].name, "alpha");
421
422 qsort(entries, 3, sizeof(s), s::comparator);
423 ASSERT_STREQ("alpha", entries[0].name);
424 ASSERT_STREQ("bravo", entries[1].name);
425 ASSERT_STREQ("charlie", entries[2].name);
426
427 qsort(entries, 3, sizeof(s), s::comparator);
428 ASSERT_STREQ("alpha", entries[0].name);
429 ASSERT_STREQ("bravo", entries[1].name);
430 ASSERT_STREQ("charlie", entries[2].name);
431}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800432
433static void* TestBug57421_child(void* arg) {
434 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700435 pthread_join(main_thread, nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800436 char* value = getenv("ENVIRONMENT_VARIABLE");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700437 if (value == nullptr) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800438 setenv("ENVIRONMENT_VARIABLE", "value", 1);
439 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700440 return nullptr;
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800441}
442
443static void TestBug57421_main() {
444 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700445 ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
446 pthread_exit(nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800447}
448
449// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
450// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800451
Elliott Hughes141b9172021-04-09 17:13:09 -0700452using stdlib_DeathTest = SilentDeathTest;
Yabin Cui9df70402014-11-05 18:01:01 -0800453
454TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800455 // https://code.google.com/p/android/issues/detail?id=57421
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800456 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
457}
Calin Juravlefe317a32014-02-21 15:11:03 +0000458
Colin Cross7da20342021-07-28 11:18:11 -0700459TEST(stdlib, mkostemp64_smoke) {
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800460 MyTemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800461 ASSERT_TRUE(CloseOnExec(tf.fd));
Elliott Hughes31165ed2014-09-23 17:34:29 -0700462}
463
464TEST(stdlib, mkostemp) {
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800465 MyTemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800466 ASSERT_TRUE(CloseOnExec(tf.fd));
Elliott Hughes31165ed2014-09-23 17:34:29 -0700467}
468
Colin Cross7da20342021-07-28 11:18:11 -0700469TEST(stdlib, mkstemp64_smoke) {
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800470 MyTemporaryFile tf(mkstemp64);
Elliott Hughes31165ed2014-09-23 17:34:29 -0700471 struct stat64 sb;
472 ASSERT_EQ(0, fstat64(tf.fd, &sb));
473 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
474}
475
Calin Juravlefe317a32014-02-21 15:11:03 +0000476TEST(stdlib, mkstemp) {
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800477 MyTemporaryFile tf(mkstemp);
Calin Juravlefe317a32014-02-21 15:11:03 +0000478 struct stat sb;
479 ASSERT_EQ(0, fstat(tf.fd, &sb));
480}
481
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700482TEST(stdlib, system) {
483 int status;
484
485 status = system("exit 0");
486 ASSERT_TRUE(WIFEXITED(status));
487 ASSERT_EQ(0, WEXITSTATUS(status));
488
489 status = system("exit 1");
490 ASSERT_TRUE(WIFEXITED(status));
491 ASSERT_EQ(1, WEXITSTATUS(status));
492}
Elliott Hughes5a817382014-03-12 16:12:57 -0700493
Elliott Hughesbbbe27f2021-03-08 14:07:01 -0800494TEST(stdlib, system_NULL) {
495 // "The system() function shall always return non-zero when command is NULL."
496 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/system.html
497 ASSERT_NE(0, system(nullptr));
498}
499
Elliott Hughesb6b7e2e2021-11-04 17:18:58 -0700500// https://austingroupbugs.net/view.php?id=1440
501TEST(stdlib, system_minus) {
502 // Create a script with a name that starts with a '-'.
503 TemporaryDir td;
504 std::string script = std::string(td.path) + "/-minus";
505 ASSERT_TRUE(android::base::WriteStringToFile("#!" BIN_DIR "sh\nexit 66\n", script));
506
507 // Set $PATH so we can find it.
508 setenv("PATH", td.path, 1);
509 // Make it executable so we can run it.
510 ASSERT_EQ(0, chmod(script.c_str(), 0555));
511
512 int status = system("-minus");
513 EXPECT_TRUE(WIFEXITED(status));
514 EXPECT_EQ(66, WEXITSTATUS(status));
515
516 // While we're here and have all the setup, let's test popen(3) too...
517 FILE* fp = popen("-minus", "r");
518 ASSERT_TRUE(fp != nullptr);
519 status = pclose(fp);
520 EXPECT_TRUE(WIFEXITED(status));
521 EXPECT_EQ(66, WEXITSTATUS(status));
522}
523
Elliott Hughes5a817382014-03-12 16:12:57 -0700524TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700525 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700526}
527
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700528template <typename T>
529static void CheckStrToFloat(T fn(const char* s, char** end)) {
530 FpUlpEq<0, T> pred;
531
532 EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr));
533 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr));
534 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr));
535
Dan Albertf6346552016-12-02 12:02:03 -0800536 const char* s = " \t\v\f\r\n9.0";
537 char* p;
538 EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p));
539 EXPECT_EQ(s + strlen(s), p);
540
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700541 EXPECT_TRUE(isnan(fn("+nan", nullptr)));
542 EXPECT_TRUE(isnan(fn("nan", nullptr)));
543 EXPECT_TRUE(isnan(fn("-nan", nullptr)));
544
545 EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr)));
546 EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr)));
547 EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr)));
548
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700549 EXPECT_TRUE(isnan(fn("+nanny", &p)));
550 EXPECT_STREQ("ny", p);
551 EXPECT_TRUE(isnan(fn("nanny", &p)));
552 EXPECT_STREQ("ny", p);
553 EXPECT_TRUE(isnan(fn("-nanny", &p)));
554 EXPECT_STREQ("ny", p);
555
556 EXPECT_EQ(0, fn("muppet", &p));
557 EXPECT_STREQ("muppet", p);
558 EXPECT_EQ(0, fn(" muppet", &p));
559 EXPECT_STREQ(" muppet", p);
560
561 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr));
562 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr));
563 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr));
564
565 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr));
566 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr));
567 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr));
568
569 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p));
570 EXPECT_STREQ("initude", p);
571 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p));
572 EXPECT_STREQ("initude", p);
573 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p));
574 EXPECT_STREQ("initude", p);
575
576 // Check case-insensitivity.
577 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr));
578 EXPECT_TRUE(isnan(fn("NaN", nullptr)));
579}
580
Elliott Hughes5a817382014-03-12 16:12:57 -0700581TEST(stdlib, strtod) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700582 CheckStrToFloat(strtod);
Elliott Hughes5a817382014-03-12 16:12:57 -0700583}
584
585TEST(stdlib, strtof) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700586 CheckStrToFloat(strtof);
Elliott Hughes5a817382014-03-12 16:12:57 -0700587}
588
589TEST(stdlib, strtold) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700590 CheckStrToFloat(strtold);
Elliott Hughes5a817382014-03-12 16:12:57 -0700591}
Elliott Hughes9f525642014-04-08 17:14:01 -0700592
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700593TEST(stdlib, strtof_2206701) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700594 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", nullptr));
595 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", nullptr));
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700596}
597
598TEST(stdlib, strtod_largest_subnormal) {
599 // This value has been known to cause javac and java to infinite loop.
600 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
Yi Kong32bc0fc2018-08-02 17:31:13 -0700601 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", nullptr));
602 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", nullptr));
603 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", nullptr));
604 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", nullptr));
605 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", nullptr));
606 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", nullptr));
607 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", nullptr));
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700608}
609
Dan Albertb8425c52014-04-29 17:49:06 -0700610TEST(stdlib, quick_exit) {
611 pid_t pid = fork();
612 ASSERT_NE(-1, pid) << strerror(errno);
613
614 if (pid == 0) {
615 quick_exit(99);
616 }
617
Elliott Hughes33697a02016-01-26 13:04:57 -0800618 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700619}
620
621static int quick_exit_status = 0;
622
623static void quick_exit_1(void) {
624 ASSERT_EQ(quick_exit_status, 0);
625 quick_exit_status = 1;
626}
627
628static void quick_exit_2(void) {
629 ASSERT_EQ(quick_exit_status, 1);
630}
631
632static void not_run(void) {
633 FAIL();
634}
635
636TEST(stdlib, at_quick_exit) {
637 pid_t pid = fork();
638 ASSERT_NE(-1, pid) << strerror(errno);
639
640 if (pid == 0) {
641 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
642 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
643 atexit(not_run);
644 quick_exit(99);
645 }
646
Elliott Hughes33697a02016-01-26 13:04:57 -0800647 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700648}
649
Elliott Hughes9f525642014-04-08 17:14:01 -0700650TEST(unistd, _Exit) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800651 pid_t pid = fork();
Elliott Hughes9f525642014-04-08 17:14:01 -0700652 ASSERT_NE(-1, pid) << strerror(errno);
653
654 if (pid == 0) {
655 _Exit(99);
656 }
657
Elliott Hughes33697a02016-01-26 13:04:57 -0800658 AssertChildExited(pid, 99);
Elliott Hughes9f525642014-04-08 17:14:01 -0700659}
Elliott Hughes49167062014-07-25 17:24:00 -0700660
Colin Cross4c5595c2021-08-16 15:51:59 -0700661#if defined(ANDROID_HOST_MUSL)
Colin Cross7da20342021-07-28 11:18:11 -0700662// musl doesn't have getpt
663int getpt() {
664 return posix_openpt(O_RDWR|O_NOCTTY);
665}
666#endif
667
Elliott Hughes49167062014-07-25 17:24:00 -0700668TEST(stdlib, pty_smoke) {
669 // getpt returns a pty with O_RDWR|O_NOCTTY.
670 int fd = getpt();
671 ASSERT_NE(-1, fd);
672
673 // grantpt is a no-op.
674 ASSERT_EQ(0, grantpt(fd));
675
676 // ptsname_r should start "/dev/pts/".
677 char name_r[128];
678 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
679 name_r[9] = 0;
680 ASSERT_STREQ("/dev/pts/", name_r);
681
682 close(fd);
683}
684
685TEST(stdlib, posix_openpt) {
686 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
687 ASSERT_NE(-1, fd);
688 close(fd);
689}
690
691TEST(stdlib, ptsname_r_ENOTTY) {
692 errno = 0;
693 char buf[128];
694 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
695 ASSERT_EQ(ENOTTY, errno);
696}
697
698TEST(stdlib, ptsname_r_EINVAL) {
699 int fd = getpt();
700 ASSERT_NE(-1, fd);
701 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700702 char* buf = nullptr;
Elliott Hughes49167062014-07-25 17:24:00 -0700703 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
704 ASSERT_EQ(EINVAL, errno);
705 close(fd);
706}
707
708TEST(stdlib, ptsname_r_ERANGE) {
709 int fd = getpt();
710 ASSERT_NE(-1, fd);
711 errno = 0;
712 char buf[1];
713 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
714 ASSERT_EQ(ERANGE, errno);
715 close(fd);
716}
717
Elliott Hughes728cde52017-11-08 21:53:50 -0800718TEST(stdlib, ttyname) {
719 int fd = getpt();
720 ASSERT_NE(-1, fd);
721
722 // ttyname returns "/dev/ptmx" for a pty.
723 ASSERT_STREQ("/dev/ptmx", ttyname(fd));
724
725 close(fd);
726}
727
Elliott Hughes49167062014-07-25 17:24:00 -0700728TEST(stdlib, ttyname_r) {
729 int fd = getpt();
730 ASSERT_NE(-1, fd);
731
732 // ttyname_r returns "/dev/ptmx" for a pty.
733 char name_r[128];
734 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
735 ASSERT_STREQ("/dev/ptmx", name_r);
736
737 close(fd);
738}
739
740TEST(stdlib, ttyname_r_ENOTTY) {
741 int fd = open("/dev/null", O_WRONLY);
742 errno = 0;
743 char buf[128];
744 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
745 ASSERT_EQ(ENOTTY, errno);
746 close(fd);
747}
748
749TEST(stdlib, ttyname_r_EINVAL) {
750 int fd = getpt();
751 ASSERT_NE(-1, fd);
752 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700753 char* buf = nullptr;
Elliott Hughes49167062014-07-25 17:24:00 -0700754 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
755 ASSERT_EQ(EINVAL, errno);
756 close(fd);
757}
758
759TEST(stdlib, ttyname_r_ERANGE) {
760 int fd = getpt();
761 ASSERT_NE(-1, fd);
762 errno = 0;
763 char buf[1];
764 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
765 ASSERT_EQ(ERANGE, errno);
766 close(fd);
767}
768
769TEST(stdlib, unlockpt_ENOTTY) {
770 int fd = open("/dev/null", O_WRONLY);
771 errno = 0;
772 ASSERT_EQ(-1, unlockpt(fd));
773 ASSERT_EQ(ENOTTY, errno);
774 close(fd);
775}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700776
Elliott Hughesdf143f82016-04-04 17:34:04 -0700777TEST(stdlib, getsubopt) {
778 char* const tokens[] = {
779 const_cast<char*>("a"),
780 const_cast<char*>("b"),
781 const_cast<char*>("foo"),
782 nullptr
783 };
784 std::string input = "a,b,foo=bar,a,unknown";
785 char* subopts = &input[0];
786 char* value = nullptr;
787
788 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
789 ASSERT_EQ(nullptr, value);
790 ASSERT_EQ(1, getsubopt(&subopts, tokens, &value));
791 ASSERT_EQ(nullptr, value);
792 ASSERT_EQ(2, getsubopt(&subopts, tokens, &value));
793 ASSERT_STREQ("bar", value);
794 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
795 ASSERT_EQ(nullptr, value);
796
797 ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
798}
Elliott Hughes6f6f9052016-04-28 14:54:52 -0700799
800TEST(stdlib, mblen) {
801 // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
802 // respectively, do or do not have state-dependent encodings." We're always UTF-8.
803 EXPECT_EQ(0, mblen(nullptr, 1));
804
805 ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
806
807 // 1-byte UTF-8.
808 EXPECT_EQ(1, mblen("abcdef", 6));
809 // 2-byte UTF-8.
810 EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
811 // 3-byte UTF-8.
812 EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
813 // 4-byte UTF-8.
814 EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
815
816 // Illegal over-long sequence.
817 ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
818
819 // "mblen() shall ... return 0 (if s points to the null byte)".
820 EXPECT_EQ(0, mblen("", 1));
821}
Elliott Hughesf826a372017-07-13 09:35:15 -0700822
823template <typename T>
824static void CheckStrToInt(T fn(const char* s, char** end, int base)) {
825 char* end_p;
826
827 // Negative base => invalid.
828 errno = 0;
829 ASSERT_EQ(T(0), fn("123", &end_p, -1));
830 ASSERT_EQ(EINVAL, errno);
831
832 // Base 1 => invalid (base 0 means "please guess").
833 errno = 0;
834 ASSERT_EQ(T(0), fn("123", &end_p, 1));
835 ASSERT_EQ(EINVAL, errno);
836
837 // Base > 36 => invalid.
838 errno = 0;
839 ASSERT_EQ(T(0), fn("123", &end_p, 37));
840 ASSERT_EQ(EINVAL, errno);
841
Elliott Hughes7cebf832020-08-12 14:25:41 -0700842 // Both leading + or - are always allowed (even for the strtou* family).
843 ASSERT_EQ(T(-123), fn("-123", &end_p, 10));
844 ASSERT_EQ(T(123), fn("+123", &end_p, 10));
845
Elliott Hughesf826a372017-07-13 09:35:15 -0700846 // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'.
847 ASSERT_EQ(T(0), fn("0xy", &end_p, 16));
848 ASSERT_EQ('x', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800849
Elliott Hughes7cebf832020-08-12 14:25:41 -0700850 // Hexadecimal (both the 0x and the digits) is case-insensitive.
851 ASSERT_EQ(T(0xab), fn("0xab", &end_p, 0));
852 ASSERT_EQ(T(0xab), fn("0Xab", &end_p, 0));
853 ASSERT_EQ(T(0xab), fn("0xAB", &end_p, 0));
854 ASSERT_EQ(T(0xab), fn("0XAB", &end_p, 0));
855 ASSERT_EQ(T(0xab), fn("0xAb", &end_p, 0));
856 ASSERT_EQ(T(0xab), fn("0XAb", &end_p, 0));
857
858 // Octal lives! (Sadly.)
859 ASSERT_EQ(T(0666), fn("0666", &end_p, 0));
860
Elliott Hughes1921dce2017-12-19 10:27:27 -0800861 if (std::numeric_limits<T>::is_signed) {
862 // Minimum (such as -128).
Elliott Hughesf61a06e2017-12-19 16:11:37 -0800863 std::string min{std::to_string(std::numeric_limits<T>::min())};
Elliott Hughescb239bd2017-12-20 17:37:11 -0800864 end_p = nullptr;
865 errno = 0;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800866 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
Elliott Hughescb239bd2017-12-20 17:37:11 -0800867 ASSERT_EQ(0, errno);
868 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800869 // Too negative (such as -129).
870 min.back() = (min.back() + 1);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800871 end_p = nullptr;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800872 errno = 0;
873 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
874 ASSERT_EQ(ERANGE, errno);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800875 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800876 }
877
878 // Maximum (such as 127).
Elliott Hughesf61a06e2017-12-19 16:11:37 -0800879 std::string max{std::to_string(std::numeric_limits<T>::max())};
Elliott Hughescb239bd2017-12-20 17:37:11 -0800880 end_p = nullptr;
881 errno = 0;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800882 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
Elliott Hughescb239bd2017-12-20 17:37:11 -0800883 ASSERT_EQ(0, errno);
884 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800885 // Too positive (such as 128).
886 max.back() = (max.back() + 1);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800887 end_p = nullptr;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800888 errno = 0;
889 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
890 ASSERT_EQ(ERANGE, errno);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800891 ASSERT_EQ('\0', *end_p);
892
893 // In case of overflow, strto* leaves us pointing past the end of the number,
894 // not at the digit that overflowed.
895 end_p = nullptr;
896 errno = 0;
897 ASSERT_EQ(std::numeric_limits<T>::max(),
898 fn("99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
899 ASSERT_EQ(ERANGE, errno);
900 ASSERT_STREQ("abc", end_p);
901 if (std::numeric_limits<T>::is_signed) {
902 end_p = nullptr;
903 errno = 0;
904 ASSERT_EQ(std::numeric_limits<T>::min(),
905 fn("-99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
906 ASSERT_EQ(ERANGE, errno);
907 ASSERT_STREQ("abc", end_p);
908 }
Elliott Hughesf826a372017-07-13 09:35:15 -0700909}
910
911TEST(stdlib, strtol_smoke) {
912 CheckStrToInt(strtol);
913}
914
915TEST(stdlib, strtoll_smoke) {
916 CheckStrToInt(strtoll);
917}
918
919TEST(stdlib, strtoul_smoke) {
920 CheckStrToInt(strtoul);
921}
922
923TEST(stdlib, strtoull_smoke) {
924 CheckStrToInt(strtoull);
925}
926
927TEST(stdlib, strtoimax_smoke) {
928 CheckStrToInt(strtoimax);
929}
930
931TEST(stdlib, strtoumax_smoke) {
932 CheckStrToInt(strtoumax);
933}
Elliott Hughes00d8a8b2017-09-07 16:42:13 -0700934
Elliott Hughes7cebf832020-08-12 14:25:41 -0700935TEST(stdlib, atoi) {
936 // Implemented using strtol in bionic, so extensive testing unnecessary.
937 ASSERT_EQ(123, atoi("123four"));
938 ASSERT_EQ(0, atoi("hello"));
939}
940
941TEST(stdlib, atol) {
942 // Implemented using strtol in bionic, so extensive testing unnecessary.
943 ASSERT_EQ(123L, atol("123four"));
944 ASSERT_EQ(0L, atol("hello"));
945}
946
Elliott Hughes00d8a8b2017-09-07 16:42:13 -0700947TEST(stdlib, abs) {
948 ASSERT_EQ(INT_MAX, abs(-INT_MAX));
949 ASSERT_EQ(INT_MAX, abs(INT_MAX));
950}
951
952TEST(stdlib, labs) {
953 ASSERT_EQ(LONG_MAX, labs(-LONG_MAX));
954 ASSERT_EQ(LONG_MAX, labs(LONG_MAX));
955}
956
957TEST(stdlib, llabs) {
958 ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX));
959 ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX));
960}
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700961
962TEST(stdlib, getloadavg) {
963 double load[3];
964
965 // The second argument should have been size_t.
966 ASSERT_EQ(-1, getloadavg(load, -1));
967 ASSERT_EQ(-1, getloadavg(load, INT_MIN));
968
969 // Zero is a no-op.
970 ASSERT_EQ(0, getloadavg(load, 0));
971
972 // The Linux kernel doesn't support more than 3 (but you can ask for fewer).
973 ASSERT_EQ(1, getloadavg(load, 1));
974 ASSERT_EQ(2, getloadavg(load, 2));
975 ASSERT_EQ(3, getloadavg(load, 3));
976 ASSERT_EQ(3, getloadavg(load, 4));
977 ASSERT_EQ(3, getloadavg(load, INT_MAX));
978
979 // Read /proc/loadavg and check that it's "close enough".
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700980 double expected[3];
981 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/loadavg", "re"), fclose};
982 ASSERT_EQ(3, fscanf(fp.get(), "%lf %lf %lf", &expected[0], &expected[1], &expected[2]));
Elliott Hughes72a54a42018-12-18 14:47:25 -0800983 load[0] = load[1] = load[2] = nan("");
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700984 ASSERT_EQ(3, getloadavg(load, 3));
985
Elliott Hughes72a54a42018-12-18 14:47:25 -0800986 // Check that getloadavg(3) at least overwrote the NaNs.
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700987 ASSERT_FALSE(isnan(load[0]));
Elliott Hughes72a54a42018-12-18 14:47:25 -0800988 ASSERT_FALSE(isnan(load[1]));
989 ASSERT_FALSE(isnan(load[2]));
990 // And that the difference between /proc/loadavg and getloadavg(3) is "small".
991 ASSERT_TRUE(fabs(expected[0] - load[0]) < 0.5) << expected[0] << ' ' << load[0];
992 ASSERT_TRUE(fabs(expected[1] - load[1]) < 0.5) << expected[1] << ' ' << load[1];
993 ASSERT_TRUE(fabs(expected[2] - load[2]) < 0.5) << expected[2] << ' ' << load[2];
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700994}
Elliott Hughes75064c12020-01-22 20:46:12 -0800995
996TEST(stdlib, getprogname) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700997#if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
Colin Cross7da20342021-07-28 11:18:11 -0700998 GTEST_SKIP() << "glibc and musl don't have getprogname()";
Elliott Hughes75064c12020-01-22 20:46:12 -0800999#else
1000 // You should always have a name.
1001 ASSERT_TRUE(getprogname() != nullptr);
1002 // The name should never have a slash in it.
1003 ASSERT_TRUE(strchr(getprogname(), '/') == nullptr);
1004#endif
1005}
1006
1007TEST(stdlib, setprogname) {
Colin Cross4c5595c2021-08-16 15:51:59 -07001008#if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
Colin Cross7da20342021-07-28 11:18:11 -07001009 GTEST_SKIP() << "glibc and musl don't have setprogname()";
Elliott Hughes75064c12020-01-22 20:46:12 -08001010#else
1011 // setprogname() only takes the basename of what you give it.
1012 setprogname("/usr/bin/muppet");
1013 ASSERT_STREQ("muppet", getprogname());
1014#endif
1015}