blob: 48c299ac57033bf3084b0dbefd8e980cd525c0d0 [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 Hughesf8ebaa42016-08-12 16:28:36 -0700105 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700106#else
107 // Everyone else should be using a signed 64-bit time_t.
108 ASSERT_GE(sizeof(time_t) * 8, 64U);
109
110 setenv("TZ", "America/Los_Angeles", 1);
111 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700112 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700113 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700114 ASSERT_EQ(0, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700115
116 setenv("TZ", "UTC", 1);
117 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700118 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700119 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700120 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700121#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800122}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800123
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700124TEST(time, mktime_EOVERFLOW) {
125 struct tm t;
126 memset(&t, 0, sizeof(tm));
127 t.tm_year = 0;
128 t.tm_mon = 2;
129 t.tm_mday = 10;
130
131 errno = 0;
132 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
133 ASSERT_EQ(0, errno);
134
135 t.tm_year = INT_MAX;
136 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
137 ASSERT_EQ(EOVERFLOW, errno);
138}
139
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700140TEST(time, strftime) {
141 setenv("TZ", "UTC", 1);
142
143 struct tm t;
144 memset(&t, 0, sizeof(tm));
145 t.tm_year = 200;
146 t.tm_mon = 2;
147 t.tm_mday = 10;
148
149 char buf[64];
150
151 // Seconds since the epoch.
152#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
153 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
154 EXPECT_STREQ("4108320000", buf);
155#endif
156
157 // Date and time as text.
158 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
159 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
160}
161
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800162TEST(time, strftime_null_tm_zone) {
163 // Netflix on Nexus Player wouldn't start (http://b/25170306).
164 struct tm t;
165 memset(&t, 0, sizeof(tm));
166
167 char buf[64];
168
169 setenv("TZ", "America/Los_Angeles", 1);
170 tzset();
171
172 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
173 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
174 EXPECT_STREQ("<PST>", buf);
175
176#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
177 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
178 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
179 EXPECT_STREQ("<PDT>", buf);
180
181 t.tm_isdst = -123; // "and negative if the information is not available".
182 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
183 EXPECT_STREQ("<>", buf);
184#endif
185
186 setenv("TZ", "UTC", 1);
187 tzset();
188
189 t.tm_isdst = 0;
190 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
191 EXPECT_STREQ("<UTC>", buf);
192
193#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
194 t.tm_isdst = 1; // UTC has no DST.
195 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
196 EXPECT_STREQ("<>", buf);
197#endif
198}
199
Elliott Hughes0a610d02016-07-29 14:04:17 -0700200TEST(time, strftime_l) {
201 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
202 locale_t old_locale = uselocale(cloc);
203
204 setenv("TZ", "UTC", 1);
205
206 struct tm t;
207 memset(&t, 0, sizeof(tm));
208 t.tm_year = 200;
209 t.tm_mon = 2;
210 t.tm_mday = 10;
211
212 // Date and time as text.
213 char buf[64];
214 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
215 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
216
217 uselocale(old_locale);
218 freelocale(cloc);
219}
220
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700221TEST(time, strptime) {
222 setenv("TZ", "UTC", 1);
223
224 struct tm t;
225 char buf[64];
226
227 memset(&t, 0, sizeof(t));
228 strptime("11:14", "%R", &t);
229 strftime(buf, sizeof(buf), "%H:%M", &t);
230 EXPECT_STREQ("11:14", buf);
231
232 memset(&t, 0, sizeof(t));
233 strptime("09:41:53", "%T", &t);
234 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
235 EXPECT_STREQ("09:41:53", buf);
236}
237
Elliott Hughes4b558f52014-03-04 15:58:02 -0800238void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
239 itimerspec ts;
240 ts.it_value.tv_sec = value_s;
241 ts.it_value.tv_nsec = value_ns;
242 ts.it_interval.tv_sec = interval_s;
243 ts.it_interval.tv_nsec = interval_ns;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700244 ASSERT_EQ(0, timer_settime(t, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800245}
246
247static void NoOpNotifyFunction(sigval_t) {
248}
249
250TEST(time, timer_create) {
251 sigevent_t se;
252 memset(&se, 0, sizeof(se));
253 se.sigev_notify = SIGEV_THREAD;
254 se.sigev_notify_function = NoOpNotifyFunction;
255 timer_t timer_id;
256 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
257
Elliott Hughes33697a02016-01-26 13:04:57 -0800258 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800259 ASSERT_NE(-1, pid) << strerror(errno);
260
261 if (pid == 0) {
262 // Timers are not inherited by the child.
263 ASSERT_EQ(-1, timer_delete(timer_id));
264 ASSERT_EQ(EINVAL, errno);
265 _exit(0);
266 }
267
Elliott Hughes33697a02016-01-26 13:04:57 -0800268 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800269
270 ASSERT_EQ(0, timer_delete(timer_id));
271}
272
Yabin Cui95f1ee22015-01-13 19:53:15 -0800273static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800274static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
275 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
276 ASSERT_EQ(SIGUSR1, signal_number);
277}
278
279TEST(time, timer_create_SIGEV_SIGNAL) {
280 sigevent_t se;
281 memset(&se, 0, sizeof(se));
282 se.sigev_notify = SIGEV_SIGNAL;
283 se.sigev_signo = SIGUSR1;
284
285 timer_t timer_id;
286 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
287
Yabin Cui95f1ee22015-01-13 19:53:15 -0800288 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800289 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
290
291 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
292
293 itimerspec ts;
294 ts.it_value.tv_sec = 0;
295 ts.it_value.tv_nsec = 1;
296 ts.it_interval.tv_sec = 0;
297 ts.it_interval.tv_nsec = 0;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700298 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800299
300 usleep(500000);
301 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
302}
303
304struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800305 private:
306 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800307 timer_t timer_id;
308 sigevent_t se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700309 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800310
Elliott Hughes4b558f52014-03-04 15:58:02 -0800311 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700312 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800313 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700314 timer_valid = true;
315 }
316
Yabin Cui95f1ee22015-01-13 19:53:15 -0800317 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700318 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800319 memset(&se, 0, sizeof(se));
320 se.sigev_notify = SIGEV_THREAD;
321 se.sigev_notify_function = fn;
322 se.sigev_value.sival_ptr = this;
323 Create();
324 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700325 void DeleteTimer() {
326 ASSERT_TRUE(timer_valid);
327 ASSERT_EQ(0, timer_delete(timer_id));
328 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800329 }
330
331 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700332 if (timer_valid) {
333 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800334 }
335 }
336
Yabin Cui95f1ee22015-01-13 19:53:15 -0800337 int Value() const {
338 return value;
339 }
340
Christopher Ferris62d84b12014-10-20 19:09:19 -0700341 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
342 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
343 }
344
345 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800346 int current_value = value;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700347 time_t start = time(NULL);
348 while (current_value == value && (time(NULL) - start) < 5) {
349 }
350 return current_value != value;
351 }
352
Elliott Hughes4b558f52014-03-04 15:58:02 -0800353 static void CountNotifyFunction(sigval_t value) {
354 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
355 ++cd->value;
356 }
357
358 static void CountAndDisarmNotifyFunction(sigval_t value) {
359 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
360 ++cd->value;
361
362 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700363 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800364 }
365};
366
367TEST(time, timer_settime_0) {
368 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800369 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800370
Yabin Cuibf572d92015-08-11 11:23:16 -0700371 counter.SetTime(0, 500000000, 1, 0);
372 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800373
374 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800375 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800376}
377
378TEST(time, timer_settime_repeats) {
379 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800380 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800381
Christopher Ferris62d84b12014-10-20 19:09:19 -0700382 counter.SetTime(0, 1, 0, 10);
383 ASSERT_TRUE(counter.ValueUpdated());
384 ASSERT_TRUE(counter.ValueUpdated());
385 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800386 counter.DeleteTimer();
387 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
388 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800389}
390
Yabin Cui95f1ee22015-01-13 19:53:15 -0800391static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800392static void timer_create_NULL_signal_handler(int signal_number) {
393 ++timer_create_NULL_signal_handler_invocation_count;
394 ASSERT_EQ(SIGALRM, signal_number);
395}
396
397TEST(time, timer_create_NULL) {
398 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
399 timer_t timer_id;
400 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
401
Yabin Cui95f1ee22015-01-13 19:53:15 -0800402 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800403 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
404
405 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
406
407 SetTime(timer_id, 0, 1, 0, 0);
408 usleep(500000);
409
410 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
411}
412
413TEST(time, timer_create_EINVAL) {
414 clockid_t invalid_clock = 16;
415
416 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
417 timer_t timer_id;
418 ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id));
419 ASSERT_EQ(EINVAL, errno);
420
421 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
422 sigevent_t se;
423 memset(&se, 0, sizeof(se));
424 se.sigev_notify = SIGEV_THREAD;
425 se.sigev_notify_function = NoOpNotifyFunction;
426 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
427 ASSERT_EQ(EINVAL, errno);
428}
429
430TEST(time, timer_delete_multiple) {
431 timer_t timer_id;
432 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
433 ASSERT_EQ(0, timer_delete(timer_id));
434 ASSERT_EQ(-1, timer_delete(timer_id));
435 ASSERT_EQ(EINVAL, errno);
436
437 sigevent_t se;
438 memset(&se, 0, sizeof(se));
439 se.sigev_notify = SIGEV_THREAD;
440 se.sigev_notify_function = NoOpNotifyFunction;
441 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
442 ASSERT_EQ(0, timer_delete(timer_id));
443 ASSERT_EQ(-1, timer_delete(timer_id));
444 ASSERT_EQ(EINVAL, errno);
445}
446
447TEST(time, timer_create_multiple) {
448 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800449 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800450 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800451
Yabin Cui95f1ee22015-01-13 19:53:15 -0800452 ASSERT_EQ(0, counter1.Value());
453 ASSERT_EQ(0, counter2.Value());
454 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800455
Yabin Cui410c1ad2015-06-18 16:19:02 -0700456 counter2.SetTime(0, 500000000, 0, 0);
457 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800458
Yabin Cui95f1ee22015-01-13 19:53:15 -0800459 EXPECT_EQ(0, counter1.Value());
460 EXPECT_EQ(1, counter2.Value());
461 EXPECT_EQ(0, counter3.Value());
462}
463
464// Test to verify that disarming a repeatable timer disables the callbacks.
465TEST(time, timer_disarm_terminates) {
466 Counter counter(Counter::CountNotifyFunction);
467 ASSERT_EQ(0, counter.Value());
468
469 counter.SetTime(0, 1, 0, 1);
470 ASSERT_TRUE(counter.ValueUpdated());
471 ASSERT_TRUE(counter.ValueUpdated());
472 ASSERT_TRUE(counter.ValueUpdated());
473
474 counter.SetTime(0, 0, 0, 0);
475 // Add a sleep as the kernel may have pending events when the timer is disarmed.
476 usleep(500000);
477 int value = counter.Value();
478 usleep(500000);
479
480 // Verify the counter has not been incremented.
481 ASSERT_EQ(value, counter.Value());
482}
483
484// Test to verify that deleting a repeatable timer disables the callbacks.
485TEST(time, timer_delete_terminates) {
486 Counter counter(Counter::CountNotifyFunction);
487 ASSERT_EQ(0, counter.Value());
488
489 counter.SetTime(0, 1, 0, 1);
490 ASSERT_TRUE(counter.ValueUpdated());
491 ASSERT_TRUE(counter.ValueUpdated());
492 ASSERT_TRUE(counter.ValueUpdated());
493
494 counter.DeleteTimer();
495 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
496 usleep(500000);
497 int value = counter.Value();
498 usleep(500000);
499
500 // Verify the counter has not been incremented.
501 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800502}
Christopher Ferris753ad772014-03-20 20:47:45 -0700503
504struct TimerDeleteData {
505 timer_t timer_id;
506 pthread_t thread_id;
507 volatile bool complete;
508};
509
510static void TimerDeleteCallback(sigval_t value) {
511 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
512
513 tdd->thread_id = pthread_self();
514 timer_delete(tdd->timer_id);
515 tdd->complete = true;
516}
517
518TEST(time, timer_delete_from_timer_thread) {
519 TimerDeleteData tdd;
520 sigevent_t se;
521
522 memset(&se, 0, sizeof(se));
523 se.sigev_notify = SIGEV_THREAD;
524 se.sigev_notify_function = TimerDeleteCallback;
525 se.sigev_value.sival_ptr = &tdd;
526
527 tdd.complete = false;
528 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
529
530 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800531 ts.it_value.tv_sec = 1;
532 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700533 ts.it_interval.tv_sec = 0;
534 ts.it_interval.tv_nsec = 0;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800535 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, NULL));
Christopher Ferris753ad772014-03-20 20:47:45 -0700536
537 time_t cur_time = time(NULL);
538 while (!tdd.complete && (time(NULL) - cur_time) < 5);
539 ASSERT_TRUE(tdd.complete);
540
541#if defined(__BIONIC__)
542 // Since bionic timers are implemented by creating a thread to handle the
543 // callback, verify that the thread actually completes.
544 cur_time = time(NULL);
545 while (pthread_detach(tdd.thread_id) != ESRCH && (time(NULL) - cur_time) < 5);
546 ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id));
547#endif
548}
Elliott Hughes625993d2014-07-15 16:53:13 -0700549
550TEST(time, clock_gettime) {
551 // Try to ensure that our vdso clock_gettime is working.
552 timespec ts1;
553 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
554 timespec ts2;
555 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
556
557 // What's the difference between the two?
558 ts2.tv_sec -= ts1.tv_sec;
559 ts2.tv_nsec -= ts1.tv_nsec;
560 if (ts2.tv_nsec < 0) {
561 --ts2.tv_sec;
Elliott Hughes04303f52014-09-18 16:11:59 -0700562 ts2.tv_nsec += NS_PER_S;
Elliott Hughes625993d2014-07-15 16:53:13 -0700563 }
564
565 // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
566 ASSERT_EQ(0, ts2.tv_sec);
567 ASSERT_LT(ts2.tv_nsec, 1000000);
568}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700569
570TEST(time, clock) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900571 // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
572 clock_t t0 = clock();
573 sleep(1);
574 clock_t t1 = clock();
575 ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
576}
577
Yabin Cuid5c65272014-11-26 14:04:26 -0800578pid_t GetInvalidPid() {
579 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
580 long pid_max;
581 fscanf(fp, "%ld", &pid_max);
582 pid_t invalid_pid = static_cast<pid_t>(pid_max + 1);
583 fclose(fp);
584 return invalid_pid;
585}
586
587TEST(time, clock_getcpuclockid) {
588 // For current process.
589 clockid_t clockid;
590 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
591
592 timespec ts;
593 ASSERT_EQ(0, clock_gettime(clockid, &ts));
594
595 // For parent process.
596 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
597 ASSERT_EQ(0, clock_gettime(clockid, &ts));
598
599 // For invalid process.
600 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
601 errno = 0;
602 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid));
603 ASSERT_EQ(0, errno);
604}
605
Haruki Hasegawa18160252014-10-13 00:50:47 +0900606TEST(time, clock_settime) {
607 errno = 0;
608 timespec ts;
609 ASSERT_EQ(-1, clock_settime(-1, &ts));
610 ASSERT_EQ(EINVAL, errno);
611}
612
613TEST(time, clock_nanosleep) {
614 timespec in;
615 timespec out;
616 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700617}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700618
619TEST(time, clock_nanosleep_thread_cputime_id) {
620 timespec in;
621 in.tv_sec = 1;
622 in.tv_nsec = 0;
623 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
624}