1 /*
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
17 #include <time.h>
18
19 #include <errno.h>
20 #include <gtest/gtest.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include <sys/syscall.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27
28 #include <atomic>
29 #include <chrono>
30
31 #include "SignalUtils.h"
32 #include "utils.h"
33
34 #include "private/bionic_constants.h"
35
36 using namespace std::chrono_literals;
37
TEST(time,time)38 TEST(time, time) {
39 // Acquire time
40 time_t p1, t1 = time(&p1);
41 // valid?
42 ASSERT_NE(static_cast<time_t>(0), t1);
43 ASSERT_NE(static_cast<time_t>(-1), t1);
44 ASSERT_EQ(p1, t1);
45
46 // Acquire time one+ second later
47 usleep(1010000);
48 time_t p2, t2 = time(&p2);
49 // valid?
50 ASSERT_NE(static_cast<time_t>(0), t2);
51 ASSERT_NE(static_cast<time_t>(-1), t2);
52 ASSERT_EQ(p2, t2);
53
54 // Expect time progression
55 ASSERT_LT(p1, p2);
56 ASSERT_LE(t2 - t1, static_cast<time_t>(2));
57
58 // Expect nullptr call to produce same results
59 ASSERT_LE(t2, time(nullptr));
60 ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1));
61 }
62
TEST(time,gmtime)63 TEST(time, gmtime) {
64 time_t t = 0;
65 tm* broken_down = gmtime(&t);
66 ASSERT_TRUE(broken_down != nullptr);
67 ASSERT_EQ(0, broken_down->tm_sec);
68 ASSERT_EQ(0, broken_down->tm_min);
69 ASSERT_EQ(0, broken_down->tm_hour);
70 ASSERT_EQ(1, broken_down->tm_mday);
71 ASSERT_EQ(0, broken_down->tm_mon);
72 ASSERT_EQ(1970, broken_down->tm_year + 1900);
73 }
74
TEST(time,gmtime_r)75 TEST(time, gmtime_r) {
76 struct tm tm = {};
77 time_t t = 0;
78 struct tm* broken_down = gmtime_r(&t, &tm);
79 ASSERT_EQ(broken_down, &tm);
80 ASSERT_EQ(0, broken_down->tm_sec);
81 ASSERT_EQ(0, broken_down->tm_min);
82 ASSERT_EQ(0, broken_down->tm_hour);
83 ASSERT_EQ(1, broken_down->tm_mday);
84 ASSERT_EQ(0, broken_down->tm_mon);
85 ASSERT_EQ(1970, broken_down->tm_year + 1900);
86 }
87
gmtime_no_stack_overflow_14313703_fn(void *)88 static void* gmtime_no_stack_overflow_14313703_fn(void*) {
89 const char* original_tz = getenv("TZ");
90 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
91 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
92 tzset();
93 if (original_tz != nullptr) {
94 setenv("TZ", original_tz, 1);
95 }
96 tzset();
97 return nullptr;
98 }
99
TEST(time,gmtime_no_stack_overflow_14313703)100 TEST(time, gmtime_no_stack_overflow_14313703) {
101 // Is it safe to call tzload on a thread with a small stack?
102 // http://b/14313703
103 // https://code.google.com/p/android/issues/detail?id=61130
104 pthread_attr_t a;
105 ASSERT_EQ(0, pthread_attr_init(&a));
106 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
107
108 pthread_t t;
109 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
110 ASSERT_EQ(0, pthread_join(t, nullptr));
111 }
112
TEST(time,mktime_empty_TZ)113 TEST(time, mktime_empty_TZ) {
114 // tzcode used to have a bug where it didn't reinitialize some internal state.
115
116 // Choose a time where DST is set.
117 struct tm t;
118 memset(&t, 0, sizeof(tm));
119 t.tm_year = 1980 - 1900;
120 t.tm_mon = 6;
121 t.tm_mday = 2;
122
123 setenv("TZ", "America/Los_Angeles", 1);
124 tzset();
125 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
126
127 memset(&t, 0, sizeof(tm));
128 t.tm_year = 1980 - 1900;
129 t.tm_mon = 6;
130 t.tm_mday = 2;
131
132 setenv("TZ", "", 1); // Implies UTC.
133 tzset();
134 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
135 }
136
TEST(time,mktime_10310929)137 TEST(time, mktime_10310929) {
138 struct tm t;
139 memset(&t, 0, sizeof(tm));
140 t.tm_year = 200;
141 t.tm_mon = 2;
142 t.tm_mday = 10;
143
144 #if !defined(__LP64__)
145 // 32-bit bionic has a signed 32-bit time_t.
146 ASSERT_EQ(-1, mktime(&t));
147 ASSERT_EQ(EOVERFLOW, errno);
148 #else
149 // Everyone else should be using a signed 64-bit time_t.
150 ASSERT_GE(sizeof(time_t) * 8, 64U);
151
152 setenv("TZ", "America/Los_Angeles", 1);
153 tzset();
154 errno = 0;
155 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
156 ASSERT_EQ(0, errno);
157
158 setenv("TZ", "UTC", 1);
159 tzset();
160 errno = 0;
161 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
162 ASSERT_EQ(0, errno);
163 #endif
164 }
165
TEST(time,mktime_EOVERFLOW)166 TEST(time, mktime_EOVERFLOW) {
167 struct tm t;
168 memset(&t, 0, sizeof(tm));
169
170 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
171 t.tm_year = 2016 - 1900;
172
173 t.tm_mon = 2;
174 t.tm_mday = 10;
175
176 errno = 0;
177 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
178 ASSERT_EQ(0, errno);
179
180 // This will overflow for LP32 or LP64.
181 t.tm_year = INT_MAX;
182
183 errno = 0;
184 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
185 ASSERT_EQ(EOVERFLOW, errno);
186 }
187
TEST(time,strftime)188 TEST(time, strftime) {
189 setenv("TZ", "UTC", 1);
190
191 struct tm t;
192 memset(&t, 0, sizeof(tm));
193 t.tm_year = 200;
194 t.tm_mon = 2;
195 t.tm_mday = 10;
196
197 char buf[64];
198
199 // Seconds since the epoch.
200 #if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
201 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
202 EXPECT_STREQ("4108320000", buf);
203 #endif
204
205 // Date and time as text.
206 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
207 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
208 }
209
TEST(time,strftime_null_tm_zone)210 TEST(time, strftime_null_tm_zone) {
211 // Netflix on Nexus Player wouldn't start (http://b/25170306).
212 struct tm t;
213 memset(&t, 0, sizeof(tm));
214
215 char buf[64];
216
217 setenv("TZ", "America/Los_Angeles", 1);
218 tzset();
219
220 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
221 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
222 EXPECT_STREQ("<PST>", buf);
223
224 #if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
225 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
226 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
227 EXPECT_STREQ("<PDT>", buf);
228
229 t.tm_isdst = -123; // "and negative if the information is not available".
230 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
231 EXPECT_STREQ("<>", buf);
232 #endif
233
234 setenv("TZ", "UTC", 1);
235 tzset();
236
237 t.tm_isdst = 0;
238 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
239 EXPECT_STREQ("<UTC>", buf);
240
241 #if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
242 t.tm_isdst = 1; // UTC has no DST.
243 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
244 EXPECT_STREQ("<>", buf);
245 #endif
246 }
247
TEST(time,strftime_l)248 TEST(time, strftime_l) {
249 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
250 locale_t old_locale = uselocale(cloc);
251
252 setenv("TZ", "UTC", 1);
253
254 struct tm t;
255 memset(&t, 0, sizeof(tm));
256 t.tm_year = 200;
257 t.tm_mon = 2;
258 t.tm_mday = 10;
259
260 // Date and time as text.
261 char buf[64];
262 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
263 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
264
265 uselocale(old_locale);
266 freelocale(cloc);
267 }
268
TEST(time,strptime)269 TEST(time, strptime) {
270 setenv("TZ", "UTC", 1);
271
272 struct tm t;
273 char buf[64];
274
275 memset(&t, 0, sizeof(t));
276 strptime("11:14", "%R", &t);
277 strftime(buf, sizeof(buf), "%H:%M", &t);
278 EXPECT_STREQ("11:14", buf);
279
280 memset(&t, 0, sizeof(t));
281 strptime("09:41:53", "%T", &t);
282 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
283 EXPECT_STREQ("09:41:53", buf);
284 }
285
TEST(time,strptime_l)286 TEST(time, strptime_l) {
287 setenv("TZ", "UTC", 1);
288
289 struct tm t;
290 char buf[64];
291
292 memset(&t, 0, sizeof(t));
293 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
294 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
295 EXPECT_STREQ("11:14", buf);
296
297 memset(&t, 0, sizeof(t));
298 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
299 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
300 EXPECT_STREQ("09:41:53", buf);
301 }
302
TEST(time,strptime_F)303 TEST(time, strptime_F) {
304 setenv("TZ", "UTC", 1);
305
306 struct tm tm = {};
307 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
308 EXPECT_EQ(119, tm.tm_year);
309 EXPECT_EQ(2, tm.tm_mon);
310 EXPECT_EQ(26, tm.tm_mday);
311 }
312
TEST(time,strptime_P_p)313 TEST(time, strptime_P_p) {
314 setenv("TZ", "UTC", 1);
315
316 // For parsing, %P and %p are the same: case doesn't matter.
317
318 struct tm tm = {.tm_hour = 12};
319 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
320 EXPECT_EQ(0, tm.tm_hour);
321
322 tm = {.tm_hour = 12};
323 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
324 EXPECT_EQ(0, tm.tm_hour);
325
326 tm = {.tm_hour = 12};
327 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
328 EXPECT_EQ(0, tm.tm_hour);
329
330 tm = {.tm_hour = 12};
331 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
332 EXPECT_EQ(0, tm.tm_hour);
333 }
334
TEST(time,strptime_u)335 TEST(time, strptime_u) {
336 setenv("TZ", "UTC", 1);
337
338 struct tm tm = {};
339 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
340 EXPECT_EQ(2, tm.tm_wday);
341 }
342
TEST(time,strptime_v)343 TEST(time, strptime_v) {
344 setenv("TZ", "UTC", 1);
345
346 struct tm tm = {};
347 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
348 EXPECT_EQ(80, tm.tm_year);
349 EXPECT_EQ(2, tm.tm_mon);
350 EXPECT_EQ(26, tm.tm_mday);
351 }
352
TEST(time,strptime_V_G_g)353 TEST(time, strptime_V_G_g) {
354 setenv("TZ", "UTC", 1);
355
356 // %V (ISO-8601 week number), %G (year of week number, without century), and
357 // %g (year of week number) have no effect when parsed, and are supported
358 // solely so that it's possible for strptime(3) to parse everything that
359 // strftime(3) can output.
360 struct tm tm = {};
361 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
362 struct tm zero = {};
363 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
364 }
365
SetTime(timer_t t,time_t value_s,time_t value_ns,time_t interval_s,time_t interval_ns)366 void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
367 itimerspec ts;
368 ts.it_value.tv_sec = value_s;
369 ts.it_value.tv_nsec = value_ns;
370 ts.it_interval.tv_sec = interval_s;
371 ts.it_interval.tv_nsec = interval_ns;
372 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
373 }
374
NoOpNotifyFunction(sigval_t)375 static void NoOpNotifyFunction(sigval_t) {
376 }
377
TEST(time,timer_create)378 TEST(time, timer_create) {
379 sigevent_t se;
380 memset(&se, 0, sizeof(se));
381 se.sigev_notify = SIGEV_THREAD;
382 se.sigev_notify_function = NoOpNotifyFunction;
383 timer_t timer_id;
384 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
385
386 pid_t pid = fork();
387 ASSERT_NE(-1, pid) << strerror(errno);
388
389 if (pid == 0) {
390 // Timers are not inherited by the child.
391 ASSERT_EQ(-1, timer_delete(timer_id));
392 ASSERT_EQ(EINVAL, errno);
393 _exit(0);
394 }
395
396 AssertChildExited(pid, 0);
397
398 ASSERT_EQ(0, timer_delete(timer_id));
399 }
400
401 static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
timer_create_SIGEV_SIGNAL_signal_handler(int signal_number)402 static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
403 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
404 ASSERT_EQ(SIGUSR1, signal_number);
405 }
406
TEST(time,timer_create_SIGEV_SIGNAL)407 TEST(time, timer_create_SIGEV_SIGNAL) {
408 sigevent_t se;
409 memset(&se, 0, sizeof(se));
410 se.sigev_notify = SIGEV_SIGNAL;
411 se.sigev_signo = SIGUSR1;
412
413 timer_t timer_id;
414 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
415
416 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
417 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
418
419 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
420
421 itimerspec ts;
422 ts.it_value.tv_sec = 0;
423 ts.it_value.tv_nsec = 1;
424 ts.it_interval.tv_sec = 0;
425 ts.it_interval.tv_nsec = 0;
426 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
427
428 usleep(500000);
429 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
430 }
431
432 struct Counter {
433 private:
434 std::atomic<int> value;
435 timer_t timer_id;
436 sigevent_t se;
437 bool timer_valid;
438
CreateCounter439 void Create() {
440 ASSERT_FALSE(timer_valid);
441 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
442 timer_valid = true;
443 }
444
445 public:
CounterCounter446 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
447 memset(&se, 0, sizeof(se));
448 se.sigev_notify = SIGEV_THREAD;
449 se.sigev_notify_function = fn;
450 se.sigev_value.sival_ptr = this;
451 Create();
452 }
DeleteTimerCounter453 void DeleteTimer() {
454 ASSERT_TRUE(timer_valid);
455 ASSERT_EQ(0, timer_delete(timer_id));
456 timer_valid = false;
457 }
458
~CounterCounter459 ~Counter() {
460 if (timer_valid) {
461 DeleteTimer();
462 }
463 }
464
ValueCounter465 int Value() const {
466 return value;
467 }
468
SetTimeCounter469 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
470 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
471 }
472
ValueUpdatedCounter473 bool ValueUpdated() {
474 int current_value = value;
475 time_t start = time(nullptr);
476 while (current_value == value && (time(nullptr) - start) < 5) {
477 }
478 return current_value != value;
479 }
480
CountNotifyFunctionCounter481 static void CountNotifyFunction(sigval_t value) {
482 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
483 ++cd->value;
484 }
485
CountAndDisarmNotifyFunctionCounter486 static void CountAndDisarmNotifyFunction(sigval_t value) {
487 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
488 ++cd->value;
489
490 // Setting the initial expiration time to 0 disarms the timer.
491 cd->SetTime(0, 0, 1, 0);
492 }
493 };
494
TEST(time,timer_settime_0)495 TEST(time, timer_settime_0) {
496 Counter counter(Counter::CountAndDisarmNotifyFunction);
497 ASSERT_EQ(0, counter.Value());
498
499 counter.SetTime(0, 500000000, 1, 0);
500 sleep(1);
501
502 // The count should just be 1 because we disarmed the timer the first time it fired.
503 ASSERT_EQ(1, counter.Value());
504 }
505
TEST(time,timer_settime_repeats)506 TEST(time, timer_settime_repeats) {
507 Counter counter(Counter::CountNotifyFunction);
508 ASSERT_EQ(0, counter.Value());
509
510 counter.SetTime(0, 1, 0, 10);
511 ASSERT_TRUE(counter.ValueUpdated());
512 ASSERT_TRUE(counter.ValueUpdated());
513 ASSERT_TRUE(counter.ValueUpdated());
514 counter.DeleteTimer();
515 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
516 usleep(500000);
517 }
518
519 static int timer_create_NULL_signal_handler_invocation_count;
timer_create_NULL_signal_handler(int signal_number)520 static void timer_create_NULL_signal_handler(int signal_number) {
521 ++timer_create_NULL_signal_handler_invocation_count;
522 ASSERT_EQ(SIGALRM, signal_number);
523 }
524
TEST(time,timer_create_NULL)525 TEST(time, timer_create_NULL) {
526 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
527 timer_t timer_id;
528 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
529
530 timer_create_NULL_signal_handler_invocation_count = 0;
531 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
532
533 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
534
535 SetTime(timer_id, 0, 1, 0, 0);
536 usleep(500000);
537
538 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
539 }
540
TEST(time,timer_create_EINVAL)541 TEST(time, timer_create_EINVAL) {
542 clockid_t invalid_clock = 16;
543
544 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
545 timer_t timer_id;
546 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
547 ASSERT_EQ(EINVAL, errno);
548
549 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
550 sigevent_t se;
551 memset(&se, 0, sizeof(se));
552 se.sigev_notify = SIGEV_THREAD;
553 se.sigev_notify_function = NoOpNotifyFunction;
554 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
555 ASSERT_EQ(EINVAL, errno);
556 }
557
TEST(time,timer_create_multiple)558 TEST(time, timer_create_multiple) {
559 Counter counter1(Counter::CountNotifyFunction);
560 Counter counter2(Counter::CountNotifyFunction);
561 Counter counter3(Counter::CountNotifyFunction);
562
563 ASSERT_EQ(0, counter1.Value());
564 ASSERT_EQ(0, counter2.Value());
565 ASSERT_EQ(0, counter3.Value());
566
567 counter2.SetTime(0, 500000000, 0, 0);
568 sleep(1);
569
570 EXPECT_EQ(0, counter1.Value());
571 EXPECT_EQ(1, counter2.Value());
572 EXPECT_EQ(0, counter3.Value());
573 }
574
575 // Test to verify that disarming a repeatable timer disables the callbacks.
TEST(time,timer_disarm_terminates)576 TEST(time, timer_disarm_terminates) {
577 Counter counter(Counter::CountNotifyFunction);
578 ASSERT_EQ(0, counter.Value());
579
580 counter.SetTime(0, 1, 0, 1);
581 ASSERT_TRUE(counter.ValueUpdated());
582 ASSERT_TRUE(counter.ValueUpdated());
583 ASSERT_TRUE(counter.ValueUpdated());
584
585 counter.SetTime(0, 0, 0, 0);
586 // Add a sleep as the kernel may have pending events when the timer is disarmed.
587 usleep(500000);
588 int value = counter.Value();
589 usleep(500000);
590
591 // Verify the counter has not been incremented.
592 ASSERT_EQ(value, counter.Value());
593 }
594
595 // Test to verify that deleting a repeatable timer disables the callbacks.
TEST(time,timer_delete_terminates)596 TEST(time, timer_delete_terminates) {
597 Counter counter(Counter::CountNotifyFunction);
598 ASSERT_EQ(0, counter.Value());
599
600 counter.SetTime(0, 1, 0, 1);
601 ASSERT_TRUE(counter.ValueUpdated());
602 ASSERT_TRUE(counter.ValueUpdated());
603 ASSERT_TRUE(counter.ValueUpdated());
604
605 counter.DeleteTimer();
606 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
607 usleep(500000);
608 int value = counter.Value();
609 usleep(500000);
610
611 // Verify the counter has not been incremented.
612 ASSERT_EQ(value, counter.Value());
613 }
614
615 struct TimerDeleteData {
616 timer_t timer_id;
617 pid_t tid;
618 volatile bool complete;
619 };
620
TimerDeleteCallback(sigval_t value)621 static void TimerDeleteCallback(sigval_t value) {
622 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
623
624 tdd->tid = gettid();
625 timer_delete(tdd->timer_id);
626 tdd->complete = true;
627 }
628
TEST(time,timer_delete_from_timer_thread)629 TEST(time, timer_delete_from_timer_thread) {
630 TimerDeleteData tdd;
631 sigevent_t se;
632
633 memset(&se, 0, sizeof(se));
634 se.sigev_notify = SIGEV_THREAD;
635 se.sigev_notify_function = TimerDeleteCallback;
636 se.sigev_value.sival_ptr = &tdd;
637
638 tdd.complete = false;
639 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
640
641 itimerspec ts;
642 ts.it_value.tv_sec = 1;
643 ts.it_value.tv_nsec = 0;
644 ts.it_interval.tv_sec = 0;
645 ts.it_interval.tv_nsec = 0;
646 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
647
648 time_t cur_time = time(nullptr);
649 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
650 ASSERT_TRUE(tdd.complete);
651
652 #if defined(__BIONIC__)
653 // Since bionic timers are implemented by creating a thread to handle the
654 // callback, verify that the thread actually completes.
655 cur_time = time(NULL);
656 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
657 ASSERT_EQ(-1, kill(tdd.tid, 0));
658 ASSERT_EQ(ESRCH, errno);
659 #endif
660 }
661
TEST(time,clock_gettime)662 TEST(time, clock_gettime) {
663 // Try to ensure that our vdso clock_gettime is working.
664 timespec ts1;
665 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
666 timespec ts2;
667 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
668
669 // What's the difference between the two?
670 ts2.tv_sec -= ts1.tv_sec;
671 ts2.tv_nsec -= ts1.tv_nsec;
672 if (ts2.tv_nsec < 0) {
673 --ts2.tv_sec;
674 ts2.tv_nsec += NS_PER_S;
675 }
676
677 // To try to avoid flakiness we'll accept answers within 10,000,000ns (0.01s).
678 ASSERT_EQ(0, ts2.tv_sec);
679 ASSERT_LT(ts2.tv_nsec, 10'000'000);
680 }
681
TEST(time,clock_gettime_CLOCK_REALTIME)682 TEST(time, clock_gettime_CLOCK_REALTIME) {
683 timespec ts;
684 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
685 }
686
TEST(time,clock_gettime_CLOCK_MONOTONIC)687 TEST(time, clock_gettime_CLOCK_MONOTONIC) {
688 timespec ts;
689 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
690 }
691
TEST(time,clock_gettime_CLOCK_PROCESS_CPUTIME_ID)692 TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
693 timespec ts;
694 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
695 }
696
TEST(time,clock_gettime_CLOCK_THREAD_CPUTIME_ID)697 TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
698 timespec ts;
699 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
700 }
701
TEST(time,clock_gettime_CLOCK_BOOTTIME)702 TEST(time, clock_gettime_CLOCK_BOOTTIME) {
703 timespec ts;
704 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
705 }
706
TEST(time,clock_gettime_unknown)707 TEST(time, clock_gettime_unknown) {
708 errno = 0;
709 timespec ts;
710 ASSERT_EQ(-1, clock_gettime(-1, &ts));
711 ASSERT_EQ(EINVAL, errno);
712 }
713
TEST(time,clock_getres_CLOCK_REALTIME)714 TEST(time, clock_getres_CLOCK_REALTIME) {
715 timespec ts;
716 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
717 ASSERT_EQ(1, ts.tv_nsec);
718 ASSERT_EQ(0, ts.tv_sec);
719 }
720
TEST(time,clock_getres_CLOCK_MONOTONIC)721 TEST(time, clock_getres_CLOCK_MONOTONIC) {
722 timespec ts;
723 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
724 ASSERT_EQ(1, ts.tv_nsec);
725 ASSERT_EQ(0, ts.tv_sec);
726 }
727
TEST(time,clock_getres_CLOCK_PROCESS_CPUTIME_ID)728 TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
729 timespec ts;
730 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
731 }
732
TEST(time,clock_getres_CLOCK_THREAD_CPUTIME_ID)733 TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
734 timespec ts;
735 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
736 }
737
TEST(time,clock_getres_CLOCK_BOOTTIME)738 TEST(time, clock_getres_CLOCK_BOOTTIME) {
739 timespec ts;
740 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
741 ASSERT_EQ(1, ts.tv_nsec);
742 ASSERT_EQ(0, ts.tv_sec);
743 }
744
TEST(time,clock_getres_unknown)745 TEST(time, clock_getres_unknown) {
746 errno = 0;
747 timespec ts = { -1, -1 };
748 ASSERT_EQ(-1, clock_getres(-1, &ts));
749 ASSERT_EQ(EINVAL, errno);
750 ASSERT_EQ(-1, ts.tv_nsec);
751 ASSERT_EQ(-1, ts.tv_sec);
752 }
753
TEST(time,clock)754 TEST(time, clock) {
755 // clock(3) is hard to test, but a 1s sleep should cost less than 5ms.
756 clock_t t0 = clock();
757 sleep(1);
758 clock_t t1 = clock();
759 ASSERT_LT(t1 - t0, 5 * (CLOCKS_PER_SEC / 1000));
760 }
761
GetInvalidPid()762 static pid_t GetInvalidPid() {
763 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
764 long pid_max;
765 fscanf(fp.get(), "%ld", &pid_max);
766 return static_cast<pid_t>(pid_max + 1);
767 }
768
TEST(time,clock_getcpuclockid_current)769 TEST(time, clock_getcpuclockid_current) {
770 clockid_t clockid;
771 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
772 timespec ts;
773 ASSERT_EQ(0, clock_gettime(clockid, &ts));
774 }
775
TEST(time,clock_getcpuclockid_parent)776 TEST(time, clock_getcpuclockid_parent) {
777 clockid_t clockid;
778 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
779 timespec ts;
780 ASSERT_EQ(0, clock_gettime(clockid, &ts));
781 }
782
TEST(time,clock_getcpuclockid_ESRCH)783 TEST(time, clock_getcpuclockid_ESRCH) {
784 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
785 errno = 0;
786 // If this fails, your kernel needs commit e1b6b6ce to be backported.
787 clockid_t clockid;
788 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
789 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
790 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
791 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
792 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
793 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
794 ASSERT_EQ(0, errno);
795 }
796
TEST(time,clock_settime)797 TEST(time, clock_settime) {
798 errno = 0;
799 timespec ts;
800 ASSERT_EQ(-1, clock_settime(-1, &ts));
801 ASSERT_EQ(EINVAL, errno);
802 }
803
TEST(time,clock_nanosleep_EINVAL)804 TEST(time, clock_nanosleep_EINVAL) {
805 timespec in;
806 timespec out;
807 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
808 }
809
TEST(time,clock_nanosleep_thread_cputime_id)810 TEST(time, clock_nanosleep_thread_cputime_id) {
811 timespec in;
812 in.tv_sec = 1;
813 in.tv_nsec = 0;
814 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
815 }
816
TEST(time,clock_nanosleep)817 TEST(time, clock_nanosleep) {
818 auto t0 = std::chrono::steady_clock::now();
819 const timespec ts = {.tv_nsec = 5000000};
820 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
821 auto t1 = std::chrono::steady_clock::now();
822 ASSERT_GE(t1-t0, 5000000ns);
823 }
824
TEST(time,nanosleep)825 TEST(time, nanosleep) {
826 auto t0 = std::chrono::steady_clock::now();
827 const timespec ts = {.tv_nsec = 5000000};
828 ASSERT_EQ(0, nanosleep(&ts, nullptr));
829 auto t1 = std::chrono::steady_clock::now();
830 ASSERT_GE(t1-t0, 5000000ns);
831 }
832
TEST(time,nanosleep_EINVAL)833 TEST(time, nanosleep_EINVAL) {
834 timespec ts = {.tv_sec = -1};
835 errno = 0;
836 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
837 ASSERT_EQ(EINVAL, errno);
838 }
839
TEST(time,bug_31938693)840 TEST(time, bug_31938693) {
841 // User-visible symptoms in N:
842 // http://b/31938693
843 // https://code.google.com/p/android/issues/detail?id=225132
844
845 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
846 // http://b/31848040
847
848 // This isn't a great test, because very few time zones were actually affected, and there's
849 // no real logic to which ones were affected: it was just a coincidence of the data that came
850 // after them in the tzdata file.
851
852 time_t t = 1475619727;
853 struct tm tm;
854
855 setenv("TZ", "America/Los_Angeles", 1);
856 tzset();
857 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
858 EXPECT_EQ(15, tm.tm_hour);
859
860 setenv("TZ", "Europe/London", 1);
861 tzset();
862 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
863 EXPECT_EQ(23, tm.tm_hour);
864
865 setenv("TZ", "America/Atka", 1);
866 tzset();
867 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
868 EXPECT_EQ(13, tm.tm_hour);
869
870 setenv("TZ", "Pacific/Apia", 1);
871 tzset();
872 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
873 EXPECT_EQ(12, tm.tm_hour);
874
875 setenv("TZ", "Pacific/Honolulu", 1);
876 tzset();
877 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
878 EXPECT_EQ(12, tm.tm_hour);
879
880 setenv("TZ", "Asia/Magadan", 1);
881 tzset();
882 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
883 EXPECT_EQ(9, tm.tm_hour);
884 }
885
TEST(time,bug_31339449)886 TEST(time, bug_31339449) {
887 // POSIX says localtime acts as if it calls tzset.
888 // tzset does two things:
889 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
890 // 2. it sets the global `tzname`.
891 // POSIX says localtime_r need not set `tzname` (2).
892 // Q: should localtime_r set the time zone (1)?
893 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
894
895 // Pick a time, any time...
896 time_t t = 1475619727;
897
898 // Call tzset with a specific timezone.
899 setenv("TZ", "America/Atka", 1);
900 tzset();
901
902 // If we change the timezone and call localtime, localtime should use the new timezone.
903 setenv("TZ", "America/Los_Angeles", 1);
904 struct tm* tm_p = localtime(&t);
905 EXPECT_EQ(15, tm_p->tm_hour);
906
907 // Reset the timezone back.
908 setenv("TZ", "America/Atka", 1);
909 tzset();
910
911 #if defined(__BIONIC__)
912 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
913 setenv("TZ", "America/Los_Angeles", 1);
914 struct tm tm = {};
915 localtime_r(&t, &tm);
916 EXPECT_EQ(15, tm.tm_hour);
917 #else
918 // The BSDs agree with us, but glibc gets this wrong.
919 #endif
920 }
921
TEST(time,asctime)922 TEST(time, asctime) {
923 const struct tm tm = {};
924 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
925 }
926
TEST(time,asctime_r)927 TEST(time, asctime_r) {
928 const struct tm tm = {};
929 char buf[256];
930 ASSERT_EQ(buf, asctime_r(&tm, buf));
931 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
932 }
933
TEST(time,ctime)934 TEST(time, ctime) {
935 setenv("TZ", "UTC", 1);
936 const time_t t = 0;
937 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
938 }
939
TEST(time,ctime_r)940 TEST(time, ctime_r) {
941 setenv("TZ", "UTC", 1);
942 const time_t t = 0;
943 char buf[256];
944 ASSERT_EQ(buf, ctime_r(&t, buf));
945 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
946 }
947
948 // https://issuetracker.google.com/37128336
TEST(time,strftime_strptime_s)949 TEST(time, strftime_strptime_s) {
950 char buf[32];
951 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
952
953 setenv("TZ", "America/Los_Angeles", 1);
954 strftime(buf, sizeof(buf), "<%s>", &tm0);
955 EXPECT_STREQ("<378720000>", buf);
956
957 setenv("TZ", "UTC", 1);
958 strftime(buf, sizeof(buf), "<%s>", &tm0);
959 EXPECT_STREQ("<378691200>", buf);
960
961 struct tm tm;
962
963 setenv("TZ", "America/Los_Angeles", 1);
964 tzset();
965 memset(&tm, 0xff, sizeof(tm));
966 char* p = strptime("378720000x", "%s", &tm);
967 ASSERT_EQ('x', *p);
968 EXPECT_EQ(0, tm.tm_sec);
969 EXPECT_EQ(0, tm.tm_min);
970 EXPECT_EQ(0, tm.tm_hour);
971 EXPECT_EQ(1, tm.tm_mday);
972 EXPECT_EQ(0, tm.tm_mon);
973 EXPECT_EQ(82, tm.tm_year);
974 EXPECT_EQ(5, tm.tm_wday);
975 EXPECT_EQ(0, tm.tm_yday);
976 EXPECT_EQ(0, tm.tm_isdst);
977
978 setenv("TZ", "UTC", 1);
979 tzset();
980 memset(&tm, 0xff, sizeof(tm));
981 p = strptime("378691200x", "%s", &tm);
982 ASSERT_EQ('x', *p);
983 EXPECT_EQ(0, tm.tm_sec);
984 EXPECT_EQ(0, tm.tm_min);
985 EXPECT_EQ(0, tm.tm_hour);
986 EXPECT_EQ(1, tm.tm_mday);
987 EXPECT_EQ(0, tm.tm_mon);
988 EXPECT_EQ(82, tm.tm_year);
989 EXPECT_EQ(5, tm.tm_wday);
990 EXPECT_EQ(0, tm.tm_yday);
991 EXPECT_EQ(0, tm.tm_isdst);
992 }
993
TEST(time,strptime_s_nothing)994 TEST(time, strptime_s_nothing) {
995 struct tm tm;
996 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
997 }
998
TEST(time,timespec_get)999 TEST(time, timespec_get) {
1000 #if __BIONIC__
1001 timespec ts = {};
1002 ASSERT_EQ(0, timespec_get(&ts, 123));
1003 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1004 #else
1005 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
1006 #endif
1007 }
1008
TEST(time,difftime)1009 TEST(time, difftime) {
1010 ASSERT_EQ(1.0, difftime(1, 0));
1011 }
1012