blob: 5b8183458717fcb7395b51956dac56abbad37194 [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>
Elliott Hughes40488562014-03-12 13:50:38 -070025#include <sys/types.h>
26#include <sys/wait.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080027#include <unistd.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070028
Elliott Hughes1921dce2017-12-19 10:27:27 -080029#include <limits>
Elliott Hughesf61a06e2017-12-19 16:11:37 -080030#include <string>
Elliott Hughes1921dce2017-12-19 10:27:27 -080031
Elliott Hughes22fb2672019-11-01 08:07:25 -070032#include <android-base/file.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080033#include <android-base/macros.h>
Elliott Hughes141b9172021-04-09 17:13:09 -070034#include <android-base/silent_death_test.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080035#include <gtest/gtest.h>
36
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080037#include "math_data_test.h"
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080038#include "utils.h"
39
Elliott Hughes22fb2672019-11-01 08:07:25 -070040using namespace std::string_literals;
41
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080042template <typename T = int (*)(char*)>
43class GenericTemporaryFile {
44 public:
45 explicit GenericTemporaryFile(T mk_fn = mkstemp) : mk_fn_(mk_fn) {
46 // Since we might be running on the host or the target, and if we're
47 // running on the host we might be running under bionic or glibc,
48 // let's just try both possible temporary directories and take the
49 // first one that works.
50 init("/data/local/tmp");
51 if (fd == -1) {
52 init("/tmp");
53 }
54 }
55
56 ~GenericTemporaryFile() {
57 close(fd);
58 unlink(path);
59 }
60
61 int fd;
62 char path[1024];
63
64 private:
65 T mk_fn_;
66
67 void init(const char* tmp_dir) {
68 snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir);
69 fd = mk_fn_(path);
70 }
71
72 DISALLOW_COPY_AND_ASSIGN(GenericTemporaryFile);
73};
74
75typedef GenericTemporaryFile<> MyTemporaryFile;
76
Elliott Hughes274afe82014-11-06 12:40:08 -080077// The random number generator tests all set the seed, get four values, reset the seed and check
78// that they get the first two values repeated, and then reset the seed and check two more values
79// to rule out the possibility that we're just going round a cycle of four values.
80// TODO: factor this out.
81
Elliott Hughes774c7f52012-10-01 13:11:03 -070082TEST(stdlib, drand48) {
83 srand48(0x01020304);
84 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
85 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
86 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
87 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080088 srand48(0x01020304);
89 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
90 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
91 srand48(0x01020304);
92 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
93 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
94}
95
96TEST(stdlib, erand48) {
97 const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 };
98 unsigned short xsubi[3];
99 memcpy(xsubi, seed, sizeof(seed));
100 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
101 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
102 EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi));
103 EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi));
104 memcpy(xsubi, seed, sizeof(seed));
105 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
106 EXPECT_DOUBLE_EQ(0.84048536941142515, 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}
111
112TEST(stdlib, lcong48) {
113 unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e };
114 lcong48(p);
115 EXPECT_EQ(1531389981, lrand48());
116 EXPECT_EQ(1598801533, lrand48());
117 EXPECT_EQ(2080534853, lrand48());
118 EXPECT_EQ(1102488897, lrand48());
119 lcong48(p);
120 EXPECT_EQ(1531389981, lrand48());
121 EXPECT_EQ(1598801533, lrand48());
122 lcong48(p);
123 EXPECT_EQ(1531389981, lrand48());
124 EXPECT_EQ(1598801533, lrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700125}
126
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700127TEST(stdlib, lrand48) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700128 srand48(0x01020304);
129 EXPECT_EQ(1409163720, lrand48());
130 EXPECT_EQ(397769746, lrand48());
131 EXPECT_EQ(902267124, lrand48());
132 EXPECT_EQ(132366131, lrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800133 srand48(0x01020304);
134 EXPECT_EQ(1409163720, lrand48());
135 EXPECT_EQ(397769746, lrand48());
136 srand48(0x01020304);
137 EXPECT_EQ(1409163720, lrand48());
138 EXPECT_EQ(397769746, lrand48());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700139}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700140
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700141TEST(stdlib, random) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700142 srandom(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700143 EXPECT_EQ(55436735, random());
144 EXPECT_EQ(1399865117, random());
145 EXPECT_EQ(2032643283, random());
146 EXPECT_EQ(571329216, random());
Elliott Hughes274afe82014-11-06 12:40:08 -0800147 srandom(0x01020304);
148 EXPECT_EQ(55436735, random());
149 EXPECT_EQ(1399865117, random());
150 srandom(0x01020304);
151 EXPECT_EQ(55436735, random());
152 EXPECT_EQ(1399865117, random());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700153}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700154
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700155TEST(stdlib, rand) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700156 srand(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700157 EXPECT_EQ(55436735, rand());
158 EXPECT_EQ(1399865117, rand());
159 EXPECT_EQ(2032643283, rand());
160 EXPECT_EQ(571329216, rand());
Elliott Hughes274afe82014-11-06 12:40:08 -0800161 srand(0x01020304);
162 EXPECT_EQ(55436735, rand());
163 EXPECT_EQ(1399865117, rand());
164 srand(0x01020304);
165 EXPECT_EQ(55436735, rand());
166 EXPECT_EQ(1399865117, rand());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700167}
168
169TEST(stdlib, mrand48) {
170 srand48(0x01020304);
171 EXPECT_EQ(-1476639856, mrand48());
172 EXPECT_EQ(795539493, mrand48());
173 EXPECT_EQ(1804534249, mrand48());
174 EXPECT_EQ(264732262, mrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800175 srand48(0x01020304);
176 EXPECT_EQ(-1476639856, mrand48());
177 EXPECT_EQ(795539493, mrand48());
178 srand48(0x01020304);
179 EXPECT_EQ(-1476639856, mrand48());
180 EXPECT_EQ(795539493, mrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700181}
Elliott Hughesb16b7222013-02-04 13:18:00 -0800182
Aleksandra Tsvetkova608b4512015-02-27 15:01:59 +0300183TEST(stdlib, jrand48_distribution) {
184 const int iterations = 4096;
185 const int pivot_low = 1536;
186 const int pivot_high = 2560;
187
188 unsigned short xsubi[3];
189 int bits[32] = {};
190
191 for (int iter = 0; iter < iterations; ++iter) {
192 long rand_val = jrand48(xsubi);
193 for (int bit = 0; bit < 32; ++bit) {
194 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
195 }
196 }
197
198 // Check that bit probability is uniform
199 for (int bit = 0; bit < 32; ++bit) {
200 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
201 }
202}
203
204TEST(stdlib, mrand48_distribution) {
205 const int iterations = 4096;
206 const int pivot_low = 1536;
207 const int pivot_high = 2560;
208
209 int bits[32] = {};
210
211 for (int iter = 0; iter < iterations; ++iter) {
212 long rand_val = mrand48();
213 for (int bit = 0; bit < 32; ++bit) {
214 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
215 }
216 }
217
218 // Check that bit probability is uniform
219 for (int bit = 0; bit < 32; ++bit) {
220 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
221 }
222}
223
Christopher Ferris3a32d952017-06-15 13:30:44 -0700224TEST(stdlib, posix_memalign_sweep) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800225 SKIP_WITH_HWASAN;
Christopher Ferris3a32d952017-06-15 13:30:44 -0700226 void* ptr;
Elliott Hughesb16b7222013-02-04 13:18:00 -0800227
Christopher Ferris3a32d952017-06-15 13:30:44 -0700228 // These should all fail.
229 for (size_t align = 0; align < sizeof(long); align++) {
230 ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256))
231 << "Unexpected value at align " << align;
232 }
Elliott Hughesb16b7222013-02-04 13:18:00 -0800233
Christopher Ferris3a32d952017-06-15 13:30:44 -0700234 // Verify powers of 2 up to 2048 allocate, and verify that all other
235 // alignment values between the powers of 2 fail.
236 size_t last_align = sizeof(long);
237 for (size_t align = sizeof(long); align <= 2048; align <<= 1) {
238 // Try all of the non power of 2 values from the last until this value.
239 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
240 ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256))
241 << "Unexpected success at align " << fail_align;
242 }
243 ASSERT_EQ(0, posix_memalign(&ptr, align, 256))
244 << "Unexpected failure at align " << align;
245 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
246 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
247 free(ptr);
248 last_align = align;
249 }
250}
251
252TEST(stdlib, posix_memalign_various_sizes) {
253 std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000};
254 for (auto size : sizes) {
255 void* ptr;
256 ASSERT_EQ(0, posix_memalign(&ptr, 16, 1))
257 << "posix_memalign failed at size " << size;
258 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf)
259 << "Pointer not aligned at size " << size << " ptr " << ptr;
260 free(ptr);
261 }
262}
263
264TEST(stdlib, posix_memalign_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800265 SKIP_WITH_HWASAN;
Christopher Ferris3a32d952017-06-15 13:30:44 -0700266 void* ptr;
267 ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX));
Elliott Hughesb16b7222013-02-04 13:18:00 -0800268}
Elliott Hughesf0777842013-03-01 16:59:46 -0800269
Christopher Ferriscae21a92018-02-05 18:14:55 -0800270TEST(stdlib, aligned_alloc_sweep) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800271 SKIP_WITH_HWASAN;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800272 // Verify powers of 2 up to 2048 allocate, and verify that all other
273 // alignment values between the powers of 2 fail.
274 size_t last_align = 1;
275 for (size_t align = 1; align <= 2048; align <<= 1) {
276 // Try all of the non power of 2 values from the last until this value.
277 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800278 ASSERT_TRUE(aligned_alloc(fail_align, fail_align) == nullptr)
Christopher Ferriscae21a92018-02-05 18:14:55 -0800279 << "Unexpected success at align " << fail_align;
280 ASSERT_EQ(EINVAL, errno) << "Unexpected errno at align " << fail_align;
281 }
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800282 void* ptr = aligned_alloc(align, 2 * align);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800283 ASSERT_TRUE(ptr != nullptr) << "Unexpected failure at align " << align;
284 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
285 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
286 free(ptr);
287 last_align = align;
288 }
Christopher Ferriscae21a92018-02-05 18:14:55 -0800289}
290
291TEST(stdlib, aligned_alloc_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800292 SKIP_WITH_HWASAN;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800293 ASSERT_TRUE(aligned_alloc(16, SIZE_MAX) == nullptr);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800294}
295
296TEST(stdlib, aligned_alloc_size_not_multiple_of_alignment) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800297 SKIP_WITH_HWASAN;
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800298
299 ASSERT_TRUE(aligned_alloc(2048, 1) == nullptr);
300 ASSERT_TRUE(aligned_alloc(4, 3) == nullptr);
301 ASSERT_TRUE(aligned_alloc(4, 7) == nullptr);
302 ASSERT_TRUE(aligned_alloc(16, 8) == nullptr);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800303}
304
Elliott Hughesf0777842013-03-01 16:59:46 -0800305TEST(stdlib, realpath__NULL_filename) {
306 errno = 0;
George Burgess IV95bd4882017-08-14 14:48:55 -0700307 // Work around the compile-time error generated by FORTIFY here.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700308 const char* path = nullptr;
309 char* p = realpath(path, nullptr);
310 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800311 ASSERT_EQ(EINVAL, errno);
312}
313
314TEST(stdlib, realpath__empty_filename) {
315 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700316 char* p = realpath("", nullptr);
317 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800318 ASSERT_EQ(ENOENT, errno);
319}
320
321TEST(stdlib, realpath__ENOENT) {
322 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700323 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", nullptr);
324 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800325 ASSERT_EQ(ENOENT, errno);
326}
327
Elliott Hughes22fb2672019-11-01 08:07:25 -0700328TEST(stdlib, realpath__ELOOP) {
329 TemporaryDir td;
330 std::string link = std::string(td.path) + "/loop";
331 ASSERT_EQ(0, symlink(link.c_str(), link.c_str()));
332
333 errno = 0;
334 char* p = realpath(link.c_str(), nullptr);
335 ASSERT_TRUE(p == nullptr);
336 ASSERT_EQ(ELOOP, errno);
337}
338
Elliott Hughes31e072f2014-09-30 16:15:42 -0700339TEST(stdlib, realpath__component_after_non_directory) {
340 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700341 char* p = realpath("/dev/null/.", nullptr);
342 ASSERT_TRUE(p == nullptr);
Elliott Hughes31e072f2014-09-30 16:15:42 -0700343 ASSERT_EQ(ENOTDIR, errno);
344
345 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700346 p = realpath("/dev/null/..", nullptr);
347 ASSERT_TRUE(p == nullptr);
Elliott Hughes31e072f2014-09-30 16:15:42 -0700348 ASSERT_EQ(ENOTDIR, errno);
349}
350
Elliott Hughesf0777842013-03-01 16:59:46 -0800351TEST(stdlib, realpath) {
352 // Get the name of this executable.
353 char executable_path[PATH_MAX];
354 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
355 ASSERT_NE(rc, -1);
356 executable_path[rc] = '\0';
357
358 char buf[PATH_MAX + 1];
359 char* p = realpath("/proc/self/exe", buf);
360 ASSERT_STREQ(executable_path, p);
361
Yi Kong32bc0fc2018-08-02 17:31:13 -0700362 p = realpath("/proc/self/exe", nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800363 ASSERT_STREQ(executable_path, p);
364 free(p);
365}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700366
Elliott Hughes22fb2672019-11-01 08:07:25 -0700367TEST(stdlib, realpath__dot) {
368 char* p = realpath("/proc/./version", nullptr);
369 ASSERT_STREQ("/proc/version", p);
370 free(p);
371}
372
373TEST(stdlib, realpath__dot_dot) {
374 char* p = realpath("/dev/../proc/version", nullptr);
375 ASSERT_STREQ("/proc/version", p);
376 free(p);
377}
378
379TEST(stdlib, realpath__deleted) {
380 TemporaryDir td;
381
382 // Create a file "A".
383 std::string A_path = td.path + "/A"s;
384 ASSERT_TRUE(android::base::WriteStringToFile("test\n", A_path));
385
386 // Get an O_PATH fd for it.
387 android::base::unique_fd fd(open(A_path.c_str(), O_PATH));
388 ASSERT_NE(fd, -1);
389
390 // Create a file "A (deleted)".
391 android::base::unique_fd fd2(open((td.path + "/A (deleted)"s).c_str(),
392 O_CREAT | O_TRUNC | O_WRONLY, 0644));
393 ASSERT_NE(fd2, -1);
394
395 // Delete "A".
396 ASSERT_EQ(0, unlink(A_path.c_str()));
397
398 // Now realpath() on the O_PATH fd, and check we *don't* get "A (deleted)".
399 std::string path = android::base::StringPrintf("/proc/%d/fd/%d", static_cast<int>(getpid()),
400 fd.get());
401 errno = 0;
402 char* result = realpath(path.c_str(), nullptr);
403 ASSERT_EQ(nullptr, result) << result;
404 ASSERT_EQ(ENOENT, errno);
405 free(result);
406}
407
Elliott Hughes0b25f632013-04-11 18:08:34 -0700408TEST(stdlib, qsort) {
409 struct s {
410 char name[16];
411 static int comparator(const void* lhs, const void* rhs) {
412 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
413 }
414 };
415 s entries[3];
416 strcpy(entries[0].name, "charlie");
417 strcpy(entries[1].name, "bravo");
418 strcpy(entries[2].name, "alpha");
419
420 qsort(entries, 3, sizeof(s), s::comparator);
421 ASSERT_STREQ("alpha", entries[0].name);
422 ASSERT_STREQ("bravo", entries[1].name);
423 ASSERT_STREQ("charlie", entries[2].name);
424
425 qsort(entries, 3, sizeof(s), s::comparator);
426 ASSERT_STREQ("alpha", entries[0].name);
427 ASSERT_STREQ("bravo", entries[1].name);
428 ASSERT_STREQ("charlie", entries[2].name);
429}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800430
431static void* TestBug57421_child(void* arg) {
432 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700433 pthread_join(main_thread, nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800434 char* value = getenv("ENVIRONMENT_VARIABLE");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700435 if (value == nullptr) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800436 setenv("ENVIRONMENT_VARIABLE", "value", 1);
437 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700438 return nullptr;
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800439}
440
441static void TestBug57421_main() {
442 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700443 ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
444 pthread_exit(nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800445}
446
447// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
448// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800449
Elliott Hughes141b9172021-04-09 17:13:09 -0700450using stdlib_DeathTest = SilentDeathTest;
Yabin Cui9df70402014-11-05 18:01:01 -0800451
452TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800453 // https://code.google.com/p/android/issues/detail?id=57421
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800454 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
455}
Calin Juravlefe317a32014-02-21 15:11:03 +0000456
Colin Cross7da20342021-07-28 11:18:11 -0700457TEST(stdlib, mkostemp64_smoke) {
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800458 MyTemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800459 ASSERT_TRUE(CloseOnExec(tf.fd));
Elliott Hughes31165ed2014-09-23 17:34:29 -0700460}
461
462TEST(stdlib, mkostemp) {
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800463 MyTemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800464 ASSERT_TRUE(CloseOnExec(tf.fd));
Elliott Hughes31165ed2014-09-23 17:34:29 -0700465}
466
Colin Cross7da20342021-07-28 11:18:11 -0700467TEST(stdlib, mkstemp64_smoke) {
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800468 MyTemporaryFile tf(mkstemp64);
Elliott Hughes31165ed2014-09-23 17:34:29 -0700469 struct stat64 sb;
470 ASSERT_EQ(0, fstat64(tf.fd, &sb));
471 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
472}
473
Calin Juravlefe317a32014-02-21 15:11:03 +0000474TEST(stdlib, mkstemp) {
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800475 MyTemporaryFile tf(mkstemp);
Calin Juravlefe317a32014-02-21 15:11:03 +0000476 struct stat sb;
477 ASSERT_EQ(0, fstat(tf.fd, &sb));
478}
479
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700480TEST(stdlib, system) {
481 int status;
482
483 status = system("exit 0");
484 ASSERT_TRUE(WIFEXITED(status));
485 ASSERT_EQ(0, WEXITSTATUS(status));
486
487 status = system("exit 1");
488 ASSERT_TRUE(WIFEXITED(status));
489 ASSERT_EQ(1, WEXITSTATUS(status));
490}
Elliott Hughes5a817382014-03-12 16:12:57 -0700491
Elliott Hughesbbbe27f2021-03-08 14:07:01 -0800492TEST(stdlib, system_NULL) {
493 // "The system() function shall always return non-zero when command is NULL."
494 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/system.html
495 ASSERT_NE(0, system(nullptr));
496}
497
Elliott Hughes5a817382014-03-12 16:12:57 -0700498TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700499 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700500}
501
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700502template <typename T>
503static void CheckStrToFloat(T fn(const char* s, char** end)) {
504 FpUlpEq<0, T> pred;
505
506 EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr));
507 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr));
508 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr));
509
Dan Albertf6346552016-12-02 12:02:03 -0800510 const char* s = " \t\v\f\r\n9.0";
511 char* p;
512 EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p));
513 EXPECT_EQ(s + strlen(s), p);
514
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700515 EXPECT_TRUE(isnan(fn("+nan", nullptr)));
516 EXPECT_TRUE(isnan(fn("nan", nullptr)));
517 EXPECT_TRUE(isnan(fn("-nan", nullptr)));
518
519 EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr)));
520 EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr)));
521 EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr)));
522
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700523 EXPECT_TRUE(isnan(fn("+nanny", &p)));
524 EXPECT_STREQ("ny", p);
525 EXPECT_TRUE(isnan(fn("nanny", &p)));
526 EXPECT_STREQ("ny", p);
527 EXPECT_TRUE(isnan(fn("-nanny", &p)));
528 EXPECT_STREQ("ny", p);
529
530 EXPECT_EQ(0, fn("muppet", &p));
531 EXPECT_STREQ("muppet", p);
532 EXPECT_EQ(0, fn(" muppet", &p));
533 EXPECT_STREQ(" muppet", p);
534
535 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr));
536 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr));
537 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr));
538
539 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr));
540 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr));
541 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr));
542
543 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p));
544 EXPECT_STREQ("initude", p);
545 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p));
546 EXPECT_STREQ("initude", p);
547 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p));
548 EXPECT_STREQ("initude", p);
549
550 // Check case-insensitivity.
551 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr));
552 EXPECT_TRUE(isnan(fn("NaN", nullptr)));
553}
554
Elliott Hughes5a817382014-03-12 16:12:57 -0700555TEST(stdlib, strtod) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700556 CheckStrToFloat(strtod);
Elliott Hughes5a817382014-03-12 16:12:57 -0700557}
558
559TEST(stdlib, strtof) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700560 CheckStrToFloat(strtof);
Elliott Hughes5a817382014-03-12 16:12:57 -0700561}
562
563TEST(stdlib, strtold) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700564 CheckStrToFloat(strtold);
Elliott Hughes5a817382014-03-12 16:12:57 -0700565}
Elliott Hughes9f525642014-04-08 17:14:01 -0700566
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700567TEST(stdlib, strtof_2206701) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700568 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", nullptr));
569 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", nullptr));
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700570}
571
572TEST(stdlib, strtod_largest_subnormal) {
573 // This value has been known to cause javac and java to infinite loop.
574 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
Yi Kong32bc0fc2018-08-02 17:31:13 -0700575 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", nullptr));
576 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", nullptr));
577 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", nullptr));
578 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", nullptr));
579 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", nullptr));
580 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", nullptr));
581 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", nullptr));
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700582}
583
Dan Albertb8425c52014-04-29 17:49:06 -0700584TEST(stdlib, quick_exit) {
585 pid_t pid = fork();
586 ASSERT_NE(-1, pid) << strerror(errno);
587
588 if (pid == 0) {
589 quick_exit(99);
590 }
591
Elliott Hughes33697a02016-01-26 13:04:57 -0800592 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700593}
594
595static int quick_exit_status = 0;
596
597static void quick_exit_1(void) {
598 ASSERT_EQ(quick_exit_status, 0);
599 quick_exit_status = 1;
600}
601
602static void quick_exit_2(void) {
603 ASSERT_EQ(quick_exit_status, 1);
604}
605
606static void not_run(void) {
607 FAIL();
608}
609
610TEST(stdlib, at_quick_exit) {
611 pid_t pid = fork();
612 ASSERT_NE(-1, pid) << strerror(errno);
613
614 if (pid == 0) {
615 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
616 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
617 atexit(not_run);
618 quick_exit(99);
619 }
620
Elliott Hughes33697a02016-01-26 13:04:57 -0800621 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700622}
623
Elliott Hughes9f525642014-04-08 17:14:01 -0700624TEST(unistd, _Exit) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800625 pid_t pid = fork();
Elliott Hughes9f525642014-04-08 17:14:01 -0700626 ASSERT_NE(-1, pid) << strerror(errno);
627
628 if (pid == 0) {
629 _Exit(99);
630 }
631
Elliott Hughes33697a02016-01-26 13:04:57 -0800632 AssertChildExited(pid, 99);
Elliott Hughes9f525642014-04-08 17:14:01 -0700633}
Elliott Hughes49167062014-07-25 17:24:00 -0700634
Colin Cross7da20342021-07-28 11:18:11 -0700635#if defined(MUSL)
636// musl doesn't have getpt
637int getpt() {
638 return posix_openpt(O_RDWR|O_NOCTTY);
639}
640#endif
641
Elliott Hughes49167062014-07-25 17:24:00 -0700642TEST(stdlib, pty_smoke) {
643 // getpt returns a pty with O_RDWR|O_NOCTTY.
644 int fd = getpt();
645 ASSERT_NE(-1, fd);
646
647 // grantpt is a no-op.
648 ASSERT_EQ(0, grantpt(fd));
649
650 // ptsname_r should start "/dev/pts/".
651 char name_r[128];
652 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
653 name_r[9] = 0;
654 ASSERT_STREQ("/dev/pts/", name_r);
655
656 close(fd);
657}
658
659TEST(stdlib, posix_openpt) {
660 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
661 ASSERT_NE(-1, fd);
662 close(fd);
663}
664
665TEST(stdlib, ptsname_r_ENOTTY) {
666 errno = 0;
667 char buf[128];
668 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
669 ASSERT_EQ(ENOTTY, errno);
670}
671
672TEST(stdlib, ptsname_r_EINVAL) {
673 int fd = getpt();
674 ASSERT_NE(-1, fd);
675 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700676 char* buf = nullptr;
Elliott Hughes49167062014-07-25 17:24:00 -0700677 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
678 ASSERT_EQ(EINVAL, errno);
679 close(fd);
680}
681
682TEST(stdlib, ptsname_r_ERANGE) {
683 int fd = getpt();
684 ASSERT_NE(-1, fd);
685 errno = 0;
686 char buf[1];
687 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
688 ASSERT_EQ(ERANGE, errno);
689 close(fd);
690}
691
Elliott Hughes728cde52017-11-08 21:53:50 -0800692TEST(stdlib, ttyname) {
693 int fd = getpt();
694 ASSERT_NE(-1, fd);
695
696 // ttyname returns "/dev/ptmx" for a pty.
697 ASSERT_STREQ("/dev/ptmx", ttyname(fd));
698
699 close(fd);
700}
701
Elliott Hughes49167062014-07-25 17:24:00 -0700702TEST(stdlib, ttyname_r) {
703 int fd = getpt();
704 ASSERT_NE(-1, fd);
705
706 // ttyname_r returns "/dev/ptmx" for a pty.
707 char name_r[128];
708 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
709 ASSERT_STREQ("/dev/ptmx", name_r);
710
711 close(fd);
712}
713
714TEST(stdlib, ttyname_r_ENOTTY) {
715 int fd = open("/dev/null", O_WRONLY);
716 errno = 0;
717 char buf[128];
718 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
719 ASSERT_EQ(ENOTTY, errno);
720 close(fd);
721}
722
723TEST(stdlib, ttyname_r_EINVAL) {
724 int fd = getpt();
725 ASSERT_NE(-1, fd);
726 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700727 char* buf = nullptr;
Elliott Hughes49167062014-07-25 17:24:00 -0700728 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
729 ASSERT_EQ(EINVAL, errno);
730 close(fd);
731}
732
733TEST(stdlib, ttyname_r_ERANGE) {
734 int fd = getpt();
735 ASSERT_NE(-1, fd);
736 errno = 0;
737 char buf[1];
738 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
739 ASSERT_EQ(ERANGE, errno);
740 close(fd);
741}
742
743TEST(stdlib, unlockpt_ENOTTY) {
744 int fd = open("/dev/null", O_WRONLY);
745 errno = 0;
746 ASSERT_EQ(-1, unlockpt(fd));
747 ASSERT_EQ(ENOTTY, errno);
748 close(fd);
749}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700750
Elliott Hughesdf143f82016-04-04 17:34:04 -0700751TEST(stdlib, getsubopt) {
752 char* const tokens[] = {
753 const_cast<char*>("a"),
754 const_cast<char*>("b"),
755 const_cast<char*>("foo"),
756 nullptr
757 };
758 std::string input = "a,b,foo=bar,a,unknown";
759 char* subopts = &input[0];
760 char* value = nullptr;
761
762 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
763 ASSERT_EQ(nullptr, value);
764 ASSERT_EQ(1, getsubopt(&subopts, tokens, &value));
765 ASSERT_EQ(nullptr, value);
766 ASSERT_EQ(2, getsubopt(&subopts, tokens, &value));
767 ASSERT_STREQ("bar", value);
768 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
769 ASSERT_EQ(nullptr, value);
770
771 ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
772}
Elliott Hughes6f6f9052016-04-28 14:54:52 -0700773
774TEST(stdlib, mblen) {
775 // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
776 // respectively, do or do not have state-dependent encodings." We're always UTF-8.
777 EXPECT_EQ(0, mblen(nullptr, 1));
778
779 ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
780
781 // 1-byte UTF-8.
782 EXPECT_EQ(1, mblen("abcdef", 6));
783 // 2-byte UTF-8.
784 EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
785 // 3-byte UTF-8.
786 EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
787 // 4-byte UTF-8.
788 EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
789
790 // Illegal over-long sequence.
791 ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
792
793 // "mblen() shall ... return 0 (if s points to the null byte)".
794 EXPECT_EQ(0, mblen("", 1));
795}
Elliott Hughesf826a372017-07-13 09:35:15 -0700796
797template <typename T>
798static void CheckStrToInt(T fn(const char* s, char** end, int base)) {
799 char* end_p;
800
801 // Negative base => invalid.
802 errno = 0;
803 ASSERT_EQ(T(0), fn("123", &end_p, -1));
804 ASSERT_EQ(EINVAL, errno);
805
806 // Base 1 => invalid (base 0 means "please guess").
807 errno = 0;
808 ASSERT_EQ(T(0), fn("123", &end_p, 1));
809 ASSERT_EQ(EINVAL, errno);
810
811 // Base > 36 => invalid.
812 errno = 0;
813 ASSERT_EQ(T(0), fn("123", &end_p, 37));
814 ASSERT_EQ(EINVAL, errno);
815
Elliott Hughes7cebf832020-08-12 14:25:41 -0700816 // Both leading + or - are always allowed (even for the strtou* family).
817 ASSERT_EQ(T(-123), fn("-123", &end_p, 10));
818 ASSERT_EQ(T(123), fn("+123", &end_p, 10));
819
Elliott Hughesf826a372017-07-13 09:35:15 -0700820 // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'.
821 ASSERT_EQ(T(0), fn("0xy", &end_p, 16));
822 ASSERT_EQ('x', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800823
Elliott Hughes7cebf832020-08-12 14:25:41 -0700824 // Hexadecimal (both the 0x and the digits) is case-insensitive.
825 ASSERT_EQ(T(0xab), fn("0xab", &end_p, 0));
826 ASSERT_EQ(T(0xab), fn("0Xab", &end_p, 0));
827 ASSERT_EQ(T(0xab), fn("0xAB", &end_p, 0));
828 ASSERT_EQ(T(0xab), fn("0XAB", &end_p, 0));
829 ASSERT_EQ(T(0xab), fn("0xAb", &end_p, 0));
830 ASSERT_EQ(T(0xab), fn("0XAb", &end_p, 0));
831
832 // Octal lives! (Sadly.)
833 ASSERT_EQ(T(0666), fn("0666", &end_p, 0));
834
Elliott Hughes1921dce2017-12-19 10:27:27 -0800835 if (std::numeric_limits<T>::is_signed) {
836 // Minimum (such as -128).
Elliott Hughesf61a06e2017-12-19 16:11:37 -0800837 std::string min{std::to_string(std::numeric_limits<T>::min())};
Elliott Hughescb239bd2017-12-20 17:37:11 -0800838 end_p = nullptr;
839 errno = 0;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800840 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
Elliott Hughescb239bd2017-12-20 17:37:11 -0800841 ASSERT_EQ(0, errno);
842 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800843 // Too negative (such as -129).
844 min.back() = (min.back() + 1);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800845 end_p = nullptr;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800846 errno = 0;
847 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
848 ASSERT_EQ(ERANGE, errno);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800849 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800850 }
851
852 // Maximum (such as 127).
Elliott Hughesf61a06e2017-12-19 16:11:37 -0800853 std::string max{std::to_string(std::numeric_limits<T>::max())};
Elliott Hughescb239bd2017-12-20 17:37:11 -0800854 end_p = nullptr;
855 errno = 0;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800856 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
Elliott Hughescb239bd2017-12-20 17:37:11 -0800857 ASSERT_EQ(0, errno);
858 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800859 // Too positive (such as 128).
860 max.back() = (max.back() + 1);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800861 end_p = nullptr;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800862 errno = 0;
863 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
864 ASSERT_EQ(ERANGE, errno);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800865 ASSERT_EQ('\0', *end_p);
866
867 // In case of overflow, strto* leaves us pointing past the end of the number,
868 // not at the digit that overflowed.
869 end_p = nullptr;
870 errno = 0;
871 ASSERT_EQ(std::numeric_limits<T>::max(),
872 fn("99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
873 ASSERT_EQ(ERANGE, errno);
874 ASSERT_STREQ("abc", end_p);
875 if (std::numeric_limits<T>::is_signed) {
876 end_p = nullptr;
877 errno = 0;
878 ASSERT_EQ(std::numeric_limits<T>::min(),
879 fn("-99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
880 ASSERT_EQ(ERANGE, errno);
881 ASSERT_STREQ("abc", end_p);
882 }
Elliott Hughesf826a372017-07-13 09:35:15 -0700883}
884
885TEST(stdlib, strtol_smoke) {
886 CheckStrToInt(strtol);
887}
888
889TEST(stdlib, strtoll_smoke) {
890 CheckStrToInt(strtoll);
891}
892
893TEST(stdlib, strtoul_smoke) {
894 CheckStrToInt(strtoul);
895}
896
897TEST(stdlib, strtoull_smoke) {
898 CheckStrToInt(strtoull);
899}
900
901TEST(stdlib, strtoimax_smoke) {
902 CheckStrToInt(strtoimax);
903}
904
905TEST(stdlib, strtoumax_smoke) {
906 CheckStrToInt(strtoumax);
907}
Elliott Hughes00d8a8b2017-09-07 16:42:13 -0700908
Elliott Hughes7cebf832020-08-12 14:25:41 -0700909TEST(stdlib, atoi) {
910 // Implemented using strtol in bionic, so extensive testing unnecessary.
911 ASSERT_EQ(123, atoi("123four"));
912 ASSERT_EQ(0, atoi("hello"));
913}
914
915TEST(stdlib, atol) {
916 // Implemented using strtol in bionic, so extensive testing unnecessary.
917 ASSERT_EQ(123L, atol("123four"));
918 ASSERT_EQ(0L, atol("hello"));
919}
920
Elliott Hughes00d8a8b2017-09-07 16:42:13 -0700921TEST(stdlib, abs) {
922 ASSERT_EQ(INT_MAX, abs(-INT_MAX));
923 ASSERT_EQ(INT_MAX, abs(INT_MAX));
924}
925
926TEST(stdlib, labs) {
927 ASSERT_EQ(LONG_MAX, labs(-LONG_MAX));
928 ASSERT_EQ(LONG_MAX, labs(LONG_MAX));
929}
930
931TEST(stdlib, llabs) {
932 ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX));
933 ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX));
934}
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700935
936TEST(stdlib, getloadavg) {
937 double load[3];
938
939 // The second argument should have been size_t.
940 ASSERT_EQ(-1, getloadavg(load, -1));
941 ASSERT_EQ(-1, getloadavg(load, INT_MIN));
942
943 // Zero is a no-op.
944 ASSERT_EQ(0, getloadavg(load, 0));
945
946 // The Linux kernel doesn't support more than 3 (but you can ask for fewer).
947 ASSERT_EQ(1, getloadavg(load, 1));
948 ASSERT_EQ(2, getloadavg(load, 2));
949 ASSERT_EQ(3, getloadavg(load, 3));
950 ASSERT_EQ(3, getloadavg(load, 4));
951 ASSERT_EQ(3, getloadavg(load, INT_MAX));
952
953 // Read /proc/loadavg and check that it's "close enough".
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700954 double expected[3];
955 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/loadavg", "re"), fclose};
956 ASSERT_EQ(3, fscanf(fp.get(), "%lf %lf %lf", &expected[0], &expected[1], &expected[2]));
Elliott Hughes72a54a42018-12-18 14:47:25 -0800957 load[0] = load[1] = load[2] = nan("");
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700958 ASSERT_EQ(3, getloadavg(load, 3));
959
Elliott Hughes72a54a42018-12-18 14:47:25 -0800960 // Check that getloadavg(3) at least overwrote the NaNs.
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700961 ASSERT_FALSE(isnan(load[0]));
Elliott Hughes72a54a42018-12-18 14:47:25 -0800962 ASSERT_FALSE(isnan(load[1]));
963 ASSERT_FALSE(isnan(load[2]));
964 // And that the difference between /proc/loadavg and getloadavg(3) is "small".
965 ASSERT_TRUE(fabs(expected[0] - load[0]) < 0.5) << expected[0] << ' ' << load[0];
966 ASSERT_TRUE(fabs(expected[1] - load[1]) < 0.5) << expected[1] << ' ' << load[1];
967 ASSERT_TRUE(fabs(expected[2] - load[2]) < 0.5) << expected[2] << ' ' << load[2];
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700968}
Elliott Hughes75064c12020-01-22 20:46:12 -0800969
970TEST(stdlib, getprogname) {
Colin Cross7da20342021-07-28 11:18:11 -0700971#if defined(__GLIBC__) || defined(MUSL)
972 GTEST_SKIP() << "glibc and musl don't have getprogname()";
Elliott Hughes75064c12020-01-22 20:46:12 -0800973#else
974 // You should always have a name.
975 ASSERT_TRUE(getprogname() != nullptr);
976 // The name should never have a slash in it.
977 ASSERT_TRUE(strchr(getprogname(), '/') == nullptr);
978#endif
979}
980
981TEST(stdlib, setprogname) {
Colin Cross7da20342021-07-28 11:18:11 -0700982#if defined(__GLIBC__) || defined(MUSL)
983 GTEST_SKIP() << "glibc and musl don't have setprogname()";
Elliott Hughes75064c12020-01-22 20:46:12 -0800984#else
985 // setprogname() only takes the basename of what you give it.
986 setprogname("/usr/bin/muppet");
987 ASSERT_STREQ("muppet", getprogname());
988#endif
989}