blob: e0231b1ca36aca1ffb2c1fbd9abd9cb96ddc8932 [file] [log] [blame]
Elliott Hughese0175ca2013-03-14 14:38:08 -07001/*
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 Hughes4b558f52014-03-04 15:58:02 -080017#include <time.h>
18
19#include <errno.h>
Elliott Hughese0175ca2013-03-14 14:38:08 -070020#include <gtest/gtest.h>
Elliott Hughes329103d2014-04-25 16:55:04 -070021#include <pthread.h>
Elliott Hughes4b558f52014-03-04 15:58:02 -080022#include <signal.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070023#include <sys/syscall.h>
Brian Carlstrombe1d91d2014-03-08 15:05:26 -080024#include <sys/types.h>
25#include <sys/wait.h>
Elliott Hughese0175ca2013-03-14 14:38:08 -070026
Elliott Hughes4b558f52014-03-04 15:58:02 -080027#include "ScopedSignalHandler.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070028
Elliott Hughes04303f52014-09-18 16:11:59 -070029#include "private/bionic_constants.h"
30
Elliott Hughesee178bf2013-07-12 11:25:20 -070031TEST(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}
Elliott Hughes7843d442013-08-22 11:37:32 -070042
Elliott Hughes329103d2014-04-25 16:55:04 -070043static 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}
54
55TEST(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
67
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}
73
Satoru Takeuchi154e2022014-05-27 17:04:04 +090074TEST(time, mktime_empty_TZ) {
75 // tzcode used to have a bug where it didn't reinitialize some internal state.
76
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;
83
84 setenv("TZ", "America/Los_Angeles", 1);
85 tzset();
86 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
87
88 memset(&t, 0, sizeof(tm));
89 t.tm_year = 1980 - 1900;
90 t.tm_mon = 6;
91 t.tm_mday = 2;
92
93 setenv("TZ", "", 1); // Implies UTC.
94 tzset();
95 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
96}
97
Elliott Hughes7843d442013-08-22 11:37:32 -070098TEST(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;
104
Elliott Hughes0c401522013-10-18 16:21:54 -0700105#if !defined(__LP64__)
106 // 32-bit bionic stupidly had a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700107 ASSERT_EQ(-1, mktime(&t));
Elliott Hughes0c401522013-10-18 16:21:54 -0700108#else
109 // Everyone else should be using a signed 64-bit time_t.
110 ASSERT_GE(sizeof(time_t) * 8, 64U);
111
112 setenv("TZ", "America/Los_Angeles", 1);
113 tzset();
114 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughes0c401522013-10-18 16:21:54 -0700115
116 setenv("TZ", "UTC", 1);
117 tzset();
118 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughes7843d442013-08-22 11:37:32 -0700119#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800120}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800121
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700122TEST(time, strftime) {
123 setenv("TZ", "UTC", 1);
124
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;
130
131 char buf[64];
132
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
138
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);
142}
143
144TEST(time, strptime) {
145 setenv("TZ", "UTC", 1);
146
147 struct tm t;
148 char buf[64];
149
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);
154
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);
159}
160
Elliott Hughes4b558f52014-03-04 15:58:02 -0800161void 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));
168}
169
170static void NoOpNotifyFunction(sigval_t) {
171}
172
173TEST(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));
180
181 int pid = fork();
182 ASSERT_NE(-1, pid) << strerror(errno);
183
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 }
190
191 int status;
192 ASSERT_EQ(pid, waitpid(pid, &status, 0));
193 ASSERT_TRUE(WIFEXITED(status));
194 ASSERT_EQ(0, WEXITSTATUS(status));
195
196 ASSERT_EQ(0, timer_delete(timer_id));
197}
198
199static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
200static 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);
203}
204
205TEST(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;
210
211 timer_t timer_id;
212 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
213
214 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
215
216 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
217
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));
224
225 usleep(500000);
226 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
227}
228
229struct Counter {
230 volatile int value;
231 timer_t timer_id;
232 sigevent_t se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700233 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800234
Christopher Ferris62d84b12014-10-20 19:09:19 -0700235 Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800236 memset(&se, 0, sizeof(se));
237 se.sigev_notify = SIGEV_THREAD;
238 se.sigev_notify_function = fn;
239 se.sigev_value.sival_ptr = this;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700240 Create();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800241 }
242
243 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700244 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800245 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700246 timer_valid = true;
247 }
248
249 void DeleteTimer() {
250 ASSERT_TRUE(timer_valid);
251 ASSERT_EQ(0, timer_delete(timer_id));
252 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800253 }
254
255 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700256 if (timer_valid) {
257 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800258 }
259 }
260
Christopher Ferris62d84b12014-10-20 19:09:19 -0700261 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 }
264
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 }
272
Elliott Hughes4b558f52014-03-04 15:58:02 -0800273 static void CountNotifyFunction(sigval_t value) {
274 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
275 ++cd->value;
276 }
277
278 static void CountAndDisarmNotifyFunction(sigval_t value) {
279 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
280 ++cd->value;
281
282 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700283 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800284 }
285};
286
287TEST(time, timer_settime_0) {
288 Counter counter(Counter::CountAndDisarmNotifyFunction);
Christopher Ferris62d84b12014-10-20 19:09:19 -0700289 ASSERT_TRUE(counter.timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800290
291 ASSERT_EQ(0, counter.value);
292
Christopher Ferris62d84b12014-10-20 19:09:19 -0700293 counter.SetTime(0, 1, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800294 usleep(500000);
295
296 // The count should just be 1 because we disarmed the timer the first time it fired.
297 ASSERT_EQ(1, counter.value);
298}
299
300TEST(time, timer_settime_repeats) {
301 Counter counter(Counter::CountNotifyFunction);
Christopher Ferris62d84b12014-10-20 19:09:19 -0700302 ASSERT_TRUE(counter.timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800303
304 ASSERT_EQ(0, counter.value);
305
Christopher Ferris62d84b12014-10-20 19:09:19 -0700306 counter.SetTime(0, 1, 0, 10);
307 ASSERT_TRUE(counter.ValueUpdated());
308 ASSERT_TRUE(counter.ValueUpdated());
309 ASSERT_TRUE(counter.ValueUpdated());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800310}
311
312static int timer_create_NULL_signal_handler_invocation_count = 0;
313static void timer_create_NULL_signal_handler(int signal_number) {
314 ++timer_create_NULL_signal_handler_invocation_count;
315 ASSERT_EQ(SIGALRM, signal_number);
316}
317
318TEST(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));
322
323 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
324
325 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
326
327 SetTime(timer_id, 0, 1, 0, 0);
328 usleep(500000);
329
330 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
331}
332
333TEST(time, timer_create_EINVAL) {
334 clockid_t invalid_clock = 16;
335
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);
340
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);
348}
349
350TEST(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);
356
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);
365}
366
367TEST(time, timer_create_multiple) {
368 Counter counter1(Counter::CountNotifyFunction);
Christopher Ferris62d84b12014-10-20 19:09:19 -0700369 ASSERT_TRUE(counter1.timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800370 Counter counter2(Counter::CountNotifyFunction);
Christopher Ferris62d84b12014-10-20 19:09:19 -0700371 ASSERT_TRUE(counter2.timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800372 Counter counter3(Counter::CountNotifyFunction);
Christopher Ferris62d84b12014-10-20 19:09:19 -0700373 ASSERT_TRUE(counter3.timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800374
375 ASSERT_EQ(0, counter1.value);
376 ASSERT_EQ(0, counter2.value);
377 ASSERT_EQ(0, counter3.value);
378
Christopher Ferris62d84b12014-10-20 19:09:19 -0700379 counter2.SetTime(0, 1, 0, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800380 usleep(500000);
381
382 EXPECT_EQ(0, counter1.value);
383 EXPECT_EQ(1, counter2.value);
384 EXPECT_EQ(0, counter3.value);
385}
Christopher Ferris753ad772014-03-20 20:47:45 -0700386
387struct TimerDeleteData {
388 timer_t timer_id;
389 pthread_t thread_id;
390 volatile bool complete;
391};
392
393static void TimerDeleteCallback(sigval_t value) {
394 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
395
396 tdd->thread_id = pthread_self();
397 timer_delete(tdd->timer_id);
398 tdd->complete = true;
399}
400
401TEST(time, timer_delete_from_timer_thread) {
402 TimerDeleteData tdd;
403 sigevent_t se;
404
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;
409
410 tdd.complete = false;
411 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
412
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));
419
420 time_t cur_time = time(NULL);
421 while (!tdd.complete && (time(NULL) - cur_time) < 5);
422 ASSERT_TRUE(tdd.complete);
423
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
431}
Elliott Hughes625993d2014-07-15 16:53:13 -0700432
433TEST(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));
439
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;
Elliott Hughes04303f52014-09-18 16:11:59 -0700445 ts2.tv_nsec += NS_PER_S;
Elliott Hughes625993d2014-07-15 16:53:13 -0700446 }
447
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);
451}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700452
453TEST(time, clock) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900454 // 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);
459}
460
461TEST(time, clock_settime) {
462 errno = 0;
463 timespec ts;
464 ASSERT_EQ(-1, clock_settime(-1, &ts));
465 ASSERT_EQ(EINVAL, errno);
466}
467
468TEST(time, clock_nanosleep) {
469 timespec in;
470 timespec out;
471 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700472}
Christopher Ferris62d84b12014-10-20 19:09:19 -0700473
474// Test to verify that disarming a repeatable timer disables the
475// callbacks.
476TEST(time, timer_disarm_terminates) {
477 Counter counter(Counter::CountNotifyFunction);
478 ASSERT_TRUE(counter.timer_valid);
479
480 ASSERT_EQ(0, counter.value);
481
482 counter.SetTime(0, 1, 0, 1);
483 ASSERT_TRUE(counter.ValueUpdated());
484 ASSERT_TRUE(counter.ValueUpdated());
485 ASSERT_TRUE(counter.ValueUpdated());
486
487 counter.SetTime(0, 0, 1, 0);
488 volatile int value = counter.value;
489 usleep(500000);
490
491 // Verify the counter has not been incremented.
492 ASSERT_EQ(value, counter.value);
493}
494
495// Test to verify that deleting a repeatable timer disables the
496// callbacks.
497TEST(time, timer_delete_terminates) {
498 Counter counter(Counter::CountNotifyFunction);
499 ASSERT_TRUE(counter.timer_valid);
500
501 ASSERT_EQ(0, counter.value);
502
503 counter.SetTime(0, 1, 0, 1);
504 ASSERT_TRUE(counter.ValueUpdated());
505 ASSERT_TRUE(counter.ValueUpdated());
506 ASSERT_TRUE(counter.ValueUpdated());
507
508 counter.DeleteTimer();
509 volatile int value = counter.value;
510 usleep(500000);
511
512 // Verify the counter has not been incremented.
513 ASSERT_EQ(value, counter.value);
514}