2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <gtest/gtest.h>
23 #include <sys/syscall.h>
24 #include <sys/types.h>
29 #include "ScopedSignalHandler.h"
32 #include "private/bionic_constants.h"
36 tm* broken_down = gmtime(&t);
37 ASSERT_TRUE(broken_down != NULL);
38 ASSERT_EQ(0, broken_down->tm_sec);
39 ASSERT_EQ(0, broken_down->tm_min);
40 ASSERT_EQ(0, broken_down->tm_hour);
41 ASSERT_EQ(1, broken_down->tm_mday);
42 ASSERT_EQ(0, broken_down->tm_mon);
43 ASSERT_EQ(1970, broken_down->tm_year + 1900);
46 static void* gmtime_no_stack_overflow_14313703_fn(void*) {
47 const char* original_tz = getenv("TZ");
48 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
49 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
51 if (original_tz != NULL) {
52 setenv("TZ", original_tz, 1);
58 TEST(time, gmtime_no_stack_overflow_14313703) {
59 // Is it safe to call tzload on a thread with a small stack?
61 // https://code.google.com/p/android/issues/detail?id=61130
63 ASSERT_EQ(0, pthread_attr_init(&a));
64 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
67 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, NULL));
68 ASSERT_EQ(0, pthread_join(t, nullptr));
71 TEST(time, mktime_empty_TZ) {
72 // tzcode used to have a bug where it didn't reinitialize some internal state.
74 // Choose a time where DST is set.
76 memset(&t, 0, sizeof(tm));
77 t.tm_year = 1980 - 1900;
81 setenv("TZ", "America/Los_Angeles", 1);
83 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
85 memset(&t, 0, sizeof(tm));
86 t.tm_year = 1980 - 1900;
90 setenv("TZ", "", 1); // Implies UTC.
92 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
95 TEST(time, mktime_10310929) {
97 memset(&t, 0, sizeof(tm));
102 #if !defined(__LP64__)
103 // 32-bit bionic stupidly had a signed 32-bit time_t.
104 ASSERT_EQ(-1, mktime(&t));
105 ASSERT_EQ(EOVERFLOW, errno);
107 // Everyone else should be using a signed 64-bit time_t.
108 ASSERT_GE(sizeof(time_t) * 8, 64U);
110 setenv("TZ", "America/Los_Angeles", 1);
113 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
116 setenv("TZ", "UTC", 1);
119 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
124 TEST(time, mktime_EOVERFLOW) {
126 memset(&t, 0, sizeof(tm));
128 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
129 t.tm_year = 2016 - 1900;
135 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
138 // This will overflow for LP32 or LP64.
142 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
143 ASSERT_EQ(EOVERFLOW, errno);
146 TEST(time, strftime) {
147 setenv("TZ", "UTC", 1);
150 memset(&t, 0, sizeof(tm));
157 // Seconds since the epoch.
158 #if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
159 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
160 EXPECT_STREQ("4108320000", buf);
163 // Date and time as text.
164 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
165 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
168 TEST(time, strftime_null_tm_zone) {
169 // Netflix on Nexus Player wouldn't start (http://b/25170306).
171 memset(&t, 0, sizeof(tm));
175 setenv("TZ", "America/Los_Angeles", 1);
178 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
179 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
180 EXPECT_STREQ("<PST>", buf);
182 #if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
183 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
184 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
185 EXPECT_STREQ("<PDT>", buf);
187 t.tm_isdst = -123; // "and negative if the information is not available".
188 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
189 EXPECT_STREQ("<>", buf);
192 setenv("TZ", "UTC", 1);
196 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
197 EXPECT_STREQ("<UTC>", buf);
199 #if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
200 t.tm_isdst = 1; // UTC has no DST.
201 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
202 EXPECT_STREQ("<>", buf);
206 TEST(time, strftime_l) {
207 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
208 locale_t old_locale = uselocale(cloc);
210 setenv("TZ", "UTC", 1);
213 memset(&t, 0, sizeof(tm));
218 // Date and time as text.
220 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
221 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
223 uselocale(old_locale);
227 TEST(time, strptime) {
228 setenv("TZ", "UTC", 1);
233 memset(&t, 0, sizeof(t));
234 strptime("11:14", "%R", &t);
235 strftime(buf, sizeof(buf), "%H:%M", &t);
236 EXPECT_STREQ("11:14", buf);
238 memset(&t, 0, sizeof(t));
239 strptime("09:41:53", "%T", &t);
240 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
241 EXPECT_STREQ("09:41:53", buf);
244 void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
246 ts.it_value.tv_sec = value_s;
247 ts.it_value.tv_nsec = value_ns;
248 ts.it_interval.tv_sec = interval_s;
249 ts.it_interval.tv_nsec = interval_ns;
250 ASSERT_EQ(0, timer_settime(t, 0, &ts, NULL));
253 static void NoOpNotifyFunction(sigval_t) {
256 TEST(time, timer_create) {
258 memset(&se, 0, sizeof(se));
259 se.sigev_notify = SIGEV_THREAD;
260 se.sigev_notify_function = NoOpNotifyFunction;
262 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
265 ASSERT_NE(-1, pid) << strerror(errno);
268 // Timers are not inherited by the child.
269 ASSERT_EQ(-1, timer_delete(timer_id));
270 ASSERT_EQ(EINVAL, errno);
274 AssertChildExited(pid, 0);
276 ASSERT_EQ(0, timer_delete(timer_id));
279 static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
280 static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
281 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
282 ASSERT_EQ(SIGUSR1, signal_number);
285 TEST(time, timer_create_SIGEV_SIGNAL) {
287 memset(&se, 0, sizeof(se));
288 se.sigev_notify = SIGEV_SIGNAL;
289 se.sigev_signo = SIGUSR1;
292 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
294 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
295 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
297 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
300 ts.it_value.tv_sec = 0;
301 ts.it_value.tv_nsec = 1;
302 ts.it_interval.tv_sec = 0;
303 ts.it_interval.tv_nsec = 0;
304 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, NULL));
307 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
312 std::atomic<int> value;
318 ASSERT_FALSE(timer_valid);
319 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
324 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
325 memset(&se, 0, sizeof(se));
326 se.sigev_notify = SIGEV_THREAD;
327 se.sigev_notify_function = fn;
328 se.sigev_value.sival_ptr = this;
332 ASSERT_TRUE(timer_valid);
333 ASSERT_EQ(0, timer_delete(timer_id));
347 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
348 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
351 bool ValueUpdated() {
352 int current_value = value;
353 time_t start = time(NULL);
354 while (current_value == value && (time(NULL) - start) < 5) {
356 return current_value != value;
359 static void CountNotifyFunction(sigval_t value) {
360 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
364 static void CountAndDisarmNotifyFunction(sigval_t value) {
365 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
368 // Setting the initial expiration time to 0 disarms the timer.
369 cd->SetTime(0, 0, 1, 0);
373 TEST(time, timer_settime_0) {
374 Counter counter(Counter::CountAndDisarmNotifyFunction);
375 ASSERT_EQ(0, counter.Value());
377 counter.SetTime(0, 500000000, 1, 0);
380 // The count should just be 1 because we disarmed the timer the first time it fired.
381 ASSERT_EQ(1, counter.Value());
384 TEST(time, timer_settime_repeats) {
385 Counter counter(Counter::CountNotifyFunction);
386 ASSERT_EQ(0, counter.Value());
388 counter.SetTime(0, 1, 0, 10);
389 ASSERT_TRUE(counter.ValueUpdated());
390 ASSERT_TRUE(counter.ValueUpdated());
391 ASSERT_TRUE(counter.ValueUpdated());
392 counter.DeleteTimer();
393 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
397 static int timer_create_NULL_signal_handler_invocation_count;
398 static void timer_create_NULL_signal_handler(int signal_number) {
399 ++timer_create_NULL_signal_handler_invocation_count;
400 ASSERT_EQ(SIGALRM, signal_number);
403 TEST(time, timer_create_NULL) {
404 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
406 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
408 timer_create_NULL_signal_handler_invocation_count = 0;
409 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
411 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
413 SetTime(timer_id, 0, 1, 0, 0);
416 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
419 TEST(time, timer_create_EINVAL) {
420 clockid_t invalid_clock = 16;
422 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
424 ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id));
425 ASSERT_EQ(EINVAL, errno);
427 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
429 memset(&se, 0, sizeof(se));
430 se.sigev_notify = SIGEV_THREAD;
431 se.sigev_notify_function = NoOpNotifyFunction;
432 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
433 ASSERT_EQ(EINVAL, errno);
436 TEST(time, timer_delete_multiple) {
438 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
439 ASSERT_EQ(0, timer_delete(timer_id));
440 ASSERT_EQ(-1, timer_delete(timer_id));
441 ASSERT_EQ(EINVAL, errno);
444 memset(&se, 0, sizeof(se));
445 se.sigev_notify = SIGEV_THREAD;
446 se.sigev_notify_function = NoOpNotifyFunction;
447 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
448 ASSERT_EQ(0, timer_delete(timer_id));
449 ASSERT_EQ(-1, timer_delete(timer_id));
450 ASSERT_EQ(EINVAL, errno);
453 TEST(time, timer_create_multiple) {
454 Counter counter1(Counter::CountNotifyFunction);
455 Counter counter2(Counter::CountNotifyFunction);
456 Counter counter3(Counter::CountNotifyFunction);
458 ASSERT_EQ(0, counter1.Value());
459 ASSERT_EQ(0, counter2.Value());
460 ASSERT_EQ(0, counter3.Value());
462 counter2.SetTime(0, 500000000, 0, 0);
465 EXPECT_EQ(0, counter1.Value());
466 EXPECT_EQ(1, counter2.Value());
467 EXPECT_EQ(0, counter3.Value());
470 // Test to verify that disarming a repeatable timer disables the callbacks.
471 TEST(time, timer_disarm_terminates) {
472 Counter counter(Counter::CountNotifyFunction);
473 ASSERT_EQ(0, counter.Value());
475 counter.SetTime(0, 1, 0, 1);
476 ASSERT_TRUE(counter.ValueUpdated());
477 ASSERT_TRUE(counter.ValueUpdated());
478 ASSERT_TRUE(counter.ValueUpdated());
480 counter.SetTime(0, 0, 0, 0);
481 // Add a sleep as the kernel may have pending events when the timer is disarmed.
483 int value = counter.Value();
486 // Verify the counter has not been incremented.
487 ASSERT_EQ(value, counter.Value());
490 // Test to verify that deleting a repeatable timer disables the callbacks.
491 TEST(time, timer_delete_terminates) {
492 Counter counter(Counter::CountNotifyFunction);
493 ASSERT_EQ(0, counter.Value());
495 counter.SetTime(0, 1, 0, 1);
496 ASSERT_TRUE(counter.ValueUpdated());
497 ASSERT_TRUE(counter.ValueUpdated());
498 ASSERT_TRUE(counter.ValueUpdated());
500 counter.DeleteTimer();
501 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
503 int value = counter.Value();
506 // Verify the counter has not been incremented.
507 ASSERT_EQ(value, counter.Value());
510 struct TimerDeleteData {
513 volatile bool complete;
516 static void TimerDeleteCallback(sigval_t value) {
517 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
520 timer_delete(tdd->timer_id);
521 tdd->complete = true;
524 TEST(time, timer_delete_from_timer_thread) {
528 memset(&se, 0, sizeof(se));
529 se.sigev_notify = SIGEV_THREAD;
530 se.sigev_notify_function = TimerDeleteCallback;
531 se.sigev_value.sival_ptr = &tdd;
533 tdd.complete = false;
534 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
537 ts.it_value.tv_sec = 1;
538 ts.it_value.tv_nsec = 0;
539 ts.it_interval.tv_sec = 0;
540 ts.it_interval.tv_nsec = 0;
541 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, NULL));
543 time_t cur_time = time(NULL);
544 while (!tdd.complete && (time(NULL) - cur_time) < 5);
545 ASSERT_TRUE(tdd.complete);
547 #if defined(__BIONIC__)
548 // Since bionic timers are implemented by creating a thread to handle the
549 // callback, verify that the thread actually completes.
550 cur_time = time(NULL);
551 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
552 ASSERT_EQ(-1, kill(tdd.tid, 0));
553 ASSERT_EQ(ESRCH, errno);
557 TEST(time, clock_gettime) {
558 // Try to ensure that our vdso clock_gettime is working.
560 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
562 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
564 // What's the difference between the two?
565 ts2.tv_sec -= ts1.tv_sec;
566 ts2.tv_nsec -= ts1.tv_nsec;
567 if (ts2.tv_nsec < 0) {
569 ts2.tv_nsec += NS_PER_S;
572 // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
573 ASSERT_EQ(0, ts2.tv_sec);
574 ASSERT_LT(ts2.tv_nsec, 1000000);
578 // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
579 clock_t t0 = clock();
581 clock_t t1 = clock();
582 ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
585 pid_t GetInvalidPid() {
586 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
588 fscanf(fp, "%ld", &pid_max);
589 pid_t invalid_pid = static_cast<pid_t>(pid_max + 1);
594 TEST(time, clock_getcpuclockid) {
595 // For current process.
597 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
600 ASSERT_EQ(0, clock_gettime(clockid, &ts));
602 // For parent process.
603 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
604 ASSERT_EQ(0, clock_gettime(clockid, &ts));
606 // For invalid process.
607 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
609 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid));
613 TEST(time, clock_settime) {
616 ASSERT_EQ(-1, clock_settime(-1, &ts));
617 ASSERT_EQ(EINVAL, errno);
620 TEST(time, clock_nanosleep) {
623 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
626 TEST(time, clock_nanosleep_thread_cputime_id) {
630 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
633 TEST(time, bug_31938693) {
634 // User-visible symptoms in N:
636 // https://code.google.com/p/android/issues/detail?id=225132
638 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
641 // This isn't a great test, because very few time zones were actually affected, and there's
642 // no real logic to which ones were affected: it was just a coincidence of the data that came
643 // after them in the tzdata file.
645 time_t t = 1475619727;
648 setenv("TZ", "America/Los_Angeles", 1);
650 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
651 EXPECT_EQ(15, tm.tm_hour);
653 setenv("TZ", "Europe/London", 1);
655 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
656 EXPECT_EQ(23, tm.tm_hour);
658 setenv("TZ", "America/Atka", 1);
660 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
661 EXPECT_EQ(13, tm.tm_hour);
663 setenv("TZ", "Pacific/Apia", 1);
665 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
666 EXPECT_EQ(12, tm.tm_hour);
668 setenv("TZ", "Pacific/Honolulu", 1);
670 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
671 EXPECT_EQ(12, tm.tm_hour);
673 setenv("TZ", "Asia/Magadan", 1);
675 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
676 EXPECT_EQ(9, tm.tm_hour);
679 TEST(time, bug_31339449) {
680 // POSIX says localtime acts as if it calls tzset.
681 // tzset does two things:
682 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
683 // 2. it sets the global `tzname`.
684 // POSIX says localtime_r need not set `tzname` (2).
685 // Q: should localtime_r set the time zone (1)?
686 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
688 // Pick a time, any time...
689 time_t t = 1475619727;
691 // Call tzset with a specific timezone.
692 setenv("TZ", "America/Atka", 1);
695 // If we change the timezone and call localtime, localtime should use the new timezone.
696 setenv("TZ", "America/Los_Angeles", 1);
697 struct tm* tm_p = localtime(&t);
698 EXPECT_EQ(15, tm_p->tm_hour);
700 // Reset the timezone back.
701 setenv("TZ", "America/Atka", 1);
704 #if defined(__BIONIC__)
705 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
706 setenv("TZ", "America/Los_Angeles", 1);
708 localtime_r(&t, &tm);
709 EXPECT_EQ(15, tm.tm_hour);
711 // The BSDs agree with us, but glibc gets this wrong.