blob: 16299cc5223a1e3649eff898c8047048ecbd5798 [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>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070027
Yabin Cui95f1ee22015-01-13 19:53:15 -080028#include <atomic>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070029#include <chrono>
Elliott Hughese0175ca2013-03-14 14:38:08 -070030
Elliott Hughes71ba5892018-02-07 12:44:45 -080031#include "SignalUtils.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080032#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070033
Elliott Hughes04303f52014-09-18 16:11:59 -070034#include "private/bionic_constants.h"
35
Elliott Hughesca3f8e42019-10-28 15:59:38 -070036using namespace std::chrono_literals;
37
Mark Salyzyn0b846e82017-12-20 08:56:18 -080038TEST(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
Elliott Hughesee178bf2013-07-12 11:25:20 -070063TEST(time, gmtime) {
64 time_t t = 0;
65 tm* broken_down = gmtime(&t);
Yi Kong32bc0fc2018-08-02 17:31:13 -070066 ASSERT_TRUE(broken_down != nullptr);
Elliott Hughesee178bf2013-07-12 11:25:20 -070067 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}
Elliott Hughes7843d442013-08-22 11:37:32 -070074
Elliott Hughes5a29d542017-12-07 16:05:57 -080075TEST(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
Elliott Hughes329103d2014-04-25 16:55:04 -070088static 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();
Yi Kong32bc0fc2018-08-02 17:31:13 -070093 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -070094 setenv("TZ", original_tz, 1);
95 }
96 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -070097 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -070098}
99
100TEST(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
Elliott Hughes43f7c872016-02-05 11:18:41 -0800104 pthread_attr_t a;
105 ASSERT_EQ(0, pthread_attr_init(&a));
106 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700107
108 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700109 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800110 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700111}
112
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900113TEST(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
Elliott Hughes7843d442013-08-22 11:37:32 -0700137TEST(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
Elliott Hughes0c401522013-10-18 16:21:54 -0700144#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700145 // 32-bit bionic has a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700146 ASSERT_EQ(-1, mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700147 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700148#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();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700154 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700155 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700156 ASSERT_EQ(0, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700157
158 setenv("TZ", "UTC", 1);
159 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700160 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700161 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700162 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700163#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800164}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800165
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700166TEST(time, mktime_EOVERFLOW) {
167 struct tm t;
168 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700169
170 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
171 t.tm_year = 2016 - 1900;
172
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700173 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
Elliott Hughes47126ed2016-09-06 13:25:53 -0700180 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700181 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700182
183 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700184 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
185 ASSERT_EQ(EOVERFLOW, errno);
186}
187
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700188TEST(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
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800210TEST(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
Elliott Hughes0a610d02016-07-29 14:04:17 -0700248TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700249 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700250 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
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700269TEST(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
Elliott Hughes3376c232018-02-13 23:14:12 -0800286TEST(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
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700303TEST(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
313TEST(time, strptime_P_p) {
314 setenv("TZ", "UTC", 1);
315
Elliott Hughes11678822019-03-27 08:56:49 -0700316 // For parsing, %P and %p are the same: case doesn't matter.
317
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700318 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};
Elliott Hughes11678822019-03-27 08:56:49 -0700323 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
324 EXPECT_EQ(0, tm.tm_hour);
325
326 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700327 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
328 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700329
330 tm = {.tm_hour = 12};
331 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
332 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700333}
334
335TEST(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
343TEST(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
353TEST(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
Elliott Hughesd065c042020-09-01 19:02:44 -0700366TEST(time, strptime_Z) {
367#if defined(__BIONIC__)
368 // glibc doesn't handle %Z at all.
369 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
370 // are in the global `tzname` (which correspond to the current $TZ).
371 struct tm tm;
372 setenv("TZ", "Europe/Berlin", 1);
373
374 // "GMT" always works.
375 tm = {};
376 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
377 EXPECT_STREQ("GMT", tm.tm_zone);
378 EXPECT_EQ(0, tm.tm_isdst);
379 EXPECT_EQ(0, tm.tm_gmtoff);
380
381 // As does "UTC".
382 tm = {};
383 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
384 EXPECT_STREQ("UTC", tm.tm_zone);
385 EXPECT_EQ(0, tm.tm_isdst);
386 EXPECT_EQ(0, tm.tm_gmtoff);
387
388 // Europe/Berlin is known as "CET" when there's no DST.
389 tm = {};
390 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
391 EXPECT_STREQ("CET", tm.tm_zone);
392 EXPECT_EQ(0, tm.tm_isdst);
393 EXPECT_EQ(3600, tm.tm_gmtoff);
394
395 // Europe/Berlin is known as "CEST" when there's no DST.
396 tm = {};
397 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
398 EXPECT_STREQ("CEST", tm.tm_zone);
399 EXPECT_EQ(1, tm.tm_isdst);
400 EXPECT_EQ(3600, tm.tm_gmtoff);
401
402 // And as long as we're in Europe/Berlin, those are the only time zone
403 // abbreviations that are recognized.
404 tm = {};
405 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
406#endif
407}
408
409TEST(time, strptime_z) {
410 struct tm tm;
411 setenv("TZ", "Europe/Berlin", 1);
412
413 // "UT" is what RFC822 called UTC.
414 tm = {};
415 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
416 EXPECT_STREQ("UTC", tm.tm_zone);
417 EXPECT_EQ(0, tm.tm_isdst);
418 EXPECT_EQ(0, tm.tm_gmtoff);
419 // "GMT" is RFC822's other name for UTC.
420 tm = {};
421 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
422 EXPECT_STREQ("UTC", tm.tm_zone);
423 EXPECT_EQ(0, tm.tm_isdst);
424 EXPECT_EQ(0, tm.tm_gmtoff);
425
426 // "Z" ("Zulu") is a synonym for UTC.
427 tm = {};
428 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
429 EXPECT_STREQ("UTC", tm.tm_zone);
430 EXPECT_EQ(0, tm.tm_isdst);
431 EXPECT_EQ(0, tm.tm_gmtoff);
432
433 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
434 tm = {};
435 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
436 EXPECT_STREQ("PST", tm.tm_zone);
437 EXPECT_EQ(0, tm.tm_isdst);
438 EXPECT_EQ(-28800, tm.tm_gmtoff);
439 tm = {};
440 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
441 EXPECT_STREQ("PDT", tm.tm_zone);
442 EXPECT_EQ(1, tm.tm_isdst);
443 EXPECT_EQ(-25200, tm.tm_gmtoff);
444
445 // +-hh
446 tm = {};
447 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
448 EXPECT_EQ(3600, tm.tm_gmtoff);
449 EXPECT_TRUE(tm.tm_zone == nullptr);
450 EXPECT_EQ(0, tm.tm_isdst);
451 // +-hhmm
452 tm = {};
453 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
454 EXPECT_EQ(5400, tm.tm_gmtoff);
455 EXPECT_TRUE(tm.tm_zone == nullptr);
456 EXPECT_EQ(0, tm.tm_isdst);
457 // +-hh:mm
458 tm = {};
459 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
460 EXPECT_EQ(5400, tm.tm_gmtoff);
461 EXPECT_TRUE(tm.tm_zone == nullptr);
462 EXPECT_EQ(0, tm.tm_isdst);
463}
464
Elliott Hughes4b558f52014-03-04 15:58:02 -0800465void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
466 itimerspec ts;
467 ts.it_value.tv_sec = value_s;
468 ts.it_value.tv_nsec = value_ns;
469 ts.it_interval.tv_sec = interval_s;
470 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700471 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800472}
473
474static void NoOpNotifyFunction(sigval_t) {
475}
476
477TEST(time, timer_create) {
478 sigevent_t se;
479 memset(&se, 0, sizeof(se));
480 se.sigev_notify = SIGEV_THREAD;
481 se.sigev_notify_function = NoOpNotifyFunction;
482 timer_t timer_id;
483 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
484
Elliott Hughes33697a02016-01-26 13:04:57 -0800485 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800486 ASSERT_NE(-1, pid) << strerror(errno);
487
488 if (pid == 0) {
489 // Timers are not inherited by the child.
490 ASSERT_EQ(-1, timer_delete(timer_id));
491 ASSERT_EQ(EINVAL, errno);
492 _exit(0);
493 }
494
Elliott Hughes33697a02016-01-26 13:04:57 -0800495 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800496
497 ASSERT_EQ(0, timer_delete(timer_id));
498}
499
Yabin Cui95f1ee22015-01-13 19:53:15 -0800500static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800501static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
502 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
503 ASSERT_EQ(SIGUSR1, signal_number);
504}
505
506TEST(time, timer_create_SIGEV_SIGNAL) {
507 sigevent_t se;
508 memset(&se, 0, sizeof(se));
509 se.sigev_notify = SIGEV_SIGNAL;
510 se.sigev_signo = SIGUSR1;
511
512 timer_t timer_id;
513 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
514
Yabin Cui95f1ee22015-01-13 19:53:15 -0800515 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800516 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
517
518 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
519
520 itimerspec ts;
521 ts.it_value.tv_sec = 0;
522 ts.it_value.tv_nsec = 1;
523 ts.it_interval.tv_sec = 0;
524 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700525 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800526
527 usleep(500000);
528 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
529}
530
531struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800532 private:
533 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800534 timer_t timer_id;
535 sigevent_t se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700536 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800537
Elliott Hughes4b558f52014-03-04 15:58:02 -0800538 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700539 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800540 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700541 timer_valid = true;
542 }
543
Yabin Cui95f1ee22015-01-13 19:53:15 -0800544 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700545 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800546 memset(&se, 0, sizeof(se));
547 se.sigev_notify = SIGEV_THREAD;
548 se.sigev_notify_function = fn;
549 se.sigev_value.sival_ptr = this;
550 Create();
551 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700552 void DeleteTimer() {
553 ASSERT_TRUE(timer_valid);
554 ASSERT_EQ(0, timer_delete(timer_id));
555 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800556 }
557
558 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700559 if (timer_valid) {
560 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800561 }
562 }
563
Yabin Cui95f1ee22015-01-13 19:53:15 -0800564 int Value() const {
565 return value;
566 }
567
Christopher Ferris62d84b12014-10-20 19:09:19 -0700568 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
569 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
570 }
571
572 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800573 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700574 time_t start = time(nullptr);
575 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700576 }
577 return current_value != value;
578 }
579
Elliott Hughes4b558f52014-03-04 15:58:02 -0800580 static void CountNotifyFunction(sigval_t value) {
581 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
582 ++cd->value;
583 }
584
585 static void CountAndDisarmNotifyFunction(sigval_t value) {
586 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
587 ++cd->value;
588
589 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700590 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800591 }
592};
593
594TEST(time, timer_settime_0) {
595 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800596 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800597
Yabin Cuibf572d92015-08-11 11:23:16 -0700598 counter.SetTime(0, 500000000, 1, 0);
599 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800600
601 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800602 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800603}
604
605TEST(time, timer_settime_repeats) {
606 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800607 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800608
Christopher Ferris62d84b12014-10-20 19:09:19 -0700609 counter.SetTime(0, 1, 0, 10);
610 ASSERT_TRUE(counter.ValueUpdated());
611 ASSERT_TRUE(counter.ValueUpdated());
612 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800613 counter.DeleteTimer();
614 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
615 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800616}
617
Yabin Cui95f1ee22015-01-13 19:53:15 -0800618static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800619static void timer_create_NULL_signal_handler(int signal_number) {
620 ++timer_create_NULL_signal_handler_invocation_count;
621 ASSERT_EQ(SIGALRM, signal_number);
622}
623
624TEST(time, timer_create_NULL) {
625 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
626 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700627 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800628
Yabin Cui95f1ee22015-01-13 19:53:15 -0800629 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800630 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
631
632 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
633
634 SetTime(timer_id, 0, 1, 0, 0);
635 usleep(500000);
636
637 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
638}
639
640TEST(time, timer_create_EINVAL) {
641 clockid_t invalid_clock = 16;
642
643 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
644 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700645 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800646 ASSERT_EQ(EINVAL, errno);
647
648 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
649 sigevent_t se;
650 memset(&se, 0, sizeof(se));
651 se.sigev_notify = SIGEV_THREAD;
652 se.sigev_notify_function = NoOpNotifyFunction;
653 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
654 ASSERT_EQ(EINVAL, errno);
655}
656
Elliott Hughes4b558f52014-03-04 15:58:02 -0800657TEST(time, timer_create_multiple) {
658 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800659 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800660 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800661
Yabin Cui95f1ee22015-01-13 19:53:15 -0800662 ASSERT_EQ(0, counter1.Value());
663 ASSERT_EQ(0, counter2.Value());
664 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800665
Yabin Cui410c1ad2015-06-18 16:19:02 -0700666 counter2.SetTime(0, 500000000, 0, 0);
667 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800668
Yabin Cui95f1ee22015-01-13 19:53:15 -0800669 EXPECT_EQ(0, counter1.Value());
670 EXPECT_EQ(1, counter2.Value());
671 EXPECT_EQ(0, counter3.Value());
672}
673
674// Test to verify that disarming a repeatable timer disables the callbacks.
675TEST(time, timer_disarm_terminates) {
676 Counter counter(Counter::CountNotifyFunction);
677 ASSERT_EQ(0, counter.Value());
678
679 counter.SetTime(0, 1, 0, 1);
680 ASSERT_TRUE(counter.ValueUpdated());
681 ASSERT_TRUE(counter.ValueUpdated());
682 ASSERT_TRUE(counter.ValueUpdated());
683
684 counter.SetTime(0, 0, 0, 0);
685 // Add a sleep as the kernel may have pending events when the timer is disarmed.
686 usleep(500000);
687 int value = counter.Value();
688 usleep(500000);
689
690 // Verify the counter has not been incremented.
691 ASSERT_EQ(value, counter.Value());
692}
693
694// Test to verify that deleting a repeatable timer disables the callbacks.
695TEST(time, timer_delete_terminates) {
696 Counter counter(Counter::CountNotifyFunction);
697 ASSERT_EQ(0, counter.Value());
698
699 counter.SetTime(0, 1, 0, 1);
700 ASSERT_TRUE(counter.ValueUpdated());
701 ASSERT_TRUE(counter.ValueUpdated());
702 ASSERT_TRUE(counter.ValueUpdated());
703
704 counter.DeleteTimer();
705 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
706 usleep(500000);
707 int value = counter.Value();
708 usleep(500000);
709
710 // Verify the counter has not been incremented.
711 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800712}
Christopher Ferris753ad772014-03-20 20:47:45 -0700713
714struct TimerDeleteData {
715 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800716 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700717 volatile bool complete;
718};
719
720static void TimerDeleteCallback(sigval_t value) {
721 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
722
Elliott Hughes11859d42017-02-13 17:59:29 -0800723 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700724 timer_delete(tdd->timer_id);
725 tdd->complete = true;
726}
727
728TEST(time, timer_delete_from_timer_thread) {
729 TimerDeleteData tdd;
730 sigevent_t se;
731
732 memset(&se, 0, sizeof(se));
733 se.sigev_notify = SIGEV_THREAD;
734 se.sigev_notify_function = TimerDeleteCallback;
735 se.sigev_value.sival_ptr = &tdd;
736
737 tdd.complete = false;
738 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
739
740 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800741 ts.it_value.tv_sec = 1;
742 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700743 ts.it_interval.tv_sec = 0;
744 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700745 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700746
Yi Kong32bc0fc2018-08-02 17:31:13 -0700747 time_t cur_time = time(nullptr);
748 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700749 ASSERT_TRUE(tdd.complete);
750
751#if defined(__BIONIC__)
752 // Since bionic timers are implemented by creating a thread to handle the
753 // callback, verify that the thread actually completes.
754 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800755 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
756 ASSERT_EQ(-1, kill(tdd.tid, 0));
757 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700758#endif
759}
Elliott Hughes625993d2014-07-15 16:53:13 -0700760
761TEST(time, clock_gettime) {
762 // Try to ensure that our vdso clock_gettime is working.
763 timespec ts1;
764 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
765 timespec ts2;
766 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
767
768 // What's the difference between the two?
769 ts2.tv_sec -= ts1.tv_sec;
770 ts2.tv_nsec -= ts1.tv_nsec;
771 if (ts2.tv_nsec < 0) {
772 --ts2.tv_sec;
Elliott Hughes04303f52014-09-18 16:11:59 -0700773 ts2.tv_nsec += NS_PER_S;
Elliott Hughes625993d2014-07-15 16:53:13 -0700774 }
775
Elliott Hughes8dff0bb2019-01-17 12:32:32 -0800776 // To try to avoid flakiness we'll accept answers within 10,000,000ns (0.01s).
Elliott Hughes625993d2014-07-15 16:53:13 -0700777 ASSERT_EQ(0, ts2.tv_sec);
Elliott Hughes8dff0bb2019-01-17 12:32:32 -0800778 ASSERT_LT(ts2.tv_nsec, 10'000'000);
Elliott Hughes625993d2014-07-15 16:53:13 -0700779}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700780
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700781TEST(time, clock_gettime_CLOCK_REALTIME) {
782 timespec ts;
783 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
784}
785
786TEST(time, clock_gettime_CLOCK_MONOTONIC) {
787 timespec ts;
788 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
789}
790
791TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
792 timespec ts;
793 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
794}
795
796TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
797 timespec ts;
798 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
799}
800
801TEST(time, clock_gettime_CLOCK_BOOTTIME) {
802 timespec ts;
803 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
804}
805
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800806TEST(time, clock_gettime_unknown) {
807 errno = 0;
808 timespec ts;
809 ASSERT_EQ(-1, clock_gettime(-1, &ts));
810 ASSERT_EQ(EINVAL, errno);
811}
812
813TEST(time, clock_getres_CLOCK_REALTIME) {
814 timespec ts;
815 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
816 ASSERT_EQ(1, ts.tv_nsec);
817 ASSERT_EQ(0, ts.tv_sec);
818}
819
820TEST(time, clock_getres_CLOCK_MONOTONIC) {
821 timespec ts;
822 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
823 ASSERT_EQ(1, ts.tv_nsec);
824 ASSERT_EQ(0, ts.tv_sec);
825}
826
827TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
828 timespec ts;
829 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
830}
831
832TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
833 timespec ts;
834 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
835}
836
837TEST(time, clock_getres_CLOCK_BOOTTIME) {
838 timespec ts;
839 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
840 ASSERT_EQ(1, ts.tv_nsec);
841 ASSERT_EQ(0, ts.tv_sec);
842}
843
844TEST(time, clock_getres_unknown) {
845 errno = 0;
846 timespec ts = { -1, -1 };
847 ASSERT_EQ(-1, clock_getres(-1, &ts));
848 ASSERT_EQ(EINVAL, errno);
849 ASSERT_EQ(-1, ts.tv_nsec);
850 ASSERT_EQ(-1, ts.tv_sec);
851}
852
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700853TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100854 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
855 static const clock_t N = 5;
856 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +0900857 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100858 for (size_t i = 0; i < N; ++i) {
859 sleep(1);
860 }
Haruki Hasegawa18160252014-10-13 00:50:47 +0900861 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100862 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +0900863}
864
Elliott Hughesb4413592017-11-29 18:17:06 -0800865static pid_t GetInvalidPid() {
866 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -0800867 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -0800868 fscanf(fp.get(), "%ld", &pid_max);
869 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -0800870}
871
Elliott Hughesb4413592017-11-29 18:17:06 -0800872TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800873 clockid_t clockid;
874 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -0800875 timespec ts;
876 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800877}
Yabin Cuid5c65272014-11-26 14:04:26 -0800878
Elliott Hughesb4413592017-11-29 18:17:06 -0800879TEST(time, clock_getcpuclockid_parent) {
880 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -0800881 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -0800882 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -0800883 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800884}
Yabin Cuid5c65272014-11-26 14:04:26 -0800885
Elliott Hughesb4413592017-11-29 18:17:06 -0800886TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800887 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
888 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800889 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -0800890 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800891 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
892 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
893 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
894 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
895 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
896 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -0800897 ASSERT_EQ(0, errno);
898}
899
Haruki Hasegawa18160252014-10-13 00:50:47 +0900900TEST(time, clock_settime) {
901 errno = 0;
902 timespec ts;
903 ASSERT_EQ(-1, clock_settime(-1, &ts));
904 ASSERT_EQ(EINVAL, errno);
905}
906
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700907TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900908 timespec in;
909 timespec out;
910 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700911}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700912
913TEST(time, clock_nanosleep_thread_cputime_id) {
914 timespec in;
915 in.tv_sec = 1;
916 in.tv_nsec = 0;
917 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
918}
Elliott Hughes12443702016-10-19 16:02:31 -0700919
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700920TEST(time, clock_nanosleep) {
921 auto t0 = std::chrono::steady_clock::now();
922 const timespec ts = {.tv_nsec = 5000000};
923 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
924 auto t1 = std::chrono::steady_clock::now();
925 ASSERT_GE(t1-t0, 5000000ns);
926}
927
928TEST(time, nanosleep) {
929 auto t0 = std::chrono::steady_clock::now();
930 const timespec ts = {.tv_nsec = 5000000};
931 ASSERT_EQ(0, nanosleep(&ts, nullptr));
932 auto t1 = std::chrono::steady_clock::now();
933 ASSERT_GE(t1-t0, 5000000ns);
934}
935
936TEST(time, nanosleep_EINVAL) {
937 timespec ts = {.tv_sec = -1};
938 errno = 0;
939 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
940 ASSERT_EQ(EINVAL, errno);
941}
942
Elliott Hughes12443702016-10-19 16:02:31 -0700943TEST(time, bug_31938693) {
944 // User-visible symptoms in N:
945 // http://b/31938693
946 // https://code.google.com/p/android/issues/detail?id=225132
947
948 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
949 // http://b/31848040
950
951 // This isn't a great test, because very few time zones were actually affected, and there's
952 // no real logic to which ones were affected: it was just a coincidence of the data that came
953 // after them in the tzdata file.
954
955 time_t t = 1475619727;
956 struct tm tm;
957
958 setenv("TZ", "America/Los_Angeles", 1);
959 tzset();
960 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
961 EXPECT_EQ(15, tm.tm_hour);
962
963 setenv("TZ", "Europe/London", 1);
964 tzset();
965 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
966 EXPECT_EQ(23, tm.tm_hour);
967
968 setenv("TZ", "America/Atka", 1);
969 tzset();
970 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
971 EXPECT_EQ(13, tm.tm_hour);
972
973 setenv("TZ", "Pacific/Apia", 1);
974 tzset();
975 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
976 EXPECT_EQ(12, tm.tm_hour);
977
978 setenv("TZ", "Pacific/Honolulu", 1);
979 tzset();
980 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
981 EXPECT_EQ(12, tm.tm_hour);
982
983 setenv("TZ", "Asia/Magadan", 1);
984 tzset();
985 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
986 EXPECT_EQ(9, tm.tm_hour);
987}
Elliott Hughesea877162017-01-11 14:34:16 -0800988
989TEST(time, bug_31339449) {
990 // POSIX says localtime acts as if it calls tzset.
991 // tzset does two things:
992 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
993 // 2. it sets the global `tzname`.
994 // POSIX says localtime_r need not set `tzname` (2).
995 // Q: should localtime_r set the time zone (1)?
996 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
997
998 // Pick a time, any time...
999 time_t t = 1475619727;
1000
1001 // Call tzset with a specific timezone.
1002 setenv("TZ", "America/Atka", 1);
1003 tzset();
1004
1005 // If we change the timezone and call localtime, localtime should use the new timezone.
1006 setenv("TZ", "America/Los_Angeles", 1);
1007 struct tm* tm_p = localtime(&t);
1008 EXPECT_EQ(15, tm_p->tm_hour);
1009
1010 // Reset the timezone back.
1011 setenv("TZ", "America/Atka", 1);
1012 tzset();
1013
1014#if defined(__BIONIC__)
1015 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1016 setenv("TZ", "America/Los_Angeles", 1);
1017 struct tm tm = {};
1018 localtime_r(&t, &tm);
1019 EXPECT_EQ(15, tm.tm_hour);
1020#else
1021 // The BSDs agree with us, but glibc gets this wrong.
1022#endif
1023}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001024
1025TEST(time, asctime) {
1026 const struct tm tm = {};
1027 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1028}
1029
1030TEST(time, asctime_r) {
1031 const struct tm tm = {};
1032 char buf[256];
1033 ASSERT_EQ(buf, asctime_r(&tm, buf));
1034 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1035}
1036
1037TEST(time, ctime) {
1038 setenv("TZ", "UTC", 1);
1039 const time_t t = 0;
1040 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1041}
1042
1043TEST(time, ctime_r) {
1044 setenv("TZ", "UTC", 1);
1045 const time_t t = 0;
1046 char buf[256];
1047 ASSERT_EQ(buf, ctime_r(&t, buf));
1048 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1049}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001050
1051// https://issuetracker.google.com/37128336
1052TEST(time, strftime_strptime_s) {
1053 char buf[32];
1054 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1055
1056 setenv("TZ", "America/Los_Angeles", 1);
1057 strftime(buf, sizeof(buf), "<%s>", &tm0);
1058 EXPECT_STREQ("<378720000>", buf);
1059
1060 setenv("TZ", "UTC", 1);
1061 strftime(buf, sizeof(buf), "<%s>", &tm0);
1062 EXPECT_STREQ("<378691200>", buf);
1063
1064 struct tm tm;
1065
1066 setenv("TZ", "America/Los_Angeles", 1);
1067 tzset();
1068 memset(&tm, 0xff, sizeof(tm));
1069 char* p = strptime("378720000x", "%s", &tm);
1070 ASSERT_EQ('x', *p);
1071 EXPECT_EQ(0, tm.tm_sec);
1072 EXPECT_EQ(0, tm.tm_min);
1073 EXPECT_EQ(0, tm.tm_hour);
1074 EXPECT_EQ(1, tm.tm_mday);
1075 EXPECT_EQ(0, tm.tm_mon);
1076 EXPECT_EQ(82, tm.tm_year);
1077 EXPECT_EQ(5, tm.tm_wday);
1078 EXPECT_EQ(0, tm.tm_yday);
1079 EXPECT_EQ(0, tm.tm_isdst);
1080
1081 setenv("TZ", "UTC", 1);
1082 tzset();
1083 memset(&tm, 0xff, sizeof(tm));
1084 p = strptime("378691200x", "%s", &tm);
1085 ASSERT_EQ('x', *p);
1086 EXPECT_EQ(0, tm.tm_sec);
1087 EXPECT_EQ(0, tm.tm_min);
1088 EXPECT_EQ(0, tm.tm_hour);
1089 EXPECT_EQ(1, tm.tm_mday);
1090 EXPECT_EQ(0, tm.tm_mon);
1091 EXPECT_EQ(82, tm.tm_year);
1092 EXPECT_EQ(5, tm.tm_wday);
1093 EXPECT_EQ(0, tm.tm_yday);
1094 EXPECT_EQ(0, tm.tm_isdst);
1095}
1096
1097TEST(time, strptime_s_nothing) {
1098 struct tm tm;
1099 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1100}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001101
1102TEST(time, timespec_get) {
1103#if __BIONIC__
1104 timespec ts = {};
1105 ASSERT_EQ(0, timespec_get(&ts, 123));
1106 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1107#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001108 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001109#endif
1110}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001111
1112TEST(time, difftime) {
1113 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001114 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001115}