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