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>
26 #include <unistd.h>
28 #include "ScopedSignalHandler.h"
30 #include "private/bionic_constants.h"
32 TEST(time, gmtime) {
33 time_t t = 0;
34 tm* broken_down = gmtime(&t);
35 ASSERT_TRUE(broken_down != NULL);
36 ASSERT_EQ(0, broken_down->tm_sec);
37 ASSERT_EQ(0, broken_down->tm_min);
38 ASSERT_EQ(0, broken_down->tm_hour);
39 ASSERT_EQ(1, broken_down->tm_mday);
40 ASSERT_EQ(0, broken_down->tm_mon);
41 ASSERT_EQ(1970, broken_down->tm_year + 1900);
42 }
44 static void* gmtime_no_stack_overflow_14313703_fn(void*) {
45 const char* original_tz = getenv("TZ");
46 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
47 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
48 tzset();
49 if (original_tz != NULL) {
50 setenv("TZ", original_tz, 1);
51 }
52 tzset();
53 return NULL;
54 }
56 TEST(time, gmtime_no_stack_overflow_14313703) {
57 // Is it safe to call tzload on a thread with a small stack?
58 // http://b/14313703
59 // https://code.google.com/p/android/issues/detail?id=61130
60 pthread_attr_t attributes;
61 ASSERT_EQ(0, pthread_attr_init(&attributes));
62 #if defined(__BIONIC__)
63 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, PTHREAD_STACK_MIN));
64 #else
65 // PTHREAD_STACK_MIN not currently in the host GCC sysroot.
66 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 4 * getpagesize()));
67 #endif
69 pthread_t t;
70 ASSERT_EQ(0, pthread_create(&t, &attributes, gmtime_no_stack_overflow_14313703_fn, NULL));
71 void* result;
72 ASSERT_EQ(0, pthread_join(t, &result));
73 }
75 TEST(time, mktime_empty_TZ) {
76 // tzcode used to have a bug where it didn't reinitialize some internal state.
78 // Choose a time where DST is set.
79 struct tm t;
80 memset(&t, 0, sizeof(tm));
81 t.tm_year = 1980 - 1900;
82 t.tm_mon = 6;
83 t.tm_mday = 2;
85 setenv("TZ", "America/Los_Angeles", 1);
86 tzset();
87 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
89 memset(&t, 0, sizeof(tm));
90 t.tm_year = 1980 - 1900;
91 t.tm_mon = 6;
92 t.tm_mday = 2;
94 setenv("TZ", "", 1); // Implies UTC.
95 tzset();
96 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
97 }
99 TEST(time, mktime_10310929) {
100 struct tm t;
101 memset(&t, 0, sizeof(tm));
102 t.tm_year = 200;
103 t.tm_mon = 2;
104 t.tm_mday = 10;
106 #if !defined(__LP64__)
107 // 32-bit bionic stupidly had a signed 32-bit time_t.
108 ASSERT_EQ(-1, mktime(&t));
109 #else
110 // Everyone else should be using a signed 64-bit time_t.
111 ASSERT_GE(sizeof(time_t) * 8, 64U);
113 setenv("TZ", "America/Los_Angeles", 1);
114 tzset();
115 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
117 setenv("TZ", "UTC", 1);
118 tzset();
119 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
120 #endif
121 }
123 TEST(time, strftime) {
124 setenv("TZ", "UTC", 1);
126 struct tm t;
127 memset(&t, 0, sizeof(tm));
128 t.tm_year = 200;
129 t.tm_mon = 2;
130 t.tm_mday = 10;
132 char buf[64];
134 // Seconds since the epoch.
135 #if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
136 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
137 EXPECT_STREQ("4108320000", buf);
138 #endif
140 // Date and time as text.
141 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
142 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
143 }
145 TEST(time, strptime) {
146 setenv("TZ", "UTC", 1);
148 struct tm t;
149 char buf[64];
151 memset(&t, 0, sizeof(t));
152 strptime("11:14", "%R", &t);
153 strftime(buf, sizeof(buf), "%H:%M", &t);
154 EXPECT_STREQ("11:14", buf);
156 memset(&t, 0, sizeof(t));
157 strptime("09:41:53", "%T", &t);
158 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
159 EXPECT_STREQ("09:41:53", buf);
160 }
162 void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
163 itimerspec ts;
164 ts.it_value.tv_sec = value_s;
165 ts.it_value.tv_nsec = value_ns;
166 ts.it_interval.tv_sec = interval_s;
167 ts.it_interval.tv_nsec = interval_ns;
168 ASSERT_EQ(0, timer_settime(t, TIMER_ABSTIME, &ts, NULL));
169 }
171 static void NoOpNotifyFunction(sigval_t) {
172 }
174 TEST(time, timer_create) {
175 sigevent_t se;
176 memset(&se, 0, sizeof(se));
177 se.sigev_notify = SIGEV_THREAD;
178 se.sigev_notify_function = NoOpNotifyFunction;
179 timer_t timer_id;
180 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
182 int pid = fork();
183 ASSERT_NE(-1, pid) << strerror(errno);
185 if (pid == 0) {
186 // Timers are not inherited by the child.
187 ASSERT_EQ(-1, timer_delete(timer_id));
188 ASSERT_EQ(EINVAL, errno);
189 _exit(0);
190 }
192 int status;
193 ASSERT_EQ(pid, waitpid(pid, &status, 0));
194 ASSERT_TRUE(WIFEXITED(status));
195 ASSERT_EQ(0, WEXITSTATUS(status));
197 ASSERT_EQ(0, timer_delete(timer_id));
198 }
200 static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
201 static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
202 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
203 ASSERT_EQ(SIGUSR1, signal_number);
204 }
206 TEST(time, timer_create_SIGEV_SIGNAL) {
207 sigevent_t se;
208 memset(&se, 0, sizeof(se));
209 se.sigev_notify = SIGEV_SIGNAL;
210 se.sigev_signo = SIGUSR1;
212 timer_t timer_id;
213 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
215 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
217 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
219 itimerspec ts;
220 ts.it_value.tv_sec = 0;
221 ts.it_value.tv_nsec = 1;
222 ts.it_interval.tv_sec = 0;
223 ts.it_interval.tv_nsec = 0;
224 ASSERT_EQ(0, timer_settime(timer_id, TIMER_ABSTIME, &ts, NULL));
226 usleep(500000);
227 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
228 }
230 struct Counter {
231 volatile int value;
232 timer_t timer_id;
233 sigevent_t se;
234 bool timer_valid;
236 Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
237 memset(&se, 0, sizeof(se));
238 se.sigev_notify = SIGEV_THREAD;
239 se.sigev_notify_function = fn;
240 se.sigev_value.sival_ptr = this;
241 Create();
242 }
244 void Create() {
245 ASSERT_FALSE(timer_valid);
246 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
247 timer_valid = true;
248 }
250 void DeleteTimer() {
251 ASSERT_TRUE(timer_valid);
252 ASSERT_EQ(0, timer_delete(timer_id));
253 timer_valid = false;
254 }
256 ~Counter() {
257 if (timer_valid) {
258 DeleteTimer();
259 }
260 }
262 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
263 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
264 }
266 bool ValueUpdated() {
267 volatile int current_value = value;
268 time_t start = time(NULL);
269 while (current_value == value && (time(NULL) - start) < 5) {
270 }
271 return current_value != value;
272 }
274 static void CountNotifyFunction(sigval_t value) {
275 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
276 ++cd->value;
277 }
279 static void CountAndDisarmNotifyFunction(sigval_t value) {
280 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
281 ++cd->value;
283 // Setting the initial expiration time to 0 disarms the timer.
284 cd->SetTime(0, 0, 1, 0);
285 }
286 };
288 TEST(time, timer_settime_0) {
289 Counter counter(Counter::CountAndDisarmNotifyFunction);
290 ASSERT_TRUE(counter.timer_valid);
292 ASSERT_EQ(0, counter.value);
294 counter.SetTime(0, 1, 1, 0);
295 usleep(500000);
297 // The count should just be 1 because we disarmed the timer the first time it fired.
298 ASSERT_EQ(1, counter.value);
299 }
301 TEST(time, timer_settime_repeats) {
302 Counter counter(Counter::CountNotifyFunction);
303 ASSERT_TRUE(counter.timer_valid);
305 ASSERT_EQ(0, counter.value);
307 counter.SetTime(0, 1, 0, 10);
308 ASSERT_TRUE(counter.ValueUpdated());
309 ASSERT_TRUE(counter.ValueUpdated());
310 ASSERT_TRUE(counter.ValueUpdated());
311 }
313 static int timer_create_NULL_signal_handler_invocation_count = 0;
314 static void timer_create_NULL_signal_handler(int signal_number) {
315 ++timer_create_NULL_signal_handler_invocation_count;
316 ASSERT_EQ(SIGALRM, signal_number);
317 }
319 TEST(time, timer_create_NULL) {
320 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
321 timer_t timer_id;
322 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
324 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
326 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
328 SetTime(timer_id, 0, 1, 0, 0);
329 usleep(500000);
331 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
332 }
334 TEST(time, timer_create_EINVAL) {
335 clockid_t invalid_clock = 16;
337 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
338 timer_t timer_id;
339 ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id));
340 ASSERT_EQ(EINVAL, errno);
342 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
343 sigevent_t se;
344 memset(&se, 0, sizeof(se));
345 se.sigev_notify = SIGEV_THREAD;
346 se.sigev_notify_function = NoOpNotifyFunction;
347 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
348 ASSERT_EQ(EINVAL, errno);
349 }
351 TEST(time, timer_delete_multiple) {
352 timer_t timer_id;
353 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
354 ASSERT_EQ(0, timer_delete(timer_id));
355 ASSERT_EQ(-1, timer_delete(timer_id));
356 ASSERT_EQ(EINVAL, errno);
358 sigevent_t se;
359 memset(&se, 0, sizeof(se));
360 se.sigev_notify = SIGEV_THREAD;
361 se.sigev_notify_function = NoOpNotifyFunction;
362 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
363 ASSERT_EQ(0, timer_delete(timer_id));
364 ASSERT_EQ(-1, timer_delete(timer_id));
365 ASSERT_EQ(EINVAL, errno);
366 }
368 TEST(time, timer_create_multiple) {
369 Counter counter1(Counter::CountNotifyFunction);
370 ASSERT_TRUE(counter1.timer_valid);
371 Counter counter2(Counter::CountNotifyFunction);
372 ASSERT_TRUE(counter2.timer_valid);
373 Counter counter3(Counter::CountNotifyFunction);
374 ASSERT_TRUE(counter3.timer_valid);
376 ASSERT_EQ(0, counter1.value);
377 ASSERT_EQ(0, counter2.value);
378 ASSERT_EQ(0, counter3.value);
380 counter2.SetTime(0, 1, 0, 0);
381 usleep(500000);
383 EXPECT_EQ(0, counter1.value);
384 EXPECT_EQ(1, counter2.value);
385 EXPECT_EQ(0, counter3.value);
386 }
388 struct TimerDeleteData {
389 timer_t timer_id;
390 pthread_t thread_id;
391 volatile bool complete;
392 };
394 static void TimerDeleteCallback(sigval_t value) {
395 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
397 tdd->thread_id = pthread_self();
398 timer_delete(tdd->timer_id);
399 tdd->complete = true;
400 }
402 TEST(time, timer_delete_from_timer_thread) {
403 TimerDeleteData tdd;
404 sigevent_t se;
406 memset(&se, 0, sizeof(se));
407 se.sigev_notify = SIGEV_THREAD;
408 se.sigev_notify_function = TimerDeleteCallback;
409 se.sigev_value.sival_ptr = &tdd;
411 tdd.complete = false;
412 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
414 itimerspec ts;
415 ts.it_value.tv_sec = 0;
416 ts.it_value.tv_nsec = 100;
417 ts.it_interval.tv_sec = 0;
418 ts.it_interval.tv_nsec = 0;
419 ASSERT_EQ(0, timer_settime(tdd.timer_id, TIMER_ABSTIME, &ts, NULL));
421 time_t cur_time = time(NULL);
422 while (!tdd.complete && (time(NULL) - cur_time) < 5);
423 ASSERT_TRUE(tdd.complete);
425 #if defined(__BIONIC__)
426 // Since bionic timers are implemented by creating a thread to handle the
427 // callback, verify that the thread actually completes.
428 cur_time = time(NULL);
429 while (pthread_detach(tdd.thread_id) != ESRCH && (time(NULL) - cur_time) < 5);
430 ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id));
431 #endif
432 }
434 TEST(time, clock_gettime) {
435 // Try to ensure that our vdso clock_gettime is working.
436 timespec ts1;
437 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
438 timespec ts2;
439 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
441 // What's the difference between the two?
442 ts2.tv_sec -= ts1.tv_sec;
443 ts2.tv_nsec -= ts1.tv_nsec;
444 if (ts2.tv_nsec < 0) {
445 --ts2.tv_sec;
446 ts2.tv_nsec += NS_PER_S;
447 }
449 // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
450 ASSERT_EQ(0, ts2.tv_sec);
451 ASSERT_LT(ts2.tv_nsec, 1000000);
452 }
454 TEST(time, clock) {
455 // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
456 clock_t t0 = clock();
457 sleep(1);
458 clock_t t1 = clock();
459 ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
460 }
462 pid_t GetInvalidPid() {
463 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
464 long pid_max;
465 fscanf(fp, "%ld", &pid_max);
466 pid_t invalid_pid = static_cast<pid_t>(pid_max + 1);
467 fclose(fp);
468 return invalid_pid;
469 }
471 TEST(time, clock_getcpuclockid) {
472 // For current process.
473 clockid_t clockid;
474 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
476 timespec ts;
477 ASSERT_EQ(0, clock_gettime(clockid, &ts));
479 // For parent process.
480 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
481 ASSERT_EQ(0, clock_gettime(clockid, &ts));
483 // For invalid process.
484 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
485 errno = 0;
486 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid));
487 ASSERT_EQ(0, errno);
488 }
490 TEST(time, clock_settime) {
491 errno = 0;
492 timespec ts;
493 ASSERT_EQ(-1, clock_settime(-1, &ts));
494 ASSERT_EQ(EINVAL, errno);
495 }
497 TEST(time, clock_nanosleep) {
498 timespec in;
499 timespec out;
500 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
501 }
503 // Test to verify that disarming a repeatable timer disables the
504 // callbacks.
505 TEST(time, timer_disarm_terminates) {
506 Counter counter(Counter::CountNotifyFunction);
507 ASSERT_TRUE(counter.timer_valid);
509 ASSERT_EQ(0, counter.value);
511 counter.SetTime(0, 1, 0, 1);
512 ASSERT_TRUE(counter.ValueUpdated());
513 ASSERT_TRUE(counter.ValueUpdated());
514 ASSERT_TRUE(counter.ValueUpdated());
516 counter.SetTime(0, 0, 1, 0);
517 volatile int value = counter.value;
518 usleep(500000);
520 // Verify the counter has not been incremented.
521 ASSERT_EQ(value, counter.value);
522 }
524 // Test to verify that deleting a repeatable timer disables the
525 // callbacks.
526 TEST(time, timer_delete_terminates) {
527 Counter counter(Counter::CountNotifyFunction);
528 ASSERT_TRUE(counter.timer_valid);
530 ASSERT_EQ(0, counter.value);
532 counter.SetTime(0, 1, 0, 1);
533 ASSERT_TRUE(counter.ValueUpdated());
534 ASSERT_TRUE(counter.ValueUpdated());
535 ASSERT_TRUE(counter.ValueUpdated());
537 counter.DeleteTimer();
538 volatile int value = counter.value;
539 usleep(500000);
541 // Verify the counter has not been incremented.
542 ASSERT_EQ(value, counter.value);
543 }