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