blob: 932af9ee0e1cf1a02aadf4ad3d22705e5bc2067c [file] [log] [blame]
Elliott Hughese0175ca2013-03-14 14:38:08 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes4b558f52014-03-04 15:58:02 -080017#include <time.h>
18
19#include <errno.h>
Elliott Hughese0175ca2013-03-14 14:38:08 -070020#include <gtest/gtest.h>
Elliott Hughes329103d2014-04-25 16:55:04 -070021#include <pthread.h>
Elliott Hughes4b558f52014-03-04 15:58:02 -080022#include <signal.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070023#include <sys/syscall.h>
Brian Carlstrombe1d91d2014-03-08 15:05:26 -080024#include <sys/types.h>
25#include <sys/wait.h>
Yabin Cuid5c65272014-11-26 14:04:26 -080026#include <unistd.h>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070027
Yabin Cui95f1ee22015-01-13 19:53:15 -080028#include <atomic>
Elliott Hughesca3f8e42019-10-28 15:59:38 -070029#include <chrono>
Elliott Hughese0175ca2013-03-14 14:38:08 -070030
Elliott Hughes71ba5892018-02-07 12:44:45 -080031#include "SignalUtils.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080032#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070033
Elliott Hughesca3f8e42019-10-28 15:59:38 -070034using namespace std::chrono_literals;
35
Mark Salyzyn0b846e82017-12-20 08:56:18 -080036TEST(time, time) {
37 // Acquire time
38 time_t p1, t1 = time(&p1);
39 // valid?
40 ASSERT_NE(static_cast<time_t>(0), t1);
41 ASSERT_NE(static_cast<time_t>(-1), t1);
42 ASSERT_EQ(p1, t1);
43
44 // Acquire time one+ second later
45 usleep(1010000);
46 time_t p2, t2 = time(&p2);
47 // valid?
48 ASSERT_NE(static_cast<time_t>(0), t2);
49 ASSERT_NE(static_cast<time_t>(-1), t2);
50 ASSERT_EQ(p2, t2);
51
52 // Expect time progression
53 ASSERT_LT(p1, p2);
54 ASSERT_LE(t2 - t1, static_cast<time_t>(2));
55
56 // Expect nullptr call to produce same results
57 ASSERT_LE(t2, time(nullptr));
58 ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1));
59}
60
Elliott Hughesee178bf2013-07-12 11:25:20 -070061TEST(time, gmtime) {
62 time_t t = 0;
63 tm* broken_down = gmtime(&t);
Yi Kong32bc0fc2018-08-02 17:31:13 -070064 ASSERT_TRUE(broken_down != nullptr);
Elliott Hughesee178bf2013-07-12 11:25:20 -070065 ASSERT_EQ(0, broken_down->tm_sec);
66 ASSERT_EQ(0, broken_down->tm_min);
67 ASSERT_EQ(0, broken_down->tm_hour);
68 ASSERT_EQ(1, broken_down->tm_mday);
69 ASSERT_EQ(0, broken_down->tm_mon);
70 ASSERT_EQ(1970, broken_down->tm_year + 1900);
71}
Elliott Hughes7843d442013-08-22 11:37:32 -070072
Elliott Hughes5a29d542017-12-07 16:05:57 -080073TEST(time, gmtime_r) {
74 struct tm tm = {};
75 time_t t = 0;
76 struct tm* broken_down = gmtime_r(&t, &tm);
77 ASSERT_EQ(broken_down, &tm);
78 ASSERT_EQ(0, broken_down->tm_sec);
79 ASSERT_EQ(0, broken_down->tm_min);
80 ASSERT_EQ(0, broken_down->tm_hour);
81 ASSERT_EQ(1, broken_down->tm_mday);
82 ASSERT_EQ(0, broken_down->tm_mon);
83 ASSERT_EQ(1970, broken_down->tm_year + 1900);
84}
85
Elliott Hughes329103d2014-04-25 16:55:04 -070086static void* gmtime_no_stack_overflow_14313703_fn(void*) {
87 const char* original_tz = getenv("TZ");
88 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
89 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
90 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -070091 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -070092 setenv("TZ", original_tz, 1);
93 }
94 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -070095 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -070096}
97
98TEST(time, gmtime_no_stack_overflow_14313703) {
99 // Is it safe to call tzload on a thread with a small stack?
100 // http://b/14313703
101 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -0800102 pthread_attr_t a;
103 ASSERT_EQ(0, pthread_attr_init(&a));
104 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700105
106 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700107 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800108 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700109}
110
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900111TEST(time, mktime_empty_TZ) {
112 // tzcode used to have a bug where it didn't reinitialize some internal state.
113
114 // Choose a time where DST is set.
115 struct tm t;
116 memset(&t, 0, sizeof(tm));
117 t.tm_year = 1980 - 1900;
118 t.tm_mon = 6;
119 t.tm_mday = 2;
120
121 setenv("TZ", "America/Los_Angeles", 1);
122 tzset();
123 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
124
125 memset(&t, 0, sizeof(tm));
126 t.tm_year = 1980 - 1900;
127 t.tm_mon = 6;
128 t.tm_mday = 2;
129
130 setenv("TZ", "", 1); // Implies UTC.
131 tzset();
132 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
133}
134
Elliott Hughes7843d442013-08-22 11:37:32 -0700135TEST(time, mktime_10310929) {
136 struct tm t;
137 memset(&t, 0, sizeof(tm));
138 t.tm_year = 200;
139 t.tm_mon = 2;
140 t.tm_mday = 10;
141
Elliott Hughes0c401522013-10-18 16:21:54 -0700142#if !defined(__LP64__)
Elliott Hughescf346532020-07-31 10:35:03 -0700143 // 32-bit bionic has a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700144 ASSERT_EQ(-1, mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700145 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700146#else
147 // Everyone else should be using a signed 64-bit time_t.
148 ASSERT_GE(sizeof(time_t) * 8, 64U);
149
150 setenv("TZ", "America/Los_Angeles", 1);
151 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700152 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700153 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700154 ASSERT_EQ(0, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700155
156 setenv("TZ", "UTC", 1);
157 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700158 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700159 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700160 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700161#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800162}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800163
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700164TEST(time, mktime_EOVERFLOW) {
165 struct tm t;
166 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700167
168 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
169 t.tm_year = 2016 - 1900;
170
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700171 t.tm_mon = 2;
172 t.tm_mday = 10;
173
174 errno = 0;
175 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
176 ASSERT_EQ(0, errno);
177
Elliott Hughes47126ed2016-09-06 13:25:53 -0700178 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700179 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700180
181 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700182 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
183 ASSERT_EQ(EOVERFLOW, errno);
184}
185
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700186TEST(time, strftime) {
187 setenv("TZ", "UTC", 1);
188
189 struct tm t;
190 memset(&t, 0, sizeof(tm));
191 t.tm_year = 200;
192 t.tm_mon = 2;
193 t.tm_mday = 10;
194
195 char buf[64];
196
197 // Seconds since the epoch.
198#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
199 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
200 EXPECT_STREQ("4108320000", buf);
201#endif
202
203 // Date and time as text.
204 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
205 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
206}
207
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800208TEST(time, strftime_null_tm_zone) {
209 // Netflix on Nexus Player wouldn't start (http://b/25170306).
210 struct tm t;
211 memset(&t, 0, sizeof(tm));
212
213 char buf[64];
214
215 setenv("TZ", "America/Los_Angeles", 1);
216 tzset();
217
218 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
219 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
220 EXPECT_STREQ("<PST>", buf);
221
222#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
223 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
224 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
225 EXPECT_STREQ("<PDT>", buf);
226
227 t.tm_isdst = -123; // "and negative if the information is not available".
228 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
229 EXPECT_STREQ("<>", buf);
230#endif
231
232 setenv("TZ", "UTC", 1);
233 tzset();
234
235 t.tm_isdst = 0;
236 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
237 EXPECT_STREQ("<UTC>", buf);
238
239#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
240 t.tm_isdst = 1; // UTC has no DST.
241 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
242 EXPECT_STREQ("<>", buf);
243#endif
244}
245
Elliott Hughes0a610d02016-07-29 14:04:17 -0700246TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700247 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700248 locale_t old_locale = uselocale(cloc);
249
250 setenv("TZ", "UTC", 1);
251
252 struct tm t;
253 memset(&t, 0, sizeof(tm));
254 t.tm_year = 200;
255 t.tm_mon = 2;
256 t.tm_mday = 10;
257
258 // Date and time as text.
259 char buf[64];
260 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
261 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
262
263 uselocale(old_locale);
264 freelocale(cloc);
265}
266
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700267TEST(time, strptime) {
268 setenv("TZ", "UTC", 1);
269
270 struct tm t;
271 char buf[64];
272
273 memset(&t, 0, sizeof(t));
274 strptime("11:14", "%R", &t);
275 strftime(buf, sizeof(buf), "%H:%M", &t);
276 EXPECT_STREQ("11:14", buf);
277
278 memset(&t, 0, sizeof(t));
279 strptime("09:41:53", "%T", &t);
280 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
281 EXPECT_STREQ("09:41:53", buf);
282}
283
Elliott Hughes3376c232018-02-13 23:14:12 -0800284TEST(time, strptime_l) {
Colin Cross7da20342021-07-28 11:18:11 -0700285#if !defined(MUSL)
Elliott Hughes3376c232018-02-13 23:14:12 -0800286 setenv("TZ", "UTC", 1);
287
288 struct tm t;
289 char buf[64];
290
291 memset(&t, 0, sizeof(t));
292 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
293 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
294 EXPECT_STREQ("11:14", buf);
295
296 memset(&t, 0, sizeof(t));
297 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
298 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
299 EXPECT_STREQ("09:41:53", buf);
Colin Cross7da20342021-07-28 11:18:11 -0700300#else
301 GTEST_SKIP() << "musl doesn't support strptime_l";
302#endif
Elliott Hughes3376c232018-02-13 23:14:12 -0800303}
304
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700305TEST(time, strptime_F) {
306 setenv("TZ", "UTC", 1);
307
308 struct tm tm = {};
309 ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm));
310 EXPECT_EQ(119, tm.tm_year);
311 EXPECT_EQ(2, tm.tm_mon);
312 EXPECT_EQ(26, tm.tm_mday);
313}
314
315TEST(time, strptime_P_p) {
316 setenv("TZ", "UTC", 1);
317
Elliott Hughes11678822019-03-27 08:56:49 -0700318 // For parsing, %P and %p are the same: case doesn't matter.
319
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700320 struct tm tm = {.tm_hour = 12};
321 ASSERT_EQ('\0', *strptime("AM", "%p", &tm));
322 EXPECT_EQ(0, tm.tm_hour);
323
324 tm = {.tm_hour = 12};
Elliott Hughes11678822019-03-27 08:56:49 -0700325 ASSERT_EQ('\0', *strptime("am", "%p", &tm));
326 EXPECT_EQ(0, tm.tm_hour);
327
328 tm = {.tm_hour = 12};
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700329 ASSERT_EQ('\0', *strptime("AM", "%P", &tm));
330 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughes11678822019-03-27 08:56:49 -0700331
332 tm = {.tm_hour = 12};
333 ASSERT_EQ('\0', *strptime("am", "%P", &tm));
334 EXPECT_EQ(0, tm.tm_hour);
Elliott Hughesa1fb15b2019-03-26 19:07:40 -0700335}
336
337TEST(time, strptime_u) {
338 setenv("TZ", "UTC", 1);
339
340 struct tm tm = {};
341 ASSERT_EQ('\0', *strptime("2", "%u", &tm));
342 EXPECT_EQ(2, tm.tm_wday);
343}
344
345TEST(time, strptime_v) {
346 setenv("TZ", "UTC", 1);
347
348 struct tm tm = {};
349 ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm));
350 EXPECT_EQ(80, tm.tm_year);
351 EXPECT_EQ(2, tm.tm_mon);
352 EXPECT_EQ(26, tm.tm_mday);
353}
354
355TEST(time, strptime_V_G_g) {
356 setenv("TZ", "UTC", 1);
357
358 // %V (ISO-8601 week number), %G (year of week number, without century), and
359 // %g (year of week number) have no effect when parsed, and are supported
360 // solely so that it's possible for strptime(3) to parse everything that
361 // strftime(3) can output.
362 struct tm tm = {};
363 ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm));
364 struct tm zero = {};
365 EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0);
366}
367
Elliott Hughesd065c042020-09-01 19:02:44 -0700368TEST(time, strptime_Z) {
369#if defined(__BIONIC__)
370 // glibc doesn't handle %Z at all.
371 // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings
372 // are in the global `tzname` (which correspond to the current $TZ).
373 struct tm tm;
374 setenv("TZ", "Europe/Berlin", 1);
375
376 // "GMT" always works.
377 tm = {};
378 ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm));
379 EXPECT_STREQ("GMT", tm.tm_zone);
380 EXPECT_EQ(0, tm.tm_isdst);
381 EXPECT_EQ(0, tm.tm_gmtoff);
382
383 // As does "UTC".
384 tm = {};
385 ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm));
386 EXPECT_STREQ("UTC", tm.tm_zone);
387 EXPECT_EQ(0, tm.tm_isdst);
388 EXPECT_EQ(0, tm.tm_gmtoff);
389
390 // Europe/Berlin is known as "CET" when there's no DST.
391 tm = {};
392 ASSERT_EQ('\0', *strptime("CET", "%Z", &tm));
393 EXPECT_STREQ("CET", tm.tm_zone);
394 EXPECT_EQ(0, tm.tm_isdst);
395 EXPECT_EQ(3600, tm.tm_gmtoff);
396
397 // Europe/Berlin is known as "CEST" when there's no DST.
398 tm = {};
399 ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm));
400 EXPECT_STREQ("CEST", tm.tm_zone);
401 EXPECT_EQ(1, tm.tm_isdst);
402 EXPECT_EQ(3600, tm.tm_gmtoff);
403
404 // And as long as we're in Europe/Berlin, those are the only time zone
405 // abbreviations that are recognized.
406 tm = {};
407 ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr);
408#endif
409}
410
411TEST(time, strptime_z) {
412 struct tm tm;
413 setenv("TZ", "Europe/Berlin", 1);
414
415 // "UT" is what RFC822 called UTC.
416 tm = {};
417 ASSERT_EQ('\0', *strptime("UT", "%z", &tm));
418 EXPECT_STREQ("UTC", tm.tm_zone);
419 EXPECT_EQ(0, tm.tm_isdst);
420 EXPECT_EQ(0, tm.tm_gmtoff);
421 // "GMT" is RFC822's other name for UTC.
422 tm = {};
423 ASSERT_EQ('\0', *strptime("GMT", "%z", &tm));
424 EXPECT_STREQ("UTC", tm.tm_zone);
425 EXPECT_EQ(0, tm.tm_isdst);
426 EXPECT_EQ(0, tm.tm_gmtoff);
427
428 // "Z" ("Zulu") is a synonym for UTC.
429 tm = {};
430 ASSERT_EQ('\0', *strptime("Z", "%z", &tm));
431 EXPECT_STREQ("UTC", tm.tm_zone);
432 EXPECT_EQ(0, tm.tm_isdst);
433 EXPECT_EQ(0, tm.tm_gmtoff);
434
435 // "PST"/"PDT" and the other common US zone abbreviations are all supported.
436 tm = {};
437 ASSERT_EQ('\0', *strptime("PST", "%z", &tm));
438 EXPECT_STREQ("PST", tm.tm_zone);
439 EXPECT_EQ(0, tm.tm_isdst);
440 EXPECT_EQ(-28800, tm.tm_gmtoff);
441 tm = {};
442 ASSERT_EQ('\0', *strptime("PDT", "%z", &tm));
443 EXPECT_STREQ("PDT", tm.tm_zone);
444 EXPECT_EQ(1, tm.tm_isdst);
445 EXPECT_EQ(-25200, tm.tm_gmtoff);
446
447 // +-hh
448 tm = {};
449 ASSERT_EQ('\0', *strptime("+01", "%z", &tm));
450 EXPECT_EQ(3600, tm.tm_gmtoff);
451 EXPECT_TRUE(tm.tm_zone == nullptr);
452 EXPECT_EQ(0, tm.tm_isdst);
453 // +-hhmm
454 tm = {};
455 ASSERT_EQ('\0', *strptime("+0130", "%z", &tm));
456 EXPECT_EQ(5400, tm.tm_gmtoff);
457 EXPECT_TRUE(tm.tm_zone == nullptr);
458 EXPECT_EQ(0, tm.tm_isdst);
459 // +-hh:mm
460 tm = {};
461 ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm));
462 EXPECT_EQ(5400, tm.tm_gmtoff);
463 EXPECT_TRUE(tm.tm_zone == nullptr);
464 EXPECT_EQ(0, tm.tm_isdst);
465}
466
Elliott Hughes4b558f52014-03-04 15:58:02 -0800467void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
468 itimerspec ts;
469 ts.it_value.tv_sec = value_s;
470 ts.it_value.tv_nsec = value_ns;
471 ts.it_interval.tv_sec = interval_s;
472 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700473 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800474}
475
Colin Cross7da20342021-07-28 11:18:11 -0700476static void NoOpNotifyFunction(sigval) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800477}
478
479TEST(time, timer_create) {
Colin Cross7da20342021-07-28 11:18:11 -0700480 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800481 memset(&se, 0, sizeof(se));
482 se.sigev_notify = SIGEV_THREAD;
483 se.sigev_notify_function = NoOpNotifyFunction;
484 timer_t timer_id;
485 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
486
Elliott Hughes33697a02016-01-26 13:04:57 -0800487 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800488 ASSERT_NE(-1, pid) << strerror(errno);
489
490 if (pid == 0) {
491 // Timers are not inherited by the child.
492 ASSERT_EQ(-1, timer_delete(timer_id));
493 ASSERT_EQ(EINVAL, errno);
494 _exit(0);
495 }
496
Elliott Hughes33697a02016-01-26 13:04:57 -0800497 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800498
499 ASSERT_EQ(0, timer_delete(timer_id));
500}
501
Yabin Cui95f1ee22015-01-13 19:53:15 -0800502static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800503static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
504 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
505 ASSERT_EQ(SIGUSR1, signal_number);
506}
507
508TEST(time, timer_create_SIGEV_SIGNAL) {
Colin Cross7da20342021-07-28 11:18:11 -0700509 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800510 memset(&se, 0, sizeof(se));
511 se.sigev_notify = SIGEV_SIGNAL;
512 se.sigev_signo = SIGUSR1;
513
514 timer_t timer_id;
515 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
516
Yabin Cui95f1ee22015-01-13 19:53:15 -0800517 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800518 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
519
520 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
521
522 itimerspec ts;
523 ts.it_value.tv_sec = 0;
524 ts.it_value.tv_nsec = 1;
525 ts.it_interval.tv_sec = 0;
526 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700527 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800528
529 usleep(500000);
530 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
531}
532
533struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800534 private:
535 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800536 timer_t timer_id;
Colin Cross7da20342021-07-28 11:18:11 -0700537 sigevent se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700538 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800539
Elliott Hughes4b558f52014-03-04 15:58:02 -0800540 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700541 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800542 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700543 timer_valid = true;
544 }
545
Yabin Cui95f1ee22015-01-13 19:53:15 -0800546 public:
Colin Cross7da20342021-07-28 11:18:11 -0700547 explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800548 memset(&se, 0, sizeof(se));
549 se.sigev_notify = SIGEV_THREAD;
550 se.sigev_notify_function = fn;
551 se.sigev_value.sival_ptr = this;
552 Create();
553 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700554 void DeleteTimer() {
555 ASSERT_TRUE(timer_valid);
556 ASSERT_EQ(0, timer_delete(timer_id));
557 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800558 }
559
560 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700561 if (timer_valid) {
562 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800563 }
564 }
565
Yabin Cui95f1ee22015-01-13 19:53:15 -0800566 int Value() const {
567 return value;
568 }
569
Christopher Ferris62d84b12014-10-20 19:09:19 -0700570 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
571 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
572 }
573
574 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800575 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700576 time_t start = time(nullptr);
577 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700578 }
579 return current_value != value;
580 }
581
Colin Cross7da20342021-07-28 11:18:11 -0700582 static void CountNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800583 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
584 ++cd->value;
585 }
586
Colin Cross7da20342021-07-28 11:18:11 -0700587 static void CountAndDisarmNotifyFunction(sigval value) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800588 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
589 ++cd->value;
590
591 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700592 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800593 }
594};
595
596TEST(time, timer_settime_0) {
597 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800598 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800599
Yabin Cuibf572d92015-08-11 11:23:16 -0700600 counter.SetTime(0, 500000000, 1, 0);
601 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800602
603 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800604 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800605}
606
607TEST(time, timer_settime_repeats) {
608 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800609 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800610
Christopher Ferris62d84b12014-10-20 19:09:19 -0700611 counter.SetTime(0, 1, 0, 10);
612 ASSERT_TRUE(counter.ValueUpdated());
613 ASSERT_TRUE(counter.ValueUpdated());
614 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800615 counter.DeleteTimer();
616 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
617 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800618}
619
Yabin Cui95f1ee22015-01-13 19:53:15 -0800620static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800621static void timer_create_NULL_signal_handler(int signal_number) {
622 ++timer_create_NULL_signal_handler_invocation_count;
623 ASSERT_EQ(SIGALRM, signal_number);
624}
625
626TEST(time, timer_create_NULL) {
627 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
628 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700629 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800630
Yabin Cui95f1ee22015-01-13 19:53:15 -0800631 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800632 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
633
634 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
635
636 SetTime(timer_id, 0, 1, 0, 0);
637 usleep(500000);
638
639 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
640}
641
642TEST(time, timer_create_EINVAL) {
643 clockid_t invalid_clock = 16;
644
645 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
646 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700647 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800648 ASSERT_EQ(EINVAL, errno);
649
650 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
Colin Cross7da20342021-07-28 11:18:11 -0700651 sigevent se;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800652 memset(&se, 0, sizeof(se));
653 se.sigev_notify = SIGEV_THREAD;
654 se.sigev_notify_function = NoOpNotifyFunction;
655 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
656 ASSERT_EQ(EINVAL, errno);
657}
658
Elliott Hughes4b558f52014-03-04 15:58:02 -0800659TEST(time, timer_create_multiple) {
660 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800661 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800662 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800663
Yabin Cui95f1ee22015-01-13 19:53:15 -0800664 ASSERT_EQ(0, counter1.Value());
665 ASSERT_EQ(0, counter2.Value());
666 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800667
Yabin Cui410c1ad2015-06-18 16:19:02 -0700668 counter2.SetTime(0, 500000000, 0, 0);
669 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800670
Yabin Cui95f1ee22015-01-13 19:53:15 -0800671 EXPECT_EQ(0, counter1.Value());
672 EXPECT_EQ(1, counter2.Value());
673 EXPECT_EQ(0, counter3.Value());
674}
675
676// Test to verify that disarming a repeatable timer disables the callbacks.
677TEST(time, timer_disarm_terminates) {
678 Counter counter(Counter::CountNotifyFunction);
679 ASSERT_EQ(0, counter.Value());
680
681 counter.SetTime(0, 1, 0, 1);
682 ASSERT_TRUE(counter.ValueUpdated());
683 ASSERT_TRUE(counter.ValueUpdated());
684 ASSERT_TRUE(counter.ValueUpdated());
685
686 counter.SetTime(0, 0, 0, 0);
687 // Add a sleep as the kernel may have pending events when the timer is disarmed.
688 usleep(500000);
689 int value = counter.Value();
690 usleep(500000);
691
692 // Verify the counter has not been incremented.
693 ASSERT_EQ(value, counter.Value());
694}
695
696// Test to verify that deleting a repeatable timer disables the callbacks.
697TEST(time, timer_delete_terminates) {
698 Counter counter(Counter::CountNotifyFunction);
699 ASSERT_EQ(0, counter.Value());
700
701 counter.SetTime(0, 1, 0, 1);
702 ASSERT_TRUE(counter.ValueUpdated());
703 ASSERT_TRUE(counter.ValueUpdated());
704 ASSERT_TRUE(counter.ValueUpdated());
705
706 counter.DeleteTimer();
707 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
708 usleep(500000);
709 int value = counter.Value();
710 usleep(500000);
711
712 // Verify the counter has not been incremented.
713 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800714}
Christopher Ferris753ad772014-03-20 20:47:45 -0700715
716struct TimerDeleteData {
717 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800718 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700719 volatile bool complete;
720};
721
Colin Cross7da20342021-07-28 11:18:11 -0700722static void TimerDeleteCallback(sigval value) {
Christopher Ferris753ad772014-03-20 20:47:45 -0700723 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
724
Elliott Hughes11859d42017-02-13 17:59:29 -0800725 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700726 timer_delete(tdd->timer_id);
727 tdd->complete = true;
728}
729
730TEST(time, timer_delete_from_timer_thread) {
731 TimerDeleteData tdd;
Colin Cross7da20342021-07-28 11:18:11 -0700732 sigevent se;
Christopher Ferris753ad772014-03-20 20:47:45 -0700733
734 memset(&se, 0, sizeof(se));
735 se.sigev_notify = SIGEV_THREAD;
736 se.sigev_notify_function = TimerDeleteCallback;
737 se.sigev_value.sival_ptr = &tdd;
738
739 tdd.complete = false;
740 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
741
742 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800743 ts.it_value.tv_sec = 1;
744 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700745 ts.it_interval.tv_sec = 0;
746 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700747 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700748
Yi Kong32bc0fc2018-08-02 17:31:13 -0700749 time_t cur_time = time(nullptr);
750 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700751 ASSERT_TRUE(tdd.complete);
752
753#if defined(__BIONIC__)
754 // Since bionic timers are implemented by creating a thread to handle the
755 // callback, verify that the thread actually completes.
756 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800757 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
758 ASSERT_EQ(-1, kill(tdd.tid, 0));
759 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700760#endif
761}
Elliott Hughes625993d2014-07-15 16:53:13 -0700762
763TEST(time, clock_gettime) {
764 // Try to ensure that our vdso clock_gettime is working.
Giuliano Procida096f5952021-04-08 10:51:58 +0100765 timespec ts0;
Elliott Hughes625993d2014-07-15 16:53:13 -0700766 timespec ts1;
Elliott Hughes625993d2014-07-15 16:53:13 -0700767 timespec ts2;
Giuliano Procida096f5952021-04-08 10:51:58 +0100768 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0));
769 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1));
770 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2));
Elliott Hughes625993d2014-07-15 16:53:13 -0700771
Giuliano Procida096f5952021-04-08 10:51:58 +0100772 // Check we have a nice monotonic timestamp sandwich.
773 ASSERT_LE(ts0.tv_sec, ts1.tv_sec);
774 if (ts0.tv_sec == ts1.tv_sec) {
775 ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec);
Elliott Hughes625993d2014-07-15 16:53:13 -0700776 }
Giuliano Procida096f5952021-04-08 10:51:58 +0100777 ASSERT_LE(ts1.tv_sec, ts2.tv_sec);
778 if (ts1.tv_sec == ts2.tv_sec) {
779 ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec);
780 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700781}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700782
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700783TEST(time, clock_gettime_CLOCK_REALTIME) {
784 timespec ts;
785 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
786}
787
788TEST(time, clock_gettime_CLOCK_MONOTONIC) {
789 timespec ts;
790 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
791}
792
793TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
794 timespec ts;
795 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
796}
797
798TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
799 timespec ts;
800 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
801}
802
803TEST(time, clock_gettime_CLOCK_BOOTTIME) {
804 timespec ts;
805 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
806}
807
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800808TEST(time, clock_gettime_unknown) {
809 errno = 0;
810 timespec ts;
811 ASSERT_EQ(-1, clock_gettime(-1, &ts));
812 ASSERT_EQ(EINVAL, errno);
813}
814
815TEST(time, clock_getres_CLOCK_REALTIME) {
816 timespec ts;
817 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
818 ASSERT_EQ(1, ts.tv_nsec);
819 ASSERT_EQ(0, ts.tv_sec);
820}
821
822TEST(time, clock_getres_CLOCK_MONOTONIC) {
823 timespec ts;
824 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
825 ASSERT_EQ(1, ts.tv_nsec);
826 ASSERT_EQ(0, ts.tv_sec);
827}
828
829TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
830 timespec ts;
831 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
832}
833
834TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
835 timespec ts;
836 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
837}
838
839TEST(time, clock_getres_CLOCK_BOOTTIME) {
840 timespec ts;
841 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
842 ASSERT_EQ(1, ts.tv_nsec);
843 ASSERT_EQ(0, ts.tv_sec);
844}
845
846TEST(time, clock_getres_unknown) {
847 errno = 0;
848 timespec ts = { -1, -1 };
849 ASSERT_EQ(-1, clock_getres(-1, &ts));
850 ASSERT_EQ(EINVAL, errno);
851 ASSERT_EQ(-1, ts.tv_nsec);
852 ASSERT_EQ(-1, ts.tv_sec);
853}
854
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700855TEST(time, clock) {
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100856 // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average.
857 static const clock_t N = 5;
858 static const clock_t mean_limit_ms = 10;
Haruki Hasegawa18160252014-10-13 00:50:47 +0900859 clock_t t0 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100860 for (size_t i = 0; i < N; ++i) {
861 sleep(1);
862 }
Haruki Hasegawa18160252014-10-13 00:50:47 +0900863 clock_t t1 = clock();
Giuliano Procidaebc88d22021-04-07 14:25:13 +0100864 ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000));
Haruki Hasegawa18160252014-10-13 00:50:47 +0900865}
866
Elliott Hughesb4413592017-11-29 18:17:06 -0800867static pid_t GetInvalidPid() {
868 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -0800869 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -0800870 fscanf(fp.get(), "%ld", &pid_max);
871 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -0800872}
873
Elliott Hughesb4413592017-11-29 18:17:06 -0800874TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800875 clockid_t clockid;
876 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -0800877 timespec ts;
878 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800879}
Yabin Cuid5c65272014-11-26 14:04:26 -0800880
Elliott Hughesb4413592017-11-29 18:17:06 -0800881TEST(time, clock_getcpuclockid_parent) {
882 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -0800883 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -0800884 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -0800885 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800886}
Yabin Cuid5c65272014-11-26 14:04:26 -0800887
Elliott Hughesb4413592017-11-29 18:17:06 -0800888TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800889 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
890 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800891 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -0800892 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800893 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
894 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
895 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
896 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
897 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
898 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -0800899 ASSERT_EQ(0, errno);
900}
901
Haruki Hasegawa18160252014-10-13 00:50:47 +0900902TEST(time, clock_settime) {
903 errno = 0;
904 timespec ts;
905 ASSERT_EQ(-1, clock_settime(-1, &ts));
906 ASSERT_EQ(EINVAL, errno);
907}
908
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700909TEST(time, clock_nanosleep_EINVAL) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900910 timespec in;
911 timespec out;
912 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700913}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700914
915TEST(time, clock_nanosleep_thread_cputime_id) {
916 timespec in;
917 in.tv_sec = 1;
918 in.tv_nsec = 0;
919 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
920}
Elliott Hughes12443702016-10-19 16:02:31 -0700921
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700922TEST(time, clock_nanosleep) {
923 auto t0 = std::chrono::steady_clock::now();
924 const timespec ts = {.tv_nsec = 5000000};
925 ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr));
926 auto t1 = std::chrono::steady_clock::now();
927 ASSERT_GE(t1-t0, 5000000ns);
928}
929
930TEST(time, nanosleep) {
931 auto t0 = std::chrono::steady_clock::now();
932 const timespec ts = {.tv_nsec = 5000000};
933 ASSERT_EQ(0, nanosleep(&ts, nullptr));
934 auto t1 = std::chrono::steady_clock::now();
935 ASSERT_GE(t1-t0, 5000000ns);
936}
937
938TEST(time, nanosleep_EINVAL) {
939 timespec ts = {.tv_sec = -1};
940 errno = 0;
941 ASSERT_EQ(-1, nanosleep(&ts, nullptr));
942 ASSERT_EQ(EINVAL, errno);
943}
944
Elliott Hughes12443702016-10-19 16:02:31 -0700945TEST(time, bug_31938693) {
946 // User-visible symptoms in N:
947 // http://b/31938693
948 // https://code.google.com/p/android/issues/detail?id=225132
949
950 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
951 // http://b/31848040
952
953 // This isn't a great test, because very few time zones were actually affected, and there's
954 // no real logic to which ones were affected: it was just a coincidence of the data that came
955 // after them in the tzdata file.
956
957 time_t t = 1475619727;
958 struct tm tm;
959
960 setenv("TZ", "America/Los_Angeles", 1);
961 tzset();
962 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
963 EXPECT_EQ(15, tm.tm_hour);
964
965 setenv("TZ", "Europe/London", 1);
966 tzset();
967 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
968 EXPECT_EQ(23, tm.tm_hour);
969
970 setenv("TZ", "America/Atka", 1);
971 tzset();
972 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
973 EXPECT_EQ(13, tm.tm_hour);
974
975 setenv("TZ", "Pacific/Apia", 1);
976 tzset();
977 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
978 EXPECT_EQ(12, tm.tm_hour);
979
980 setenv("TZ", "Pacific/Honolulu", 1);
981 tzset();
982 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
983 EXPECT_EQ(12, tm.tm_hour);
984
985 setenv("TZ", "Asia/Magadan", 1);
986 tzset();
987 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
988 EXPECT_EQ(9, tm.tm_hour);
989}
Elliott Hughesea877162017-01-11 14:34:16 -0800990
991TEST(time, bug_31339449) {
992 // POSIX says localtime acts as if it calls tzset.
993 // tzset does two things:
994 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
995 // 2. it sets the global `tzname`.
996 // POSIX says localtime_r need not set `tzname` (2).
997 // Q: should localtime_r set the time zone (1)?
998 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
999
1000 // Pick a time, any time...
1001 time_t t = 1475619727;
1002
1003 // Call tzset with a specific timezone.
1004 setenv("TZ", "America/Atka", 1);
1005 tzset();
1006
1007 // If we change the timezone and call localtime, localtime should use the new timezone.
1008 setenv("TZ", "America/Los_Angeles", 1);
1009 struct tm* tm_p = localtime(&t);
1010 EXPECT_EQ(15, tm_p->tm_hour);
1011
1012 // Reset the timezone back.
1013 setenv("TZ", "America/Atka", 1);
1014 tzset();
1015
1016#if defined(__BIONIC__)
1017 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
1018 setenv("TZ", "America/Los_Angeles", 1);
1019 struct tm tm = {};
1020 localtime_r(&t, &tm);
1021 EXPECT_EQ(15, tm.tm_hour);
1022#else
1023 // The BSDs agree with us, but glibc gets this wrong.
1024#endif
1025}
Elliott Hughes5a29d542017-12-07 16:05:57 -08001026
1027TEST(time, asctime) {
1028 const struct tm tm = {};
1029 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
1030}
1031
1032TEST(time, asctime_r) {
1033 const struct tm tm = {};
1034 char buf[256];
1035 ASSERT_EQ(buf, asctime_r(&tm, buf));
1036 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
1037}
1038
1039TEST(time, ctime) {
1040 setenv("TZ", "UTC", 1);
1041 const time_t t = 0;
1042 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
1043}
1044
1045TEST(time, ctime_r) {
1046 setenv("TZ", "UTC", 1);
1047 const time_t t = 0;
1048 char buf[256];
1049 ASSERT_EQ(buf, ctime_r(&t, buf));
1050 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
1051}
Elliott Hughes81baaf22018-02-28 15:22:48 -08001052
1053// https://issuetracker.google.com/37128336
1054TEST(time, strftime_strptime_s) {
1055 char buf[32];
1056 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
1057
1058 setenv("TZ", "America/Los_Angeles", 1);
1059 strftime(buf, sizeof(buf), "<%s>", &tm0);
1060 EXPECT_STREQ("<378720000>", buf);
1061
1062 setenv("TZ", "UTC", 1);
1063 strftime(buf, sizeof(buf), "<%s>", &tm0);
1064 EXPECT_STREQ("<378691200>", buf);
1065
1066 struct tm tm;
1067
1068 setenv("TZ", "America/Los_Angeles", 1);
1069 tzset();
1070 memset(&tm, 0xff, sizeof(tm));
1071 char* p = strptime("378720000x", "%s", &tm);
1072 ASSERT_EQ('x', *p);
1073 EXPECT_EQ(0, tm.tm_sec);
1074 EXPECT_EQ(0, tm.tm_min);
1075 EXPECT_EQ(0, tm.tm_hour);
1076 EXPECT_EQ(1, tm.tm_mday);
1077 EXPECT_EQ(0, tm.tm_mon);
1078 EXPECT_EQ(82, tm.tm_year);
1079 EXPECT_EQ(5, tm.tm_wday);
1080 EXPECT_EQ(0, tm.tm_yday);
1081 EXPECT_EQ(0, tm.tm_isdst);
1082
1083 setenv("TZ", "UTC", 1);
1084 tzset();
1085 memset(&tm, 0xff, sizeof(tm));
1086 p = strptime("378691200x", "%s", &tm);
1087 ASSERT_EQ('x', *p);
1088 EXPECT_EQ(0, tm.tm_sec);
1089 EXPECT_EQ(0, tm.tm_min);
1090 EXPECT_EQ(0, tm.tm_hour);
1091 EXPECT_EQ(1, tm.tm_mday);
1092 EXPECT_EQ(0, tm.tm_mon);
1093 EXPECT_EQ(82, tm.tm_year);
1094 EXPECT_EQ(5, tm.tm_wday);
1095 EXPECT_EQ(0, tm.tm_yday);
1096 EXPECT_EQ(0, tm.tm_isdst);
1097}
1098
1099TEST(time, strptime_s_nothing) {
1100 struct tm tm;
1101 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
1102}
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001103
1104TEST(time, timespec_get) {
1105#if __BIONIC__
1106 timespec ts = {};
1107 ASSERT_EQ(0, timespec_get(&ts, 123));
1108 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
1109#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001110 GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21";
Elliott Hughesf98d87b2018-07-17 13:21:05 -07001111#endif
1112}
Elliott Hughes208fdd12020-06-05 17:19:00 -07001113
1114TEST(time, difftime) {
1115 ASSERT_EQ(1.0, difftime(1, 0));
Elliott Hughes7cebf832020-08-12 14:25:41 -07001116 ASSERT_EQ(-1.0, difftime(0, 1));
Elliott Hughes208fdd12020-06-05 17:19:00 -07001117}