blob: 40e5c6d1ff1e6585ce0d7f2363a01a4c6d9dc6a3 [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) {
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100147 struct tm tm = {.tm_year = 2100 - 1900, .tm_mon = 2, .tm_mday = 10};
Elliott Hughes7843d442013-08-22 11:37:32 -0700148
Elliott Hughes0c401522013-10-18 16:21:54 -0700149#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700150 // 32-bit bionic has a signed 32-bit time_t.
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100151 ASSERT_EQ(-1, mktime(&tm));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700152 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700153#else
154 // Everyone else should be using a signed 64-bit time_t.
155 ASSERT_GE(sizeof(time_t) * 8, 64U);
156
157 setenv("TZ", "America/Los_Angeles", 1);
158 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700159 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700160
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100161 // On the date/time specified by tm America/Los_Angeles
162 // follows DST. But tm_isdst is set to 0, which forces
163 // mktime to interpret that time as local standard, hence offset
164 // is 8 hours, not 7.
165 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&tm));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700166 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700167#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800168}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800169
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700170TEST(time, mktime_EOVERFLOW) {
171 struct tm t;
172 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700173
174 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
175 t.tm_year = 2016 - 1900;
176
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700177 t.tm_mon = 2;
178 t.tm_mday = 10;
179
180 errno = 0;
181 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
182 ASSERT_EQ(0, errno);
183
Elliott Hughes47126ed2016-09-06 13:25:53 -0700184 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700185 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700186
187 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700188 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
189 ASSERT_EQ(EOVERFLOW, errno);
190}
191
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000192TEST(time, mktime_invalid_tm_TZ_combination) {
193 setenv("TZ", "UTC", 1);
194
195 struct tm t;
196 memset(&t, 0, sizeof(tm));
197 t.tm_year = 2022 - 1900;
198 t.tm_mon = 11;
199 t.tm_mday = 31;
200 // UTC does not observe DST
201 t.tm_isdst = 1;
202
203 errno = 0;
204
205 EXPECT_EQ(static_cast<time_t>(-1), mktime(&t));
206 // mktime sets errno to EOVERFLOW if result is unrepresentable.
207 EXPECT_EQ(EOVERFLOW, errno);
208}
209
Almaz Mingaleev10fed722022-07-04 16:05:22 +0100210// Transitions in the tzdata file are generated up to the year 2100. Testing
211// that dates beyond that are handled properly too.
212TEST(time, mktime_after_2100) {
213 struct tm tm = {.tm_year = 2150 - 1900, .tm_mon = 2, .tm_mday = 10, .tm_isdst = -1};
214
215#if !defined(__LP64__)
216 // 32-bit bionic has a signed 32-bit time_t.
217 ASSERT_EQ(-1, mktime(&tm));
218 ASSERT_EQ(EOVERFLOW, errno);
219#else
220 setenv("TZ", "Europe/London", 1);
221 tzset();
222 errno = 0;
223
224 ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm));
225 ASSERT_EQ(0, errno);
226#endif
227}
228
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700229TEST(time, strftime) {
230 setenv("TZ", "UTC", 1);
231
232 struct tm t;
233 memset(&t, 0, sizeof(tm));
234 t.tm_year = 200;
235 t.tm_mon = 2;
236 t.tm_mday = 10;
237
238 char buf[64];
239
240 // Seconds since the epoch.
241#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
242 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
243 EXPECT_STREQ("4108320000", buf);
244#endif
245
246 // Date and time as text.
247 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
248 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
249}
250
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000251TEST(time, strftime_second_before_epoch) {
252 setenv("TZ", "UTC", 1);
253
254 struct tm t;
255 memset(&t, 0, sizeof(tm));
256 t.tm_year = 1969 - 1900;
257 t.tm_mon = 11;
258 t.tm_mday = 31;
259 t.tm_hour = 23;
260 t.tm_min = 59;
261 t.tm_sec = 59;
262
263 char buf[64];
264
265 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t));
266 EXPECT_STREQ("-1", buf);
267}
268
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100269TEST(time, strftime_Z_null_tm_zone) {
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800270 // Netflix on Nexus Player wouldn't start (http://b/25170306).
271 struct tm t;
272 memset(&t, 0, sizeof(tm));
273
274 char buf[64];
275
276 setenv("TZ", "America/Los_Angeles", 1);
277 tzset();
278
279 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
280 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
281 EXPECT_STREQ("<PST>", buf);
282
283#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
284 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
285 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
286 EXPECT_STREQ("<PDT>", buf);
287
288 t.tm_isdst = -123; // "and negative if the information is not available".
289 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
290 EXPECT_STREQ("<>", buf);
291#endif
292
293 setenv("TZ", "UTC", 1);
294 tzset();
295
296 t.tm_isdst = 0;
297 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
298 EXPECT_STREQ("<UTC>", buf);
299
300#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
301 t.tm_isdst = 1; // UTC has no DST.
302 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
303 EXPECT_STREQ("<>", buf);
304#endif
305}
306
Almaz Mingaleev24bfd8e2022-07-15 11:01:53 +0100307// According to C language specification the only tm struct field needed to
308// find out replacement for %z and %Z in strftime is tm_isdst. Which is
309// wrong, as time zones change their standard offset and even DST savings.
310// tzcode deviates from C language specification and requires tm struct either
311// to be output of localtime-like functions or to be modified by mktime call
312// before passing to strftime. See tz mailing discussion for more details
313// https://mm.icann.org/pipermail/tz/2022-July/031674.html
314// But we are testing case when tm.tm_zone is null, which means that tm struct
315// is not coming from localtime and is neither modified by mktime. That's why
316// we are comparing against +0000, even though America/Los_Angeles never
317// observes it.
318TEST(time, strftime_z_null_tm_zone) {
319 char str[64];
320 struct tm tm = {.tm_year = 109, .tm_mon = 4, .tm_mday = 2, .tm_isdst = 0};
321
322 setenv("TZ", "America/Los_Angeles", 1);
323 tzset();
324
325 tm.tm_zone = NULL;
326
327 size_t result = strftime(str, sizeof(str), "%z", &tm);
328
329 EXPECT_EQ(5U, result);
330 EXPECT_STREQ("+0000", str);
331
332 tm.tm_isdst = 1;
333
334 result = strftime(str, sizeof(str), "%z", &tm);
335
336 EXPECT_EQ(5U, result);
337 EXPECT_STREQ("+0000", str);
338
339 setenv("TZ", "UTC", 1);
340 tzset();
341
342 tm.tm_isdst = 0;
343
344 result = strftime(str, sizeof(str), "%z", &tm);
345
346 EXPECT_EQ(5U, result);
347 EXPECT_STREQ("+0000", str);
348
349 tm.tm_isdst = 1;
350
351 result = strftime(str, sizeof(str), "%z", &tm);
352
353 EXPECT_EQ(5U, result);
354 EXPECT_STREQ("+0000", str);
355}
356
357TEST(time, strftime_z_Europe_Lisbon) {
358 char str[64];
359 // During 1992-1996 Europe/Lisbon standard offset was 1 hour.
360 // tm_isdst is not set as it will be overridden by mktime call anyway.
361 struct tm tm = {.tm_year = 1996 - 1900, .tm_mon = 2, .tm_mday = 13};
362
363 setenv("TZ", "Europe/Lisbon", 1);
364 tzset();
365
366 // tzcode's strftime implementation for %z relies on prior mktime call.
367 // At the moment of writing %z value is taken from tm_gmtoff. So without
368 // mktime call %z is replaced with +0000.
369 // See https://mm.icann.org/pipermail/tz/2022-July/031674.html
370 mktime(&tm);
371
372 size_t result = strftime(str, sizeof(str), "%z", &tm);
373
374 EXPECT_EQ(5U, result);
375 EXPECT_STREQ("+0100", str);
376
377 // Now standard offset is 0.
378 tm = {.tm_year = 2022 - 1900, .tm_mon = 2, .tm_mday = 13};
379
380 mktime(&tm);
381 result = strftime(str, sizeof(str), "%z", &tm);
382
383 EXPECT_EQ(5U, result);
384 EXPECT_STREQ("+0000", str);
385}
386
Elliott Hughes0a610d02016-07-29 14:04:17 -0700387TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700388 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700389 locale_t old_locale = uselocale(cloc);
390
391 setenv("TZ", "UTC", 1);
392
393 struct tm t;
394 memset(&t, 0, sizeof(tm));
395 t.tm_year = 200;
396 t.tm_mon = 2;
397 t.tm_mday = 10;
398
399 // Date and time as text.
400 char buf[64];
401 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
402 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
403
404 uselocale(old_locale);
405 freelocale(cloc);
406}
407
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700408TEST(time, strptime) {
409 setenv("TZ", "UTC", 1);
410
411 struct tm t;
412 char buf[64];
413
414 memset(&t, 0, sizeof(t));
415 strptime("11:14", "%R", &t);
416 strftime(buf, sizeof(buf), "%H:%M", &t);
417 EXPECT_STREQ("11:14", buf);
418
419 memset(&t, 0, sizeof(t));
420 strptime("09:41:53", "%T", &t);
421 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
422 EXPECT_STREQ("09:41:53", buf);
423}
424
Elliott Hughes3376c232018-02-13 23:14:12 -0800425TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700426#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800427 setenv("TZ", "UTC", 1);
428
429 struct tm t;
430 char buf[64];
431
432 memset(&t, 0, sizeof(t));
433 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
434 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
435 EXPECT_STREQ("11:14", buf);
436
437 memset(&t, 0, sizeof(t));
438 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
439 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
440 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700441#else
442 GTEST_SKIP() << "musl doesn't support strptime_l";
443#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800444}
445
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700446TEST(time, strptime_F) {
447 setenv("TZ", "UTC", 1);
448
449 struct tm tm = {};
450 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
451 EXPECT_EQ(119, tm.tm_year);
452 EXPECT_EQ(2, tm.tm_mon);
453 EXPECT_EQ(26, tm.tm_mday);
454}
455
456TEST(time, strptime_P_p) {
457 setenv("TZ", "UTC", 1);
458
Elliott Hughes11678822019-03-27 08:56:49 -0700459 // For parsing, %P and %p are the same: case doesn't matter.
460
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700461 struct tm tm = {.tm_hour = 12};
462 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
463 EXPECT_EQ(0, tm.tm_hour);
464
465 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700466 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
467 EXPECT_EQ(0, tm.tm_hour);
468
469 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700470 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
471 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700472
473 tm = {.tm_hour = 12};
474 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
475 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700476}
477
478TEST(time, strptime_u) {
479 setenv("TZ", "UTC", 1);
480
481 struct tm tm = {};
482 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
483 EXPECT_EQ(2, tm.tm_wday);
484}
485
486TEST(time, strptime_v) {
487 setenv("TZ", "UTC", 1);
488
489 struct tm tm = {};
490 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
491 EXPECT_EQ(80, tm.tm_year);
492 EXPECT_EQ(2, tm.tm_mon);
493 EXPECT_EQ(26, tm.tm_mday);
494}
495
496TEST(time, strptime_V_G_g) {
497 setenv("TZ", "UTC", 1);
498
499 // %V (ISO-8601 week number), %G (year of week number, without century), and
500 // %g (year of week number) have no effect when parsed, and are supported
501 // solely so that it's possible for strptime(3) to parse everything that
502 // strftime(3) can output.
503 struct tm tm = {};
504 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
505 struct tm zero = {};
506 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
507}
508
Elliott Hughesd065c042020-09-01 19:02:44 -0700509TEST(time, strptime_Z) {
510#if defined(__BIONIC__)
511 // glibc doesn't handle %Z at all.
512 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
513 // are in the global `tzname` (which correspond to the current $TZ).
514 struct tm tm;
515 setenv("TZ", "Europe/Berlin", 1);
516
517 // "GMT" always works.
518 tm = {};
519 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
520 EXPECT_STREQ("GMT", tm.tm_zone);
521 EXPECT_EQ(0, tm.tm_isdst);
522 EXPECT_EQ(0, tm.tm_gmtoff);
523
524 // As does "UTC".
525 tm = {};
526 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
527 EXPECT_STREQ("UTC", tm.tm_zone);
528 EXPECT_EQ(0, tm.tm_isdst);
529 EXPECT_EQ(0, tm.tm_gmtoff);
530
531 // Europe/Berlin is known as "CET" when there's no DST.
532 tm = {};
533 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
534 EXPECT_STREQ("CET", tm.tm_zone);
535 EXPECT_EQ(0, tm.tm_isdst);
536 EXPECT_EQ(3600, tm.tm_gmtoff);
537
538 // Europe/Berlin is known as "CEST" when there's no DST.
539 tm = {};
540 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
541 EXPECT_STREQ("CEST", tm.tm_zone);
542 EXPECT_EQ(1, tm.tm_isdst);
543 EXPECT_EQ(3600, tm.tm_gmtoff);
544
545 // And as long as we're in Europe/Berlin, those are the only time zone
546 // abbreviations that are recognized.
547 tm = {};
548 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
549#endif
550}
551
552TEST(time, strptime_z) {
553 struct tm tm;
554 setenv("TZ", "Europe/Berlin", 1);
555
556 // "UT" is what RFC822 called UTC.
557 tm = {};
558 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
559 EXPECT_STREQ("UTC", tm.tm_zone);
560 EXPECT_EQ(0, tm.tm_isdst);
561 EXPECT_EQ(0, tm.tm_gmtoff);
562 // "GMT" is RFC822's other name for UTC.
563 tm = {};
564 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
565 EXPECT_STREQ("UTC", tm.tm_zone);
566 EXPECT_EQ(0, tm.tm_isdst);
567 EXPECT_EQ(0, tm.tm_gmtoff);
568
569 // "Z" ("Zulu") is a synonym for UTC.
570 tm = {};
571 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
572 EXPECT_STREQ("UTC", tm.tm_zone);
573 EXPECT_EQ(0, tm.tm_isdst);
574 EXPECT_EQ(0, tm.tm_gmtoff);
575
576 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
577 tm = {};
578 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
579 EXPECT_STREQ("PST", tm.tm_zone);
580 EXPECT_EQ(0, tm.tm_isdst);
581 EXPECT_EQ(-28800, tm.tm_gmtoff);
582 tm = {};
583 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
584 EXPECT_STREQ("PDT", tm.tm_zone);
585 EXPECT_EQ(1, tm.tm_isdst);
586 EXPECT_EQ(-25200, tm.tm_gmtoff);
587
588 // +-hh
589 tm = {};
590 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
591 EXPECT_EQ(3600, tm.tm_gmtoff);
592 EXPECT_TRUE(tm.tm_zone == nullptr);
593 EXPECT_EQ(0, tm.tm_isdst);
594 // +-hhmm
595 tm = {};
596 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
597 EXPECT_EQ(5400, tm.tm_gmtoff);
598 EXPECT_TRUE(tm.tm_zone == nullptr);
599 EXPECT_EQ(0, tm.tm_isdst);
600 // +-hh:mm
601 tm = {};
602 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
603 EXPECT_EQ(5400, tm.tm_gmtoff);
604 EXPECT_TRUE(tm.tm_zone == nullptr);
605 EXPECT_EQ(0, tm.tm_isdst);
606}
607
Elliott Hughes4b558f52014-03-04 15:58:02 -0800608void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
609 itimerspec ts;
610 ts.it_value.tv_sec = value_s;
611 ts.it_value.tv_nsec = value_ns;
612 ts.it_interval.tv_sec = interval_s;
613 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700614 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800615}
616
Colin Cross7da20342021-07-28 11:18:11 -0700617static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800618}
619
620TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700621 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800622 memset(&se, 0, sizeof(se));
623 se.sigev_notify = SIGEV_THREAD;
624 se.sigev_notify_function = NoOpNotifyFunction;
625 timer_t timer_id;
626 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
627
Elliott Hughes33697a02016-01-26 13:04:57 -0800628 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800629 ASSERT_NE(-1, pid) << strerror(errno);
630
631 if (pid == 0) {
632 // Timers are not inherited by the child.
633 ASSERT_EQ(-1, timer_delete(timer_id));
634 ASSERT_EQ(EINVAL, errno);
635 _exit(0);
636 }
637
Elliott Hughes33697a02016-01-26 13:04:57 -0800638 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800639
640 ASSERT_EQ(0, timer_delete(timer_id));
641}
642
Yabin Cui95f1ee22015-01-13 19:53:15 -0800643static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800644static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
645 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
646 ASSERT_EQ(SIGUSR1, signal_number);
647}
648
649TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700650 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800651 memset(&se, 0, sizeof(se));
652 se.sigev_notify = SIGEV_SIGNAL;
653 se.sigev_signo = SIGUSR1;
654
655 timer_t timer_id;
656 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
657
Yabin Cui95f1ee22015-01-13 19:53:15 -0800658 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800659 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
660
661 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
662
663 itimerspec ts;
664 ts.it_value.tv_sec = 0;
665 ts.it_value.tv_nsec = 1;
666 ts.it_interval.tv_sec = 0;
667 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700668 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800669
670 usleep(500000);
671 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
672}
673
674struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800675 private:
676 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800677 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700678 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700679 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800680
Elliott Hughes4b558f52014-03-04 15:58:02 -0800681 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700682 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800683 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700684 timer_valid = true;
685 }
686
Yabin Cui95f1ee22015-01-13 19:53:15 -0800687 public:
Colin Cross7da20342021-07-28 11:18:11 -0700688 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800689 memset(&se, 0, sizeof(se));
690 se.sigev_notify = SIGEV_THREAD;
691 se.sigev_notify_function = fn;
692 se.sigev_value.sival_ptr = this;
693 Create();
694 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700695 void DeleteTimer() {
696 ASSERT_TRUE(timer_valid);
697 ASSERT_EQ(0, timer_delete(timer_id));
698 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800699 }
700
701 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700702 if (timer_valid) {
703 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800704 }
705 }
706
Yabin Cui95f1ee22015-01-13 19:53:15 -0800707 int Value() const {
708 return value;
709 }
710
Christopher Ferris62d84b12014-10-20 19:09:19 -0700711 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
712 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
713 }
714
715 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800716 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700717 time_t start = time(nullptr);
718 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700719 }
720 return current_value != value;
721 }
722
Colin Cross7da20342021-07-28 11:18:11 -0700723 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800724 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
725 ++cd->value;
726 }
727
Colin Cross7da20342021-07-28 11:18:11 -0700728 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800729 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
730 ++cd->value;
731
732 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700733 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800734 }
735};
736
737TEST(time, timer_settime_0) {
738 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800739 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800740
Yabin Cuibf572d92015-08-11 11:23:16 -0700741 counter.SetTime(0, 500000000, 1, 0);
742 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800743
744 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800745 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800746}
747
748TEST(time, timer_settime_repeats) {
749 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800750 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800751
Christopher Ferris62d84b12014-10-20 19:09:19 -0700752 counter.SetTime(0, 1, 0, 10);
753 ASSERT_TRUE(counter.ValueUpdated());
754 ASSERT_TRUE(counter.ValueUpdated());
755 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800756 counter.DeleteTimer();
757 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
758 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800759}
760
Yabin Cui95f1ee22015-01-13 19:53:15 -0800761static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800762static void timer_create_NULL_signal_handler(int signal_number) {
763 ++timer_create_NULL_signal_handler_invocation_count;
764 ASSERT_EQ(SIGALRM, signal_number);
765}
766
767TEST(time, timer_create_NULL) {
768 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
769 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700770 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800771
Yabin Cui95f1ee22015-01-13 19:53:15 -0800772 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800773 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
774
775 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
776
777 SetTime(timer_id, 0, 1, 0, 0);
778 usleep(500000);
779
780 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
781}
782
783TEST(time, timer_create_EINVAL) {
784 clockid_t invalid_clock = 16;
785
786 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
787 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700788 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800789 ASSERT_EQ(EINVAL, errno);
790
791 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700792 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800793 memset(&se, 0, sizeof(se));
794 se.sigev_notify = SIGEV_THREAD;
795 se.sigev_notify_function = NoOpNotifyFunction;
796 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
797 ASSERT_EQ(EINVAL, errno);
798}
799
Elliott Hughes4b558f52014-03-04 15:58:02 -0800800TEST(time, timer_create_multiple) {
801 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800802 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800803 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800804
Yabin Cui95f1ee22015-01-13 19:53:15 -0800805 ASSERT_EQ(0, counter1.Value());
806 ASSERT_EQ(0, counter2.Value());
807 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800808
Yabin Cui410c1ad2015-06-18 16:19:02 -0700809 counter2.SetTime(0, 500000000, 0, 0);
810 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800811
Yabin Cui95f1ee22015-01-13 19:53:15 -0800812 EXPECT_EQ(0, counter1.Value());
813 EXPECT_EQ(1, counter2.Value());
814 EXPECT_EQ(0, counter3.Value());
815}
816
817// Test to verify that disarming a repeatable timer disables the callbacks.
818TEST(time, timer_disarm_terminates) {
819 Counter counter(Counter::CountNotifyFunction);
820 ASSERT_EQ(0, counter.Value());
821
822 counter.SetTime(0, 1, 0, 1);
823 ASSERT_TRUE(counter.ValueUpdated());
824 ASSERT_TRUE(counter.ValueUpdated());
825 ASSERT_TRUE(counter.ValueUpdated());
826
827 counter.SetTime(0, 0, 0, 0);
828 // Add a sleep as the kernel may have pending events when the timer is disarmed.
829 usleep(500000);
830 int value = counter.Value();
831 usleep(500000);
832
833 // Verify the counter has not been incremented.
834 ASSERT_EQ(value, counter.Value());
835}
836
837// Test to verify that deleting a repeatable timer disables the callbacks.
838TEST(time, timer_delete_terminates) {
839 Counter counter(Counter::CountNotifyFunction);
840 ASSERT_EQ(0, counter.Value());
841
842 counter.SetTime(0, 1, 0, 1);
843 ASSERT_TRUE(counter.ValueUpdated());
844 ASSERT_TRUE(counter.ValueUpdated());
845 ASSERT_TRUE(counter.ValueUpdated());
846
847 counter.DeleteTimer();
848 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
849 usleep(500000);
850 int value = counter.Value();
851 usleep(500000);
852
853 // Verify the counter has not been incremented.
854 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800855}
Christopher Ferris753ad772014-03-20 20:47:45 -0700856
857struct TimerDeleteData {
858 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800859 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700860 volatile bool complete;
861};
862
Colin Cross7da20342021-07-28 11:18:11 -0700863static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700864 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
865
Elliott Hughes11859d42017-02-13 17:59:29 -0800866 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700867 timer_delete(tdd->timer_id);
868 tdd->complete = true;
869}
870
871TEST(time, timer_delete_from_timer_thread) {
872 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700873 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700874
875 memset(&se, 0, sizeof(se));
876 se.sigev_notify = SIGEV_THREAD;
877 se.sigev_notify_function = TimerDeleteCallback;
878 se.sigev_value.sival_ptr = &tdd;
879
880 tdd.complete = false;
881 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
882
883 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800884 ts.it_value.tv_sec = 1;
885 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700886 ts.it_interval.tv_sec = 0;
887 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700888 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700889
Yi Kong32bc0fc2018-08-02 17:31:13 -0700890 time_t cur_time = time(nullptr);
891 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700892 ASSERT_TRUE(tdd.complete);
893
894#if defined(__BIONIC__)
895 // Since bionic timers are implemented by creating a thread to handle the
896 // callback, verify that the thread actually completes.
897 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800898 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
899 ASSERT_EQ(-1, kill(tdd.tid, 0));
900 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700901#endif
902}
Elliott Hughes625993d2014-07-15 16:53:13 -0700903
Colin Cross35d469b2021-08-16 15:26:28 -0700904// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
905#if !defined(__NR_clock_gettime)
906#define __NR_clock_gettime __NR_clock_gettime32
907#endif
908
Elliott Hughes625993d2014-07-15 16:53:13 -0700909TEST(time, clock_gettime) {
910 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100911 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700912 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700913 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100914 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
915 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
916 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700917
Giuliano Procida096f5952021-04-08 10:51:58 +0100918 // Check we have a nice monotonic timestamp sandwich.
919 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
920 if (ts0.tv_sec == ts1.tv_sec) {
921 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700922 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100923 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
924 if (ts1.tv_sec == ts2.tv_sec) {
925 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
926 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700927}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700928
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700929TEST(time, clock_gettime_CLOCK_REALTIME) {
930 timespec ts;
931 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
932}
933
934TEST(time, clock_gettime_CLOCK_MONOTONIC) {
935 timespec ts;
936 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
937}
938
939TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
940 timespec ts;
941 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
942}
943
944TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
945 timespec ts;
946 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
947}
948
949TEST(time, clock_gettime_CLOCK_BOOTTIME) {
950 timespec ts;
951 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
952}
953
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800954TEST(time, clock_gettime_unknown) {
955 errno = 0;
956 timespec ts;
957 ASSERT_EQ(-1, clock_gettime(-1, &ts));
958 ASSERT_EQ(EINVAL, errno);
959}
960
961TEST(time, clock_getres_CLOCK_REALTIME) {
962 timespec ts;
963 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
964 ASSERT_EQ(1, ts.tv_nsec);
965 ASSERT_EQ(0, ts.tv_sec);
966}
967
968TEST(time, clock_getres_CLOCK_MONOTONIC) {
969 timespec ts;
970 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
971 ASSERT_EQ(1, ts.tv_nsec);
972 ASSERT_EQ(0, ts.tv_sec);
973}
974
975TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
976 timespec ts;
977 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
978}
979
980TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
981 timespec ts;
982 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
983}
984
985TEST(time, clock_getres_CLOCK_BOOTTIME) {
986 timespec ts;
987 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
988 ASSERT_EQ(1, ts.tv_nsec);
989 ASSERT_EQ(0, ts.tv_sec);
990}
991
992TEST(time, clock_getres_unknown) {
993 errno = 0;
994 timespec ts = { -1, -1 };
995 ASSERT_EQ(-1, clock_getres(-1, &ts));
996 ASSERT_EQ(EINVAL, errno);
997 ASSERT_EQ(-1, ts.tv_nsec);
998 ASSERT_EQ(-1, ts.tv_sec);
999}
1000
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001001TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001002 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
1003 static const clock_t N = 5;
1004 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +09001005 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001006 for (size_t i = 0; i < N; ++i) {
1007 sleep(1);
1008 }
Haruki Hasegawa18160252014-10-13 00:50:47 +09001009 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +01001010 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +09001011}
1012
Elliott Hughesb4413592017-11-29 18:17:06 -08001013static pid_t GetInvalidPid() {
1014 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -08001015 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -08001016 fscanf(fp.get(), "%ld", &pid_max);
1017 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -08001018}
1019
Elliott Hughesb4413592017-11-29 18:17:06 -08001020TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001021 clockid_t clockid;
1022 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -08001023 timespec ts;
1024 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001025}
Yabin Cuid5c65272014-11-26 14:04:26 -08001026
Elliott Hughesb4413592017-11-29 18:17:06 -08001027TEST(time, clock_getcpuclockid_parent) {
1028 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -08001029 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -08001030 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -08001031 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -08001032}
Yabin Cuid5c65272014-11-26 14:04:26 -08001033
Elliott Hughesb4413592017-11-29 18:17:06 -08001034TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -08001035 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
1036 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001037 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -08001038 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -08001039 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
1040 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
1041 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1042 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
1043 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
1044 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -08001045 ASSERT_EQ(0, errno);
1046}
1047
Haruki Hasegawa18160252014-10-13 00:50:47 +09001048TEST(time, clock_settime) {
1049 errno = 0;
1050 timespec ts;
1051 ASSERT_EQ(-1, clock_settime(-1, &ts));
1052 ASSERT_EQ(EINVAL, errno);
1053}
1054
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001055TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +09001056 timespec in;
1057 timespec out;
1058 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -07001059}
Greg Hackmannd15dfb22016-03-26 11:37:55 -07001060
1061TEST(time, clock_nanosleep_thread_cputime_id) {
1062 timespec in;
1063 in.tv_sec = 1;
1064 in.tv_nsec = 0;
1065 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
1066}
Elliott Hughes12443702016-10-19 16:02:31 -07001067
Elliott Hughesca3f8e42019-10-28 15:59:38 -07001068TEST(time, clock_nanosleep) {
1069 auto t0 = std::chrono::steady_clock::now();
1070 const timespec ts = {.tv_nsec = 5000000};
1071 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
1072 auto t1 = std::chrono::steady_clock::now();
1073 ASSERT_GE(t1-t0, 5000000ns);
1074}
1075
1076TEST(time, nanosleep) {
1077 auto t0 = std::chrono::steady_clock::now();
1078 const timespec ts = {.tv_nsec = 5000000};
1079 ASSERT_EQ(0, nanosleep(&ts, nullptr));
1080 auto t1 = std::chrono::steady_clock::now();
1081 ASSERT_GE(t1-t0, 5000000ns);
1082}
1083
1084TEST(time, nanosleep_EINVAL) {
1085 timespec ts = {.tv_sec = -1};
1086 errno = 0;
1087 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
1088 ASSERT_EQ(EINVAL, errno);
1089}
1090
Elliott Hughes12443702016-10-19 16:02:31 -07001091TEST(time, bug_31938693) {
1092 // User-visible symptoms in N:
1093 // http://b/31938693
1094 // https://code.google.com/p/android/issues/detail?id=225132
1095
1096 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
1097 // http://b/31848040
1098
1099 // This isn't a great test, because very few time zones were actually affected, and there's
1100 // no real logic to which ones were affected: it was just a coincidence of the data that came
1101 // after them in the tzdata file.
1102
1103 time_t t = 1475619727;
1104 struct tm tm;
1105
1106 setenv("TZ", "America/Los_Angeles", 1);
1107 tzset();
1108 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1109 EXPECT_EQ(15, tm.tm_hour);
1110
1111 setenv("TZ", "Europe/London", 1);
1112 tzset();
1113 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1114 EXPECT_EQ(23, tm.tm_hour);
1115
1116 setenv("TZ", "America/Atka", 1);
1117 tzset();
1118 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1119 EXPECT_EQ(13, tm.tm_hour);
1120
1121 setenv("TZ", "Pacific/Apia", 1);
1122 tzset();
1123 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1124 EXPECT_EQ(12, tm.tm_hour);
1125
1126 setenv("TZ", "Pacific/Honolulu", 1);
1127 tzset();
1128 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1129 EXPECT_EQ(12, tm.tm_hour);
1130
1131 setenv("TZ", "Asia/Magadan", 1);
1132 tzset();
1133 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
1134 EXPECT_EQ(9, tm.tm_hour);
1135}
Elliott Hughesea877162017-01-11 14:34:16 -08001136
1137TEST(time, bug_31339449) {
1138 // POSIX says localtime acts as if it calls tzset.
1139 // tzset does two things:
1140 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
1141 // 2. it sets the global `tzname`.
1142 // POSIX says localtime_r need not set `tzname` (2).
1143 // Q: should localtime_r set the time zone (1)?
1144 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1145
1146 // Pick a time, any time...
1147 time_t t = 1475619727;
1148
1149 // Call tzset with a specific timezone.
1150 setenv("TZ", "America/Atka", 1);
1151 tzset();
1152
1153 // If we change the timezone and call localtime, localtime should use the new timezone.
1154 setenv("TZ", "America/Los_Angeles", 1);
1155 struct tm* tm_p = localtime(&t);
1156 EXPECT_EQ(15, tm_p->tm_hour);
1157
1158 // Reset the timezone back.
1159 setenv("TZ", "America/Atka", 1);
1160 tzset();
1161
1162#if defined(__BIONIC__)
1163 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1164 setenv("TZ", "America/Los_Angeles", 1);
1165 struct tm tm = {};
1166 localtime_r(&t, &tm);
1167 EXPECT_EQ(15, tm.tm_hour);
1168#else
1169 // The BSDs agree with us, but glibc gets this wrong.
1170#endif
1171}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001172
1173TEST(time, asctime) {
1174 const struct tm tm = {};
1175 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1176}
1177
1178TEST(time, asctime_r) {
1179 const struct tm tm = {};
1180 char buf[256];
1181 ASSERT_EQ(buf, asctime_r(&tm, buf));
1182 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1183}
1184
1185TEST(time, ctime) {
1186 setenv("TZ", "UTC", 1);
1187 const time_t t = 0;
1188 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1189}
1190
1191TEST(time, ctime_r) {
1192 setenv("TZ", "UTC", 1);
1193 const time_t t = 0;
1194 char buf[256];
1195 ASSERT_EQ(buf, ctime_r(&t, buf));
1196 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1197}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001198
1199// https://issuetracker.google.com/37128336
1200TEST(time, strftime_strptime_s) {
1201 char buf[32];
1202 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1203
1204 setenv("TZ", "America/Los_Angeles", 1);
1205 strftime(buf, sizeof(buf), "<%s>", &tm0);
1206 EXPECT_STREQ("<378720000>", buf);
1207
1208 setenv("TZ", "UTC", 1);
1209 strftime(buf, sizeof(buf), "<%s>", &tm0);
1210 EXPECT_STREQ("<378691200>", buf);
1211
1212 struct tm tm;
1213
1214 setenv("TZ", "America/Los_Angeles", 1);
1215 tzset();
1216 memset(&tm, 0xff, sizeof(tm));
1217 char* p = strptime("378720000x", "%s", &tm);
1218 ASSERT_EQ('x', *p);
1219 EXPECT_EQ(0, tm.tm_sec);
1220 EXPECT_EQ(0, tm.tm_min);
1221 EXPECT_EQ(0, tm.tm_hour);
1222 EXPECT_EQ(1, tm.tm_mday);
1223 EXPECT_EQ(0, tm.tm_mon);
1224 EXPECT_EQ(82, tm.tm_year);
1225 EXPECT_EQ(5, tm.tm_wday);
1226 EXPECT_EQ(0, tm.tm_yday);
1227 EXPECT_EQ(0, tm.tm_isdst);
1228
1229 setenv("TZ", "UTC", 1);
1230 tzset();
1231 memset(&tm, 0xff, sizeof(tm));
1232 p = strptime("378691200x", "%s", &tm);
1233 ASSERT_EQ('x', *p);
1234 EXPECT_EQ(0, tm.tm_sec);
1235 EXPECT_EQ(0, tm.tm_min);
1236 EXPECT_EQ(0, tm.tm_hour);
1237 EXPECT_EQ(1, tm.tm_mday);
1238 EXPECT_EQ(0, tm.tm_mon);
1239 EXPECT_EQ(82, tm.tm_year);
1240 EXPECT_EQ(5, tm.tm_wday);
1241 EXPECT_EQ(0, tm.tm_yday);
1242 EXPECT_EQ(0, tm.tm_isdst);
1243}
1244
1245TEST(time, strptime_s_nothing) {
1246 struct tm tm;
1247 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1248}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001249
1250TEST(time, timespec_get) {
1251#if __BIONIC__
1252 timespec ts = {};
1253 ASSERT_EQ(0, timespec_get(&ts, 123));
1254 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1255#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001256 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001257#endif
1258}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001259
1260TEST(time, difftime) {
1261 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001262 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001263}