Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 17 | #include <time.h> |
| 18 | |
| 19 | #include <errno.h> |
Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 21 | #include <pthread.h> |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 22 | #include <signal.h> |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 23 | #include <sys/cdefs.h> |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 24 | #include <sys/syscall.h> |
Brian Carlstrom | be1d91d | 2014-03-08 15:05:26 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 28 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 29 | #include <atomic> |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 30 | #include <chrono> |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 31 | #include <thread> |
Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 33 | #include "SignalUtils.h" |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 34 | #include "android-base/file.h" |
| 35 | #include "android-base/strings.h" |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 36 | #include "utils.h" |
Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 38 | using namespace std::chrono_literals; |
| 39 | |
Mark Salyzyn | 0b846e8 | 2017-12-20 08:56:18 -0800 | [diff] [blame] | 40 | TEST(time, time) { |
| 41 | // Acquire time |
| 42 | time_t p1, t1 = time(&p1); |
| 43 | // valid? |
| 44 | ASSERT_NE(static_cast<time_t>(0), t1); |
| 45 | ASSERT_NE(static_cast<time_t>(-1), t1); |
| 46 | ASSERT_EQ(p1, t1); |
| 47 | |
| 48 | // Acquire time one+ second later |
| 49 | usleep(1010000); |
| 50 | time_t p2, t2 = time(&p2); |
| 51 | // valid? |
| 52 | ASSERT_NE(static_cast<time_t>(0), t2); |
| 53 | ASSERT_NE(static_cast<time_t>(-1), t2); |
| 54 | ASSERT_EQ(p2, t2); |
| 55 | |
| 56 | // Expect time progression |
| 57 | ASSERT_LT(p1, p2); |
| 58 | ASSERT_LE(t2 - t1, static_cast<time_t>(2)); |
| 59 | |
| 60 | // Expect nullptr call to produce same results |
| 61 | ASSERT_LE(t2, time(nullptr)); |
| 62 | ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1)); |
| 63 | } |
| 64 | |
Elliott Hughes | ee178bf | 2013-07-12 11:25:20 -0700 | [diff] [blame] | 65 | TEST(time, gmtime) { |
| 66 | time_t t = 0; |
| 67 | tm* broken_down = gmtime(&t); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 68 | ASSERT_TRUE(broken_down != nullptr); |
Elliott Hughes | ee178bf | 2013-07-12 11:25:20 -0700 | [diff] [blame] | 69 | ASSERT_EQ(0, broken_down->tm_sec); |
| 70 | ASSERT_EQ(0, broken_down->tm_min); |
| 71 | ASSERT_EQ(0, broken_down->tm_hour); |
| 72 | ASSERT_EQ(1, broken_down->tm_mday); |
| 73 | ASSERT_EQ(0, broken_down->tm_mon); |
| 74 | ASSERT_EQ(1970, broken_down->tm_year + 1900); |
| 75 | } |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | 5a29d54 | 2017-12-07 16:05:57 -0800 | [diff] [blame] | 77 | TEST(time, gmtime_r) { |
| 78 | struct tm tm = {}; |
| 79 | time_t t = 0; |
| 80 | struct tm* broken_down = gmtime_r(&t, &tm); |
| 81 | ASSERT_EQ(broken_down, &tm); |
| 82 | ASSERT_EQ(0, broken_down->tm_sec); |
| 83 | ASSERT_EQ(0, broken_down->tm_min); |
| 84 | ASSERT_EQ(0, broken_down->tm_hour); |
| 85 | ASSERT_EQ(1, broken_down->tm_mday); |
| 86 | ASSERT_EQ(0, broken_down->tm_mon); |
| 87 | ASSERT_EQ(1970, broken_down->tm_year + 1900); |
| 88 | } |
| 89 | |
Almaz Mingaleev | da75bb6 | 2022-06-29 15:47:37 +0000 | [diff] [blame] | 90 | TEST(time, mktime_TZ_as_UTC_and_offset) { |
| 91 | struct tm tm = {.tm_year = 70, .tm_mon = 0, .tm_mday = 1}; |
| 92 | |
| 93 | // This TZ value is not a valid Olson ID and is not present in tzdata file, |
| 94 | // but is a valid TZ string according to POSIX standard. |
| 95 | setenv("TZ", "UTC+08:00:00", 1); |
| 96 | tzset(); |
| 97 | ASSERT_EQ(static_cast<time_t>(8 * 60 * 60), mktime(&tm)); |
| 98 | } |
| 99 | |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 100 | static void* gmtime_no_stack_overflow_14313703_fn(void*) { |
| 101 | const char* original_tz = getenv("TZ"); |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 102 | // Ensure we'll actually have to enter tzload by using a timezone that doesn't exist. |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 103 | setenv("TZ", "gmtime_stack_overflow_14313703", 1); |
| 104 | tzset(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 105 | if (original_tz != nullptr) { |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 106 | setenv("TZ", original_tz, 1); |
| 107 | } |
| 108 | tzset(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 109 | return nullptr; |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST(time, gmtime_no_stack_overflow_14313703) { |
| 113 | // Is it safe to call tzload on a thread with a small stack? |
| 114 | // http://b/14313703 |
| 115 | // https://code.google.com/p/android/issues/detail?id=61130 |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame] | 116 | pthread_attr_t a; |
| 117 | ASSERT_EQ(0, pthread_attr_init(&a)); |
| 118 | ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN)); |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 119 | |
| 120 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 121 | ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr)); |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame] | 122 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Satoru Takeuchi | 154e202 | 2014-05-27 17:04:04 +0900 | [diff] [blame] | 125 | TEST(time, mktime_empty_TZ) { |
| 126 | // tzcode used to have a bug where it didn't reinitialize some internal state. |
| 127 | |
| 128 | // Choose a time where DST is set. |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 129 | struct tm t = {}; |
Satoru Takeuchi | 154e202 | 2014-05-27 17:04:04 +0900 | [diff] [blame] | 130 | t.tm_year = 1980 - 1900; |
| 131 | t.tm_mon = 6; |
| 132 | t.tm_mday = 2; |
| 133 | |
| 134 | setenv("TZ", "America/Los_Angeles", 1); |
| 135 | tzset(); |
| 136 | ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t)); |
| 137 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 138 | t = {}; |
Satoru Takeuchi | 154e202 | 2014-05-27 17:04:04 +0900 | [diff] [blame] | 139 | t.tm_year = 1980 - 1900; |
| 140 | t.tm_mon = 6; |
| 141 | t.tm_mday = 2; |
| 142 | |
| 143 | setenv("TZ", "", 1); // Implies UTC. |
| 144 | tzset(); |
| 145 | ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t)); |
| 146 | } |
| 147 | |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 148 | TEST(time, mktime_10310929) { |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 149 | struct tm tm = {.tm_year = 2100 - 1900, .tm_mon = 2, .tm_mday = 10}; |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 150 | |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 151 | #if !defined(__LP64__) |
Elliott Hughes | cf34653 | 2020-07-31 10:35:03 -0700 | [diff] [blame] | 152 | // 32-bit bionic has a signed 32-bit time_t. |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 153 | ASSERT_EQ(-1, mktime(&tm)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 154 | ASSERT_ERRNO(EOVERFLOW); |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 155 | #else |
| 156 | // Everyone else should be using a signed 64-bit time_t. |
| 157 | ASSERT_GE(sizeof(time_t) * 8, 64U); |
| 158 | |
| 159 | setenv("TZ", "America/Los_Angeles", 1); |
| 160 | tzset(); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 161 | errno = 0; |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 162 | |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 163 | // On the date/time specified by tm America/Los_Angeles |
| 164 | // follows DST. But tm_isdst is set to 0, which forces |
| 165 | // mktime to interpret that time as local standard, hence offset |
| 166 | // is 8 hours, not 7. |
| 167 | ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&tm)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 168 | ASSERT_ERRNO(0); |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 169 | #endif |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 170 | } |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 171 | |
Elliott Hughes | 7c9c122 | 2024-01-12 23:46:29 +0000 | [diff] [blame] | 172 | TEST(time, mktime_EOVERFLOW) { |
Elliott Hughes | f52b216 | 2023-05-19 16:09:47 -0700 | [diff] [blame] | 173 | setenv("TZ", "UTC", 1); |
| 174 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 175 | struct tm t = {}; |
Elliott Hughes | 47126ed | 2016-09-06 13:25:53 -0700 | [diff] [blame] | 176 | |
| 177 | // LP32 year range is 1901-2038, so this year is guaranteed not to overflow. |
| 178 | t.tm_year = 2016 - 1900; |
| 179 | |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 180 | t.tm_mon = 2; |
| 181 | t.tm_mday = 10; |
| 182 | |
| 183 | errno = 0; |
| 184 | ASSERT_NE(static_cast<time_t>(-1), mktime(&t)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 185 | ASSERT_ERRNO(0); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 186 | |
Almaz Mingaleev | d86a3ab | 2023-04-27 11:24:48 +0100 | [diff] [blame] | 187 | // This will overflow for LP32. |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 188 | t.tm_year = INT_MAX; |
Elliott Hughes | 47126ed | 2016-09-06 13:25:53 -0700 | [diff] [blame] | 189 | |
| 190 | errno = 0; |
Almaz Mingaleev | d86a3ab | 2023-04-27 11:24:48 +0100 | [diff] [blame] | 191 | #if !defined(__LP64__) |
| 192 | ASSERT_EQ(static_cast<time_t>(-1), mktime(&t)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 193 | ASSERT_ERRNO(EOVERFLOW); |
Almaz Mingaleev | d86a3ab | 2023-04-27 11:24:48 +0100 | [diff] [blame] | 194 | #else |
| 195 | ASSERT_EQ(static_cast<time_t>(67768036166016000U), mktime(&t)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 196 | ASSERT_ERRNO(0); |
Almaz Mingaleev | d86a3ab | 2023-04-27 11:24:48 +0100 | [diff] [blame] | 197 | #endif |
| 198 | |
| 199 | // This will overflow for LP32 or LP64. |
| 200 | // tm_year is int, this t struct points to INT_MAX + 1 no matter what TZ is. |
| 201 | t.tm_year = INT_MAX; |
| 202 | t.tm_mon = 11; |
| 203 | t.tm_mday = 45; |
| 204 | |
| 205 | errno = 0; |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 206 | ASSERT_EQ(static_cast<time_t>(-1), mktime(&t)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 207 | ASSERT_ERRNO(EOVERFLOW); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Almaz Mingaleev | 5411aff | 2022-02-11 12:27:12 +0000 | [diff] [blame] | 210 | TEST(time, mktime_invalid_tm_TZ_combination) { |
| 211 | setenv("TZ", "UTC", 1); |
| 212 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 213 | struct tm t = {}; |
Almaz Mingaleev | 5411aff | 2022-02-11 12:27:12 +0000 | [diff] [blame] | 214 | t.tm_year = 2022 - 1900; |
| 215 | t.tm_mon = 11; |
| 216 | t.tm_mday = 31; |
| 217 | // UTC does not observe DST |
| 218 | t.tm_isdst = 1; |
| 219 | |
| 220 | errno = 0; |
| 221 | |
| 222 | EXPECT_EQ(static_cast<time_t>(-1), mktime(&t)); |
| 223 | // mktime sets errno to EOVERFLOW if result is unrepresentable. |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 224 | EXPECT_ERRNO(EOVERFLOW); |
Almaz Mingaleev | 5411aff | 2022-02-11 12:27:12 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 227 | // Transitions in the tzdata file are generated up to the year 2100. Testing |
| 228 | // that dates beyond that are handled properly too. |
| 229 | TEST(time, mktime_after_2100) { |
| 230 | struct tm tm = {.tm_year = 2150 - 1900, .tm_mon = 2, .tm_mday = 10, .tm_isdst = -1}; |
| 231 | |
| 232 | #if !defined(__LP64__) |
| 233 | // 32-bit bionic has a signed 32-bit time_t. |
| 234 | ASSERT_EQ(-1, mktime(&tm)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 235 | ASSERT_ERRNO(EOVERFLOW); |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 236 | #else |
| 237 | setenv("TZ", "Europe/London", 1); |
| 238 | tzset(); |
| 239 | errno = 0; |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 240 | ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 241 | ASSERT_ERRNO(0); |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 242 | #endif |
| 243 | } |
| 244 | |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 245 | TEST(time, strftime) { |
| 246 | setenv("TZ", "UTC", 1); |
| 247 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 248 | struct tm t = {}; |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 249 | t.tm_year = 200; |
| 250 | t.tm_mon = 2; |
| 251 | t.tm_mday = 10; |
| 252 | |
| 253 | char buf[64]; |
| 254 | |
| 255 | // Seconds since the epoch. |
| 256 | #if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc. |
| 257 | EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t)); |
| 258 | EXPECT_STREQ("4108320000", buf); |
| 259 | #endif |
| 260 | |
| 261 | // Date and time as text. |
| 262 | EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t)); |
| 263 | EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf); |
| 264 | } |
| 265 | |
Almaz Mingaleev | 5411aff | 2022-02-11 12:27:12 +0000 | [diff] [blame] | 266 | TEST(time, strftime_second_before_epoch) { |
| 267 | setenv("TZ", "UTC", 1); |
| 268 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 269 | struct tm t = {}; |
Almaz Mingaleev | 5411aff | 2022-02-11 12:27:12 +0000 | [diff] [blame] | 270 | t.tm_year = 1969 - 1900; |
| 271 | t.tm_mon = 11; |
| 272 | t.tm_mday = 31; |
| 273 | t.tm_hour = 23; |
| 274 | t.tm_min = 59; |
| 275 | t.tm_sec = 59; |
| 276 | |
| 277 | char buf[64]; |
| 278 | |
| 279 | EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t)); |
| 280 | EXPECT_STREQ("-1", buf); |
| 281 | } |
| 282 | |
Almaz Mingaleev | 24bfd8e | 2022-07-15 11:01:53 +0100 | [diff] [blame] | 283 | TEST(time, strftime_Z_null_tm_zone) { |
Elliott Hughes | a9cac4c | 2015-11-12 16:51:31 -0800 | [diff] [blame] | 284 | // Netflix on Nexus Player wouldn't start (http://b/25170306). |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 285 | struct tm t = {}; |
Elliott Hughes | a9cac4c | 2015-11-12 16:51:31 -0800 | [diff] [blame] | 286 | char buf[64]; |
| 287 | |
| 288 | setenv("TZ", "America/Los_Angeles", 1); |
| 289 | tzset(); |
| 290 | |
| 291 | t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect". |
| 292 | EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 293 | EXPECT_STREQ("<PST>", buf); |
| 294 | |
| 295 | #if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1. |
| 296 | t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect" |
| 297 | EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 298 | EXPECT_STREQ("<PDT>", buf); |
| 299 | |
| 300 | t.tm_isdst = -123; // "and negative if the information is not available". |
| 301 | EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 302 | EXPECT_STREQ("<>", buf); |
| 303 | #endif |
| 304 | |
| 305 | setenv("TZ", "UTC", 1); |
| 306 | tzset(); |
| 307 | |
| 308 | t.tm_isdst = 0; |
| 309 | EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 310 | EXPECT_STREQ("<UTC>", buf); |
| 311 | |
| 312 | #if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC". |
| 313 | t.tm_isdst = 1; // UTC has no DST. |
| 314 | EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 315 | EXPECT_STREQ("<>", buf); |
| 316 | #endif |
| 317 | } |
| 318 | |
Almaz Mingaleev | 24bfd8e | 2022-07-15 11:01:53 +0100 | [diff] [blame] | 319 | // According to C language specification the only tm struct field needed to |
| 320 | // find out replacement for %z and %Z in strftime is tm_isdst. Which is |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 321 | // wrong, as timezones change their standard offset and even DST savings. |
Almaz Mingaleev | 24bfd8e | 2022-07-15 11:01:53 +0100 | [diff] [blame] | 322 | // tzcode deviates from C language specification and requires tm struct either |
| 323 | // to be output of localtime-like functions or to be modified by mktime call |
| 324 | // before passing to strftime. See tz mailing discussion for more details |
| 325 | // https://mm.icann.org/pipermail/tz/2022-July/031674.html |
| 326 | // But we are testing case when tm.tm_zone is null, which means that tm struct |
| 327 | // is not coming from localtime and is neither modified by mktime. That's why |
| 328 | // we are comparing against +0000, even though America/Los_Angeles never |
| 329 | // observes it. |
| 330 | TEST(time, strftime_z_null_tm_zone) { |
| 331 | char str[64]; |
| 332 | struct tm tm = {.tm_year = 109, .tm_mon = 4, .tm_mday = 2, .tm_isdst = 0}; |
| 333 | |
| 334 | setenv("TZ", "America/Los_Angeles", 1); |
| 335 | tzset(); |
| 336 | |
| 337 | tm.tm_zone = NULL; |
| 338 | |
| 339 | size_t result = strftime(str, sizeof(str), "%z", &tm); |
| 340 | |
| 341 | EXPECT_EQ(5U, result); |
| 342 | EXPECT_STREQ("+0000", str); |
| 343 | |
| 344 | tm.tm_isdst = 1; |
| 345 | |
| 346 | result = strftime(str, sizeof(str), "%z", &tm); |
| 347 | |
| 348 | EXPECT_EQ(5U, result); |
| 349 | EXPECT_STREQ("+0000", str); |
| 350 | |
| 351 | setenv("TZ", "UTC", 1); |
| 352 | tzset(); |
| 353 | |
| 354 | tm.tm_isdst = 0; |
| 355 | |
| 356 | result = strftime(str, sizeof(str), "%z", &tm); |
| 357 | |
| 358 | EXPECT_EQ(5U, result); |
| 359 | EXPECT_STREQ("+0000", str); |
| 360 | |
| 361 | tm.tm_isdst = 1; |
| 362 | |
| 363 | result = strftime(str, sizeof(str), "%z", &tm); |
| 364 | |
| 365 | EXPECT_EQ(5U, result); |
| 366 | EXPECT_STREQ("+0000", str); |
| 367 | } |
| 368 | |
| 369 | TEST(time, strftime_z_Europe_Lisbon) { |
| 370 | char str[64]; |
| 371 | // During 1992-1996 Europe/Lisbon standard offset was 1 hour. |
| 372 | // tm_isdst is not set as it will be overridden by mktime call anyway. |
| 373 | struct tm tm = {.tm_year = 1996 - 1900, .tm_mon = 2, .tm_mday = 13}; |
| 374 | |
| 375 | setenv("TZ", "Europe/Lisbon", 1); |
| 376 | tzset(); |
| 377 | |
| 378 | // tzcode's strftime implementation for %z relies on prior mktime call. |
| 379 | // At the moment of writing %z value is taken from tm_gmtoff. So without |
| 380 | // mktime call %z is replaced with +0000. |
| 381 | // See https://mm.icann.org/pipermail/tz/2022-July/031674.html |
| 382 | mktime(&tm); |
| 383 | |
| 384 | size_t result = strftime(str, sizeof(str), "%z", &tm); |
| 385 | |
| 386 | EXPECT_EQ(5U, result); |
| 387 | EXPECT_STREQ("+0100", str); |
| 388 | |
| 389 | // Now standard offset is 0. |
| 390 | tm = {.tm_year = 2022 - 1900, .tm_mon = 2, .tm_mday = 13}; |
| 391 | |
| 392 | mktime(&tm); |
| 393 | result = strftime(str, sizeof(str), "%z", &tm); |
| 394 | |
| 395 | EXPECT_EQ(5U, result); |
| 396 | EXPECT_STREQ("+0000", str); |
| 397 | } |
| 398 | |
Elliott Hughes | 0a610d0 | 2016-07-29 14:04:17 -0700 | [diff] [blame] | 399 | TEST(time, strftime_l) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 400 | locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr); |
Elliott Hughes | 0a610d0 | 2016-07-29 14:04:17 -0700 | [diff] [blame] | 401 | locale_t old_locale = uselocale(cloc); |
| 402 | |
| 403 | setenv("TZ", "UTC", 1); |
| 404 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 405 | struct tm t = {}; |
Elliott Hughes | 0a610d0 | 2016-07-29 14:04:17 -0700 | [diff] [blame] | 406 | t.tm_year = 200; |
| 407 | t.tm_mon = 2; |
| 408 | t.tm_mday = 10; |
| 409 | |
| 410 | // Date and time as text. |
| 411 | char buf[64]; |
| 412 | EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc)); |
| 413 | EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf); |
| 414 | |
| 415 | uselocale(old_locale); |
| 416 | freelocale(cloc); |
| 417 | } |
| 418 | |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 419 | TEST(time, strptime) { |
| 420 | setenv("TZ", "UTC", 1); |
| 421 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 422 | struct tm t = {}; |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 423 | char buf[64]; |
| 424 | |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 425 | strptime("11:14", "%R", &t); |
| 426 | strftime(buf, sizeof(buf), "%H:%M", &t); |
| 427 | EXPECT_STREQ("11:14", buf); |
| 428 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 429 | t = {}; |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 430 | strptime("09:41:53", "%T", &t); |
| 431 | strftime(buf, sizeof(buf), "%H:%M:%S", &t); |
| 432 | EXPECT_STREQ("09:41:53", buf); |
| 433 | } |
| 434 | |
Elliott Hughes | 3376c23 | 2018-02-13 23:14:12 -0800 | [diff] [blame] | 435 | TEST(time, strptime_l) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 436 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 3376c23 | 2018-02-13 23:14:12 -0800 | [diff] [blame] | 437 | setenv("TZ", "UTC", 1); |
| 438 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 439 | struct tm t = {}; |
Elliott Hughes | 3376c23 | 2018-02-13 23:14:12 -0800 | [diff] [blame] | 440 | char buf[64]; |
| 441 | |
Elliott Hughes | 3376c23 | 2018-02-13 23:14:12 -0800 | [diff] [blame] | 442 | strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE); |
| 443 | strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE); |
| 444 | EXPECT_STREQ("11:14", buf); |
| 445 | |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 446 | t = {}; |
Elliott Hughes | 3376c23 | 2018-02-13 23:14:12 -0800 | [diff] [blame] | 447 | strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE); |
| 448 | strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE); |
| 449 | EXPECT_STREQ("09:41:53", buf); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 450 | #else |
| 451 | GTEST_SKIP() << "musl doesn't support strptime_l"; |
| 452 | #endif |
Elliott Hughes | 3376c23 | 2018-02-13 23:14:12 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Elliott Hughes | a1fb15b | 2019-03-26 19:07:40 -0700 | [diff] [blame] | 455 | TEST(time, strptime_F) { |
| 456 | setenv("TZ", "UTC", 1); |
| 457 | |
| 458 | struct tm tm = {}; |
| 459 | ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm)); |
| 460 | EXPECT_EQ(119, tm.tm_year); |
| 461 | EXPECT_EQ(2, tm.tm_mon); |
| 462 | EXPECT_EQ(26, tm.tm_mday); |
| 463 | } |
| 464 | |
| 465 | TEST(time, strptime_P_p) { |
| 466 | setenv("TZ", "UTC", 1); |
| 467 | |
Elliott Hughes | 1167882 | 2019-03-27 08:56:49 -0700 | [diff] [blame] | 468 | // For parsing, %P and %p are the same: case doesn't matter. |
| 469 | |
Elliott Hughes | a1fb15b | 2019-03-26 19:07:40 -0700 | [diff] [blame] | 470 | struct tm tm = {.tm_hour = 12}; |
| 471 | ASSERT_EQ('\0', *strptime("AM", "%p", &tm)); |
| 472 | EXPECT_EQ(0, tm.tm_hour); |
| 473 | |
| 474 | tm = {.tm_hour = 12}; |
Elliott Hughes | 1167882 | 2019-03-27 08:56:49 -0700 | [diff] [blame] | 475 | ASSERT_EQ('\0', *strptime("am", "%p", &tm)); |
| 476 | EXPECT_EQ(0, tm.tm_hour); |
| 477 | |
| 478 | tm = {.tm_hour = 12}; |
Elliott Hughes | a1fb15b | 2019-03-26 19:07:40 -0700 | [diff] [blame] | 479 | ASSERT_EQ('\0', *strptime("AM", "%P", &tm)); |
| 480 | EXPECT_EQ(0, tm.tm_hour); |
Elliott Hughes | 1167882 | 2019-03-27 08:56:49 -0700 | [diff] [blame] | 481 | |
| 482 | tm = {.tm_hour = 12}; |
| 483 | ASSERT_EQ('\0', *strptime("am", "%P", &tm)); |
| 484 | EXPECT_EQ(0, tm.tm_hour); |
Elliott Hughes | a1fb15b | 2019-03-26 19:07:40 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | TEST(time, strptime_u) { |
| 488 | setenv("TZ", "UTC", 1); |
| 489 | |
| 490 | struct tm tm = {}; |
| 491 | ASSERT_EQ('\0', *strptime("2", "%u", &tm)); |
| 492 | EXPECT_EQ(2, tm.tm_wday); |
| 493 | } |
| 494 | |
| 495 | TEST(time, strptime_v) { |
| 496 | setenv("TZ", "UTC", 1); |
| 497 | |
| 498 | struct tm tm = {}; |
| 499 | ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm)); |
| 500 | EXPECT_EQ(80, tm.tm_year); |
| 501 | EXPECT_EQ(2, tm.tm_mon); |
| 502 | EXPECT_EQ(26, tm.tm_mday); |
| 503 | } |
| 504 | |
| 505 | TEST(time, strptime_V_G_g) { |
| 506 | setenv("TZ", "UTC", 1); |
| 507 | |
| 508 | // %V (ISO-8601 week number), %G (year of week number, without century), and |
| 509 | // %g (year of week number) have no effect when parsed, and are supported |
| 510 | // solely so that it's possible for strptime(3) to parse everything that |
| 511 | // strftime(3) can output. |
| 512 | struct tm tm = {}; |
| 513 | ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm)); |
| 514 | struct tm zero = {}; |
| 515 | EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0); |
| 516 | } |
| 517 | |
Elliott Hughes | d065c04 | 2020-09-01 19:02:44 -0700 | [diff] [blame] | 518 | TEST(time, strptime_Z) { |
| 519 | #if defined(__BIONIC__) |
| 520 | // glibc doesn't handle %Z at all. |
| 521 | // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings |
| 522 | // are in the global `tzname` (which correspond to the current $TZ). |
| 523 | struct tm tm; |
| 524 | setenv("TZ", "Europe/Berlin", 1); |
| 525 | |
| 526 | // "GMT" always works. |
| 527 | tm = {}; |
| 528 | ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm)); |
| 529 | EXPECT_STREQ("GMT", tm.tm_zone); |
| 530 | EXPECT_EQ(0, tm.tm_isdst); |
| 531 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 532 | |
| 533 | // As does "UTC". |
| 534 | tm = {}; |
| 535 | ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm)); |
| 536 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 537 | EXPECT_EQ(0, tm.tm_isdst); |
| 538 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 539 | |
| 540 | // Europe/Berlin is known as "CET" when there's no DST. |
| 541 | tm = {}; |
| 542 | ASSERT_EQ('\0', *strptime("CET", "%Z", &tm)); |
| 543 | EXPECT_STREQ("CET", tm.tm_zone); |
| 544 | EXPECT_EQ(0, tm.tm_isdst); |
| 545 | EXPECT_EQ(3600, tm.tm_gmtoff); |
| 546 | |
| 547 | // Europe/Berlin is known as "CEST" when there's no DST. |
| 548 | tm = {}; |
| 549 | ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm)); |
| 550 | EXPECT_STREQ("CEST", tm.tm_zone); |
| 551 | EXPECT_EQ(1, tm.tm_isdst); |
| 552 | EXPECT_EQ(3600, tm.tm_gmtoff); |
| 553 | |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 554 | // And as long as we're in Europe/Berlin, those are the only timezone |
Elliott Hughes | d065c04 | 2020-09-01 19:02:44 -0700 | [diff] [blame] | 555 | // abbreviations that are recognized. |
| 556 | tm = {}; |
| 557 | ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr); |
| 558 | #endif |
| 559 | } |
| 560 | |
| 561 | TEST(time, strptime_z) { |
| 562 | struct tm tm; |
| 563 | setenv("TZ", "Europe/Berlin", 1); |
| 564 | |
| 565 | // "UT" is what RFC822 called UTC. |
| 566 | tm = {}; |
| 567 | ASSERT_EQ('\0', *strptime("UT", "%z", &tm)); |
| 568 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 569 | EXPECT_EQ(0, tm.tm_isdst); |
| 570 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 571 | // "GMT" is RFC822's other name for UTC. |
| 572 | tm = {}; |
| 573 | ASSERT_EQ('\0', *strptime("GMT", "%z", &tm)); |
| 574 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 575 | EXPECT_EQ(0, tm.tm_isdst); |
| 576 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 577 | |
| 578 | // "Z" ("Zulu") is a synonym for UTC. |
| 579 | tm = {}; |
| 580 | ASSERT_EQ('\0', *strptime("Z", "%z", &tm)); |
| 581 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 582 | EXPECT_EQ(0, tm.tm_isdst); |
| 583 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 584 | |
| 585 | // "PST"/"PDT" and the other common US zone abbreviations are all supported. |
| 586 | tm = {}; |
| 587 | ASSERT_EQ('\0', *strptime("PST", "%z", &tm)); |
| 588 | EXPECT_STREQ("PST", tm.tm_zone); |
| 589 | EXPECT_EQ(0, tm.tm_isdst); |
| 590 | EXPECT_EQ(-28800, tm.tm_gmtoff); |
| 591 | tm = {}; |
| 592 | ASSERT_EQ('\0', *strptime("PDT", "%z", &tm)); |
| 593 | EXPECT_STREQ("PDT", tm.tm_zone); |
| 594 | EXPECT_EQ(1, tm.tm_isdst); |
| 595 | EXPECT_EQ(-25200, tm.tm_gmtoff); |
| 596 | |
| 597 | // +-hh |
| 598 | tm = {}; |
| 599 | ASSERT_EQ('\0', *strptime("+01", "%z", &tm)); |
| 600 | EXPECT_EQ(3600, tm.tm_gmtoff); |
| 601 | EXPECT_TRUE(tm.tm_zone == nullptr); |
| 602 | EXPECT_EQ(0, tm.tm_isdst); |
| 603 | // +-hhmm |
| 604 | tm = {}; |
| 605 | ASSERT_EQ('\0', *strptime("+0130", "%z", &tm)); |
| 606 | EXPECT_EQ(5400, tm.tm_gmtoff); |
| 607 | EXPECT_TRUE(tm.tm_zone == nullptr); |
| 608 | EXPECT_EQ(0, tm.tm_isdst); |
| 609 | // +-hh:mm |
| 610 | tm = {}; |
| 611 | ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm)); |
| 612 | EXPECT_EQ(5400, tm.tm_gmtoff); |
| 613 | EXPECT_TRUE(tm.tm_zone == nullptr); |
| 614 | EXPECT_EQ(0, tm.tm_isdst); |
| 615 | } |
| 616 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 617 | void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { |
| 618 | itimerspec ts; |
| 619 | ts.it_value.tv_sec = value_s; |
| 620 | ts.it_value.tv_nsec = value_ns; |
| 621 | ts.it_interval.tv_sec = interval_s; |
| 622 | ts.it_interval.tv_nsec = interval_ns; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 623 | ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 626 | static void NoOpNotifyFunction(sigval) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | TEST(time, timer_create) { |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 630 | sigevent se = {}; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 631 | se.sigev_notify = SIGEV_THREAD; |
| 632 | se.sigev_notify_function = NoOpNotifyFunction; |
| 633 | timer_t timer_id; |
| 634 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); |
| 635 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 636 | pid_t pid = fork(); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 637 | ASSERT_NE(-1, pid) << strerror(errno); |
| 638 | |
| 639 | if (pid == 0) { |
| 640 | // Timers are not inherited by the child. |
| 641 | ASSERT_EQ(-1, timer_delete(timer_id)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 642 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 643 | _exit(0); |
| 644 | } |
| 645 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 646 | AssertChildExited(pid, 0); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 647 | |
| 648 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 649 | } |
| 650 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 651 | static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 652 | static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) { |
| 653 | ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; |
| 654 | ASSERT_EQ(SIGUSR1, signal_number); |
| 655 | } |
| 656 | |
| 657 | TEST(time, timer_create_SIGEV_SIGNAL) { |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 658 | sigevent se = {}; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 659 | se.sigev_notify = SIGEV_SIGNAL; |
| 660 | se.sigev_signo = SIGUSR1; |
| 661 | |
| 662 | timer_t timer_id; |
| 663 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); |
| 664 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 665 | timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 666 | ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler); |
| 667 | |
| 668 | ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); |
| 669 | |
| 670 | itimerspec ts; |
| 671 | ts.it_value.tv_sec = 0; |
| 672 | ts.it_value.tv_nsec = 1; |
| 673 | ts.it_interval.tv_sec = 0; |
| 674 | ts.it_interval.tv_nsec = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 675 | ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 676 | |
| 677 | usleep(500000); |
| 678 | ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); |
| 679 | } |
| 680 | |
| 681 | struct Counter { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 682 | private: |
| 683 | std::atomic<int> value; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 684 | timer_t timer_id; |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 685 | sigevent se; |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 686 | bool timer_valid; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 687 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 688 | void Create() { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 689 | ASSERT_FALSE(timer_valid); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 690 | ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id)); |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 691 | timer_valid = true; |
| 692 | } |
| 693 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 694 | public: |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 695 | explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) { |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 696 | se = {.sigev_notify = SIGEV_THREAD, .sigev_notify_function = fn, .sigev_value.sival_ptr = this}; |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 697 | Create(); |
| 698 | } |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 699 | void DeleteTimer() { |
| 700 | ASSERT_TRUE(timer_valid); |
| 701 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 702 | timer_valid = false; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | ~Counter() { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 706 | if (timer_valid) { |
| 707 | DeleteTimer(); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 711 | int Value() const { |
| 712 | return value; |
| 713 | } |
| 714 | |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 715 | void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { |
| 716 | ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns); |
| 717 | } |
| 718 | |
| 719 | bool ValueUpdated() { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 720 | int current_value = value; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 721 | time_t start = time(nullptr); |
| 722 | while (current_value == value && (time(nullptr) - start) < 5) { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 723 | } |
| 724 | return current_value != value; |
| 725 | } |
| 726 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 727 | static void CountNotifyFunction(sigval value) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 728 | Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); |
| 729 | ++cd->value; |
| 730 | } |
| 731 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 732 | static void CountAndDisarmNotifyFunction(sigval value) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 733 | Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); |
| 734 | ++cd->value; |
| 735 | |
| 736 | // Setting the initial expiration time to 0 disarms the timer. |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 737 | cd->SetTime(0, 0, 1, 0); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 738 | } |
| 739 | }; |
| 740 | |
| 741 | TEST(time, timer_settime_0) { |
| 742 | Counter counter(Counter::CountAndDisarmNotifyFunction); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 743 | ASSERT_EQ(0, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 744 | |
Yabin Cui | bf572d9 | 2015-08-11 11:23:16 -0700 | [diff] [blame] | 745 | counter.SetTime(0, 500000000, 1, 0); |
| 746 | sleep(1); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 747 | |
| 748 | // The count should just be 1 because we disarmed the timer the first time it fired. |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 749 | ASSERT_EQ(1, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | TEST(time, timer_settime_repeats) { |
| 753 | Counter counter(Counter::CountNotifyFunction); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 754 | ASSERT_EQ(0, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 755 | |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 756 | counter.SetTime(0, 1, 0, 10); |
| 757 | ASSERT_TRUE(counter.ValueUpdated()); |
| 758 | ASSERT_TRUE(counter.ValueUpdated()); |
| 759 | ASSERT_TRUE(counter.ValueUpdated()); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 760 | counter.DeleteTimer(); |
| 761 | // Add a sleep as other threads may be calling the callback function when the timer is deleted. |
| 762 | usleep(500000); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 763 | } |
| 764 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 765 | static int timer_create_NULL_signal_handler_invocation_count; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 766 | static void timer_create_NULL_signal_handler(int signal_number) { |
| 767 | ++timer_create_NULL_signal_handler_invocation_count; |
| 768 | ASSERT_EQ(SIGALRM, signal_number); |
| 769 | } |
| 770 | |
| 771 | TEST(time, timer_create_NULL) { |
| 772 | // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM. |
| 773 | timer_t timer_id; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 774 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 775 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 776 | timer_create_NULL_signal_handler_invocation_count = 0; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 777 | ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler); |
| 778 | |
| 779 | ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count); |
| 780 | |
| 781 | SetTime(timer_id, 0, 1, 0, 0); |
| 782 | usleep(500000); |
| 783 | |
| 784 | ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count); |
| 785 | } |
| 786 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 787 | static int GetThreadCount() { |
| 788 | std::string status; |
| 789 | if (android::base::ReadFileToString("/proc/self/status", &status)) { |
| 790 | for (const auto& line : android::base::Split(status, "\n")) { |
| 791 | int thread_count; |
| 792 | if (sscanf(line.c_str(), "Threads: %d", &thread_count) == 1) { |
| 793 | return thread_count; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | return -1; |
| 798 | } |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 799 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 800 | TEST(time, timer_create_EINVAL) { |
| 801 | const clockid_t kInvalidClock = 16; |
| 802 | |
| 803 | // A SIGEV_SIGNAL timer failure is easy; that's the kernel's problem. |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 804 | timer_t timer_id; |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 805 | ASSERT_EQ(-1, timer_create(kInvalidClock, nullptr, &timer_id)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 806 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 807 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 808 | // A SIGEV_THREAD timer failure is more interesting because we have a thread |
| 809 | // to clean up (https://issuetracker.google.com/340125671). |
| 810 | sigevent se = {}; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 811 | se.sigev_notify = SIGEV_THREAD; |
| 812 | se.sigev_notify_function = NoOpNotifyFunction; |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 813 | ASSERT_EQ(-1, timer_create(kInvalidClock, &se, &timer_id)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 814 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 815 | |
| 816 | // timer_create() doesn't guarantee that the thread will be dead _before_ |
| 817 | // it returns because that would require extra synchronization that's |
| 818 | // unnecessary in the normal (successful) case. A timeout here means we |
| 819 | // leaked a thread. |
| 820 | while (GetThreadCount() > 1) { |
| 821 | } |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 824 | TEST(time, timer_create_multiple) { |
| 825 | Counter counter1(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 826 | Counter counter2(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 827 | Counter counter3(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 828 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 829 | ASSERT_EQ(0, counter1.Value()); |
| 830 | ASSERT_EQ(0, counter2.Value()); |
| 831 | ASSERT_EQ(0, counter3.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 832 | |
Yabin Cui | 410c1ad | 2015-06-18 16:19:02 -0700 | [diff] [blame] | 833 | counter2.SetTime(0, 500000000, 0, 0); |
| 834 | sleep(1); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 835 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 836 | EXPECT_EQ(0, counter1.Value()); |
| 837 | EXPECT_EQ(1, counter2.Value()); |
| 838 | EXPECT_EQ(0, counter3.Value()); |
| 839 | } |
| 840 | |
| 841 | // Test to verify that disarming a repeatable timer disables the callbacks. |
| 842 | TEST(time, timer_disarm_terminates) { |
| 843 | Counter counter(Counter::CountNotifyFunction); |
| 844 | ASSERT_EQ(0, counter.Value()); |
| 845 | |
| 846 | counter.SetTime(0, 1, 0, 1); |
| 847 | ASSERT_TRUE(counter.ValueUpdated()); |
| 848 | ASSERT_TRUE(counter.ValueUpdated()); |
| 849 | ASSERT_TRUE(counter.ValueUpdated()); |
| 850 | |
| 851 | counter.SetTime(0, 0, 0, 0); |
| 852 | // Add a sleep as the kernel may have pending events when the timer is disarmed. |
| 853 | usleep(500000); |
| 854 | int value = counter.Value(); |
| 855 | usleep(500000); |
| 856 | |
| 857 | // Verify the counter has not been incremented. |
| 858 | ASSERT_EQ(value, counter.Value()); |
| 859 | } |
| 860 | |
| 861 | // Test to verify that deleting a repeatable timer disables the callbacks. |
| 862 | TEST(time, timer_delete_terminates) { |
| 863 | Counter counter(Counter::CountNotifyFunction); |
| 864 | ASSERT_EQ(0, counter.Value()); |
| 865 | |
| 866 | counter.SetTime(0, 1, 0, 1); |
| 867 | ASSERT_TRUE(counter.ValueUpdated()); |
| 868 | ASSERT_TRUE(counter.ValueUpdated()); |
| 869 | ASSERT_TRUE(counter.ValueUpdated()); |
| 870 | |
| 871 | counter.DeleteTimer(); |
| 872 | // Add a sleep as other threads may be calling the callback function when the timer is deleted. |
| 873 | usleep(500000); |
| 874 | int value = counter.Value(); |
| 875 | usleep(500000); |
| 876 | |
| 877 | // Verify the counter has not been incremented. |
| 878 | ASSERT_EQ(value, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 879 | } |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 880 | |
| 881 | struct TimerDeleteData { |
| 882 | timer_t timer_id; |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 883 | pid_t tid; |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 884 | volatile bool complete; |
| 885 | }; |
| 886 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 887 | static void TimerDeleteCallback(sigval value) { |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 888 | TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr); |
| 889 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 890 | tdd->tid = gettid(); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 891 | timer_delete(tdd->timer_id); |
| 892 | tdd->complete = true; |
| 893 | } |
| 894 | |
| 895 | TEST(time, timer_delete_from_timer_thread) { |
| 896 | TimerDeleteData tdd; |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 897 | sigevent se = {}; |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 898 | se.sigev_notify = SIGEV_THREAD; |
| 899 | se.sigev_notify_function = TimerDeleteCallback; |
| 900 | se.sigev_value.sival_ptr = &tdd; |
| 901 | |
| 902 | tdd.complete = false; |
| 903 | ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id)); |
| 904 | |
| 905 | itimerspec ts; |
Christopher Ferris | 3da136a | 2015-02-18 17:11:47 -0800 | [diff] [blame] | 906 | ts.it_value.tv_sec = 1; |
| 907 | ts.it_value.tv_nsec = 0; |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 908 | ts.it_interval.tv_sec = 0; |
| 909 | ts.it_interval.tv_nsec = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 910 | ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr)); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 911 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 912 | time_t cur_time = time(nullptr); |
| 913 | while (!tdd.complete && (time(nullptr) - cur_time) < 5); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 914 | ASSERT_TRUE(tdd.complete); |
| 915 | |
| 916 | #if defined(__BIONIC__) |
| 917 | // Since bionic timers are implemented by creating a thread to handle the |
| 918 | // callback, verify that the thread actually completes. |
| 919 | cur_time = time(NULL); |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 920 | while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5); |
| 921 | ASSERT_EQ(-1, kill(tdd.tid, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 922 | ASSERT_ERRNO(ESRCH); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 923 | #endif |
| 924 | } |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 925 | |
Colin Cross | 35d469b | 2021-08-16 15:26:28 -0700 | [diff] [blame] | 926 | // Musl doesn't define __NR_clock_gettime on 32-bit architectures. |
| 927 | #if !defined(__NR_clock_gettime) |
| 928 | #define __NR_clock_gettime __NR_clock_gettime32 |
| 929 | #endif |
| 930 | |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 931 | TEST(time, clock_gettime) { |
| 932 | // Try to ensure that our vdso clock_gettime is working. |
Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 933 | timespec ts0; |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 934 | timespec ts1; |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 935 | timespec ts2; |
Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 936 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0)); |
| 937 | ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1)); |
| 938 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2)); |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 939 | |
Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 940 | // Check we have a nice monotonic timestamp sandwich. |
| 941 | ASSERT_LE(ts0.tv_sec, ts1.tv_sec); |
| 942 | if (ts0.tv_sec == ts1.tv_sec) { |
| 943 | ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec); |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 944 | } |
Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 945 | ASSERT_LE(ts1.tv_sec, ts2.tv_sec); |
| 946 | if (ts1.tv_sec == ts2.tv_sec) { |
| 947 | ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec); |
| 948 | } |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 949 | } |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 950 | |
Elliott Hughes | 3a8f75d | 2017-10-05 10:33:18 -0700 | [diff] [blame] | 951 | TEST(time, clock_gettime_CLOCK_REALTIME) { |
| 952 | timespec ts; |
| 953 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 954 | } |
| 955 | |
| 956 | TEST(time, clock_gettime_CLOCK_MONOTONIC) { |
| 957 | timespec ts; |
| 958 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 959 | } |
| 960 | |
| 961 | TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) { |
| 962 | timespec ts; |
| 963 | ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 964 | } |
| 965 | |
| 966 | TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) { |
| 967 | timespec ts; |
| 968 | ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts)); |
| 969 | } |
| 970 | |
| 971 | TEST(time, clock_gettime_CLOCK_BOOTTIME) { |
| 972 | timespec ts; |
| 973 | ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts)); |
| 974 | } |
| 975 | |
Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame] | 976 | TEST(time, clock_gettime_unknown) { |
| 977 | errno = 0; |
| 978 | timespec ts; |
| 979 | ASSERT_EQ(-1, clock_gettime(-1, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 980 | ASSERT_ERRNO(EINVAL); |
Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | TEST(time, clock_getres_CLOCK_REALTIME) { |
| 984 | timespec ts; |
| 985 | ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts)); |
| 986 | ASSERT_EQ(1, ts.tv_nsec); |
| 987 | ASSERT_EQ(0, ts.tv_sec); |
| 988 | } |
| 989 | |
| 990 | TEST(time, clock_getres_CLOCK_MONOTONIC) { |
| 991 | timespec ts; |
| 992 | ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts)); |
| 993 | ASSERT_EQ(1, ts.tv_nsec); |
| 994 | ASSERT_EQ(0, ts.tv_sec); |
| 995 | } |
| 996 | |
| 997 | TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) { |
| 998 | timespec ts; |
| 999 | ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1000 | } |
| 1001 | |
| 1002 | TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) { |
| 1003 | timespec ts; |
| 1004 | ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts)); |
| 1005 | } |
| 1006 | |
| 1007 | TEST(time, clock_getres_CLOCK_BOOTTIME) { |
| 1008 | timespec ts; |
| 1009 | ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts)); |
| 1010 | ASSERT_EQ(1, ts.tv_nsec); |
| 1011 | ASSERT_EQ(0, ts.tv_sec); |
| 1012 | } |
| 1013 | |
| 1014 | TEST(time, clock_getres_unknown) { |
| 1015 | errno = 0; |
| 1016 | timespec ts = { -1, -1 }; |
| 1017 | ASSERT_EQ(-1, clock_getres(-1, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1018 | ASSERT_ERRNO(EINVAL); |
Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame] | 1019 | ASSERT_EQ(-1, ts.tv_nsec); |
| 1020 | ASSERT_EQ(-1, ts.tv_sec); |
| 1021 | } |
| 1022 | |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 1023 | TEST(time, clock_getres_null_resolution) { |
| 1024 | ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, nullptr)); |
| 1025 | } |
| 1026 | |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 1027 | TEST(time, clock) { |
Giuliano Procida | ebc88d2 | 2021-04-07 14:25:13 +0100 | [diff] [blame] | 1028 | // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average. |
| 1029 | static const clock_t N = 5; |
| 1030 | static const clock_t mean_limit_ms = 10; |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1031 | clock_t t0 = clock(); |
Giuliano Procida | ebc88d2 | 2021-04-07 14:25:13 +0100 | [diff] [blame] | 1032 | for (size_t i = 0; i < N; ++i) { |
| 1033 | sleep(1); |
| 1034 | } |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1035 | clock_t t1 = clock(); |
Giuliano Procida | ebc88d2 | 2021-04-07 14:25:13 +0100 | [diff] [blame] | 1036 | ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000)); |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1037 | } |
| 1038 | |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1039 | static pid_t GetInvalidPid() { |
| 1040 | std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose}; |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1041 | long pid_max; |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1042 | fscanf(fp.get(), "%ld", &pid_max); |
| 1043 | return static_cast<pid_t>(pid_max + 1); |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1046 | TEST(time, clock_getcpuclockid_current) { |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1047 | clockid_t clockid; |
| 1048 | ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid)); |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1049 | timespec ts; |
| 1050 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1051 | } |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1052 | |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1053 | TEST(time, clock_getcpuclockid_parent) { |
| 1054 | clockid_t clockid; |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1055 | ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid)); |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1056 | timespec ts; |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1057 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1058 | } |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1059 | |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1060 | TEST(time, clock_getcpuclockid_ESRCH) { |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1061 | // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it. |
| 1062 | errno = 0; |
Mark Salyzyn | 71b81a2 | 2017-11-28 13:48:45 -0800 | [diff] [blame] | 1063 | // If this fails, your kernel needs commit e1b6b6ce to be backported. |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1064 | clockid_t clockid; |
Mark Salyzyn | 71b81a2 | 2017-11-28 13:48:45 -0800 | [diff] [blame] | 1065 | ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n" |
| 1066 | << "Please ensure that the following kernel patches or their replacements have been applied:\n" |
| 1067 | << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/" |
| 1068 | << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n" |
| 1069 | << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/" |
| 1070 | << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n"; |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1071 | ASSERT_ERRNO(0); |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1074 | TEST(time, clock_settime) { |
| 1075 | errno = 0; |
| 1076 | timespec ts; |
| 1077 | ASSERT_EQ(-1, clock_settime(-1, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1078 | ASSERT_ERRNO(EINVAL); |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1079 | } |
| 1080 | |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 1081 | TEST(time, clock_nanosleep_EINVAL) { |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1082 | timespec in; |
| 1083 | timespec out; |
| 1084 | ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out)); |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 1085 | } |
Greg Hackmann | d15dfb2 | 2016-03-26 11:37:55 -0700 | [diff] [blame] | 1086 | |
| 1087 | TEST(time, clock_nanosleep_thread_cputime_id) { |
| 1088 | timespec in; |
| 1089 | in.tv_sec = 1; |
| 1090 | in.tv_nsec = 0; |
| 1091 | ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr)); |
| 1092 | } |
Elliott Hughes | 1244370 | 2016-10-19 16:02:31 -0700 | [diff] [blame] | 1093 | |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 1094 | TEST(time, clock_nanosleep) { |
| 1095 | auto t0 = std::chrono::steady_clock::now(); |
| 1096 | const timespec ts = {.tv_nsec = 5000000}; |
| 1097 | ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr)); |
| 1098 | auto t1 = std::chrono::steady_clock::now(); |
| 1099 | ASSERT_GE(t1-t0, 5000000ns); |
| 1100 | } |
| 1101 | |
| 1102 | TEST(time, nanosleep) { |
| 1103 | auto t0 = std::chrono::steady_clock::now(); |
| 1104 | const timespec ts = {.tv_nsec = 5000000}; |
| 1105 | ASSERT_EQ(0, nanosleep(&ts, nullptr)); |
| 1106 | auto t1 = std::chrono::steady_clock::now(); |
| 1107 | ASSERT_GE(t1-t0, 5000000ns); |
| 1108 | } |
| 1109 | |
| 1110 | TEST(time, nanosleep_EINVAL) { |
| 1111 | timespec ts = {.tv_sec = -1}; |
| 1112 | errno = 0; |
| 1113 | ASSERT_EQ(-1, nanosleep(&ts, nullptr)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1114 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Elliott Hughes | 1244370 | 2016-10-19 16:02:31 -0700 | [diff] [blame] | 1117 | TEST(time, bug_31938693) { |
| 1118 | // User-visible symptoms in N: |
| 1119 | // http://b/31938693 |
| 1120 | // https://code.google.com/p/android/issues/detail?id=225132 |
| 1121 | |
| 1122 | // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug): |
| 1123 | // http://b/31848040 |
| 1124 | |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1125 | // This isn't a great test, because very few timezones were actually affected, and there's |
Elliott Hughes | 1244370 | 2016-10-19 16:02:31 -0700 | [diff] [blame] | 1126 | // no real logic to which ones were affected: it was just a coincidence of the data that came |
| 1127 | // after them in the tzdata file. |
| 1128 | |
| 1129 | time_t t = 1475619727; |
| 1130 | struct tm tm; |
| 1131 | |
| 1132 | setenv("TZ", "America/Los_Angeles", 1); |
| 1133 | tzset(); |
| 1134 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1135 | EXPECT_EQ(15, tm.tm_hour); |
| 1136 | |
| 1137 | setenv("TZ", "Europe/London", 1); |
| 1138 | tzset(); |
| 1139 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1140 | EXPECT_EQ(23, tm.tm_hour); |
| 1141 | |
| 1142 | setenv("TZ", "America/Atka", 1); |
| 1143 | tzset(); |
| 1144 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1145 | EXPECT_EQ(13, tm.tm_hour); |
| 1146 | |
| 1147 | setenv("TZ", "Pacific/Apia", 1); |
| 1148 | tzset(); |
| 1149 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1150 | EXPECT_EQ(12, tm.tm_hour); |
| 1151 | |
| 1152 | setenv("TZ", "Pacific/Honolulu", 1); |
| 1153 | tzset(); |
| 1154 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1155 | EXPECT_EQ(12, tm.tm_hour); |
| 1156 | |
| 1157 | setenv("TZ", "Asia/Magadan", 1); |
| 1158 | tzset(); |
| 1159 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1160 | EXPECT_EQ(9, tm.tm_hour); |
| 1161 | } |
Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 1162 | |
| 1163 | TEST(time, bug_31339449) { |
| 1164 | // POSIX says localtime acts as if it calls tzset. |
| 1165 | // tzset does two things: |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1166 | // 1. it sets the timezone ctime/localtime/mktime/strftime will use. |
Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 1167 | // 2. it sets the global `tzname`. |
| 1168 | // POSIX says localtime_r need not set `tzname` (2). |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1169 | // Q: should localtime_r set the timezone (1)? |
Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 1170 | // Upstream tzcode (and glibc) answer "no", everyone else answers "yes". |
| 1171 | |
| 1172 | // Pick a time, any time... |
| 1173 | time_t t = 1475619727; |
| 1174 | |
| 1175 | // Call tzset with a specific timezone. |
| 1176 | setenv("TZ", "America/Atka", 1); |
| 1177 | tzset(); |
| 1178 | |
| 1179 | // If we change the timezone and call localtime, localtime should use the new timezone. |
| 1180 | setenv("TZ", "America/Los_Angeles", 1); |
| 1181 | struct tm* tm_p = localtime(&t); |
| 1182 | EXPECT_EQ(15, tm_p->tm_hour); |
| 1183 | |
| 1184 | // Reset the timezone back. |
| 1185 | setenv("TZ", "America/Atka", 1); |
| 1186 | tzset(); |
| 1187 | |
| 1188 | #if defined(__BIONIC__) |
| 1189 | // If we change the timezone again and call localtime_r, localtime_r should use the new timezone. |
| 1190 | setenv("TZ", "America/Los_Angeles", 1); |
| 1191 | struct tm tm = {}; |
| 1192 | localtime_r(&t, &tm); |
| 1193 | EXPECT_EQ(15, tm.tm_hour); |
| 1194 | #else |
| 1195 | // The BSDs agree with us, but glibc gets this wrong. |
| 1196 | #endif |
| 1197 | } |
Elliott Hughes | 5a29d54 | 2017-12-07 16:05:57 -0800 | [diff] [blame] | 1198 | |
| 1199 | TEST(time, asctime) { |
| 1200 | const struct tm tm = {}; |
| 1201 | ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm)); |
| 1202 | } |
| 1203 | |
| 1204 | TEST(time, asctime_r) { |
| 1205 | const struct tm tm = {}; |
| 1206 | char buf[256]; |
| 1207 | ASSERT_EQ(buf, asctime_r(&tm, buf)); |
| 1208 | ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf); |
| 1209 | } |
| 1210 | |
| 1211 | TEST(time, ctime) { |
| 1212 | setenv("TZ", "UTC", 1); |
| 1213 | const time_t t = 0; |
| 1214 | ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t)); |
| 1215 | } |
| 1216 | |
| 1217 | TEST(time, ctime_r) { |
| 1218 | setenv("TZ", "UTC", 1); |
| 1219 | const time_t t = 0; |
| 1220 | char buf[256]; |
| 1221 | ASSERT_EQ(buf, ctime_r(&t, buf)); |
| 1222 | ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf); |
| 1223 | } |
Elliott Hughes | 81baaf2 | 2018-02-28 15:22:48 -0800 | [diff] [blame] | 1224 | |
| 1225 | // https://issuetracker.google.com/37128336 |
| 1226 | TEST(time, strftime_strptime_s) { |
| 1227 | char buf[32]; |
| 1228 | const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 }; |
| 1229 | |
| 1230 | setenv("TZ", "America/Los_Angeles", 1); |
| 1231 | strftime(buf, sizeof(buf), "<%s>", &tm0); |
| 1232 | EXPECT_STREQ("<378720000>", buf); |
| 1233 | |
| 1234 | setenv("TZ", "UTC", 1); |
| 1235 | strftime(buf, sizeof(buf), "<%s>", &tm0); |
| 1236 | EXPECT_STREQ("<378691200>", buf); |
| 1237 | |
Elliott Hughes | 81baaf2 | 2018-02-28 15:22:48 -0800 | [diff] [blame] | 1238 | setenv("TZ", "America/Los_Angeles", 1); |
| 1239 | tzset(); |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 1240 | struct tm tm = {}; |
Elliott Hughes | 81baaf2 | 2018-02-28 15:22:48 -0800 | [diff] [blame] | 1241 | char* p = strptime("378720000x", "%s", &tm); |
| 1242 | ASSERT_EQ('x', *p); |
| 1243 | EXPECT_EQ(0, tm.tm_sec); |
| 1244 | EXPECT_EQ(0, tm.tm_min); |
| 1245 | EXPECT_EQ(0, tm.tm_hour); |
| 1246 | EXPECT_EQ(1, tm.tm_mday); |
| 1247 | EXPECT_EQ(0, tm.tm_mon); |
| 1248 | EXPECT_EQ(82, tm.tm_year); |
| 1249 | EXPECT_EQ(5, tm.tm_wday); |
| 1250 | EXPECT_EQ(0, tm.tm_yday); |
| 1251 | EXPECT_EQ(0, tm.tm_isdst); |
| 1252 | |
| 1253 | setenv("TZ", "UTC", 1); |
| 1254 | tzset(); |
Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 1255 | tm = {}; |
Elliott Hughes | 81baaf2 | 2018-02-28 15:22:48 -0800 | [diff] [blame] | 1256 | p = strptime("378691200x", "%s", &tm); |
| 1257 | ASSERT_EQ('x', *p); |
| 1258 | EXPECT_EQ(0, tm.tm_sec); |
| 1259 | EXPECT_EQ(0, tm.tm_min); |
| 1260 | EXPECT_EQ(0, tm.tm_hour); |
| 1261 | EXPECT_EQ(1, tm.tm_mday); |
| 1262 | EXPECT_EQ(0, tm.tm_mon); |
| 1263 | EXPECT_EQ(82, tm.tm_year); |
| 1264 | EXPECT_EQ(5, tm.tm_wday); |
| 1265 | EXPECT_EQ(0, tm.tm_yday); |
| 1266 | EXPECT_EQ(0, tm.tm_isdst); |
| 1267 | } |
| 1268 | |
| 1269 | TEST(time, strptime_s_nothing) { |
| 1270 | struct tm tm; |
| 1271 | ASSERT_EQ(nullptr, strptime("x", "%s", &tm)); |
| 1272 | } |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1273 | |
| 1274 | TEST(time, timespec_get) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1275 | #if defined(__BIONIC__) |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1276 | timespec ts = {}; |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1277 | ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC)); |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1278 | ASSERT_EQ(TIME_MONOTONIC, timespec_get(&ts, TIME_MONOTONIC)); |
| 1279 | ASSERT_EQ(TIME_ACTIVE, timespec_get(&ts, TIME_ACTIVE)); |
| 1280 | ASSERT_EQ(TIME_THREAD_ACTIVE, timespec_get(&ts, TIME_THREAD_ACTIVE)); |
| 1281 | #else |
| 1282 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| 1283 | #endif |
| 1284 | } |
| 1285 | |
| 1286 | TEST(time, timespec_get_invalid) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1287 | #if defined(__BIONIC__) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1288 | timespec ts = {}; |
Elliott Hughes | 7db0a6c | 2023-05-03 15:37:46 -0700 | [diff] [blame] | 1289 | ASSERT_EQ(0, timespec_get(&ts, 123)); |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1290 | #else |
| 1291 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| 1292 | #endif |
| 1293 | } |
| 1294 | |
| 1295 | TEST(time, timespec_getres) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1296 | #if defined(__BIONIC__) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1297 | timespec ts = {}; |
| 1298 | ASSERT_EQ(TIME_UTC, timespec_getres(&ts, TIME_UTC)); |
| 1299 | ASSERT_EQ(1, ts.tv_nsec); |
| 1300 | ASSERT_EQ(0, ts.tv_sec); |
| 1301 | #else |
| 1302 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| 1303 | #endif |
| 1304 | } |
| 1305 | |
| 1306 | TEST(time, timespec_getres_invalid) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1307 | #if defined(__BIONIC__) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1308 | timespec ts = {}; |
| 1309 | ASSERT_EQ(0, timespec_getres(&ts, 123)); |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1310 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1311 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1312 | #endif |
| 1313 | } |
Elliott Hughes | 208fdd1 | 2020-06-05 17:19:00 -0700 | [diff] [blame] | 1314 | |
| 1315 | TEST(time, difftime) { |
| 1316 | ASSERT_EQ(1.0, difftime(1, 0)); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 1317 | ASSERT_EQ(-1.0, difftime(0, 1)); |
Elliott Hughes | 208fdd1 | 2020-06-05 17:19:00 -0700 | [diff] [blame] | 1318 | } |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1319 | |
| 1320 | TEST(time, tzfree_null) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1321 | #if defined(__BIONIC__) |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1322 | tzfree(nullptr); |
| 1323 | #else |
| 1324 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1325 | #endif |
| 1326 | } |
| 1327 | |
| 1328 | TEST(time, localtime_rz) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1329 | #if defined(__BIONIC__) |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1330 | setenv("TZ", "America/Los_Angeles", 1); |
| 1331 | tzset(); |
| 1332 | |
| 1333 | auto AssertTmEq = [](const struct tm& rhs, int hour) { |
| 1334 | ASSERT_EQ(93, rhs.tm_year); |
| 1335 | ASSERT_EQ(0, rhs.tm_mon); |
| 1336 | ASSERT_EQ(1, rhs.tm_mday); |
| 1337 | ASSERT_EQ(hour, rhs.tm_hour); |
| 1338 | ASSERT_EQ(0, rhs.tm_min); |
| 1339 | ASSERT_EQ(0, rhs.tm_sec); |
| 1340 | }; |
| 1341 | |
| 1342 | const time_t t = 725875200; |
| 1343 | |
| 1344 | // Spam localtime_r() while we use localtime_rz(). |
| 1345 | std::atomic<bool> done = false; |
| 1346 | std::thread thread{[&] { |
| 1347 | while (!done) { |
| 1348 | struct tm tm {}; |
| 1349 | ASSERT_EQ(&tm, localtime_r(&t, &tm)); |
| 1350 | AssertTmEq(tm, 0); |
| 1351 | } |
| 1352 | }}; |
| 1353 | |
| 1354 | struct tm tm; |
| 1355 | |
| 1356 | timezone_t london{tzalloc("Europe/London")}; |
| 1357 | tm = {}; |
| 1358 | ASSERT_EQ(&tm, localtime_rz(london, &t, &tm)); |
| 1359 | AssertTmEq(tm, 8); |
| 1360 | |
| 1361 | timezone_t seoul{tzalloc("Asia/Seoul")}; |
| 1362 | tm = {}; |
| 1363 | ASSERT_EQ(&tm, localtime_rz(seoul, &t, &tm)); |
| 1364 | AssertTmEq(tm, 17); |
| 1365 | |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1366 | // Just check that mktime()'s timezone didn't change. |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1367 | tm = {}; |
| 1368 | ASSERT_EQ(&tm, localtime_r(&t, &tm)); |
| 1369 | ASSERT_EQ(0, tm.tm_hour); |
| 1370 | AssertTmEq(tm, 0); |
| 1371 | |
| 1372 | done = true; |
| 1373 | thread.join(); |
| 1374 | |
| 1375 | tzfree(london); |
| 1376 | tzfree(seoul); |
| 1377 | #else |
| 1378 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1379 | #endif |
| 1380 | } |
| 1381 | |
| 1382 | TEST(time, mktime_z) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1383 | #if defined(__BIONIC__) |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1384 | setenv("TZ", "America/Los_Angeles", 1); |
| 1385 | tzset(); |
| 1386 | |
| 1387 | // Spam mktime() while we use mktime_z(). |
| 1388 | std::atomic<bool> done = false; |
| 1389 | std::thread thread{[&done] { |
| 1390 | while (!done) { |
| 1391 | struct tm tm { |
| 1392 | .tm_year = 93, .tm_mday = 1 |
| 1393 | }; |
| 1394 | ASSERT_EQ(725875200, mktime(&tm)); |
| 1395 | } |
| 1396 | }}; |
| 1397 | |
| 1398 | struct tm tm; |
| 1399 | |
| 1400 | timezone_t london{tzalloc("Europe/London")}; |
| 1401 | tm = {.tm_year = 93, .tm_mday = 1}; |
| 1402 | ASSERT_EQ(725846400, mktime_z(london, &tm)); |
| 1403 | |
| 1404 | timezone_t seoul{tzalloc("Asia/Seoul")}; |
| 1405 | tm = {.tm_year = 93, .tm_mday = 1}; |
| 1406 | ASSERT_EQ(725814000, mktime_z(seoul, &tm)); |
| 1407 | |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1408 | // Just check that mktime()'s timezone didn't change. |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1409 | tm = {.tm_year = 93, .tm_mday = 1}; |
| 1410 | ASSERT_EQ(725875200, mktime(&tm)); |
| 1411 | |
| 1412 | done = true; |
| 1413 | thread.join(); |
| 1414 | |
| 1415 | tzfree(london); |
| 1416 | tzfree(seoul); |
| 1417 | #else |
| 1418 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1419 | #endif |
| 1420 | } |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1421 | |
| 1422 | TEST(time, tzalloc_nullptr) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1423 | #if defined(__BIONIC__) |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1424 | // tzalloc(nullptr) returns the system timezone. |
| 1425 | timezone_t default_tz = tzalloc(nullptr); |
| 1426 | ASSERT_NE(nullptr, default_tz); |
| 1427 | |
| 1428 | // Check that mktime_z() with the default timezone matches mktime(). |
| 1429 | // This assumes that the system timezone doesn't change during the test, |
| 1430 | // but that should be unlikely, and we don't have much choice if we |
| 1431 | // want to write a test at all. |
| 1432 | // We unset $TZ before calling mktime() because mktime() honors $TZ. |
| 1433 | unsetenv("TZ"); |
| 1434 | struct tm tm = {.tm_year = 93, .tm_mday = 1}; |
| 1435 | time_t t = mktime(&tm); |
| 1436 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1437 | |
| 1438 | // Check that changing $TZ doesn't affect the tzalloc() default in |
| 1439 | // the same way it would the mktime() default. |
| 1440 | setenv("TZ", "America/Los_Angeles", 1); |
| 1441 | tzset(); |
| 1442 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1443 | |
| 1444 | setenv("TZ", "Europe/London", 1); |
| 1445 | tzset(); |
| 1446 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1447 | |
| 1448 | setenv("TZ", "Asia/Seoul", 1); |
| 1449 | tzset(); |
| 1450 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1451 | |
| 1452 | tzfree(default_tz); |
| 1453 | #else |
| 1454 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1455 | #endif |
| 1456 | } |
Elliott Hughes | 5ea305b | 2023-06-22 20:53:00 +0000 | [diff] [blame] | 1457 | |
| 1458 | TEST(time, tzalloc_unique_ptr) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1459 | #if defined(__BIONIC__) |
Elliott Hughes | 5ea305b | 2023-06-22 20:53:00 +0000 | [diff] [blame] | 1460 | std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), |
| 1461 | tzfree}; |
| 1462 | #else |
| 1463 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1464 | #endif |
| 1465 | } |