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