Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 17 | #include <time.h> |
| 18 | |
| 19 | #include <errno.h> |
Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 21 | #include <pthread.h> |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 22 | #include <signal.h> |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 23 | #include <sys/syscall.h> |
Brian Carlstrom | be1d91d | 2014-03-08 15:05:26 -0800 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 27 | #include <atomic> |
Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 29 | #include "ScopedSignalHandler.h" |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame^] | 30 | #include "utils.h" |
Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 32 | #include "private/bionic_constants.h" |
| 33 | |
Elliott Hughes | ee178bf | 2013-07-12 11:25:20 -0700 | [diff] [blame] | 34 | TEST(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 Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 46 | static 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 | |
| 58 | TEST(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 |
| 62 | pthread_attr_t attributes; |
| 63 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 64 | #if defined(__BIONIC__) |
| 65 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, PTHREAD_STACK_MIN)); |
| 66 | #else |
| 67 | // PTHREAD_STACK_MIN not currently in the host GCC sysroot. |
| 68 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 4 * getpagesize())); |
| 69 | #endif |
| 70 | |
| 71 | pthread_t t; |
| 72 | ASSERT_EQ(0, pthread_create(&t, &attributes, gmtime_no_stack_overflow_14313703_fn, NULL)); |
| 73 | void* result; |
| 74 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 75 | } |
| 76 | |
Satoru Takeuchi | 154e202 | 2014-05-27 17:04:04 +0900 | [diff] [blame] | 77 | TEST(time, mktime_empty_TZ) { |
| 78 | // tzcode used to have a bug where it didn't reinitialize some internal state. |
| 79 | |
| 80 | // Choose a time where DST is set. |
| 81 | struct tm t; |
| 82 | memset(&t, 0, sizeof(tm)); |
| 83 | t.tm_year = 1980 - 1900; |
| 84 | t.tm_mon = 6; |
| 85 | t.tm_mday = 2; |
| 86 | |
| 87 | setenv("TZ", "America/Los_Angeles", 1); |
| 88 | tzset(); |
| 89 | ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t)); |
| 90 | |
| 91 | memset(&t, 0, sizeof(tm)); |
| 92 | t.tm_year = 1980 - 1900; |
| 93 | t.tm_mon = 6; |
| 94 | t.tm_mday = 2; |
| 95 | |
| 96 | setenv("TZ", "", 1); // Implies UTC. |
| 97 | tzset(); |
| 98 | ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t)); |
| 99 | } |
| 100 | |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 101 | TEST(time, mktime_10310929) { |
| 102 | struct tm t; |
| 103 | memset(&t, 0, sizeof(tm)); |
| 104 | t.tm_year = 200; |
| 105 | t.tm_mon = 2; |
| 106 | t.tm_mday = 10; |
| 107 | |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 108 | #if !defined(__LP64__) |
| 109 | // 32-bit bionic stupidly had a signed 32-bit time_t. |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 110 | ASSERT_EQ(-1, mktime(&t)); |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 111 | #else |
| 112 | // Everyone else should be using a signed 64-bit time_t. |
| 113 | ASSERT_GE(sizeof(time_t) * 8, 64U); |
| 114 | |
| 115 | setenv("TZ", "America/Los_Angeles", 1); |
| 116 | tzset(); |
| 117 | ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t)); |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 118 | |
| 119 | setenv("TZ", "UTC", 1); |
| 120 | tzset(); |
| 121 | ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t)); |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 122 | #endif |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 123 | } |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 124 | |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 125 | TEST(time, strftime) { |
| 126 | setenv("TZ", "UTC", 1); |
| 127 | |
| 128 | struct tm t; |
| 129 | memset(&t, 0, sizeof(tm)); |
| 130 | t.tm_year = 200; |
| 131 | t.tm_mon = 2; |
| 132 | t.tm_mday = 10; |
| 133 | |
| 134 | char buf[64]; |
| 135 | |
| 136 | // Seconds since the epoch. |
| 137 | #if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc. |
| 138 | EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t)); |
| 139 | EXPECT_STREQ("4108320000", buf); |
| 140 | #endif |
| 141 | |
| 142 | // Date and time as text. |
| 143 | EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t)); |
| 144 | EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf); |
| 145 | } |
| 146 | |
Elliott Hughes | a9cac4c | 2015-11-12 16:51:31 -0800 | [diff] [blame] | 147 | TEST(time, strftime_null_tm_zone) { |
| 148 | // Netflix on Nexus Player wouldn't start (http://b/25170306). |
| 149 | struct tm t; |
| 150 | memset(&t, 0, sizeof(tm)); |
| 151 | |
| 152 | char buf[64]; |
| 153 | |
| 154 | setenv("TZ", "America/Los_Angeles", 1); |
| 155 | tzset(); |
| 156 | |
| 157 | t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect". |
| 158 | EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 159 | EXPECT_STREQ("<PST>", buf); |
| 160 | |
| 161 | #if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1. |
| 162 | t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect" |
| 163 | EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 164 | EXPECT_STREQ("<PDT>", buf); |
| 165 | |
| 166 | t.tm_isdst = -123; // "and negative if the information is not available". |
| 167 | EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 168 | EXPECT_STREQ("<>", buf); |
| 169 | #endif |
| 170 | |
| 171 | setenv("TZ", "UTC", 1); |
| 172 | tzset(); |
| 173 | |
| 174 | t.tm_isdst = 0; |
| 175 | EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 176 | EXPECT_STREQ("<UTC>", buf); |
| 177 | |
| 178 | #if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC". |
| 179 | t.tm_isdst = 1; // UTC has no DST. |
| 180 | EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 181 | EXPECT_STREQ("<>", buf); |
| 182 | #endif |
| 183 | } |
| 184 | |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 185 | TEST(time, strptime) { |
| 186 | setenv("TZ", "UTC", 1); |
| 187 | |
| 188 | struct tm t; |
| 189 | char buf[64]; |
| 190 | |
| 191 | memset(&t, 0, sizeof(t)); |
| 192 | strptime("11:14", "%R", &t); |
| 193 | strftime(buf, sizeof(buf), "%H:%M", &t); |
| 194 | EXPECT_STREQ("11:14", buf); |
| 195 | |
| 196 | memset(&t, 0, sizeof(t)); |
| 197 | strptime("09:41:53", "%T", &t); |
| 198 | strftime(buf, sizeof(buf), "%H:%M:%S", &t); |
| 199 | EXPECT_STREQ("09:41:53", buf); |
| 200 | } |
| 201 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 202 | void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { |
| 203 | itimerspec ts; |
| 204 | ts.it_value.tv_sec = value_s; |
| 205 | ts.it_value.tv_nsec = value_ns; |
| 206 | ts.it_interval.tv_sec = interval_s; |
| 207 | ts.it_interval.tv_nsec = interval_ns; |
Yabin Cui | d1ade7c | 2015-06-18 17:01:11 -0700 | [diff] [blame] | 208 | ASSERT_EQ(0, timer_settime(t, 0, &ts, NULL)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static void NoOpNotifyFunction(sigval_t) { |
| 212 | } |
| 213 | |
| 214 | TEST(time, timer_create) { |
| 215 | sigevent_t se; |
| 216 | memset(&se, 0, sizeof(se)); |
| 217 | se.sigev_notify = SIGEV_THREAD; |
| 218 | se.sigev_notify_function = NoOpNotifyFunction; |
| 219 | timer_t timer_id; |
| 220 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); |
| 221 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame^] | 222 | pid_t pid = fork(); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 223 | ASSERT_NE(-1, pid) << strerror(errno); |
| 224 | |
| 225 | if (pid == 0) { |
| 226 | // Timers are not inherited by the child. |
| 227 | ASSERT_EQ(-1, timer_delete(timer_id)); |
| 228 | ASSERT_EQ(EINVAL, errno); |
| 229 | _exit(0); |
| 230 | } |
| 231 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame^] | 232 | AssertChildExited(pid, 0); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 233 | |
| 234 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 235 | } |
| 236 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 237 | static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 238 | static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) { |
| 239 | ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; |
| 240 | ASSERT_EQ(SIGUSR1, signal_number); |
| 241 | } |
| 242 | |
| 243 | TEST(time, timer_create_SIGEV_SIGNAL) { |
| 244 | sigevent_t se; |
| 245 | memset(&se, 0, sizeof(se)); |
| 246 | se.sigev_notify = SIGEV_SIGNAL; |
| 247 | se.sigev_signo = SIGUSR1; |
| 248 | |
| 249 | timer_t timer_id; |
| 250 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); |
| 251 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 252 | timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 253 | ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler); |
| 254 | |
| 255 | ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); |
| 256 | |
| 257 | itimerspec ts; |
| 258 | ts.it_value.tv_sec = 0; |
| 259 | ts.it_value.tv_nsec = 1; |
| 260 | ts.it_interval.tv_sec = 0; |
| 261 | ts.it_interval.tv_nsec = 0; |
Yabin Cui | d1ade7c | 2015-06-18 17:01:11 -0700 | [diff] [blame] | 262 | ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, NULL)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 263 | |
| 264 | usleep(500000); |
| 265 | ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); |
| 266 | } |
| 267 | |
| 268 | struct Counter { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 269 | private: |
| 270 | std::atomic<int> value; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 271 | timer_t timer_id; |
| 272 | sigevent_t se; |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 273 | bool timer_valid; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 274 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 275 | void Create() { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 276 | ASSERT_FALSE(timer_valid); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 277 | ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id)); |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 278 | timer_valid = true; |
| 279 | } |
| 280 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 281 | public: |
| 282 | Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) { |
| 283 | memset(&se, 0, sizeof(se)); |
| 284 | se.sigev_notify = SIGEV_THREAD; |
| 285 | se.sigev_notify_function = fn; |
| 286 | se.sigev_value.sival_ptr = this; |
| 287 | Create(); |
| 288 | } |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 289 | void DeleteTimer() { |
| 290 | ASSERT_TRUE(timer_valid); |
| 291 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 292 | timer_valid = false; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | ~Counter() { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 296 | if (timer_valid) { |
| 297 | DeleteTimer(); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 301 | int Value() const { |
| 302 | return value; |
| 303 | } |
| 304 | |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 305 | void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { |
| 306 | ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns); |
| 307 | } |
| 308 | |
| 309 | bool ValueUpdated() { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 310 | int current_value = value; |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 311 | time_t start = time(NULL); |
| 312 | while (current_value == value && (time(NULL) - start) < 5) { |
| 313 | } |
| 314 | return current_value != value; |
| 315 | } |
| 316 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 317 | static void CountNotifyFunction(sigval_t value) { |
| 318 | Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); |
| 319 | ++cd->value; |
| 320 | } |
| 321 | |
| 322 | static void CountAndDisarmNotifyFunction(sigval_t value) { |
| 323 | Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); |
| 324 | ++cd->value; |
| 325 | |
| 326 | // Setting the initial expiration time to 0 disarms the timer. |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 327 | cd->SetTime(0, 0, 1, 0); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 328 | } |
| 329 | }; |
| 330 | |
| 331 | TEST(time, timer_settime_0) { |
| 332 | Counter counter(Counter::CountAndDisarmNotifyFunction); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 333 | ASSERT_EQ(0, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 334 | |
Yabin Cui | bf572d9 | 2015-08-11 11:23:16 -0700 | [diff] [blame] | 335 | counter.SetTime(0, 500000000, 1, 0); |
| 336 | sleep(1); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 337 | |
| 338 | // The count should just be 1 because we disarmed the timer the first time it fired. |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 339 | ASSERT_EQ(1, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | TEST(time, timer_settime_repeats) { |
| 343 | Counter counter(Counter::CountNotifyFunction); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 344 | ASSERT_EQ(0, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 345 | |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 346 | counter.SetTime(0, 1, 0, 10); |
| 347 | ASSERT_TRUE(counter.ValueUpdated()); |
| 348 | ASSERT_TRUE(counter.ValueUpdated()); |
| 349 | ASSERT_TRUE(counter.ValueUpdated()); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 350 | counter.DeleteTimer(); |
| 351 | // Add a sleep as other threads may be calling the callback function when the timer is deleted. |
| 352 | usleep(500000); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 355 | static int timer_create_NULL_signal_handler_invocation_count; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 356 | static void timer_create_NULL_signal_handler(int signal_number) { |
| 357 | ++timer_create_NULL_signal_handler_invocation_count; |
| 358 | ASSERT_EQ(SIGALRM, signal_number); |
| 359 | } |
| 360 | |
| 361 | TEST(time, timer_create_NULL) { |
| 362 | // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM. |
| 363 | timer_t timer_id; |
| 364 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id)); |
| 365 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 366 | timer_create_NULL_signal_handler_invocation_count = 0; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 367 | ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler); |
| 368 | |
| 369 | ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count); |
| 370 | |
| 371 | SetTime(timer_id, 0, 1, 0, 0); |
| 372 | usleep(500000); |
| 373 | |
| 374 | ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count); |
| 375 | } |
| 376 | |
| 377 | TEST(time, timer_create_EINVAL) { |
| 378 | clockid_t invalid_clock = 16; |
| 379 | |
| 380 | // A SIGEV_SIGNAL timer is easy; the kernel does all that. |
| 381 | timer_t timer_id; |
| 382 | ASSERT_EQ(-1, timer_create(invalid_clock, NULL, &timer_id)); |
| 383 | ASSERT_EQ(EINVAL, errno); |
| 384 | |
| 385 | // A SIGEV_THREAD timer is more interesting because we have stuff to clean up. |
| 386 | sigevent_t se; |
| 387 | memset(&se, 0, sizeof(se)); |
| 388 | se.sigev_notify = SIGEV_THREAD; |
| 389 | se.sigev_notify_function = NoOpNotifyFunction; |
| 390 | ASSERT_EQ(-1, timer_create(invalid_clock, &se, &timer_id)); |
| 391 | ASSERT_EQ(EINVAL, errno); |
| 392 | } |
| 393 | |
| 394 | TEST(time, timer_delete_multiple) { |
| 395 | timer_t timer_id; |
| 396 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id)); |
| 397 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 398 | ASSERT_EQ(-1, timer_delete(timer_id)); |
| 399 | ASSERT_EQ(EINVAL, errno); |
| 400 | |
| 401 | sigevent_t se; |
| 402 | memset(&se, 0, sizeof(se)); |
| 403 | se.sigev_notify = SIGEV_THREAD; |
| 404 | se.sigev_notify_function = NoOpNotifyFunction; |
| 405 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); |
| 406 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 407 | ASSERT_EQ(-1, timer_delete(timer_id)); |
| 408 | ASSERT_EQ(EINVAL, errno); |
| 409 | } |
| 410 | |
| 411 | TEST(time, timer_create_multiple) { |
| 412 | Counter counter1(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 413 | Counter counter2(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 414 | Counter counter3(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 415 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 416 | ASSERT_EQ(0, counter1.Value()); |
| 417 | ASSERT_EQ(0, counter2.Value()); |
| 418 | ASSERT_EQ(0, counter3.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 419 | |
Yabin Cui | 410c1ad | 2015-06-18 16:19:02 -0700 | [diff] [blame] | 420 | counter2.SetTime(0, 500000000, 0, 0); |
| 421 | sleep(1); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 422 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 423 | EXPECT_EQ(0, counter1.Value()); |
| 424 | EXPECT_EQ(1, counter2.Value()); |
| 425 | EXPECT_EQ(0, counter3.Value()); |
| 426 | } |
| 427 | |
| 428 | // Test to verify that disarming a repeatable timer disables the callbacks. |
| 429 | TEST(time, timer_disarm_terminates) { |
| 430 | Counter counter(Counter::CountNotifyFunction); |
| 431 | ASSERT_EQ(0, counter.Value()); |
| 432 | |
| 433 | counter.SetTime(0, 1, 0, 1); |
| 434 | ASSERT_TRUE(counter.ValueUpdated()); |
| 435 | ASSERT_TRUE(counter.ValueUpdated()); |
| 436 | ASSERT_TRUE(counter.ValueUpdated()); |
| 437 | |
| 438 | counter.SetTime(0, 0, 0, 0); |
| 439 | // Add a sleep as the kernel may have pending events when the timer is disarmed. |
| 440 | usleep(500000); |
| 441 | int value = counter.Value(); |
| 442 | usleep(500000); |
| 443 | |
| 444 | // Verify the counter has not been incremented. |
| 445 | ASSERT_EQ(value, counter.Value()); |
| 446 | } |
| 447 | |
| 448 | // Test to verify that deleting a repeatable timer disables the callbacks. |
| 449 | TEST(time, timer_delete_terminates) { |
| 450 | Counter counter(Counter::CountNotifyFunction); |
| 451 | ASSERT_EQ(0, counter.Value()); |
| 452 | |
| 453 | counter.SetTime(0, 1, 0, 1); |
| 454 | ASSERT_TRUE(counter.ValueUpdated()); |
| 455 | ASSERT_TRUE(counter.ValueUpdated()); |
| 456 | ASSERT_TRUE(counter.ValueUpdated()); |
| 457 | |
| 458 | counter.DeleteTimer(); |
| 459 | // Add a sleep as other threads may be calling the callback function when the timer is deleted. |
| 460 | usleep(500000); |
| 461 | int value = counter.Value(); |
| 462 | usleep(500000); |
| 463 | |
| 464 | // Verify the counter has not been incremented. |
| 465 | ASSERT_EQ(value, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 466 | } |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 467 | |
| 468 | struct TimerDeleteData { |
| 469 | timer_t timer_id; |
| 470 | pthread_t thread_id; |
| 471 | volatile bool complete; |
| 472 | }; |
| 473 | |
| 474 | static void TimerDeleteCallback(sigval_t value) { |
| 475 | TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr); |
| 476 | |
| 477 | tdd->thread_id = pthread_self(); |
| 478 | timer_delete(tdd->timer_id); |
| 479 | tdd->complete = true; |
| 480 | } |
| 481 | |
| 482 | TEST(time, timer_delete_from_timer_thread) { |
| 483 | TimerDeleteData tdd; |
| 484 | sigevent_t se; |
| 485 | |
| 486 | memset(&se, 0, sizeof(se)); |
| 487 | se.sigev_notify = SIGEV_THREAD; |
| 488 | se.sigev_notify_function = TimerDeleteCallback; |
| 489 | se.sigev_value.sival_ptr = &tdd; |
| 490 | |
| 491 | tdd.complete = false; |
| 492 | ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id)); |
| 493 | |
| 494 | itimerspec ts; |
Christopher Ferris | 3da136a | 2015-02-18 17:11:47 -0800 | [diff] [blame] | 495 | ts.it_value.tv_sec = 1; |
| 496 | ts.it_value.tv_nsec = 0; |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 497 | ts.it_interval.tv_sec = 0; |
| 498 | ts.it_interval.tv_nsec = 0; |
Christopher Ferris | 3da136a | 2015-02-18 17:11:47 -0800 | [diff] [blame] | 499 | ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, NULL)); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 500 | |
| 501 | time_t cur_time = time(NULL); |
| 502 | while (!tdd.complete && (time(NULL) - cur_time) < 5); |
| 503 | ASSERT_TRUE(tdd.complete); |
| 504 | |
| 505 | #if defined(__BIONIC__) |
| 506 | // Since bionic timers are implemented by creating a thread to handle the |
| 507 | // callback, verify that the thread actually completes. |
| 508 | cur_time = time(NULL); |
| 509 | while (pthread_detach(tdd.thread_id) != ESRCH && (time(NULL) - cur_time) < 5); |
| 510 | ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id)); |
| 511 | #endif |
| 512 | } |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 513 | |
| 514 | TEST(time, clock_gettime) { |
| 515 | // Try to ensure that our vdso clock_gettime is working. |
| 516 | timespec ts1; |
| 517 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1)); |
| 518 | timespec ts2; |
| 519 | ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2)); |
| 520 | |
| 521 | // What's the difference between the two? |
| 522 | ts2.tv_sec -= ts1.tv_sec; |
| 523 | ts2.tv_nsec -= ts1.tv_nsec; |
| 524 | if (ts2.tv_nsec < 0) { |
| 525 | --ts2.tv_sec; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 526 | ts2.tv_nsec += NS_PER_S; |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // Should be less than (a very generous, to try to avoid flakiness) 1000000ns. |
| 530 | ASSERT_EQ(0, ts2.tv_sec); |
| 531 | ASSERT_LT(ts2.tv_nsec, 1000000); |
| 532 | } |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 533 | |
| 534 | TEST(time, clock) { |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 535 | // clock(3) is hard to test, but a 1s sleep should cost less than 1ms. |
| 536 | clock_t t0 = clock(); |
| 537 | sleep(1); |
| 538 | clock_t t1 = clock(); |
| 539 | ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000); |
| 540 | } |
| 541 | |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 542 | pid_t GetInvalidPid() { |
| 543 | FILE* fp = fopen("/proc/sys/kernel/pid_max", "r"); |
| 544 | long pid_max; |
| 545 | fscanf(fp, "%ld", &pid_max); |
| 546 | pid_t invalid_pid = static_cast<pid_t>(pid_max + 1); |
| 547 | fclose(fp); |
| 548 | return invalid_pid; |
| 549 | } |
| 550 | |
| 551 | TEST(time, clock_getcpuclockid) { |
| 552 | // For current process. |
| 553 | clockid_t clockid; |
| 554 | ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid)); |
| 555 | |
| 556 | timespec ts; |
| 557 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
| 558 | |
| 559 | // For parent process. |
| 560 | ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid)); |
| 561 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
| 562 | |
| 563 | // For invalid process. |
| 564 | // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it. |
| 565 | errno = 0; |
| 566 | ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)); |
| 567 | ASSERT_EQ(0, errno); |
| 568 | } |
| 569 | |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 570 | TEST(time, clock_settime) { |
| 571 | errno = 0; |
| 572 | timespec ts; |
| 573 | ASSERT_EQ(-1, clock_settime(-1, &ts)); |
| 574 | ASSERT_EQ(EINVAL, errno); |
| 575 | } |
| 576 | |
| 577 | TEST(time, clock_nanosleep) { |
| 578 | timespec in; |
| 579 | timespec out; |
| 580 | ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out)); |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 581 | } |