Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 18 | #include "TemporaryFile.h" |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 20 | #include <errno.h> |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 21 | #include <libgen.h> |
| 22 | #include <limits.h> |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 23 | #include <pthread.h> |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 24 | #include <stdint.h> |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
Elliott Hughes | 4048856 | 2014-03-12 13:50:38 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <sys/wait.h> |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 29 | |
| 30 | TEST(stdlib, drand48) { |
| 31 | srand48(0x01020304); |
| 32 | EXPECT_DOUBLE_EQ(0.65619299195623526, drand48()); |
| 33 | EXPECT_DOUBLE_EQ(0.18522597229772941, drand48()); |
| 34 | EXPECT_DOUBLE_EQ(0.42015087072844537, drand48()); |
| 35 | EXPECT_DOUBLE_EQ(0.061637783047395089, drand48()); |
| 36 | } |
| 37 | |
| 38 | TEST(stdlib, lrand48_random_rand) { |
| 39 | srand48(0x01020304); |
| 40 | EXPECT_EQ(1409163720, lrand48()); |
| 41 | EXPECT_EQ(397769746, lrand48()); |
| 42 | EXPECT_EQ(902267124, lrand48()); |
| 43 | EXPECT_EQ(132366131, lrand48()); |
| 44 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 45 | #if defined(__BIONIC__) |
Elliott Hughes | 774c7f5 | 2012-10-01 13:11:03 -0700 | [diff] [blame] | 46 | // On bionic, random(3) is equivalent to lrand48... |
| 47 | srandom(0x01020304); |
| 48 | EXPECT_EQ(1409163720, random()); |
| 49 | EXPECT_EQ(397769746, random()); |
| 50 | EXPECT_EQ(902267124, random()); |
| 51 | EXPECT_EQ(132366131, random()); |
| 52 | |
| 53 | // ...and rand(3) is the bottom 32 bits. |
| 54 | srand(0x01020304); |
| 55 | EXPECT_EQ(static_cast<int>(1409163720), rand()); |
| 56 | EXPECT_EQ(static_cast<int>(397769746), rand()); |
| 57 | EXPECT_EQ(static_cast<int>(902267124), rand()); |
| 58 | EXPECT_EQ(static_cast<int>(132366131), rand()); |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | TEST(stdlib, mrand48) { |
| 63 | srand48(0x01020304); |
| 64 | EXPECT_EQ(-1476639856, mrand48()); |
| 65 | EXPECT_EQ(795539493, mrand48()); |
| 66 | EXPECT_EQ(1804534249, mrand48()); |
| 67 | EXPECT_EQ(264732262, mrand48()); |
| 68 | } |
Elliott Hughes | b16b722 | 2013-02-04 13:18:00 -0800 | [diff] [blame] | 69 | |
| 70 | TEST(stdlib, posix_memalign) { |
| 71 | void* p; |
| 72 | |
| 73 | ASSERT_EQ(0, posix_memalign(&p, 512, 128)); |
| 74 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(p) % 512); |
| 75 | free(p); |
| 76 | |
| 77 | // Can't align to a non-power of 2. |
| 78 | ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128)); |
| 79 | } |
Elliott Hughes | f077784 | 2013-03-01 16:59:46 -0800 | [diff] [blame] | 80 | |
| 81 | TEST(stdlib, realpath__NULL_filename) { |
| 82 | errno = 0; |
| 83 | char* p = realpath(NULL, NULL); |
| 84 | ASSERT_TRUE(p == NULL); |
| 85 | ASSERT_EQ(EINVAL, errno); |
| 86 | } |
| 87 | |
| 88 | TEST(stdlib, realpath__empty_filename) { |
| 89 | errno = 0; |
| 90 | char* p = realpath("", NULL); |
| 91 | ASSERT_TRUE(p == NULL); |
| 92 | ASSERT_EQ(ENOENT, errno); |
| 93 | } |
| 94 | |
| 95 | TEST(stdlib, realpath__ENOENT) { |
| 96 | errno = 0; |
| 97 | char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL); |
| 98 | ASSERT_TRUE(p == NULL); |
| 99 | ASSERT_EQ(ENOENT, errno); |
| 100 | } |
| 101 | |
| 102 | TEST(stdlib, realpath) { |
| 103 | // Get the name of this executable. |
| 104 | char executable_path[PATH_MAX]; |
| 105 | int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); |
| 106 | ASSERT_NE(rc, -1); |
| 107 | executable_path[rc] = '\0'; |
| 108 | |
| 109 | char buf[PATH_MAX + 1]; |
| 110 | char* p = realpath("/proc/self/exe", buf); |
| 111 | ASSERT_STREQ(executable_path, p); |
| 112 | |
| 113 | p = realpath("/proc/self/exe", NULL); |
| 114 | ASSERT_STREQ(executable_path, p); |
| 115 | free(p); |
| 116 | } |
Elliott Hughes | 0b25f63 | 2013-04-11 18:08:34 -0700 | [diff] [blame] | 117 | |
| 118 | TEST(stdlib, qsort) { |
| 119 | struct s { |
| 120 | char name[16]; |
| 121 | static int comparator(const void* lhs, const void* rhs) { |
| 122 | return strcmp(reinterpret_cast<const s*>(lhs)->name, reinterpret_cast<const s*>(rhs)->name); |
| 123 | } |
| 124 | }; |
| 125 | s entries[3]; |
| 126 | strcpy(entries[0].name, "charlie"); |
| 127 | strcpy(entries[1].name, "bravo"); |
| 128 | strcpy(entries[2].name, "alpha"); |
| 129 | |
| 130 | qsort(entries, 3, sizeof(s), s::comparator); |
| 131 | ASSERT_STREQ("alpha", entries[0].name); |
| 132 | ASSERT_STREQ("bravo", entries[1].name); |
| 133 | ASSERT_STREQ("charlie", entries[2].name); |
| 134 | |
| 135 | qsort(entries, 3, sizeof(s), s::comparator); |
| 136 | ASSERT_STREQ("alpha", entries[0].name); |
| 137 | ASSERT_STREQ("bravo", entries[1].name); |
| 138 | ASSERT_STREQ("charlie", entries[2].name); |
| 139 | } |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 140 | |
| 141 | static void* TestBug57421_child(void* arg) { |
| 142 | pthread_t main_thread = reinterpret_cast<pthread_t>(arg); |
| 143 | pthread_join(main_thread, NULL); |
| 144 | char* value = getenv("ENVIRONMENT_VARIABLE"); |
| 145 | if (value == NULL) { |
| 146 | setenv("ENVIRONMENT_VARIABLE", "value", 1); |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | static void TestBug57421_main() { |
| 152 | pthread_t t; |
| 153 | ASSERT_EQ(0, pthread_create(&t, NULL, TestBug57421_child, reinterpret_cast<void*>(pthread_self()))); |
| 154 | pthread_exit(NULL); |
| 155 | } |
| 156 | |
| 157 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 158 | // run this test (which exits normally) in its own process. |
| 159 | TEST(stdlib_DeathTest, getenv_after_main_thread_exits) { |
| 160 | // https://code.google.com/p/android/issues/detail?id=57421 |
| 161 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 162 | ASSERT_EXIT(TestBug57421_main(), ::testing::ExitedWithCode(0), ""); |
| 163 | } |
Calin Juravle | fe317a3 | 2014-02-21 15:11:03 +0000 | [diff] [blame] | 164 | |
| 165 | TEST(stdlib, mkstemp) { |
| 166 | TemporaryFile tf; |
| 167 | struct stat sb; |
| 168 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 169 | } |
| 170 | |
| 171 | TEST(stdlib, mkstemp64) { |
| 172 | GenericTemporaryFile<mkstemp64> tf; |
| 173 | struct stat64 sb; |
| 174 | ASSERT_EQ(0, fstat64(tf.fd, &sb)); |
| 175 | ASSERT_EQ(O_LARGEFILE, fcntl(tf.fd, F_GETFL) & O_LARGEFILE); |
| 176 | } |
Elliott Hughes | 3cdf573 | 2014-03-11 12:54:44 -0700 | [diff] [blame] | 177 | |
| 178 | TEST(stdlib, system) { |
| 179 | int status; |
| 180 | |
| 181 | status = system("exit 0"); |
| 182 | ASSERT_TRUE(WIFEXITED(status)); |
| 183 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 184 | |
| 185 | status = system("exit 1"); |
| 186 | ASSERT_TRUE(WIFEXITED(status)); |
| 187 | ASSERT_EQ(1, WEXITSTATUS(status)); |
| 188 | } |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 189 | |
| 190 | TEST(stdlib, atof) { |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 191 | ASSERT_DOUBLE_EQ(1.23, atof("1.23")); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | TEST(stdlib, strtod) { |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 195 | ASSERT_DOUBLE_EQ(1.23, strtod("1.23", NULL)); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | TEST(stdlib, strtof) { |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 199 | ASSERT_FLOAT_EQ(1.23, strtof("1.23", NULL)); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | TEST(stdlib, strtold) { |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 203 | ASSERT_DOUBLE_EQ(1.23, strtold("1.23", NULL)); |
Elliott Hughes | 5a81738 | 2014-03-12 16:12:57 -0700 | [diff] [blame] | 204 | } |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 205 | |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 206 | TEST(stdlib, quick_exit) { |
| 207 | pid_t pid = fork(); |
| 208 | ASSERT_NE(-1, pid) << strerror(errno); |
| 209 | |
| 210 | if (pid == 0) { |
| 211 | quick_exit(99); |
| 212 | } |
| 213 | |
| 214 | int status; |
| 215 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 216 | ASSERT_TRUE(WIFEXITED(status)); |
| 217 | ASSERT_EQ(99, WEXITSTATUS(status)); |
| 218 | } |
| 219 | |
| 220 | static int quick_exit_status = 0; |
| 221 | |
| 222 | static void quick_exit_1(void) { |
| 223 | ASSERT_EQ(quick_exit_status, 0); |
| 224 | quick_exit_status = 1; |
| 225 | } |
| 226 | |
| 227 | static void quick_exit_2(void) { |
| 228 | ASSERT_EQ(quick_exit_status, 1); |
| 229 | } |
| 230 | |
| 231 | static void not_run(void) { |
| 232 | FAIL(); |
| 233 | } |
| 234 | |
| 235 | TEST(stdlib, at_quick_exit) { |
| 236 | pid_t pid = fork(); |
| 237 | ASSERT_NE(-1, pid) << strerror(errno); |
| 238 | |
| 239 | if (pid == 0) { |
| 240 | ASSERT_EQ(at_quick_exit(quick_exit_2), 0); |
| 241 | ASSERT_EQ(at_quick_exit(quick_exit_1), 0); |
| 242 | atexit(not_run); |
| 243 | quick_exit(99); |
| 244 | } |
| 245 | |
| 246 | int status; |
| 247 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 248 | ASSERT_TRUE(WIFEXITED(status)); |
| 249 | ASSERT_EQ(99, WEXITSTATUS(status)); |
| 250 | } |
| 251 | |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 252 | TEST(unistd, _Exit) { |
| 253 | int pid = fork(); |
| 254 | ASSERT_NE(-1, pid) << strerror(errno); |
| 255 | |
| 256 | if (pid == 0) { |
| 257 | _Exit(99); |
| 258 | } |
| 259 | |
| 260 | int status; |
| 261 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 262 | ASSERT_TRUE(WIFEXITED(status)); |
| 263 | ASSERT_EQ(99, WEXITSTATUS(status)); |
| 264 | } |