blob: 983a823d553f16fa618a7f54c849387bdb392c4c [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
Elliott Hughes329103d2014-04-25 16:55:04 -070087static void* gmtime_no_stack_overflow_14313703_fn(void*) {
88 const char* original_tz = getenv("TZ");
89 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
90 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
91 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -070092 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -070093 setenv("TZ", original_tz, 1);
94 }
95 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -070096 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -070097}
98
99TEST(time, gmtime_no_stack_overflow_14313703) {
100 // Is it safe to call tzload on a thread with a small stack?
101 // http://b/14313703
102 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -0800103 pthread_attr_t a;
104 ASSERT_EQ(0, pthread_attr_init(&a));
105 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700106
107 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700108 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800109 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700110}
111
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900112TEST(time, mktime_empty_TZ) {
113 // tzcode used to have a bug where it didn't reinitialize some internal state.
114
115 // Choose a time where DST is set.
116 struct tm t;
117 memset(&t, 0, sizeof(tm));
118 t.tm_year = 1980 - 1900;
119 t.tm_mon = 6;
120 t.tm_mday = 2;
121
122 setenv("TZ", "America/Los_Angeles", 1);
123 tzset();
124 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
125
126 memset(&t, 0, sizeof(tm));
127 t.tm_year = 1980 - 1900;
128 t.tm_mon = 6;
129 t.tm_mday = 2;
130
131 setenv("TZ", "", 1); // Implies UTC.
132 tzset();
133 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
134}
135
Elliott Hughes7843d442013-08-22 11:37:32 -0700136TEST(time, mktime_10310929) {
137 struct tm t;
138 memset(&t, 0, sizeof(tm));
139 t.tm_year = 200;
140 t.tm_mon = 2;
141 t.tm_mday = 10;
142
Elliott Hughes0c401522013-10-18 16:21:54 -0700143#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700144 // 32-bit bionic has a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700145 ASSERT_EQ(-1, mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700146 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700147#else
148 // Everyone else should be using a signed 64-bit time_t.
149 ASSERT_GE(sizeof(time_t) * 8, 64U);
150
151 setenv("TZ", "America/Los_Angeles", 1);
152 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700153 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700154 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700155 ASSERT_EQ(0, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700156
157 setenv("TZ", "UTC", 1);
158 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700159 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700160 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700161 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700162#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800163}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800164
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700165TEST(time, mktime_EOVERFLOW) {
166 struct tm t;
167 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700168
169 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
170 t.tm_year = 2016 - 1900;
171
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700172 t.tm_mon = 2;
173 t.tm_mday = 10;
174
175 errno = 0;
176 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
177 ASSERT_EQ(0, errno);
178
Elliott Hughes47126ed2016-09-06 13:25:53 -0700179 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700180 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700181
182 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700183 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
184 ASSERT_EQ(EOVERFLOW, errno);
185}
186
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000187TEST(time, mktime_invalid_tm_TZ_combination) {
188 setenv("TZ", "UTC", 1);
189
190 struct tm t;
191 memset(&t, 0, sizeof(tm));
192 t.tm_year = 2022 - 1900;
193 t.tm_mon = 11;
194 t.tm_mday = 31;
195 // UTC does not observe DST
196 t.tm_isdst = 1;
197
198 errno = 0;
199
200 EXPECT_EQ(static_cast<time_t>(-1), mktime(&t));
201 // mktime sets errno to EOVERFLOW if result is unrepresentable.
202 EXPECT_EQ(EOVERFLOW, errno);
203}
204
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700205TEST(time, strftime) {
206 setenv("TZ", "UTC", 1);
207
208 struct tm t;
209 memset(&t, 0, sizeof(tm));
210 t.tm_year = 200;
211 t.tm_mon = 2;
212 t.tm_mday = 10;
213
214 char buf[64];
215
216 // Seconds since the epoch.
217#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
218 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
219 EXPECT_STREQ("4108320000", buf);
220#endif
221
222 // Date and time as text.
223 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
224 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
225}
226
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000227TEST(time, strftime_second_before_epoch) {
228 setenv("TZ", "UTC", 1);
229
230 struct tm t;
231 memset(&t, 0, sizeof(tm));
232 t.tm_year = 1969 - 1900;
233 t.tm_mon = 11;
234 t.tm_mday = 31;
235 t.tm_hour = 23;
236 t.tm_min = 59;
237 t.tm_sec = 59;
238
239 char buf[64];
240
241 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
242 EXPECT_STREQ("-1", buf);
243}
244
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800245TEST(time, strftime_null_tm_zone) {
246 // Netflix on Nexus Player wouldn't start (http://b/25170306).
247 struct tm t;
248 memset(&t, 0, sizeof(tm));
249
250 char buf[64];
251
252 setenv("TZ", "America/Los_Angeles", 1);
253 tzset();
254
255 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
256 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
257 EXPECT_STREQ("<PST>", buf);
258
259#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
260 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
261 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
262 EXPECT_STREQ("<PDT>", buf);
263
264 t.tm_isdst = -123; // "and negative if the information is not available".
265 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
266 EXPECT_STREQ("<>", buf);
267#endif
268
269 setenv("TZ", "UTC", 1);
270 tzset();
271
272 t.tm_isdst = 0;
273 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
274 EXPECT_STREQ("<UTC>", buf);
275
276#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
277 t.tm_isdst = 1; // UTC has no DST.
278 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
279 EXPECT_STREQ("<>", buf);
280#endif
281}
282
Elliott Hughes0a610d02016-07-29 14:04:17 -0700283TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700284 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700285 locale_t old_locale = uselocale(cloc);
286
287 setenv("TZ", "UTC", 1);
288
289 struct tm t;
290 memset(&t, 0, sizeof(tm));
291 t.tm_year = 200;
292 t.tm_mon = 2;
293 t.tm_mday = 10;
294
295 // Date and time as text.
296 char buf[64];
297 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
298 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
299
300 uselocale(old_locale);
301 freelocale(cloc);
302}
303
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700304TEST(time, strptime) {
305 setenv("TZ", "UTC", 1);
306
307 struct tm t;
308 char buf[64];
309
310 memset(&t, 0, sizeof(t));
311 strptime("11:14", "%R", &t);
312 strftime(buf, sizeof(buf), "%H:%M", &t);
313 EXPECT_STREQ("11:14", buf);
314
315 memset(&t, 0, sizeof(t));
316 strptime("09:41:53", "%T", &t);
317 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
318 EXPECT_STREQ("09:41:53", buf);
319}
320
Elliott Hughes3376c232018-02-13 23:14:12 -0800321TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700322#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800323 setenv("TZ", "UTC", 1);
324
325 struct tm t;
326 char buf[64];
327
328 memset(&t, 0, sizeof(t));
329 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
330 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
331 EXPECT_STREQ("11:14", buf);
332
333 memset(&t, 0, sizeof(t));
334 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
335 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
336 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700337#else
338 GTEST_SKIP() << "musl doesn't support strptime_l";
339#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800340}
341
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700342TEST(time, strptime_F) {
343 setenv("TZ", "UTC", 1);
344
345 struct tm tm = {};
346 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
347 EXPECT_EQ(119, tm.tm_year);
348 EXPECT_EQ(2, tm.tm_mon);
349 EXPECT_EQ(26, tm.tm_mday);
350}
351
352TEST(time, strptime_P_p) {
353 setenv("TZ", "UTC", 1);
354
Elliott Hughes11678822019-03-27 08:56:49 -0700355 // For parsing, %P and %p are the same: case doesn't matter.
356
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700357 struct tm tm = {.tm_hour = 12};
358 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
359 EXPECT_EQ(0, tm.tm_hour);
360
361 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700362 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
363 EXPECT_EQ(0, tm.tm_hour);
364
365 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700366 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
367 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700368
369 tm = {.tm_hour = 12};
370 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
371 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700372}
373
374TEST(time, strptime_u) {
375 setenv("TZ", "UTC", 1);
376
377 struct tm tm = {};
378 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
379 EXPECT_EQ(2, tm.tm_wday);
380}
381
382TEST(time, strptime_v) {
383 setenv("TZ", "UTC", 1);
384
385 struct tm tm = {};
386 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
387 EXPECT_EQ(80, tm.tm_year);
388 EXPECT_EQ(2, tm.tm_mon);
389 EXPECT_EQ(26, tm.tm_mday);
390}
391
392TEST(time, strptime_V_G_g) {
393 setenv("TZ", "UTC", 1);
394
395 // %V (ISO-8601 week number), %G (year of week number, without century), and
396 // %g (year of week number) have no effect when parsed, and are supported
397 // solely so that it's possible for strptime(3) to parse everything that
398 // strftime(3) can output.
399 struct tm tm = {};
400 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
401 struct tm zero = {};
402 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
403}
404
Elliott Hughesd065c042020-09-01 19:02:44 -0700405TEST(time, strptime_Z) {
406#if defined(__BIONIC__)
407 // glibc doesn't handle %Z at all.
408 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
409 // are in the global `tzname` (which correspond to the current $TZ).
410 struct tm tm;
411 setenv("TZ", "Europe/Berlin", 1);
412
413 // "GMT" always works.
414 tm = {};
415 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
416 EXPECT_STREQ("GMT", tm.tm_zone);
417 EXPECT_EQ(0, tm.tm_isdst);
418 EXPECT_EQ(0, tm.tm_gmtoff);
419
420 // As does "UTC".
421 tm = {};
422 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
423 EXPECT_STREQ("UTC", tm.tm_zone);
424 EXPECT_EQ(0, tm.tm_isdst);
425 EXPECT_EQ(0, tm.tm_gmtoff);
426
427 // Europe/Berlin is known as "CET" when there's no DST.
428 tm = {};
429 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
430 EXPECT_STREQ("CET", tm.tm_zone);
431 EXPECT_EQ(0, tm.tm_isdst);
432 EXPECT_EQ(3600, tm.tm_gmtoff);
433
434 // Europe/Berlin is known as "CEST" when there's no DST.
435 tm = {};
436 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
437 EXPECT_STREQ("CEST", tm.tm_zone);
438 EXPECT_EQ(1, tm.tm_isdst);
439 EXPECT_EQ(3600, tm.tm_gmtoff);
440
441 // And as long as we're in Europe/Berlin, those are the only time zone
442 // abbreviations that are recognized.
443 tm = {};
444 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
445#endif
446}
447
448TEST(time, strptime_z) {
449 struct tm tm;
450 setenv("TZ", "Europe/Berlin", 1);
451
452 // "UT" is what RFC822 called UTC.
453 tm = {};
454 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
455 EXPECT_STREQ("UTC", tm.tm_zone);
456 EXPECT_EQ(0, tm.tm_isdst);
457 EXPECT_EQ(0, tm.tm_gmtoff);
458 // "GMT" is RFC822's other name for UTC.
459 tm = {};
460 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
461 EXPECT_STREQ("UTC", tm.tm_zone);
462 EXPECT_EQ(0, tm.tm_isdst);
463 EXPECT_EQ(0, tm.tm_gmtoff);
464
465 // "Z" ("Zulu") is a synonym for UTC.
466 tm = {};
467 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
468 EXPECT_STREQ("UTC", tm.tm_zone);
469 EXPECT_EQ(0, tm.tm_isdst);
470 EXPECT_EQ(0, tm.tm_gmtoff);
471
472 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
473 tm = {};
474 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
475 EXPECT_STREQ("PST", tm.tm_zone);
476 EXPECT_EQ(0, tm.tm_isdst);
477 EXPECT_EQ(-28800, tm.tm_gmtoff);
478 tm = {};
479 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
480 EXPECT_STREQ("PDT", tm.tm_zone);
481 EXPECT_EQ(1, tm.tm_isdst);
482 EXPECT_EQ(-25200, tm.tm_gmtoff);
483
484 // +-hh
485 tm = {};
486 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
487 EXPECT_EQ(3600, tm.tm_gmtoff);
488 EXPECT_TRUE(tm.tm_zone == nullptr);
489 EXPECT_EQ(0, tm.tm_isdst);
490 // +-hhmm
491 tm = {};
492 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
493 EXPECT_EQ(5400, tm.tm_gmtoff);
494 EXPECT_TRUE(tm.tm_zone == nullptr);
495 EXPECT_EQ(0, tm.tm_isdst);
496 // +-hh:mm
497 tm = {};
498 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
499 EXPECT_EQ(5400, tm.tm_gmtoff);
500 EXPECT_TRUE(tm.tm_zone == nullptr);
501 EXPECT_EQ(0, tm.tm_isdst);
502}
503
Elliott Hughes4b558f52014-03-04 15:58:02 -0800504void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
505 itimerspec ts;
506 ts.it_value.tv_sec = value_s;
507 ts.it_value.tv_nsec = value_ns;
508 ts.it_interval.tv_sec = interval_s;
509 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700510 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800511}
512
Colin Cross7da20342021-07-28 11:18:11 -0700513static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800514}
515
516TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700517 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800518 memset(&se, 0, sizeof(se));
519 se.sigev_notify = SIGEV_THREAD;
520 se.sigev_notify_function = NoOpNotifyFunction;
521 timer_t timer_id;
522 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
523
Elliott Hughes33697a02016-01-26 13:04:57 -0800524 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800525 ASSERT_NE(-1, pid) << strerror(errno);
526
527 if (pid == 0) {
528 // Timers are not inherited by the child.
529 ASSERT_EQ(-1, timer_delete(timer_id));
530 ASSERT_EQ(EINVAL, errno);
531 _exit(0);
532 }
533
Elliott Hughes33697a02016-01-26 13:04:57 -0800534 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800535
536 ASSERT_EQ(0, timer_delete(timer_id));
537}
538
Yabin Cui95f1ee22015-01-13 19:53:15 -0800539static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800540static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
541 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
542 ASSERT_EQ(SIGUSR1, signal_number);
543}
544
545TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700546 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800547 memset(&se, 0, sizeof(se));
548 se.sigev_notify = SIGEV_SIGNAL;
549 se.sigev_signo = SIGUSR1;
550
551 timer_t timer_id;
552 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
553
Yabin Cui95f1ee22015-01-13 19:53:15 -0800554 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800555 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
556
557 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
558
559 itimerspec ts;
560 ts.it_value.tv_sec = 0;
561 ts.it_value.tv_nsec = 1;
562 ts.it_interval.tv_sec = 0;
563 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700564 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800565
566 usleep(500000);
567 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
568}
569
570struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800571 private:
572 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800573 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700574 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700575 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800576
Elliott Hughes4b558f52014-03-04 15:58:02 -0800577 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700578 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800579 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700580 timer_valid = true;
581 }
582
Yabin Cui95f1ee22015-01-13 19:53:15 -0800583 public:
Colin Cross7da20342021-07-28 11:18:11 -0700584 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800585 memset(&se, 0, sizeof(se));
586 se.sigev_notify = SIGEV_THREAD;
587 se.sigev_notify_function = fn;
588 se.sigev_value.sival_ptr = this;
589 Create();
590 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700591 void DeleteTimer() {
592 ASSERT_TRUE(timer_valid);
593 ASSERT_EQ(0, timer_delete(timer_id));
594 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800595 }
596
597 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700598 if (timer_valid) {
599 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800600 }
601 }
602
Yabin Cui95f1ee22015-01-13 19:53:15 -0800603 int Value() const {
604 return value;
605 }
606
Christopher Ferris62d84b12014-10-20 19:09:19 -0700607 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
608 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
609 }
610
611 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800612 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700613 time_t start = time(nullptr);
614 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700615 }
616 return current_value != value;
617 }
618
Colin Cross7da20342021-07-28 11:18:11 -0700619 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800620 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
621 ++cd->value;
622 }
623
Colin Cross7da20342021-07-28 11:18:11 -0700624 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800625 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
626 ++cd->value;
627
628 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700629 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800630 }
631};
632
633TEST(time, timer_settime_0) {
634 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800635 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800636
Yabin Cuibf572d92015-08-11 11:23:16 -0700637 counter.SetTime(0, 500000000, 1, 0);
638 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800639
640 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800641 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800642}
643
644TEST(time, timer_settime_repeats) {
645 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800646 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800647
Christopher Ferris62d84b12014-10-20 19:09:19 -0700648 counter.SetTime(0, 1, 0, 10);
649 ASSERT_TRUE(counter.ValueUpdated());
650 ASSERT_TRUE(counter.ValueUpdated());
651 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800652 counter.DeleteTimer();
653 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
654 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800655}
656
Yabin Cui95f1ee22015-01-13 19:53:15 -0800657static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800658static void timer_create_NULL_signal_handler(int signal_number) {
659 ++timer_create_NULL_signal_handler_invocation_count;
660 ASSERT_EQ(SIGALRM, signal_number);
661}
662
663TEST(time, timer_create_NULL) {
664 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
665 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700666 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800667
Yabin Cui95f1ee22015-01-13 19:53:15 -0800668 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800669 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
670
671 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
672
673 SetTime(timer_id, 0, 1, 0, 0);
674 usleep(500000);
675
676 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
677}
678
679TEST(time, timer_create_EINVAL) {
680 clockid_t invalid_clock = 16;
681
682 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
683 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700684 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800685 ASSERT_EQ(EINVAL, errno);
686
687 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700688 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800689 memset(&se, 0, sizeof(se));
690 se.sigev_notify = SIGEV_THREAD;
691 se.sigev_notify_function = NoOpNotifyFunction;
692 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
693 ASSERT_EQ(EINVAL, errno);
694}
695
Elliott Hughes4b558f52014-03-04 15:58:02 -0800696TEST(time, timer_create_multiple) {
697 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800698 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800699 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800700
Yabin Cui95f1ee22015-01-13 19:53:15 -0800701 ASSERT_EQ(0, counter1.Value());
702 ASSERT_EQ(0, counter2.Value());
703 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800704
Yabin Cui410c1ad2015-06-18 16:19:02 -0700705 counter2.SetTime(0, 500000000, 0, 0);
706 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800707
Yabin Cui95f1ee22015-01-13 19:53:15 -0800708 EXPECT_EQ(0, counter1.Value());
709 EXPECT_EQ(1, counter2.Value());
710 EXPECT_EQ(0, counter3.Value());
711}
712
713// Test to verify that disarming a repeatable timer disables the callbacks.
714TEST(time, timer_disarm_terminates) {
715 Counter counter(Counter::CountNotifyFunction);
716 ASSERT_EQ(0, counter.Value());
717
718 counter.SetTime(0, 1, 0, 1);
719 ASSERT_TRUE(counter.ValueUpdated());
720 ASSERT_TRUE(counter.ValueUpdated());
721 ASSERT_TRUE(counter.ValueUpdated());
722
723 counter.SetTime(0, 0, 0, 0);
724 // Add a sleep as the kernel may have pending events when the timer is disarmed.
725 usleep(500000);
726 int value = counter.Value();
727 usleep(500000);
728
729 // Verify the counter has not been incremented.
730 ASSERT_EQ(value, counter.Value());
731}
732
733// Test to verify that deleting a repeatable timer disables the callbacks.
734TEST(time, timer_delete_terminates) {
735 Counter counter(Counter::CountNotifyFunction);
736 ASSERT_EQ(0, counter.Value());
737
738 counter.SetTime(0, 1, 0, 1);
739 ASSERT_TRUE(counter.ValueUpdated());
740 ASSERT_TRUE(counter.ValueUpdated());
741 ASSERT_TRUE(counter.ValueUpdated());
742
743 counter.DeleteTimer();
744 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
745 usleep(500000);
746 int value = counter.Value();
747 usleep(500000);
748
749 // Verify the counter has not been incremented.
750 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800751}
Christopher Ferris753ad772014-03-20 20:47:45 -0700752
753struct TimerDeleteData {
754 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800755 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700756 volatile bool complete;
757};
758
Colin Cross7da20342021-07-28 11:18:11 -0700759static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700760 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
761
Elliott Hughes11859d42017-02-13 17:59:29 -0800762 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700763 timer_delete(tdd->timer_id);
764 tdd->complete = true;
765}
766
767TEST(time, timer_delete_from_timer_thread) {
768 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700769 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700770
771 memset(&se, 0, sizeof(se));
772 se.sigev_notify = SIGEV_THREAD;
773 se.sigev_notify_function = TimerDeleteCallback;
774 se.sigev_value.sival_ptr = &tdd;
775
776 tdd.complete = false;
777 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
778
779 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800780 ts.it_value.tv_sec = 1;
781 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700782 ts.it_interval.tv_sec = 0;
783 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700784 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700785
Yi Kong32bc0fc2018-08-02 17:31:13 -0700786 time_t cur_time = time(nullptr);
787 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700788 ASSERT_TRUE(tdd.complete);
789
790#if defined(__BIONIC__)
791 // Since bionic timers are implemented by creating a thread to handle the
792 // callback, verify that the thread actually completes.
793 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800794 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
795 ASSERT_EQ(-1, kill(tdd.tid, 0));
796 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700797#endif
798}
Elliott Hughes625993d2014-07-15 16:53:13 -0700799
Colin Cross35d469b2021-08-16 15:26:28 -0700800// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
801#if !defined(__NR_clock_gettime)
802#define __NR_clock_gettime __NR_clock_gettime32
803#endif
804
Elliott Hughes625993d2014-07-15 16:53:13 -0700805TEST(time, clock_gettime) {
806 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100807 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700808 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700809 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100810 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
811 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
812 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700813
Giuliano Procida096f5952021-04-08 10:51:58 +0100814 // Check we have a nice monotonic timestamp sandwich.
815 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
816 if (ts0.tv_sec == ts1.tv_sec) {
817 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700818 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100819 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
820 if (ts1.tv_sec == ts2.tv_sec) {
821 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
822 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700823}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700824
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700825TEST(time, clock_gettime_CLOCK_REALTIME) {
826 timespec ts;
827 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
828}
829
830TEST(time, clock_gettime_CLOCK_MONOTONIC) {
831 timespec ts;
832 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
833}
834
835TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
836 timespec ts;
837 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
838}
839
840TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
841 timespec ts;
842 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
843}
844
845TEST(time, clock_gettime_CLOCK_BOOTTIME) {
846 timespec ts;
847 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
848}
849
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800850TEST(time, clock_gettime_unknown) {
851 errno = 0;
852 timespec ts;
853 ASSERT_EQ(-1, clock_gettime(-1, &ts));
854 ASSERT_EQ(EINVAL, errno);
855}
856
857TEST(time, clock_getres_CLOCK_REALTIME) {
858 timespec ts;
859 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
860 ASSERT_EQ(1, ts.tv_nsec);
861 ASSERT_EQ(0, ts.tv_sec);
862}
863
864TEST(time, clock_getres_CLOCK_MONOTONIC) {
865 timespec ts;
866 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
867 ASSERT_EQ(1, ts.tv_nsec);
868 ASSERT_EQ(0, ts.tv_sec);
869}
870
871TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
872 timespec ts;
873 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
874}
875
876TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
877 timespec ts;
878 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
879}
880
881TEST(time, clock_getres_CLOCK_BOOTTIME) {
882 timespec ts;
883 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
884 ASSERT_EQ(1, ts.tv_nsec);
885 ASSERT_EQ(0, ts.tv_sec);
886}
887
888TEST(time, clock_getres_unknown) {
889 errno = 0;
890 timespec ts = { -1, -1 };
891 ASSERT_EQ(-1, clock_getres(-1, &ts));
892 ASSERT_EQ(EINVAL, errno);
893 ASSERT_EQ(-1, ts.tv_nsec);
894 ASSERT_EQ(-1, ts.tv_sec);
895}
896
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700897TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100898 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
899 static const clock_t N = 5;
900 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +0900901 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100902 for (size_t i = 0; i < N; ++i) {
903 sleep(1);
904 }
Haruki Hasegawa18160252014-10-13 00:50:47 +0900905 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100906 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +0900907}
908
Elliott Hughesb4413592017-11-29 18:17:06 -0800909static pid_t GetInvalidPid() {
910 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -0800911 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -0800912 fscanf(fp.get(), "%ld", &pid_max);
913 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -0800914}
915
Elliott Hughesb4413592017-11-29 18:17:06 -0800916TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800917 clockid_t clockid;
918 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -0800919 timespec ts;
920 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800921}
Yabin Cuid5c65272014-11-26 14:04:26 -0800922
Elliott Hughesb4413592017-11-29 18:17:06 -0800923TEST(time, clock_getcpuclockid_parent) {
924 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -0800925 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -0800926 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -0800927 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800928}
Yabin Cuid5c65272014-11-26 14:04:26 -0800929
Elliott Hughesb4413592017-11-29 18:17:06 -0800930TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800931 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
932 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800933 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -0800934 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800935 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
936 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
937 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
938 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
939 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
940 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -0800941 ASSERT_EQ(0, errno);
942}
943
Haruki Hasegawa18160252014-10-13 00:50:47 +0900944TEST(time, clock_settime) {
945 errno = 0;
946 timespec ts;
947 ASSERT_EQ(-1, clock_settime(-1, &ts));
948 ASSERT_EQ(EINVAL, errno);
949}
950
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700951TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900952 timespec in;
953 timespec out;
954 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700955}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700956
957TEST(time, clock_nanosleep_thread_cputime_id) {
958 timespec in;
959 in.tv_sec = 1;
960 in.tv_nsec = 0;
961 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
962}
Elliott Hughes12443702016-10-19 16:02:31 -0700963
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700964TEST(time, clock_nanosleep) {
965 auto t0 = std::chrono::steady_clock::now();
966 const timespec ts = {.tv_nsec = 5000000};
967 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
968 auto t1 = std::chrono::steady_clock::now();
969 ASSERT_GE(t1-t0, 5000000ns);
970}
971
972TEST(time, nanosleep) {
973 auto t0 = std::chrono::steady_clock::now();
974 const timespec ts = {.tv_nsec = 5000000};
975 ASSERT_EQ(0, nanosleep(&ts, nullptr));
976 auto t1 = std::chrono::steady_clock::now();
977 ASSERT_GE(t1-t0, 5000000ns);
978}
979
980TEST(time, nanosleep_EINVAL) {
981 timespec ts = {.tv_sec = -1};
982 errno = 0;
983 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
984 ASSERT_EQ(EINVAL, errno);
985}
986
Elliott Hughes12443702016-10-19 16:02:31 -0700987TEST(time, bug_31938693) {
988 // User-visible symptoms in N:
989 // http://b/31938693
990 // https://code.google.com/p/android/issues/detail?id=225132
991
992 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
993 // http://b/31848040
994
995 // This isn't a great test, because very few time zones were actually affected, and there's
996 // no real logic to which ones were affected: it was just a coincidence of the data that came
997 // after them in the tzdata file.
998
999 time_t t = 1475619727;
1000 struct tm tm;
1001
1002 setenv("TZ", "America/Los_Angeles", 1);
1003 tzset();
1004 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1005 EXPECT_EQ(15, tm.tm_hour);
1006
1007 setenv("TZ", "Europe/London", 1);
1008 tzset();
1009 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1010 EXPECT_EQ(23, tm.tm_hour);
1011
1012 setenv("TZ", "America/Atka", 1);
1013 tzset();
1014 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1015 EXPECT_EQ(13, tm.tm_hour);
1016
1017 setenv("TZ", "Pacific/Apia", 1);
1018 tzset();
1019 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1020 EXPECT_EQ(12, tm.tm_hour);
1021
1022 setenv("TZ", "Pacific/Honolulu", 1);
1023 tzset();
1024 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1025 EXPECT_EQ(12, tm.tm_hour);
1026
1027 setenv("TZ", "Asia/Magadan", 1);
1028 tzset();
1029 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1030 EXPECT_EQ(9, tm.tm_hour);
1031}
Elliott Hughesea877162017-01-11 14:34:16 -08001032
1033TEST(time, bug_31339449) {
1034 // POSIX says localtime acts as if it calls tzset.
1035 // tzset does two things:
1036 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
1037 // 2. it sets the global `tzname`.
1038 // POSIX says localtime_r need not set `tzname` (2).
1039 // Q: should localtime_r set the time zone (1)?
1040 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1041
1042 // Pick a time, any time...
1043 time_t t = 1475619727;
1044
1045 // Call tzset with a specific timezone.
1046 setenv("TZ", "America/Atka", 1);
1047 tzset();
1048
1049 // If we change the timezone and call localtime, localtime should use the new timezone.
1050 setenv("TZ", "America/Los_Angeles", 1);
1051 struct tm* tm_p = localtime(&t);
1052 EXPECT_EQ(15, tm_p->tm_hour);
1053
1054 // Reset the timezone back.
1055 setenv("TZ", "America/Atka", 1);
1056 tzset();
1057
1058#if defined(__BIONIC__)
1059 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1060 setenv("TZ", "America/Los_Angeles", 1);
1061 struct tm tm = {};
1062 localtime_r(&t, &tm);
1063 EXPECT_EQ(15, tm.tm_hour);
1064#else
1065 // The BSDs agree with us, but glibc gets this wrong.
1066#endif
1067}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001068
1069TEST(time, asctime) {
1070 const struct tm tm = {};
1071 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1072}
1073
1074TEST(time, asctime_r) {
1075 const struct tm tm = {};
1076 char buf[256];
1077 ASSERT_EQ(buf, asctime_r(&tm, buf));
1078 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1079}
1080
1081TEST(time, ctime) {
1082 setenv("TZ", "UTC", 1);
1083 const time_t t = 0;
1084 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1085}
1086
1087TEST(time, ctime_r) {
1088 setenv("TZ", "UTC", 1);
1089 const time_t t = 0;
1090 char buf[256];
1091 ASSERT_EQ(buf, ctime_r(&t, buf));
1092 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1093}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001094
1095// https://issuetracker.google.com/37128336
1096TEST(time, strftime_strptime_s) {
1097 char buf[32];
1098 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1099
1100 setenv("TZ", "America/Los_Angeles", 1);
1101 strftime(buf, sizeof(buf), "<%s>", &tm0);
1102 EXPECT_STREQ("<378720000>", buf);
1103
1104 setenv("TZ", "UTC", 1);
1105 strftime(buf, sizeof(buf), "<%s>", &tm0);
1106 EXPECT_STREQ("<378691200>", buf);
1107
1108 struct tm tm;
1109
1110 setenv("TZ", "America/Los_Angeles", 1);
1111 tzset();
1112 memset(&tm, 0xff, sizeof(tm));
1113 char* p = strptime("378720000x", "%s", &tm);
1114 ASSERT_EQ('x', *p);
1115 EXPECT_EQ(0, tm.tm_sec);
1116 EXPECT_EQ(0, tm.tm_min);
1117 EXPECT_EQ(0, tm.tm_hour);
1118 EXPECT_EQ(1, tm.tm_mday);
1119 EXPECT_EQ(0, tm.tm_mon);
1120 EXPECT_EQ(82, tm.tm_year);
1121 EXPECT_EQ(5, tm.tm_wday);
1122 EXPECT_EQ(0, tm.tm_yday);
1123 EXPECT_EQ(0, tm.tm_isdst);
1124
1125 setenv("TZ", "UTC", 1);
1126 tzset();
1127 memset(&tm, 0xff, sizeof(tm));
1128 p = strptime("378691200x", "%s", &tm);
1129 ASSERT_EQ('x', *p);
1130 EXPECT_EQ(0, tm.tm_sec);
1131 EXPECT_EQ(0, tm.tm_min);
1132 EXPECT_EQ(0, tm.tm_hour);
1133 EXPECT_EQ(1, tm.tm_mday);
1134 EXPECT_EQ(0, tm.tm_mon);
1135 EXPECT_EQ(82, tm.tm_year);
1136 EXPECT_EQ(5, tm.tm_wday);
1137 EXPECT_EQ(0, tm.tm_yday);
1138 EXPECT_EQ(0, tm.tm_isdst);
1139}
1140
1141TEST(time, strptime_s_nothing) {
1142 struct tm tm;
1143 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1144}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001145
1146TEST(time, timespec_get) {
1147#if __BIONIC__
1148 timespec ts = {};
1149 ASSERT_EQ(0, timespec_get(&ts, 123));
1150 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1151#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001152 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001153#endif
1154}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001155
1156TEST(time, difftime) {
1157 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001158 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001159}