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