]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - tests/time_test.cpp
e0231b1ca36aca1ffb2c1fbd9abd9cb96ddc8932
[android-sdk/platform-bionic.git] / tests / time_test.cpp
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  */
17 #include <time.h>
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>
27 #include "ScopedSignalHandler.h"
29 #include "private/bionic_constants.h"
31 TEST(time, gmtime) {
32   time_t t = 0;
33   tm* broken_down = gmtime(&t);
34   ASSERT_TRUE(broken_down != NULL);
35   ASSERT_EQ(0, broken_down->tm_sec);
36   ASSERT_EQ(0, broken_down->tm_min);
37   ASSERT_EQ(0, broken_down->tm_hour);
38   ASSERT_EQ(1, broken_down->tm_mday);
39   ASSERT_EQ(0, broken_down->tm_mon);
40   ASSERT_EQ(1970, broken_down->tm_year + 1900);
41 }
43 static void* gmtime_no_stack_overflow_14313703_fn(void*) {
44   const char* original_tz = getenv("TZ");
45   // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
46   setenv("TZ", "gmtime_stack_overflow_14313703", 1);
47   tzset();
48   if (original_tz != NULL) {
49     setenv("TZ", original_tz, 1);
50   }
51   tzset();
52   return NULL;
53 }
55 TEST(time, gmtime_no_stack_overflow_14313703) {
56   // Is it safe to call tzload on a thread with a small stack?
57   // http://b/14313703
58   // https://code.google.com/p/android/issues/detail?id=61130
59   pthread_attr_t attributes;
60   ASSERT_EQ(0, pthread_attr_init(&attributes));
61 #if defined(__BIONIC__)
62   ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, PTHREAD_STACK_MIN));
63 #else
64   // PTHREAD_STACK_MIN not currently in the host GCC sysroot.
65   ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 4 * getpagesize()));
66 #endif
68   pthread_t t;
69   ASSERT_EQ(0, pthread_create(&t, &attributes, gmtime_no_stack_overflow_14313703_fn, NULL));
70   void* result;
71   ASSERT_EQ(0, pthread_join(t, &result));
72 }
74 TEST(time, mktime_empty_TZ) {
75   // tzcode used to have a bug where it didn't reinitialize some internal state.
77   // Choose a time where DST is set.
78   struct tm t;
79   memset(&t, 0, sizeof(tm));
80   t.tm_year = 1980 - 1900;
81   t.tm_mon = 6;
82   t.tm_mday = 2;
84   setenv("TZ", "America/Los_Angeles", 1);
85   tzset();
86   ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
88   memset(&t, 0, sizeof(tm));
89   t.tm_year = 1980 - 1900;
90   t.tm_mon = 6;
91   t.tm_mday = 2;
93   setenv("TZ", "", 1); // Implies UTC.
94   tzset();
95   ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
96 }
98 TEST(time, mktime_10310929) {
99   struct tm t;
100   memset(&t, 0, sizeof(tm));
101   t.tm_year = 200;
102   t.tm_mon = 2;
103   t.tm_mday = 10;
105 #if !defined(__LP64__)
106   // 32-bit bionic stupidly had a signed 32-bit time_t.
107   ASSERT_EQ(-1, mktime(&t));
108 #else
109   // Everyone else should be using a signed 64-bit time_t.
110   ASSERT_GE(sizeof(time_t) * 8, 64U);
112   setenv("TZ", "America/Los_Angeles", 1);
113   tzset();
114   ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
116   setenv("TZ", "UTC", 1);
117   tzset();
118   ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
119 #endif
122 TEST(time, strftime) {
123   setenv("TZ", "UTC", 1);
125   struct tm t;
126   memset(&t, 0, sizeof(tm));
127   t.tm_year = 200;
128   t.tm_mon = 2;
129   t.tm_mday = 10;
131   char buf[64];
133   // Seconds since the epoch.
134 #if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
135   EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
136   EXPECT_STREQ("4108320000", buf);
137 #endif
139   // Date and time as text.
140   EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
141   EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
144 TEST(time, strptime) {
145   setenv("TZ", "UTC", 1);
147   struct tm t;
148   char buf[64];
150   memset(&t, 0, sizeof(t));
151   strptime("11:14", "%R", &t);
152   strftime(buf, sizeof(buf), "%H:%M", &t);
153   EXPECT_STREQ("11:14", buf);
155   memset(&t, 0, sizeof(t));
156   strptime("09:41:53", "%T", &t);
157   strftime(buf, sizeof(buf), "%H:%M:%S", &t);
158   EXPECT_STREQ("09:41:53", buf);
161 void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
162   itimerspec ts;
163   ts.it_value.tv_sec = value_s;
164   ts.it_value.tv_nsec = value_ns;
165   ts.it_interval.tv_sec = interval_s;
166   ts.it_interval.tv_nsec = interval_ns;
167   ASSERT_EQ(0, timer_settime(t, TIMER_ABSTIME, &ts, NULL));
170 static void NoOpNotifyFunction(sigval_t) {
173 TEST(time, timer_create) {
174   sigevent_t se;
175   memset(&se, 0, sizeof(se));
176   se.sigev_notify = SIGEV_THREAD;
177   se.sigev_notify_function = NoOpNotifyFunction;
178   timer_t timer_id;
179   ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
181   int pid = fork();
182   ASSERT_NE(-1, pid) << strerror(errno);
184   if (pid == 0) {
185     // Timers are not inherited by the child.
186     ASSERT_EQ(-1, timer_delete(timer_id));
187     ASSERT_EQ(EINVAL, errno);
188     _exit(0);
189   }
191   int status;
192   ASSERT_EQ(pid, waitpid(pid, &status, 0));
193   ASSERT_TRUE(WIFEXITED(status));
194   ASSERT_EQ(0, WEXITSTATUS(status));
196   ASSERT_EQ(0, timer_delete(timer_id));
199 static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
200 static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
201   ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
202   ASSERT_EQ(SIGUSR1, signal_number);
205 TEST(time, timer_create_SIGEV_SIGNAL) {
206   sigevent_t se;
207   memset(&se, 0, sizeof(se));
208   se.sigev_notify = SIGEV_SIGNAL;
209   se.sigev_signo = SIGUSR1;
211   timer_t timer_id;
212   ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
214   ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
216   ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
218   itimerspec ts;
219   ts.it_value.tv_sec =  0;
220   ts.it_value.tv_nsec = 1;
221   ts.it_interval.tv_sec = 0;
222   ts.it_interval.tv_nsec = 0;
223   ASSERT_EQ(0, timer_settime(timer_id, TIMER_ABSTIME, &ts, NULL));
225   usleep(500000);
226   ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
229 struct Counter {
230   volatile int value;
231   timer_t timer_id;
232   sigevent_t se;
233   bool timer_valid;
235   Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
236     memset(&se, 0, sizeof(se));
237     se.sigev_notify = SIGEV_THREAD;
238     se.sigev_notify_function = fn;
239     se.sigev_value.sival_ptr = this;
240     Create();
241   }
243   void Create() {
244     ASSERT_FALSE(timer_valid);
245     ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
246     timer_valid = true;
247   }
249   void DeleteTimer() {
250     ASSERT_TRUE(timer_valid);
251     ASSERT_EQ(0, timer_delete(timer_id));
252     timer_valid = false;
253   }
255   ~Counter() {
256     if (timer_valid) {
257       DeleteTimer();
258     }
259   }
261   void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
262     ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
263   }
265   bool ValueUpdated() {
266     volatile int current_value = value;
267     time_t start = time(NULL);
268     while (current_value == value && (time(NULL) - start) < 5) {
269     }
270     return current_value != value;
271   }
273   static void CountNotifyFunction(sigval_t value) {
274     Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
275     ++cd->value;
276   }
278   static void CountAndDisarmNotifyFunction(sigval_t value) {
279     Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
280     ++cd->value;
282     // Setting the initial expiration time to 0 disarms the timer.
283     cd->SetTime(0, 0, 1, 0);
284   }
285 };
287 TEST(time, timer_settime_0) {
288   Counter counter(Counter::CountAndDisarmNotifyFunction);
289   ASSERT_TRUE(counter.timer_valid);
291   ASSERT_EQ(0, counter.value);
293   counter.SetTime(0, 1, 1, 0);
294   usleep(500000);
296   // The count should just be 1 because we disarmed the timer the first time it fired.
297   ASSERT_EQ(1, counter.value);
300 TEST(time, timer_settime_repeats) {
301   Counter counter(Counter::CountNotifyFunction);
302   ASSERT_TRUE(counter.timer_valid);
304   ASSERT_EQ(0, counter.value);
306   counter.SetTime(0, 1, 0, 10);
307   ASSERT_TRUE(counter.ValueUpdated());
308   ASSERT_TRUE(counter.ValueUpdated());
309   ASSERT_TRUE(counter.ValueUpdated());
312 static int timer_create_NULL_signal_handler_invocation_count = 0;
313 static void timer_create_NULL_signal_handler(int signal_number) {
314   ++timer_create_NULL_signal_handler_invocation_count;
315   ASSERT_EQ(SIGALRM, signal_number);
318 TEST(time, timer_create_NULL) {
319   // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
320   timer_t timer_id;
321   ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
323   ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
325   ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
327   SetTime(timer_id, 0, 1, 0, 0);
328   usleep(500000);
330   ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
333 TEST(time, timer_create_EINVAL) {
334   clockid_t invalid_clock = 16;
336   // A SIGEV_SIGNAL timer is easy; the kernel does all that.
337   timer_t timer_id;
338   ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id));
339   ASSERT_EQ(EINVAL, errno);
341   // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
342   sigevent_t se;
343   memset(&se, 0, sizeof(se));
344   se.sigev_notify = SIGEV_THREAD;
345   se.sigev_notify_function = NoOpNotifyFunction;
346   ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
347   ASSERT_EQ(EINVAL, errno);
350 TEST(time, timer_delete_multiple) {
351   timer_t timer_id;
352   ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
353   ASSERT_EQ(0, timer_delete(timer_id));
354   ASSERT_EQ(-1, timer_delete(timer_id));
355   ASSERT_EQ(EINVAL, errno);
357   sigevent_t se;
358   memset(&se, 0, sizeof(se));
359   se.sigev_notify = SIGEV_THREAD;
360   se.sigev_notify_function = NoOpNotifyFunction;
361   ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
362   ASSERT_EQ(0, timer_delete(timer_id));
363   ASSERT_EQ(-1, timer_delete(timer_id));
364   ASSERT_EQ(EINVAL, errno);
367 TEST(time, timer_create_multiple) {
368   Counter counter1(Counter::CountNotifyFunction);
369   ASSERT_TRUE(counter1.timer_valid);
370   Counter counter2(Counter::CountNotifyFunction);
371   ASSERT_TRUE(counter2.timer_valid);
372   Counter counter3(Counter::CountNotifyFunction);
373   ASSERT_TRUE(counter3.timer_valid);
375   ASSERT_EQ(0, counter1.value);
376   ASSERT_EQ(0, counter2.value);
377   ASSERT_EQ(0, counter3.value);
379   counter2.SetTime(0, 1, 0, 0);
380   usleep(500000);
382   EXPECT_EQ(0, counter1.value);
383   EXPECT_EQ(1, counter2.value);
384   EXPECT_EQ(0, counter3.value);
387 struct TimerDeleteData {
388   timer_t timer_id;
389   pthread_t thread_id;
390   volatile bool complete;
391 };
393 static void TimerDeleteCallback(sigval_t value) {
394   TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
396   tdd->thread_id = pthread_self();
397   timer_delete(tdd->timer_id);
398   tdd->complete = true;
401 TEST(time, timer_delete_from_timer_thread) {
402   TimerDeleteData tdd;
403   sigevent_t se;
405   memset(&se, 0, sizeof(se));
406   se.sigev_notify = SIGEV_THREAD;
407   se.sigev_notify_function = TimerDeleteCallback;
408   se.sigev_value.sival_ptr = &tdd;
410   tdd.complete = false;
411   ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
413   itimerspec ts;
414   ts.it_value.tv_sec = 0;
415   ts.it_value.tv_nsec = 100;
416   ts.it_interval.tv_sec = 0;
417   ts.it_interval.tv_nsec = 0;
418   ASSERT_EQ(0, timer_settime(tdd.timer_id, TIMER_ABSTIME, &ts, NULL));
420   time_t cur_time = time(NULL);
421   while (!tdd.complete && (time(NULL) - cur_time) < 5);
422   ASSERT_TRUE(tdd.complete);
424 #if defined(__BIONIC__)
425   // Since bionic timers are implemented by creating a thread to handle the
426   // callback, verify that the thread actually completes.
427   cur_time = time(NULL);
428   while (pthread_detach(tdd.thread_id) != ESRCH && (time(NULL) - cur_time) < 5);
429   ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id));
430 #endif
433 TEST(time, clock_gettime) {
434   // Try to ensure that our vdso clock_gettime is working.
435   timespec ts1;
436   ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
437   timespec ts2;
438   ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
440   // What's the difference between the two?
441   ts2.tv_sec -= ts1.tv_sec;
442   ts2.tv_nsec -= ts1.tv_nsec;
443   if (ts2.tv_nsec < 0) {
444     --ts2.tv_sec;
445     ts2.tv_nsec += NS_PER_S;
446   }
448   // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
449   ASSERT_EQ(0, ts2.tv_sec);
450   ASSERT_LT(ts2.tv_nsec, 1000000);
453 TEST(time, clock) {
454   // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
455   clock_t t0 = clock();
456   sleep(1);
457   clock_t t1 = clock();
458   ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
461 TEST(time, clock_settime) {
462   errno = 0;
463   timespec ts;
464   ASSERT_EQ(-1, clock_settime(-1, &ts));
465   ASSERT_EQ(EINVAL, errno);
468 TEST(time, clock_nanosleep) {
469   timespec in;
470   timespec out;
471   ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
474 // Test to verify that disarming a repeatable timer disables the
475 // callbacks.
476 TEST(time, timer_disarm_terminates) {
477   Counter counter(Counter::CountNotifyFunction);
478   ASSERT_TRUE(counter.timer_valid);
480   ASSERT_EQ(0, counter.value);
482   counter.SetTime(0, 1, 0, 1);
483   ASSERT_TRUE(counter.ValueUpdated());
484   ASSERT_TRUE(counter.ValueUpdated());
485   ASSERT_TRUE(counter.ValueUpdated());
487   counter.SetTime(0, 0, 1, 0);
488   volatile int value = counter.value;
489   usleep(500000);
491   // Verify the counter has not been incremented.
492   ASSERT_EQ(value, counter.value);
495 // Test to verify that deleting a repeatable timer disables the
496 // callbacks.
497 TEST(time, timer_delete_terminates) {
498   Counter counter(Counter::CountNotifyFunction);
499   ASSERT_TRUE(counter.timer_valid);
501   ASSERT_EQ(0, counter.value);
503   counter.SetTime(0, 1, 0, 1);
504   ASSERT_TRUE(counter.ValueUpdated());
505   ASSERT_TRUE(counter.ValueUpdated());
506   ASSERT_TRUE(counter.ValueUpdated());
508   counter.DeleteTimer();
509   volatile int value = counter.value;
510   usleep(500000);
512   // Verify the counter has not been incremented.
513   ASSERT_EQ(value, counter.value);