blob: 50830ee1a1baf32b3ecea8e7ebaea63709337730 [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>
Yabin Cui95f1ee22015-01-13 19:53:15 -080027#include <atomic>
Elliott Hughese0175ca2013-03-14 14:38:08 -070028
Elliott Hughes71ba5892018-02-07 12:44:45 -080029#include "SignalUtils.h"
Elliott Hughes33697a02016-01-26 13:04:57 -080030#include "utils.h"
Elliott Hughese0175ca2013-03-14 14:38:08 -070031
Elliott Hughes04303f52014-09-18 16:11:59 -070032#include "private/bionic_constants.h"
33
Mark Salyzyn0b846e82017-12-20 08:56:18 -080034TEST(time, time) {
35 // Acquire time
36 time_t p1, t1 = time(&p1);
37 // valid?
38 ASSERT_NE(static_cast<time_t>(0), t1);
39 ASSERT_NE(static_cast<time_t>(-1), t1);
40 ASSERT_EQ(p1, t1);
41
42 // Acquire time one+ second later
43 usleep(1010000);
44 time_t p2, t2 = time(&p2);
45 // valid?
46 ASSERT_NE(static_cast<time_t>(0), t2);
47 ASSERT_NE(static_cast<time_t>(-1), t2);
48 ASSERT_EQ(p2, t2);
49
50 // Expect time progression
51 ASSERT_LT(p1, p2);
52 ASSERT_LE(t2 - t1, static_cast<time_t>(2));
53
54 // Expect nullptr call to produce same results
55 ASSERT_LE(t2, time(nullptr));
56 ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1));
57}
58
Elliott Hughesee178bf2013-07-12 11:25:20 -070059TEST(time, gmtime) {
60 time_t t = 0;
61 tm* broken_down = gmtime(&t);
Yi Kong32bc0fc2018-08-02 17:31:13 -070062 ASSERT_TRUE(broken_down != nullptr);
Elliott Hughesee178bf2013-07-12 11:25:20 -070063 ASSERT_EQ(0, broken_down->tm_sec);
64 ASSERT_EQ(0, broken_down->tm_min);
65 ASSERT_EQ(0, broken_down->tm_hour);
66 ASSERT_EQ(1, broken_down->tm_mday);
67 ASSERT_EQ(0, broken_down->tm_mon);
68 ASSERT_EQ(1970, broken_down->tm_year + 1900);
69}
Elliott Hughes7843d442013-08-22 11:37:32 -070070
Elliott Hughes5a29d542017-12-07 16:05:57 -080071TEST(time, gmtime_r) {
72 struct tm tm = {};
73 time_t t = 0;
74 struct tm* broken_down = gmtime_r(&t, &tm);
75 ASSERT_EQ(broken_down, &tm);
76 ASSERT_EQ(0, broken_down->tm_sec);
77 ASSERT_EQ(0, broken_down->tm_min);
78 ASSERT_EQ(0, broken_down->tm_hour);
79 ASSERT_EQ(1, broken_down->tm_mday);
80 ASSERT_EQ(0, broken_down->tm_mon);
81 ASSERT_EQ(1970, broken_down->tm_year + 1900);
82}
83
Elliott Hughes329103d2014-04-25 16:55:04 -070084static void* gmtime_no_stack_overflow_14313703_fn(void*) {
85 const char* original_tz = getenv("TZ");
86 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
87 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
88 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -070089 if (original_tz != nullptr) {
Elliott Hughes329103d2014-04-25 16:55:04 -070090 setenv("TZ", original_tz, 1);
91 }
92 tzset();
Yi Kong32bc0fc2018-08-02 17:31:13 -070093 return nullptr;
Elliott Hughes329103d2014-04-25 16:55:04 -070094}
95
96TEST(time, gmtime_no_stack_overflow_14313703) {
97 // Is it safe to call tzload on a thread with a small stack?
98 // http://b/14313703
99 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -0800100 pthread_attr_t a;
101 ASSERT_EQ(0, pthread_attr_init(&a));
102 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -0700103
104 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700105 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr));
Elliott Hughes43f7c872016-02-05 11:18:41 -0800106 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -0700107}
108
Satoru Takeuchi154e2022014-05-27 17:04:04 +0900109TEST(time, mktime_empty_TZ) {
110 // tzcode used to have a bug where it didn't reinitialize some internal state.
111
112 // Choose a time where DST is set.
113 struct tm t;
114 memset(&t, 0, sizeof(tm));
115 t.tm_year = 1980 - 1900;
116 t.tm_mon = 6;
117 t.tm_mday = 2;
118
119 setenv("TZ", "America/Los_Angeles", 1);
120 tzset();
121 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
122
123 memset(&t, 0, sizeof(tm));
124 t.tm_year = 1980 - 1900;
125 t.tm_mon = 6;
126 t.tm_mday = 2;
127
128 setenv("TZ", "", 1); // Implies UTC.
129 tzset();
130 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
131}
132
Elliott Hughes7843d442013-08-22 11:37:32 -0700133TEST(time, mktime_10310929) {
134 struct tm t;
135 memset(&t, 0, sizeof(tm));
136 t.tm_year = 200;
137 t.tm_mon = 2;
138 t.tm_mday = 10;
139
Elliott Hughes0c401522013-10-18 16:21:54 -0700140#if !defined(__LP64__)
141 // 32-bit bionic stupidly had a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700142 ASSERT_EQ(-1, mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700143 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700144#else
145 // Everyone else should be using a signed 64-bit time_t.
146 ASSERT_GE(sizeof(time_t) * 8, 64U);
147
148 setenv("TZ", "America/Los_Angeles", 1);
149 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700150 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700151 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700152 ASSERT_EQ(0, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700153
154 setenv("TZ", "UTC", 1);
155 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700156 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700157 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700158 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700159#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800160}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800161
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700162TEST(time, mktime_EOVERFLOW) {
163 struct tm t;
164 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700165
166 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
167 t.tm_year = 2016 - 1900;
168
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700169 t.tm_mon = 2;
170 t.tm_mday = 10;
171
172 errno = 0;
173 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
174 ASSERT_EQ(0, errno);
175
Elliott Hughes47126ed2016-09-06 13:25:53 -0700176 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700177 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700178
179 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700180 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
181 ASSERT_EQ(EOVERFLOW, errno);
182}
183
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700184TEST(time, strftime) {
185 setenv("TZ", "UTC", 1);
186
187 struct tm t;
188 memset(&t, 0, sizeof(tm));
189 t.tm_year = 200;
190 t.tm_mon = 2;
191 t.tm_mday = 10;
192
193 char buf[64];
194
195 // Seconds since the epoch.
196#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
197 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
198 EXPECT_STREQ("4108320000", buf);
199#endif
200
201 // Date and time as text.
202 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
203 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
204}
205
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800206TEST(time, strftime_null_tm_zone) {
207 // Netflix on Nexus Player wouldn't start (http://b/25170306).
208 struct tm t;
209 memset(&t, 0, sizeof(tm));
210
211 char buf[64];
212
213 setenv("TZ", "America/Los_Angeles", 1);
214 tzset();
215
216 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
217 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
218 EXPECT_STREQ("<PST>", buf);
219
220#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
221 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
222 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
223 EXPECT_STREQ("<PDT>", buf);
224
225 t.tm_isdst = -123; // "and negative if the information is not available".
226 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
227 EXPECT_STREQ("<>", buf);
228#endif
229
230 setenv("TZ", "UTC", 1);
231 tzset();
232
233 t.tm_isdst = 0;
234 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
235 EXPECT_STREQ("<UTC>", buf);
236
237#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
238 t.tm_isdst = 1; // UTC has no DST.
239 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
240 EXPECT_STREQ("<>", buf);
241#endif
242}
243
Elliott Hughes0a610d02016-07-29 14:04:17 -0700244TEST(time, strftime_l) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700245 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Elliott Hughes0a610d02016-07-29 14:04:17 -0700246 locale_t old_locale = uselocale(cloc);
247
248 setenv("TZ", "UTC", 1);
249
250 struct tm t;
251 memset(&t, 0, sizeof(tm));
252 t.tm_year = 200;
253 t.tm_mon = 2;
254 t.tm_mday = 10;
255
256 // Date and time as text.
257 char buf[64];
258 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
259 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
260
261 uselocale(old_locale);
262 freelocale(cloc);
263}
264
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700265TEST(time, strptime) {
266 setenv("TZ", "UTC", 1);
267
268 struct tm t;
269 char buf[64];
270
271 memset(&t, 0, sizeof(t));
272 strptime("11:14", "%R", &t);
273 strftime(buf, sizeof(buf), "%H:%M", &t);
274 EXPECT_STREQ("11:14", buf);
275
276 memset(&t, 0, sizeof(t));
277 strptime("09:41:53", "%T", &t);
278 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
279 EXPECT_STREQ("09:41:53", buf);
280}
281
Elliott Hughes3376c232018-02-13 23:14:12 -0800282TEST(time, strptime_l) {
283 setenv("TZ", "UTC", 1);
284
285 struct tm t;
286 char buf[64];
287
288 memset(&t, 0, sizeof(t));
289 strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE);
290 strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE);
291 EXPECT_STREQ("11:14", buf);
292
293 memset(&t, 0, sizeof(t));
294 strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE);
295 strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE);
296 EXPECT_STREQ("09:41:53", buf);
297}
298
Elliott Hughes4b558f52014-03-04 15:58:02 -0800299void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
300 itimerspec ts;
301 ts.it_value.tv_sec = value_s;
302 ts.it_value.tv_nsec = value_ns;
303 ts.it_interval.tv_sec = interval_s;
304 ts.it_interval.tv_nsec = interval_ns;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700305 ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800306}
307
308static void NoOpNotifyFunction(sigval_t) {
309}
310
311TEST(time, timer_create) {
312 sigevent_t se;
313 memset(&se, 0, sizeof(se));
314 se.sigev_notify = SIGEV_THREAD;
315 se.sigev_notify_function = NoOpNotifyFunction;
316 timer_t timer_id;
317 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
318
Elliott Hughes33697a02016-01-26 13:04:57 -0800319 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800320 ASSERT_NE(-1, pid) << strerror(errno);
321
322 if (pid == 0) {
323 // Timers are not inherited by the child.
324 ASSERT_EQ(-1, timer_delete(timer_id));
325 ASSERT_EQ(EINVAL, errno);
326 _exit(0);
327 }
328
Elliott Hughes33697a02016-01-26 13:04:57 -0800329 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800330
331 ASSERT_EQ(0, timer_delete(timer_id));
332}
333
Yabin Cui95f1ee22015-01-13 19:53:15 -0800334static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800335static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
336 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
337 ASSERT_EQ(SIGUSR1, signal_number);
338}
339
340TEST(time, timer_create_SIGEV_SIGNAL) {
341 sigevent_t se;
342 memset(&se, 0, sizeof(se));
343 se.sigev_notify = SIGEV_SIGNAL;
344 se.sigev_signo = SIGUSR1;
345
346 timer_t timer_id;
347 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
348
Yabin Cui95f1ee22015-01-13 19:53:15 -0800349 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800350 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
351
352 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
353
354 itimerspec ts;
355 ts.it_value.tv_sec = 0;
356 ts.it_value.tv_nsec = 1;
357 ts.it_interval.tv_sec = 0;
358 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700359 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800360
361 usleep(500000);
362 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
363}
364
365struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800366 private:
367 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800368 timer_t timer_id;
369 sigevent_t se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700370 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800371
Elliott Hughes4b558f52014-03-04 15:58:02 -0800372 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700373 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800374 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700375 timer_valid = true;
376 }
377
Yabin Cui95f1ee22015-01-13 19:53:15 -0800378 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700379 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800380 memset(&se, 0, sizeof(se));
381 se.sigev_notify = SIGEV_THREAD;
382 se.sigev_notify_function = fn;
383 se.sigev_value.sival_ptr = this;
384 Create();
385 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700386 void DeleteTimer() {
387 ASSERT_TRUE(timer_valid);
388 ASSERT_EQ(0, timer_delete(timer_id));
389 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800390 }
391
392 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700393 if (timer_valid) {
394 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800395 }
396 }
397
Yabin Cui95f1ee22015-01-13 19:53:15 -0800398 int Value() const {
399 return value;
400 }
401
Christopher Ferris62d84b12014-10-20 19:09:19 -0700402 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
403 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
404 }
405
406 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800407 int current_value = value;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700408 time_t start = time(nullptr);
409 while (current_value == value && (time(nullptr) - start) < 5) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700410 }
411 return current_value != value;
412 }
413
Elliott Hughes4b558f52014-03-04 15:58:02 -0800414 static void CountNotifyFunction(sigval_t value) {
415 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
416 ++cd->value;
417 }
418
419 static void CountAndDisarmNotifyFunction(sigval_t value) {
420 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
421 ++cd->value;
422
423 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700424 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800425 }
426};
427
428TEST(time, timer_settime_0) {
429 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800430 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800431
Yabin Cuibf572d92015-08-11 11:23:16 -0700432 counter.SetTime(0, 500000000, 1, 0);
433 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800434
435 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800436 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800437}
438
439TEST(time, timer_settime_repeats) {
440 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800441 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800442
Christopher Ferris62d84b12014-10-20 19:09:19 -0700443 counter.SetTime(0, 1, 0, 10);
444 ASSERT_TRUE(counter.ValueUpdated());
445 ASSERT_TRUE(counter.ValueUpdated());
446 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800447 counter.DeleteTimer();
448 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
449 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800450}
451
Yabin Cui95f1ee22015-01-13 19:53:15 -0800452static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800453static void timer_create_NULL_signal_handler(int signal_number) {
454 ++timer_create_NULL_signal_handler_invocation_count;
455 ASSERT_EQ(SIGALRM, signal_number);
456}
457
458TEST(time, timer_create_NULL) {
459 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
460 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700461 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800462
Yabin Cui95f1ee22015-01-13 19:53:15 -0800463 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800464 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
465
466 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
467
468 SetTime(timer_id, 0, 1, 0, 0);
469 usleep(500000);
470
471 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
472}
473
474TEST(time, timer_create_EINVAL) {
475 clockid_t invalid_clock = 16;
476
477 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
478 timer_t timer_id;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700479 ASSERT_EQ(-1, timer_create(invalid_clock, nullptr, &timer_id));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800480 ASSERT_EQ(EINVAL, errno);
481
482 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
483 sigevent_t se;
484 memset(&se, 0, sizeof(se));
485 se.sigev_notify = SIGEV_THREAD;
486 se.sigev_notify_function = NoOpNotifyFunction;
487 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
488 ASSERT_EQ(EINVAL, errno);
489}
490
Elliott Hughes4b558f52014-03-04 15:58:02 -0800491TEST(time, timer_create_multiple) {
492 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800493 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800494 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800495
Yabin Cui95f1ee22015-01-13 19:53:15 -0800496 ASSERT_EQ(0, counter1.Value());
497 ASSERT_EQ(0, counter2.Value());
498 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800499
Yabin Cui410c1ad2015-06-18 16:19:02 -0700500 counter2.SetTime(0, 500000000, 0, 0);
501 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800502
Yabin Cui95f1ee22015-01-13 19:53:15 -0800503 EXPECT_EQ(0, counter1.Value());
504 EXPECT_EQ(1, counter2.Value());
505 EXPECT_EQ(0, counter3.Value());
506}
507
508// Test to verify that disarming a repeatable timer disables the callbacks.
509TEST(time, timer_disarm_terminates) {
510 Counter counter(Counter::CountNotifyFunction);
511 ASSERT_EQ(0, counter.Value());
512
513 counter.SetTime(0, 1, 0, 1);
514 ASSERT_TRUE(counter.ValueUpdated());
515 ASSERT_TRUE(counter.ValueUpdated());
516 ASSERT_TRUE(counter.ValueUpdated());
517
518 counter.SetTime(0, 0, 0, 0);
519 // Add a sleep as the kernel may have pending events when the timer is disarmed.
520 usleep(500000);
521 int value = counter.Value();
522 usleep(500000);
523
524 // Verify the counter has not been incremented.
525 ASSERT_EQ(value, counter.Value());
526}
527
528// Test to verify that deleting a repeatable timer disables the callbacks.
529TEST(time, timer_delete_terminates) {
530 Counter counter(Counter::CountNotifyFunction);
531 ASSERT_EQ(0, counter.Value());
532
533 counter.SetTime(0, 1, 0, 1);
534 ASSERT_TRUE(counter.ValueUpdated());
535 ASSERT_TRUE(counter.ValueUpdated());
536 ASSERT_TRUE(counter.ValueUpdated());
537
538 counter.DeleteTimer();
539 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
540 usleep(500000);
541 int value = counter.Value();
542 usleep(500000);
543
544 // Verify the counter has not been incremented.
545 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800546}
Christopher Ferris753ad772014-03-20 20:47:45 -0700547
548struct TimerDeleteData {
549 timer_t timer_id;
Elliott Hughes11859d42017-02-13 17:59:29 -0800550 pid_t tid;
Christopher Ferris753ad772014-03-20 20:47:45 -0700551 volatile bool complete;
552};
553
554static void TimerDeleteCallback(sigval_t value) {
555 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
556
Elliott Hughes11859d42017-02-13 17:59:29 -0800557 tdd->tid = gettid();
Christopher Ferris753ad772014-03-20 20:47:45 -0700558 timer_delete(tdd->timer_id);
559 tdd->complete = true;
560}
561
562TEST(time, timer_delete_from_timer_thread) {
563 TimerDeleteData tdd;
564 sigevent_t se;
565
566 memset(&se, 0, sizeof(se));
567 se.sigev_notify = SIGEV_THREAD;
568 se.sigev_notify_function = TimerDeleteCallback;
569 se.sigev_value.sival_ptr = &tdd;
570
571 tdd.complete = false;
572 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
573
574 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800575 ts.it_value.tv_sec = 1;
576 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700577 ts.it_interval.tv_sec = 0;
578 ts.it_interval.tv_nsec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700579 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr));
Christopher Ferris753ad772014-03-20 20:47:45 -0700580
Yi Kong32bc0fc2018-08-02 17:31:13 -0700581 time_t cur_time = time(nullptr);
582 while (!tdd.complete && (time(nullptr) - cur_time) < 5);
Christopher Ferris753ad772014-03-20 20:47:45 -0700583 ASSERT_TRUE(tdd.complete);
584
585#if defined(__BIONIC__)
586 // Since bionic timers are implemented by creating a thread to handle the
587 // callback, verify that the thread actually completes.
588 cur_time = time(NULL);
Elliott Hughes11859d42017-02-13 17:59:29 -0800589 while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5);
590 ASSERT_EQ(-1, kill(tdd.tid, 0));
591 ASSERT_EQ(ESRCH, errno);
Christopher Ferris753ad772014-03-20 20:47:45 -0700592#endif
593}
Elliott Hughes625993d2014-07-15 16:53:13 -0700594
595TEST(time, clock_gettime) {
596 // Try to ensure that our vdso clock_gettime is working.
597 timespec ts1;
598 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
599 timespec ts2;
600 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
601
602 // What's the difference between the two?
603 ts2.tv_sec -= ts1.tv_sec;
604 ts2.tv_nsec -= ts1.tv_nsec;
605 if (ts2.tv_nsec < 0) {
606 --ts2.tv_sec;
Elliott Hughes04303f52014-09-18 16:11:59 -0700607 ts2.tv_nsec += NS_PER_S;
Elliott Hughes625993d2014-07-15 16:53:13 -0700608 }
609
Elliott Hughes8dff0bb2019-01-17 12:32:32 -0800610 // To try to avoid flakiness we'll accept answers within 10,000,000ns (0.01s).
Elliott Hughes625993d2014-07-15 16:53:13 -0700611 ASSERT_EQ(0, ts2.tv_sec);
Elliott Hughes8dff0bb2019-01-17 12:32:32 -0800612 ASSERT_LT(ts2.tv_nsec, 10'000'000);
Elliott Hughes625993d2014-07-15 16:53:13 -0700613}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700614
Elliott Hughes3a8f75d2017-10-05 10:33:18 -0700615TEST(time, clock_gettime_CLOCK_REALTIME) {
616 timespec ts;
617 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
618}
619
620TEST(time, clock_gettime_CLOCK_MONOTONIC) {
621 timespec ts;
622 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
623}
624
625TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) {
626 timespec ts;
627 ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts));
628}
629
630TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) {
631 timespec ts;
632 ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts));
633}
634
635TEST(time, clock_gettime_CLOCK_BOOTTIME) {
636 timespec ts;
637 ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts));
638}
639
Mark Salyzyn6a5a99f2017-11-21 12:29:06 -0800640TEST(time, clock_gettime_unknown) {
641 errno = 0;
642 timespec ts;
643 ASSERT_EQ(-1, clock_gettime(-1, &ts));
644 ASSERT_EQ(EINVAL, errno);
645}
646
647TEST(time, clock_getres_CLOCK_REALTIME) {
648 timespec ts;
649 ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts));
650 ASSERT_EQ(1, ts.tv_nsec);
651 ASSERT_EQ(0, ts.tv_sec);
652}
653
654TEST(time, clock_getres_CLOCK_MONOTONIC) {
655 timespec ts;
656 ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts));
657 ASSERT_EQ(1, ts.tv_nsec);
658 ASSERT_EQ(0, ts.tv_sec);
659}
660
661TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) {
662 timespec ts;
663 ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts));
664}
665
666TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) {
667 timespec ts;
668 ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts));
669}
670
671TEST(time, clock_getres_CLOCK_BOOTTIME) {
672 timespec ts;
673 ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts));
674 ASSERT_EQ(1, ts.tv_nsec);
675 ASSERT_EQ(0, ts.tv_sec);
676}
677
678TEST(time, clock_getres_unknown) {
679 errno = 0;
680 timespec ts = { -1, -1 };
681 ASSERT_EQ(-1, clock_getres(-1, &ts));
682 ASSERT_EQ(EINVAL, errno);
683 ASSERT_EQ(-1, ts.tv_nsec);
684 ASSERT_EQ(-1, ts.tv_sec);
685}
686
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700687TEST(time, clock) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900688 // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
689 clock_t t0 = clock();
690 sleep(1);
691 clock_t t1 = clock();
692 ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
693}
694
Elliott Hughesb4413592017-11-29 18:17:06 -0800695static pid_t GetInvalidPid() {
696 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose};
Yabin Cuid5c65272014-11-26 14:04:26 -0800697 long pid_max;
Elliott Hughesb4413592017-11-29 18:17:06 -0800698 fscanf(fp.get(), "%ld", &pid_max);
699 return static_cast<pid_t>(pid_max + 1);
Yabin Cuid5c65272014-11-26 14:04:26 -0800700}
701
Elliott Hughesb4413592017-11-29 18:17:06 -0800702TEST(time, clock_getcpuclockid_current) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800703 clockid_t clockid;
704 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
Yabin Cuid5c65272014-11-26 14:04:26 -0800705 timespec ts;
706 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800707}
Yabin Cuid5c65272014-11-26 14:04:26 -0800708
Elliott Hughesb4413592017-11-29 18:17:06 -0800709TEST(time, clock_getcpuclockid_parent) {
710 clockid_t clockid;
Yabin Cuid5c65272014-11-26 14:04:26 -0800711 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
Elliott Hughesb4413592017-11-29 18:17:06 -0800712 timespec ts;
Yabin Cuid5c65272014-11-26 14:04:26 -0800713 ASSERT_EQ(0, clock_gettime(clockid, &ts));
Elliott Hughesb4413592017-11-29 18:17:06 -0800714}
Yabin Cuid5c65272014-11-26 14:04:26 -0800715
Elliott Hughesb4413592017-11-29 18:17:06 -0800716TEST(time, clock_getcpuclockid_ESRCH) {
Yabin Cuid5c65272014-11-26 14:04:26 -0800717 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
718 errno = 0;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800719 // If this fails, your kernel needs commit e1b6b6ce to be backported.
Elliott Hughesb4413592017-11-29 18:17:06 -0800720 clockid_t clockid;
Mark Salyzyn71b81a22017-11-28 13:48:45 -0800721 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n"
722 << "Please ensure that the following kernel patches or their replacements have been applied:\n"
723 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
724 << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n"
725 << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
726 << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n";
Yabin Cuid5c65272014-11-26 14:04:26 -0800727 ASSERT_EQ(0, errno);
728}
729
Haruki Hasegawa18160252014-10-13 00:50:47 +0900730TEST(time, clock_settime) {
731 errno = 0;
732 timespec ts;
733 ASSERT_EQ(-1, clock_settime(-1, &ts));
734 ASSERT_EQ(EINVAL, errno);
735}
736
737TEST(time, clock_nanosleep) {
738 timespec in;
739 timespec out;
740 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700741}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700742
743TEST(time, clock_nanosleep_thread_cputime_id) {
744 timespec in;
745 in.tv_sec = 1;
746 in.tv_nsec = 0;
747 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
748}
Elliott Hughes12443702016-10-19 16:02:31 -0700749
750TEST(time, bug_31938693) {
751 // User-visible symptoms in N:
752 // http://b/31938693
753 // https://code.google.com/p/android/issues/detail?id=225132
754
755 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
756 // http://b/31848040
757
758 // This isn't a great test, because very few time zones were actually affected, and there's
759 // no real logic to which ones were affected: it was just a coincidence of the data that came
760 // after them in the tzdata file.
761
762 time_t t = 1475619727;
763 struct tm tm;
764
765 setenv("TZ", "America/Los_Angeles", 1);
766 tzset();
767 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
768 EXPECT_EQ(15, tm.tm_hour);
769
770 setenv("TZ", "Europe/London", 1);
771 tzset();
772 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
773 EXPECT_EQ(23, tm.tm_hour);
774
775 setenv("TZ", "America/Atka", 1);
776 tzset();
777 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
778 EXPECT_EQ(13, tm.tm_hour);
779
780 setenv("TZ", "Pacific/Apia", 1);
781 tzset();
782 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
783 EXPECT_EQ(12, tm.tm_hour);
784
785 setenv("TZ", "Pacific/Honolulu", 1);
786 tzset();
787 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
788 EXPECT_EQ(12, tm.tm_hour);
789
790 setenv("TZ", "Asia/Magadan", 1);
791 tzset();
792 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
793 EXPECT_EQ(9, tm.tm_hour);
794}
Elliott Hughesea877162017-01-11 14:34:16 -0800795
796TEST(time, bug_31339449) {
797 // POSIX says localtime acts as if it calls tzset.
798 // tzset does two things:
799 // 1. it sets the time zone ctime/localtime/mktime/strftime will use.
800 // 2. it sets the global `tzname`.
801 // POSIX says localtime_r need not set `tzname` (2).
802 // Q: should localtime_r set the time zone (1)?
803 // Upstream tzcode (and glibc) answer "no", everyone else answers "yes".
804
805 // Pick a time, any time...
806 time_t t = 1475619727;
807
808 // Call tzset with a specific timezone.
809 setenv("TZ", "America/Atka", 1);
810 tzset();
811
812 // If we change the timezone and call localtime, localtime should use the new timezone.
813 setenv("TZ", "America/Los_Angeles", 1);
814 struct tm* tm_p = localtime(&t);
815 EXPECT_EQ(15, tm_p->tm_hour);
816
817 // Reset the timezone back.
818 setenv("TZ", "America/Atka", 1);
819 tzset();
820
821#if defined(__BIONIC__)
822 // If we change the timezone again and call localtime_r, localtime_r should use the new timezone.
823 setenv("TZ", "America/Los_Angeles", 1);
824 struct tm tm = {};
825 localtime_r(&t, &tm);
826 EXPECT_EQ(15, tm.tm_hour);
827#else
828 // The BSDs agree with us, but glibc gets this wrong.
829#endif
830}
Elliott Hughes5a29d542017-12-07 16:05:57 -0800831
832TEST(time, asctime) {
833 const struct tm tm = {};
834 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm));
835}
836
837TEST(time, asctime_r) {
838 const struct tm tm = {};
839 char buf[256];
840 ASSERT_EQ(buf, asctime_r(&tm, buf));
841 ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf);
842}
843
844TEST(time, ctime) {
845 setenv("TZ", "UTC", 1);
846 const time_t t = 0;
847 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t));
848}
849
850TEST(time, ctime_r) {
851 setenv("TZ", "UTC", 1);
852 const time_t t = 0;
853 char buf[256];
854 ASSERT_EQ(buf, ctime_r(&t, buf));
855 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf);
856}
Elliott Hughes81baaf22018-02-28 15:22:48 -0800857
858// https://issuetracker.google.com/37128336
859TEST(time, strftime_strptime_s) {
860 char buf[32];
861 const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 };
862
863 setenv("TZ", "America/Los_Angeles", 1);
864 strftime(buf, sizeof(buf), "<%s>", &tm0);
865 EXPECT_STREQ("<378720000>", buf);
866
867 setenv("TZ", "UTC", 1);
868 strftime(buf, sizeof(buf), "<%s>", &tm0);
869 EXPECT_STREQ("<378691200>", buf);
870
871 struct tm tm;
872
873 setenv("TZ", "America/Los_Angeles", 1);
874 tzset();
875 memset(&tm, 0xff, sizeof(tm));
876 char* p = strptime("378720000x", "%s", &tm);
877 ASSERT_EQ('x', *p);
878 EXPECT_EQ(0, tm.tm_sec);
879 EXPECT_EQ(0, tm.tm_min);
880 EXPECT_EQ(0, tm.tm_hour);
881 EXPECT_EQ(1, tm.tm_mday);
882 EXPECT_EQ(0, tm.tm_mon);
883 EXPECT_EQ(82, tm.tm_year);
884 EXPECT_EQ(5, tm.tm_wday);
885 EXPECT_EQ(0, tm.tm_yday);
886 EXPECT_EQ(0, tm.tm_isdst);
887
888 setenv("TZ", "UTC", 1);
889 tzset();
890 memset(&tm, 0xff, sizeof(tm));
891 p = strptime("378691200x", "%s", &tm);
892 ASSERT_EQ('x', *p);
893 EXPECT_EQ(0, tm.tm_sec);
894 EXPECT_EQ(0, tm.tm_min);
895 EXPECT_EQ(0, tm.tm_hour);
896 EXPECT_EQ(1, tm.tm_mday);
897 EXPECT_EQ(0, tm.tm_mon);
898 EXPECT_EQ(82, tm.tm_year);
899 EXPECT_EQ(5, tm.tm_wday);
900 EXPECT_EQ(0, tm.tm_yday);
901 EXPECT_EQ(0, tm.tm_isdst);
902}
903
904TEST(time, strptime_s_nothing) {
905 struct tm tm;
906 ASSERT_EQ(nullptr, strptime("x", "%s", &tm));
907}
Elliott Hughesf98d87b2018-07-17 13:21:05 -0700908
909TEST(time, timespec_get) {
910#if __BIONIC__
911 timespec ts = {};
912 ASSERT_EQ(0, timespec_get(&ts, 123));
913 ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC));
914#else
915 GTEST_LOG_(INFO) << "glibc doesn't have timespec_get until 2.21\n";
916#endif
917}