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