blob: d86550d0e973f59b588f9134b9f7e360bb589172 [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>
Colin Cross4c5595c2021-08-16 15:51:59 -070023#include <sys/cdefs.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070024#include <sys/syscall.h>
Brian Carlstrombe1d91d2014-03-08 15:05:26 -080025#include <sys/types.h>
26#include <sys/wait.h>
Yabin Cuid5c65272014-11-26 14:04:26 -080027#include <unistd.h>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070028
Yabin Cui95f1ee22015-01-13 19:53:15 -080029#include <atomic>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070030#include <chrono>
Elliott Hughese0175ca2013-03-14 14:38:08 -070031
Elliott Hughes71ba5892018-02-07 12:44:45 -080032#include "SignalUtils.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080033#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070034
Elliott Hughesca3f8e42019-10-28 15:59:38 -070035using namespace std::chrono_literals;
36
Mark Salyzyn0b846e82017-12-20 08:56:18 -080037TEST(time, time) {
38 // Acquire time
39 time_t p1, t1 = time(&p1);
40 // valid?
41 ASSERT_NE(static_cast<time_t>(0), t1);
42 ASSERT_NE(static_cast<time_t>(-1), t1);
43 ASSERT_EQ(p1, t1);
44
45 // Acquire time one+ second later
46 usleep(1010000);
47 time_t p2, t2 = time(&p2);
48 // valid?
49 ASSERT_NE(static_cast<time_t>(0), t2);
50 ASSERT_NE(static_cast<time_t>(-1), t2);
51 ASSERT_EQ(p2, t2);
52
53 // Expect time progression
54 ASSERT_LT(p1, p2);
55 ASSERT_LE(t2 - t1, static_cast<time_t>(2));
56
57 // Expect nullptr call to produce same results
58 ASSERT_LE(t2, time(nullptr));
59 ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1));
60}
61
Elliott Hughesee178bf2013-07-12 11:25:20 -070062TEST(time, gmtime) {
63 time_t t = 0;
64 tm* broken_down = gmtime(&t);
Yi Kong32bc0fc2018-08-02 17:31:13 -070065 ASSERT_TRUE(broken_down != nullptr);
Elliott Hughesee178bf2013-07-12 11:25:20 -070066 ASSERT_EQ(0, broken_down->tm_sec);
67 ASSERT_EQ(0, broken_down->tm_min);
68 ASSERT_EQ(0, broken_down->tm_hour);
69 ASSERT_EQ(1, broken_down->tm_mday);
70 ASSERT_EQ(0, broken_down->tm_mon);
71 ASSERT_EQ(1970, broken_down->tm_year + 1900);
72}
Elliott Hughes7843d442013-08-22 11:37:32 -070073
Elliott Hughes5a29d542017-12-07 16:05:57 -080074TEST(time, gmtime_r) {
75 struct tm tm = {};
76 time_t t = 0;
77 struct tm* broken_down = gmtime_r(&t, &tm);
78 ASSERT_EQ(broken_down, &tm);
79 ASSERT_EQ(0, broken_down->tm_sec);
80 ASSERT_EQ(0, broken_down->tm_min);
81 ASSERT_EQ(0, broken_down->tm_hour);
82 ASSERT_EQ(1, broken_down->tm_mday);
83 ASSERT_EQ(0, broken_down->tm_mon);
84 ASSERT_EQ(1970, broken_down->tm_year + 1900);
85}
86
Almaz Mingaleevda75bb62022-06-29 15:47:37 +000087TEST(time, mktime_TZ_as_UTC_and_offset) {
88 struct tm tm = {.tm_year = 70, .tm_mon = 0, .tm_mday = 1};
89
90 // This TZ value is not a valid Olson ID and is not present in tzdata file,
91 // but is a valid TZ string according to POSIX standard.
92 setenv("TZ", "UTC+08:00:00", 1);
93 tzset();
94 ASSERT_EQ(static_cast<time_t>(8 * 60 * 60), mktime(&tm));
95}
96
Elliott Hughes329103d2014-04-25 16:55:04 -070097static void* gmtime_no_stack_overflow_14313703_fn(void*) {
98 const char* original_tz = getenv("TZ");
99 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
100 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
101 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700102 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -0700103 setenv("TZ", original_tz, 1);
104 }
105 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700106 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -0700107}
108
109TEST(time, gmtime_no_stack_overflow_14313703) {
110 // Is it safe to call tzload on a thread with a small stack?
111 // http://b/14313703
112 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -0800113 pthread_attr_t a;
114 ASSERT_EQ(0, pthread_attr_init(&a));
115 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700116
117 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700118 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800119 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700120}
121
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900122TEST(time, mktime_empty_TZ) {
123 // tzcode used to have a bug where it didn't reinitialize some internal state.
124
125 // Choose a time where DST is set.
126 struct tm t;
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", "America/Los_Angeles", 1);
133 tzset();
134 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
135
136 memset(&t, 0, sizeof(tm));
137 t.tm_year = 1980 - 1900;
138 t.tm_mon = 6;
139 t.tm_mday = 2;
140
141 setenv("TZ", "", 1); // Implies UTC.
142 tzset();
143 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
144}
145
Elliott Hughes7843d442013-08-22 11:37:32 -0700146TEST(time, mktime_10310929) {
147 struct tm t;
148 memset(&t, 0, sizeof(tm));
149 t.tm_year = 200;
150 t.tm_mon = 2;
151 t.tm_mday = 10;
152
Elliott Hughes0c401522013-10-18 16:21:54 -0700153#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700154 // 32-bit bionic has a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700155 ASSERT_EQ(-1, mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700156 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700157#else
158 // Everyone else should be using a signed 64-bit time_t.
159 ASSERT_GE(sizeof(time_t) * 8, 64U);
160
161 setenv("TZ", "America/Los_Angeles", 1);
162 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700163 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700164 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700165 ASSERT_EQ(0, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700166
167 setenv("TZ", "UTC", 1);
168 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700169 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700170 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700171 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700172#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800173}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800174
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700175TEST(time, mktime_EOVERFLOW) {
176 struct tm t;
177 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700178
179 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
180 t.tm_year = 2016 - 1900;
181
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700182 t.tm_mon = 2;
183 t.tm_mday = 10;
184
185 errno = 0;
186 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
187 ASSERT_EQ(0, errno);
188
Elliott Hughes47126ed2016-09-06 13:25:53 -0700189 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700190 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700191
192 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700193 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
194 ASSERT_EQ(EOVERFLOW, errno);
195}
196
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000197TEST(time, mktime_invalid_tm_TZ_combination) {
198 setenv("TZ", "UTC", 1);
199
200 struct tm t;
201 memset(&t, 0, sizeof(tm));
202 t.tm_year = 2022 - 1900;
203 t.tm_mon = 11;
204 t.tm_mday = 31;
205 // UTC does not observe DST
206 t.tm_isdst = 1;
207
208 errno = 0;
209
210 EXPECT_EQ(static_cast<time_t>(-1), mktime(&t));
211 // mktime sets errno to EOVERFLOW if result is unrepresentable.
212 EXPECT_EQ(EOVERFLOW, errno);
213}
214
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700215TEST(time, strftime) {
216 setenv("TZ", "UTC", 1);
217
218 struct tm t;
219 memset(&t, 0, sizeof(tm));
220 t.tm_year = 200;
221 t.tm_mon = 2;
222 t.tm_mday = 10;
223
224 char buf[64];
225
226 // Seconds since the epoch.
227#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
228 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
229 EXPECT_STREQ("4108320000", buf);
230#endif
231
232 // Date and time as text.
233 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
234 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
235}
236
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000237TEST(time, strftime_second_before_epoch) {
238 setenv("TZ", "UTC", 1);
239
240 struct tm t;
241 memset(&t, 0, sizeof(tm));
242 t.tm_year = 1969 - 1900;
243 t.tm_mon = 11;
244 t.tm_mday = 31;
245 t.tm_hour = 23;
246 t.tm_min = 59;
247 t.tm_sec = 59;
248
249 char buf[64];
250
251 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
252 EXPECT_STREQ("-1", buf);
253}
254
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800255TEST(time, strftime_null_tm_zone) {
256 // Netflix on Nexus Player wouldn't start (http://b/25170306).
257 struct tm t;
258 memset(&t, 0, sizeof(tm));
259
260 char buf[64];
261
262 setenv("TZ", "America/Los_Angeles", 1);
263 tzset();
264
265 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
266 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
267 EXPECT_STREQ("<PST>", buf);
268
269#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
270 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
271 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
272 EXPECT_STREQ("<PDT>", buf);
273
274 t.tm_isdst = -123; // "and negative if the information is not available".
275 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
276 EXPECT_STREQ("<>", buf);
277#endif
278
279 setenv("TZ", "UTC", 1);
280 tzset();
281
282 t.tm_isdst = 0;
283 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
284 EXPECT_STREQ("<UTC>", buf);
285
286#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
287 t.tm_isdst = 1; // UTC has no DST.
288 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
289 EXPECT_STREQ("<>", buf);
290#endif
291}
292
Elliott Hughes0a610d02016-07-29 14:04:17 -0700293TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700294 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700295 locale_t old_locale = uselocale(cloc);
296
297 setenv("TZ", "UTC", 1);
298
299 struct tm t;
300 memset(&t, 0, sizeof(tm));
301 t.tm_year = 200;
302 t.tm_mon = 2;
303 t.tm_mday = 10;
304
305 // Date and time as text.
306 char buf[64];
307 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
308 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
309
310 uselocale(old_locale);
311 freelocale(cloc);
312}
313
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700314TEST(time, strptime) {
315 setenv("TZ", "UTC", 1);
316
317 struct tm t;
318 char buf[64];
319
320 memset(&t, 0, sizeof(t));
321 strptime("11:14", "%R", &t);
322 strftime(buf, sizeof(buf), "%H:%M", &t);
323 EXPECT_STREQ("11:14", buf);
324
325 memset(&t, 0, sizeof(t));
326 strptime("09:41:53", "%T", &t);
327 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
328 EXPECT_STREQ("09:41:53", buf);
329}
330
Elliott Hughes3376c232018-02-13 23:14:12 -0800331TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700332#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800333 setenv("TZ", "UTC", 1);
334
335 struct tm t;
336 char buf[64];
337
338 memset(&t, 0, sizeof(t));
339 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
340 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
341 EXPECT_STREQ("11:14", buf);
342
343 memset(&t, 0, sizeof(t));
344 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
345 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
346 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700347#else
348 GTEST_SKIP() << "musl doesn't support strptime_l";
349#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800350}
351
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700352TEST(time, strptime_F) {
353 setenv("TZ", "UTC", 1);
354
355 struct tm tm = {};
356 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
357 EXPECT_EQ(119, tm.tm_year);
358 EXPECT_EQ(2, tm.tm_mon);
359 EXPECT_EQ(26, tm.tm_mday);
360}
361
362TEST(time, strptime_P_p) {
363 setenv("TZ", "UTC", 1);
364
Elliott Hughes11678822019-03-27 08:56:49 -0700365 // For parsing, %P and %p are the same: case doesn't matter.
366
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700367 struct tm tm = {.tm_hour = 12};
368 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
369 EXPECT_EQ(0, tm.tm_hour);
370
371 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700372 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
373 EXPECT_EQ(0, tm.tm_hour);
374
375 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700376 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
377 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700378
379 tm = {.tm_hour = 12};
380 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
381 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700382}
383
384TEST(time, strptime_u) {
385 setenv("TZ", "UTC", 1);
386
387 struct tm tm = {};
388 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
389 EXPECT_EQ(2, tm.tm_wday);
390}
391
392TEST(time, strptime_v) {
393 setenv("TZ", "UTC", 1);
394
395 struct tm tm = {};
396 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
397 EXPECT_EQ(80, tm.tm_year);
398 EXPECT_EQ(2, tm.tm_mon);
399 EXPECT_EQ(26, tm.tm_mday);
400}
401
402TEST(time, strptime_V_G_g) {
403 setenv("TZ", "UTC", 1);
404
405 // %V (ISO-8601 week number), %G (year of week number, without century), and
406 // %g (year of week number) have no effect when parsed, and are supported
407 // solely so that it's possible for strptime(3) to parse everything that
408 // strftime(3) can output.
409 struct tm tm = {};
410 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
411 struct tm zero = {};
412 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
413}
414
Elliott Hughesd065c042020-09-01 19:02:44 -0700415TEST(time, strptime_Z) {
416#if defined(__BIONIC__)
417 // glibc doesn't handle %Z at all.
418 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
419 // are in the global `tzname` (which correspond to the current $TZ).
420 struct tm tm;
421 setenv("TZ", "Europe/Berlin", 1);
422
423 // "GMT" always works.
424 tm = {};
425 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
426 EXPECT_STREQ("GMT", tm.tm_zone);
427 EXPECT_EQ(0, tm.tm_isdst);
428 EXPECT_EQ(0, tm.tm_gmtoff);
429
430 // As does "UTC".
431 tm = {};
432 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
433 EXPECT_STREQ("UTC", tm.tm_zone);
434 EXPECT_EQ(0, tm.tm_isdst);
435 EXPECT_EQ(0, tm.tm_gmtoff);
436
437 // Europe/Berlin is known as "CET" when there's no DST.
438 tm = {};
439 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
440 EXPECT_STREQ("CET", tm.tm_zone);
441 EXPECT_EQ(0, tm.tm_isdst);
442 EXPECT_EQ(3600, tm.tm_gmtoff);
443
444 // Europe/Berlin is known as "CEST" when there's no DST.
445 tm = {};
446 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
447 EXPECT_STREQ("CEST", tm.tm_zone);
448 EXPECT_EQ(1, tm.tm_isdst);
449 EXPECT_EQ(3600, tm.tm_gmtoff);
450
451 // And as long as we're in Europe/Berlin, those are the only time zone
452 // abbreviations that are recognized.
453 tm = {};
454 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
455#endif
456}
457
458TEST(time, strptime_z) {
459 struct tm tm;
460 setenv("TZ", "Europe/Berlin", 1);
461
462 // "UT" is what RFC822 called UTC.
463 tm = {};
464 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
465 EXPECT_STREQ("UTC", tm.tm_zone);
466 EXPECT_EQ(0, tm.tm_isdst);
467 EXPECT_EQ(0, tm.tm_gmtoff);
468 // "GMT" is RFC822's other name for UTC.
469 tm = {};
470 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
471 EXPECT_STREQ("UTC", tm.tm_zone);
472 EXPECT_EQ(0, tm.tm_isdst);
473 EXPECT_EQ(0, tm.tm_gmtoff);
474
475 // "Z" ("Zulu") is a synonym for UTC.
476 tm = {};
477 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
478 EXPECT_STREQ("UTC", tm.tm_zone);
479 EXPECT_EQ(0, tm.tm_isdst);
480 EXPECT_EQ(0, tm.tm_gmtoff);
481
482 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
483 tm = {};
484 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
485 EXPECT_STREQ("PST", tm.tm_zone);
486 EXPECT_EQ(0, tm.tm_isdst);
487 EXPECT_EQ(-28800, tm.tm_gmtoff);
488 tm = {};
489 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
490 EXPECT_STREQ("PDT", tm.tm_zone);
491 EXPECT_EQ(1, tm.tm_isdst);
492 EXPECT_EQ(-25200, tm.tm_gmtoff);
493
494 // +-hh
495 tm = {};
496 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
497 EXPECT_EQ(3600, tm.tm_gmtoff);
498 EXPECT_TRUE(tm.tm_zone == nullptr);
499 EXPECT_EQ(0, tm.tm_isdst);
500 // +-hhmm
501 tm = {};
502 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
503 EXPECT_EQ(5400, tm.tm_gmtoff);
504 EXPECT_TRUE(tm.tm_zone == nullptr);
505 EXPECT_EQ(0, tm.tm_isdst);
506 // +-hh:mm
507 tm = {};
508 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
509 EXPECT_EQ(5400, tm.tm_gmtoff);
510 EXPECT_TRUE(tm.tm_zone == nullptr);
511 EXPECT_EQ(0, tm.tm_isdst);
512}
513
Elliott Hughes4b558f52014-03-04 15:58:02 -0800514void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
515 itimerspec ts;
516 ts.it_value.tv_sec = value_s;
517 ts.it_value.tv_nsec = value_ns;
518 ts.it_interval.tv_sec = interval_s;
519 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700520 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800521}
522
Colin Cross7da20342021-07-28 11:18:11 -0700523static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800524}
525
526TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700527 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800528 memset(&se, 0, sizeof(se));
529 se.sigev_notify = SIGEV_THREAD;
530 se.sigev_notify_function = NoOpNotifyFunction;
531 timer_t timer_id;
532 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
533
Elliott Hughes33697a02016-01-26 13:04:57 -0800534 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800535 ASSERT_NE(-1, pid) << strerror(errno);
536
537 if (pid == 0) {
538 // Timers are not inherited by the child.
539 ASSERT_EQ(-1, timer_delete(timer_id));
540 ASSERT_EQ(EINVAL, errno);
541 _exit(0);
542 }
543
Elliott Hughes33697a02016-01-26 13:04:57 -0800544 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800545
546 ASSERT_EQ(0, timer_delete(timer_id));
547}
548
Yabin Cui95f1ee22015-01-13 19:53:15 -0800549static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800550static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
551 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
552 ASSERT_EQ(SIGUSR1, signal_number);
553}
554
555TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700556 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800557 memset(&se, 0, sizeof(se));
558 se.sigev_notify = SIGEV_SIGNAL;
559 se.sigev_signo = SIGUSR1;
560
561 timer_t timer_id;
562 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
563
Yabin Cui95f1ee22015-01-13 19:53:15 -0800564 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800565 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
566
567 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
568
569 itimerspec ts;
570 ts.it_value.tv_sec = 0;
571 ts.it_value.tv_nsec = 1;
572 ts.it_interval.tv_sec = 0;
573 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700574 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800575
576 usleep(500000);
577 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
578}
579
580struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800581 private:
582 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800583 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700584 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700585 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800586
Elliott Hughes4b558f52014-03-04 15:58:02 -0800587 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700588 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800589 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700590 timer_valid = true;
591 }
592
Yabin Cui95f1ee22015-01-13 19:53:15 -0800593 public:
Colin Cross7da20342021-07-28 11:18:11 -0700594 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800595 memset(&se, 0, sizeof(se));
596 se.sigev_notify = SIGEV_THREAD;
597 se.sigev_notify_function = fn;
598 se.sigev_value.sival_ptr = this;
599 Create();
600 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700601 void DeleteTimer() {
602 ASSERT_TRUE(timer_valid);
603 ASSERT_EQ(0, timer_delete(timer_id));
604 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800605 }
606
607 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700608 if (timer_valid) {
609 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800610 }
611 }
612
Yabin Cui95f1ee22015-01-13 19:53:15 -0800613 int Value() const {
614 return value;
615 }
616
Christopher Ferris62d84b12014-10-20 19:09:19 -0700617 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
618 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
619 }
620
621 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800622 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700623 time_t start = time(nullptr);
624 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700625 }
626 return current_value != value;
627 }
628
Colin Cross7da20342021-07-28 11:18:11 -0700629 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800630 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
631 ++cd->value;
632 }
633
Colin Cross7da20342021-07-28 11:18:11 -0700634 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800635 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
636 ++cd->value;
637
638 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700639 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800640 }
641};
642
643TEST(time, timer_settime_0) {
644 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800645 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800646
Yabin Cuibf572d92015-08-11 11:23:16 -0700647 counter.SetTime(0, 500000000, 1, 0);
648 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800649
650 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800651 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800652}
653
654TEST(time, timer_settime_repeats) {
655 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800656 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800657
Christopher Ferris62d84b12014-10-20 19:09:19 -0700658 counter.SetTime(0, 1, 0, 10);
659 ASSERT_TRUE(counter.ValueUpdated());
660 ASSERT_TRUE(counter.ValueUpdated());
661 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800662 counter.DeleteTimer();
663 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
664 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800665}
666
Yabin Cui95f1ee22015-01-13 19:53:15 -0800667static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800668static void timer_create_NULL_signal_handler(int signal_number) {
669 ++timer_create_NULL_signal_handler_invocation_count;
670 ASSERT_EQ(SIGALRM, signal_number);
671}
672
673TEST(time, timer_create_NULL) {
674 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
675 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700676 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800677
Yabin Cui95f1ee22015-01-13 19:53:15 -0800678 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800679 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
680
681 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
682
683 SetTime(timer_id, 0, 1, 0, 0);
684 usleep(500000);
685
686 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
687}
688
689TEST(time, timer_create_EINVAL) {
690 clockid_t invalid_clock = 16;
691
692 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
693 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700694 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800695 ASSERT_EQ(EINVAL, errno);
696
697 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700698 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800699 memset(&se, 0, sizeof(se));
700 se.sigev_notify = SIGEV_THREAD;
701 se.sigev_notify_function = NoOpNotifyFunction;
702 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
703 ASSERT_EQ(EINVAL, errno);
704}
705
Elliott Hughes4b558f52014-03-04 15:58:02 -0800706TEST(time, timer_create_multiple) {
707 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800708 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800709 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800710
Yabin Cui95f1ee22015-01-13 19:53:15 -0800711 ASSERT_EQ(0, counter1.Value());
712 ASSERT_EQ(0, counter2.Value());
713 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800714
Yabin Cui410c1ad2015-06-18 16:19:02 -0700715 counter2.SetTime(0, 500000000, 0, 0);
716 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800717
Yabin Cui95f1ee22015-01-13 19:53:15 -0800718 EXPECT_EQ(0, counter1.Value());
719 EXPECT_EQ(1, counter2.Value());
720 EXPECT_EQ(0, counter3.Value());
721}
722
723// Test to verify that disarming a repeatable timer disables the callbacks.
724TEST(time, timer_disarm_terminates) {
725 Counter counter(Counter::CountNotifyFunction);
726 ASSERT_EQ(0, counter.Value());
727
728 counter.SetTime(0, 1, 0, 1);
729 ASSERT_TRUE(counter.ValueUpdated());
730 ASSERT_TRUE(counter.ValueUpdated());
731 ASSERT_TRUE(counter.ValueUpdated());
732
733 counter.SetTime(0, 0, 0, 0);
734 // Add a sleep as the kernel may have pending events when the timer is disarmed.
735 usleep(500000);
736 int value = counter.Value();
737 usleep(500000);
738
739 // Verify the counter has not been incremented.
740 ASSERT_EQ(value, counter.Value());
741}
742
743// Test to verify that deleting a repeatable timer disables the callbacks.
744TEST(time, timer_delete_terminates) {
745 Counter counter(Counter::CountNotifyFunction);
746 ASSERT_EQ(0, counter.Value());
747
748 counter.SetTime(0, 1, 0, 1);
749 ASSERT_TRUE(counter.ValueUpdated());
750 ASSERT_TRUE(counter.ValueUpdated());
751 ASSERT_TRUE(counter.ValueUpdated());
752
753 counter.DeleteTimer();
754 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
755 usleep(500000);
756 int value = counter.Value();
757 usleep(500000);
758
759 // Verify the counter has not been incremented.
760 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800761}
Christopher Ferris753ad772014-03-20 20:47:45 -0700762
763struct TimerDeleteData {
764 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800765 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700766 volatile bool complete;
767};
768
Colin Cross7da20342021-07-28 11:18:11 -0700769static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700770 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
771
Elliott Hughes11859d42017-02-13 17:59:29 -0800772 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700773 timer_delete(tdd->timer_id);
774 tdd->complete = true;
775}
776
777TEST(time, timer_delete_from_timer_thread) {
778 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700779 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700780
781 memset(&se, 0, sizeof(se));
782 se.sigev_notify = SIGEV_THREAD;
783 se.sigev_notify_function = TimerDeleteCallback;
784 se.sigev_value.sival_ptr = &tdd;
785
786 tdd.complete = false;
787 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
788
789 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800790 ts.it_value.tv_sec = 1;
791 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700792 ts.it_interval.tv_sec = 0;
793 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700794 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700795
Yi Kong32bc0fc2018-08-02 17:31:13 -0700796 time_t cur_time = time(nullptr);
797 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700798 ASSERT_TRUE(tdd.complete);
799
800#if defined(__BIONIC__)
801 // Since bionic timers are implemented by creating a thread to handle the
802 // callback, verify that the thread actually completes.
803 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800804 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
805 ASSERT_EQ(-1, kill(tdd.tid, 0));
806 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700807#endif
808}
Elliott Hughes625993d2014-07-15 16:53:13 -0700809
Colin Cross35d469b2021-08-16 15:26:28 -0700810// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
811#if !defined(__NR_clock_gettime)
812#define __NR_clock_gettime __NR_clock_gettime32
813#endif
814
Elliott Hughes625993d2014-07-15 16:53:13 -0700815TEST(time, clock_gettime) {
816 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100817 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700818 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700819 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100820 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
821 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
822 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700823
Giuliano Procida096f5952021-04-08 10:51:58 +0100824 // Check we have a nice monotonic timestamp sandwich.
825 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
826 if (ts0.tv_sec == ts1.tv_sec) {
827 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700828 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100829 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
830 if (ts1.tv_sec == ts2.tv_sec) {
831 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
832 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700833}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700834
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700835TEST(time, clock_gettime_CLOCK_REALTIME) {
836 timespec ts;
837 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
838}
839
840TEST(time, clock_gettime_CLOCK_MONOTONIC) {
841 timespec ts;
842 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
843}
844
845TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
846 timespec ts;
847 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
848}
849
850TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
851 timespec ts;
852 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
853}
854
855TEST(time, clock_gettime_CLOCK_BOOTTIME) {
856 timespec ts;
857 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
858}
859
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800860TEST(time, clock_gettime_unknown) {
861 errno = 0;
862 timespec ts;
863 ASSERT_EQ(-1, clock_gettime(-1, &ts));
864 ASSERT_EQ(EINVAL, errno);
865}
866
867TEST(time, clock_getres_CLOCK_REALTIME) {
868 timespec ts;
869 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
870 ASSERT_EQ(1, ts.tv_nsec);
871 ASSERT_EQ(0, ts.tv_sec);
872}
873
874TEST(time, clock_getres_CLOCK_MONOTONIC) {
875 timespec ts;
876 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
877 ASSERT_EQ(1, ts.tv_nsec);
878 ASSERT_EQ(0, ts.tv_sec);
879}
880
881TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
882 timespec ts;
883 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
884}
885
886TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
887 timespec ts;
888 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
889}
890
891TEST(time, clock_getres_CLOCK_BOOTTIME) {
892 timespec ts;
893 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
894 ASSERT_EQ(1, ts.tv_nsec);
895 ASSERT_EQ(0, ts.tv_sec);
896}
897
898TEST(time, clock_getres_unknown) {
899 errno = 0;
900 timespec ts = { -1, -1 };
901 ASSERT_EQ(-1, clock_getres(-1, &ts));
902 ASSERT_EQ(EINVAL, errno);
903 ASSERT_EQ(-1, ts.tv_nsec);
904 ASSERT_EQ(-1, ts.tv_sec);
905}
906
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700907TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100908 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
909 static const clock_t N = 5;
910 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +0900911 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100912 for (size_t i = 0; i < N; ++i) {
913 sleep(1);
914 }
Haruki Hasegawa18160252014-10-13 00:50:47 +0900915 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100916 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +0900917}
918
Elliott Hughesb4413592017-11-29 18:17:06 -0800919static pid_t GetInvalidPid() {
920 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -0800921 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -0800922 fscanf(fp.get(), "%ld", &pid_max);
923 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -0800924}
925
Elliott Hughesb4413592017-11-29 18:17:06 -0800926TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800927 clockid_t clockid;
928 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -0800929 timespec ts;
930 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800931}
Yabin Cuid5c65272014-11-26 14:04:26 -0800932
Elliott Hughesb4413592017-11-29 18:17:06 -0800933TEST(time, clock_getcpuclockid_parent) {
934 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -0800935 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -0800936 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -0800937 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800938}
Yabin Cuid5c65272014-11-26 14:04:26 -0800939
Elliott Hughesb4413592017-11-29 18:17:06 -0800940TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800941 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
942 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800943 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -0800944 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800945 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
946 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
947 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
948 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
949 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
950 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -0800951 ASSERT_EQ(0, errno);
952}
953
Haruki Hasegawa18160252014-10-13 00:50:47 +0900954TEST(time, clock_settime) {
955 errno = 0;
956 timespec ts;
957 ASSERT_EQ(-1, clock_settime(-1, &ts));
958 ASSERT_EQ(EINVAL, errno);
959}
960
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700961TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900962 timespec in;
963 timespec out;
964 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700965}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700966
967TEST(time, clock_nanosleep_thread_cputime_id) {
968 timespec in;
969 in.tv_sec = 1;
970 in.tv_nsec = 0;
971 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
972}
Elliott Hughes12443702016-10-19 16:02:31 -0700973
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700974TEST(time, clock_nanosleep) {
975 auto t0 = std::chrono::steady_clock::now();
976 const timespec ts = {.tv_nsec = 5000000};
977 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
978 auto t1 = std::chrono::steady_clock::now();
979 ASSERT_GE(t1-t0, 5000000ns);
980}
981
982TEST(time, nanosleep) {
983 auto t0 = std::chrono::steady_clock::now();
984 const timespec ts = {.tv_nsec = 5000000};
985 ASSERT_EQ(0, nanosleep(&ts, nullptr));
986 auto t1 = std::chrono::steady_clock::now();
987 ASSERT_GE(t1-t0, 5000000ns);
988}
989
990TEST(time, nanosleep_EINVAL) {
991 timespec ts = {.tv_sec = -1};
992 errno = 0;
993 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
994 ASSERT_EQ(EINVAL, errno);
995}
996
Elliott Hughes12443702016-10-19 16:02:31 -0700997TEST(time, bug_31938693) {
998 // User-visible symptoms in N:
999 // http://b/31938693
1000 // https://code.google.com/p/android/issues/detail?id=225132
1001
1002 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
1003 // http://b/31848040
1004
1005 // This isn't a great test, because very few time zones were actually affected, and there's
1006 // no real logic to which ones were affected: it was just a coincidence of the data that came
1007 // after them in the tzdata file.
1008
1009 time_t t = 1475619727;
1010 struct tm tm;
1011
1012 setenv("TZ", "America/Los_Angeles", 1);
1013 tzset();
1014 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1015 EXPECT_EQ(15, tm.tm_hour);
1016
1017 setenv("TZ", "Europe/London", 1);
1018 tzset();
1019 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1020 EXPECT_EQ(23, tm.tm_hour);
1021
1022 setenv("TZ", "America/Atka", 1);
1023 tzset();
1024 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1025 EXPECT_EQ(13, tm.tm_hour);
1026
1027 setenv("TZ", "Pacific/Apia", 1);
1028 tzset();
1029 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1030 EXPECT_EQ(12, tm.tm_hour);
1031
1032 setenv("TZ", "Pacific/Honolulu", 1);
1033 tzset();
1034 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1035 EXPECT_EQ(12, tm.tm_hour);
1036
1037 setenv("TZ", "Asia/Magadan", 1);
1038 tzset();
1039 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1040 EXPECT_EQ(9, tm.tm_hour);
1041}
Elliott Hughesea877162017-01-11 14:34:16 -08001042
1043TEST(time, bug_31339449) {
1044 // POSIX says localtime acts as if it calls tzset.
1045 // tzset does two things:
1046 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
1047 // 2. it sets the global `tzname`.
1048 // POSIX says localtime_r need not set `tzname` (2).
1049 // Q: should localtime_r set the time zone (1)?
1050 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1051
1052 // Pick a time, any time...
1053 time_t t = 1475619727;
1054
1055 // Call tzset with a specific timezone.
1056 setenv("TZ", "America/Atka", 1);
1057 tzset();
1058
1059 // If we change the timezone and call localtime, localtime should use the new timezone.
1060 setenv("TZ", "America/Los_Angeles", 1);
1061 struct tm* tm_p = localtime(&t);
1062 EXPECT_EQ(15, tm_p->tm_hour);
1063
1064 // Reset the timezone back.
1065 setenv("TZ", "America/Atka", 1);
1066 tzset();
1067
1068#if defined(__BIONIC__)
1069 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1070 setenv("TZ", "America/Los_Angeles", 1);
1071 struct tm tm = {};
1072 localtime_r(&t, &tm);
1073 EXPECT_EQ(15, tm.tm_hour);
1074#else
1075 // The BSDs agree with us, but glibc gets this wrong.
1076#endif
1077}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001078
1079TEST(time, asctime) {
1080 const struct tm tm = {};
1081 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1082}
1083
1084TEST(time, asctime_r) {
1085 const struct tm tm = {};
1086 char buf[256];
1087 ASSERT_EQ(buf, asctime_r(&tm, buf));
1088 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1089}
1090
1091TEST(time, ctime) {
1092 setenv("TZ", "UTC", 1);
1093 const time_t t = 0;
1094 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1095}
1096
1097TEST(time, ctime_r) {
1098 setenv("TZ", "UTC", 1);
1099 const time_t t = 0;
1100 char buf[256];
1101 ASSERT_EQ(buf, ctime_r(&t, buf));
1102 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1103}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001104
1105// https://issuetracker.google.com/37128336
1106TEST(time, strftime_strptime_s) {
1107 char buf[32];
1108 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1109
1110 setenv("TZ", "America/Los_Angeles", 1);
1111 strftime(buf, sizeof(buf), "<%s>", &tm0);
1112 EXPECT_STREQ("<378720000>", buf);
1113
1114 setenv("TZ", "UTC", 1);
1115 strftime(buf, sizeof(buf), "<%s>", &tm0);
1116 EXPECT_STREQ("<378691200>", buf);
1117
1118 struct tm tm;
1119
1120 setenv("TZ", "America/Los_Angeles", 1);
1121 tzset();
1122 memset(&tm, 0xff, sizeof(tm));
1123 char* p = strptime("378720000x", "%s", &tm);
1124 ASSERT_EQ('x', *p);
1125 EXPECT_EQ(0, tm.tm_sec);
1126 EXPECT_EQ(0, tm.tm_min);
1127 EXPECT_EQ(0, tm.tm_hour);
1128 EXPECT_EQ(1, tm.tm_mday);
1129 EXPECT_EQ(0, tm.tm_mon);
1130 EXPECT_EQ(82, tm.tm_year);
1131 EXPECT_EQ(5, tm.tm_wday);
1132 EXPECT_EQ(0, tm.tm_yday);
1133 EXPECT_EQ(0, tm.tm_isdst);
1134
1135 setenv("TZ", "UTC", 1);
1136 tzset();
1137 memset(&tm, 0xff, sizeof(tm));
1138 p = strptime("378691200x", "%s", &tm);
1139 ASSERT_EQ('x', *p);
1140 EXPECT_EQ(0, tm.tm_sec);
1141 EXPECT_EQ(0, tm.tm_min);
1142 EXPECT_EQ(0, tm.tm_hour);
1143 EXPECT_EQ(1, tm.tm_mday);
1144 EXPECT_EQ(0, tm.tm_mon);
1145 EXPECT_EQ(82, tm.tm_year);
1146 EXPECT_EQ(5, tm.tm_wday);
1147 EXPECT_EQ(0, tm.tm_yday);
1148 EXPECT_EQ(0, tm.tm_isdst);
1149}
1150
1151TEST(time, strptime_s_nothing) {
1152 struct tm tm;
1153 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1154}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001155
1156TEST(time, timespec_get) {
1157#if __BIONIC__
1158 timespec ts = {};
1159 ASSERT_EQ(0, timespec_get(&ts, 123));
1160 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1161#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001162 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001163#endif
1164}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001165
1166TEST(time, difftime) {
1167 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001168 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001169}