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