blob: 914cb61af658c5e7b2fd152715a5bdffbc2df4da [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 Hughes4b558f52014-03-04 15:58:02 -080029#include "ScopedSignalHandler.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
Elliott Hughesee178bf2013-07-12 11:25:20 -070034TEST(time, gmtime) {
35 time_t t = 0;
36 tm* broken_down = gmtime(&t);
37 ASSERT_TRUE(broken_down != NULL);
38 ASSERT_EQ(0, broken_down->tm_sec);
39 ASSERT_EQ(0, broken_down->tm_min);
40 ASSERT_EQ(0, broken_down->tm_hour);
41 ASSERT_EQ(1, broken_down->tm_mday);
42 ASSERT_EQ(0, broken_down->tm_mon);
43 ASSERT_EQ(1970, broken_down->tm_year + 1900);
44}
Elliott Hughes7843d442013-08-22 11:37:32 -070045
Elliott Hughes329103d2014-04-25 16:55:04 -070046static void* gmtime_no_stack_overflow_14313703_fn(void*) {
47 const char* original_tz = getenv("TZ");
48 // Ensure we'll actually have to enter tzload by using a time zone that doesn't exist.
49 setenv("TZ", "gmtime_stack_overflow_14313703", 1);
50 tzset();
51 if (original_tz != NULL) {
52 setenv("TZ", original_tz, 1);
53 }
54 tzset();
55 return NULL;
56}
57
58TEST(time, gmtime_no_stack_overflow_14313703) {
59 // Is it safe to call tzload on a thread with a small stack?
60 // http://b/14313703
61 // https://code.google.com/p/android/issues/detail?id=61130
Elliott Hughes43f7c872016-02-05 11:18:41 -080062 pthread_attr_t a;
63 ASSERT_EQ(0, pthread_attr_init(&a));
64 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
Elliott Hughes329103d2014-04-25 16:55:04 -070065
66 pthread_t t;
Elliott Hughes43f7c872016-02-05 11:18:41 -080067 ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, NULL));
68 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes329103d2014-04-25 16:55:04 -070069}
70
Satoru Takeuchi154e2022014-05-27 17:04:04 +090071TEST(time, mktime_empty_TZ) {
72 // tzcode used to have a bug where it didn't reinitialize some internal state.
73
74 // Choose a time where DST is set.
75 struct tm t;
76 memset(&t, 0, sizeof(tm));
77 t.tm_year = 1980 - 1900;
78 t.tm_mon = 6;
79 t.tm_mday = 2;
80
81 setenv("TZ", "America/Los_Angeles", 1);
82 tzset();
83 ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t));
84
85 memset(&t, 0, sizeof(tm));
86 t.tm_year = 1980 - 1900;
87 t.tm_mon = 6;
88 t.tm_mday = 2;
89
90 setenv("TZ", "", 1); // Implies UTC.
91 tzset();
92 ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t));
93}
94
Elliott Hughes7843d442013-08-22 11:37:32 -070095TEST(time, mktime_10310929) {
96 struct tm t;
97 memset(&t, 0, sizeof(tm));
98 t.tm_year = 200;
99 t.tm_mon = 2;
100 t.tm_mday = 10;
101
Elliott Hughes0c401522013-10-18 16:21:54 -0700102#if !defined(__LP64__)
103 // 32-bit bionic stupidly had a signed 32-bit time_t.
Elliott Hughes7843d442013-08-22 11:37:32 -0700104 ASSERT_EQ(-1, mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700105 ASSERT_EQ(EOVERFLOW, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700106#else
107 // Everyone else should be using a signed 64-bit time_t.
108 ASSERT_GE(sizeof(time_t) * 8, 64U);
109
110 setenv("TZ", "America/Los_Angeles", 1);
111 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700112 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700113 ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700114 ASSERT_EQ(0, errno);
Elliott Hughes0c401522013-10-18 16:21:54 -0700115
116 setenv("TZ", "UTC", 1);
117 tzset();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700118 errno = 0;
Elliott Hughes0c401522013-10-18 16:21:54 -0700119 ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t));
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700120 ASSERT_EQ(0, errno);
Elliott Hughes7843d442013-08-22 11:37:32 -0700121#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800122}
Elliott Hughes4b558f52014-03-04 15:58:02 -0800123
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700124TEST(time, mktime_EOVERFLOW) {
125 struct tm t;
126 memset(&t, 0, sizeof(tm));
Elliott Hughes47126ed2016-09-06 13:25:53 -0700127
128 // LP32 year range is 1901-2038, so this year is guaranteed not to overflow.
129 t.tm_year = 2016 - 1900;
130
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700131 t.tm_mon = 2;
132 t.tm_mday = 10;
133
134 errno = 0;
135 ASSERT_NE(static_cast<time_t>(-1), mktime(&t));
136 ASSERT_EQ(0, errno);
137
Elliott Hughes47126ed2016-09-06 13:25:53 -0700138 // This will overflow for LP32 or LP64.
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700139 t.tm_year = INT_MAX;
Elliott Hughes47126ed2016-09-06 13:25:53 -0700140
141 errno = 0;
Elliott Hughesf8ebaa42016-08-12 16:28:36 -0700142 ASSERT_EQ(static_cast<time_t>(-1), mktime(&t));
143 ASSERT_EQ(EOVERFLOW, errno);
144}
145
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700146TEST(time, strftime) {
147 setenv("TZ", "UTC", 1);
148
149 struct tm t;
150 memset(&t, 0, sizeof(tm));
151 t.tm_year = 200;
152 t.tm_mon = 2;
153 t.tm_mday = 10;
154
155 char buf[64];
156
157 // Seconds since the epoch.
158#if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc.
159 EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t));
160 EXPECT_STREQ("4108320000", buf);
161#endif
162
163 // Date and time as text.
164 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t));
165 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
166}
167
Elliott Hughesa9cac4c2015-11-12 16:51:31 -0800168TEST(time, strftime_null_tm_zone) {
169 // Netflix on Nexus Player wouldn't start (http://b/25170306).
170 struct tm t;
171 memset(&t, 0, sizeof(tm));
172
173 char buf[64];
174
175 setenv("TZ", "America/Los_Angeles", 1);
176 tzset();
177
178 t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect".
179 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
180 EXPECT_STREQ("<PST>", buf);
181
182#if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1.
183 t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect"
184 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
185 EXPECT_STREQ("<PDT>", buf);
186
187 t.tm_isdst = -123; // "and negative if the information is not available".
188 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
189 EXPECT_STREQ("<>", buf);
190#endif
191
192 setenv("TZ", "UTC", 1);
193 tzset();
194
195 t.tm_isdst = 0;
196 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t));
197 EXPECT_STREQ("<UTC>", buf);
198
199#if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC".
200 t.tm_isdst = 1; // UTC has no DST.
201 EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t));
202 EXPECT_STREQ("<>", buf);
203#endif
204}
205
Elliott Hughes0a610d02016-07-29 14:04:17 -0700206TEST(time, strftime_l) {
207 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
208 locale_t old_locale = uselocale(cloc);
209
210 setenv("TZ", "UTC", 1);
211
212 struct tm t;
213 memset(&t, 0, sizeof(tm));
214 t.tm_year = 200;
215 t.tm_mon = 2;
216 t.tm_mday = 10;
217
218 // Date and time as text.
219 char buf[64];
220 EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc));
221 EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf);
222
223 uselocale(old_locale);
224 freelocale(cloc);
225}
226
Elliott Hughes3e3409a2014-03-10 18:19:03 -0700227TEST(time, strptime) {
228 setenv("TZ", "UTC", 1);
229
230 struct tm t;
231 char buf[64];
232
233 memset(&t, 0, sizeof(t));
234 strptime("11:14", "%R", &t);
235 strftime(buf, sizeof(buf), "%H:%M", &t);
236 EXPECT_STREQ("11:14", buf);
237
238 memset(&t, 0, sizeof(t));
239 strptime("09:41:53", "%T", &t);
240 strftime(buf, sizeof(buf), "%H:%M:%S", &t);
241 EXPECT_STREQ("09:41:53", buf);
242}
243
Elliott Hughes4b558f52014-03-04 15:58:02 -0800244void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
245 itimerspec ts;
246 ts.it_value.tv_sec = value_s;
247 ts.it_value.tv_nsec = value_ns;
248 ts.it_interval.tv_sec = interval_s;
249 ts.it_interval.tv_nsec = interval_ns;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700250 ASSERT_EQ(0, timer_settime(t, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800251}
252
253static void NoOpNotifyFunction(sigval_t) {
254}
255
256TEST(time, timer_create) {
257 sigevent_t se;
258 memset(&se, 0, sizeof(se));
259 se.sigev_notify = SIGEV_THREAD;
260 se.sigev_notify_function = NoOpNotifyFunction;
261 timer_t timer_id;
262 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
263
Elliott Hughes33697a02016-01-26 13:04:57 -0800264 pid_t pid = fork();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800265 ASSERT_NE(-1, pid) << strerror(errno);
266
267 if (pid == 0) {
268 // Timers are not inherited by the child.
269 ASSERT_EQ(-1, timer_delete(timer_id));
270 ASSERT_EQ(EINVAL, errno);
271 _exit(0);
272 }
273
Elliott Hughes33697a02016-01-26 13:04:57 -0800274 AssertChildExited(pid, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800275
276 ASSERT_EQ(0, timer_delete(timer_id));
277}
278
Yabin Cui95f1ee22015-01-13 19:53:15 -0800279static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800280static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) {
281 ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count;
282 ASSERT_EQ(SIGUSR1, signal_number);
283}
284
285TEST(time, timer_create_SIGEV_SIGNAL) {
286 sigevent_t se;
287 memset(&se, 0, sizeof(se));
288 se.sigev_notify = SIGEV_SIGNAL;
289 se.sigev_signo = SIGUSR1;
290
291 timer_t timer_id;
292 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
293
Yabin Cui95f1ee22015-01-13 19:53:15 -0800294 timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800295 ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler);
296
297 ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
298
299 itimerspec ts;
300 ts.it_value.tv_sec = 0;
301 ts.it_value.tv_nsec = 1;
302 ts.it_interval.tv_sec = 0;
303 ts.it_interval.tv_nsec = 0;
Yabin Cuid1ade7c2015-06-18 17:01:11 -0700304 ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, NULL));
Elliott Hughes4b558f52014-03-04 15:58:02 -0800305
306 usleep(500000);
307 ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count);
308}
309
310struct Counter {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800311 private:
312 std::atomic<int> value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800313 timer_t timer_id;
314 sigevent_t se;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700315 bool timer_valid;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800316
Elliott Hughes4b558f52014-03-04 15:58:02 -0800317 void Create() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700318 ASSERT_FALSE(timer_valid);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800319 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id));
Christopher Ferris62d84b12014-10-20 19:09:19 -0700320 timer_valid = true;
321 }
322
Yabin Cui95f1ee22015-01-13 19:53:15 -0800323 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700324 explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800325 memset(&se, 0, sizeof(se));
326 se.sigev_notify = SIGEV_THREAD;
327 se.sigev_notify_function = fn;
328 se.sigev_value.sival_ptr = this;
329 Create();
330 }
Christopher Ferris62d84b12014-10-20 19:09:19 -0700331 void DeleteTimer() {
332 ASSERT_TRUE(timer_valid);
333 ASSERT_EQ(0, timer_delete(timer_id));
334 timer_valid = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800335 }
336
337 ~Counter() {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700338 if (timer_valid) {
339 DeleteTimer();
Elliott Hughes4b558f52014-03-04 15:58:02 -0800340 }
341 }
342
Yabin Cui95f1ee22015-01-13 19:53:15 -0800343 int Value() const {
344 return value;
345 }
346
Christopher Ferris62d84b12014-10-20 19:09:19 -0700347 void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) {
348 ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns);
349 }
350
351 bool ValueUpdated() {
Yabin Cui95f1ee22015-01-13 19:53:15 -0800352 int current_value = value;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700353 time_t start = time(NULL);
354 while (current_value == value && (time(NULL) - start) < 5) {
355 }
356 return current_value != value;
357 }
358
Elliott Hughes4b558f52014-03-04 15:58:02 -0800359 static void CountNotifyFunction(sigval_t value) {
360 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
361 ++cd->value;
362 }
363
364 static void CountAndDisarmNotifyFunction(sigval_t value) {
365 Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr);
366 ++cd->value;
367
368 // Setting the initial expiration time to 0 disarms the timer.
Christopher Ferris62d84b12014-10-20 19:09:19 -0700369 cd->SetTime(0, 0, 1, 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800370 }
371};
372
373TEST(time, timer_settime_0) {
374 Counter counter(Counter::CountAndDisarmNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800375 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800376
Yabin Cuibf572d92015-08-11 11:23:16 -0700377 counter.SetTime(0, 500000000, 1, 0);
378 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800379
380 // The count should just be 1 because we disarmed the timer the first time it fired.
Yabin Cui95f1ee22015-01-13 19:53:15 -0800381 ASSERT_EQ(1, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800382}
383
384TEST(time, timer_settime_repeats) {
385 Counter counter(Counter::CountNotifyFunction);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800386 ASSERT_EQ(0, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800387
Christopher Ferris62d84b12014-10-20 19:09:19 -0700388 counter.SetTime(0, 1, 0, 10);
389 ASSERT_TRUE(counter.ValueUpdated());
390 ASSERT_TRUE(counter.ValueUpdated());
391 ASSERT_TRUE(counter.ValueUpdated());
Yabin Cui95f1ee22015-01-13 19:53:15 -0800392 counter.DeleteTimer();
393 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
394 usleep(500000);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800395}
396
Yabin Cui95f1ee22015-01-13 19:53:15 -0800397static int timer_create_NULL_signal_handler_invocation_count;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800398static void timer_create_NULL_signal_handler(int signal_number) {
399 ++timer_create_NULL_signal_handler_invocation_count;
400 ASSERT_EQ(SIGALRM, signal_number);
401}
402
403TEST(time, timer_create_NULL) {
404 // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM.
405 timer_t timer_id;
406 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
407
Yabin Cui95f1ee22015-01-13 19:53:15 -0800408 timer_create_NULL_signal_handler_invocation_count = 0;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800409 ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler);
410
411 ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count);
412
413 SetTime(timer_id, 0, 1, 0, 0);
414 usleep(500000);
415
416 ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count);
417}
418
419TEST(time, timer_create_EINVAL) {
420 clockid_t invalid_clock = 16;
421
422 // A SIGEV_SIGNAL timer is easy; the kernel does all that.
423 timer_t timer_id;
424 ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id));
425 ASSERT_EQ(EINVAL, errno);
426
427 // A SIGEV_THREAD timer is more interesting because we have stuff to clean up.
428 sigevent_t se;
429 memset(&se, 0, sizeof(se));
430 se.sigev_notify = SIGEV_THREAD;
431 se.sigev_notify_function = NoOpNotifyFunction;
432 ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id));
433 ASSERT_EQ(EINVAL, errno);
434}
435
436TEST(time, timer_delete_multiple) {
437 timer_t timer_id;
438 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id));
439 ASSERT_EQ(0, timer_delete(timer_id));
440 ASSERT_EQ(-1, timer_delete(timer_id));
441 ASSERT_EQ(EINVAL, errno);
442
443 sigevent_t se;
444 memset(&se, 0, sizeof(se));
445 se.sigev_notify = SIGEV_THREAD;
446 se.sigev_notify_function = NoOpNotifyFunction;
447 ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
448 ASSERT_EQ(0, timer_delete(timer_id));
449 ASSERT_EQ(-1, timer_delete(timer_id));
450 ASSERT_EQ(EINVAL, errno);
451}
452
453TEST(time, timer_create_multiple) {
454 Counter counter1(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800455 Counter counter2(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800456 Counter counter3(Counter::CountNotifyFunction);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800457
Yabin Cui95f1ee22015-01-13 19:53:15 -0800458 ASSERT_EQ(0, counter1.Value());
459 ASSERT_EQ(0, counter2.Value());
460 ASSERT_EQ(0, counter3.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800461
Yabin Cui410c1ad2015-06-18 16:19:02 -0700462 counter2.SetTime(0, 500000000, 0, 0);
463 sleep(1);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800464
Yabin Cui95f1ee22015-01-13 19:53:15 -0800465 EXPECT_EQ(0, counter1.Value());
466 EXPECT_EQ(1, counter2.Value());
467 EXPECT_EQ(0, counter3.Value());
468}
469
470// Test to verify that disarming a repeatable timer disables the callbacks.
471TEST(time, timer_disarm_terminates) {
472 Counter counter(Counter::CountNotifyFunction);
473 ASSERT_EQ(0, counter.Value());
474
475 counter.SetTime(0, 1, 0, 1);
476 ASSERT_TRUE(counter.ValueUpdated());
477 ASSERT_TRUE(counter.ValueUpdated());
478 ASSERT_TRUE(counter.ValueUpdated());
479
480 counter.SetTime(0, 0, 0, 0);
481 // Add a sleep as the kernel may have pending events when the timer is disarmed.
482 usleep(500000);
483 int value = counter.Value();
484 usleep(500000);
485
486 // Verify the counter has not been incremented.
487 ASSERT_EQ(value, counter.Value());
488}
489
490// Test to verify that deleting a repeatable timer disables the callbacks.
491TEST(time, timer_delete_terminates) {
492 Counter counter(Counter::CountNotifyFunction);
493 ASSERT_EQ(0, counter.Value());
494
495 counter.SetTime(0, 1, 0, 1);
496 ASSERT_TRUE(counter.ValueUpdated());
497 ASSERT_TRUE(counter.ValueUpdated());
498 ASSERT_TRUE(counter.ValueUpdated());
499
500 counter.DeleteTimer();
501 // Add a sleep as other threads may be calling the callback function when the timer is deleted.
502 usleep(500000);
503 int value = counter.Value();
504 usleep(500000);
505
506 // Verify the counter has not been incremented.
507 ASSERT_EQ(value, counter.Value());
Elliott Hughes4b558f52014-03-04 15:58:02 -0800508}
Christopher Ferris753ad772014-03-20 20:47:45 -0700509
510struct TimerDeleteData {
511 timer_t timer_id;
512 pthread_t thread_id;
513 volatile bool complete;
514};
515
516static void TimerDeleteCallback(sigval_t value) {
517 TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr);
518
519 tdd->thread_id = pthread_self();
520 timer_delete(tdd->timer_id);
521 tdd->complete = true;
522}
523
524TEST(time, timer_delete_from_timer_thread) {
525 TimerDeleteData tdd;
526 sigevent_t se;
527
528 memset(&se, 0, sizeof(se));
529 se.sigev_notify = SIGEV_THREAD;
530 se.sigev_notify_function = TimerDeleteCallback;
531 se.sigev_value.sival_ptr = &tdd;
532
533 tdd.complete = false;
534 ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id));
535
536 itimerspec ts;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800537 ts.it_value.tv_sec = 1;
538 ts.it_value.tv_nsec = 0;
Christopher Ferris753ad772014-03-20 20:47:45 -0700539 ts.it_interval.tv_sec = 0;
540 ts.it_interval.tv_nsec = 0;
Christopher Ferris3da136a2015-02-18 17:11:47 -0800541 ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, NULL));
Christopher Ferris753ad772014-03-20 20:47:45 -0700542
543 time_t cur_time = time(NULL);
544 while (!tdd.complete && (time(NULL) - cur_time) < 5);
545 ASSERT_TRUE(tdd.complete);
546
547#if defined(__BIONIC__)
548 // Since bionic timers are implemented by creating a thread to handle the
549 // callback, verify that the thread actually completes.
550 cur_time = time(NULL);
551 while (pthread_detach(tdd.thread_id) != ESRCH && (time(NULL) - cur_time) < 5);
552 ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id));
553#endif
554}
Elliott Hughes625993d2014-07-15 16:53:13 -0700555
556TEST(time, clock_gettime) {
557 // Try to ensure that our vdso clock_gettime is working.
558 timespec ts1;
559 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1));
560 timespec ts2;
561 ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2));
562
563 // What's the difference between the two?
564 ts2.tv_sec -= ts1.tv_sec;
565 ts2.tv_nsec -= ts1.tv_nsec;
566 if (ts2.tv_nsec < 0) {
567 --ts2.tv_sec;
Elliott Hughes04303f52014-09-18 16:11:59 -0700568 ts2.tv_nsec += NS_PER_S;
Elliott Hughes625993d2014-07-15 16:53:13 -0700569 }
570
571 // Should be less than (a very generous, to try to avoid flakiness) 1000000ns.
572 ASSERT_EQ(0, ts2.tv_sec);
573 ASSERT_LT(ts2.tv_nsec, 1000000);
574}
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700575
576TEST(time, clock) {
Haruki Hasegawa18160252014-10-13 00:50:47 +0900577 // clock(3) is hard to test, but a 1s sleep should cost less than 1ms.
578 clock_t t0 = clock();
579 sleep(1);
580 clock_t t1 = clock();
581 ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000);
582}
583
Yabin Cuid5c65272014-11-26 14:04:26 -0800584pid_t GetInvalidPid() {
585 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
586 long pid_max;
587 fscanf(fp, "%ld", &pid_max);
588 pid_t invalid_pid = static_cast<pid_t>(pid_max + 1);
589 fclose(fp);
590 return invalid_pid;
591}
592
593TEST(time, clock_getcpuclockid) {
594 // For current process.
595 clockid_t clockid;
596 ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid));
597
598 timespec ts;
599 ASSERT_EQ(0, clock_gettime(clockid, &ts));
600
601 // For parent process.
602 ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid));
603 ASSERT_EQ(0, clock_gettime(clockid, &ts));
604
605 // For invalid process.
606 // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it.
607 errno = 0;
608 ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid));
609 ASSERT_EQ(0, errno);
610}
611
Haruki Hasegawa18160252014-10-13 00:50:47 +0900612TEST(time, clock_settime) {
613 errno = 0;
614 timespec ts;
615 ASSERT_EQ(-1, clock_settime(-1, &ts));
616 ASSERT_EQ(EINVAL, errno);
617}
618
619TEST(time, clock_nanosleep) {
620 timespec in;
621 timespec out;
622 ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out));
Alex Van Brunt8d0b2db2014-09-26 13:32:47 -0700623}
Greg Hackmannd15dfb22016-03-26 11:37:55 -0700624
625TEST(time, clock_nanosleep_thread_cputime_id) {
626 timespec in;
627 in.tv_sec = 1;
628 in.tv_nsec = 0;
629 ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr));
630}
Elliott Hughes12443702016-10-19 16:02:31 -0700631
632TEST(time, bug_31938693) {
633 // User-visible symptoms in N:
634 // http://b/31938693
635 // https://code.google.com/p/android/issues/detail?id=225132
636
637 // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug):
638 // http://b/31848040
639
640 // This isn't a great test, because very few time zones were actually affected, and there's
641 // no real logic to which ones were affected: it was just a coincidence of the data that came
642 // after them in the tzdata file.
643
644 time_t t = 1475619727;
645 struct tm tm;
646
647 setenv("TZ", "America/Los_Angeles", 1);
648 tzset();
649 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
650 EXPECT_EQ(15, tm.tm_hour);
651
652 setenv("TZ", "Europe/London", 1);
653 tzset();
654 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
655 EXPECT_EQ(23, tm.tm_hour);
656
657 setenv("TZ", "America/Atka", 1);
658 tzset();
659 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
660 EXPECT_EQ(13, tm.tm_hour);
661
662 setenv("TZ", "Pacific/Apia", 1);
663 tzset();
664 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
665 EXPECT_EQ(12, tm.tm_hour);
666
667 setenv("TZ", "Pacific/Honolulu", 1);
668 tzset();
669 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
670 EXPECT_EQ(12, tm.tm_hour);
671
672 setenv("TZ", "Asia/Magadan", 1);
673 tzset();
674 ASSERT_TRUE(localtime_r(&t, &tm) != nullptr);
675 EXPECT_EQ(9, tm.tm_hour);
676}