blob: 10399c5ff21e8f421e1ac829809a6f8cb88df008 [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>
Yabin Cuid5c65272014-11-26 14:04:26 -080026#include <unistd.h>
Yabin Cui95f1ee22015-01-13 19:53:15 -080027#include <atomic>
Elliott Hughese0175ca2013-03-14 14:38:08 -070028
Elliott Hughes4b558f52014-03-04 15:58:02 -080029#include "ScopedSignalHandler.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080030#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070031
Elliott Hughes04303f52014-09-18 16:11:59 -070032#include "private/bionic_constants.h"
33
Elliott Hughesee178bf2013-07-12 11:25:20 -070034TEST(time, gmtime) {
35 time_t t = 0;
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);
44}
Elliott Hughes7843d442013-08-22 11:37:32 -070045
Elliott Hughes329103d2014-04-25 16:55:04 -070046static 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);
50 tzset();
51 if (original_tz != NULL) {
52 setenv("TZ", original_tz, 1);
53 }
54 tzset();
55 return NULL;
56}
57
58TEST(time, gmtime_no_stack_overflow_14313703) {
59 // Is it safe to call tzload on a thread with a small stack?
60 // http://b/14313703
61 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -080062 pthread_attr_t a;
63 ASSERT_EQ(0, pthread_attr_init(&a));
64 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -070065
66 pthread_t t;
Elliott Hughes43f7c872016-02-05 11:18:41 -080067 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, NULL));
68 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -070069}
70
Satoru Takeuchi154e2022014-05-27 17:04:04 +090071TEST(time, mktime_empty_TZ) {
72 // tzcode used to have a bug where it didn't reinitialize some internal state.
73
74 // Choose a time where DST is set.
75 struct tm t;
76 memset(&t, 0, sizeof(tm));
77 t.tm_year = 1980 - 1900;
78 t.tm_mon = 6;
79 t.tm_mday = 2;
80
81 setenv("TZ", "America/Los_Angeles", 1);
82 tzset();
83 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
84
85 memset(&t, 0, sizeof(tm));
86 t.tm_year = 1980 - 1900;
87 t.tm_mon = 6;
88 t.tm_mday = 2;
89
90 setenv("TZ", "", 1); // Implies UTC.
91 tzset();
92 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
93}
94
Elliott Hughes7843d442013-08-22 11:37:32 -070095TEST(time, mktime_10310929) {
96 struct tm t;
97 memset(&t, 0, sizeof(tm));
98 t.tm_year = 200;
99 t.tm_mon = 2;
100 t.tm_mday = 10;
101
Elliott Hughes0c401522013-10-18 16:21:54 -0700102#if !defined(__LP64__)
103 // 32-bit bionic stupidly had a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700104 ASSERT_EQ(-1, mktime(&t));
Elliott Hughes0c401522013-10-18 16:21:54 -0700105#else
106 // Everyone else should be using a signed 64-bit time_t.
107 ASSERT_GE(sizeof(time_t) * 8, 64U);
108
109 setenv("TZ", "America/Los_Angeles", 1);
110 tzset();
111 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughes0c401522013-10-18 16:21:54 -0700112
113 setenv("TZ", "UTC", 1);
114 tzset();
115 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughes7843d442013-08-22 11:37:32 -0700116#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800117}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800118
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700119TEST(time, strftime) {
120 setenv("TZ", "UTC", 1);
121
122 struct tm t;
123 memset(&t, 0, sizeof(tm));
124 t.tm_year = 200;
125 t.tm_mon = 2;
126 t.tm_mday = 10;
127
128 char buf[64];
129
130 // Seconds since the epoch.
131#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
132 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
133 EXPECT_STREQ("4108320000", buf);
134#endif
135
136 // Date and time as text.
137 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
138 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
139}
140
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800141TEST(time, strftime_null_tm_zone) {
142 // Netflix on Nexus Player wouldn't start (http://b/25170306).
143 struct tm t;
144 memset(&t, 0, sizeof(tm));
145
146 char buf[64];
147
148 setenv("TZ", "America/Los_Angeles", 1);
149 tzset();
150
151 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
152 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
153 EXPECT_STREQ("<PST>", buf);
154
155#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
156 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
157 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
158 EXPECT_STREQ("<PDT>", buf);
159
160 t.tm_isdst = -123; // "and negative if the information is not available".
161 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
162 EXPECT_STREQ("<>", buf);
163#endif
164
165 setenv("TZ", "UTC", 1);
166 tzset();
167
168 t.tm_isdst = 0;
169 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
170 EXPECT_STREQ("<UTC>", buf);
171
172#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
173 t.tm_isdst = 1; // UTC has no DST.
174 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
175 EXPECT_STREQ("<>", buf);
176#endif
177}
178
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700179TEST(time, strptime) {
180 setenv("TZ", "UTC", 1);
181
182 struct tm t;
183 char buf[64];
184
185 memset(&t, 0, sizeof(t));
186 strptime("11:14", "%R", &t);
187 strftime(buf, sizeof(buf), "%H:%M", &t);
188 EXPECT_STREQ("11:14", buf);
189
190 memset(&t, 0, sizeof(t));
191 strptime("09:41:53", "%T", &t);
192 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
193 EXPECT_STREQ("09:41:53", buf);
194}
195
Elliott Hughes4b558f52014-03-04 15:58:02 -0800196void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
197 itimerspec ts;
198 ts.it_value.tv_sec = value_s;
199 ts.it_value.tv_nsec = value_ns;
200 ts.it_interval.tv_sec = interval_s;
201 ts.it_interval.tv_nsec = interval_ns;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700202 ASSERT_EQ(0, timer_settime(t, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800203}
204
205static void NoOpNotifyFunction(sigval_t) {
206}
207
208TEST(time, timer_create) {
209 sigevent_t se;
210 memset(&se, 0, sizeof(se));
211 se.sigev_notify = SIGEV_THREAD;
212 se.sigev_notify_function = NoOpNotifyFunction;
213 timer_t timer_id;
214 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
215
Elliott Hughes33697a02016-01-26 13:04:57 -0800216 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800217 ASSERT_NE(-1, pid) << strerror(errno);
218
219 if (pid == 0) {
220 // Timers are not inherited by the child.
221 ASSERT_EQ(-1, timer_delete(timer_id));
222 ASSERT_EQ(EINVAL, errno);
223 _exit(0);
224 }
225
Elliott Hughes33697a02016-01-26 13:04:57 -0800226 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800227
228 ASSERT_EQ(0, timer_delete(timer_id));
229}
230
Yabin Cui95f1ee22015-01-13 19:53:15 -0800231static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800232static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
233 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
234 ASSERT_EQ(SIGUSR1, signal_number);
235}
236
237TEST(time, timer_create_SIGEV_SIGNAL) {
238 sigevent_t se;
239 memset(&se, 0, sizeof(se));
240 se.sigev_notify = SIGEV_SIGNAL;
241 se.sigev_signo = SIGUSR1;
242
243 timer_t timer_id;
244 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
245
Yabin Cui95f1ee22015-01-13 19:53:15 -0800246 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800247 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
248
249 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
250
251 itimerspec ts;
252 ts.it_value.tv_sec = 0;
253 ts.it_value.tv_nsec = 1;
254 ts.it_interval.tv_sec = 0;
255 ts.it_interval.tv_nsec = 0;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700256 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800257
258 usleep(500000);
259 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
260}
261
262struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800263 private:
264 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800265 timer_t timer_id;
266 sigevent_t se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700267 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800268
Elliott Hughes4b558f52014-03-04 15:58:02 -0800269 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700270 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800271 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700272 timer_valid = true;
273 }
274
Yabin Cui95f1ee22015-01-13 19:53:15 -0800275 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700276 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800277 memset(&se, 0, sizeof(se));
278 se.sigev_notify = SIGEV_THREAD;
279 se.sigev_notify_function = fn;
280 se.sigev_value.sival_ptr = this;
281 Create();
282 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700283 void DeleteTimer() {
284 ASSERT_TRUE(timer_valid);
285 ASSERT_EQ(0, timer_delete(timer_id));
286 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800287 }
288
289 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700290 if (timer_valid) {
291 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800292 }
293 }
294
Yabin Cui95f1ee22015-01-13 19:53:15 -0800295 int Value() const {
296 return value;
297 }
298
Christopher Ferris62d84b12014-10-20 19:09:19 -0700299 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
300 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
301 }
302
303 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800304 int current_value = value;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700305 time_t start = time(NULL);
306 while (current_value == value && (time(NULL) - start) < 5) {
307 }
308 return current_value != value;
309 }
310
Elliott Hughes4b558f52014-03-04 15:58:02 -0800311 static void CountNotifyFunction(sigval_t value) {
312 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
313 ++cd->value;
314 }
315
316 static void CountAndDisarmNotifyFunction(sigval_t value) {
317 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
318 ++cd->value;
319
320 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700321 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800322 }
323};
324
325TEST(time, timer_settime_0) {
326 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800327 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800328
Yabin Cuibf572d92015-08-11 11:23:16 -0700329 counter.SetTime(0, 500000000, 1, 0);
330 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800331
332 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800333 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800334}
335
336TEST(time, timer_settime_repeats) {
337 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800338 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800339
Christopher Ferris62d84b12014-10-20 19:09:19 -0700340 counter.SetTime(0, 1, 0, 10);
341 ASSERT_TRUE(counter.ValueUpdated());
342 ASSERT_TRUE(counter.ValueUpdated());
343 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800344 counter.DeleteTimer();
345 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
346 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800347}
348
Yabin Cui95f1ee22015-01-13 19:53:15 -0800349static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800350static void timer_create_NULL_signal_handler(int signal_number) {
351 ++timer_create_NULL_signal_handler_invocation_count;
352 ASSERT_EQ(SIGALRM, signal_number);
353}
354
355TEST(time, timer_create_NULL) {
356 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
357 timer_t timer_id;
358 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
359
Yabin Cui95f1ee22015-01-13 19:53:15 -0800360 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800361 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
362
363 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
364
365 SetTime(timer_id, 0, 1, 0, 0);
366 usleep(500000);
367
368 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
369}
370
371TEST(time, timer_create_EINVAL) {
372 clockid_t invalid_clock = 16;
373
374 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
375 timer_t timer_id;
376 ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id));
377 ASSERT_EQ(EINVAL, errno);
378
379 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
380 sigevent_t se;
381 memset(&se, 0, sizeof(se));
382 se.sigev_notify = SIGEV_THREAD;
383 se.sigev_notify_function = NoOpNotifyFunction;
384 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
385 ASSERT_EQ(EINVAL, errno);
386}
387
388TEST(time, timer_delete_multiple) {
389 timer_t timer_id;
390 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
391 ASSERT_EQ(0, timer_delete(timer_id));
392 ASSERT_EQ(-1, timer_delete(timer_id));
393 ASSERT_EQ(EINVAL, errno);
394
395 sigevent_t se;
396 memset(&se, 0, sizeof(se));
397 se.sigev_notify = SIGEV_THREAD;
398 se.sigev_notify_function = NoOpNotifyFunction;
399 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
400 ASSERT_EQ(0, timer_delete(timer_id));
401 ASSERT_EQ(-1, timer_delete(timer_id));
402 ASSERT_EQ(EINVAL, errno);
403}
404
405TEST(time, timer_create_multiple) {
406 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800407 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800408 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800409
Yabin Cui95f1ee22015-01-13 19:53:15 -0800410 ASSERT_EQ(0, counter1.Value());
411 ASSERT_EQ(0, counter2.Value());
412 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800413
Yabin Cui410c1ad2015-06-18 16:19:02 -0700414 counter2.SetTime(0, 500000000, 0, 0);
415 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800416
Yabin Cui95f1ee22015-01-13 19:53:15 -0800417 EXPECT_EQ(0, counter1.Value());
418 EXPECT_EQ(1, counter2.Value());
419 EXPECT_EQ(0, counter3.Value());
420}
421
422// Test to verify that disarming a repeatable timer disables the callbacks.
423TEST(time, timer_disarm_terminates) {
424 Counter counter(Counter::CountNotifyFunction);
425 ASSERT_EQ(0, counter.Value());
426
427 counter.SetTime(0, 1, 0, 1);
428 ASSERT_TRUE(counter.ValueUpdated());
429 ASSERT_TRUE(counter.ValueUpdated());
430 ASSERT_TRUE(counter.ValueUpdated());
431
432 counter.SetTime(0, 0, 0, 0);
433 // Add a sleep as the kernel may have pending events when the timer is disarmed.
434 usleep(500000);
435 int value = counter.Value();
436 usleep(500000);
437
438 // Verify the counter has not been incremented.
439 ASSERT_EQ(value, counter.Value());
440}
441
442// Test to verify that deleting a repeatable timer disables the callbacks.
443TEST(time, timer_delete_terminates) {
444 Counter counter(Counter::CountNotifyFunction);
445 ASSERT_EQ(0, counter.Value());
446
447 counter.SetTime(0, 1, 0, 1);
448 ASSERT_TRUE(counter.ValueUpdated());
449 ASSERT_TRUE(counter.ValueUpdated());
450 ASSERT_TRUE(counter.ValueUpdated());
451
452 counter.DeleteTimer();
453 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
454 usleep(500000);
455 int value = counter.Value();
456 usleep(500000);
457
458 // Verify the counter has not been incremented.
459 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800460}
Christopher Ferris753ad772014-03-20 20:47:45 -0700461
462struct TimerDeleteData {
463 timer_t timer_id;
464 pthread_t thread_id;
465 volatile bool complete;
466};
467
468static void TimerDeleteCallback(sigval_t value) {
469 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
470
471 tdd->thread_id = pthread_self();
472 timer_delete(tdd->timer_id);
473 tdd->complete = true;
474}
475
476TEST(time, timer_delete_from_timer_thread) {
477 TimerDeleteData tdd;
478 sigevent_t se;
479
480 memset(&se, 0, sizeof(se));
481 se.sigev_notify = SIGEV_THREAD;
482 se.sigev_notify_function = TimerDeleteCallback;
483 se.sigev_value.sival_ptr = &tdd;
484
485 tdd.complete = false;
486 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
487
488 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800489 ts.it_value.tv_sec = 1;
490 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700491 ts.it_interval.tv_sec = 0;
492 ts.it_interval.tv_nsec = 0;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800493 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, NULL));
Christopher Ferris753ad772014-03-20 20:47:45 -0700494
495 time_t cur_time = time(NULL);
496 while (!tdd.complete && (time(NULL) - cur_time) < 5);
497 ASSERT_TRUE(tdd.complete);
498
499#if defined(__BIONIC__)
500 // Since bionic timers are implemented by creating a thread to handle the
501 // callback, verify that the thread actually completes.
502 cur_time = time(NULL);
503 while (pthread_detach(tdd.thread_id) != ESRCH && (time(NULL) - cur_time) < 5);
504 ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id));
505#endif
506}
Elliott Hughes625993d2014-07-15 16:53:13 -0700507
508TEST(time, clock_gettime) {
509 // Try to ensure that our vdso clock_gettime is working.
510 timespec ts1;
511 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
512 timespec ts2;
513 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
514
515 // What's the difference between the two?
516 ts2.tv_sec -= ts1.tv_sec;
517 ts2.tv_nsec -= ts1.tv_nsec;
518 if (ts2.tv_nsec < 0) {
519 --ts2.tv_sec;
Elliott Hughes04303f52014-09-18 16:11:59 -0700520 ts2.tv_nsec += NS_PER_S;
Elliott Hughes625993d2014-07-15 16:53:13 -0700521 }
522
523 // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
524 ASSERT_EQ(0, ts2.tv_sec);
525 ASSERT_LT(ts2.tv_nsec, 1000000);
526}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700527
528TEST(time, clock) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900529 // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
530 clock_t t0 = clock();
531 sleep(1);
532 clock_t t1 = clock();
533 ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
534}
535
Yabin Cuid5c65272014-11-26 14:04:26 -0800536pid_t GetInvalidPid() {
537 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
538 long pid_max;
539 fscanf(fp, "%ld", &pid_max);
540 pid_t invalid_pid = static_cast<pid_t>(pid_max + 1);
541 fclose(fp);
542 return invalid_pid;
543}
544
545TEST(time, clock_getcpuclockid) {
546 // For current process.
547 clockid_t clockid;
548 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
549
550 timespec ts;
551 ASSERT_EQ(0, clock_gettime(clockid, &ts));
552
553 // For parent process.
554 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
555 ASSERT_EQ(0, clock_gettime(clockid, &ts));
556
557 // For invalid process.
558 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
559 errno = 0;
560 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid));
561 ASSERT_EQ(0, errno);
562}
563
Haruki Hasegawa18160252014-10-13 00:50:47 +0900564TEST(time, clock_settime) {
565 errno = 0;
566 timespec ts;
567 ASSERT_EQ(-1, clock_settime(-1, &ts));
568 ASSERT_EQ(EINVAL, errno);
569}
570
571TEST(time, clock_nanosleep) {
572 timespec in;
573 timespec out;
574 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700575}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700576
577TEST(time, clock_nanosleep_thread_cputime_id) {
578 timespec in;
579 in.tv_sec = 1;
580 in.tv_nsec = 0;
581 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
582}