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