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