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 17 #include <time.h> 18 19 #include <errno.h> 20 #include <gtest/gtest.h> 21 #include <pthread.h> 22 #include <signal.h> 23 #include <sys/syscall.h> 24 #include <sys/types.h> 25 #include <sys/wait.h> 26 #include <unistd.h> 27 #include <atomic> 28 29 #include "ScopedSignalHandler.h" 30 #include "utils.h" 31 32 #include "private/bionic_constants.h" 33 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 59 TEST(time, gmtime) { 60 time_t t = 0; 61 tm* broken_down = gmtime(&t); 62 ASSERT_TRUE(broken_down != NULL); 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 } 70 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 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(); 89 if (original_tz != NULL) { 90 setenv("TZ", original_tz, 1); 91 } 92 tzset(); 93 return NULL; 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 100 pthread_attr_t a; 101 ASSERT_EQ(0, pthread_attr_init(&a)); 102 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN)); 103 104 pthread_t t; 105 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, NULL)); 106 ASSERT_EQ(0, pthread_join(t, nullptr)); 107 } 108 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 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 140 #if !defined(__LP64__) 141 // 32-bit bionic stupidly had a signed 32-bit time_t. 142 ASSERT_EQ(-1, mktime(&t)); 143 ASSERT_EQ(EOVERFLOW, errno); 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(); 150 errno = 0; 151 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t)); 152 ASSERT_EQ(0, errno); 153 154 setenv("TZ", "UTC", 1); 155 tzset(); 156 errno = 0; 157 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t)); 158 ASSERT_EQ(0, errno); 159 #endif 160 } 161 162 TEST(time, mktime_EOVERFLOW) { 163 struct tm t; 164 memset(&t, 0, sizeof(tm)); 165 166 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow. 167 t.tm_year = 2016 - 1900; 168 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 176 // This will overflow for LP32 or LP64. 177 t.tm_year = INT_MAX; 178 179 errno = 0; 180 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t)); 181 ASSERT_EQ(EOVERFLOW, errno); 182 } 183 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 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 244 TEST(time, strftime_l) { 245 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0); 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 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 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 299 void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { 300 itimerspec ts; 301 ts.it_value.tv_sec = value_s; 302 ts.it_value.tv_nsec = value_ns; 303 ts.it_interval.tv_sec = interval_s; 304 ts.it_interval.tv_nsec = interval_ns; 305 ASSERT_EQ(0, timer_settime(t, 0, &ts, NULL)); 306 } 307 308 static void NoOpNotifyFunction(sigval_t) { 309 } 310 311 TEST(time, timer_create) { 312 sigevent_t se; 313 memset(&se, 0, sizeof(se)); 314 se.sigev_notify = SIGEV_THREAD; 315 se.sigev_notify_function = NoOpNotifyFunction; 316 timer_t timer_id; 317 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); 318 319 pid_t pid = fork(); 320 ASSERT_NE(-1, pid) << strerror(errno); 321 322 if (pid == 0) { 323 // Timers are not inherited by the child. 324 ASSERT_EQ(-1, timer_delete(timer_id)); 325 ASSERT_EQ(EINVAL, errno); 326 _exit(0); 327 } 328 329 AssertChildExited(pid, 0); 330 331 ASSERT_EQ(0, timer_delete(timer_id)); 332 } 333 334 static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; 335 static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) { 336 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; 337 ASSERT_EQ(SIGUSR1, signal_number); 338 } 339 340 TEST(time, timer_create_SIGEV_SIGNAL) { 341 sigevent_t se; 342 memset(&se, 0, sizeof(se)); 343 se.sigev_notify = SIGEV_SIGNAL; 344 se.sigev_signo = SIGUSR1; 345 346 timer_t timer_id; 347 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); 348 349 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0; 350 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler); 351 352 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); 353 354 itimerspec ts; 355 ts.it_value.tv_sec = 0; 356 ts.it_value.tv_nsec = 1; 357 ts.it_interval.tv_sec = 0; 358 ts.it_interval.tv_nsec = 0; 359 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, NULL)); 360 361 usleep(500000); 362 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); 363 } 364 365 struct Counter { 366 private: 367 std::atomic<int> value; 368 timer_t timer_id; 369 sigevent_t se; 370 bool timer_valid; 371 372 void Create() { 373 ASSERT_FALSE(timer_valid); 374 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id)); 375 timer_valid = true; 376 } 377 378 public: 379 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) { 380 memset(&se, 0, sizeof(se)); 381 se.sigev_notify = SIGEV_THREAD; 382 se.sigev_notify_function = fn; 383 se.sigev_value.sival_ptr = this; 384 Create(); 385 } 386 void DeleteTimer() { 387 ASSERT_TRUE(timer_valid); 388 ASSERT_EQ(0, timer_delete(timer_id)); 389 timer_valid = false; 390 } 391 392 ~Counter() { 393 if (timer_valid) { 394 DeleteTimer(); 395 } 396 } 397 398 int Value() const { 399 return value; 400 } 401 402 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { 403 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns); 404 } 405 406 bool ValueUpdated() { 407 int current_value = value; 408 time_t start = time(NULL); 409 while (current_value == value && (time(NULL) - start) < 5) { 410 } 411 return current_value != value; 412 } 413 414 static void CountNotifyFunction(sigval_t value) { 415 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); 416 ++cd->value; 417 } 418 419 static void CountAndDisarmNotifyFunction(sigval_t value) { 420 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); 421 ++cd->value; 422 423 // Setting the initial expiration time to 0 disarms the timer. 424 cd->SetTime(0, 0, 1, 0); 425 } 426 }; 427 428 TEST(time, timer_settime_0) { 429 Counter counter(Counter::CountAndDisarmNotifyFunction); 430 ASSERT_EQ(0, counter.Value()); 431 432 counter.SetTime(0, 500000000, 1, 0); 433 sleep(1); 434 435 // The count should just be 1 because we disarmed the timer the first time it fired. 436 ASSERT_EQ(1, counter.Value()); 437 } 438 439 TEST(time, timer_settime_repeats) { 440 Counter counter(Counter::CountNotifyFunction); 441 ASSERT_EQ(0, counter.Value()); 442 443 counter.SetTime(0, 1, 0, 10); 444 ASSERT_TRUE(counter.ValueUpdated()); 445 ASSERT_TRUE(counter.ValueUpdated()); 446 ASSERT_TRUE(counter.ValueUpdated()); 447 counter.DeleteTimer(); 448 // Add a sleep as other threads may be calling the callback function when the timer is deleted. 449 usleep(500000); 450 } 451 452 static int timer_create_NULL_signal_handler_invocation_count; 453 static void timer_create_NULL_signal_handler(int signal_number) { 454 ++timer_create_NULL_signal_handler_invocation_count; 455 ASSERT_EQ(SIGALRM, signal_number); 456 } 457 458 TEST(time, timer_create_NULL) { 459 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM. 460 timer_t timer_id; 461 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id)); 462 463 timer_create_NULL_signal_handler_invocation_count = 0; 464 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler); 465 466 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count); 467 468 SetTime(timer_id, 0, 1, 0, 0); 469 usleep(500000); 470 471 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count); 472 } 473 474 TEST(time, timer_create_EINVAL) { 475 clockid_t invalid_clock = 16; 476 477 // A SIGEV_SIGNAL timer is easy; the kernel does all that. 478 timer_t timer_id; 479 ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id)); 480 ASSERT_EQ(EINVAL, errno); 481 482 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up. 483 sigevent_t se; 484 memset(&se, 0, sizeof(se)); 485 se.sigev_notify = SIGEV_THREAD; 486 se.sigev_notify_function = NoOpNotifyFunction; 487 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id)); 488 ASSERT_EQ(EINVAL, errno); 489 } 490 491 TEST(time, timer_delete_multiple) { 492 timer_t timer_id; 493 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id)); 494 ASSERT_EQ(0, timer_delete(timer_id)); 495 ASSERT_EQ(-1, timer_delete(timer_id)); 496 ASSERT_EQ(EINVAL, errno); 497 498 sigevent_t se; 499 memset(&se, 0, sizeof(se)); 500 se.sigev_notify = SIGEV_THREAD; 501 se.sigev_notify_function = NoOpNotifyFunction; 502 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); 503 ASSERT_EQ(0, timer_delete(timer_id)); 504 ASSERT_EQ(-1, timer_delete(timer_id)); 505 ASSERT_EQ(EINVAL, errno); 506 } 507 508 TEST(time, timer_create_multiple) { 509 Counter counter1(Counter::CountNotifyFunction); 510 Counter counter2(Counter::CountNotifyFunction); 511 Counter counter3(Counter::CountNotifyFunction); 512 513 ASSERT_EQ(0, counter1.Value()); 514 ASSERT_EQ(0, counter2.Value()); 515 ASSERT_EQ(0, counter3.Value()); 516 517 counter2.SetTime(0, 500000000, 0, 0); 518 sleep(1); 519 520 EXPECT_EQ(0, counter1.Value()); 521 EXPECT_EQ(1, counter2.Value()); 522 EXPECT_EQ(0, counter3.Value()); 523 } 524 525 // Test to verify that disarming a repeatable timer disables the callbacks. 526 TEST(time, timer_disarm_terminates) { 527 Counter counter(Counter::CountNotifyFunction); 528 ASSERT_EQ(0, counter.Value()); 529 530 counter.SetTime(0, 1, 0, 1); 531 ASSERT_TRUE(counter.ValueUpdated()); 532 ASSERT_TRUE(counter.ValueUpdated()); 533 ASSERT_TRUE(counter.ValueUpdated()); 534 535 counter.SetTime(0, 0, 0, 0); 536 // Add a sleep as the kernel may have pending events when the timer is disarmed. 537 usleep(500000); 538 int value = counter.Value(); 539 usleep(500000); 540 541 // Verify the counter has not been incremented. 542 ASSERT_EQ(value, counter.Value()); 543 } 544 545 // Test to verify that deleting a repeatable timer disables the callbacks. 546 TEST(time, timer_delete_terminates) { 547 Counter counter(Counter::CountNotifyFunction); 548 ASSERT_EQ(0, counter.Value()); 549 550 counter.SetTime(0, 1, 0, 1); 551 ASSERT_TRUE(counter.ValueUpdated()); 552 ASSERT_TRUE(counter.ValueUpdated()); 553 ASSERT_TRUE(counter.ValueUpdated()); 554 555 counter.DeleteTimer(); 556 // Add a sleep as other threads may be calling the callback function when the timer is deleted. 557 usleep(500000); 558 int value = counter.Value(); 559 usleep(500000); 560 561 // Verify the counter has not been incremented. 562 ASSERT_EQ(value, counter.Value()); 563 } 564 565 struct TimerDeleteData { 566 timer_t timer_id; 567 pid_t tid; 568 volatile bool complete; 569 }; 570 571 static void TimerDeleteCallback(sigval_t value) { 572 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr); 573 574 tdd->tid = gettid(); 575 timer_delete(tdd->timer_id); 576 tdd->complete = true; 577 } 578 579 TEST(time, timer_delete_from_timer_thread) { 580 TimerDeleteData tdd; 581 sigevent_t se; 582 583 memset(&se, 0, sizeof(se)); 584 se.sigev_notify = SIGEV_THREAD; 585 se.sigev_notify_function = TimerDeleteCallback; 586 se.sigev_value.sival_ptr = &tdd; 587 588 tdd.complete = false; 589 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id)); 590 591 itimerspec ts; 592 ts.it_value.tv_sec = 1; 593 ts.it_value.tv_nsec = 0; 594 ts.it_interval.tv_sec = 0; 595 ts.it_interval.tv_nsec = 0; 596 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, NULL)); 597 598 time_t cur_time = time(NULL); 599 while (!tdd.complete && (time(NULL) - cur_time) < 5); 600 ASSERT_TRUE(tdd.complete); 601 602 #if defined(__BIONIC__) 603 // Since bionic timers are implemented by creating a thread to handle the 604 // callback, verify that the thread actually completes. 605 cur_time = time(NULL); 606 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5); 607 ASSERT_EQ(-1, kill(tdd.tid, 0)); 608 ASSERT_EQ(ESRCH, errno); 609 #endif 610 } 611 612 TEST(time, clock_gettime) { 613 // Try to ensure that our vdso clock_gettime is working. 614 timespec ts1; 615 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1)); 616 timespec ts2; 617 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2)); 618 619 // What's the difference between the two? 620 ts2.tv_sec -= ts1.tv_sec; 621 ts2.tv_nsec -= ts1.tv_nsec; 622 if (ts2.tv_nsec < 0) { 623 --ts2.tv_sec; 624 ts2.tv_nsec += NS_PER_S; 625 } 626 627 // Should be less than (a very generous, to try to avoid flakiness) 1000000ns. 628 ASSERT_EQ(0, ts2.tv_sec); 629 ASSERT_LT(ts2.tv_nsec, 1000000); 630 } 631 632 TEST(time, clock_gettime_CLOCK_REALTIME) { 633 timespec ts; 634 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); 635 } 636 637 TEST(time, clock_gettime_CLOCK_MONOTONIC) { 638 timespec ts; 639 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); 640 } 641 642 TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) { 643 timespec ts; 644 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)); 645 } 646 647 TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) { 648 timespec ts; 649 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts)); 650 } 651 652 TEST(time, clock_gettime_CLOCK_BOOTTIME) { 653 timespec ts; 654 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts)); 655 } 656 657 TEST(time, clock_gettime_unknown) { 658 errno = 0; 659 timespec ts; 660 ASSERT_EQ(-1, clock_gettime(-1, &ts)); 661 ASSERT_EQ(EINVAL, errno); 662 } 663 664 TEST(time, clock_getres_CLOCK_REALTIME) { 665 timespec ts; 666 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts)); 667 ASSERT_EQ(1, ts.tv_nsec); 668 ASSERT_EQ(0, ts.tv_sec); 669 } 670 671 TEST(time, clock_getres_CLOCK_MONOTONIC) { 672 timespec ts; 673 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts)); 674 ASSERT_EQ(1, ts.tv_nsec); 675 ASSERT_EQ(0, ts.tv_sec); 676 } 677 678 TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) { 679 timespec ts; 680 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts)); 681 } 682 683 TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) { 684 timespec ts; 685 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts)); 686 } 687 688 TEST(time, clock_getres_CLOCK_BOOTTIME) { 689 timespec ts; 690 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts)); 691 ASSERT_EQ(1, ts.tv_nsec); 692 ASSERT_EQ(0, ts.tv_sec); 693 } 694 695 TEST(time, clock_getres_unknown) { 696 errno = 0; 697 timespec ts = { -1, -1 }; 698 ASSERT_EQ(-1, clock_getres(-1, &ts)); 699 ASSERT_EQ(EINVAL, errno); 700 ASSERT_EQ(-1, ts.tv_nsec); 701 ASSERT_EQ(-1, ts.tv_sec); 702 } 703 704 TEST(time, clock) { 705 // clock(3) is hard to test, but a 1s sleep should cost less than 1ms. 706 clock_t t0 = clock(); 707 sleep(1); 708 clock_t t1 = clock(); 709 ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000); 710 } 711 712 static pid_t GetInvalidPid() { 713 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose}; 714 long pid_max; 715 fscanf(fp.get(), "%ld", &pid_max); 716 return static_cast<pid_t>(pid_max + 1); 717 } 718 719 TEST(time, clock_getcpuclockid_current) { 720 clockid_t clockid; 721 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid)); 722 timespec ts; 723 ASSERT_EQ(0, clock_gettime(clockid, &ts)); 724 } 725 726 TEST(time, clock_getcpuclockid_parent) { 727 clockid_t clockid; 728 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid)); 729 timespec ts; 730 ASSERT_EQ(0, clock_gettime(clockid, &ts)); 731 } 732 733 TEST(time, clock_getcpuclockid_ESRCH) { 734 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it. 735 errno = 0; 736 // If this fails, your kernel needs commit e1b6b6ce to be backported. 737 clockid_t clockid; 738 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n" 739 << "Please ensure that the following kernel patches or their replacements have been applied:\n" 740 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/" 741 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n" 742 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/" 743 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n"; 744 ASSERT_EQ(0, errno); 745 } 746 747 TEST(time, clock_settime) { 748 errno = 0; 749 timespec ts; 750 ASSERT_EQ(-1, clock_settime(-1, &ts)); 751 ASSERT_EQ(EINVAL, errno); 752 } 753 754 TEST(time, clock_nanosleep) { 755 timespec in; 756 timespec out; 757 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out)); 758 } 759 760 TEST(time, clock_nanosleep_thread_cputime_id) { 761 timespec in; 762 in.tv_sec = 1; 763 in.tv_nsec = 0; 764 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr)); 765 } 766 767 TEST(time, bug_31938693) { 768 // User-visible symptoms in N: 769 // http://b/31938693 770 // https://code.google.com/p/android/issues/detail?id=225132 771 772 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug): 773 // http://b/31848040 774 775 // This isn't a great test, because very few time zones were actually affected, and there's 776 // no real logic to which ones were affected: it was just a coincidence of the data that came 777 // after them in the tzdata file. 778 779 time_t t = 1475619727; 780 struct tm tm; 781 782 setenv("TZ", "America/Los_Angeles", 1); 783 tzset(); 784 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); 785 EXPECT_EQ(15, tm.tm_hour); 786 787 setenv("TZ", "Europe/London", 1); 788 tzset(); 789 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); 790 EXPECT_EQ(23, tm.tm_hour); 791 792 setenv("TZ", "America/Atka", 1); 793 tzset(); 794 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); 795 EXPECT_EQ(13, tm.tm_hour); 796 797 setenv("TZ", "Pacific/Apia", 1); 798 tzset(); 799 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); 800 EXPECT_EQ(12, tm.tm_hour); 801 802 setenv("TZ", "Pacific/Honolulu", 1); 803 tzset(); 804 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); 805 EXPECT_EQ(12, tm.tm_hour); 806 807 setenv("TZ", "Asia/Magadan", 1); 808 tzset(); 809 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); 810 EXPECT_EQ(9, tm.tm_hour); 811 } 812 813 TEST(time, bug_31339449) { 814 // POSIX says localtime acts as if it calls tzset. 815 // tzset does two things: 816 // 1. it sets the time zone ctime/localtime/mktime/strftime will use. 817 // 2. it sets the global `tzname`. 818 // POSIX says localtime_r need not set `tzname` (2). 819 // Q: should localtime_r set the time zone (1)? 820 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes". 821 822 // Pick a time, any time... 823 time_t t = 1475619727; 824 825 // Call tzset with a specific timezone. 826 setenv("TZ", "America/Atka", 1); 827 tzset(); 828 829 // If we change the timezone and call localtime, localtime should use the new timezone. 830 setenv("TZ", "America/Los_Angeles", 1); 831 struct tm* tm_p = localtime(&t); 832 EXPECT_EQ(15, tm_p->tm_hour); 833 834 // Reset the timezone back. 835 setenv("TZ", "America/Atka", 1); 836 tzset(); 837 838 #if defined(__BIONIC__) 839 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone. 840 setenv("TZ", "America/Los_Angeles", 1); 841 struct tm tm = {}; 842 localtime_r(&t, &tm); 843 EXPECT_EQ(15, tm.tm_hour); 844 #else 845 // The BSDs agree with us, but glibc gets this wrong. 846 #endif 847 } 848 849 TEST(time, asctime) { 850 const struct tm tm = {}; 851 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm)); 852 } 853 854 TEST(time, asctime_r) { 855 const struct tm tm = {}; 856 char buf[256]; 857 ASSERT_EQ(buf, asctime_r(&tm, buf)); 858 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf); 859 } 860 861 TEST(time, ctime) { 862 setenv("TZ", "UTC", 1); 863 const time_t t = 0; 864 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t)); 865 } 866 867 TEST(time, ctime_r) { 868 setenv("TZ", "UTC", 1); 869 const time_t t = 0; 870 char buf[256]; 871 ASSERT_EQ(buf, ctime_r(&t, buf)); 872 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf); 873 } 874 875 // https://issuetracker.google.com/37128336 876 TEST(time, strftime_strptime_s) { 877 char buf[32]; 878 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 }; 879 880 setenv("TZ", "America/Los_Angeles", 1); 881 strftime(buf, sizeof(buf), "<%s>", &tm0); 882 EXPECT_STREQ("<378720000>", buf); 883 884 setenv("TZ", "UTC", 1); 885 strftime(buf, sizeof(buf), "<%s>", &tm0); 886 EXPECT_STREQ("<378691200>", buf); 887 888 struct tm tm; 889 890 setenv("TZ", "America/Los_Angeles", 1); 891 tzset(); 892 memset(&tm, 0xff, sizeof(tm)); 893 char* p = strptime("378720000x", "%s", &tm); 894 ASSERT_EQ('x', *p); 895 EXPECT_EQ(0, tm.tm_sec); 896 EXPECT_EQ(0, tm.tm_min); 897 EXPECT_EQ(0, tm.tm_hour); 898 EXPECT_EQ(1, tm.tm_mday); 899 EXPECT_EQ(0, tm.tm_mon); 900 EXPECT_EQ(82, tm.tm_year); 901 EXPECT_EQ(5, tm.tm_wday); 902 EXPECT_EQ(0, tm.tm_yday); 903 EXPECT_EQ(0, tm.tm_isdst); 904 905 setenv("TZ", "UTC", 1); 906 tzset(); 907 memset(&tm, 0xff, sizeof(tm)); 908 p = strptime("378691200x", "%s", &tm); 909 ASSERT_EQ('x', *p); 910 EXPECT_EQ(0, tm.tm_sec); 911 EXPECT_EQ(0, tm.tm_min); 912 EXPECT_EQ(0, tm.tm_hour); 913 EXPECT_EQ(1, tm.tm_mday); 914 EXPECT_EQ(0, tm.tm_mon); 915 EXPECT_EQ(82, tm.tm_year); 916 EXPECT_EQ(5, tm.tm_wday); 917 EXPECT_EQ(0, tm.tm_yday); 918 EXPECT_EQ(0, tm.tm_isdst); 919 } 920 921 TEST(time, strptime_s_nothing) { 922 struct tm tm; 923 ASSERT_EQ(nullptr, strptime("x", "%s", &tm)); 924 } 925