blob: 14848aebb8f8e3df21383456d29d4fcf79c3eb8a [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
17#include <gtest/gtest.h>
Elliott Hughes33697a02016-01-26 13:04:57 -080018
Yabin Cui9df70402014-11-05 18:01:01 -080019#include "BionicDeathTest.h"
Elliott Hughes7f0849f2016-08-26 16:17:17 -070020#include "math_data_test.h"
Calin Juravlefe317a32014-02-21 15:11:03 +000021#include "TemporaryFile.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080022#include "utils.h"
Elliott Hughes774c7f52012-10-01 13:11:03 -070023
Elliott Hughesb16b7222013-02-04 13:18:00 -080024#include <errno.h>
Elliott Hughes7f0849f2016-08-26 16:17:17 -070025#include <fcntl.h>
Elliott Hughesf0777842013-03-01 16:59:46 -080026#include <libgen.h>
27#include <limits.h>
Elliott Hughes7f0849f2016-08-26 16:17:17 -070028#include <math.h>
Elliott Hughes877ec6d2013-11-15 17:40:18 -080029#include <pthread.h>
Elliott Hughesb16b7222013-02-04 13:18:00 -080030#include <stdint.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070031#include <stdlib.h>
Elliott Hughes40488562014-03-12 13:50:38 -070032#include <sys/types.h>
33#include <sys/wait.h>
Elliott Hughes774c7f52012-10-01 13:11:03 -070034
Elliott Hughes1921dce2017-12-19 10:27:27 -080035#include <limits>
Elliott Hughesf61a06e2017-12-19 16:11:37 -080036#include <string>
Elliott Hughes1921dce2017-12-19 10:27:27 -080037
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080038#include "utils.h"
39
Christopher Ferriscae21a92018-02-05 18:14:55 -080040#if defined(__BIONIC__)
41 #define ALIGNED_ALLOC_AVAILABLE 1
42#elif defined(__GLIBC_PREREQ)
43 #if __GLIBC_PREREQ(2, 16)
44 #define ALIGNED_ALLOC_AVAILABLE 1
45 #endif
46#endif
47
Elliott Hughes274afe82014-11-06 12:40:08 -080048// The random number generator tests all set the seed, get four values, reset the seed and check
49// that they get the first two values repeated, and then reset the seed and check two more values
50// to rule out the possibility that we're just going round a cycle of four values.
51// TODO: factor this out.
52
Elliott Hughes774c7f52012-10-01 13:11:03 -070053TEST(stdlib, drand48) {
54 srand48(0x01020304);
55 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
56 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
57 EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
58 EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
Elliott Hughes274afe82014-11-06 12:40:08 -080059 srand48(0x01020304);
60 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
61 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
62 srand48(0x01020304);
63 EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
64 EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
65}
66
67TEST(stdlib, erand48) {
68 const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 };
69 unsigned short xsubi[3];
70 memcpy(xsubi, seed, sizeof(seed));
71 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
72 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
73 EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi));
74 EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi));
75 memcpy(xsubi, seed, sizeof(seed));
76 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
77 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
78 memcpy(xsubi, seed, sizeof(seed));
79 EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
80 EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
81}
82
83TEST(stdlib, lcong48) {
84 unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e };
85 lcong48(p);
86 EXPECT_EQ(1531389981, lrand48());
87 EXPECT_EQ(1598801533, lrand48());
88 EXPECT_EQ(2080534853, lrand48());
89 EXPECT_EQ(1102488897, lrand48());
90 lcong48(p);
91 EXPECT_EQ(1531389981, lrand48());
92 EXPECT_EQ(1598801533, lrand48());
93 lcong48(p);
94 EXPECT_EQ(1531389981, lrand48());
95 EXPECT_EQ(1598801533, lrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -070096}
97
Elliott Hughesa0beeea2014-06-12 11:48:04 -070098TEST(stdlib, lrand48) {
Elliott Hughes774c7f52012-10-01 13:11:03 -070099 srand48(0x01020304);
100 EXPECT_EQ(1409163720, lrand48());
101 EXPECT_EQ(397769746, lrand48());
102 EXPECT_EQ(902267124, lrand48());
103 EXPECT_EQ(132366131, lrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800104 srand48(0x01020304);
105 EXPECT_EQ(1409163720, lrand48());
106 EXPECT_EQ(397769746, lrand48());
107 srand48(0x01020304);
108 EXPECT_EQ(1409163720, lrand48());
109 EXPECT_EQ(397769746, lrand48());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700110}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700111
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700112TEST(stdlib, random) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700113 srandom(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700114 EXPECT_EQ(55436735, random());
115 EXPECT_EQ(1399865117, random());
116 EXPECT_EQ(2032643283, random());
117 EXPECT_EQ(571329216, random());
Elliott Hughes274afe82014-11-06 12:40:08 -0800118 srandom(0x01020304);
119 EXPECT_EQ(55436735, random());
120 EXPECT_EQ(1399865117, random());
121 srandom(0x01020304);
122 EXPECT_EQ(55436735, random());
123 EXPECT_EQ(1399865117, random());
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700124}
Elliott Hughes774c7f52012-10-01 13:11:03 -0700125
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700126TEST(stdlib, rand) {
Elliott Hughes774c7f52012-10-01 13:11:03 -0700127 srand(0x01020304);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700128 EXPECT_EQ(55436735, rand());
129 EXPECT_EQ(1399865117, rand());
130 EXPECT_EQ(2032643283, rand());
131 EXPECT_EQ(571329216, rand());
Elliott Hughes274afe82014-11-06 12:40:08 -0800132 srand(0x01020304);
133 EXPECT_EQ(55436735, rand());
134 EXPECT_EQ(1399865117, rand());
135 srand(0x01020304);
136 EXPECT_EQ(55436735, rand());
137 EXPECT_EQ(1399865117, rand());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700138}
139
140TEST(stdlib, mrand48) {
141 srand48(0x01020304);
142 EXPECT_EQ(-1476639856, mrand48());
143 EXPECT_EQ(795539493, mrand48());
144 EXPECT_EQ(1804534249, mrand48());
145 EXPECT_EQ(264732262, mrand48());
Elliott Hughes274afe82014-11-06 12:40:08 -0800146 srand48(0x01020304);
147 EXPECT_EQ(-1476639856, mrand48());
148 EXPECT_EQ(795539493, mrand48());
149 srand48(0x01020304);
150 EXPECT_EQ(-1476639856, mrand48());
151 EXPECT_EQ(795539493, mrand48());
Elliott Hughes774c7f52012-10-01 13:11:03 -0700152}
Elliott Hughesb16b7222013-02-04 13:18:00 -0800153
Aleksandra Tsvetkova608b4512015-02-27 15:01:59 +0300154TEST(stdlib, jrand48_distribution) {
155 const int iterations = 4096;
156 const int pivot_low = 1536;
157 const int pivot_high = 2560;
158
159 unsigned short xsubi[3];
160 int bits[32] = {};
161
162 for (int iter = 0; iter < iterations; ++iter) {
163 long rand_val = jrand48(xsubi);
164 for (int bit = 0; bit < 32; ++bit) {
165 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
166 }
167 }
168
169 // Check that bit probability is uniform
170 for (int bit = 0; bit < 32; ++bit) {
171 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
172 }
173}
174
175TEST(stdlib, mrand48_distribution) {
176 const int iterations = 4096;
177 const int pivot_low = 1536;
178 const int pivot_high = 2560;
179
180 int bits[32] = {};
181
182 for (int iter = 0; iter < iterations; ++iter) {
183 long rand_val = mrand48();
184 for (int bit = 0; bit < 32; ++bit) {
185 bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
186 }
187 }
188
189 // Check that bit probability is uniform
190 for (int bit = 0; bit < 32; ++bit) {
191 EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
192 }
193}
194
Christopher Ferris3a32d952017-06-15 13:30:44 -0700195TEST(stdlib, posix_memalign_sweep) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800196 SKIP_WITH_HWASAN;
Christopher Ferris3a32d952017-06-15 13:30:44 -0700197 void* ptr;
Elliott Hughesb16b7222013-02-04 13:18:00 -0800198
Christopher Ferris3a32d952017-06-15 13:30:44 -0700199 // These should all fail.
200 for (size_t align = 0; align < sizeof(long); align++) {
201 ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256))
202 << "Unexpected value at align " << align;
203 }
Elliott Hughesb16b7222013-02-04 13:18:00 -0800204
Christopher Ferris3a32d952017-06-15 13:30:44 -0700205 // Verify powers of 2 up to 2048 allocate, and verify that all other
206 // alignment values between the powers of 2 fail.
207 size_t last_align = sizeof(long);
208 for (size_t align = sizeof(long); align <= 2048; align <<= 1) {
209 // Try all of the non power of 2 values from the last until this value.
210 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
211 ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256))
212 << "Unexpected success at align " << fail_align;
213 }
214 ASSERT_EQ(0, posix_memalign(&ptr, align, 256))
215 << "Unexpected failure at align " << align;
216 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
217 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
218 free(ptr);
219 last_align = align;
220 }
221}
222
223TEST(stdlib, posix_memalign_various_sizes) {
224 std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000};
225 for (auto size : sizes) {
226 void* ptr;
227 ASSERT_EQ(0, posix_memalign(&ptr, 16, 1))
228 << "posix_memalign failed at size " << size;
229 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf)
230 << "Pointer not aligned at size " << size << " ptr " << ptr;
231 free(ptr);
232 }
233}
234
235TEST(stdlib, posix_memalign_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800236 SKIP_WITH_HWASAN;
Christopher Ferris3a32d952017-06-15 13:30:44 -0700237 void* ptr;
238 ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX));
Elliott Hughesb16b7222013-02-04 13:18:00 -0800239}
Elliott Hughesf0777842013-03-01 16:59:46 -0800240
Christopher Ferriscae21a92018-02-05 18:14:55 -0800241TEST(stdlib, aligned_alloc_sweep) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800242 SKIP_WITH_HWASAN;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800243#if defined(ALIGNED_ALLOC_AVAILABLE)
244 // Verify powers of 2 up to 2048 allocate, and verify that all other
245 // alignment values between the powers of 2 fail.
246 size_t last_align = 1;
247 for (size_t align = 1; align <= 2048; align <<= 1) {
248 // Try all of the non power of 2 values from the last until this value.
249 for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
250 ASSERT_TRUE(aligned_alloc(fail_align, 256) == nullptr)
251 << "Unexpected success at align " << fail_align;
252 ASSERT_EQ(EINVAL, errno) << "Unexpected errno at align " << fail_align;
253 }
254 void* ptr = aligned_alloc(align, 256);
255 ASSERT_TRUE(ptr != nullptr) << "Unexpected failure at align " << align;
256 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
257 << "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
258 free(ptr);
259 last_align = align;
260 }
261#else
262 GTEST_LOG_(INFO) << "This test requires a C library that has aligned_alloc.\n";
263#endif
264}
265
266TEST(stdlib, aligned_alloc_overflow) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800267 SKIP_WITH_HWASAN;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800268#if defined(ALIGNED_ALLOC_AVAILABLE)
269 ASSERT_TRUE(aligned_alloc(16, SIZE_MAX) == nullptr);
270#else
271 GTEST_LOG_(INFO) << "This test requires a C library that has aligned_alloc.\n";
272#endif
273}
274
275TEST(stdlib, aligned_alloc_size_not_multiple_of_alignment) {
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -0800276 SKIP_WITH_HWASAN;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800277#if defined(ALIGNED_ALLOC_AVAILABLE)
278 for (size_t size = 1; size <= 2048; size++) {
279 void* ptr = aligned_alloc(2048, size);
280 ASSERT_TRUE(ptr != nullptr) << "Failed at size " << std::to_string(size);
281 free(ptr);
282 }
283#else
284 GTEST_LOG_(INFO) << "This test requires a C library that has aligned_alloc.\n";
285#endif
286}
287
Elliott Hughesf0777842013-03-01 16:59:46 -0800288TEST(stdlib, realpath__NULL_filename) {
289 errno = 0;
George Burgess IV95bd4882017-08-14 14:48:55 -0700290 // Work around the compile-time error generated by FORTIFY here.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700291 const char* path = nullptr;
292 char* p = realpath(path, nullptr);
293 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800294 ASSERT_EQ(EINVAL, errno);
295}
296
297TEST(stdlib, realpath__empty_filename) {
298 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700299 char* p = realpath("", nullptr);
300 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800301 ASSERT_EQ(ENOENT, errno);
302}
303
304TEST(stdlib, realpath__ENOENT) {
305 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700306 char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", nullptr);
307 ASSERT_TRUE(p == nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800308 ASSERT_EQ(ENOENT, errno);
309}
310
Elliott Hughes31e072f2014-09-30 16:15:42 -0700311TEST(stdlib, realpath__component_after_non_directory) {
312 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700313 char* p = realpath("/dev/null/.", nullptr);
314 ASSERT_TRUE(p == nullptr);
Elliott Hughes31e072f2014-09-30 16:15:42 -0700315 ASSERT_EQ(ENOTDIR, errno);
316
317 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700318 p = realpath("/dev/null/..", nullptr);
319 ASSERT_TRUE(p == nullptr);
Elliott Hughes31e072f2014-09-30 16:15:42 -0700320 ASSERT_EQ(ENOTDIR, errno);
321}
322
Elliott Hughesf0777842013-03-01 16:59:46 -0800323TEST(stdlib, realpath) {
324 // Get the name of this executable.
325 char executable_path[PATH_MAX];
326 int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
327 ASSERT_NE(rc, -1);
328 executable_path[rc] = '\0';
329
330 char buf[PATH_MAX + 1];
331 char* p = realpath("/proc/self/exe", buf);
332 ASSERT_STREQ(executable_path, p);
333
Yi Kong32bc0fc2018-08-02 17:31:13 -0700334 p = realpath("/proc/self/exe", nullptr);
Elliott Hughesf0777842013-03-01 16:59:46 -0800335 ASSERT_STREQ(executable_path, p);
336 free(p);
337}
Elliott Hughes0b25f632013-04-11 18:08:34 -0700338
339TEST(stdlib, qsort) {
340 struct s {
341 char name[16];
342 static int comparator(const void* lhs, const void* rhs) {
343 return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
344 }
345 };
346 s entries[3];
347 strcpy(entries[0].name, "charlie");
348 strcpy(entries[1].name, "bravo");
349 strcpy(entries[2].name, "alpha");
350
351 qsort(entries, 3, sizeof(s), s::comparator);
352 ASSERT_STREQ("alpha", entries[0].name);
353 ASSERT_STREQ("bravo", entries[1].name);
354 ASSERT_STREQ("charlie", entries[2].name);
355
356 qsort(entries, 3, sizeof(s), s::comparator);
357 ASSERT_STREQ("alpha", entries[0].name);
358 ASSERT_STREQ("bravo", entries[1].name);
359 ASSERT_STREQ("charlie", entries[2].name);
360}
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800361
362static void* TestBug57421_child(void* arg) {
363 pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700364 pthread_join(main_thread, nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800365 char* value = getenv("ENVIRONMENT_VARIABLE");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700366 if (value == nullptr) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800367 setenv("ENVIRONMENT_VARIABLE", "value", 1);
368 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700369 return nullptr;
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800370}
371
372static void TestBug57421_main() {
373 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700374 ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
375 pthread_exit(nullptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800376}
377
378// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
379// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800380
381class stdlib_DeathTest : public BionicDeathTest {};
382
383TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800384 // https://code.google.com/p/android/issues/detail?id=57421
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800385 ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
386}
Calin Juravlefe317a32014-02-21 15:11:03 +0000387
Elliott Hughes31165ed2014-09-23 17:34:29 -0700388TEST(stdlib, mkostemp64) {
389 TemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
Elliott Hughesa7f12942017-12-15 13:55:53 -0800390 AssertCloseOnExec(tf.fd, true);
Elliott Hughes31165ed2014-09-23 17:34:29 -0700391}
392
393TEST(stdlib, mkostemp) {
394 TemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
Elliott Hughesa7f12942017-12-15 13:55:53 -0800395 AssertCloseOnExec(tf.fd, true);
Elliott Hughes31165ed2014-09-23 17:34:29 -0700396}
397
398TEST(stdlib, mkstemp64) {
399 TemporaryFile tf(mkstemp64);
400 struct stat64 sb;
401 ASSERT_EQ(0, fstat64(tf.fd, &sb));
402 ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
403}
404
Calin Juravlefe317a32014-02-21 15:11:03 +0000405TEST(stdlib, mkstemp) {
406 TemporaryFile tf;
407 struct stat sb;
408 ASSERT_EQ(0, fstat(tf.fd, &sb));
409}
410
Elliott Hughes3cdf5732014-03-11 12:54:44 -0700411TEST(stdlib, system) {
412 int status;
413
414 status = system("exit 0");
415 ASSERT_TRUE(WIFEXITED(status));
416 ASSERT_EQ(0, WEXITSTATUS(status));
417
418 status = system("exit 1");
419 ASSERT_TRUE(WIFEXITED(status));
420 ASSERT_EQ(1, WEXITSTATUS(status));
421}
Elliott Hughes5a817382014-03-12 16:12:57 -0700422
423TEST(stdlib, atof) {
Christopher Ferrisf171b342014-03-17 16:40:26 -0700424 ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
Elliott Hughes5a817382014-03-12 16:12:57 -0700425}
426
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700427template <typename T>
428static void CheckStrToFloat(T fn(const char* s, char** end)) {
429 FpUlpEq<0, T> pred;
430
431 EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr));
432 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr));
433 EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr));
434
Dan Albertf6346552016-12-02 12:02:03 -0800435 const char* s = " \t\v\f\r\n9.0";
436 char* p;
437 EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p));
438 EXPECT_EQ(s + strlen(s), p);
439
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700440 EXPECT_TRUE(isnan(fn("+nan", nullptr)));
441 EXPECT_TRUE(isnan(fn("nan", nullptr)));
442 EXPECT_TRUE(isnan(fn("-nan", nullptr)));
443
444 EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr)));
445 EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr)));
446 EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr)));
447
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700448 EXPECT_TRUE(isnan(fn("+nanny", &p)));
449 EXPECT_STREQ("ny", p);
450 EXPECT_TRUE(isnan(fn("nanny", &p)));
451 EXPECT_STREQ("ny", p);
452 EXPECT_TRUE(isnan(fn("-nanny", &p)));
453 EXPECT_STREQ("ny", p);
454
455 EXPECT_EQ(0, fn("muppet", &p));
456 EXPECT_STREQ("muppet", p);
457 EXPECT_EQ(0, fn(" muppet", &p));
458 EXPECT_STREQ(" muppet", p);
459
460 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr));
461 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr));
462 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr));
463
464 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr));
465 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr));
466 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr));
467
468 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p));
469 EXPECT_STREQ("initude", p);
470 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p));
471 EXPECT_STREQ("initude", p);
472 EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p));
473 EXPECT_STREQ("initude", p);
474
475 // Check case-insensitivity.
476 EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr));
477 EXPECT_TRUE(isnan(fn("NaN", nullptr)));
478}
479
Elliott Hughes5a817382014-03-12 16:12:57 -0700480TEST(stdlib, strtod) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700481 CheckStrToFloat(strtod);
Elliott Hughes5a817382014-03-12 16:12:57 -0700482}
483
484TEST(stdlib, strtof) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700485 CheckStrToFloat(strtof);
Elliott Hughes5a817382014-03-12 16:12:57 -0700486}
487
488TEST(stdlib, strtold) {
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700489 CheckStrToFloat(strtold);
Elliott Hughes5a817382014-03-12 16:12:57 -0700490}
Elliott Hughes9f525642014-04-08 17:14:01 -0700491
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700492TEST(stdlib, strtof_2206701) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700493 ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", nullptr));
494 ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", nullptr));
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700495}
496
497TEST(stdlib, strtod_largest_subnormal) {
498 // This value has been known to cause javac and java to infinite loop.
499 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
Yi Kong32bc0fc2018-08-02 17:31:13 -0700500 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", nullptr));
501 ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", nullptr));
502 ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", nullptr));
503 ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", nullptr));
504 ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", nullptr));
505 ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", nullptr));
506 ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", nullptr));
Elliott Hughes89aaaff2014-10-28 17:54:23 -0700507}
508
Dan Albertb8425c52014-04-29 17:49:06 -0700509TEST(stdlib, quick_exit) {
510 pid_t pid = fork();
511 ASSERT_NE(-1, pid) << strerror(errno);
512
513 if (pid == 0) {
514 quick_exit(99);
515 }
516
Elliott Hughes33697a02016-01-26 13:04:57 -0800517 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700518}
519
520static int quick_exit_status = 0;
521
522static void quick_exit_1(void) {
523 ASSERT_EQ(quick_exit_status, 0);
524 quick_exit_status = 1;
525}
526
527static void quick_exit_2(void) {
528 ASSERT_EQ(quick_exit_status, 1);
529}
530
531static void not_run(void) {
532 FAIL();
533}
534
535TEST(stdlib, at_quick_exit) {
536 pid_t pid = fork();
537 ASSERT_NE(-1, pid) << strerror(errno);
538
539 if (pid == 0) {
540 ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
541 ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
542 atexit(not_run);
543 quick_exit(99);
544 }
545
Elliott Hughes33697a02016-01-26 13:04:57 -0800546 AssertChildExited(pid, 99);
Dan Albertb8425c52014-04-29 17:49:06 -0700547}
548
Elliott Hughes9f525642014-04-08 17:14:01 -0700549TEST(unistd, _Exit) {
Elliott Hughes33697a02016-01-26 13:04:57 -0800550 pid_t pid = fork();
Elliott Hughes9f525642014-04-08 17:14:01 -0700551 ASSERT_NE(-1, pid) << strerror(errno);
552
553 if (pid == 0) {
554 _Exit(99);
555 }
556
Elliott Hughes33697a02016-01-26 13:04:57 -0800557 AssertChildExited(pid, 99);
Elliott Hughes9f525642014-04-08 17:14:01 -0700558}
Elliott Hughes49167062014-07-25 17:24:00 -0700559
560TEST(stdlib, pty_smoke) {
561 // getpt returns a pty with O_RDWR|O_NOCTTY.
562 int fd = getpt();
563 ASSERT_NE(-1, fd);
564
565 // grantpt is a no-op.
566 ASSERT_EQ(0, grantpt(fd));
567
568 // ptsname_r should start "/dev/pts/".
569 char name_r[128];
570 ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
571 name_r[9] = 0;
572 ASSERT_STREQ("/dev/pts/", name_r);
573
574 close(fd);
575}
576
577TEST(stdlib, posix_openpt) {
578 int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
579 ASSERT_NE(-1, fd);
580 close(fd);
581}
582
583TEST(stdlib, ptsname_r_ENOTTY) {
584 errno = 0;
585 char buf[128];
586 ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
587 ASSERT_EQ(ENOTTY, errno);
588}
589
590TEST(stdlib, ptsname_r_EINVAL) {
591 int fd = getpt();
592 ASSERT_NE(-1, fd);
593 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700594 char* buf = nullptr;
Elliott Hughes49167062014-07-25 17:24:00 -0700595 ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
596 ASSERT_EQ(EINVAL, errno);
597 close(fd);
598}
599
600TEST(stdlib, ptsname_r_ERANGE) {
601 int fd = getpt();
602 ASSERT_NE(-1, fd);
603 errno = 0;
604 char buf[1];
605 ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
606 ASSERT_EQ(ERANGE, errno);
607 close(fd);
608}
609
Elliott Hughes728cde52017-11-08 21:53:50 -0800610TEST(stdlib, ttyname) {
611 int fd = getpt();
612 ASSERT_NE(-1, fd);
613
614 // ttyname returns "/dev/ptmx" for a pty.
615 ASSERT_STREQ("/dev/ptmx", ttyname(fd));
616
617 close(fd);
618}
619
Elliott Hughes49167062014-07-25 17:24:00 -0700620TEST(stdlib, ttyname_r) {
621 int fd = getpt();
622 ASSERT_NE(-1, fd);
623
624 // ttyname_r returns "/dev/ptmx" for a pty.
625 char name_r[128];
626 ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
627 ASSERT_STREQ("/dev/ptmx", name_r);
628
629 close(fd);
630}
631
632TEST(stdlib, ttyname_r_ENOTTY) {
633 int fd = open("/dev/null", O_WRONLY);
634 errno = 0;
635 char buf[128];
636 ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
637 ASSERT_EQ(ENOTTY, errno);
638 close(fd);
639}
640
641TEST(stdlib, ttyname_r_EINVAL) {
642 int fd = getpt();
643 ASSERT_NE(-1, fd);
644 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700645 char* buf = nullptr;
Elliott Hughes49167062014-07-25 17:24:00 -0700646 ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
647 ASSERT_EQ(EINVAL, errno);
648 close(fd);
649}
650
651TEST(stdlib, ttyname_r_ERANGE) {
652 int fd = getpt();
653 ASSERT_NE(-1, fd);
654 errno = 0;
655 char buf[1];
656 ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
657 ASSERT_EQ(ERANGE, errno);
658 close(fd);
659}
660
661TEST(stdlib, unlockpt_ENOTTY) {
662 int fd = open("/dev/null", O_WRONLY);
663 errno = 0;
664 ASSERT_EQ(-1, unlockpt(fd));
665 ASSERT_EQ(ENOTTY, errno);
666 close(fd);
667}
Elliott Hughesb05ec5a2014-09-23 14:53:10 -0700668
Elliott Hughesdf143f82016-04-04 17:34:04 -0700669TEST(stdlib, getsubopt) {
670 char* const tokens[] = {
671 const_cast<char*>("a"),
672 const_cast<char*>("b"),
673 const_cast<char*>("foo"),
674 nullptr
675 };
676 std::string input = "a,b,foo=bar,a,unknown";
677 char* subopts = &input[0];
678 char* value = nullptr;
679
680 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
681 ASSERT_EQ(nullptr, value);
682 ASSERT_EQ(1, getsubopt(&subopts, tokens, &value));
683 ASSERT_EQ(nullptr, value);
684 ASSERT_EQ(2, getsubopt(&subopts, tokens, &value));
685 ASSERT_STREQ("bar", value);
686 ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
687 ASSERT_EQ(nullptr, value);
688
689 ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
690}
Elliott Hughes6f6f9052016-04-28 14:54:52 -0700691
692TEST(stdlib, mblen) {
693 // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
694 // respectively, do or do not have state-dependent encodings." We're always UTF-8.
695 EXPECT_EQ(0, mblen(nullptr, 1));
696
697 ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
698
699 // 1-byte UTF-8.
700 EXPECT_EQ(1, mblen("abcdef", 6));
701 // 2-byte UTF-8.
702 EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
703 // 3-byte UTF-8.
704 EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
705 // 4-byte UTF-8.
706 EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
707
708 // Illegal over-long sequence.
709 ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
710
711 // "mblen() shall ... return 0 (if s points to the null byte)".
712 EXPECT_EQ(0, mblen("", 1));
713}
Elliott Hughesf826a372017-07-13 09:35:15 -0700714
715template <typename T>
716static void CheckStrToInt(T fn(const char* s, char** end, int base)) {
717 char* end_p;
718
719 // Negative base => invalid.
720 errno = 0;
721 ASSERT_EQ(T(0), fn("123", &end_p, -1));
722 ASSERT_EQ(EINVAL, errno);
723
724 // Base 1 => invalid (base 0 means "please guess").
725 errno = 0;
726 ASSERT_EQ(T(0), fn("123", &end_p, 1));
727 ASSERT_EQ(EINVAL, errno);
728
729 // Base > 36 => invalid.
730 errno = 0;
731 ASSERT_EQ(T(0), fn("123", &end_p, 37));
732 ASSERT_EQ(EINVAL, errno);
733
734 // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'.
735 ASSERT_EQ(T(0), fn("0xy", &end_p, 16));
736 ASSERT_EQ('x', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800737
738 if (std::numeric_limits<T>::is_signed) {
739 // Minimum (such as -128).
Elliott Hughesf61a06e2017-12-19 16:11:37 -0800740 std::string min{std::to_string(std::numeric_limits<T>::min())};
Elliott Hughescb239bd2017-12-20 17:37:11 -0800741 end_p = nullptr;
742 errno = 0;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800743 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
Elliott Hughescb239bd2017-12-20 17:37:11 -0800744 ASSERT_EQ(0, errno);
745 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800746 // Too negative (such as -129).
747 min.back() = (min.back() + 1);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800748 end_p = nullptr;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800749 errno = 0;
750 ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
751 ASSERT_EQ(ERANGE, errno);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800752 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800753 }
754
755 // Maximum (such as 127).
Elliott Hughesf61a06e2017-12-19 16:11:37 -0800756 std::string max{std::to_string(std::numeric_limits<T>::max())};
Elliott Hughescb239bd2017-12-20 17:37:11 -0800757 end_p = nullptr;
758 errno = 0;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800759 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
Elliott Hughescb239bd2017-12-20 17:37:11 -0800760 ASSERT_EQ(0, errno);
761 ASSERT_EQ('\0', *end_p);
Elliott Hughes1921dce2017-12-19 10:27:27 -0800762 // Too positive (such as 128).
763 max.back() = (max.back() + 1);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800764 end_p = nullptr;
Elliott Hughes1921dce2017-12-19 10:27:27 -0800765 errno = 0;
766 ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
767 ASSERT_EQ(ERANGE, errno);
Elliott Hughescb239bd2017-12-20 17:37:11 -0800768 ASSERT_EQ('\0', *end_p);
769
770 // In case of overflow, strto* leaves us pointing past the end of the number,
771 // not at the digit that overflowed.
772 end_p = nullptr;
773 errno = 0;
774 ASSERT_EQ(std::numeric_limits<T>::max(),
775 fn("99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
776 ASSERT_EQ(ERANGE, errno);
777 ASSERT_STREQ("abc", end_p);
778 if (std::numeric_limits<T>::is_signed) {
779 end_p = nullptr;
780 errno = 0;
781 ASSERT_EQ(std::numeric_limits<T>::min(),
782 fn("-99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
783 ASSERT_EQ(ERANGE, errno);
784 ASSERT_STREQ("abc", end_p);
785 }
Elliott Hughesf826a372017-07-13 09:35:15 -0700786}
787
788TEST(stdlib, strtol_smoke) {
789 CheckStrToInt(strtol);
790}
791
792TEST(stdlib, strtoll_smoke) {
793 CheckStrToInt(strtoll);
794}
795
796TEST(stdlib, strtoul_smoke) {
797 CheckStrToInt(strtoul);
798}
799
800TEST(stdlib, strtoull_smoke) {
801 CheckStrToInt(strtoull);
802}
803
804TEST(stdlib, strtoimax_smoke) {
805 CheckStrToInt(strtoimax);
806}
807
808TEST(stdlib, strtoumax_smoke) {
809 CheckStrToInt(strtoumax);
810}
Elliott Hughes00d8a8b2017-09-07 16:42:13 -0700811
812TEST(stdlib, abs) {
813 ASSERT_EQ(INT_MAX, abs(-INT_MAX));
814 ASSERT_EQ(INT_MAX, abs(INT_MAX));
815}
816
817TEST(stdlib, labs) {
818 ASSERT_EQ(LONG_MAX, labs(-LONG_MAX));
819 ASSERT_EQ(LONG_MAX, labs(LONG_MAX));
820}
821
822TEST(stdlib, llabs) {
823 ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX));
824 ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX));
825}
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700826
827TEST(stdlib, getloadavg) {
828 double load[3];
829
830 // The second argument should have been size_t.
831 ASSERT_EQ(-1, getloadavg(load, -1));
832 ASSERT_EQ(-1, getloadavg(load, INT_MIN));
833
834 // Zero is a no-op.
835 ASSERT_EQ(0, getloadavg(load, 0));
836
837 // The Linux kernel doesn't support more than 3 (but you can ask for fewer).
838 ASSERT_EQ(1, getloadavg(load, 1));
839 ASSERT_EQ(2, getloadavg(load, 2));
840 ASSERT_EQ(3, getloadavg(load, 3));
841 ASSERT_EQ(3, getloadavg(load, 4));
842 ASSERT_EQ(3, getloadavg(load, INT_MAX));
843
844 // Read /proc/loadavg and check that it's "close enough".
845 load[0] = nan("");
846 double expected[3];
847 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/loadavg", "re"), fclose};
848 ASSERT_EQ(3, fscanf(fp.get(), "%lf %lf %lf", &expected[0], &expected[1], &expected[2]));
849 ASSERT_EQ(3, getloadavg(load, 3));
850
851 // It's probably too flaky if we look at the 1-minute average, so we just place a NaN there
852 // and check that it's overwritten with _something_.
853 ASSERT_FALSE(isnan(load[0]));
854 // For the others, rounding to an integer is pessimistic but at least gives us a sanity check.
855 ASSERT_DOUBLE_EQ(rint(expected[1]), rint(load[1]));
856 ASSERT_DOUBLE_EQ(rint(expected[2]), rint(load[2]));
857}