blob: 028a359db082ba55d9309daf77abd955f0cddd9e [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 Hughes0a610d02016-07-29 14:04:17 -0700179TEST(time, strftime_l) {
180 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
181 locale_t old_locale = uselocale(cloc);
182
183 setenv("TZ", "UTC", 1);
184
185 struct tm t;
186 memset(&t, 0, sizeof(tm));
187 t.tm_year = 200;
188 t.tm_mon = 2;
189 t.tm_mday = 10;
190
191 // Date and time as text.
192 char buf[64];
193 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
194 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
195
196 uselocale(old_locale);
197 freelocale(cloc);
198}
199
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700200TEST(time, strptime) {
201 setenv("TZ", "UTC", 1);
202
203 struct tm t;
204 char buf[64];
205
206 memset(&t, 0, sizeof(t));
207 strptime("11:14", "%R", &t);
208 strftime(buf, sizeof(buf), "%H:%M", &t);
209 EXPECT_STREQ("11:14", buf);
210
211 memset(&t, 0, sizeof(t));
212 strptime("09:41:53", "%T", &t);
213 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
214 EXPECT_STREQ("09:41:53", buf);
215}
216
Elliott Hughes4b558f52014-03-04 15:58:02 -0800217void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
218 itimerspec ts;
219 ts.it_value.tv_sec = value_s;
220 ts.it_value.tv_nsec = value_ns;
221 ts.it_interval.tv_sec = interval_s;
222 ts.it_interval.tv_nsec = interval_ns;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700223 ASSERT_EQ(0, timer_settime(t, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800224}
225
226static void NoOpNotifyFunction(sigval_t) {
227}
228
229TEST(time, timer_create) {
230 sigevent_t se;
231 memset(&se, 0, sizeof(se));
232 se.sigev_notify = SIGEV_THREAD;
233 se.sigev_notify_function = NoOpNotifyFunction;
234 timer_t timer_id;
235 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
236
Elliott Hughes33697a02016-01-26 13:04:57 -0800237 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800238 ASSERT_NE(-1, pid) << strerror(errno);
239
240 if (pid == 0) {
241 // Timers are not inherited by the child.
242 ASSERT_EQ(-1, timer_delete(timer_id));
243 ASSERT_EQ(EINVAL, errno);
244 _exit(0);
245 }
246
Elliott Hughes33697a02016-01-26 13:04:57 -0800247 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800248
249 ASSERT_EQ(0, timer_delete(timer_id));
250}
251
Yabin Cui95f1ee22015-01-13 19:53:15 -0800252static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800253static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
254 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
255 ASSERT_EQ(SIGUSR1, signal_number);
256}
257
258TEST(time, timer_create_SIGEV_SIGNAL) {
259 sigevent_t se;
260 memset(&se, 0, sizeof(se));
261 se.sigev_notify = SIGEV_SIGNAL;
262 se.sigev_signo = SIGUSR1;
263
264 timer_t timer_id;
265 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
266
Yabin Cui95f1ee22015-01-13 19:53:15 -0800267 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800268 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
269
270 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
271
272 itimerspec ts;
273 ts.it_value.tv_sec = 0;
274 ts.it_value.tv_nsec = 1;
275 ts.it_interval.tv_sec = 0;
276 ts.it_interval.tv_nsec = 0;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700277 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800278
279 usleep(500000);
280 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
281}
282
283struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800284 private:
285 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800286 timer_t timer_id;
287 sigevent_t se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700288 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800289
Elliott Hughes4b558f52014-03-04 15:58:02 -0800290 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700291 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800292 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700293 timer_valid = true;
294 }
295
Yabin Cui95f1ee22015-01-13 19:53:15 -0800296 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700297 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800298 memset(&se, 0, sizeof(se));
299 se.sigev_notify = SIGEV_THREAD;
300 se.sigev_notify_function = fn;
301 se.sigev_value.sival_ptr = this;
302 Create();
303 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700304 void DeleteTimer() {
305 ASSERT_TRUE(timer_valid);
306 ASSERT_EQ(0, timer_delete(timer_id));
307 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800308 }
309
310 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700311 if (timer_valid) {
312 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800313 }
314 }
315
Yabin Cui95f1ee22015-01-13 19:53:15 -0800316 int Value() const {
317 return value;
318 }
319
Christopher Ferris62d84b12014-10-20 19:09:19 -0700320 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
321 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
322 }
323
324 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800325 int current_value = value;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700326 time_t start = time(NULL);
327 while (current_value == value && (time(NULL) - start) < 5) {
328 }
329 return current_value != value;
330 }
331
Elliott Hughes4b558f52014-03-04 15:58:02 -0800332 static void CountNotifyFunction(sigval_t value) {
333 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
334 ++cd->value;
335 }
336
337 static void CountAndDisarmNotifyFunction(sigval_t value) {
338 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
339 ++cd->value;
340
341 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700342 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800343 }
344};
345
346TEST(time, timer_settime_0) {
347 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800348 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800349
Yabin Cuibf572d92015-08-11 11:23:16 -0700350 counter.SetTime(0, 500000000, 1, 0);
351 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800352
353 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800354 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800355}
356
357TEST(time, timer_settime_repeats) {
358 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800359 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800360
Christopher Ferris62d84b12014-10-20 19:09:19 -0700361 counter.SetTime(0, 1, 0, 10);
362 ASSERT_TRUE(counter.ValueUpdated());
363 ASSERT_TRUE(counter.ValueUpdated());
364 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800365 counter.DeleteTimer();
366 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
367 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800368}
369
Yabin Cui95f1ee22015-01-13 19:53:15 -0800370static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800371static void timer_create_NULL_signal_handler(int signal_number) {
372 ++timer_create_NULL_signal_handler_invocation_count;
373 ASSERT_EQ(SIGALRM, signal_number);
374}
375
376TEST(time, timer_create_NULL) {
377 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
378 timer_t timer_id;
379 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
380
Yabin Cui95f1ee22015-01-13 19:53:15 -0800381 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800382 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
383
384 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
385
386 SetTime(timer_id, 0, 1, 0, 0);
387 usleep(500000);
388
389 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
390}
391
392TEST(time, timer_create_EINVAL) {
393 clockid_t invalid_clock = 16;
394
395 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
396 timer_t timer_id;
397 ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id));
398 ASSERT_EQ(EINVAL, errno);
399
400 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
401 sigevent_t se;
402 memset(&se, 0, sizeof(se));
403 se.sigev_notify = SIGEV_THREAD;
404 se.sigev_notify_function = NoOpNotifyFunction;
405 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
406 ASSERT_EQ(EINVAL, errno);
407}
408
409TEST(time, timer_delete_multiple) {
410 timer_t timer_id;
411 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
412 ASSERT_EQ(0, timer_delete(timer_id));
413 ASSERT_EQ(-1, timer_delete(timer_id));
414 ASSERT_EQ(EINVAL, errno);
415
416 sigevent_t se;
417 memset(&se, 0, sizeof(se));
418 se.sigev_notify = SIGEV_THREAD;
419 se.sigev_notify_function = NoOpNotifyFunction;
420 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
421 ASSERT_EQ(0, timer_delete(timer_id));
422 ASSERT_EQ(-1, timer_delete(timer_id));
423 ASSERT_EQ(EINVAL, errno);
424}
425
426TEST(time, timer_create_multiple) {
427 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800428 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800429 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800430
Yabin Cui95f1ee22015-01-13 19:53:15 -0800431 ASSERT_EQ(0, counter1.Value());
432 ASSERT_EQ(0, counter2.Value());
433 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800434
Yabin Cui410c1ad2015-06-18 16:19:02 -0700435 counter2.SetTime(0, 500000000, 0, 0);
436 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800437
Yabin Cui95f1ee22015-01-13 19:53:15 -0800438 EXPECT_EQ(0, counter1.Value());
439 EXPECT_EQ(1, counter2.Value());
440 EXPECT_EQ(0, counter3.Value());
441}
442
443// Test to verify that disarming a repeatable timer disables the callbacks.
444TEST(time, timer_disarm_terminates) {
445 Counter counter(Counter::CountNotifyFunction);
446 ASSERT_EQ(0, counter.Value());
447
448 counter.SetTime(0, 1, 0, 1);
449 ASSERT_TRUE(counter.ValueUpdated());
450 ASSERT_TRUE(counter.ValueUpdated());
451 ASSERT_TRUE(counter.ValueUpdated());
452
453 counter.SetTime(0, 0, 0, 0);
454 // Add a sleep as the kernel may have pending events when the timer is disarmed.
455 usleep(500000);
456 int value = counter.Value();
457 usleep(500000);
458
459 // Verify the counter has not been incremented.
460 ASSERT_EQ(value, counter.Value());
461}
462
463// Test to verify that deleting a repeatable timer disables the callbacks.
464TEST(time, timer_delete_terminates) {
465 Counter counter(Counter::CountNotifyFunction);
466 ASSERT_EQ(0, counter.Value());
467
468 counter.SetTime(0, 1, 0, 1);
469 ASSERT_TRUE(counter.ValueUpdated());
470 ASSERT_TRUE(counter.ValueUpdated());
471 ASSERT_TRUE(counter.ValueUpdated());
472
473 counter.DeleteTimer();
474 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
475 usleep(500000);
476 int value = counter.Value();
477 usleep(500000);
478
479 // Verify the counter has not been incremented.
480 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800481}
Christopher Ferris753ad772014-03-20 20:47:45 -0700482
483struct TimerDeleteData {
484 timer_t timer_id;
485 pthread_t thread_id;
486 volatile bool complete;
487};
488
489static void TimerDeleteCallback(sigval_t value) {
490 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
491
492 tdd->thread_id = pthread_self();
493 timer_delete(tdd->timer_id);
494 tdd->complete = true;
495}
496
497TEST(time, timer_delete_from_timer_thread) {
498 TimerDeleteData tdd;
499 sigevent_t se;
500
501 memset(&se, 0, sizeof(se));
502 se.sigev_notify = SIGEV_THREAD;
503 se.sigev_notify_function = TimerDeleteCallback;
504 se.sigev_value.sival_ptr = &tdd;
505
506 tdd.complete = false;
507 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
508
509 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800510 ts.it_value.tv_sec = 1;
511 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700512 ts.it_interval.tv_sec = 0;
513 ts.it_interval.tv_nsec = 0;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800514 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, NULL));
Christopher Ferris753ad772014-03-20 20:47:45 -0700515
516 time_t cur_time = time(NULL);
517 while (!tdd.complete && (time(NULL) - cur_time) < 5);
518 ASSERT_TRUE(tdd.complete);
519
520#if defined(__BIONIC__)
521 // Since bionic timers are implemented by creating a thread to handle the
522 // callback, verify that the thread actually completes.
523 cur_time = time(NULL);
524 while (pthread_detach(tdd.thread_id) != ESRCH && (time(NULL) - cur_time) < 5);
525 ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id));
526#endif
527}
Elliott Hughes625993d2014-07-15 16:53:13 -0700528
529TEST(time, clock_gettime) {
530 // Try to ensure that our vdso clock_gettime is working.
531 timespec ts1;
532 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
533 timespec ts2;
534 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
535
536 // What's the difference between the two?
537 ts2.tv_sec -= ts1.tv_sec;
538 ts2.tv_nsec -= ts1.tv_nsec;
539 if (ts2.tv_nsec < 0) {
540 --ts2.tv_sec;
Elliott Hughes04303f52014-09-18 16:11:59 -0700541 ts2.tv_nsec += NS_PER_S;
Elliott Hughes625993d2014-07-15 16:53:13 -0700542 }
543
544 // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
545 ASSERT_EQ(0, ts2.tv_sec);
546 ASSERT_LT(ts2.tv_nsec, 1000000);
547}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700548
549TEST(time, clock) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900550 // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
551 clock_t t0 = clock();
552 sleep(1);
553 clock_t t1 = clock();
554 ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
555}
556
Yabin Cuid5c65272014-11-26 14:04:26 -0800557pid_t GetInvalidPid() {
558 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
559 long pid_max;
560 fscanf(fp, "%ld", &pid_max);
561 pid_t invalid_pid = static_cast<pid_t>(pid_max + 1);
562 fclose(fp);
563 return invalid_pid;
564}
565
566TEST(time, clock_getcpuclockid) {
567 // For current process.
568 clockid_t clockid;
569 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
570
571 timespec ts;
572 ASSERT_EQ(0, clock_gettime(clockid, &ts));
573
574 // For parent process.
575 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
576 ASSERT_EQ(0, clock_gettime(clockid, &ts));
577
578 // For invalid process.
579 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
580 errno = 0;
581 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid));
582 ASSERT_EQ(0, errno);
583}
584
Haruki Hasegawa18160252014-10-13 00:50:47 +0900585TEST(time, clock_settime) {
586 errno = 0;
587 timespec ts;
588 ASSERT_EQ(-1, clock_settime(-1, &ts));
589 ASSERT_EQ(EINVAL, errno);
590}
591
592TEST(time, clock_nanosleep) {
593 timespec in;
594 timespec out;
595 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700596}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700597
598TEST(time, clock_nanosleep_thread_cputime_id) {
599 timespec in;
600 in.tv_sec = 1;
601 in.tv_nsec = 0;
602 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
603}