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