2012-10-01 22:11:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-02-04 22:18:00 +01:00
|
|
|
#include <errno.h>
|
2016-08-27 01:17:17 +02:00
|
|
|
#include <fcntl.h>
|
2013-03-02 01:59:46 +01:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <limits.h>
|
2016-08-27 01:17:17 +02:00
|
|
|
#include <math.h>
|
2013-11-16 02:40:18 +01:00
|
|
|
#include <pthread.h>
|
2013-02-04 22:18:00 +01:00
|
|
|
#include <stdint.h>
|
2012-10-01 22:11:03 +02:00
|
|
|
#include <stdlib.h>
|
2021-08-17 00:51:59 +02:00
|
|
|
#include <sys/cdefs.h>
|
2014-03-12 21:50:38 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2018-11-13 16:35:21 +01:00
|
|
|
#include <unistd.h>
|
2012-10-01 22:11:03 +02:00
|
|
|
|
2017-12-19 19:27:27 +01:00
|
|
|
#include <limits>
|
2017-12-20 01:11:37 +01:00
|
|
|
#include <string>
|
2017-12-19 19:27:27 +01:00
|
|
|
|
2019-11-01 16:07:25 +01:00
|
|
|
#include <android-base/file.h>
|
2018-11-13 16:35:21 +01:00
|
|
|
#include <android-base/macros.h>
|
2021-04-10 02:13:09 +02:00
|
|
|
#include <android-base/silent_death_test.h>
|
2022-04-16 00:54:47 +02:00
|
|
|
#include <android-base/test_utils.h>
|
2018-11-13 16:35:21 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "math_data_test.h"
|
2018-11-07 01:48:27 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
2019-11-01 16:07:25 +01:00
|
|
|
using namespace std::string_literals;
|
|
|
|
|
2018-11-13 16:35:21 +01:00
|
|
|
template <typename T = int (*)(char*)>
|
|
|
|
class GenericTemporaryFile {
|
|
|
|
public:
|
|
|
|
explicit GenericTemporaryFile(T mk_fn = mkstemp) : mk_fn_(mk_fn) {
|
|
|
|
// Since we might be running on the host or the target, and if we're
|
|
|
|
// running on the host we might be running under bionic or glibc,
|
|
|
|
// let's just try both possible temporary directories and take the
|
|
|
|
// first one that works.
|
|
|
|
init("/data/local/tmp");
|
|
|
|
if (fd == -1) {
|
|
|
|
init("/tmp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~GenericTemporaryFile() {
|
|
|
|
close(fd);
|
|
|
|
unlink(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
char path[1024];
|
|
|
|
|
|
|
|
private:
|
|
|
|
T mk_fn_;
|
|
|
|
|
|
|
|
void init(const char* tmp_dir) {
|
|
|
|
snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir);
|
|
|
|
fd = mk_fn_(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(GenericTemporaryFile);
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef GenericTemporaryFile<> MyTemporaryFile;
|
|
|
|
|
2014-11-06 21:40:08 +01:00
|
|
|
// The random number generator tests all set the seed, get four values, reset the seed and check
|
|
|
|
// that they get the first two values repeated, and then reset the seed and check two more values
|
|
|
|
// to rule out the possibility that we're just going round a cycle of four values.
|
|
|
|
// TODO: factor this out.
|
|
|
|
|
2012-10-01 22:11:03 +02:00
|
|
|
TEST(stdlib, drand48) {
|
|
|
|
srand48(0x01020304);
|
|
|
|
EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
|
|
|
|
EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
|
|
|
|
EXPECT_DOUBLE_EQ(0.42015087072844537, drand48());
|
|
|
|
EXPECT_DOUBLE_EQ(0.061637783047395089, drand48());
|
2014-11-06 21:40:08 +01:00
|
|
|
srand48(0x01020304);
|
|
|
|
EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
|
|
|
|
EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
|
|
|
|
srand48(0x01020304);
|
|
|
|
EXPECT_DOUBLE_EQ(0.65619299195623526, drand48());
|
|
|
|
EXPECT_DOUBLE_EQ(0.18522597229772941, drand48());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, erand48) {
|
|
|
|
const unsigned short seed[3] = { 0x330e, 0xabcd, 0x1234 };
|
|
|
|
unsigned short xsubi[3];
|
|
|
|
memcpy(xsubi, seed, sizeof(seed));
|
|
|
|
EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
|
|
|
|
EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
|
|
|
|
EXPECT_DOUBLE_EQ(0.35333609724524351, erand48(xsubi));
|
|
|
|
EXPECT_DOUBLE_EQ(0.44658343479654405, erand48(xsubi));
|
|
|
|
memcpy(xsubi, seed, sizeof(seed));
|
|
|
|
EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
|
|
|
|
EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
|
|
|
|
memcpy(xsubi, seed, sizeof(seed));
|
|
|
|
EXPECT_DOUBLE_EQ(0.39646477376027534, erand48(xsubi));
|
|
|
|
EXPECT_DOUBLE_EQ(0.84048536941142515, erand48(xsubi));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, lcong48) {
|
|
|
|
unsigned short p[7] = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e };
|
|
|
|
lcong48(p);
|
|
|
|
EXPECT_EQ(1531389981, lrand48());
|
|
|
|
EXPECT_EQ(1598801533, lrand48());
|
|
|
|
EXPECT_EQ(2080534853, lrand48());
|
|
|
|
EXPECT_EQ(1102488897, lrand48());
|
|
|
|
lcong48(p);
|
|
|
|
EXPECT_EQ(1531389981, lrand48());
|
|
|
|
EXPECT_EQ(1598801533, lrand48());
|
|
|
|
lcong48(p);
|
|
|
|
EXPECT_EQ(1531389981, lrand48());
|
|
|
|
EXPECT_EQ(1598801533, lrand48());
|
2012-10-01 22:11:03 +02:00
|
|
|
}
|
|
|
|
|
2014-06-12 20:48:04 +02:00
|
|
|
TEST(stdlib, lrand48) {
|
2012-10-01 22:11:03 +02:00
|
|
|
srand48(0x01020304);
|
|
|
|
EXPECT_EQ(1409163720, lrand48());
|
|
|
|
EXPECT_EQ(397769746, lrand48());
|
|
|
|
EXPECT_EQ(902267124, lrand48());
|
|
|
|
EXPECT_EQ(132366131, lrand48());
|
2014-11-06 21:40:08 +01:00
|
|
|
srand48(0x01020304);
|
|
|
|
EXPECT_EQ(1409163720, lrand48());
|
|
|
|
EXPECT_EQ(397769746, lrand48());
|
|
|
|
srand48(0x01020304);
|
|
|
|
EXPECT_EQ(1409163720, lrand48());
|
|
|
|
EXPECT_EQ(397769746, lrand48());
|
2014-06-12 20:48:04 +02:00
|
|
|
}
|
2012-10-01 22:11:03 +02:00
|
|
|
|
2014-06-12 20:48:04 +02:00
|
|
|
TEST(stdlib, random) {
|
2012-10-01 22:11:03 +02:00
|
|
|
srandom(0x01020304);
|
2014-06-12 20:48:04 +02:00
|
|
|
EXPECT_EQ(55436735, random());
|
|
|
|
EXPECT_EQ(1399865117, random());
|
|
|
|
EXPECT_EQ(2032643283, random());
|
|
|
|
EXPECT_EQ(571329216, random());
|
2014-11-06 21:40:08 +01:00
|
|
|
srandom(0x01020304);
|
|
|
|
EXPECT_EQ(55436735, random());
|
|
|
|
EXPECT_EQ(1399865117, random());
|
|
|
|
srandom(0x01020304);
|
|
|
|
EXPECT_EQ(55436735, random());
|
|
|
|
EXPECT_EQ(1399865117, random());
|
2014-06-12 20:48:04 +02:00
|
|
|
}
|
2012-10-01 22:11:03 +02:00
|
|
|
|
2014-06-12 20:48:04 +02:00
|
|
|
TEST(stdlib, rand) {
|
2012-10-01 22:11:03 +02:00
|
|
|
srand(0x01020304);
|
2014-06-12 20:48:04 +02:00
|
|
|
EXPECT_EQ(55436735, rand());
|
|
|
|
EXPECT_EQ(1399865117, rand());
|
|
|
|
EXPECT_EQ(2032643283, rand());
|
|
|
|
EXPECT_EQ(571329216, rand());
|
2014-11-06 21:40:08 +01:00
|
|
|
srand(0x01020304);
|
|
|
|
EXPECT_EQ(55436735, rand());
|
|
|
|
EXPECT_EQ(1399865117, rand());
|
|
|
|
srand(0x01020304);
|
|
|
|
EXPECT_EQ(55436735, rand());
|
|
|
|
EXPECT_EQ(1399865117, rand());
|
2012-10-01 22:11:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, mrand48) {
|
|
|
|
srand48(0x01020304);
|
|
|
|
EXPECT_EQ(-1476639856, mrand48());
|
|
|
|
EXPECT_EQ(795539493, mrand48());
|
|
|
|
EXPECT_EQ(1804534249, mrand48());
|
|
|
|
EXPECT_EQ(264732262, mrand48());
|
2014-11-06 21:40:08 +01:00
|
|
|
srand48(0x01020304);
|
|
|
|
EXPECT_EQ(-1476639856, mrand48());
|
|
|
|
EXPECT_EQ(795539493, mrand48());
|
|
|
|
srand48(0x01020304);
|
|
|
|
EXPECT_EQ(-1476639856, mrand48());
|
|
|
|
EXPECT_EQ(795539493, mrand48());
|
2012-10-01 22:11:03 +02:00
|
|
|
}
|
2013-02-04 22:18:00 +01:00
|
|
|
|
2015-02-27 13:01:59 +01:00
|
|
|
TEST(stdlib, jrand48_distribution) {
|
|
|
|
const int iterations = 4096;
|
|
|
|
const int pivot_low = 1536;
|
|
|
|
const int pivot_high = 2560;
|
|
|
|
|
|
|
|
unsigned short xsubi[3];
|
|
|
|
int bits[32] = {};
|
|
|
|
|
|
|
|
for (int iter = 0; iter < iterations; ++iter) {
|
|
|
|
long rand_val = jrand48(xsubi);
|
|
|
|
for (int bit = 0; bit < 32; ++bit) {
|
|
|
|
bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that bit probability is uniform
|
|
|
|
for (int bit = 0; bit < 32; ++bit) {
|
|
|
|
EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, mrand48_distribution) {
|
|
|
|
const int iterations = 4096;
|
|
|
|
const int pivot_low = 1536;
|
|
|
|
const int pivot_high = 2560;
|
|
|
|
|
|
|
|
int bits[32] = {};
|
|
|
|
|
|
|
|
for (int iter = 0; iter < iterations; ++iter) {
|
|
|
|
long rand_val = mrand48();
|
|
|
|
for (int bit = 0; bit < 32; ++bit) {
|
|
|
|
bits[bit] += (static_cast<unsigned long>(rand_val) >> bit) & 0x01;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that bit probability is uniform
|
|
|
|
for (int bit = 0; bit < 32; ++bit) {
|
|
|
|
EXPECT_TRUE((pivot_low <= bits[bit]) && (bits[bit] <= pivot_high));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 22:30:44 +02:00
|
|
|
TEST(stdlib, posix_memalign_sweep) {
|
2018-11-07 01:48:27 +01:00
|
|
|
SKIP_WITH_HWASAN;
|
2017-06-15 22:30:44 +02:00
|
|
|
void* ptr;
|
|
|
|
|
|
|
|
// These should all fail.
|
|
|
|
for (size_t align = 0; align < sizeof(long); align++) {
|
|
|
|
ASSERT_EQ(EINVAL, posix_memalign(&ptr, align, 256))
|
|
|
|
<< "Unexpected value at align " << align;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify powers of 2 up to 2048 allocate, and verify that all other
|
|
|
|
// alignment values between the powers of 2 fail.
|
|
|
|
size_t last_align = sizeof(long);
|
|
|
|
for (size_t align = sizeof(long); align <= 2048; align <<= 1) {
|
|
|
|
// Try all of the non power of 2 values from the last until this value.
|
|
|
|
for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
|
|
|
|
ASSERT_EQ(EINVAL, posix_memalign(&ptr, fail_align, 256))
|
|
|
|
<< "Unexpected success at align " << fail_align;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(0, posix_memalign(&ptr, align, 256))
|
|
|
|
<< "Unexpected failure at align " << align;
|
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
|
|
|
|
<< "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
|
|
|
|
free(ptr);
|
|
|
|
last_align = align;
|
|
|
|
}
|
|
|
|
}
|
2013-02-04 22:18:00 +01:00
|
|
|
|
2017-06-15 22:30:44 +02:00
|
|
|
TEST(stdlib, posix_memalign_various_sizes) {
|
|
|
|
std::vector<size_t> sizes{1, 4, 8, 256, 1024, 65000, 128000, 256000, 1000000};
|
|
|
|
for (auto size : sizes) {
|
|
|
|
void* ptr;
|
|
|
|
ASSERT_EQ(0, posix_memalign(&ptr, 16, 1))
|
|
|
|
<< "posix_memalign failed at size " << size;
|
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & 0xf)
|
|
|
|
<< "Pointer not aligned at size " << size << " ptr " << ptr;
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
}
|
2013-02-04 22:18:00 +01:00
|
|
|
|
2017-06-15 22:30:44 +02:00
|
|
|
TEST(stdlib, posix_memalign_overflow) {
|
2018-11-07 01:48:27 +01:00
|
|
|
SKIP_WITH_HWASAN;
|
2017-06-15 22:30:44 +02:00
|
|
|
void* ptr;
|
|
|
|
ASSERT_NE(0, posix_memalign(&ptr, 16, SIZE_MAX));
|
2013-02-04 22:18:00 +01:00
|
|
|
}
|
2013-03-02 01:59:46 +01:00
|
|
|
|
2018-02-06 03:14:55 +01:00
|
|
|
TEST(stdlib, aligned_alloc_sweep) {
|
2018-11-07 01:48:27 +01:00
|
|
|
SKIP_WITH_HWASAN;
|
2018-02-06 03:14:55 +01:00
|
|
|
// Verify powers of 2 up to 2048 allocate, and verify that all other
|
|
|
|
// alignment values between the powers of 2 fail.
|
|
|
|
size_t last_align = 1;
|
|
|
|
for (size_t align = 1; align <= 2048; align <<= 1) {
|
|
|
|
// Try all of the non power of 2 values from the last until this value.
|
|
|
|
for (size_t fail_align = last_align + 1; fail_align < align; fail_align++) {
|
2019-03-02 01:40:59 +01:00
|
|
|
ASSERT_TRUE(aligned_alloc(fail_align, fail_align) == nullptr)
|
2018-02-06 03:14:55 +01:00
|
|
|
<< "Unexpected success at align " << fail_align;
|
|
|
|
ASSERT_EQ(EINVAL, errno) << "Unexpected errno at align " << fail_align;
|
|
|
|
}
|
2019-03-02 01:40:59 +01:00
|
|
|
void* ptr = aligned_alloc(align, 2 * align);
|
2018-02-06 03:14:55 +01:00
|
|
|
ASSERT_TRUE(ptr != nullptr) << "Unexpected failure at align " << align;
|
|
|
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
|
|
|
|
<< "Did not return a valid aligned ptr " << ptr << " expected alignment " << align;
|
|
|
|
free(ptr);
|
|
|
|
last_align = align;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, aligned_alloc_overflow) {
|
2018-11-07 01:48:27 +01:00
|
|
|
SKIP_WITH_HWASAN;
|
2018-02-06 03:14:55 +01:00
|
|
|
ASSERT_TRUE(aligned_alloc(16, SIZE_MAX) == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, aligned_alloc_size_not_multiple_of_alignment) {
|
2018-11-07 01:48:27 +01:00
|
|
|
SKIP_WITH_HWASAN;
|
2019-03-02 01:40:59 +01:00
|
|
|
|
|
|
|
ASSERT_TRUE(aligned_alloc(2048, 1) == nullptr);
|
|
|
|
ASSERT_TRUE(aligned_alloc(4, 3) == nullptr);
|
|
|
|
ASSERT_TRUE(aligned_alloc(4, 7) == nullptr);
|
|
|
|
ASSERT_TRUE(aligned_alloc(16, 8) == nullptr);
|
2018-02-06 03:14:55 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 01:59:46 +01:00
|
|
|
TEST(stdlib, realpath__NULL_filename) {
|
|
|
|
errno = 0;
|
2017-08-14 23:48:55 +02:00
|
|
|
// Work around the compile-time error generated by FORTIFY here.
|
2018-08-03 02:31:13 +02:00
|
|
|
const char* path = nullptr;
|
|
|
|
char* p = realpath(path, nullptr);
|
|
|
|
ASSERT_TRUE(p == nullptr);
|
2013-03-02 01:59:46 +01:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, realpath__empty_filename) {
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
char* p = realpath("", nullptr);
|
|
|
|
ASSERT_TRUE(p == nullptr);
|
2013-03-02 01:59:46 +01:00
|
|
|
ASSERT_EQ(ENOENT, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, realpath__ENOENT) {
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", nullptr);
|
|
|
|
ASSERT_TRUE(p == nullptr);
|
2013-03-02 01:59:46 +01:00
|
|
|
ASSERT_EQ(ENOENT, errno);
|
|
|
|
}
|
|
|
|
|
2019-11-01 16:07:25 +01:00
|
|
|
TEST(stdlib, realpath__ELOOP) {
|
|
|
|
TemporaryDir td;
|
|
|
|
std::string link = std::string(td.path) + "/loop";
|
|
|
|
ASSERT_EQ(0, symlink(link.c_str(), link.c_str()));
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
char* p = realpath(link.c_str(), nullptr);
|
|
|
|
ASSERT_TRUE(p == nullptr);
|
|
|
|
ASSERT_EQ(ELOOP, errno);
|
|
|
|
}
|
|
|
|
|
2014-10-01 01:15:42 +02:00
|
|
|
TEST(stdlib, realpath__component_after_non_directory) {
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
char* p = realpath("/dev/null/.", nullptr);
|
|
|
|
ASSERT_TRUE(p == nullptr);
|
2014-10-01 01:15:42 +02:00
|
|
|
ASSERT_EQ(ENOTDIR, errno);
|
|
|
|
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
p = realpath("/dev/null/..", nullptr);
|
|
|
|
ASSERT_TRUE(p == nullptr);
|
2014-10-01 01:15:42 +02:00
|
|
|
ASSERT_EQ(ENOTDIR, errno);
|
|
|
|
}
|
|
|
|
|
2013-03-02 01:59:46 +01:00
|
|
|
TEST(stdlib, realpath) {
|
|
|
|
// Get the name of this executable.
|
|
|
|
char executable_path[PATH_MAX];
|
|
|
|
int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
|
|
|
|
ASSERT_NE(rc, -1);
|
|
|
|
executable_path[rc] = '\0';
|
|
|
|
|
|
|
|
char buf[PATH_MAX + 1];
|
|
|
|
char* p = realpath("/proc/self/exe", buf);
|
|
|
|
ASSERT_STREQ(executable_path, p);
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
p = realpath("/proc/self/exe", nullptr);
|
2013-03-02 01:59:46 +01:00
|
|
|
ASSERT_STREQ(executable_path, p);
|
|
|
|
free(p);
|
|
|
|
}
|
2013-04-12 03:08:34 +02:00
|
|
|
|
2019-11-01 16:07:25 +01:00
|
|
|
TEST(stdlib, realpath__dot) {
|
|
|
|
char* p = realpath("/proc/./version", nullptr);
|
|
|
|
ASSERT_STREQ("/proc/version", p);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, realpath__dot_dot) {
|
|
|
|
char* p = realpath("/dev/../proc/version", nullptr);
|
|
|
|
ASSERT_STREQ("/proc/version", p);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, realpath__deleted) {
|
|
|
|
TemporaryDir td;
|
|
|
|
|
|
|
|
// Create a file "A".
|
|
|
|
std::string A_path = td.path + "/A"s;
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("test\n", A_path));
|
|
|
|
|
|
|
|
// Get an O_PATH fd for it.
|
|
|
|
android::base::unique_fd fd(open(A_path.c_str(), O_PATH));
|
|
|
|
ASSERT_NE(fd, -1);
|
|
|
|
|
|
|
|
// Create a file "A (deleted)".
|
|
|
|
android::base::unique_fd fd2(open((td.path + "/A (deleted)"s).c_str(),
|
|
|
|
O_CREAT | O_TRUNC | O_WRONLY, 0644));
|
|
|
|
ASSERT_NE(fd2, -1);
|
|
|
|
|
|
|
|
// Delete "A".
|
|
|
|
ASSERT_EQ(0, unlink(A_path.c_str()));
|
|
|
|
|
|
|
|
// Now realpath() on the O_PATH fd, and check we *don't* get "A (deleted)".
|
|
|
|
std::string path = android::base::StringPrintf("/proc/%d/fd/%d", static_cast<int>(getpid()),
|
|
|
|
fd.get());
|
|
|
|
errno = 0;
|
|
|
|
char* result = realpath(path.c_str(), nullptr);
|
|
|
|
ASSERT_EQ(nullptr, result) << result;
|
|
|
|
ASSERT_EQ(ENOENT, errno);
|
|
|
|
free(result);
|
|
|
|
}
|
|
|
|
|
2013-04-12 03:08:34 +02:00
|
|
|
TEST(stdlib, qsort) {
|
|
|
|
struct s {
|
|
|
|
char name[16];
|
|
|
|
static int comparator(const void* lhs, const void* rhs) {
|
|
|
|
return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
s entries[3];
|
|
|
|
strcpy(entries[0].name, "charlie");
|
|
|
|
strcpy(entries[1].name, "bravo");
|
|
|
|
strcpy(entries[2].name, "alpha");
|
|
|
|
|
|
|
|
qsort(entries, 3, sizeof(s), s::comparator);
|
|
|
|
ASSERT_STREQ("alpha", entries[0].name);
|
|
|
|
ASSERT_STREQ("bravo", entries[1].name);
|
|
|
|
ASSERT_STREQ("charlie", entries[2].name);
|
|
|
|
|
|
|
|
qsort(entries, 3, sizeof(s), s::comparator);
|
|
|
|
ASSERT_STREQ("alpha", entries[0].name);
|
|
|
|
ASSERT_STREQ("bravo", entries[1].name);
|
|
|
|
ASSERT_STREQ("charlie", entries[2].name);
|
|
|
|
}
|
2013-11-16 02:40:18 +01:00
|
|
|
|
|
|
|
static void* TestBug57421_child(void* arg) {
|
|
|
|
pthread_t main_thread = reinterpret_cast<pthread_t>(arg);
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_join(main_thread, nullptr);
|
2013-11-16 02:40:18 +01:00
|
|
|
char* value = getenv("ENVIRONMENT_VARIABLE");
|
2018-08-03 02:31:13 +02:00
|
|
|
if (value == nullptr) {
|
2013-11-16 02:40:18 +01:00
|
|
|
setenv("ENVIRONMENT_VARIABLE", "value", 1);
|
|
|
|
}
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2013-11-16 02:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TestBug57421_main() {
|
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug57421_child, reinterpret_cast<void*>(pthread_self())));
|
|
|
|
pthread_exit(nullptr);
|
2013-11-16 02:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
|
|
|
|
// run this test (which exits normally) in its own process.
|
2014-11-06 03:01:01 +01:00
|
|
|
|
2021-04-10 02:13:09 +02:00
|
|
|
using stdlib_DeathTest = SilentDeathTest;
|
2014-11-06 03:01:01 +01:00
|
|
|
|
|
|
|
TEST_F(stdlib_DeathTest, getenv_after_main_thread_exits) {
|
2013-11-16 02:40:18 +01:00
|
|
|
// https://code.google.com/p/android/issues/detail?id=57421
|
|
|
|
ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), "");
|
|
|
|
}
|
2014-02-21 16:11:03 +01:00
|
|
|
|
2021-07-28 20:18:11 +02:00
|
|
|
TEST(stdlib, mkostemp64_smoke) {
|
2018-11-13 16:35:21 +01:00
|
|
|
MyTemporaryFile tf([](char* path) { return mkostemp64(path, O_CLOEXEC); });
|
2021-02-05 01:58:13 +01:00
|
|
|
ASSERT_TRUE(CloseOnExec(tf.fd));
|
2014-09-24 02:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, mkostemp) {
|
2018-11-13 16:35:21 +01:00
|
|
|
MyTemporaryFile tf([](char* path) { return mkostemp(path, O_CLOEXEC); });
|
2021-02-05 01:58:13 +01:00
|
|
|
ASSERT_TRUE(CloseOnExec(tf.fd));
|
2014-02-21 16:11:03 +01:00
|
|
|
}
|
|
|
|
|
2021-07-28 20:18:11 +02:00
|
|
|
TEST(stdlib, mkstemp64_smoke) {
|
2018-11-13 16:35:21 +01:00
|
|
|
MyTemporaryFile tf(mkstemp64);
|
2014-02-21 16:11:03 +01:00
|
|
|
struct stat64 sb;
|
|
|
|
ASSERT_EQ(0, fstat64(tf.fd, &sb));
|
|
|
|
ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE);
|
|
|
|
}
|
2014-03-11 20:54:44 +01:00
|
|
|
|
2014-09-24 02:34:29 +02:00
|
|
|
TEST(stdlib, mkstemp) {
|
2018-11-13 16:35:21 +01:00
|
|
|
MyTemporaryFile tf(mkstemp);
|
2014-09-24 02:34:29 +02:00
|
|
|
struct stat sb;
|
|
|
|
ASSERT_EQ(0, fstat(tf.fd, &sb));
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:54:44 +01:00
|
|
|
TEST(stdlib, system) {
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = system("exit 0");
|
|
|
|
ASSERT_TRUE(WIFEXITED(status));
|
|
|
|
ASSERT_EQ(0, WEXITSTATUS(status));
|
|
|
|
|
|
|
|
status = system("exit 1");
|
|
|
|
ASSERT_TRUE(WIFEXITED(status));
|
|
|
|
ASSERT_EQ(1, WEXITSTATUS(status));
|
|
|
|
}
|
2014-03-13 00:12:57 +01:00
|
|
|
|
2021-03-08 23:07:01 +01:00
|
|
|
TEST(stdlib, system_NULL) {
|
|
|
|
// "The system() function shall always return non-zero when command is NULL."
|
|
|
|
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/system.html
|
2022-11-28 22:05:55 +01:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wnonnull"
|
2021-03-08 23:07:01 +01:00
|
|
|
ASSERT_NE(0, system(nullptr));
|
2022-11-28 22:05:55 +01:00
|
|
|
#pragma clang diagnostic pop
|
2021-03-08 23:07:01 +01:00
|
|
|
}
|
|
|
|
|
2021-11-05 01:18:58 +01:00
|
|
|
// https://austingroupbugs.net/view.php?id=1440
|
|
|
|
TEST(stdlib, system_minus) {
|
|
|
|
// Create a script with a name that starts with a '-'.
|
|
|
|
TemporaryDir td;
|
|
|
|
std::string script = std::string(td.path) + "/-minus";
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("#!" BIN_DIR "sh\nexit 66\n", script));
|
|
|
|
|
|
|
|
// Set $PATH so we can find it.
|
|
|
|
setenv("PATH", td.path, 1);
|
|
|
|
// Make it executable so we can run it.
|
|
|
|
ASSERT_EQ(0, chmod(script.c_str(), 0555));
|
|
|
|
|
|
|
|
int status = system("-minus");
|
|
|
|
EXPECT_TRUE(WIFEXITED(status));
|
|
|
|
EXPECT_EQ(66, WEXITSTATUS(status));
|
|
|
|
|
|
|
|
// While we're here and have all the setup, let's test popen(3) too...
|
|
|
|
FILE* fp = popen("-minus", "r");
|
|
|
|
ASSERT_TRUE(fp != nullptr);
|
|
|
|
status = pclose(fp);
|
|
|
|
EXPECT_TRUE(WIFEXITED(status));
|
|
|
|
EXPECT_EQ(66, WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
|
2014-03-13 00:12:57 +01:00
|
|
|
TEST(stdlib, atof) {
|
2014-03-18 00:40:26 +01:00
|
|
|
ASSERT_DOUBLE_EQ(1.23, atof("1.23"));
|
2014-03-13 00:12:57 +01:00
|
|
|
}
|
|
|
|
|
2016-08-27 01:17:17 +02:00
|
|
|
template <typename T>
|
|
|
|
static void CheckStrToFloat(T fn(const char* s, char** end)) {
|
|
|
|
FpUlpEq<0, T> pred;
|
|
|
|
|
|
|
|
EXPECT_PRED_FORMAT2(pred, 9.0, fn("9.0", nullptr));
|
|
|
|
EXPECT_PRED_FORMAT2(pred, 9.0, fn("0.9e1", nullptr));
|
|
|
|
EXPECT_PRED_FORMAT2(pred, 9.0, fn("0x1.2p3", nullptr));
|
|
|
|
|
2016-12-02 21:02:03 +01:00
|
|
|
const char* s = " \t\v\f\r\n9.0";
|
|
|
|
char* p;
|
|
|
|
EXPECT_PRED_FORMAT2(pred, 9.0, fn(s, &p));
|
|
|
|
EXPECT_EQ(s + strlen(s), p);
|
|
|
|
|
2016-08-27 01:17:17 +02:00
|
|
|
EXPECT_TRUE(isnan(fn("+nan", nullptr)));
|
|
|
|
EXPECT_TRUE(isnan(fn("nan", nullptr)));
|
|
|
|
EXPECT_TRUE(isnan(fn("-nan", nullptr)));
|
|
|
|
|
|
|
|
EXPECT_TRUE(isnan(fn("+nan(0xff)", nullptr)));
|
|
|
|
EXPECT_TRUE(isnan(fn("nan(0xff)", nullptr)));
|
|
|
|
EXPECT_TRUE(isnan(fn("-nan(0xff)", nullptr)));
|
|
|
|
|
|
|
|
EXPECT_TRUE(isnan(fn("+nanny", &p)));
|
|
|
|
EXPECT_STREQ("ny", p);
|
|
|
|
EXPECT_TRUE(isnan(fn("nanny", &p)));
|
|
|
|
EXPECT_STREQ("ny", p);
|
|
|
|
EXPECT_TRUE(isnan(fn("-nanny", &p)));
|
|
|
|
EXPECT_STREQ("ny", p);
|
|
|
|
|
|
|
|
EXPECT_EQ(0, fn("muppet", &p));
|
|
|
|
EXPECT_STREQ("muppet", p);
|
|
|
|
EXPECT_EQ(0, fn(" muppet", &p));
|
|
|
|
EXPECT_STREQ(" muppet", p);
|
|
|
|
|
|
|
|
EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+inf", nullptr));
|
|
|
|
EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("inf", nullptr));
|
|
|
|
EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-inf", nullptr));
|
|
|
|
|
|
|
|
EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinity", nullptr));
|
|
|
|
EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinity", nullptr));
|
|
|
|
EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinity", nullptr));
|
|
|
|
|
|
|
|
EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("+infinitude", &p));
|
|
|
|
EXPECT_STREQ("initude", p);
|
|
|
|
EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("infinitude", &p));
|
|
|
|
EXPECT_STREQ("initude", p);
|
|
|
|
EXPECT_EQ(-std::numeric_limits<T>::infinity(), fn("-infinitude", &p));
|
|
|
|
EXPECT_STREQ("initude", p);
|
|
|
|
|
|
|
|
// Check case-insensitivity.
|
|
|
|
EXPECT_EQ(std::numeric_limits<T>::infinity(), fn("InFiNiTy", nullptr));
|
|
|
|
EXPECT_TRUE(isnan(fn("NaN", nullptr)));
|
|
|
|
}
|
|
|
|
|
2014-03-13 00:12:57 +01:00
|
|
|
TEST(stdlib, strtod) {
|
2016-08-27 01:17:17 +02:00
|
|
|
CheckStrToFloat(strtod);
|
2014-03-13 00:12:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, strtof) {
|
2016-08-27 01:17:17 +02:00
|
|
|
CheckStrToFloat(strtof);
|
2014-03-13 00:12:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, strtold) {
|
2016-08-27 01:17:17 +02:00
|
|
|
CheckStrToFloat(strtold);
|
2014-03-13 00:12:57 +01:00
|
|
|
}
|
2014-04-09 02:14:01 +02:00
|
|
|
|
2014-10-29 01:54:23 +01:00
|
|
|
TEST(stdlib, strtof_2206701) {
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0.0f, strtof("7.0064923216240853546186479164495e-46", nullptr));
|
|
|
|
ASSERT_EQ(1.4e-45f, strtof("7.0064923216240853546186479164496e-46", nullptr));
|
2014-10-29 01:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, strtod_largest_subnormal) {
|
|
|
|
// This value has been known to cause javac and java to infinite loop.
|
|
|
|
// http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-308", nullptr));
|
|
|
|
ASSERT_EQ(2.2250738585072014e-308, strtod("0.00022250738585072012e-304", nullptr));
|
|
|
|
ASSERT_EQ(2.2250738585072014e-308, strtod("00000002.2250738585072012e-308", nullptr));
|
|
|
|
ASSERT_EQ(2.2250738585072014e-308, strtod("2.225073858507201200000e-308", nullptr));
|
|
|
|
ASSERT_EQ(2.2250738585072014e-308, strtod("2.2250738585072012e-00308", nullptr));
|
|
|
|
ASSERT_EQ(2.2250738585072014e-308, strtod("2.22507385850720129978001e-308", nullptr));
|
|
|
|
ASSERT_EQ(-2.2250738585072014e-308, strtod("-2.2250738585072012e-308", nullptr));
|
2014-10-29 01:54:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:49:06 +02:00
|
|
|
TEST(stdlib, quick_exit) {
|
|
|
|
pid_t pid = fork();
|
|
|
|
ASSERT_NE(-1, pid) << strerror(errno);
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
quick_exit(99);
|
|
|
|
}
|
|
|
|
|
2016-01-26 22:04:57 +01:00
|
|
|
AssertChildExited(pid, 99);
|
2014-04-30 02:49:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int quick_exit_status = 0;
|
|
|
|
|
|
|
|
static void quick_exit_1(void) {
|
|
|
|
ASSERT_EQ(quick_exit_status, 0);
|
|
|
|
quick_exit_status = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void quick_exit_2(void) {
|
|
|
|
ASSERT_EQ(quick_exit_status, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void not_run(void) {
|
|
|
|
FAIL();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, at_quick_exit) {
|
|
|
|
pid_t pid = fork();
|
|
|
|
ASSERT_NE(-1, pid) << strerror(errno);
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
ASSERT_EQ(at_quick_exit(quick_exit_2), 0);
|
|
|
|
ASSERT_EQ(at_quick_exit(quick_exit_1), 0);
|
|
|
|
atexit(not_run);
|
|
|
|
quick_exit(99);
|
|
|
|
}
|
|
|
|
|
2016-01-26 22:04:57 +01:00
|
|
|
AssertChildExited(pid, 99);
|
2014-04-30 02:49:06 +02:00
|
|
|
}
|
|
|
|
|
2014-04-09 02:14:01 +02:00
|
|
|
TEST(unistd, _Exit) {
|
2016-01-26 22:04:57 +01:00
|
|
|
pid_t pid = fork();
|
2014-04-09 02:14:01 +02:00
|
|
|
ASSERT_NE(-1, pid) << strerror(errno);
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
_Exit(99);
|
|
|
|
}
|
|
|
|
|
2016-01-26 22:04:57 +01:00
|
|
|
AssertChildExited(pid, 99);
|
2014-04-09 02:14:01 +02:00
|
|
|
}
|
2014-07-26 02:24:00 +02:00
|
|
|
|
2021-08-17 00:51:59 +02:00
|
|
|
#if defined(ANDROID_HOST_MUSL)
|
2021-07-28 20:18:11 +02:00
|
|
|
// musl doesn't have getpt
|
|
|
|
int getpt() {
|
|
|
|
return posix_openpt(O_RDWR|O_NOCTTY);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-26 02:24:00 +02:00
|
|
|
TEST(stdlib, pty_smoke) {
|
|
|
|
// getpt returns a pty with O_RDWR|O_NOCTTY.
|
|
|
|
int fd = getpt();
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
|
|
|
|
// grantpt is a no-op.
|
|
|
|
ASSERT_EQ(0, grantpt(fd));
|
|
|
|
|
|
|
|
// ptsname_r should start "/dev/pts/".
|
|
|
|
char name_r[128];
|
|
|
|
ASSERT_EQ(0, ptsname_r(fd, name_r, sizeof(name_r)));
|
|
|
|
name_r[9] = 0;
|
|
|
|
ASSERT_STREQ("/dev/pts/", name_r);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, posix_openpt) {
|
|
|
|
int fd = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC);
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, ptsname_r_ENOTTY) {
|
|
|
|
errno = 0;
|
|
|
|
char buf[128];
|
|
|
|
ASSERT_EQ(ENOTTY, ptsname_r(STDOUT_FILENO, buf, sizeof(buf)));
|
|
|
|
ASSERT_EQ(ENOTTY, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, ptsname_r_EINVAL) {
|
|
|
|
int fd = getpt();
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
char* buf = nullptr;
|
2014-07-26 02:24:00 +02:00
|
|
|
ASSERT_EQ(EINVAL, ptsname_r(fd, buf, 128));
|
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, ptsname_r_ERANGE) {
|
|
|
|
int fd = getpt();
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
errno = 0;
|
|
|
|
char buf[1];
|
|
|
|
ASSERT_EQ(ERANGE, ptsname_r(fd, buf, sizeof(buf)));
|
|
|
|
ASSERT_EQ(ERANGE, errno);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2017-11-09 06:53:50 +01:00
|
|
|
TEST(stdlib, ttyname) {
|
|
|
|
int fd = getpt();
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
|
|
|
|
// ttyname returns "/dev/ptmx" for a pty.
|
|
|
|
ASSERT_STREQ("/dev/ptmx", ttyname(fd));
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2014-07-26 02:24:00 +02:00
|
|
|
TEST(stdlib, ttyname_r) {
|
|
|
|
int fd = getpt();
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
|
|
|
|
// ttyname_r returns "/dev/ptmx" for a pty.
|
|
|
|
char name_r[128];
|
|
|
|
ASSERT_EQ(0, ttyname_r(fd, name_r, sizeof(name_r)));
|
|
|
|
ASSERT_STREQ("/dev/ptmx", name_r);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, ttyname_r_ENOTTY) {
|
|
|
|
int fd = open("/dev/null", O_WRONLY);
|
|
|
|
errno = 0;
|
|
|
|
char buf[128];
|
|
|
|
ASSERT_EQ(ENOTTY, ttyname_r(fd, buf, sizeof(buf)));
|
|
|
|
ASSERT_EQ(ENOTTY, errno);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, ttyname_r_EINVAL) {
|
|
|
|
int fd = getpt();
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
char* buf = nullptr;
|
2014-07-26 02:24:00 +02:00
|
|
|
ASSERT_EQ(EINVAL, ttyname_r(fd, buf, 128));
|
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, ttyname_r_ERANGE) {
|
|
|
|
int fd = getpt();
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
errno = 0;
|
|
|
|
char buf[1];
|
|
|
|
ASSERT_EQ(ERANGE, ttyname_r(fd, buf, sizeof(buf)));
|
|
|
|
ASSERT_EQ(ERANGE, errno);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, unlockpt_ENOTTY) {
|
|
|
|
int fd = open("/dev/null", O_WRONLY);
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(-1, unlockpt(fd));
|
|
|
|
ASSERT_EQ(ENOTTY, errno);
|
|
|
|
close(fd);
|
|
|
|
}
|
2014-09-23 23:53:10 +02:00
|
|
|
|
2016-04-05 02:34:04 +02:00
|
|
|
TEST(stdlib, getsubopt) {
|
|
|
|
char* const tokens[] = {
|
|
|
|
const_cast<char*>("a"),
|
|
|
|
const_cast<char*>("b"),
|
|
|
|
const_cast<char*>("foo"),
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
std::string input = "a,b,foo=bar,a,unknown";
|
|
|
|
char* subopts = &input[0];
|
|
|
|
char* value = nullptr;
|
|
|
|
|
|
|
|
ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
|
|
|
|
ASSERT_EQ(nullptr, value);
|
|
|
|
ASSERT_EQ(1, getsubopt(&subopts, tokens, &value));
|
|
|
|
ASSERT_EQ(nullptr, value);
|
|
|
|
ASSERT_EQ(2, getsubopt(&subopts, tokens, &value));
|
|
|
|
ASSERT_STREQ("bar", value);
|
|
|
|
ASSERT_EQ(0, getsubopt(&subopts, tokens, &value));
|
|
|
|
ASSERT_EQ(nullptr, value);
|
|
|
|
|
|
|
|
ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value));
|
|
|
|
}
|
2016-04-28 23:54:52 +02:00
|
|
|
|
|
|
|
TEST(stdlib, mblen) {
|
|
|
|
// "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings,
|
|
|
|
// respectively, do or do not have state-dependent encodings." We're always UTF-8.
|
|
|
|
EXPECT_EQ(0, mblen(nullptr, 1));
|
|
|
|
|
|
|
|
ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8"));
|
|
|
|
|
|
|
|
// 1-byte UTF-8.
|
|
|
|
EXPECT_EQ(1, mblen("abcdef", 6));
|
|
|
|
// 2-byte UTF-8.
|
|
|
|
EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6));
|
|
|
|
// 3-byte UTF-8.
|
|
|
|
EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6));
|
|
|
|
// 4-byte UTF-8.
|
|
|
|
EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6));
|
|
|
|
|
|
|
|
// Illegal over-long sequence.
|
|
|
|
ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6));
|
|
|
|
|
|
|
|
// "mblen() shall ... return 0 (if s points to the null byte)".
|
|
|
|
EXPECT_EQ(0, mblen("", 1));
|
|
|
|
}
|
2017-07-13 18:35:15 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void CheckStrToInt(T fn(const char* s, char** end, int base)) {
|
|
|
|
char* end_p;
|
|
|
|
|
|
|
|
// Negative base => invalid.
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(T(0), fn("123", &end_p, -1));
|
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
|
|
|
|
// Base 1 => invalid (base 0 means "please guess").
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(T(0), fn("123", &end_p, 1));
|
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
|
|
|
|
// Base > 36 => invalid.
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(T(0), fn("123", &end_p, 37));
|
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
|
2020-08-12 23:25:41 +02:00
|
|
|
// Both leading + or - are always allowed (even for the strtou* family).
|
|
|
|
ASSERT_EQ(T(-123), fn("-123", &end_p, 10));
|
|
|
|
ASSERT_EQ(T(123), fn("+123", &end_p, 10));
|
|
|
|
|
2022-08-06 00:51:05 +02:00
|
|
|
// If we see "0b" *not* followed by a binary digit, we shouldn't swallow the 'b'.
|
|
|
|
ASSERT_EQ(T(0), fn("0b", &end_p, 2));
|
|
|
|
ASSERT_EQ('b', *end_p);
|
|
|
|
|
|
|
|
// Binary (the "0b" prefix) is case-insensitive.
|
|
|
|
ASSERT_EQ(T(0b101), fn("0b101", &end_p, 0));
|
|
|
|
ASSERT_EQ(T(0b101), fn("0B101", &end_p, 0));
|
|
|
|
|
2017-07-13 18:35:15 +02:00
|
|
|
// If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'.
|
|
|
|
ASSERT_EQ(T(0), fn("0xy", &end_p, 16));
|
|
|
|
ASSERT_EQ('x', *end_p);
|
2017-12-19 19:27:27 +01:00
|
|
|
|
2022-08-06 00:51:05 +02:00
|
|
|
// Hexadecimal (both the "0x" prefix and the digits) is case-insensitive.
|
2020-08-12 23:25:41 +02:00
|
|
|
ASSERT_EQ(T(0xab), fn("0xab", &end_p, 0));
|
|
|
|
ASSERT_EQ(T(0xab), fn("0Xab", &end_p, 0));
|
|
|
|
ASSERT_EQ(T(0xab), fn("0xAB", &end_p, 0));
|
|
|
|
ASSERT_EQ(T(0xab), fn("0XAB", &end_p, 0));
|
|
|
|
ASSERT_EQ(T(0xab), fn("0xAb", &end_p, 0));
|
|
|
|
ASSERT_EQ(T(0xab), fn("0XAb", &end_p, 0));
|
|
|
|
|
|
|
|
// Octal lives! (Sadly.)
|
|
|
|
ASSERT_EQ(T(0666), fn("0666", &end_p, 0));
|
|
|
|
|
2017-12-19 19:27:27 +01:00
|
|
|
if (std::numeric_limits<T>::is_signed) {
|
|
|
|
// Minimum (such as -128).
|
2017-12-20 01:11:37 +01:00
|
|
|
std::string min{std::to_string(std::numeric_limits<T>::min())};
|
2017-12-21 02:37:11 +01:00
|
|
|
end_p = nullptr;
|
|
|
|
errno = 0;
|
2017-12-19 19:27:27 +01:00
|
|
|
ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
|
2017-12-21 02:37:11 +01:00
|
|
|
ASSERT_EQ(0, errno);
|
|
|
|
ASSERT_EQ('\0', *end_p);
|
2017-12-19 19:27:27 +01:00
|
|
|
// Too negative (such as -129).
|
|
|
|
min.back() = (min.back() + 1);
|
2017-12-21 02:37:11 +01:00
|
|
|
end_p = nullptr;
|
2017-12-19 19:27:27 +01:00
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0));
|
|
|
|
ASSERT_EQ(ERANGE, errno);
|
2017-12-21 02:37:11 +01:00
|
|
|
ASSERT_EQ('\0', *end_p);
|
2017-12-19 19:27:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Maximum (such as 127).
|
2017-12-20 01:11:37 +01:00
|
|
|
std::string max{std::to_string(std::numeric_limits<T>::max())};
|
2017-12-21 02:37:11 +01:00
|
|
|
end_p = nullptr;
|
|
|
|
errno = 0;
|
2017-12-19 19:27:27 +01:00
|
|
|
ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
|
2017-12-21 02:37:11 +01:00
|
|
|
ASSERT_EQ(0, errno);
|
|
|
|
ASSERT_EQ('\0', *end_p);
|
2017-12-19 19:27:27 +01:00
|
|
|
// Too positive (such as 128).
|
|
|
|
max.back() = (max.back() + 1);
|
2017-12-21 02:37:11 +01:00
|
|
|
end_p = nullptr;
|
2017-12-19 19:27:27 +01:00
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0));
|
|
|
|
ASSERT_EQ(ERANGE, errno);
|
2017-12-21 02:37:11 +01:00
|
|
|
ASSERT_EQ('\0', *end_p);
|
|
|
|
|
|
|
|
// In case of overflow, strto* leaves us pointing past the end of the number,
|
|
|
|
// not at the digit that overflowed.
|
|
|
|
end_p = nullptr;
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::max(),
|
|
|
|
fn("99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
|
|
|
|
ASSERT_EQ(ERANGE, errno);
|
|
|
|
ASSERT_STREQ("abc", end_p);
|
|
|
|
if (std::numeric_limits<T>::is_signed) {
|
|
|
|
end_p = nullptr;
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::min(),
|
|
|
|
fn("-99999999999999999999999999999999999999999999999999999abc", &end_p, 0));
|
|
|
|
ASSERT_EQ(ERANGE, errno);
|
|
|
|
ASSERT_STREQ("abc", end_p);
|
|
|
|
}
|
2017-07-13 18:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, strtol_smoke) {
|
|
|
|
CheckStrToInt(strtol);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, strtoll_smoke) {
|
|
|
|
CheckStrToInt(strtoll);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, strtoul_smoke) {
|
|
|
|
CheckStrToInt(strtoul);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, strtoull_smoke) {
|
|
|
|
CheckStrToInt(strtoull);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, strtoimax_smoke) {
|
|
|
|
CheckStrToInt(strtoimax);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, strtoumax_smoke) {
|
|
|
|
CheckStrToInt(strtoumax);
|
|
|
|
}
|
2017-09-08 01:42:13 +02:00
|
|
|
|
2020-08-12 23:25:41 +02:00
|
|
|
TEST(stdlib, atoi) {
|
|
|
|
// Implemented using strtol in bionic, so extensive testing unnecessary.
|
|
|
|
ASSERT_EQ(123, atoi("123four"));
|
|
|
|
ASSERT_EQ(0, atoi("hello"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, atol) {
|
|
|
|
// Implemented using strtol in bionic, so extensive testing unnecessary.
|
|
|
|
ASSERT_EQ(123L, atol("123four"));
|
|
|
|
ASSERT_EQ(0L, atol("hello"));
|
|
|
|
}
|
|
|
|
|
2017-09-08 01:42:13 +02:00
|
|
|
TEST(stdlib, abs) {
|
|
|
|
ASSERT_EQ(INT_MAX, abs(-INT_MAX));
|
|
|
|
ASSERT_EQ(INT_MAX, abs(INT_MAX));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, labs) {
|
|
|
|
ASSERT_EQ(LONG_MAX, labs(-LONG_MAX));
|
|
|
|
ASSERT_EQ(LONG_MAX, labs(LONG_MAX));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, llabs) {
|
|
|
|
ASSERT_EQ(LLONG_MAX, llabs(-LLONG_MAX));
|
|
|
|
ASSERT_EQ(LLONG_MAX, llabs(LLONG_MAX));
|
|
|
|
}
|
2018-10-23 20:23:00 +02:00
|
|
|
|
|
|
|
TEST(stdlib, getloadavg) {
|
|
|
|
double load[3];
|
|
|
|
|
|
|
|
// The second argument should have been size_t.
|
|
|
|
ASSERT_EQ(-1, getloadavg(load, -1));
|
|
|
|
ASSERT_EQ(-1, getloadavg(load, INT_MIN));
|
|
|
|
|
|
|
|
// Zero is a no-op.
|
|
|
|
ASSERT_EQ(0, getloadavg(load, 0));
|
|
|
|
|
|
|
|
// The Linux kernel doesn't support more than 3 (but you can ask for fewer).
|
|
|
|
ASSERT_EQ(1, getloadavg(load, 1));
|
|
|
|
ASSERT_EQ(2, getloadavg(load, 2));
|
|
|
|
ASSERT_EQ(3, getloadavg(load, 3));
|
|
|
|
ASSERT_EQ(3, getloadavg(load, 4));
|
|
|
|
ASSERT_EQ(3, getloadavg(load, INT_MAX));
|
|
|
|
|
|
|
|
// Read /proc/loadavg and check that it's "close enough".
|
|
|
|
double expected[3];
|
|
|
|
std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/loadavg", "re"), fclose};
|
|
|
|
ASSERT_EQ(3, fscanf(fp.get(), "%lf %lf %lf", &expected[0], &expected[1], &expected[2]));
|
2018-12-18 23:47:25 +01:00
|
|
|
load[0] = load[1] = load[2] = nan("");
|
2018-10-23 20:23:00 +02:00
|
|
|
ASSERT_EQ(3, getloadavg(load, 3));
|
|
|
|
|
2018-12-18 23:47:25 +01:00
|
|
|
// Check that getloadavg(3) at least overwrote the NaNs.
|
2018-10-23 20:23:00 +02:00
|
|
|
ASSERT_FALSE(isnan(load[0]));
|
2018-12-18 23:47:25 +01:00
|
|
|
ASSERT_FALSE(isnan(load[1]));
|
|
|
|
ASSERT_FALSE(isnan(load[2]));
|
|
|
|
// And that the difference between /proc/loadavg and getloadavg(3) is "small".
|
|
|
|
ASSERT_TRUE(fabs(expected[0] - load[0]) < 0.5) << expected[0] << ' ' << load[0];
|
|
|
|
ASSERT_TRUE(fabs(expected[1] - load[1]) < 0.5) << expected[1] << ' ' << load[1];
|
|
|
|
ASSERT_TRUE(fabs(expected[2] - load[2]) < 0.5) << expected[2] << ' ' << load[2];
|
2018-10-23 20:23:00 +02:00
|
|
|
}
|
2020-01-23 05:46:12 +01:00
|
|
|
|
|
|
|
TEST(stdlib, getprogname) {
|
2021-08-17 00:51:59 +02:00
|
|
|
#if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
|
2021-07-28 20:18:11 +02:00
|
|
|
GTEST_SKIP() << "glibc and musl don't have getprogname()";
|
2020-01-23 05:46:12 +01:00
|
|
|
#else
|
|
|
|
// You should always have a name.
|
|
|
|
ASSERT_TRUE(getprogname() != nullptr);
|
|
|
|
// The name should never have a slash in it.
|
|
|
|
ASSERT_TRUE(strchr(getprogname(), '/') == nullptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdlib, setprogname) {
|
2021-08-17 00:51:59 +02:00
|
|
|
#if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
|
2021-07-28 20:18:11 +02:00
|
|
|
GTEST_SKIP() << "glibc and musl don't have setprogname()";
|
2020-01-23 05:46:12 +01:00
|
|
|
#else
|
|
|
|
// setprogname() only takes the basename of what you give it.
|
|
|
|
setprogname("/usr/bin/muppet");
|
|
|
|
ASSERT_STREQ("muppet", getprogname());
|
|
|
|
#endif
|
|
|
|
}
|