| 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 | |
| Elliott Hughes | dd56beb | 2025-08-26 14:53:48 +0000 | [diff] [blame] | 487 | TEST(time, strptime_s) { |
| 488 | setenv("TZ", "UTC", 1); |
| 489 | |
| 490 | struct tm tm; |
| 491 | |
| 492 | // 0 + 1 --- trivial. |
| 493 | tm = {}; |
| 494 | ASSERT_EQ('\0', *strptime("1", "%s", &tm)); |
| 495 | EXPECT_EQ(70, tm.tm_year); |
| 496 | EXPECT_EQ(0, tm.tm_mon); |
| 497 | EXPECT_EQ(1, tm.tm_mday); |
| 498 | EXPECT_EQ(0, tm.tm_hour); |
| 499 | EXPECT_EQ(0, tm.tm_min); |
| 500 | EXPECT_EQ(1, tm.tm_sec); |
| 501 | |
| 502 | // INT32_MAX (aka "time_t max" for ILP32). |
| 503 | tm = {}; |
| 504 | ASSERT_EQ('\0', *strptime("2147483647", "%s", &tm)); |
| 505 | EXPECT_EQ(138, tm.tm_year); |
| 506 | EXPECT_EQ(0, tm.tm_mon); |
| 507 | EXPECT_EQ(19, tm.tm_mday); |
| 508 | EXPECT_EQ(3, tm.tm_hour); |
| 509 | EXPECT_EQ(14, tm.tm_min); |
| 510 | EXPECT_EQ(7, tm.tm_sec); |
| 511 | |
| 512 | // INT32_MAX + 1 (aka overflow for ILP32). |
| 513 | // This should be easy to detect because it'll be negative. |
| 514 | tm = {}; |
| 515 | #if defined(__LP64__) |
| 516 | ASSERT_EQ('\0', *strptime("2147483648", "%s", &tm)); |
| 517 | #else |
| 518 | ASSERT_EQ(nullptr, strptime("2147483648", "%s", &tm)); |
| 519 | #endif |
| 520 | |
| 521 | // This wraps to 1 as an int32_t. |
| 522 | tm = {}; |
| 523 | #if defined(__LP64__) |
| 524 | ASSERT_EQ('\0', *strptime("4294967297", "%s", &tm)); |
| 525 | EXPECT_EQ(206, tm.tm_year); |
| 526 | EXPECT_EQ(1, tm.tm_mon); |
| 527 | EXPECT_EQ(7, tm.tm_mday); |
| 528 | EXPECT_EQ(6, tm.tm_hour); |
| 529 | EXPECT_EQ(28, tm.tm_min); |
| 530 | EXPECT_EQ(17, tm.tm_sec); |
| 531 | #else |
| 532 | ASSERT_EQ(nullptr, strptime("4294967297", "%s", &tm)); |
| 533 | #endif |
| 534 | |
| 535 | // INT64_MAX (aka "time_t max" for LP64). |
| 536 | // This actually fails for LP64 too... |
| 537 | // ...but in localtime_r() because the year is too large. |
| 538 | // (Wolfram Alpha says this is 21 times the age of the universe!) |
| 539 | tm = {}; |
| 540 | ASSERT_EQ(nullptr, strptime("9223372036854775807", "%s", &tm)); |
| 541 | } |
| 542 | |
| Elliott Hughes | a1fb15b | 2019-03-26 19:07:40 -0700 | [diff] [blame] | 543 | TEST(time, strptime_u) { |
| 544 | setenv("TZ", "UTC", 1); |
| 545 | |
| 546 | struct tm tm = {}; |
| 547 | ASSERT_EQ('\0', *strptime("2", "%u", &tm)); |
| 548 | EXPECT_EQ(2, tm.tm_wday); |
| 549 | } |
| 550 | |
| 551 | TEST(time, strptime_v) { |
| 552 | setenv("TZ", "UTC", 1); |
| 553 | |
| 554 | struct tm tm = {}; |
| 555 | ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm)); |
| 556 | EXPECT_EQ(80, tm.tm_year); |
| 557 | EXPECT_EQ(2, tm.tm_mon); |
| 558 | EXPECT_EQ(26, tm.tm_mday); |
| 559 | } |
| 560 | |
| 561 | TEST(time, strptime_V_G_g) { |
| 562 | setenv("TZ", "UTC", 1); |
| 563 | |
| 564 | // %V (ISO-8601 week number), %G (year of week number, without century), and |
| 565 | // %g (year of week number) have no effect when parsed, and are supported |
| 566 | // solely so that it's possible for strptime(3) to parse everything that |
| 567 | // strftime(3) can output. |
| 568 | struct tm tm = {}; |
| 569 | ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm)); |
| 570 | struct tm zero = {}; |
| 571 | EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0); |
| 572 | } |
| 573 | |
| Elliott Hughes | d065c04 | 2020-09-01 19:02:44 -0700 | [diff] [blame] | 574 | TEST(time, strptime_Z) { |
| 575 | #if defined(__BIONIC__) |
| 576 | // glibc doesn't handle %Z at all. |
| 577 | // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings |
| 578 | // are in the global `tzname` (which correspond to the current $TZ). |
| 579 | struct tm tm; |
| 580 | setenv("TZ", "Europe/Berlin", 1); |
| 581 | |
| 582 | // "GMT" always works. |
| 583 | tm = {}; |
| 584 | ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm)); |
| 585 | EXPECT_STREQ("GMT", tm.tm_zone); |
| 586 | EXPECT_EQ(0, tm.tm_isdst); |
| 587 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 588 | |
| 589 | // As does "UTC". |
| 590 | tm = {}; |
| 591 | ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm)); |
| 592 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 593 | EXPECT_EQ(0, tm.tm_isdst); |
| 594 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 595 | |
| 596 | // Europe/Berlin is known as "CET" when there's no DST. |
| 597 | tm = {}; |
| 598 | ASSERT_EQ('\0', *strptime("CET", "%Z", &tm)); |
| 599 | EXPECT_STREQ("CET", tm.tm_zone); |
| 600 | EXPECT_EQ(0, tm.tm_isdst); |
| 601 | EXPECT_EQ(3600, tm.tm_gmtoff); |
| 602 | |
| 603 | // Europe/Berlin is known as "CEST" when there's no DST. |
| 604 | tm = {}; |
| 605 | ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm)); |
| 606 | EXPECT_STREQ("CEST", tm.tm_zone); |
| 607 | EXPECT_EQ(1, tm.tm_isdst); |
| 608 | EXPECT_EQ(3600, tm.tm_gmtoff); |
| 609 | |
| Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 610 | // 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] | 611 | // abbreviations that are recognized. |
| 612 | tm = {}; |
| 613 | ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr); |
| 614 | #endif |
| 615 | } |
| 616 | |
| 617 | TEST(time, strptime_z) { |
| 618 | struct tm tm; |
| 619 | setenv("TZ", "Europe/Berlin", 1); |
| 620 | |
| 621 | // "UT" is what RFC822 called UTC. |
| 622 | tm = {}; |
| 623 | ASSERT_EQ('\0', *strptime("UT", "%z", &tm)); |
| 624 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 625 | EXPECT_EQ(0, tm.tm_isdst); |
| 626 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 627 | // "GMT" is RFC822's other name for UTC. |
| 628 | tm = {}; |
| 629 | ASSERT_EQ('\0', *strptime("GMT", "%z", &tm)); |
| 630 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 631 | EXPECT_EQ(0, tm.tm_isdst); |
| 632 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 633 | |
| 634 | // "Z" ("Zulu") is a synonym for UTC. |
| 635 | tm = {}; |
| 636 | ASSERT_EQ('\0', *strptime("Z", "%z", &tm)); |
| 637 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 638 | EXPECT_EQ(0, tm.tm_isdst); |
| 639 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 640 | |
| 641 | // "PST"/"PDT" and the other common US zone abbreviations are all supported. |
| 642 | tm = {}; |
| 643 | ASSERT_EQ('\0', *strptime("PST", "%z", &tm)); |
| 644 | EXPECT_STREQ("PST", tm.tm_zone); |
| 645 | EXPECT_EQ(0, tm.tm_isdst); |
| 646 | EXPECT_EQ(-28800, tm.tm_gmtoff); |
| 647 | tm = {}; |
| 648 | ASSERT_EQ('\0', *strptime("PDT", "%z", &tm)); |
| 649 | EXPECT_STREQ("PDT", tm.tm_zone); |
| 650 | EXPECT_EQ(1, tm.tm_isdst); |
| 651 | EXPECT_EQ(-25200, tm.tm_gmtoff); |
| 652 | |
| 653 | // +-hh |
| 654 | tm = {}; |
| 655 | ASSERT_EQ('\0', *strptime("+01", "%z", &tm)); |
| 656 | EXPECT_EQ(3600, tm.tm_gmtoff); |
| 657 | EXPECT_TRUE(tm.tm_zone == nullptr); |
| 658 | EXPECT_EQ(0, tm.tm_isdst); |
| 659 | // +-hhmm |
| 660 | tm = {}; |
| 661 | ASSERT_EQ('\0', *strptime("+0130", "%z", &tm)); |
| 662 | EXPECT_EQ(5400, tm.tm_gmtoff); |
| 663 | EXPECT_TRUE(tm.tm_zone == nullptr); |
| 664 | EXPECT_EQ(0, tm.tm_isdst); |
| 665 | // +-hh:mm |
| 666 | tm = {}; |
| 667 | ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm)); |
| 668 | EXPECT_EQ(5400, tm.tm_gmtoff); |
| 669 | EXPECT_TRUE(tm.tm_zone == nullptr); |
| 670 | EXPECT_EQ(0, tm.tm_isdst); |
| 671 | } |
| 672 | |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 673 | void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { |
| 674 | itimerspec ts; |
| 675 | ts.it_value.tv_sec = value_s; |
| 676 | ts.it_value.tv_nsec = value_ns; |
| 677 | ts.it_interval.tv_sec = interval_s; |
| 678 | ts.it_interval.tv_nsec = interval_ns; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 679 | ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr)); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 682 | static void NoOpNotifyFunction(sigval) { |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | TEST(time, timer_create) { |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 686 | sigevent se = {}; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 687 | se.sigev_notify = SIGEV_THREAD; |
| 688 | se.sigev_notify_function = NoOpNotifyFunction; |
| 689 | timer_t timer_id; |
| 690 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); |
| 691 | |
| Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 692 | pid_t pid = fork(); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 693 | ASSERT_NE(-1, pid) << strerror(errno); |
| 694 | |
| 695 | if (pid == 0) { |
| 696 | // Timers are not inherited by the child. |
| 697 | ASSERT_EQ(-1, timer_delete(timer_id)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 698 | ASSERT_ERRNO(EINVAL); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 699 | _exit(0); |
| 700 | } |
| 701 | |
| Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 702 | AssertChildExited(pid, 0); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 703 | |
| 704 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 705 | } |
| 706 | |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 707 | static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 708 | static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) { |
| 709 | ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; |
| 710 | ASSERT_EQ(SIGUSR1, signal_number); |
| 711 | } |
| 712 | |
| 713 | TEST(time, timer_create_SIGEV_SIGNAL) { |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 714 | sigevent se = {}; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 715 | se.sigev_notify = SIGEV_SIGNAL; |
| 716 | se.sigev_signo = SIGUSR1; |
| 717 | |
| 718 | timer_t timer_id; |
| 719 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); |
| 720 | |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 721 | timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 722 | ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler); |
| 723 | |
| 724 | ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); |
| 725 | |
| 726 | itimerspec ts; |
| 727 | ts.it_value.tv_sec = 0; |
| 728 | ts.it_value.tv_nsec = 1; |
| 729 | ts.it_interval.tv_sec = 0; |
| 730 | ts.it_interval.tv_nsec = 0; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 731 | ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr)); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 732 | |
| 733 | usleep(500000); |
| 734 | ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); |
| 735 | } |
| 736 | |
| 737 | struct Counter { |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 738 | private: |
| 739 | std::atomic<int> value; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 740 | timer_t timer_id; |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 741 | sigevent se; |
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 742 | bool timer_valid; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 743 | |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 744 | void Create() { |
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 745 | ASSERT_FALSE(timer_valid); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 746 | ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id)); |
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 747 | timer_valid = true; |
| 748 | } |
| 749 | |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 750 | public: |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 751 | explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) { |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 752 | 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] | 753 | Create(); |
| 754 | } |
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 755 | void DeleteTimer() { |
| 756 | ASSERT_TRUE(timer_valid); |
| 757 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 758 | timer_valid = false; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | ~Counter() { |
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 762 | if (timer_valid) { |
| 763 | DeleteTimer(); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 767 | int Value() const { |
| 768 | return value; |
| 769 | } |
| 770 | |
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 771 | void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { |
| 772 | ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns); |
| 773 | } |
| 774 | |
| 775 | bool ValueUpdated() { |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 776 | int current_value = value; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 777 | time_t start = time(nullptr); |
| 778 | while (current_value == value && (time(nullptr) - start) < 5) { |
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 779 | } |
| 780 | return current_value != value; |
| 781 | } |
| 782 | |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 783 | static void CountNotifyFunction(sigval value) { |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 784 | Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); |
| 785 | ++cd->value; |
| 786 | } |
| 787 | |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 788 | static void CountAndDisarmNotifyFunction(sigval value) { |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 789 | Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); |
| 790 | ++cd->value; |
| 791 | |
| 792 | // Setting the initial expiration time to 0 disarms the timer. |
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 793 | cd->SetTime(0, 0, 1, 0); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 794 | } |
| 795 | }; |
| 796 | |
| 797 | TEST(time, timer_settime_0) { |
| 798 | Counter counter(Counter::CountAndDisarmNotifyFunction); |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 799 | ASSERT_EQ(0, counter.Value()); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 800 | |
| Yabin Cui | bf572d9 | 2015-08-11 11:23:16 -0700 | [diff] [blame] | 801 | counter.SetTime(0, 500000000, 1, 0); |
| 802 | sleep(1); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 803 | |
| 804 | // 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] | 805 | ASSERT_EQ(1, counter.Value()); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | TEST(time, timer_settime_repeats) { |
| 809 | Counter counter(Counter::CountNotifyFunction); |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 810 | ASSERT_EQ(0, counter.Value()); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 811 | |
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 812 | counter.SetTime(0, 1, 0, 10); |
| 813 | ASSERT_TRUE(counter.ValueUpdated()); |
| 814 | ASSERT_TRUE(counter.ValueUpdated()); |
| 815 | ASSERT_TRUE(counter.ValueUpdated()); |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 816 | counter.DeleteTimer(); |
| 817 | // Add a sleep as other threads may be calling the callback function when the timer is deleted. |
| 818 | usleep(500000); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 819 | } |
| 820 | |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 821 | static int timer_create_NULL_signal_handler_invocation_count; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 822 | static void timer_create_NULL_signal_handler(int signal_number) { |
| 823 | ++timer_create_NULL_signal_handler_invocation_count; |
| 824 | ASSERT_EQ(SIGALRM, signal_number); |
| 825 | } |
| 826 | |
| 827 | TEST(time, timer_create_NULL) { |
| 828 | // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM. |
| 829 | timer_t timer_id; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 830 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id)); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 831 | |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 832 | timer_create_NULL_signal_handler_invocation_count = 0; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 833 | ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler); |
| 834 | |
| 835 | ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count); |
| 836 | |
| 837 | SetTime(timer_id, 0, 1, 0, 0); |
| 838 | usleep(500000); |
| 839 | |
| 840 | ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count); |
| 841 | } |
| 842 | |
| Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 843 | static int GetThreadCount() { |
| 844 | std::string status; |
| 845 | if (android::base::ReadFileToString("/proc/self/status", &status)) { |
| 846 | for (const auto& line : android::base::Split(status, "\n")) { |
| 847 | int thread_count; |
| 848 | if (sscanf(line.c_str(), "Threads: %d", &thread_count) == 1) { |
| 849 | return thread_count; |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | return -1; |
| 854 | } |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 855 | |
| Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 856 | TEST(time, timer_create_EINVAL) { |
| 857 | const clockid_t kInvalidClock = 16; |
| 858 | |
| 859 | // 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] | 860 | timer_t timer_id; |
| Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 861 | ASSERT_EQ(-1, timer_create(kInvalidClock, nullptr, &timer_id)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 862 | ASSERT_ERRNO(EINVAL); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 863 | |
| Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 864 | // A SIGEV_THREAD timer failure is more interesting because we have a thread |
| 865 | // to clean up (https://issuetracker.google.com/340125671). |
| 866 | sigevent se = {}; |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 867 | se.sigev_notify = SIGEV_THREAD; |
| 868 | se.sigev_notify_function = NoOpNotifyFunction; |
| Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 869 | ASSERT_EQ(-1, timer_create(kInvalidClock, &se, &timer_id)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 870 | ASSERT_ERRNO(EINVAL); |
| Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 871 | |
| 872 | // timer_create() doesn't guarantee that the thread will be dead _before_ |
| 873 | // it returns because that would require extra synchronization that's |
| 874 | // unnecessary in the normal (successful) case. A timeout here means we |
| 875 | // leaked a thread. |
| 876 | while (GetThreadCount() > 1) { |
| 877 | } |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 878 | } |
| 879 | |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 880 | TEST(time, timer_create_multiple) { |
| 881 | Counter counter1(Counter::CountNotifyFunction); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 882 | Counter counter2(Counter::CountNotifyFunction); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 883 | Counter counter3(Counter::CountNotifyFunction); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 884 | |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 885 | ASSERT_EQ(0, counter1.Value()); |
| 886 | ASSERT_EQ(0, counter2.Value()); |
| 887 | ASSERT_EQ(0, counter3.Value()); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 888 | |
| Yabin Cui | 410c1ad | 2015-06-18 16:19:02 -0700 | [diff] [blame] | 889 | counter2.SetTime(0, 500000000, 0, 0); |
| 890 | sleep(1); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 891 | |
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 892 | EXPECT_EQ(0, counter1.Value()); |
| 893 | EXPECT_EQ(1, counter2.Value()); |
| 894 | EXPECT_EQ(0, counter3.Value()); |
| 895 | } |
| 896 | |
| 897 | // Test to verify that disarming a repeatable timer disables the callbacks. |
| 898 | TEST(time, timer_disarm_terminates) { |
| 899 | Counter counter(Counter::CountNotifyFunction); |
| 900 | ASSERT_EQ(0, counter.Value()); |
| 901 | |
| 902 | counter.SetTime(0, 1, 0, 1); |
| 903 | ASSERT_TRUE(counter.ValueUpdated()); |
| 904 | ASSERT_TRUE(counter.ValueUpdated()); |
| 905 | ASSERT_TRUE(counter.ValueUpdated()); |
| 906 | |
| 907 | counter.SetTime(0, 0, 0, 0); |
| 908 | // Add a sleep as the kernel may have pending events when the timer is disarmed. |
| 909 | usleep(500000); |
| 910 | int value = counter.Value(); |
| 911 | usleep(500000); |
| 912 | |
| 913 | // Verify the counter has not been incremented. |
| 914 | ASSERT_EQ(value, counter.Value()); |
| 915 | } |
| 916 | |
| 917 | // Test to verify that deleting a repeatable timer disables the callbacks. |
| 918 | TEST(time, timer_delete_terminates) { |
| 919 | Counter counter(Counter::CountNotifyFunction); |
| 920 | ASSERT_EQ(0, counter.Value()); |
| 921 | |
| 922 | counter.SetTime(0, 1, 0, 1); |
| 923 | ASSERT_TRUE(counter.ValueUpdated()); |
| 924 | ASSERT_TRUE(counter.ValueUpdated()); |
| 925 | ASSERT_TRUE(counter.ValueUpdated()); |
| 926 | |
| 927 | counter.DeleteTimer(); |
| 928 | // Add a sleep as other threads may be calling the callback function when the timer is deleted. |
| 929 | usleep(500000); |
| 930 | int value = counter.Value(); |
| 931 | usleep(500000); |
| 932 | |
| 933 | // Verify the counter has not been incremented. |
| 934 | ASSERT_EQ(value, counter.Value()); |
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 935 | } |
| Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 936 | |
| 937 | struct TimerDeleteData { |
| 938 | timer_t timer_id; |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 939 | pid_t tid; |
| Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 940 | volatile bool complete; |
| 941 | }; |
| 942 | |
| Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 943 | static void TimerDeleteCallback(sigval value) { |
| Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 944 | TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr); |
| 945 | |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 946 | tdd->tid = gettid(); |
| Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 947 | timer_delete(tdd->timer_id); |
| 948 | tdd->complete = true; |
| 949 | } |
| 950 | |
| 951 | TEST(time, timer_delete_from_timer_thread) { |
| 952 | TimerDeleteData tdd; |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 953 | sigevent se = {}; |
| Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 954 | se.sigev_notify = SIGEV_THREAD; |
| 955 | se.sigev_notify_function = TimerDeleteCallback; |
| 956 | se.sigev_value.sival_ptr = &tdd; |
| 957 | |
| 958 | tdd.complete = false; |
| 959 | ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id)); |
| 960 | |
| 961 | itimerspec ts; |
| Christopher Ferris | 3da136a | 2015-02-18 17:11:47 -0800 | [diff] [blame] | 962 | ts.it_value.tv_sec = 1; |
| 963 | ts.it_value.tv_nsec = 0; |
| Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 964 | ts.it_interval.tv_sec = 0; |
| 965 | ts.it_interval.tv_nsec = 0; |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 966 | ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr)); |
| Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 967 | |
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 968 | time_t cur_time = time(nullptr); |
| 969 | while (!tdd.complete && (time(nullptr) - cur_time) < 5); |
| Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 970 | ASSERT_TRUE(tdd.complete); |
| 971 | |
| 972 | #if defined(__BIONIC__) |
| 973 | // Since bionic timers are implemented by creating a thread to handle the |
| 974 | // callback, verify that the thread actually completes. |
| 975 | cur_time = time(NULL); |
| Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 976 | while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5); |
| 977 | ASSERT_EQ(-1, kill(tdd.tid, 0)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 978 | ASSERT_ERRNO(ESRCH); |
| Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 979 | #endif |
| 980 | } |
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 981 | |
| Colin Cross | 35d469b | 2021-08-16 15:26:28 -0700 | [diff] [blame] | 982 | // Musl doesn't define __NR_clock_gettime on 32-bit architectures. |
| 983 | #if !defined(__NR_clock_gettime) |
| 984 | #define __NR_clock_gettime __NR_clock_gettime32 |
| 985 | #endif |
| 986 | |
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 987 | TEST(time, clock_gettime) { |
| 988 | // Try to ensure that our vdso clock_gettime is working. |
| Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 989 | timespec ts0; |
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 990 | timespec ts1; |
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 991 | timespec ts2; |
| Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 992 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0)); |
| 993 | ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1)); |
| 994 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2)); |
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 995 | |
| Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 996 | // Check we have a nice monotonic timestamp sandwich. |
| 997 | ASSERT_LE(ts0.tv_sec, ts1.tv_sec); |
| 998 | if (ts0.tv_sec == ts1.tv_sec) { |
| 999 | ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec); |
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 1000 | } |
| Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 1001 | ASSERT_LE(ts1.tv_sec, ts2.tv_sec); |
| 1002 | if (ts1.tv_sec == ts2.tv_sec) { |
| 1003 | ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec); |
| 1004 | } |
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 1005 | } |
| Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 1006 | |
| Elliott Hughes | 3a8f75d | 2017-10-05 10:33:18 -0700 | [diff] [blame] | 1007 | TEST(time, clock_gettime_CLOCK_REALTIME) { |
| 1008 | timespec ts; |
| 1009 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1010 | } |
| 1011 | |
| 1012 | TEST(time, clock_gettime_CLOCK_MONOTONIC) { |
| 1013 | timespec ts; |
| 1014 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1015 | } |
| 1016 | |
| 1017 | TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) { |
| 1018 | timespec ts; |
| 1019 | ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1020 | } |
| 1021 | |
| 1022 | TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) { |
| 1023 | timespec ts; |
| 1024 | ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts)); |
| 1025 | } |
| 1026 | |
| 1027 | TEST(time, clock_gettime_CLOCK_BOOTTIME) { |
| 1028 | timespec ts; |
| 1029 | ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts)); |
| 1030 | } |
| 1031 | |
| Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame] | 1032 | TEST(time, clock_gettime_unknown) { |
| 1033 | errno = 0; |
| 1034 | timespec ts; |
| 1035 | ASSERT_EQ(-1, clock_gettime(-1, &ts)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1036 | ASSERT_ERRNO(EINVAL); |
| Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | TEST(time, clock_getres_CLOCK_REALTIME) { |
| 1040 | timespec ts; |
| 1041 | ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts)); |
| 1042 | ASSERT_EQ(1, ts.tv_nsec); |
| 1043 | ASSERT_EQ(0, ts.tv_sec); |
| 1044 | } |
| 1045 | |
| 1046 | TEST(time, clock_getres_CLOCK_MONOTONIC) { |
| 1047 | timespec ts; |
| 1048 | ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts)); |
| 1049 | ASSERT_EQ(1, ts.tv_nsec); |
| 1050 | ASSERT_EQ(0, ts.tv_sec); |
| 1051 | } |
| 1052 | |
| 1053 | TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) { |
| 1054 | timespec ts; |
| 1055 | ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1056 | } |
| 1057 | |
| 1058 | TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) { |
| 1059 | timespec ts; |
| 1060 | ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts)); |
| 1061 | } |
| 1062 | |
| 1063 | TEST(time, clock_getres_CLOCK_BOOTTIME) { |
| 1064 | timespec ts; |
| 1065 | ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts)); |
| 1066 | ASSERT_EQ(1, ts.tv_nsec); |
| 1067 | ASSERT_EQ(0, ts.tv_sec); |
| 1068 | } |
| 1069 | |
| 1070 | TEST(time, clock_getres_unknown) { |
| 1071 | errno = 0; |
| 1072 | timespec ts = { -1, -1 }; |
| 1073 | ASSERT_EQ(-1, clock_getres(-1, &ts)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1074 | ASSERT_ERRNO(EINVAL); |
| Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame] | 1075 | ASSERT_EQ(-1, ts.tv_nsec); |
| 1076 | ASSERT_EQ(-1, ts.tv_sec); |
| 1077 | } |
| 1078 | |
| zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 1079 | TEST(time, clock_getres_null_resolution) { |
| 1080 | ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, nullptr)); |
| 1081 | } |
| 1082 | |
| Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 1083 | TEST(time, clock) { |
| Giuliano Procida | ebc88d2 | 2021-04-07 14:25:13 +0100 | [diff] [blame] | 1084 | // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average. |
| 1085 | static const clock_t N = 5; |
| 1086 | static const clock_t mean_limit_ms = 10; |
| Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1087 | clock_t t0 = clock(); |
| Giuliano Procida | ebc88d2 | 2021-04-07 14:25:13 +0100 | [diff] [blame] | 1088 | for (size_t i = 0; i < N; ++i) { |
| 1089 | sleep(1); |
| 1090 | } |
| Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1091 | clock_t t1 = clock(); |
| Giuliano Procida | ebc88d2 | 2021-04-07 14:25:13 +0100 | [diff] [blame] | 1092 | ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000)); |
| Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1093 | } |
| 1094 | |
| Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1095 | static pid_t GetInvalidPid() { |
| 1096 | 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] | 1097 | long pid_max; |
| Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1098 | fscanf(fp.get(), "%ld", &pid_max); |
| 1099 | return static_cast<pid_t>(pid_max + 1); |
| Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
| Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1102 | TEST(time, clock_getcpuclockid_current) { |
| Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1103 | clockid_t clockid; |
| 1104 | ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid)); |
| Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1105 | timespec ts; |
| 1106 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
| Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1107 | } |
| Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1108 | |
| Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1109 | TEST(time, clock_getcpuclockid_parent) { |
| 1110 | clockid_t clockid; |
| Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1111 | ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid)); |
| Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1112 | timespec ts; |
| Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1113 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
| Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1114 | } |
| Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1115 | |
| Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1116 | TEST(time, clock_getcpuclockid_ESRCH) { |
| Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1117 | // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it. |
| 1118 | errno = 0; |
| Mark Salyzyn | 71b81a2 | 2017-11-28 13:48:45 -0800 | [diff] [blame] | 1119 | // If this fails, your kernel needs commit e1b6b6ce to be backported. |
| Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1120 | clockid_t clockid; |
| Mark Salyzyn | 71b81a2 | 2017-11-28 13:48:45 -0800 | [diff] [blame] | 1121 | ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n" |
| 1122 | << "Please ensure that the following kernel patches or their replacements have been applied:\n" |
| 1123 | << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/" |
| 1124 | << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n" |
| 1125 | << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/" |
| 1126 | << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n"; |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1127 | ASSERT_ERRNO(0); |
| Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
| Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1130 | TEST(time, clock_settime) { |
| 1131 | errno = 0; |
| 1132 | timespec ts; |
| 1133 | ASSERT_EQ(-1, clock_settime(-1, &ts)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1134 | ASSERT_ERRNO(EINVAL); |
| Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1135 | } |
| 1136 | |
| Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 1137 | TEST(time, clock_nanosleep_EINVAL) { |
| Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1138 | timespec in; |
| 1139 | timespec out; |
| 1140 | ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out)); |
| Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 1141 | } |
| Greg Hackmann | d15dfb2 | 2016-03-26 11:37:55 -0700 | [diff] [blame] | 1142 | |
| 1143 | TEST(time, clock_nanosleep_thread_cputime_id) { |
| 1144 | timespec in; |
| 1145 | in.tv_sec = 1; |
| 1146 | in.tv_nsec = 0; |
| 1147 | ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr)); |
| 1148 | } |
| Elliott Hughes | 1244370 | 2016-10-19 16:02:31 -0700 | [diff] [blame] | 1149 | |
| Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 1150 | TEST(time, clock_nanosleep) { |
| 1151 | auto t0 = std::chrono::steady_clock::now(); |
| 1152 | const timespec ts = {.tv_nsec = 5000000}; |
| 1153 | ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr)); |
| 1154 | auto t1 = std::chrono::steady_clock::now(); |
| 1155 | ASSERT_GE(t1-t0, 5000000ns); |
| 1156 | } |
| 1157 | |
| 1158 | TEST(time, nanosleep) { |
| 1159 | auto t0 = std::chrono::steady_clock::now(); |
| 1160 | const timespec ts = {.tv_nsec = 5000000}; |
| 1161 | ASSERT_EQ(0, nanosleep(&ts, nullptr)); |
| 1162 | auto t1 = std::chrono::steady_clock::now(); |
| 1163 | ASSERT_GE(t1-t0, 5000000ns); |
| 1164 | } |
| 1165 | |
| 1166 | TEST(time, nanosleep_EINVAL) { |
| 1167 | timespec ts = {.tv_sec = -1}; |
| 1168 | errno = 0; |
| 1169 | ASSERT_EQ(-1, nanosleep(&ts, nullptr)); |
| Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1170 | ASSERT_ERRNO(EINVAL); |
| Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| Elliott Hughes | 1244370 | 2016-10-19 16:02:31 -0700 | [diff] [blame] | 1173 | TEST(time, bug_31938693) { |
| 1174 | // User-visible symptoms in N: |
| 1175 | // http://b/31938693 |
| 1176 | // https://code.google.com/p/android/issues/detail?id=225132 |
| 1177 | |
| 1178 | // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug): |
| 1179 | // http://b/31848040 |
| 1180 | |
| Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1181 | // 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] | 1182 | // no real logic to which ones were affected: it was just a coincidence of the data that came |
| 1183 | // after them in the tzdata file. |
| 1184 | |
| 1185 | time_t t = 1475619727; |
| 1186 | struct tm tm; |
| 1187 | |
| 1188 | setenv("TZ", "America/Los_Angeles", 1); |
| 1189 | tzset(); |
| 1190 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1191 | EXPECT_EQ(15, tm.tm_hour); |
| 1192 | |
| 1193 | setenv("TZ", "Europe/London", 1); |
| 1194 | tzset(); |
| 1195 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1196 | EXPECT_EQ(23, tm.tm_hour); |
| 1197 | |
| 1198 | setenv("TZ", "America/Atka", 1); |
| 1199 | tzset(); |
| 1200 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1201 | EXPECT_EQ(13, tm.tm_hour); |
| 1202 | |
| 1203 | setenv("TZ", "Pacific/Apia", 1); |
| 1204 | tzset(); |
| 1205 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1206 | EXPECT_EQ(12, tm.tm_hour); |
| 1207 | |
| 1208 | setenv("TZ", "Pacific/Honolulu", 1); |
| 1209 | tzset(); |
| 1210 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1211 | EXPECT_EQ(12, tm.tm_hour); |
| 1212 | |
| 1213 | setenv("TZ", "Asia/Magadan", 1); |
| 1214 | tzset(); |
| 1215 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1216 | EXPECT_EQ(9, tm.tm_hour); |
| 1217 | } |
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 1218 | |
| 1219 | TEST(time, bug_31339449) { |
| 1220 | // POSIX says localtime acts as if it calls tzset. |
| 1221 | // tzset does two things: |
| Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1222 | // 1. it sets the timezone ctime/localtime/mktime/strftime will use. |
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 1223 | // 2. it sets the global `tzname`. |
| 1224 | // POSIX says localtime_r need not set `tzname` (2). |
| Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1225 | // Q: should localtime_r set the timezone (1)? |
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 1226 | // Upstream tzcode (and glibc) answer "no", everyone else answers "yes". |
| 1227 | |
| 1228 | // Pick a time, any time... |
| 1229 | time_t t = 1475619727; |
| 1230 | |
| 1231 | // Call tzset with a specific timezone. |
| 1232 | setenv("TZ", "America/Atka", 1); |
| 1233 | tzset(); |
| 1234 | |
| 1235 | // If we change the timezone and call localtime, localtime should use the new timezone. |
| 1236 | setenv("TZ", "America/Los_Angeles", 1); |
| 1237 | struct tm* tm_p = localtime(&t); |
| 1238 | EXPECT_EQ(15, tm_p->tm_hour); |
| 1239 | |
| 1240 | // Reset the timezone back. |
| 1241 | setenv("TZ", "America/Atka", 1); |
| 1242 | tzset(); |
| 1243 | |
| 1244 | #if defined(__BIONIC__) |
| 1245 | // If we change the timezone again and call localtime_r, localtime_r should use the new timezone. |
| 1246 | setenv("TZ", "America/Los_Angeles", 1); |
| 1247 | struct tm tm = {}; |
| 1248 | localtime_r(&t, &tm); |
| 1249 | EXPECT_EQ(15, tm.tm_hour); |
| 1250 | #else |
| 1251 | // The BSDs agree with us, but glibc gets this wrong. |
| 1252 | #endif |
| 1253 | } |
| Elliott Hughes | 5a29d54 | 2017-12-07 16:05:57 -0800 | [diff] [blame] | 1254 | |
| 1255 | TEST(time, asctime) { |
| Elliott Hughes | b891dda | 2025-08-27 07:19:07 -0700 | [diff] [blame] | 1256 | const tm tm = {}; |
| Elliott Hughes | 5a29d54 | 2017-12-07 16:05:57 -0800 | [diff] [blame] | 1257 | ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm)); |
| 1258 | } |
| 1259 | |
| 1260 | TEST(time, asctime_r) { |
| Elliott Hughes | b891dda | 2025-08-27 07:19:07 -0700 | [diff] [blame] | 1261 | const tm tm = {}; |
| Elliott Hughes | 5a29d54 | 2017-12-07 16:05:57 -0800 | [diff] [blame] | 1262 | char buf[256]; |
| 1263 | ASSERT_EQ(buf, asctime_r(&tm, buf)); |
| 1264 | ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf); |
| 1265 | } |
| 1266 | |
| Elliott Hughes | b891dda | 2025-08-27 07:19:07 -0700 | [diff] [blame] | 1267 | TEST(time, asctime_nullptr) { |
| 1268 | tm* smuggled_null = nullptr; |
| 1269 | char buf[256]; |
| 1270 | // I'd argue that the glibc behavior is more reasonable, |
| 1271 | // but traditionally we've had the BSD behavior. |
| 1272 | errno = 0; |
| 1273 | #if defined(__GLIBC__) |
| 1274 | ASSERT_EQ(nullptr, asctime_r(smuggled_null, buf)); |
| 1275 | #else |
| 1276 | ASSERT_EQ(buf, asctime_r(smuggled_null, buf)); |
| 1277 | ASSERT_STREQ("??? ??? ?? ??:??:?? ????\n", buf); |
| 1278 | #endif |
| 1279 | ASSERT_ERRNO(EINVAL); |
| 1280 | } |
| 1281 | |
| 1282 | TEST(time, asctime_bad_wday) { |
| 1283 | // This is undefined behavior, but our traditional behavior is to substitute "???". |
| 1284 | tm tm = { .tm_wday = -1 }; |
| 1285 | char buf[256]; |
| 1286 | ASSERT_EQ(buf, asctime_r(&tm, buf)); |
| 1287 | ASSERT_STREQ("??? Jan 0 00:00:00 1900\n", buf); |
| 1288 | tm.tm_wday = 7; |
| 1289 | ASSERT_EQ(buf, asctime_r(&tm, buf)); |
| 1290 | ASSERT_STREQ("??? Jan 0 00:00:00 1900\n", buf); |
| 1291 | } |
| 1292 | |
| 1293 | TEST(time, asctime_bad_mon) { |
| 1294 | // This is undefined behavior, but our traditional behavior is to substitute "???". |
| 1295 | tm tm = { .tm_mon = -1 }; |
| 1296 | char buf[256]; |
| 1297 | ASSERT_EQ(buf, asctime_r(&tm, buf)); |
| 1298 | ASSERT_STREQ("Sun ??? 0 00:00:00 1900\n", buf); |
| 1299 | tm.tm_mon = 12; |
| 1300 | ASSERT_EQ(buf, asctime_r(&tm, buf)); |
| 1301 | ASSERT_STREQ("Sun ??? 0 00:00:00 1900\n", buf); |
| 1302 | } |
| 1303 | |
| 1304 | TEST(time, asctime_bad_year) { |
| 1305 | // This is undefined behavior, but our traditional behavior is to return NULL/EOVERFLOW. |
| 1306 | tm tm = { .tm_year = 99999 }; |
| 1307 | char buf[256]; |
| 1308 | errno = 0; |
| 1309 | ASSERT_EQ(nullptr, asctime_r(&tm, buf)); |
| 1310 | ASSERT_ERRNO(EOVERFLOW); |
| 1311 | } |
| 1312 | |
| Elliott Hughes | 5a29d54 | 2017-12-07 16:05:57 -0800 | [diff] [blame] | 1313 | TEST(time, ctime) { |
| 1314 | setenv("TZ", "UTC", 1); |
| 1315 | const time_t t = 0; |
| 1316 | ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t)); |
| 1317 | } |
| 1318 | |
| 1319 | TEST(time, ctime_r) { |
| 1320 | setenv("TZ", "UTC", 1); |
| 1321 | const time_t t = 0; |
| 1322 | char buf[256]; |
| 1323 | ASSERT_EQ(buf, ctime_r(&t, buf)); |
| 1324 | ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf); |
| 1325 | } |
| Elliott Hughes | 81baaf2 | 2018-02-28 15:22:48 -0800 | [diff] [blame] | 1326 | |
| 1327 | // https://issuetracker.google.com/37128336 |
| 1328 | TEST(time, strftime_strptime_s) { |
| 1329 | char buf[32]; |
| 1330 | const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 }; |
| 1331 | |
| 1332 | setenv("TZ", "America/Los_Angeles", 1); |
| 1333 | strftime(buf, sizeof(buf), "<%s>", &tm0); |
| 1334 | EXPECT_STREQ("<378720000>", buf); |
| 1335 | |
| 1336 | setenv("TZ", "UTC", 1); |
| 1337 | strftime(buf, sizeof(buf), "<%s>", &tm0); |
| 1338 | EXPECT_STREQ("<378691200>", buf); |
| 1339 | |
| Elliott Hughes | 81baaf2 | 2018-02-28 15:22:48 -0800 | [diff] [blame] | 1340 | setenv("TZ", "America/Los_Angeles", 1); |
| 1341 | tzset(); |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 1342 | struct tm tm = {}; |
| Elliott Hughes | 81baaf2 | 2018-02-28 15:22:48 -0800 | [diff] [blame] | 1343 | char* p = strptime("378720000x", "%s", &tm); |
| 1344 | ASSERT_EQ('x', *p); |
| 1345 | EXPECT_EQ(0, tm.tm_sec); |
| 1346 | EXPECT_EQ(0, tm.tm_min); |
| 1347 | EXPECT_EQ(0, tm.tm_hour); |
| 1348 | EXPECT_EQ(1, tm.tm_mday); |
| 1349 | EXPECT_EQ(0, tm.tm_mon); |
| 1350 | EXPECT_EQ(82, tm.tm_year); |
| 1351 | EXPECT_EQ(5, tm.tm_wday); |
| 1352 | EXPECT_EQ(0, tm.tm_yday); |
| 1353 | EXPECT_EQ(0, tm.tm_isdst); |
| 1354 | |
| 1355 | setenv("TZ", "UTC", 1); |
| 1356 | tzset(); |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 1357 | tm = {}; |
| Elliott Hughes | 81baaf2 | 2018-02-28 15:22:48 -0800 | [diff] [blame] | 1358 | p = strptime("378691200x", "%s", &tm); |
| 1359 | ASSERT_EQ('x', *p); |
| 1360 | EXPECT_EQ(0, tm.tm_sec); |
| 1361 | EXPECT_EQ(0, tm.tm_min); |
| 1362 | EXPECT_EQ(0, tm.tm_hour); |
| 1363 | EXPECT_EQ(1, tm.tm_mday); |
| 1364 | EXPECT_EQ(0, tm.tm_mon); |
| 1365 | EXPECT_EQ(82, tm.tm_year); |
| 1366 | EXPECT_EQ(5, tm.tm_wday); |
| 1367 | EXPECT_EQ(0, tm.tm_yday); |
| 1368 | EXPECT_EQ(0, tm.tm_isdst); |
| 1369 | } |
| 1370 | |
| 1371 | TEST(time, strptime_s_nothing) { |
| 1372 | struct tm tm; |
| 1373 | ASSERT_EQ(nullptr, strptime("x", "%s", &tm)); |
| 1374 | } |
| Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1375 | |
| 1376 | TEST(time, timespec_get) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1377 | #if defined(__BIONIC__) |
| Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1378 | timespec ts = {}; |
| Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1379 | ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC)); |
| Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1380 | ASSERT_EQ(TIME_MONOTONIC, timespec_get(&ts, TIME_MONOTONIC)); |
| 1381 | ASSERT_EQ(TIME_ACTIVE, timespec_get(&ts, TIME_ACTIVE)); |
| 1382 | ASSERT_EQ(TIME_THREAD_ACTIVE, timespec_get(&ts, TIME_THREAD_ACTIVE)); |
| 1383 | #else |
| 1384 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| 1385 | #endif |
| 1386 | } |
| 1387 | |
| 1388 | TEST(time, timespec_get_invalid) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1389 | #if defined(__BIONIC__) |
| Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1390 | timespec ts = {}; |
| Elliott Hughes | 7db0a6c | 2023-05-03 15:37:46 -0700 | [diff] [blame] | 1391 | ASSERT_EQ(0, timespec_get(&ts, 123)); |
| Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1392 | #else |
| 1393 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| 1394 | #endif |
| 1395 | } |
| 1396 | |
| 1397 | TEST(time, timespec_getres) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1398 | #if defined(__BIONIC__) |
| Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1399 | timespec ts = {}; |
| 1400 | ASSERT_EQ(TIME_UTC, timespec_getres(&ts, TIME_UTC)); |
| 1401 | ASSERT_EQ(1, ts.tv_nsec); |
| 1402 | ASSERT_EQ(0, ts.tv_sec); |
| 1403 | #else |
| 1404 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| 1405 | #endif |
| 1406 | } |
| 1407 | |
| 1408 | TEST(time, timespec_getres_invalid) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1409 | #if defined(__BIONIC__) |
| Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1410 | timespec ts = {}; |
| 1411 | ASSERT_EQ(0, timespec_getres(&ts, 123)); |
| Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1412 | #else |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1413 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1414 | #endif |
| 1415 | } |
| Elliott Hughes | 208fdd1 | 2020-06-05 17:19:00 -0700 | [diff] [blame] | 1416 | |
| 1417 | TEST(time, difftime) { |
| 1418 | ASSERT_EQ(1.0, difftime(1, 0)); |
| Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 1419 | ASSERT_EQ(-1.0, difftime(0, 1)); |
| Elliott Hughes | 208fdd1 | 2020-06-05 17:19:00 -0700 | [diff] [blame] | 1420 | } |
| Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1421 | |
| 1422 | TEST(time, tzfree_null) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1423 | #if defined(__BIONIC__) |
| Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1424 | tzfree(nullptr); |
| 1425 | #else |
| 1426 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1427 | #endif |
| 1428 | } |
| 1429 | |
| 1430 | TEST(time, localtime_rz) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1431 | #if defined(__BIONIC__) |
| Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1432 | setenv("TZ", "America/Los_Angeles", 1); |
| 1433 | tzset(); |
| 1434 | |
| 1435 | auto AssertTmEq = [](const struct tm& rhs, int hour) { |
| 1436 | ASSERT_EQ(93, rhs.tm_year); |
| 1437 | ASSERT_EQ(0, rhs.tm_mon); |
| 1438 | ASSERT_EQ(1, rhs.tm_mday); |
| 1439 | ASSERT_EQ(hour, rhs.tm_hour); |
| 1440 | ASSERT_EQ(0, rhs.tm_min); |
| 1441 | ASSERT_EQ(0, rhs.tm_sec); |
| 1442 | }; |
| 1443 | |
| 1444 | const time_t t = 725875200; |
| 1445 | |
| 1446 | // Spam localtime_r() while we use localtime_rz(). |
| 1447 | std::atomic<bool> done = false; |
| 1448 | std::thread thread{[&] { |
| 1449 | while (!done) { |
| 1450 | struct tm tm {}; |
| 1451 | ASSERT_EQ(&tm, localtime_r(&t, &tm)); |
| 1452 | AssertTmEq(tm, 0); |
| 1453 | } |
| 1454 | }}; |
| 1455 | |
| 1456 | struct tm tm; |
| 1457 | |
| 1458 | timezone_t london{tzalloc("Europe/London")}; |
| 1459 | tm = {}; |
| 1460 | ASSERT_EQ(&tm, localtime_rz(london, &t, &tm)); |
| 1461 | AssertTmEq(tm, 8); |
| 1462 | |
| 1463 | timezone_t seoul{tzalloc("Asia/Seoul")}; |
| 1464 | tm = {}; |
| 1465 | ASSERT_EQ(&tm, localtime_rz(seoul, &t, &tm)); |
| 1466 | AssertTmEq(tm, 17); |
| 1467 | |
| Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1468 | // Just check that mktime()'s timezone didn't change. |
| Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1469 | tm = {}; |
| 1470 | ASSERT_EQ(&tm, localtime_r(&t, &tm)); |
| 1471 | ASSERT_EQ(0, tm.tm_hour); |
| 1472 | AssertTmEq(tm, 0); |
| 1473 | |
| 1474 | done = true; |
| 1475 | thread.join(); |
| 1476 | |
| 1477 | tzfree(london); |
| 1478 | tzfree(seoul); |
| 1479 | #else |
| 1480 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1481 | #endif |
| 1482 | } |
| 1483 | |
| 1484 | TEST(time, mktime_z) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1485 | #if defined(__BIONIC__) |
| Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1486 | setenv("TZ", "America/Los_Angeles", 1); |
| 1487 | tzset(); |
| 1488 | |
| 1489 | // Spam mktime() while we use mktime_z(). |
| 1490 | std::atomic<bool> done = false; |
| 1491 | std::thread thread{[&done] { |
| 1492 | while (!done) { |
| 1493 | struct tm tm { |
| 1494 | .tm_year = 93, .tm_mday = 1 |
| 1495 | }; |
| 1496 | ASSERT_EQ(725875200, mktime(&tm)); |
| 1497 | } |
| 1498 | }}; |
| 1499 | |
| 1500 | struct tm tm; |
| 1501 | |
| 1502 | timezone_t london{tzalloc("Europe/London")}; |
| 1503 | tm = {.tm_year = 93, .tm_mday = 1}; |
| 1504 | ASSERT_EQ(725846400, mktime_z(london, &tm)); |
| 1505 | |
| 1506 | timezone_t seoul{tzalloc("Asia/Seoul")}; |
| 1507 | tm = {.tm_year = 93, .tm_mday = 1}; |
| 1508 | ASSERT_EQ(725814000, mktime_z(seoul, &tm)); |
| 1509 | |
| Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1510 | // Just check that mktime()'s timezone didn't change. |
| Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1511 | tm = {.tm_year = 93, .tm_mday = 1}; |
| 1512 | ASSERT_EQ(725875200, mktime(&tm)); |
| 1513 | |
| 1514 | done = true; |
| 1515 | thread.join(); |
| 1516 | |
| 1517 | tzfree(london); |
| 1518 | tzfree(seoul); |
| 1519 | #else |
| 1520 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1521 | #endif |
| 1522 | } |
| Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1523 | |
| 1524 | TEST(time, tzalloc_nullptr) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1525 | #if defined(__BIONIC__) |
| Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1526 | // tzalloc(nullptr) returns the system timezone. |
| 1527 | timezone_t default_tz = tzalloc(nullptr); |
| 1528 | ASSERT_NE(nullptr, default_tz); |
| 1529 | |
| 1530 | // Check that mktime_z() with the default timezone matches mktime(). |
| 1531 | // This assumes that the system timezone doesn't change during the test, |
| 1532 | // but that should be unlikely, and we don't have much choice if we |
| 1533 | // want to write a test at all. |
| 1534 | // We unset $TZ before calling mktime() because mktime() honors $TZ. |
| 1535 | unsetenv("TZ"); |
| 1536 | struct tm tm = {.tm_year = 93, .tm_mday = 1}; |
| 1537 | time_t t = mktime(&tm); |
| 1538 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1539 | |
| 1540 | // Check that changing $TZ doesn't affect the tzalloc() default in |
| 1541 | // the same way it would the mktime() default. |
| 1542 | setenv("TZ", "America/Los_Angeles", 1); |
| 1543 | tzset(); |
| 1544 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1545 | |
| 1546 | setenv("TZ", "Europe/London", 1); |
| 1547 | tzset(); |
| 1548 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1549 | |
| 1550 | setenv("TZ", "Asia/Seoul", 1); |
| 1551 | tzset(); |
| 1552 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1553 | |
| 1554 | tzfree(default_tz); |
| 1555 | #else |
| 1556 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1557 | #endif |
| 1558 | } |
| Elliott Hughes | 5ea305b | 2023-06-22 20:53:00 +0000 | [diff] [blame] | 1559 | |
| 1560 | TEST(time, tzalloc_unique_ptr) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1561 | #if defined(__BIONIC__) |
| Elliott Hughes | 5ea305b | 2023-06-22 20:53:00 +0000 | [diff] [blame] | 1562 | std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), |
| 1563 | tzfree}; |
| 1564 | #else |
| 1565 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1566 | #endif |
| 1567 | } |