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 |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame] | 62 | pthread_attr_t a; |
| 63 | ASSERT_EQ(0, pthread_attr_init(&a)); |
| 64 | ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN)); |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 65 | |
| 66 | pthread_t t; |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame] | 67 | ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, NULL)); |
| 68 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Satoru Takeuchi | 154e202 | 2014-05-27 17:04:04 +0900 | [diff] [blame] | 71 | TEST(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 Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 95 | TEST(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 Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 102 | #if !defined(__LP64__) |
| 103 | // 32-bit bionic stupidly had a signed 32-bit time_t. |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 104 | ASSERT_EQ(-1, mktime(&t)); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 105 | ASSERT_EQ(EOVERFLOW, errno); |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 106 | #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 Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 112 | errno = 0; |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 113 | ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&t)); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 114 | ASSERT_EQ(0, errno); |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 115 | |
| 116 | setenv("TZ", "UTC", 1); |
| 117 | tzset(); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 118 | errno = 0; |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 119 | ASSERT_EQ(static_cast<time_t>(4108320000U), mktime(&t)); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 120 | ASSERT_EQ(0, errno); |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 121 | #endif |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 122 | } |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 123 | |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 124 | TEST(time, mktime_EOVERFLOW) { |
| 125 | struct tm t; |
| 126 | memset(&t, 0, sizeof(tm)); |
Elliott Hughes | 47126ed | 2016-09-06 13:25:53 -0700 | [diff] [blame] | 127 | |
| 128 | // LP32 year range is 1901-2038, so this year is guaranteed not to overflow. |
| 129 | t.tm_year = 2016 - 1900; |
| 130 | |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 131 | 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 Hughes | 47126ed | 2016-09-06 13:25:53 -0700 | [diff] [blame] | 138 | // This will overflow for LP32 or LP64. |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 139 | t.tm_year = INT_MAX; |
Elliott Hughes | 47126ed | 2016-09-06 13:25:53 -0700 | [diff] [blame] | 140 | |
| 141 | errno = 0; |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 142 | ASSERT_EQ(static_cast<time_t>(-1), mktime(&t)); |
| 143 | ASSERT_EQ(EOVERFLOW, errno); |
| 144 | } |
| 145 | |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 146 | TEST(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 Hughes | a9cac4c | 2015-11-12 16:51:31 -0800 | [diff] [blame] | 168 | TEST(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 Hughes | 0a610d0 | 2016-07-29 14:04:17 -0700 | [diff] [blame] | 206 | TEST(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 Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 227 | TEST(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 Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 244 | void 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 Cui | d1ade7c | 2015-06-18 17:01:11 -0700 | [diff] [blame] | 250 | ASSERT_EQ(0, timer_settime(t, 0, &ts, NULL)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static void NoOpNotifyFunction(sigval_t) { |
| 254 | } |
| 255 | |
| 256 | TEST(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 Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 264 | pid_t pid = fork(); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 265 | 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 Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 274 | AssertChildExited(pid, 0); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 275 | |
| 276 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 277 | } |
| 278 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 279 | static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 280 | static 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 | |
| 285 | TEST(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 Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 294 | timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 295 | 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 Cui | d1ade7c | 2015-06-18 17:01:11 -0700 | [diff] [blame] | 304 | ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, NULL)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 305 | |
| 306 | usleep(500000); |
| 307 | ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); |
| 308 | } |
| 309 | |
| 310 | struct Counter { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 311 | private: |
| 312 | std::atomic<int> value; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 313 | timer_t timer_id; |
| 314 | sigevent_t se; |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 315 | bool timer_valid; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 316 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 317 | void Create() { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 318 | ASSERT_FALSE(timer_valid); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 319 | ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id)); |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 320 | timer_valid = true; |
| 321 | } |
| 322 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 323 | public: |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 324 | explicit Counter(void (*fn)(sigval_t)) : value(0), timer_valid(false) { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 325 | 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 Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 331 | void DeleteTimer() { |
| 332 | ASSERT_TRUE(timer_valid); |
| 333 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 334 | timer_valid = false; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | ~Counter() { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 338 | if (timer_valid) { |
| 339 | DeleteTimer(); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 343 | int Value() const { |
| 344 | return value; |
| 345 | } |
| 346 | |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 347 | 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 Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 352 | int current_value = value; |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 353 | time_t start = time(NULL); |
| 354 | while (current_value == value && (time(NULL) - start) < 5) { |
| 355 | } |
| 356 | return current_value != value; |
| 357 | } |
| 358 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 359 | 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 Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 369 | cd->SetTime(0, 0, 1, 0); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 370 | } |
| 371 | }; |
| 372 | |
| 373 | TEST(time, timer_settime_0) { |
| 374 | Counter counter(Counter::CountAndDisarmNotifyFunction); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 375 | ASSERT_EQ(0, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 376 | |
Yabin Cui | bf572d9 | 2015-08-11 11:23:16 -0700 | [diff] [blame] | 377 | counter.SetTime(0, 500000000, 1, 0); |
| 378 | sleep(1); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 379 | |
| 380 | // 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] | 381 | ASSERT_EQ(1, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | TEST(time, timer_settime_repeats) { |
| 385 | Counter counter(Counter::CountNotifyFunction); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 386 | ASSERT_EQ(0, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 387 | |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 388 | counter.SetTime(0, 1, 0, 10); |
| 389 | ASSERT_TRUE(counter.ValueUpdated()); |
| 390 | ASSERT_TRUE(counter.ValueUpdated()); |
| 391 | ASSERT_TRUE(counter.ValueUpdated()); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 392 | counter.DeleteTimer(); |
| 393 | // Add a sleep as other threads may be calling the callback function when the timer is deleted. |
| 394 | usleep(500000); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 397 | static int timer_create_NULL_signal_handler_invocation_count; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 398 | static 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 | |
| 403 | TEST(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 Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 408 | timer_create_NULL_signal_handler_invocation_count = 0; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 409 | 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 | |
| 419 | TEST(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 | |
| 436 | TEST(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 | |
| 453 | TEST(time, timer_create_multiple) { |
| 454 | Counter counter1(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 455 | Counter counter2(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 456 | Counter counter3(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 457 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 458 | ASSERT_EQ(0, counter1.Value()); |
| 459 | ASSERT_EQ(0, counter2.Value()); |
| 460 | ASSERT_EQ(0, counter3.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 461 | |
Yabin Cui | 410c1ad | 2015-06-18 16:19:02 -0700 | [diff] [blame] | 462 | counter2.SetTime(0, 500000000, 0, 0); |
| 463 | sleep(1); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 464 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 465 | 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. |
| 471 | TEST(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. |
| 491 | TEST(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 Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 508 | } |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 509 | |
| 510 | struct TimerDeleteData { |
| 511 | timer_t timer_id; |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 512 | pid_t tid; |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 513 | volatile bool complete; |
| 514 | }; |
| 515 | |
| 516 | static void TimerDeleteCallback(sigval_t value) { |
| 517 | TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr); |
| 518 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 519 | tdd->tid = gettid(); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 520 | timer_delete(tdd->timer_id); |
| 521 | tdd->complete = true; |
| 522 | } |
| 523 | |
| 524 | TEST(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 Ferris | 3da136a | 2015-02-18 17:11:47 -0800 | [diff] [blame] | 537 | ts.it_value.tv_sec = 1; |
| 538 | ts.it_value.tv_nsec = 0; |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 539 | ts.it_interval.tv_sec = 0; |
| 540 | ts.it_interval.tv_nsec = 0; |
Christopher Ferris | 3da136a | 2015-02-18 17:11:47 -0800 | [diff] [blame] | 541 | ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, NULL)); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 542 | |
| 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); |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 551 | while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5); |
| 552 | ASSERT_EQ(-1, kill(tdd.tid, 0)); |
| 553 | ASSERT_EQ(ESRCH, errno); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 554 | #endif |
| 555 | } |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 556 | |
| 557 | TEST(time, clock_gettime) { |
| 558 | // Try to ensure that our vdso clock_gettime is working. |
| 559 | timespec ts1; |
| 560 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts1)); |
| 561 | timespec ts2; |
| 562 | ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts2)); |
| 563 | |
| 564 | // What's the difference between the two? |
| 565 | ts2.tv_sec -= ts1.tv_sec; |
| 566 | ts2.tv_nsec -= ts1.tv_nsec; |
| 567 | if (ts2.tv_nsec < 0) { |
| 568 | --ts2.tv_sec; |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 569 | ts2.tv_nsec += NS_PER_S; |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // Should be less than (a very generous, to try to avoid flakiness) 1000000ns. |
| 573 | ASSERT_EQ(0, ts2.tv_sec); |
| 574 | ASSERT_LT(ts2.tv_nsec, 1000000); |
| 575 | } |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 576 | |
Elliott Hughes | 3a8f75d | 2017-10-05 10:33:18 -0700 | [diff] [blame] | 577 | TEST(time, clock_gettime_CLOCK_REALTIME) { |
| 578 | timespec ts; |
| 579 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 580 | } |
| 581 | |
| 582 | TEST(time, clock_gettime_CLOCK_MONOTONIC) { |
| 583 | timespec ts; |
| 584 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 585 | } |
| 586 | |
| 587 | TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) { |
| 588 | timespec ts; |
| 589 | ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 590 | } |
| 591 | |
| 592 | TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) { |
| 593 | timespec ts; |
| 594 | ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts)); |
| 595 | } |
| 596 | |
| 597 | TEST(time, clock_gettime_CLOCK_BOOTTIME) { |
| 598 | timespec ts; |
| 599 | ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts)); |
| 600 | } |
| 601 | |
Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame^] | 602 | TEST(time, clock_gettime_unknown) { |
| 603 | errno = 0; |
| 604 | timespec ts; |
| 605 | ASSERT_EQ(-1, clock_gettime(-1, &ts)); |
| 606 | ASSERT_EQ(EINVAL, errno); |
| 607 | } |
| 608 | |
| 609 | TEST(time, clock_getres_CLOCK_REALTIME) { |
| 610 | timespec ts; |
| 611 | ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts)); |
| 612 | ASSERT_EQ(1, ts.tv_nsec); |
| 613 | ASSERT_EQ(0, ts.tv_sec); |
| 614 | } |
| 615 | |
| 616 | TEST(time, clock_getres_CLOCK_MONOTONIC) { |
| 617 | timespec ts; |
| 618 | ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts)); |
| 619 | ASSERT_EQ(1, ts.tv_nsec); |
| 620 | ASSERT_EQ(0, ts.tv_sec); |
| 621 | } |
| 622 | |
| 623 | TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) { |
| 624 | timespec ts; |
| 625 | ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 626 | } |
| 627 | |
| 628 | TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) { |
| 629 | timespec ts; |
| 630 | ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts)); |
| 631 | } |
| 632 | |
| 633 | TEST(time, clock_getres_CLOCK_BOOTTIME) { |
| 634 | timespec ts; |
| 635 | ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts)); |
| 636 | ASSERT_EQ(1, ts.tv_nsec); |
| 637 | ASSERT_EQ(0, ts.tv_sec); |
| 638 | } |
| 639 | |
| 640 | TEST(time, clock_getres_unknown) { |
| 641 | errno = 0; |
| 642 | timespec ts = { -1, -1 }; |
| 643 | ASSERT_EQ(-1, clock_getres(-1, &ts)); |
| 644 | ASSERT_EQ(EINVAL, errno); |
| 645 | ASSERT_EQ(-1, ts.tv_nsec); |
| 646 | ASSERT_EQ(-1, ts.tv_sec); |
| 647 | } |
| 648 | |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 649 | TEST(time, clock) { |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 650 | // clock(3) is hard to test, but a 1s sleep should cost less than 1ms. |
| 651 | clock_t t0 = clock(); |
| 652 | sleep(1); |
| 653 | clock_t t1 = clock(); |
| 654 | ASSERT_LT(t1 - t0, CLOCKS_PER_SEC / 1000); |
| 655 | } |
| 656 | |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 657 | pid_t GetInvalidPid() { |
| 658 | FILE* fp = fopen("/proc/sys/kernel/pid_max", "r"); |
| 659 | long pid_max; |
| 660 | fscanf(fp, "%ld", &pid_max); |
| 661 | pid_t invalid_pid = static_cast<pid_t>(pid_max + 1); |
| 662 | fclose(fp); |
| 663 | return invalid_pid; |
| 664 | } |
| 665 | |
| 666 | TEST(time, clock_getcpuclockid) { |
| 667 | // For current process. |
| 668 | clockid_t clockid; |
| 669 | ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid)); |
| 670 | |
| 671 | timespec ts; |
| 672 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
| 673 | |
| 674 | // For parent process. |
| 675 | ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid)); |
| 676 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
| 677 | |
| 678 | // For invalid process. |
| 679 | // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it. |
| 680 | errno = 0; |
| 681 | ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)); |
| 682 | ASSERT_EQ(0, errno); |
| 683 | } |
| 684 | |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 685 | TEST(time, clock_settime) { |
| 686 | errno = 0; |
| 687 | timespec ts; |
| 688 | ASSERT_EQ(-1, clock_settime(-1, &ts)); |
| 689 | ASSERT_EQ(EINVAL, errno); |
| 690 | } |
| 691 | |
| 692 | TEST(time, clock_nanosleep) { |
| 693 | timespec in; |
| 694 | timespec out; |
| 695 | ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out)); |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 696 | } |
Greg Hackmann | d15dfb2 | 2016-03-26 11:37:55 -0700 | [diff] [blame] | 697 | |
| 698 | TEST(time, clock_nanosleep_thread_cputime_id) { |
| 699 | timespec in; |
| 700 | in.tv_sec = 1; |
| 701 | in.tv_nsec = 0; |
| 702 | ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr)); |
| 703 | } |
Elliott Hughes | 1244370 | 2016-10-19 16:02:31 -0700 | [diff] [blame] | 704 | |
| 705 | TEST(time, bug_31938693) { |
| 706 | // User-visible symptoms in N: |
| 707 | // http://b/31938693 |
| 708 | // https://code.google.com/p/android/issues/detail?id=225132 |
| 709 | |
| 710 | // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug): |
| 711 | // http://b/31848040 |
| 712 | |
| 713 | // This isn't a great test, because very few time zones were actually affected, and there's |
| 714 | // no real logic to which ones were affected: it was just a coincidence of the data that came |
| 715 | // after them in the tzdata file. |
| 716 | |
| 717 | time_t t = 1475619727; |
| 718 | struct tm tm; |
| 719 | |
| 720 | setenv("TZ", "America/Los_Angeles", 1); |
| 721 | tzset(); |
| 722 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 723 | EXPECT_EQ(15, tm.tm_hour); |
| 724 | |
| 725 | setenv("TZ", "Europe/London", 1); |
| 726 | tzset(); |
| 727 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 728 | EXPECT_EQ(23, tm.tm_hour); |
| 729 | |
| 730 | setenv("TZ", "America/Atka", 1); |
| 731 | tzset(); |
| 732 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 733 | EXPECT_EQ(13, tm.tm_hour); |
| 734 | |
| 735 | setenv("TZ", "Pacific/Apia", 1); |
| 736 | tzset(); |
| 737 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 738 | EXPECT_EQ(12, tm.tm_hour); |
| 739 | |
| 740 | setenv("TZ", "Pacific/Honolulu", 1); |
| 741 | tzset(); |
| 742 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 743 | EXPECT_EQ(12, tm.tm_hour); |
| 744 | |
| 745 | setenv("TZ", "Asia/Magadan", 1); |
| 746 | tzset(); |
| 747 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 748 | EXPECT_EQ(9, tm.tm_hour); |
| 749 | } |
Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 750 | |
| 751 | TEST(time, bug_31339449) { |
| 752 | // POSIX says localtime acts as if it calls tzset. |
| 753 | // tzset does two things: |
| 754 | // 1. it sets the time zone ctime/localtime/mktime/strftime will use. |
| 755 | // 2. it sets the global `tzname`. |
| 756 | // POSIX says localtime_r need not set `tzname` (2). |
| 757 | // Q: should localtime_r set the time zone (1)? |
| 758 | // Upstream tzcode (and glibc) answer "no", everyone else answers "yes". |
| 759 | |
| 760 | // Pick a time, any time... |
| 761 | time_t t = 1475619727; |
| 762 | |
| 763 | // Call tzset with a specific timezone. |
| 764 | setenv("TZ", "America/Atka", 1); |
| 765 | tzset(); |
| 766 | |
| 767 | // If we change the timezone and call localtime, localtime should use the new timezone. |
| 768 | setenv("TZ", "America/Los_Angeles", 1); |
| 769 | struct tm* tm_p = localtime(&t); |
| 770 | EXPECT_EQ(15, tm_p->tm_hour); |
| 771 | |
| 772 | // Reset the timezone back. |
| 773 | setenv("TZ", "America/Atka", 1); |
| 774 | tzset(); |
| 775 | |
| 776 | #if defined(__BIONIC__) |
| 777 | // If we change the timezone again and call localtime_r, localtime_r should use the new timezone. |
| 778 | setenv("TZ", "America/Los_Angeles", 1); |
| 779 | struct tm tm = {}; |
| 780 | localtime_r(&t, &tm); |
| 781 | EXPECT_EQ(15, tm.tm_hour); |
| 782 | #else |
| 783 | // The BSDs agree with us, but glibc gets this wrong. |
| 784 | #endif |
| 785 | } |