blob: 5fc4cad31ef0a9689b9093ff815e710ad79d4791 [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
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700187TEST(time, strftime) {
188 setenv("TZ", "UTC", 1);
189
190 struct tm t;
191 memset(&t, 0, sizeof(tm));
192 t.tm_year = 200;
193 t.tm_mon = 2;
194 t.tm_mday = 10;
195
196 char buf[64];
197
198 // Seconds since the epoch.
199#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
200 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
201 EXPECT_STREQ("4108320000", buf);
202#endif
203
204 // Date and time as text.
205 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
206 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
207}
208
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800209TEST(time, strftime_null_tm_zone) {
210 // Netflix on Nexus Player wouldn't start (http://b/25170306).
211 struct tm t;
212 memset(&t, 0, sizeof(tm));
213
214 char buf[64];
215
216 setenv("TZ", "America/Los_Angeles", 1);
217 tzset();
218
219 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
220 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
221 EXPECT_STREQ("<PST>", buf);
222
223#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
224 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
225 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
226 EXPECT_STREQ("<PDT>", buf);
227
228 t.tm_isdst = -123; // "and negative if the information is not available".
229 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
230 EXPECT_STREQ("<>", buf);
231#endif
232
233 setenv("TZ", "UTC", 1);
234 tzset();
235
236 t.tm_isdst = 0;
237 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
238 EXPECT_STREQ("<UTC>", buf);
239
240#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
241 t.tm_isdst = 1; // UTC has no DST.
242 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
243 EXPECT_STREQ("<>", buf);
244#endif
245}
246
Elliott Hughes0a610d02016-07-29 14:04:17 -0700247TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700248 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700249 locale_t old_locale = uselocale(cloc);
250
251 setenv("TZ", "UTC", 1);
252
253 struct tm t;
254 memset(&t, 0, sizeof(tm));
255 t.tm_year = 200;
256 t.tm_mon = 2;
257 t.tm_mday = 10;
258
259 // Date and time as text.
260 char buf[64];
261 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
262 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
263
264 uselocale(old_locale);
265 freelocale(cloc);
266}
267
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700268TEST(time, strptime) {
269 setenv("TZ", "UTC", 1);
270
271 struct tm t;
272 char buf[64];
273
274 memset(&t, 0, sizeof(t));
275 strptime("11:14", "%R", &t);
276 strftime(buf, sizeof(buf), "%H:%M", &t);
277 EXPECT_STREQ("11:14", buf);
278
279 memset(&t, 0, sizeof(t));
280 strptime("09:41:53", "%T", &t);
281 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
282 EXPECT_STREQ("09:41:53", buf);
283}
284
Elliott Hughes3376c232018-02-13 23:14:12 -0800285TEST(time, strptime_l) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700286#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800287 setenv("TZ", "UTC", 1);
288
289 struct tm t;
290 char buf[64];
291
292 memset(&t, 0, sizeof(t));
293 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
294 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
295 EXPECT_STREQ("11:14", buf);
296
297 memset(&t, 0, sizeof(t));
298 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
299 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
300 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700301#else
302 GTEST_SKIP() << "musl doesn't support strptime_l";
303#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800304}
305
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700306TEST(time, strptime_F) {
307 setenv("TZ", "UTC", 1);
308
309 struct tm tm = {};
310 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
311 EXPECT_EQ(119, tm.tm_year);
312 EXPECT_EQ(2, tm.tm_mon);
313 EXPECT_EQ(26, tm.tm_mday);
314}
315
316TEST(time, strptime_P_p) {
317 setenv("TZ", "UTC", 1);
318
Elliott Hughes11678822019-03-27 08:56:49 -0700319 // For parsing, %P and %p are the same: case doesn't matter.
320
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700321 struct tm tm = {.tm_hour = 12};
322 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
323 EXPECT_EQ(0, tm.tm_hour);
324
325 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700326 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
327 EXPECT_EQ(0, tm.tm_hour);
328
329 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700330 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
331 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700332
333 tm = {.tm_hour = 12};
334 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
335 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700336}
337
338TEST(time, strptime_u) {
339 setenv("TZ", "UTC", 1);
340
341 struct tm tm = {};
342 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
343 EXPECT_EQ(2, tm.tm_wday);
344}
345
346TEST(time, strptime_v) {
347 setenv("TZ", "UTC", 1);
348
349 struct tm tm = {};
350 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
351 EXPECT_EQ(80, tm.tm_year);
352 EXPECT_EQ(2, tm.tm_mon);
353 EXPECT_EQ(26, tm.tm_mday);
354}
355
356TEST(time, strptime_V_G_g) {
357 setenv("TZ", "UTC", 1);
358
359 // %V (ISO-8601 week number), %G (year of week number, without century), and
360 // %g (year of week number) have no effect when parsed, and are supported
361 // solely so that it's possible for strptime(3) to parse everything that
362 // strftime(3) can output.
363 struct tm tm = {};
364 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
365 struct tm zero = {};
366 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
367}
368
Elliott Hughesd065c042020-09-01 19:02:44 -0700369TEST(time, strptime_Z) {
370#if defined(__BIONIC__)
371 // glibc doesn't handle %Z at all.
372 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
373 // are in the global `tzname` (which correspond to the current $TZ).
374 struct tm tm;
375 setenv("TZ", "Europe/Berlin", 1);
376
377 // "GMT" always works.
378 tm = {};
379 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
380 EXPECT_STREQ("GMT", tm.tm_zone);
381 EXPECT_EQ(0, tm.tm_isdst);
382 EXPECT_EQ(0, tm.tm_gmtoff);
383
384 // As does "UTC".
385 tm = {};
386 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
387 EXPECT_STREQ("UTC", tm.tm_zone);
388 EXPECT_EQ(0, tm.tm_isdst);
389 EXPECT_EQ(0, tm.tm_gmtoff);
390
391 // Europe/Berlin is known as "CET" when there's no DST.
392 tm = {};
393 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
394 EXPECT_STREQ("CET", tm.tm_zone);
395 EXPECT_EQ(0, tm.tm_isdst);
396 EXPECT_EQ(3600, tm.tm_gmtoff);
397
398 // Europe/Berlin is known as "CEST" when there's no DST.
399 tm = {};
400 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
401 EXPECT_STREQ("CEST", tm.tm_zone);
402 EXPECT_EQ(1, tm.tm_isdst);
403 EXPECT_EQ(3600, tm.tm_gmtoff);
404
405 // And as long as we're in Europe/Berlin, those are the only time zone
406 // abbreviations that are recognized.
407 tm = {};
408 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
409#endif
410}
411
412TEST(time, strptime_z) {
413 struct tm tm;
414 setenv("TZ", "Europe/Berlin", 1);
415
416 // "UT" is what RFC822 called UTC.
417 tm = {};
418 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
419 EXPECT_STREQ("UTC", tm.tm_zone);
420 EXPECT_EQ(0, tm.tm_isdst);
421 EXPECT_EQ(0, tm.tm_gmtoff);
422 // "GMT" is RFC822's other name for UTC.
423 tm = {};
424 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
425 EXPECT_STREQ("UTC", tm.tm_zone);
426 EXPECT_EQ(0, tm.tm_isdst);
427 EXPECT_EQ(0, tm.tm_gmtoff);
428
429 // "Z" ("Zulu") is a synonym for UTC.
430 tm = {};
431 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
432 EXPECT_STREQ("UTC", tm.tm_zone);
433 EXPECT_EQ(0, tm.tm_isdst);
434 EXPECT_EQ(0, tm.tm_gmtoff);
435
436 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
437 tm = {};
438 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
439 EXPECT_STREQ("PST", tm.tm_zone);
440 EXPECT_EQ(0, tm.tm_isdst);
441 EXPECT_EQ(-28800, tm.tm_gmtoff);
442 tm = {};
443 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
444 EXPECT_STREQ("PDT", tm.tm_zone);
445 EXPECT_EQ(1, tm.tm_isdst);
446 EXPECT_EQ(-25200, tm.tm_gmtoff);
447
448 // +-hh
449 tm = {};
450 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
451 EXPECT_EQ(3600, tm.tm_gmtoff);
452 EXPECT_TRUE(tm.tm_zone == nullptr);
453 EXPECT_EQ(0, tm.tm_isdst);
454 // +-hhmm
455 tm = {};
456 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
457 EXPECT_EQ(5400, tm.tm_gmtoff);
458 EXPECT_TRUE(tm.tm_zone == nullptr);
459 EXPECT_EQ(0, tm.tm_isdst);
460 // +-hh:mm
461 tm = {};
462 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
463 EXPECT_EQ(5400, tm.tm_gmtoff);
464 EXPECT_TRUE(tm.tm_zone == nullptr);
465 EXPECT_EQ(0, tm.tm_isdst);
466}
467
Elliott Hughes4b558f52014-03-04 15:58:02 -0800468void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
469 itimerspec ts;
470 ts.it_value.tv_sec = value_s;
471 ts.it_value.tv_nsec = value_ns;
472 ts.it_interval.tv_sec = interval_s;
473 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700474 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800475}
476
Colin Cross7da20342021-07-28 11:18:11 -0700477static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800478}
479
480TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700481 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800482 memset(&se, 0, sizeof(se));
483 se.sigev_notify = SIGEV_THREAD;
484 se.sigev_notify_function = NoOpNotifyFunction;
485 timer_t timer_id;
486 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
487
Elliott Hughes33697a02016-01-26 13:04:57 -0800488 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800489 ASSERT_NE(-1, pid) << strerror(errno);
490
491 if (pid == 0) {
492 // Timers are not inherited by the child.
493 ASSERT_EQ(-1, timer_delete(timer_id));
494 ASSERT_EQ(EINVAL, errno);
495 _exit(0);
496 }
497
Elliott Hughes33697a02016-01-26 13:04:57 -0800498 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800499
500 ASSERT_EQ(0, timer_delete(timer_id));
501}
502
Yabin Cui95f1ee22015-01-13 19:53:15 -0800503static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800504static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
505 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
506 ASSERT_EQ(SIGUSR1, signal_number);
507}
508
509TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700510 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800511 memset(&se, 0, sizeof(se));
512 se.sigev_notify = SIGEV_SIGNAL;
513 se.sigev_signo = SIGUSR1;
514
515 timer_t timer_id;
516 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
517
Yabin Cui95f1ee22015-01-13 19:53:15 -0800518 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800519 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
520
521 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
522
523 itimerspec ts;
524 ts.it_value.tv_sec = 0;
525 ts.it_value.tv_nsec = 1;
526 ts.it_interval.tv_sec = 0;
527 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700528 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800529
530 usleep(500000);
531 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
532}
533
534struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800535 private:
536 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800537 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700538 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700539 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800540
Elliott Hughes4b558f52014-03-04 15:58:02 -0800541 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700542 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800543 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700544 timer_valid = true;
545 }
546
Yabin Cui95f1ee22015-01-13 19:53:15 -0800547 public:
Colin Cross7da20342021-07-28 11:18:11 -0700548 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800549 memset(&se, 0, sizeof(se));
550 se.sigev_notify = SIGEV_THREAD;
551 se.sigev_notify_function = fn;
552 se.sigev_value.sival_ptr = this;
553 Create();
554 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700555 void DeleteTimer() {
556 ASSERT_TRUE(timer_valid);
557 ASSERT_EQ(0, timer_delete(timer_id));
558 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800559 }
560
561 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700562 if (timer_valid) {
563 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800564 }
565 }
566
Yabin Cui95f1ee22015-01-13 19:53:15 -0800567 int Value() const {
568 return value;
569 }
570
Christopher Ferris62d84b12014-10-20 19:09:19 -0700571 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
572 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
573 }
574
575 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800576 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700577 time_t start = time(nullptr);
578 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700579 }
580 return current_value != value;
581 }
582
Colin Cross7da20342021-07-28 11:18:11 -0700583 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800584 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
585 ++cd->value;
586 }
587
Colin Cross7da20342021-07-28 11:18:11 -0700588 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800589 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
590 ++cd->value;
591
592 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700593 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800594 }
595};
596
597TEST(time, timer_settime_0) {
598 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800599 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800600
Yabin Cuibf572d92015-08-11 11:23:16 -0700601 counter.SetTime(0, 500000000, 1, 0);
602 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800603
604 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800605 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800606}
607
608TEST(time, timer_settime_repeats) {
609 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800610 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800611
Christopher Ferris62d84b12014-10-20 19:09:19 -0700612 counter.SetTime(0, 1, 0, 10);
613 ASSERT_TRUE(counter.ValueUpdated());
614 ASSERT_TRUE(counter.ValueUpdated());
615 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800616 counter.DeleteTimer();
617 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
618 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800619}
620
Yabin Cui95f1ee22015-01-13 19:53:15 -0800621static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800622static void timer_create_NULL_signal_handler(int signal_number) {
623 ++timer_create_NULL_signal_handler_invocation_count;
624 ASSERT_EQ(SIGALRM, signal_number);
625}
626
627TEST(time, timer_create_NULL) {
628 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
629 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700630 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800631
Yabin Cui95f1ee22015-01-13 19:53:15 -0800632 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800633 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
634
635 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
636
637 SetTime(timer_id, 0, 1, 0, 0);
638 usleep(500000);
639
640 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
641}
642
643TEST(time, timer_create_EINVAL) {
644 clockid_t invalid_clock = 16;
645
646 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
647 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700648 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800649 ASSERT_EQ(EINVAL, errno);
650
651 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700652 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800653 memset(&se, 0, sizeof(se));
654 se.sigev_notify = SIGEV_THREAD;
655 se.sigev_notify_function = NoOpNotifyFunction;
656 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
657 ASSERT_EQ(EINVAL, errno);
658}
659
Elliott Hughes4b558f52014-03-04 15:58:02 -0800660TEST(time, timer_create_multiple) {
661 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800662 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800663 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800664
Yabin Cui95f1ee22015-01-13 19:53:15 -0800665 ASSERT_EQ(0, counter1.Value());
666 ASSERT_EQ(0, counter2.Value());
667 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800668
Yabin Cui410c1ad2015-06-18 16:19:02 -0700669 counter2.SetTime(0, 500000000, 0, 0);
670 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800671
Yabin Cui95f1ee22015-01-13 19:53:15 -0800672 EXPECT_EQ(0, counter1.Value());
673 EXPECT_EQ(1, counter2.Value());
674 EXPECT_EQ(0, counter3.Value());
675}
676
677// Test to verify that disarming a repeatable timer disables the callbacks.
678TEST(time, timer_disarm_terminates) {
679 Counter counter(Counter::CountNotifyFunction);
680 ASSERT_EQ(0, counter.Value());
681
682 counter.SetTime(0, 1, 0, 1);
683 ASSERT_TRUE(counter.ValueUpdated());
684 ASSERT_TRUE(counter.ValueUpdated());
685 ASSERT_TRUE(counter.ValueUpdated());
686
687 counter.SetTime(0, 0, 0, 0);
688 // Add a sleep as the kernel may have pending events when the timer is disarmed.
689 usleep(500000);
690 int value = counter.Value();
691 usleep(500000);
692
693 // Verify the counter has not been incremented.
694 ASSERT_EQ(value, counter.Value());
695}
696
697// Test to verify that deleting a repeatable timer disables the callbacks.
698TEST(time, timer_delete_terminates) {
699 Counter counter(Counter::CountNotifyFunction);
700 ASSERT_EQ(0, counter.Value());
701
702 counter.SetTime(0, 1, 0, 1);
703 ASSERT_TRUE(counter.ValueUpdated());
704 ASSERT_TRUE(counter.ValueUpdated());
705 ASSERT_TRUE(counter.ValueUpdated());
706
707 counter.DeleteTimer();
708 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
709 usleep(500000);
710 int value = counter.Value();
711 usleep(500000);
712
713 // Verify the counter has not been incremented.
714 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800715}
Christopher Ferris753ad772014-03-20 20:47:45 -0700716
717struct TimerDeleteData {
718 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800719 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700720 volatile bool complete;
721};
722
Colin Cross7da20342021-07-28 11:18:11 -0700723static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700724 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
725
Elliott Hughes11859d42017-02-13 17:59:29 -0800726 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700727 timer_delete(tdd->timer_id);
728 tdd->complete = true;
729}
730
731TEST(time, timer_delete_from_timer_thread) {
732 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700733 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700734
735 memset(&se, 0, sizeof(se));
736 se.sigev_notify = SIGEV_THREAD;
737 se.sigev_notify_function = TimerDeleteCallback;
738 se.sigev_value.sival_ptr = &tdd;
739
740 tdd.complete = false;
741 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
742
743 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800744 ts.it_value.tv_sec = 1;
745 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700746 ts.it_interval.tv_sec = 0;
747 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700748 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700749
Yi Kong32bc0fc2018-08-02 17:31:13 -0700750 time_t cur_time = time(nullptr);
751 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700752 ASSERT_TRUE(tdd.complete);
753
754#if defined(__BIONIC__)
755 // Since bionic timers are implemented by creating a thread to handle the
756 // callback, verify that the thread actually completes.
757 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800758 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
759 ASSERT_EQ(-1, kill(tdd.tid, 0));
760 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700761#endif
762}
Elliott Hughes625993d2014-07-15 16:53:13 -0700763
Colin Cross35d469b2021-08-16 15:26:28 -0700764// Musl doesn't define __NR_clock_gettime on 32-bit architectures.
765#if !defined(__NR_clock_gettime)
766#define __NR_clock_gettime __NR_clock_gettime32
767#endif
768
Elliott Hughes625993d2014-07-15 16:53:13 -0700769TEST(time, clock_gettime) {
770 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100771 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700772 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700773 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100774 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
775 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
776 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700777
Giuliano Procida096f5952021-04-08 10:51:58 +0100778 // Check we have a nice monotonic timestamp sandwich.
779 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
780 if (ts0.tv_sec == ts1.tv_sec) {
781 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700782 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100783 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
784 if (ts1.tv_sec == ts2.tv_sec) {
785 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
786 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700787}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700788
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700789TEST(time, clock_gettime_CLOCK_REALTIME) {
790 timespec ts;
791 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
792}
793
794TEST(time, clock_gettime_CLOCK_MONOTONIC) {
795 timespec ts;
796 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
797}
798
799TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
800 timespec ts;
801 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
802}
803
804TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
805 timespec ts;
806 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
807}
808
809TEST(time, clock_gettime_CLOCK_BOOTTIME) {
810 timespec ts;
811 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
812}
813
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800814TEST(time, clock_gettime_unknown) {
815 errno = 0;
816 timespec ts;
817 ASSERT_EQ(-1, clock_gettime(-1, &ts));
818 ASSERT_EQ(EINVAL, errno);
819}
820
821TEST(time, clock_getres_CLOCK_REALTIME) {
822 timespec ts;
823 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
824 ASSERT_EQ(1, ts.tv_nsec);
825 ASSERT_EQ(0, ts.tv_sec);
826}
827
828TEST(time, clock_getres_CLOCK_MONOTONIC) {
829 timespec ts;
830 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
831 ASSERT_EQ(1, ts.tv_nsec);
832 ASSERT_EQ(0, ts.tv_sec);
833}
834
835TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
836 timespec ts;
837 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
838}
839
840TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
841 timespec ts;
842 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
843}
844
845TEST(time, clock_getres_CLOCK_BOOTTIME) {
846 timespec ts;
847 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
848 ASSERT_EQ(1, ts.tv_nsec);
849 ASSERT_EQ(0, ts.tv_sec);
850}
851
852TEST(time, clock_getres_unknown) {
853 errno = 0;
854 timespec ts = { -1, -1 };
855 ASSERT_EQ(-1, clock_getres(-1, &ts));
856 ASSERT_EQ(EINVAL, errno);
857 ASSERT_EQ(-1, ts.tv_nsec);
858 ASSERT_EQ(-1, ts.tv_sec);
859}
860
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700861TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100862 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
863 static const clock_t N = 5;
864 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +0900865 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100866 for (size_t i = 0; i < N; ++i) {
867 sleep(1);
868 }
Haruki Hasegawa18160252014-10-13 00:50:47 +0900869 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100870 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +0900871}
872
Elliott Hughesb4413592017-11-29 18:17:06 -0800873static pid_t GetInvalidPid() {
874 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -0800875 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -0800876 fscanf(fp.get(), "%ld", &pid_max);
877 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -0800878}
879
Elliott Hughesb4413592017-11-29 18:17:06 -0800880TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800881 clockid_t clockid;
882 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -0800883 timespec ts;
884 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800885}
Yabin Cuid5c65272014-11-26 14:04:26 -0800886
Elliott Hughesb4413592017-11-29 18:17:06 -0800887TEST(time, clock_getcpuclockid_parent) {
888 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -0800889 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -0800890 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -0800891 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800892}
Yabin Cuid5c65272014-11-26 14:04:26 -0800893
Elliott Hughesb4413592017-11-29 18:17:06 -0800894TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800895 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
896 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800897 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -0800898 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800899 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
900 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
901 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
902 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
903 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
904 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -0800905 ASSERT_EQ(0, errno);
906}
907
Haruki Hasegawa18160252014-10-13 00:50:47 +0900908TEST(time, clock_settime) {
909 errno = 0;
910 timespec ts;
911 ASSERT_EQ(-1, clock_settime(-1, &ts));
912 ASSERT_EQ(EINVAL, errno);
913}
914
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700915TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900916 timespec in;
917 timespec out;
918 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700919}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700920
921TEST(time, clock_nanosleep_thread_cputime_id) {
922 timespec in;
923 in.tv_sec = 1;
924 in.tv_nsec = 0;
925 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
926}
Elliott Hughes12443702016-10-19 16:02:31 -0700927
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700928TEST(time, clock_nanosleep) {
929 auto t0 = std::chrono::steady_clock::now();
930 const timespec ts = {.tv_nsec = 5000000};
931 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
932 auto t1 = std::chrono::steady_clock::now();
933 ASSERT_GE(t1-t0, 5000000ns);
934}
935
936TEST(time, nanosleep) {
937 auto t0 = std::chrono::steady_clock::now();
938 const timespec ts = {.tv_nsec = 5000000};
939 ASSERT_EQ(0, nanosleep(&ts, nullptr));
940 auto t1 = std::chrono::steady_clock::now();
941 ASSERT_GE(t1-t0, 5000000ns);
942}
943
944TEST(time, nanosleep_EINVAL) {
945 timespec ts = {.tv_sec = -1};
946 errno = 0;
947 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
948 ASSERT_EQ(EINVAL, errno);
949}
950
Elliott Hughes12443702016-10-19 16:02:31 -0700951TEST(time, bug_31938693) {
952 // User-visible symptoms in N:
953 // http://b/31938693
954 // https://code.google.com/p/android/issues/detail?id=225132
955
956 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
957 // http://b/31848040
958
959 // This isn't a great test, because very few time zones were actually affected, and there's
960 // no real logic to which ones were affected: it was just a coincidence of the data that came
961 // after them in the tzdata file.
962
963 time_t t = 1475619727;
964 struct tm tm;
965
966 setenv("TZ", "America/Los_Angeles", 1);
967 tzset();
968 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
969 EXPECT_EQ(15, tm.tm_hour);
970
971 setenv("TZ", "Europe/London", 1);
972 tzset();
973 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
974 EXPECT_EQ(23, tm.tm_hour);
975
976 setenv("TZ", "America/Atka", 1);
977 tzset();
978 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
979 EXPECT_EQ(13, tm.tm_hour);
980
981 setenv("TZ", "Pacific/Apia", 1);
982 tzset();
983 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
984 EXPECT_EQ(12, tm.tm_hour);
985
986 setenv("TZ", "Pacific/Honolulu", 1);
987 tzset();
988 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
989 EXPECT_EQ(12, tm.tm_hour);
990
991 setenv("TZ", "Asia/Magadan", 1);
992 tzset();
993 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
994 EXPECT_EQ(9, tm.tm_hour);
995}
Elliott Hughesea877162017-01-11 14:34:16 -0800996
997TEST(time, bug_31339449) {
998 // POSIX says localtime acts as if it calls tzset.
999 // tzset does two things:
1000 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
1001 // 2. it sets the global `tzname`.
1002 // POSIX says localtime_r need not set `tzname` (2).
1003 // Q: should localtime_r set the time zone (1)?
1004 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
1005
1006 // Pick a time, any time...
1007 time_t t = 1475619727;
1008
1009 // Call tzset with a specific timezone.
1010 setenv("TZ", "America/Atka", 1);
1011 tzset();
1012
1013 // If we change the timezone and call localtime, localtime should use the new timezone.
1014 setenv("TZ", "America/Los_Angeles", 1);
1015 struct tm* tm_p = localtime(&t);
1016 EXPECT_EQ(15, tm_p->tm_hour);
1017
1018 // Reset the timezone back.
1019 setenv("TZ", "America/Atka", 1);
1020 tzset();
1021
1022#if defined(__BIONIC__)
1023 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1024 setenv("TZ", "America/Los_Angeles", 1);
1025 struct tm tm = {};
1026 localtime_r(&t, &tm);
1027 EXPECT_EQ(15, tm.tm_hour);
1028#else
1029 // The BSDs agree with us, but glibc gets this wrong.
1030#endif
1031}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001032
1033TEST(time, asctime) {
1034 const struct tm tm = {};
1035 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1036}
1037
1038TEST(time, asctime_r) {
1039 const struct tm tm = {};
1040 char buf[256];
1041 ASSERT_EQ(buf, asctime_r(&tm, buf));
1042 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1043}
1044
1045TEST(time, ctime) {
1046 setenv("TZ", "UTC", 1);
1047 const time_t t = 0;
1048 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1049}
1050
1051TEST(time, ctime_r) {
1052 setenv("TZ", "UTC", 1);
1053 const time_t t = 0;
1054 char buf[256];
1055 ASSERT_EQ(buf, ctime_r(&t, buf));
1056 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1057}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001058
1059// https://issuetracker.google.com/37128336
1060TEST(time, strftime_strptime_s) {
1061 char buf[32];
1062 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1063
1064 setenv("TZ", "America/Los_Angeles", 1);
1065 strftime(buf, sizeof(buf), "<%s>", &tm0);
1066 EXPECT_STREQ("<378720000>", buf);
1067
1068 setenv("TZ", "UTC", 1);
1069 strftime(buf, sizeof(buf), "<%s>", &tm0);
1070 EXPECT_STREQ("<378691200>", buf);
1071
1072 struct tm tm;
1073
1074 setenv("TZ", "America/Los_Angeles", 1);
1075 tzset();
1076 memset(&tm, 0xff, sizeof(tm));
1077 char* p = strptime("378720000x", "%s", &tm);
1078 ASSERT_EQ('x', *p);
1079 EXPECT_EQ(0, tm.tm_sec);
1080 EXPECT_EQ(0, tm.tm_min);
1081 EXPECT_EQ(0, tm.tm_hour);
1082 EXPECT_EQ(1, tm.tm_mday);
1083 EXPECT_EQ(0, tm.tm_mon);
1084 EXPECT_EQ(82, tm.tm_year);
1085 EXPECT_EQ(5, tm.tm_wday);
1086 EXPECT_EQ(0, tm.tm_yday);
1087 EXPECT_EQ(0, tm.tm_isdst);
1088
1089 setenv("TZ", "UTC", 1);
1090 tzset();
1091 memset(&tm, 0xff, sizeof(tm));
1092 p = strptime("378691200x", "%s", &tm);
1093 ASSERT_EQ('x', *p);
1094 EXPECT_EQ(0, tm.tm_sec);
1095 EXPECT_EQ(0, tm.tm_min);
1096 EXPECT_EQ(0, tm.tm_hour);
1097 EXPECT_EQ(1, tm.tm_mday);
1098 EXPECT_EQ(0, tm.tm_mon);
1099 EXPECT_EQ(82, tm.tm_year);
1100 EXPECT_EQ(5, tm.tm_wday);
1101 EXPECT_EQ(0, tm.tm_yday);
1102 EXPECT_EQ(0, tm.tm_isdst);
1103}
1104
1105TEST(time, strptime_s_nothing) {
1106 struct tm tm;
1107 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1108}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001109
1110TEST(time, timespec_get) {
1111#if __BIONIC__
1112 timespec ts = {};
1113 ASSERT_EQ(0, timespec_get(&ts, 123));
1114 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1115#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001116 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001117#endif
1118}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001119
1120TEST(time, difftime) {
1121 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001122 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001123}