blob: c9ead8dbb721023a3535ce94f75dfa18153e6f89 [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 Hughesee178bf2013-07-12 11:25:20 -070029TEST(time, gmtime) {
30 time_t t = 0;
31 tm* broken_down = gmtime(&t);
32 ASSERT_TRUE(broken_down != NULL);
33 ASSERT_EQ(0, broken_down->tm_sec);
34 ASSERT_EQ(0, broken_down->tm_min);
35 ASSERT_EQ(0, broken_down->tm_hour);
36 ASSERT_EQ(1, broken_down->tm_mday);
37 ASSERT_EQ(0, broken_down->tm_mon);
38 ASSERT_EQ(1970, broken_down->tm_year + 1900);
39}
Elliott Hughes7843d442013-08-22 11:37:32 -070040
Elliott Hughes329103d2014-04-25 16:55:04 -070041static void* gmtime_no_stack_overflow_14313703_fn(void*) {
42 const char* original_tz = getenv("TZ");
43 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
44 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
45 tzset();
46 if (original_tz != NULL) {
47 setenv("TZ", original_tz, 1);
48 }
49 tzset();
50 return NULL;
51}
52
53TEST(time, gmtime_no_stack_overflow_14313703) {
54 // Is it safe to call tzload on a thread with a small stack?
55 // http://b/14313703
56 // https://code.google.com/p/android/issues/detail?id=61130
57 pthread_attr_t attributes;
58 ASSERT_EQ(0, pthread_attr_init(&attributes));
59#if defined(__BIONIC__)
60 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, PTHREAD_STACK_MIN));
61#else
62 // PTHREAD_STACK_MIN not currently in the host GCC sysroot.
63 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 4 * getpagesize()));
64#endif
65
66 pthread_t t;
67 ASSERT_EQ(0, pthread_create(&t, &attributes, gmtime_no_stack_overflow_14313703_fn, NULL));
68 void* result;
69 ASSERT_EQ(0, pthread_join(t, &result));
70}
71
Elliott Hughes7843d442013-08-22 11:37:32 -070072TEST(time, mktime_10310929) {
73 struct tm t;
74 memset(&t, 0, sizeof(tm));
75 t.tm_year = 200;
76 t.tm_mon = 2;
77 t.tm_mday = 10;
78
Elliott Hughes0c401522013-10-18 16:21:54 -070079#if !defined(__LP64__)
80 // 32-bit bionic stupidly had a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -070081 ASSERT_EQ(-1, mktime(&t));
Elliott Hughes0c401522013-10-18 16:21:54 -070082#else
83 // Everyone else should be using a signed 64-bit time_t.
84 ASSERT_GE(sizeof(time_t) * 8, 64U);
85
86 setenv("TZ", "America/Los_Angeles", 1);
87 tzset();
88 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughes0c401522013-10-18 16:21:54 -070089
90 setenv("TZ", "UTC", 1);
91 tzset();
92 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughes7843d442013-08-22 11:37:32 -070093#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -080094}
Elliott Hughes4b558f52014-03-04 15:58:02 -080095
Elliott Hughes3e3409a2014-03-10 18:19:03 -070096TEST(time, strftime) {
97 setenv("TZ", "UTC", 1);
98
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
105 char buf[64];
106
107 // Seconds since the epoch.
108#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
109 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
110 EXPECT_STREQ("4108320000", buf);
111#endif
112
113 // Date and time as text.
114 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
115 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
116}
117
118TEST(time, strptime) {
119 setenv("TZ", "UTC", 1);
120
121 struct tm t;
122 char buf[64];
123
124 memset(&t, 0, sizeof(t));
125 strptime("11:14", "%R", &t);
126 strftime(buf, sizeof(buf), "%H:%M", &t);
127 EXPECT_STREQ("11:14", buf);
128
129 memset(&t, 0, sizeof(t));
130 strptime("09:41:53", "%T", &t);
131 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
132 EXPECT_STREQ("09:41:53", buf);
133}
134
Elliott Hughes4b558f52014-03-04 15:58:02 -0800135void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
136 itimerspec ts;
137 ts.it_value.tv_sec = value_s;
138 ts.it_value.tv_nsec = value_ns;
139 ts.it_interval.tv_sec = interval_s;
140 ts.it_interval.tv_nsec = interval_ns;
141 ASSERT_EQ(0, timer_settime(t, TIMER_ABSTIME, &ts, NULL));
142}
143
144static void NoOpNotifyFunction(sigval_t) {
145}
146
147TEST(time, timer_create) {
148 sigevent_t se;
149 memset(&se, 0, sizeof(se));
150 se.sigev_notify = SIGEV_THREAD;
151 se.sigev_notify_function = NoOpNotifyFunction;
152 timer_t timer_id;
153 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
154
155 int pid = fork();
156 ASSERT_NE(-1, pid) << strerror(errno);
157
158 if (pid == 0) {
159 // Timers are not inherited by the child.
160 ASSERT_EQ(-1, timer_delete(timer_id));
161 ASSERT_EQ(EINVAL, errno);
162 _exit(0);
163 }
164
165 int status;
166 ASSERT_EQ(pid, waitpid(pid, &status, 0));
167 ASSERT_TRUE(WIFEXITED(status));
168 ASSERT_EQ(0, WEXITSTATUS(status));
169
170 ASSERT_EQ(0, timer_delete(timer_id));
171}
172
173static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
174static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
175 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
176 ASSERT_EQ(SIGUSR1, signal_number);
177}
178
179TEST(time, timer_create_SIGEV_SIGNAL) {
180 sigevent_t se;
181 memset(&se, 0, sizeof(se));
182 se.sigev_notify = SIGEV_SIGNAL;
183 se.sigev_signo = SIGUSR1;
184
185 timer_t timer_id;
186 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
187
188 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
189
190 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
191
192 itimerspec ts;
193 ts.it_value.tv_sec = 0;
194 ts.it_value.tv_nsec = 1;
195 ts.it_interval.tv_sec = 0;
196 ts.it_interval.tv_nsec = 0;
197 ASSERT_EQ(0, timer_settime(timer_id, TIMER_ABSTIME, &ts, NULL));
198
199 usleep(500000);
200 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
201}
202
203struct Counter {
204 volatile int value;
205 timer_t timer_id;
206 sigevent_t se;
207
208 Counter(void (*fn)(sigval_t)) : value(0) {
209 memset(&se, 0, sizeof(se));
210 se.sigev_notify = SIGEV_THREAD;
211 se.sigev_notify_function = fn;
212 se.sigev_value.sival_ptr = this;
213 }
214
215 void Create() {
216 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
217 }
218
219 ~Counter() {
220 if (timer_delete(timer_id) != 0) {
221 abort();
222 }
223 }
224
225 static void CountNotifyFunction(sigval_t value) {
226 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
227 ++cd->value;
228 }
229
230 static void CountAndDisarmNotifyFunction(sigval_t value) {
231 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
232 ++cd->value;
233
234 // Setting the initial expiration time to 0 disarms the timer.
235 SetTime(cd->timer_id, 0, 0, 1, 0);
236 }
237};
238
239TEST(time, timer_settime_0) {
240 Counter counter(Counter::CountAndDisarmNotifyFunction);
241 counter.Create();
242
243 ASSERT_EQ(0, counter.value);
244
245 SetTime(counter.timer_id, 0, 1, 1, 0);
246 usleep(500000);
247
248 // The count should just be 1 because we disarmed the timer the first time it fired.
249 ASSERT_EQ(1, counter.value);
250}
251
252TEST(time, timer_settime_repeats) {
253 Counter counter(Counter::CountNotifyFunction);
254 counter.Create();
255
256 ASSERT_EQ(0, counter.value);
257
258 SetTime(counter.timer_id, 0, 1, 0, 10);
259 usleep(500000);
260
261 // The count should just be > 1 because we let the timer repeat.
262 ASSERT_GT(counter.value, 1);
263}
264
265static int timer_create_NULL_signal_handler_invocation_count = 0;
266static void timer_create_NULL_signal_handler(int signal_number) {
267 ++timer_create_NULL_signal_handler_invocation_count;
268 ASSERT_EQ(SIGALRM, signal_number);
269}
270
271TEST(time, timer_create_NULL) {
272 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
273 timer_t timer_id;
274 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
275
276 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
277
278 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
279
280 SetTime(timer_id, 0, 1, 0, 0);
281 usleep(500000);
282
283 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
284}
285
286TEST(time, timer_create_EINVAL) {
287 clockid_t invalid_clock = 16;
288
289 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
290 timer_t timer_id;
291 ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id));
292 ASSERT_EQ(EINVAL, errno);
293
294 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
295 sigevent_t se;
296 memset(&se, 0, sizeof(se));
297 se.sigev_notify = SIGEV_THREAD;
298 se.sigev_notify_function = NoOpNotifyFunction;
299 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
300 ASSERT_EQ(EINVAL, errno);
301}
302
303TEST(time, timer_delete_multiple) {
304 timer_t timer_id;
305 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
306 ASSERT_EQ(0, timer_delete(timer_id));
307 ASSERT_EQ(-1, timer_delete(timer_id));
308 ASSERT_EQ(EINVAL, errno);
309
310 sigevent_t se;
311 memset(&se, 0, sizeof(se));
312 se.sigev_notify = SIGEV_THREAD;
313 se.sigev_notify_function = NoOpNotifyFunction;
314 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
315 ASSERT_EQ(0, timer_delete(timer_id));
316 ASSERT_EQ(-1, timer_delete(timer_id));
317 ASSERT_EQ(EINVAL, errno);
318}
319
320TEST(time, timer_create_multiple) {
321 Counter counter1(Counter::CountNotifyFunction);
322 counter1.Create();
323 Counter counter2(Counter::CountNotifyFunction);
324 counter2.Create();
325 Counter counter3(Counter::CountNotifyFunction);
326 counter3.Create();
327
328 ASSERT_EQ(0, counter1.value);
329 ASSERT_EQ(0, counter2.value);
330 ASSERT_EQ(0, counter3.value);
331
332 SetTime(counter2.timer_id, 0, 1, 0, 0);
333 usleep(500000);
334
335 EXPECT_EQ(0, counter1.value);
336 EXPECT_EQ(1, counter2.value);
337 EXPECT_EQ(0, counter3.value);
338}
Christopher Ferris753ad772014-03-20 20:47:45 -0700339
340struct TimerDeleteData {
341 timer_t timer_id;
342 pthread_t thread_id;
343 volatile bool complete;
344};
345
346static void TimerDeleteCallback(sigval_t value) {
347 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
348
349 tdd->thread_id = pthread_self();
350 timer_delete(tdd->timer_id);
351 tdd->complete = true;
352}
353
354TEST(time, timer_delete_from_timer_thread) {
355 TimerDeleteData tdd;
356 sigevent_t se;
357
358 memset(&se, 0, sizeof(se));
359 se.sigev_notify = SIGEV_THREAD;
360 se.sigev_notify_function = TimerDeleteCallback;
361 se.sigev_value.sival_ptr = &tdd;
362
363 tdd.complete = false;
364 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
365
366 itimerspec ts;
367 ts.it_value.tv_sec = 0;
368 ts.it_value.tv_nsec = 100;
369 ts.it_interval.tv_sec = 0;
370 ts.it_interval.tv_nsec = 0;
371 ASSERT_EQ(0, timer_settime(tdd.timer_id, TIMER_ABSTIME, &ts, NULL));
372
373 time_t cur_time = time(NULL);
374 while (!tdd.complete && (time(NULL) - cur_time) < 5);
375 ASSERT_TRUE(tdd.complete);
376
377#if defined(__BIONIC__)
378 // Since bionic timers are implemented by creating a thread to handle the
379 // callback, verify that the thread actually completes.
380 cur_time = time(NULL);
381 while (pthread_detach(tdd.thread_id) != ESRCH && (time(NULL) - cur_time) < 5);
382 ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id));
383#endif
384}
Elliott Hughes625993d2014-07-15 16:53:13 -0700385
386TEST(time, clock_gettime) {
387 // Try to ensure that our vdso clock_gettime is working.
388 timespec ts1;
389 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
390 timespec ts2;
391 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
392
393 // What's the difference between the two?
394 ts2.tv_sec -= ts1.tv_sec;
395 ts2.tv_nsec -= ts1.tv_nsec;
396 if (ts2.tv_nsec < 0) {
397 --ts2.tv_sec;
398 ts2.tv_nsec += 1000000000;
399 }
400
401 // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
402 ASSERT_EQ(0, ts2.tv_sec);
403 ASSERT_LT(ts2.tv_nsec, 1000000);
404}