blob: 7a96760c22ecd7316739a329e95bc98c0ad89fda [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 Hughes5a29d542017-12-07 16:05:57 -080046TEST(time, gmtime_r) {
47 struct tm tm = {};
48 time_t t = 0;
49 struct tm* broken_down = gmtime_r(&t, &tm);
50 ASSERT_EQ(broken_down, &tm);
51 ASSERT_EQ(0, broken_down->tm_sec);
52 ASSERT_EQ(0, broken_down->tm_min);
53 ASSERT_EQ(0, broken_down->tm_hour);
54 ASSERT_EQ(1, broken_down->tm_mday);
55 ASSERT_EQ(0, broken_down->tm_mon);
56 ASSERT_EQ(1970, broken_down->tm_year + 1900);
57}
58
Elliott Hughes329103d2014-04-25 16:55:04 -070059static void* gmtime_no_stack_overflow_14313703_fn(void*) {
60 const char* original_tz = getenv("TZ");
61 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
62 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
63 tzset();
64 if (original_tz != NULL) {
65 setenv("TZ", original_tz, 1);
66 }
67 tzset();
68 return NULL;
69}
70
71TEST(time, gmtime_no_stack_overflow_14313703) {
72 // Is it safe to call tzload on a thread with a small stack?
73 // http://b/14313703
74 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -080075 pthread_attr_t a;
76 ASSERT_EQ(0, pthread_attr_init(&a));
77 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -070078
79 pthread_t t;
Elliott Hughes43f7c872016-02-05 11:18:41 -080080 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, NULL));
81 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -070082}
83
Satoru Takeuchi154e2022014-05-27 17:04:04 +090084TEST(time, mktime_empty_TZ) {
85 // tzcode used to have a bug where it didn't reinitialize some internal state.
86
87 // Choose a time where DST is set.
88 struct tm t;
89 memset(&t, 0, sizeof(tm));
90 t.tm_year = 1980 - 1900;
91 t.tm_mon = 6;
92 t.tm_mday = 2;
93
94 setenv("TZ", "America/Los_Angeles", 1);
95 tzset();
96 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
97
98 memset(&t, 0, sizeof(tm));
99 t.tm_year = 1980 - 1900;
100 t.tm_mon = 6;
101 t.tm_mday = 2;
102
103 setenv("TZ", "", 1); // Implies UTC.
104 tzset();
105 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
106}
107
Elliott Hughes7843d442013-08-22 11:37:32 -0700108TEST(time, mktime_10310929) {
109 struct tm t;
110 memset(&t, 0, sizeof(tm));
111 t.tm_year = 200;
112 t.tm_mon = 2;
113 t.tm_mday = 10;
114
Elliott Hughes0c401522013-10-18 16:21:54 -0700115#if !defined(__LP64__)
116 // 32-bit bionic stupidly had a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700117 ASSERT_EQ(-1, mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700118 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700119#else
120 // Everyone else should be using a signed 64-bit time_t.
121 ASSERT_GE(sizeof(time_t) * 8, 64U);
122
123 setenv("TZ", "America/Los_Angeles", 1);
124 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700125 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700126 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700127 ASSERT_EQ(0, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700128
129 setenv("TZ", "UTC", 1);
130 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700131 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700132 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700133 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700134#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800135}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800136
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700137TEST(time, mktime_EOVERFLOW) {
138 struct tm t;
139 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700140
141 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
142 t.tm_year = 2016 - 1900;
143
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700144 t.tm_mon = 2;
145 t.tm_mday = 10;
146
147 errno = 0;
148 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
149 ASSERT_EQ(0, errno);
150
Elliott Hughes47126ed2016-09-06 13:25:53 -0700151 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700152 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700153
154 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700155 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
156 ASSERT_EQ(EOVERFLOW, errno);
157}
158
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700159TEST(time, strftime) {
160 setenv("TZ", "UTC", 1);
161
162 struct tm t;
163 memset(&t, 0, sizeof(tm));
164 t.tm_year = 200;
165 t.tm_mon = 2;
166 t.tm_mday = 10;
167
168 char buf[64];
169
170 // Seconds since the epoch.
171#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
172 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
173 EXPECT_STREQ("4108320000", buf);
174#endif
175
176 // Date and time as text.
177 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
178 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
179}
180
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800181TEST(time, strftime_null_tm_zone) {
182 // Netflix on Nexus Player wouldn't start (http://b/25170306).
183 struct tm t;
184 memset(&t, 0, sizeof(tm));
185
186 char buf[64];
187
188 setenv("TZ", "America/Los_Angeles", 1);
189 tzset();
190
191 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
192 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
193 EXPECT_STREQ("<PST>", buf);
194
195#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
196 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
197 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
198 EXPECT_STREQ("<PDT>", buf);
199
200 t.tm_isdst = -123; // "and negative if the information is not available".
201 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
202 EXPECT_STREQ("<>", buf);
203#endif
204
205 setenv("TZ", "UTC", 1);
206 tzset();
207
208 t.tm_isdst = 0;
209 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
210 EXPECT_STREQ("<UTC>", buf);
211
212#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
213 t.tm_isdst = 1; // UTC has no DST.
214 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
215 EXPECT_STREQ("<>", buf);
216#endif
217}
218
Elliott Hughes0a610d02016-07-29 14:04:17 -0700219TEST(time, strftime_l) {
220 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
221 locale_t old_locale = uselocale(cloc);
222
223 setenv("TZ", "UTC", 1);
224
225 struct tm t;
226 memset(&t, 0, sizeof(tm));
227 t.tm_year = 200;
228 t.tm_mon = 2;
229 t.tm_mday = 10;
230
231 // Date and time as text.
232 char buf[64];
233 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
234 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
235
236 uselocale(old_locale);
237 freelocale(cloc);
238}
239
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700240TEST(time, strptime) {
241 setenv("TZ", "UTC", 1);
242
243 struct tm t;
244 char buf[64];
245
246 memset(&t, 0, sizeof(t));
247 strptime("11:14", "%R", &t);
248 strftime(buf, sizeof(buf), "%H:%M", &t);
249 EXPECT_STREQ("11:14", buf);
250
251 memset(&t, 0, sizeof(t));
252 strptime("09:41:53", "%T", &t);
253 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
254 EXPECT_STREQ("09:41:53", buf);
255}
256
Elliott Hughes4b558f52014-03-04 15:58:02 -0800257void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
258 itimerspec ts;
259 ts.it_value.tv_sec = value_s;
260 ts.it_value.tv_nsec = value_ns;
261 ts.it_interval.tv_sec = interval_s;
262 ts.it_interval.tv_nsec = interval_ns;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700263 ASSERT_EQ(0, timer_settime(t, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800264}
265
266static void NoOpNotifyFunction(sigval_t) {
267}
268
269TEST(time, timer_create) {
270 sigevent_t se;
271 memset(&se, 0, sizeof(se));
272 se.sigev_notify = SIGEV_THREAD;
273 se.sigev_notify_function = NoOpNotifyFunction;
274 timer_t timer_id;
275 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
276
Elliott Hughes33697a02016-01-26 13:04:57 -0800277 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800278 ASSERT_NE(-1, pid) << strerror(errno);
279
280 if (pid == 0) {
281 // Timers are not inherited by the child.
282 ASSERT_EQ(-1, timer_delete(timer_id));
283 ASSERT_EQ(EINVAL, errno);
284 _exit(0);
285 }
286
Elliott Hughes33697a02016-01-26 13:04:57 -0800287 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800288
289 ASSERT_EQ(0, timer_delete(timer_id));
290}
291
Yabin Cui95f1ee22015-01-13 19:53:15 -0800292static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800293static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
294 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
295 ASSERT_EQ(SIGUSR1, signal_number);
296}
297
298TEST(time, timer_create_SIGEV_SIGNAL) {
299 sigevent_t se;
300 memset(&se, 0, sizeof(se));
301 se.sigev_notify = SIGEV_SIGNAL;
302 se.sigev_signo = SIGUSR1;
303
304 timer_t timer_id;
305 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
306
Yabin Cui95f1ee22015-01-13 19:53:15 -0800307 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800308 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
309
310 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
311
312 itimerspec ts;
313 ts.it_value.tv_sec = 0;
314 ts.it_value.tv_nsec = 1;
315 ts.it_interval.tv_sec = 0;
316 ts.it_interval.tv_nsec = 0;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700317 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800318
319 usleep(500000);
320 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
321}
322
323struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800324 private:
325 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800326 timer_t timer_id;
327 sigevent_t se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700328 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800329
Elliott Hughes4b558f52014-03-04 15:58:02 -0800330 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700331 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800332 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700333 timer_valid = true;
334 }
335
Yabin Cui95f1ee22015-01-13 19:53:15 -0800336 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700337 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800338 memset(&se, 0, sizeof(se));
339 se.sigev_notify = SIGEV_THREAD;
340 se.sigev_notify_function = fn;
341 se.sigev_value.sival_ptr = this;
342 Create();
343 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700344 void DeleteTimer() {
345 ASSERT_TRUE(timer_valid);
346 ASSERT_EQ(0, timer_delete(timer_id));
347 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800348 }
349
350 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700351 if (timer_valid) {
352 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800353 }
354 }
355
Yabin Cui95f1ee22015-01-13 19:53:15 -0800356 int Value() const {
357 return value;
358 }
359
Christopher Ferris62d84b12014-10-20 19:09:19 -0700360 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
361 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
362 }
363
364 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800365 int current_value = value;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700366 time_t start = time(NULL);
367 while (current_value == value && (time(NULL) - start) < 5) {
368 }
369 return current_value != value;
370 }
371
Elliott Hughes4b558f52014-03-04 15:58:02 -0800372 static void CountNotifyFunction(sigval_t value) {
373 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
374 ++cd->value;
375 }
376
377 static void CountAndDisarmNotifyFunction(sigval_t value) {
378 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
379 ++cd->value;
380
381 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700382 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800383 }
384};
385
386TEST(time, timer_settime_0) {
387 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800388 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800389
Yabin Cuibf572d92015-08-11 11:23:16 -0700390 counter.SetTime(0, 500000000, 1, 0);
391 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800392
393 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800394 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800395}
396
397TEST(time, timer_settime_repeats) {
398 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800399 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800400
Christopher Ferris62d84b12014-10-20 19:09:19 -0700401 counter.SetTime(0, 1, 0, 10);
402 ASSERT_TRUE(counter.ValueUpdated());
403 ASSERT_TRUE(counter.ValueUpdated());
404 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800405 counter.DeleteTimer();
406 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
407 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800408}
409
Yabin Cui95f1ee22015-01-13 19:53:15 -0800410static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800411static void timer_create_NULL_signal_handler(int signal_number) {
412 ++timer_create_NULL_signal_handler_invocation_count;
413 ASSERT_EQ(SIGALRM, signal_number);
414}
415
416TEST(time, timer_create_NULL) {
417 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
418 timer_t timer_id;
419 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
420
Yabin Cui95f1ee22015-01-13 19:53:15 -0800421 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800422 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
423
424 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
425
426 SetTime(timer_id, 0, 1, 0, 0);
427 usleep(500000);
428
429 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
430}
431
432TEST(time, timer_create_EINVAL) {
433 clockid_t invalid_clock = 16;
434
435 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
436 timer_t timer_id;
437 ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id));
438 ASSERT_EQ(EINVAL, errno);
439
440 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
441 sigevent_t se;
442 memset(&se, 0, sizeof(se));
443 se.sigev_notify = SIGEV_THREAD;
444 se.sigev_notify_function = NoOpNotifyFunction;
445 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
446 ASSERT_EQ(EINVAL, errno);
447}
448
449TEST(time, timer_delete_multiple) {
450 timer_t timer_id;
451 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
452 ASSERT_EQ(0, timer_delete(timer_id));
453 ASSERT_EQ(-1, timer_delete(timer_id));
454 ASSERT_EQ(EINVAL, errno);
455
456 sigevent_t se;
457 memset(&se, 0, sizeof(se));
458 se.sigev_notify = SIGEV_THREAD;
459 se.sigev_notify_function = NoOpNotifyFunction;
460 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
461 ASSERT_EQ(0, timer_delete(timer_id));
462 ASSERT_EQ(-1, timer_delete(timer_id));
463 ASSERT_EQ(EINVAL, errno);
464}
465
466TEST(time, timer_create_multiple) {
467 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800468 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800469 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800470
Yabin Cui95f1ee22015-01-13 19:53:15 -0800471 ASSERT_EQ(0, counter1.Value());
472 ASSERT_EQ(0, counter2.Value());
473 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800474
Yabin Cui410c1ad2015-06-18 16:19:02 -0700475 counter2.SetTime(0, 500000000, 0, 0);
476 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800477
Yabin Cui95f1ee22015-01-13 19:53:15 -0800478 EXPECT_EQ(0, counter1.Value());
479 EXPECT_EQ(1, counter2.Value());
480 EXPECT_EQ(0, counter3.Value());
481}
482
483// Test to verify that disarming a repeatable timer disables the callbacks.
484TEST(time, timer_disarm_terminates) {
485 Counter counter(Counter::CountNotifyFunction);
486 ASSERT_EQ(0, counter.Value());
487
488 counter.SetTime(0, 1, 0, 1);
489 ASSERT_TRUE(counter.ValueUpdated());
490 ASSERT_TRUE(counter.ValueUpdated());
491 ASSERT_TRUE(counter.ValueUpdated());
492
493 counter.SetTime(0, 0, 0, 0);
494 // Add a sleep as the kernel may have pending events when the timer is disarmed.
495 usleep(500000);
496 int value = counter.Value();
497 usleep(500000);
498
499 // Verify the counter has not been incremented.
500 ASSERT_EQ(value, counter.Value());
501}
502
503// Test to verify that deleting a repeatable timer disables the callbacks.
504TEST(time, timer_delete_terminates) {
505 Counter counter(Counter::CountNotifyFunction);
506 ASSERT_EQ(0, counter.Value());
507
508 counter.SetTime(0, 1, 0, 1);
509 ASSERT_TRUE(counter.ValueUpdated());
510 ASSERT_TRUE(counter.ValueUpdated());
511 ASSERT_TRUE(counter.ValueUpdated());
512
513 counter.DeleteTimer();
514 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
515 usleep(500000);
516 int value = counter.Value();
517 usleep(500000);
518
519 // Verify the counter has not been incremented.
520 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800521}
Christopher Ferris753ad772014-03-20 20:47:45 -0700522
523struct TimerDeleteData {
524 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800525 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700526 volatile bool complete;
527};
528
529static void TimerDeleteCallback(sigval_t value) {
530 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
531
Elliott Hughes11859d42017-02-13 17:59:29 -0800532 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700533 timer_delete(tdd->timer_id);
534 tdd->complete = true;
535}
536
537TEST(time, timer_delete_from_timer_thread) {
538 TimerDeleteData tdd;
539 sigevent_t se;
540
541 memset(&se, 0, sizeof(se));
542 se.sigev_notify = SIGEV_THREAD;
543 se.sigev_notify_function = TimerDeleteCallback;
544 se.sigev_value.sival_ptr = &tdd;
545
546 tdd.complete = false;
547 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
548
549 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800550 ts.it_value.tv_sec = 1;
551 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700552 ts.it_interval.tv_sec = 0;
553 ts.it_interval.tv_nsec = 0;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800554 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, NULL));
Christopher Ferris753ad772014-03-20 20:47:45 -0700555
556 time_t cur_time = time(NULL);
557 while (!tdd.complete && (time(NULL) - cur_time) < 5);
558 ASSERT_TRUE(tdd.complete);
559
560#if defined(__BIONIC__)
561 // Since bionic timers are implemented by creating a thread to handle the
562 // callback, verify that the thread actually completes.
563 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800564 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
565 ASSERT_EQ(-1, kill(tdd.tid, 0));
566 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700567#endif
568}
Elliott Hughes625993d2014-07-15 16:53:13 -0700569
570TEST(time, clock_gettime) {
571 // Try to ensure that our vdso clock_gettime is working.
572 timespec ts1;
573 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
574 timespec ts2;
575 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
576
577 // What's the difference between the two?
578 ts2.tv_sec -= ts1.tv_sec;
579 ts2.tv_nsec -= ts1.tv_nsec;
580 if (ts2.tv_nsec < 0) {
581 --ts2.tv_sec;
Elliott Hughes04303f52014-09-18 16:11:59 -0700582 ts2.tv_nsec += NS_PER_S;
Elliott Hughes625993d2014-07-15 16:53:13 -0700583 }
584
585 // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
586 ASSERT_EQ(0, ts2.tv_sec);
587 ASSERT_LT(ts2.tv_nsec, 1000000);
588}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700589
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700590TEST(time, clock_gettime_CLOCK_REALTIME) {
591 timespec ts;
592 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
593}
594
595TEST(time, clock_gettime_CLOCK_MONOTONIC) {
596 timespec ts;
597 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
598}
599
600TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
601 timespec ts;
602 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
603}
604
605TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
606 timespec ts;
607 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
608}
609
610TEST(time, clock_gettime_CLOCK_BOOTTIME) {
611 timespec ts;
612 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
613}
614
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800615TEST(time, clock_gettime_unknown) {
616 errno = 0;
617 timespec ts;
618 ASSERT_EQ(-1, clock_gettime(-1, &ts));
619 ASSERT_EQ(EINVAL, errno);
620}
621
622TEST(time, clock_getres_CLOCK_REALTIME) {
623 timespec ts;
624 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
625 ASSERT_EQ(1, ts.tv_nsec);
626 ASSERT_EQ(0, ts.tv_sec);
627}
628
629TEST(time, clock_getres_CLOCK_MONOTONIC) {
630 timespec ts;
631 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
632 ASSERT_EQ(1, ts.tv_nsec);
633 ASSERT_EQ(0, ts.tv_sec);
634}
635
636TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
637 timespec ts;
638 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
639}
640
641TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
642 timespec ts;
643 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
644}
645
646TEST(time, clock_getres_CLOCK_BOOTTIME) {
647 timespec ts;
648 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
649 ASSERT_EQ(1, ts.tv_nsec);
650 ASSERT_EQ(0, ts.tv_sec);
651}
652
653TEST(time, clock_getres_unknown) {
654 errno = 0;
655 timespec ts = { -1, -1 };
656 ASSERT_EQ(-1, clock_getres(-1, &ts));
657 ASSERT_EQ(EINVAL, errno);
658 ASSERT_EQ(-1, ts.tv_nsec);
659 ASSERT_EQ(-1, ts.tv_sec);
660}
661
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700662TEST(time, clock) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900663 // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
664 clock_t t0 = clock();
665 sleep(1);
666 clock_t t1 = clock();
667 ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
668}
669
Elliott Hughesb4413592017-11-29 18:17:06 -0800670static pid_t GetInvalidPid() {
671 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -0800672 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -0800673 fscanf(fp.get(), "%ld", &pid_max);
674 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -0800675}
676
Elliott Hughesb4413592017-11-29 18:17:06 -0800677TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800678 clockid_t clockid;
679 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -0800680 timespec ts;
681 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800682}
Yabin Cuid5c65272014-11-26 14:04:26 -0800683
Elliott Hughesb4413592017-11-29 18:17:06 -0800684TEST(time, clock_getcpuclockid_parent) {
685 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -0800686 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -0800687 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -0800688 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800689}
Yabin Cuid5c65272014-11-26 14:04:26 -0800690
Elliott Hughesb4413592017-11-29 18:17:06 -0800691TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800692 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
693 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800694 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -0800695 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800696 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
697 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
698 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
699 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
700 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
701 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -0800702 ASSERT_EQ(0, errno);
703}
704
Haruki Hasegawa18160252014-10-13 00:50:47 +0900705TEST(time, clock_settime) {
706 errno = 0;
707 timespec ts;
708 ASSERT_EQ(-1, clock_settime(-1, &ts));
709 ASSERT_EQ(EINVAL, errno);
710}
711
712TEST(time, clock_nanosleep) {
713 timespec in;
714 timespec out;
715 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700716}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700717
718TEST(time, clock_nanosleep_thread_cputime_id) {
719 timespec in;
720 in.tv_sec = 1;
721 in.tv_nsec = 0;
722 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
723}
Elliott Hughes12443702016-10-19 16:02:31 -0700724
725TEST(time, bug_31938693) {
726 // User-visible symptoms in N:
727 // http://b/31938693
728 // https://code.google.com/p/android/issues/detail?id=225132
729
730 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
731 // http://b/31848040
732
733 // This isn't a great test, because very few time zones were actually affected, and there's
734 // no real logic to which ones were affected: it was just a coincidence of the data that came
735 // after them in the tzdata file.
736
737 time_t t = 1475619727;
738 struct tm tm;
739
740 setenv("TZ", "America/Los_Angeles", 1);
741 tzset();
742 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
743 EXPECT_EQ(15, tm.tm_hour);
744
745 setenv("TZ", "Europe/London", 1);
746 tzset();
747 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
748 EXPECT_EQ(23, tm.tm_hour);
749
750 setenv("TZ", "America/Atka", 1);
751 tzset();
752 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
753 EXPECT_EQ(13, tm.tm_hour);
754
755 setenv("TZ", "Pacific/Apia", 1);
756 tzset();
757 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
758 EXPECT_EQ(12, tm.tm_hour);
759
760 setenv("TZ", "Pacific/Honolulu", 1);
761 tzset();
762 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
763 EXPECT_EQ(12, tm.tm_hour);
764
765 setenv("TZ", "Asia/Magadan", 1);
766 tzset();
767 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
768 EXPECT_EQ(9, tm.tm_hour);
769}
Elliott Hughesea877162017-01-11 14:34:16 -0800770
771TEST(time, bug_31339449) {
772 // POSIX says localtime acts as if it calls tzset.
773 // tzset does two things:
774 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
775 // 2. it sets the global `tzname`.
776 // POSIX says localtime_r need not set `tzname` (2).
777 // Q: should localtime_r set the time zone (1)?
778 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
779
780 // Pick a time, any time...
781 time_t t = 1475619727;
782
783 // Call tzset with a specific timezone.
784 setenv("TZ", "America/Atka", 1);
785 tzset();
786
787 // If we change the timezone and call localtime, localtime should use the new timezone.
788 setenv("TZ", "America/Los_Angeles", 1);
789 struct tm* tm_p = localtime(&t);
790 EXPECT_EQ(15, tm_p->tm_hour);
791
792 // Reset the timezone back.
793 setenv("TZ", "America/Atka", 1);
794 tzset();
795
796#if defined(__BIONIC__)
797 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
798 setenv("TZ", "America/Los_Angeles", 1);
799 struct tm tm = {};
800 localtime_r(&t, &tm);
801 EXPECT_EQ(15, tm.tm_hour);
802#else
803 // The BSDs agree with us, but glibc gets this wrong.
804#endif
805}
Elliott Hughes5a29d542017-12-07 16:05:57 -0800806
807TEST(time, asctime) {
808 const struct tm tm = {};
809 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
810}
811
812TEST(time, asctime_r) {
813 const struct tm tm = {};
814 char buf[256];
815 ASSERT_EQ(buf, asctime_r(&tm, buf));
816 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
817}
818
819TEST(time, ctime) {
820 setenv("TZ", "UTC", 1);
821 const time_t t = 0;
822 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
823}
824
825TEST(time, ctime_r) {
826 setenv("TZ", "UTC", 1);
827 const time_t t = 0;
828 char buf[256];
829 ASSERT_EQ(buf, ctime_r(&t, buf));
830 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
831}