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> |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 23 | #include <sys/cdefs.h> |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 24 | #include <sys/syscall.h> |
Brian Carlstrom | be1d91d | 2014-03-08 15:05:26 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 28 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 29 | #include <atomic> |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 30 | #include <chrono> |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 31 | #include <thread> |
Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 33 | #include "SignalUtils.h" |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 34 | #include "android-base/file.h" |
| 35 | #include "android-base/strings.h" |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 36 | #include "utils.h" |
Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 38 | using namespace std::chrono_literals; |
| 39 | |
Mark Salyzyn | 0b846e8 | 2017-12-20 08:56:18 -0800 | [diff] [blame] | 40 | TEST(time, time) { |
| 41 | // Acquire time |
| 42 | time_t p1, t1 = time(&p1); |
| 43 | // valid? |
| 44 | ASSERT_NE(static_cast<time_t>(0), t1); |
| 45 | ASSERT_NE(static_cast<time_t>(-1), t1); |
| 46 | ASSERT_EQ(p1, t1); |
| 47 | |
| 48 | // Acquire time one+ second later |
| 49 | usleep(1010000); |
| 50 | time_t p2, t2 = time(&p2); |
| 51 | // valid? |
| 52 | ASSERT_NE(static_cast<time_t>(0), t2); |
| 53 | ASSERT_NE(static_cast<time_t>(-1), t2); |
| 54 | ASSERT_EQ(p2, t2); |
| 55 | |
| 56 | // Expect time progression |
| 57 | ASSERT_LT(p1, p2); |
| 58 | ASSERT_LE(t2 - t1, static_cast<time_t>(2)); |
| 59 | |
| 60 | // Expect nullptr call to produce same results |
| 61 | ASSERT_LE(t2, time(nullptr)); |
| 62 | ASSERT_LE(time(nullptr) - t2, static_cast<time_t>(1)); |
| 63 | } |
| 64 | |
Elliott Hughes | ee178bf | 2013-07-12 11:25:20 -0700 | [diff] [blame] | 65 | TEST(time, gmtime) { |
| 66 | time_t t = 0; |
| 67 | tm* broken_down = gmtime(&t); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 68 | ASSERT_TRUE(broken_down != nullptr); |
Elliott Hughes | ee178bf | 2013-07-12 11:25:20 -0700 | [diff] [blame] | 69 | ASSERT_EQ(0, broken_down->tm_sec); |
| 70 | ASSERT_EQ(0, broken_down->tm_min); |
| 71 | ASSERT_EQ(0, broken_down->tm_hour); |
| 72 | ASSERT_EQ(1, broken_down->tm_mday); |
| 73 | ASSERT_EQ(0, broken_down->tm_mon); |
| 74 | ASSERT_EQ(1970, broken_down->tm_year + 1900); |
| 75 | } |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | 5a29d54 | 2017-12-07 16:05:57 -0800 | [diff] [blame] | 77 | TEST(time, gmtime_r) { |
| 78 | struct tm tm = {}; |
| 79 | time_t t = 0; |
| 80 | struct tm* broken_down = gmtime_r(&t, &tm); |
| 81 | ASSERT_EQ(broken_down, &tm); |
| 82 | ASSERT_EQ(0, broken_down->tm_sec); |
| 83 | ASSERT_EQ(0, broken_down->tm_min); |
| 84 | ASSERT_EQ(0, broken_down->tm_hour); |
| 85 | ASSERT_EQ(1, broken_down->tm_mday); |
| 86 | ASSERT_EQ(0, broken_down->tm_mon); |
| 87 | ASSERT_EQ(1970, broken_down->tm_year + 1900); |
| 88 | } |
| 89 | |
Almaz Mingaleev | da75bb6 | 2022-06-29 15:47:37 +0000 | [diff] [blame] | 90 | TEST(time, mktime_TZ_as_UTC_and_offset) { |
| 91 | struct tm tm = {.tm_year = 70, .tm_mon = 0, .tm_mday = 1}; |
| 92 | |
| 93 | // This TZ value is not a valid Olson ID and is not present in tzdata file, |
| 94 | // but is a valid TZ string according to POSIX standard. |
| 95 | setenv("TZ", "UTC+08:00:00", 1); |
| 96 | tzset(); |
| 97 | ASSERT_EQ(static_cast<time_t>(8 * 60 * 60), mktime(&tm)); |
| 98 | } |
| 99 | |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 100 | static void* gmtime_no_stack_overflow_14313703_fn(void*) { |
| 101 | const char* original_tz = getenv("TZ"); |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 102 | // Ensure we'll actually have to enter tzload by using a timezone that doesn't exist. |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 103 | setenv("TZ", "gmtime_stack_overflow_14313703", 1); |
| 104 | tzset(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 105 | if (original_tz != nullptr) { |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 106 | setenv("TZ", original_tz, 1); |
| 107 | } |
| 108 | tzset(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 109 | return nullptr; |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST(time, gmtime_no_stack_overflow_14313703) { |
| 113 | // Is it safe to call tzload on a thread with a small stack? |
| 114 | // http://b/14313703 |
| 115 | // https://code.google.com/p/android/issues/detail?id=61130 |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame] | 116 | pthread_attr_t a; |
| 117 | ASSERT_EQ(0, pthread_attr_init(&a)); |
| 118 | ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN)); |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 119 | |
| 120 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 121 | ASSERT_EQ(0, pthread_create(&t, &a, gmtime_no_stack_overflow_14313703_fn, nullptr)); |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame] | 122 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Elliott Hughes | 329103d | 2014-04-25 16:55:04 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Satoru Takeuchi | 154e202 | 2014-05-27 17:04:04 +0900 | [diff] [blame] | 125 | TEST(time, mktime_empty_TZ) { |
| 126 | // tzcode used to have a bug where it didn't reinitialize some internal state. |
| 127 | |
| 128 | // Choose a time where DST is set. |
| 129 | struct tm t; |
| 130 | memset(&t, 0, sizeof(tm)); |
| 131 | t.tm_year = 1980 - 1900; |
| 132 | t.tm_mon = 6; |
| 133 | t.tm_mday = 2; |
| 134 | |
| 135 | setenv("TZ", "America/Los_Angeles", 1); |
| 136 | tzset(); |
| 137 | ASSERT_EQ(static_cast<time_t>(331372800U), mktime(&t)); |
| 138 | |
| 139 | memset(&t, 0, sizeof(tm)); |
| 140 | t.tm_year = 1980 - 1900; |
| 141 | t.tm_mon = 6; |
| 142 | t.tm_mday = 2; |
| 143 | |
| 144 | setenv("TZ", "", 1); // Implies UTC. |
| 145 | tzset(); |
| 146 | ASSERT_EQ(static_cast<time_t>(331344000U), mktime(&t)); |
| 147 | } |
| 148 | |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 149 | TEST(time, mktime_10310929) { |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 150 | struct tm tm = {.tm_year = 2100 - 1900, .tm_mon = 2, .tm_mday = 10}; |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 151 | |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 152 | #if !defined(__LP64__) |
Elliott Hughes | cf34653 | 2020-07-31 10:35:03 -0700 | [diff] [blame] | 153 | // 32-bit bionic has a signed 32-bit time_t. |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 154 | ASSERT_EQ(-1, mktime(&tm)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 155 | ASSERT_ERRNO(EOVERFLOW); |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 156 | #else |
| 157 | // Everyone else should be using a signed 64-bit time_t. |
| 158 | ASSERT_GE(sizeof(time_t) * 8, 64U); |
| 159 | |
| 160 | setenv("TZ", "America/Los_Angeles", 1); |
| 161 | tzset(); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 162 | errno = 0; |
Elliott Hughes | 0c40152 | 2013-10-18 16:21:54 -0700 | [diff] [blame] | 163 | |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 164 | // On the date/time specified by tm America/Los_Angeles |
| 165 | // follows DST. But tm_isdst is set to 0, which forces |
| 166 | // mktime to interpret that time as local standard, hence offset |
| 167 | // is 8 hours, not 7. |
| 168 | ASSERT_EQ(static_cast<time_t>(4108348800U), mktime(&tm)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 169 | ASSERT_ERRNO(0); |
Elliott Hughes | 7843d44 | 2013-08-22 11:37:32 -0700 | [diff] [blame] | 170 | #endif |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 171 | } |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 172 | |
Elliott Hughes | 7c9c122 | 2024-01-12 23:46:29 +0000 | [diff] [blame] | 173 | TEST(time, mktime_EOVERFLOW) { |
Elliott Hughes | f52b216 | 2023-05-19 16:09:47 -0700 | [diff] [blame] | 174 | setenv("TZ", "UTC", 1); |
| 175 | |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 176 | struct tm t; |
| 177 | memset(&t, 0, sizeof(tm)); |
Elliott Hughes | 47126ed | 2016-09-06 13:25:53 -0700 | [diff] [blame] | 178 | |
| 179 | // LP32 year range is 1901-2038, so this year is guaranteed not to overflow. |
| 180 | t.tm_year = 2016 - 1900; |
| 181 | |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 182 | t.tm_mon = 2; |
| 183 | t.tm_mday = 10; |
| 184 | |
| 185 | errno = 0; |
| 186 | ASSERT_NE(static_cast<time_t>(-1), mktime(&t)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 187 | ASSERT_ERRNO(0); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 188 | |
Almaz Mingaleev | d86a3ab | 2023-04-27 11:24:48 +0100 | [diff] [blame] | 189 | // This will overflow for LP32. |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 190 | t.tm_year = INT_MAX; |
Elliott Hughes | 47126ed | 2016-09-06 13:25:53 -0700 | [diff] [blame] | 191 | |
| 192 | errno = 0; |
Almaz Mingaleev | d86a3ab | 2023-04-27 11:24:48 +0100 | [diff] [blame] | 193 | #if !defined(__LP64__) |
| 194 | ASSERT_EQ(static_cast<time_t>(-1), mktime(&t)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 195 | ASSERT_ERRNO(EOVERFLOW); |
Almaz Mingaleev | d86a3ab | 2023-04-27 11:24:48 +0100 | [diff] [blame] | 196 | #else |
| 197 | ASSERT_EQ(static_cast<time_t>(67768036166016000U), mktime(&t)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 198 | ASSERT_ERRNO(0); |
Almaz Mingaleev | d86a3ab | 2023-04-27 11:24:48 +0100 | [diff] [blame] | 199 | #endif |
| 200 | |
| 201 | // This will overflow for LP32 or LP64. |
| 202 | // tm_year is int, this t struct points to INT_MAX + 1 no matter what TZ is. |
| 203 | t.tm_year = INT_MAX; |
| 204 | t.tm_mon = 11; |
| 205 | t.tm_mday = 45; |
| 206 | |
| 207 | errno = 0; |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 208 | ASSERT_EQ(static_cast<time_t>(-1), mktime(&t)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 209 | ASSERT_ERRNO(EOVERFLOW); |
Elliott Hughes | f8ebaa4 | 2016-08-12 16:28:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Almaz Mingaleev | 5411aff | 2022-02-11 12:27:12 +0000 | [diff] [blame] | 212 | TEST(time, mktime_invalid_tm_TZ_combination) { |
| 213 | setenv("TZ", "UTC", 1); |
| 214 | |
| 215 | struct tm t; |
| 216 | memset(&t, 0, sizeof(tm)); |
| 217 | t.tm_year = 2022 - 1900; |
| 218 | t.tm_mon = 11; |
| 219 | t.tm_mday = 31; |
| 220 | // UTC does not observe DST |
| 221 | t.tm_isdst = 1; |
| 222 | |
| 223 | errno = 0; |
| 224 | |
| 225 | EXPECT_EQ(static_cast<time_t>(-1), mktime(&t)); |
| 226 | // mktime sets errno to EOVERFLOW if result is unrepresentable. |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 227 | EXPECT_ERRNO(EOVERFLOW); |
Almaz Mingaleev | 5411aff | 2022-02-11 12:27:12 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 230 | // Transitions in the tzdata file are generated up to the year 2100. Testing |
| 231 | // that dates beyond that are handled properly too. |
| 232 | TEST(time, mktime_after_2100) { |
| 233 | struct tm tm = {.tm_year = 2150 - 1900, .tm_mon = 2, .tm_mday = 10, .tm_isdst = -1}; |
| 234 | |
| 235 | #if !defined(__LP64__) |
| 236 | // 32-bit bionic has a signed 32-bit time_t. |
| 237 | ASSERT_EQ(-1, mktime(&tm)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 238 | ASSERT_ERRNO(EOVERFLOW); |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 239 | #else |
| 240 | setenv("TZ", "Europe/London", 1); |
| 241 | tzset(); |
| 242 | errno = 0; |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 243 | ASSERT_EQ(static_cast<time_t>(5686156800U), mktime(&tm)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 244 | ASSERT_ERRNO(0); |
Almaz Mingaleev | 10fed72 | 2022-07-04 16:05:22 +0100 | [diff] [blame] | 245 | #endif |
| 246 | } |
| 247 | |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 248 | TEST(time, strftime) { |
| 249 | setenv("TZ", "UTC", 1); |
| 250 | |
| 251 | struct tm t; |
| 252 | memset(&t, 0, sizeof(tm)); |
| 253 | t.tm_year = 200; |
| 254 | t.tm_mon = 2; |
| 255 | t.tm_mday = 10; |
| 256 | |
| 257 | char buf[64]; |
| 258 | |
| 259 | // Seconds since the epoch. |
| 260 | #if defined(__BIONIC__) || defined(__LP64__) // Not 32-bit glibc. |
| 261 | EXPECT_EQ(10U, strftime(buf, sizeof(buf), "%s", &t)); |
| 262 | EXPECT_STREQ("4108320000", buf); |
| 263 | #endif |
| 264 | |
| 265 | // Date and time as text. |
| 266 | EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &t)); |
| 267 | EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf); |
| 268 | } |
| 269 | |
Almaz Mingaleev | 5411aff | 2022-02-11 12:27:12 +0000 | [diff] [blame] | 270 | TEST(time, strftime_second_before_epoch) { |
| 271 | setenv("TZ", "UTC", 1); |
| 272 | |
| 273 | struct tm t; |
| 274 | memset(&t, 0, sizeof(tm)); |
| 275 | t.tm_year = 1969 - 1900; |
| 276 | t.tm_mon = 11; |
| 277 | t.tm_mday = 31; |
| 278 | t.tm_hour = 23; |
| 279 | t.tm_min = 59; |
| 280 | t.tm_sec = 59; |
| 281 | |
| 282 | char buf[64]; |
| 283 | |
| 284 | EXPECT_EQ(2U, strftime(buf, sizeof(buf), "%s", &t)); |
| 285 | EXPECT_STREQ("-1", buf); |
| 286 | } |
| 287 | |
Almaz Mingaleev | 24bfd8e | 2022-07-15 11:01:53 +0100 | [diff] [blame] | 288 | TEST(time, strftime_Z_null_tm_zone) { |
Elliott Hughes | a9cac4c | 2015-11-12 16:51:31 -0800 | [diff] [blame] | 289 | // Netflix on Nexus Player wouldn't start (http://b/25170306). |
| 290 | struct tm t; |
| 291 | memset(&t, 0, sizeof(tm)); |
| 292 | |
| 293 | char buf[64]; |
| 294 | |
| 295 | setenv("TZ", "America/Los_Angeles", 1); |
| 296 | tzset(); |
| 297 | |
| 298 | t.tm_isdst = 0; // "0 if Daylight Savings Time is not in effect". |
| 299 | EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 300 | EXPECT_STREQ("<PST>", buf); |
| 301 | |
| 302 | #if defined(__BIONIC__) // glibc 2.19 only copes with tm_isdst being 0 and 1. |
| 303 | t.tm_isdst = 2; // "positive if Daylight Savings Time is in effect" |
| 304 | EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 305 | EXPECT_STREQ("<PDT>", buf); |
| 306 | |
| 307 | t.tm_isdst = -123; // "and negative if the information is not available". |
| 308 | EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 309 | EXPECT_STREQ("<>", buf); |
| 310 | #endif |
| 311 | |
| 312 | setenv("TZ", "UTC", 1); |
| 313 | tzset(); |
| 314 | |
| 315 | t.tm_isdst = 0; |
| 316 | EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 317 | EXPECT_STREQ("<UTC>", buf); |
| 318 | |
| 319 | #if defined(__BIONIC__) // glibc 2.19 thinks UTC DST is "UTC". |
| 320 | t.tm_isdst = 1; // UTC has no DST. |
| 321 | EXPECT_EQ(2U, strftime(buf, sizeof(buf), "<%Z>", &t)); |
| 322 | EXPECT_STREQ("<>", buf); |
| 323 | #endif |
| 324 | } |
| 325 | |
Almaz Mingaleev | 24bfd8e | 2022-07-15 11:01:53 +0100 | [diff] [blame] | 326 | // According to C language specification the only tm struct field needed to |
| 327 | // find out replacement for %z and %Z in strftime is tm_isdst. Which is |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 328 | // wrong, as timezones change their standard offset and even DST savings. |
Almaz Mingaleev | 24bfd8e | 2022-07-15 11:01:53 +0100 | [diff] [blame] | 329 | // tzcode deviates from C language specification and requires tm struct either |
| 330 | // to be output of localtime-like functions or to be modified by mktime call |
| 331 | // before passing to strftime. See tz mailing discussion for more details |
| 332 | // https://mm.icann.org/pipermail/tz/2022-July/031674.html |
| 333 | // But we are testing case when tm.tm_zone is null, which means that tm struct |
| 334 | // is not coming from localtime and is neither modified by mktime. That's why |
| 335 | // we are comparing against +0000, even though America/Los_Angeles never |
| 336 | // observes it. |
| 337 | TEST(time, strftime_z_null_tm_zone) { |
| 338 | char str[64]; |
| 339 | struct tm tm = {.tm_year = 109, .tm_mon = 4, .tm_mday = 2, .tm_isdst = 0}; |
| 340 | |
| 341 | setenv("TZ", "America/Los_Angeles", 1); |
| 342 | tzset(); |
| 343 | |
| 344 | tm.tm_zone = NULL; |
| 345 | |
| 346 | size_t result = strftime(str, sizeof(str), "%z", &tm); |
| 347 | |
| 348 | EXPECT_EQ(5U, result); |
| 349 | EXPECT_STREQ("+0000", str); |
| 350 | |
| 351 | tm.tm_isdst = 1; |
| 352 | |
| 353 | result = strftime(str, sizeof(str), "%z", &tm); |
| 354 | |
| 355 | EXPECT_EQ(5U, result); |
| 356 | EXPECT_STREQ("+0000", str); |
| 357 | |
| 358 | setenv("TZ", "UTC", 1); |
| 359 | tzset(); |
| 360 | |
| 361 | tm.tm_isdst = 0; |
| 362 | |
| 363 | result = strftime(str, sizeof(str), "%z", &tm); |
| 364 | |
| 365 | EXPECT_EQ(5U, result); |
| 366 | EXPECT_STREQ("+0000", str); |
| 367 | |
| 368 | tm.tm_isdst = 1; |
| 369 | |
| 370 | result = strftime(str, sizeof(str), "%z", &tm); |
| 371 | |
| 372 | EXPECT_EQ(5U, result); |
| 373 | EXPECT_STREQ("+0000", str); |
| 374 | } |
| 375 | |
| 376 | TEST(time, strftime_z_Europe_Lisbon) { |
| 377 | char str[64]; |
| 378 | // During 1992-1996 Europe/Lisbon standard offset was 1 hour. |
| 379 | // tm_isdst is not set as it will be overridden by mktime call anyway. |
| 380 | struct tm tm = {.tm_year = 1996 - 1900, .tm_mon = 2, .tm_mday = 13}; |
| 381 | |
| 382 | setenv("TZ", "Europe/Lisbon", 1); |
| 383 | tzset(); |
| 384 | |
| 385 | // tzcode's strftime implementation for %z relies on prior mktime call. |
| 386 | // At the moment of writing %z value is taken from tm_gmtoff. So without |
| 387 | // mktime call %z is replaced with +0000. |
| 388 | // See https://mm.icann.org/pipermail/tz/2022-July/031674.html |
| 389 | mktime(&tm); |
| 390 | |
| 391 | size_t result = strftime(str, sizeof(str), "%z", &tm); |
| 392 | |
| 393 | EXPECT_EQ(5U, result); |
| 394 | EXPECT_STREQ("+0100", str); |
| 395 | |
| 396 | // Now standard offset is 0. |
| 397 | tm = {.tm_year = 2022 - 1900, .tm_mon = 2, .tm_mday = 13}; |
| 398 | |
| 399 | mktime(&tm); |
| 400 | result = strftime(str, sizeof(str), "%z", &tm); |
| 401 | |
| 402 | EXPECT_EQ(5U, result); |
| 403 | EXPECT_STREQ("+0000", str); |
| 404 | } |
| 405 | |
Elliott Hughes | 0a610d0 | 2016-07-29 14:04:17 -0700 | [diff] [blame] | 406 | TEST(time, strftime_l) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 407 | locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr); |
Elliott Hughes | 0a610d0 | 2016-07-29 14:04:17 -0700 | [diff] [blame] | 408 | locale_t old_locale = uselocale(cloc); |
| 409 | |
| 410 | setenv("TZ", "UTC", 1); |
| 411 | |
| 412 | struct tm t; |
| 413 | memset(&t, 0, sizeof(tm)); |
| 414 | t.tm_year = 200; |
| 415 | t.tm_mon = 2; |
| 416 | t.tm_mday = 10; |
| 417 | |
| 418 | // Date and time as text. |
| 419 | char buf[64]; |
| 420 | EXPECT_EQ(24U, strftime_l(buf, sizeof(buf), "%c", &t, cloc)); |
| 421 | EXPECT_STREQ("Sun Mar 10 00:00:00 2100", buf); |
| 422 | |
| 423 | uselocale(old_locale); |
| 424 | freelocale(cloc); |
| 425 | } |
| 426 | |
Elliott Hughes | 3e3409a | 2014-03-10 18:19:03 -0700 | [diff] [blame] | 427 | TEST(time, strptime) { |
| 428 | setenv("TZ", "UTC", 1); |
| 429 | |
| 430 | struct tm t; |
| 431 | char buf[64]; |
| 432 | |
| 433 | memset(&t, 0, sizeof(t)); |
| 434 | strptime("11:14", "%R", &t); |
| 435 | strftime(buf, sizeof(buf), "%H:%M", &t); |
| 436 | EXPECT_STREQ("11:14", buf); |
| 437 | |
| 438 | memset(&t, 0, sizeof(t)); |
| 439 | strptime("09:41:53", "%T", &t); |
| 440 | strftime(buf, sizeof(buf), "%H:%M:%S", &t); |
| 441 | EXPECT_STREQ("09:41:53", buf); |
| 442 | } |
| 443 | |
Elliott Hughes | 3376c23 | 2018-02-13 23:14:12 -0800 | [diff] [blame] | 444 | TEST(time, strptime_l) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 445 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 3376c23 | 2018-02-13 23:14:12 -0800 | [diff] [blame] | 446 | setenv("TZ", "UTC", 1); |
| 447 | |
| 448 | struct tm t; |
| 449 | char buf[64]; |
| 450 | |
| 451 | memset(&t, 0, sizeof(t)); |
| 452 | strptime_l("11:14", "%R", &t, LC_GLOBAL_LOCALE); |
| 453 | strftime_l(buf, sizeof(buf), "%H:%M", &t, LC_GLOBAL_LOCALE); |
| 454 | EXPECT_STREQ("11:14", buf); |
| 455 | |
| 456 | memset(&t, 0, sizeof(t)); |
| 457 | strptime_l("09:41:53", "%T", &t, LC_GLOBAL_LOCALE); |
| 458 | strftime_l(buf, sizeof(buf), "%H:%M:%S", &t, LC_GLOBAL_LOCALE); |
| 459 | EXPECT_STREQ("09:41:53", buf); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 460 | #else |
| 461 | GTEST_SKIP() << "musl doesn't support strptime_l"; |
| 462 | #endif |
Elliott Hughes | 3376c23 | 2018-02-13 23:14:12 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Elliott Hughes | a1fb15b | 2019-03-26 19:07:40 -0700 | [diff] [blame] | 465 | TEST(time, strptime_F) { |
| 466 | setenv("TZ", "UTC", 1); |
| 467 | |
| 468 | struct tm tm = {}; |
| 469 | ASSERT_EQ('\0', *strptime("2019-03-26", "%F", &tm)); |
| 470 | EXPECT_EQ(119, tm.tm_year); |
| 471 | EXPECT_EQ(2, tm.tm_mon); |
| 472 | EXPECT_EQ(26, tm.tm_mday); |
| 473 | } |
| 474 | |
| 475 | TEST(time, strptime_P_p) { |
| 476 | setenv("TZ", "UTC", 1); |
| 477 | |
Elliott Hughes | 1167882 | 2019-03-27 08:56:49 -0700 | [diff] [blame] | 478 | // For parsing, %P and %p are the same: case doesn't matter. |
| 479 | |
Elliott Hughes | a1fb15b | 2019-03-26 19:07:40 -0700 | [diff] [blame] | 480 | struct tm tm = {.tm_hour = 12}; |
| 481 | ASSERT_EQ('\0', *strptime("AM", "%p", &tm)); |
| 482 | EXPECT_EQ(0, tm.tm_hour); |
| 483 | |
| 484 | tm = {.tm_hour = 12}; |
Elliott Hughes | 1167882 | 2019-03-27 08:56:49 -0700 | [diff] [blame] | 485 | ASSERT_EQ('\0', *strptime("am", "%p", &tm)); |
| 486 | EXPECT_EQ(0, tm.tm_hour); |
| 487 | |
| 488 | tm = {.tm_hour = 12}; |
Elliott Hughes | a1fb15b | 2019-03-26 19:07:40 -0700 | [diff] [blame] | 489 | ASSERT_EQ('\0', *strptime("AM", "%P", &tm)); |
| 490 | EXPECT_EQ(0, tm.tm_hour); |
Elliott Hughes | 1167882 | 2019-03-27 08:56:49 -0700 | [diff] [blame] | 491 | |
| 492 | tm = {.tm_hour = 12}; |
| 493 | ASSERT_EQ('\0', *strptime("am", "%P", &tm)); |
| 494 | EXPECT_EQ(0, tm.tm_hour); |
Elliott Hughes | a1fb15b | 2019-03-26 19:07:40 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | TEST(time, strptime_u) { |
| 498 | setenv("TZ", "UTC", 1); |
| 499 | |
| 500 | struct tm tm = {}; |
| 501 | ASSERT_EQ('\0', *strptime("2", "%u", &tm)); |
| 502 | EXPECT_EQ(2, tm.tm_wday); |
| 503 | } |
| 504 | |
| 505 | TEST(time, strptime_v) { |
| 506 | setenv("TZ", "UTC", 1); |
| 507 | |
| 508 | struct tm tm = {}; |
| 509 | ASSERT_EQ('\0', *strptime("26-Mar-1980", "%v", &tm)); |
| 510 | EXPECT_EQ(80, tm.tm_year); |
| 511 | EXPECT_EQ(2, tm.tm_mon); |
| 512 | EXPECT_EQ(26, tm.tm_mday); |
| 513 | } |
| 514 | |
| 515 | TEST(time, strptime_V_G_g) { |
| 516 | setenv("TZ", "UTC", 1); |
| 517 | |
| 518 | // %V (ISO-8601 week number), %G (year of week number, without century), and |
| 519 | // %g (year of week number) have no effect when parsed, and are supported |
| 520 | // solely so that it's possible for strptime(3) to parse everything that |
| 521 | // strftime(3) can output. |
| 522 | struct tm tm = {}; |
| 523 | ASSERT_EQ('\0', *strptime("1 2 3", "%V %G %g", &tm)); |
| 524 | struct tm zero = {}; |
| 525 | EXPECT_TRUE(memcmp(&tm, &zero, sizeof(tm)) == 0); |
| 526 | } |
| 527 | |
Elliott Hughes | d065c04 | 2020-09-01 19:02:44 -0700 | [diff] [blame] | 528 | TEST(time, strptime_Z) { |
| 529 | #if defined(__BIONIC__) |
| 530 | // glibc doesn't handle %Z at all. |
| 531 | // The BSDs only handle hard-coded "GMT" and "UTC", plus whatever two strings |
| 532 | // are in the global `tzname` (which correspond to the current $TZ). |
| 533 | struct tm tm; |
| 534 | setenv("TZ", "Europe/Berlin", 1); |
| 535 | |
| 536 | // "GMT" always works. |
| 537 | tm = {}; |
| 538 | ASSERT_EQ('\0', *strptime("GMT", "%Z", &tm)); |
| 539 | EXPECT_STREQ("GMT", tm.tm_zone); |
| 540 | EXPECT_EQ(0, tm.tm_isdst); |
| 541 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 542 | |
| 543 | // As does "UTC". |
| 544 | tm = {}; |
| 545 | ASSERT_EQ('\0', *strptime("UTC", "%Z", &tm)); |
| 546 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 547 | EXPECT_EQ(0, tm.tm_isdst); |
| 548 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 549 | |
| 550 | // Europe/Berlin is known as "CET" when there's no DST. |
| 551 | tm = {}; |
| 552 | ASSERT_EQ('\0', *strptime("CET", "%Z", &tm)); |
| 553 | EXPECT_STREQ("CET", tm.tm_zone); |
| 554 | EXPECT_EQ(0, tm.tm_isdst); |
| 555 | EXPECT_EQ(3600, tm.tm_gmtoff); |
| 556 | |
| 557 | // Europe/Berlin is known as "CEST" when there's no DST. |
| 558 | tm = {}; |
| 559 | ASSERT_EQ('\0', *strptime("CEST", "%Z", &tm)); |
| 560 | EXPECT_STREQ("CEST", tm.tm_zone); |
| 561 | EXPECT_EQ(1, tm.tm_isdst); |
| 562 | EXPECT_EQ(3600, tm.tm_gmtoff); |
| 563 | |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 564 | // And as long as we're in Europe/Berlin, those are the only timezone |
Elliott Hughes | d065c04 | 2020-09-01 19:02:44 -0700 | [diff] [blame] | 565 | // abbreviations that are recognized. |
| 566 | tm = {}; |
| 567 | ASSERT_TRUE(strptime("PDT", "%Z", &tm) == nullptr); |
| 568 | #endif |
| 569 | } |
| 570 | |
| 571 | TEST(time, strptime_z) { |
| 572 | struct tm tm; |
| 573 | setenv("TZ", "Europe/Berlin", 1); |
| 574 | |
| 575 | // "UT" is what RFC822 called UTC. |
| 576 | tm = {}; |
| 577 | ASSERT_EQ('\0', *strptime("UT", "%z", &tm)); |
| 578 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 579 | EXPECT_EQ(0, tm.tm_isdst); |
| 580 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 581 | // "GMT" is RFC822's other name for UTC. |
| 582 | tm = {}; |
| 583 | ASSERT_EQ('\0', *strptime("GMT", "%z", &tm)); |
| 584 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 585 | EXPECT_EQ(0, tm.tm_isdst); |
| 586 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 587 | |
| 588 | // "Z" ("Zulu") is a synonym for UTC. |
| 589 | tm = {}; |
| 590 | ASSERT_EQ('\0', *strptime("Z", "%z", &tm)); |
| 591 | EXPECT_STREQ("UTC", tm.tm_zone); |
| 592 | EXPECT_EQ(0, tm.tm_isdst); |
| 593 | EXPECT_EQ(0, tm.tm_gmtoff); |
| 594 | |
| 595 | // "PST"/"PDT" and the other common US zone abbreviations are all supported. |
| 596 | tm = {}; |
| 597 | ASSERT_EQ('\0', *strptime("PST", "%z", &tm)); |
| 598 | EXPECT_STREQ("PST", tm.tm_zone); |
| 599 | EXPECT_EQ(0, tm.tm_isdst); |
| 600 | EXPECT_EQ(-28800, tm.tm_gmtoff); |
| 601 | tm = {}; |
| 602 | ASSERT_EQ('\0', *strptime("PDT", "%z", &tm)); |
| 603 | EXPECT_STREQ("PDT", tm.tm_zone); |
| 604 | EXPECT_EQ(1, tm.tm_isdst); |
| 605 | EXPECT_EQ(-25200, tm.tm_gmtoff); |
| 606 | |
| 607 | // +-hh |
| 608 | tm = {}; |
| 609 | ASSERT_EQ('\0', *strptime("+01", "%z", &tm)); |
| 610 | EXPECT_EQ(3600, tm.tm_gmtoff); |
| 611 | EXPECT_TRUE(tm.tm_zone == nullptr); |
| 612 | EXPECT_EQ(0, tm.tm_isdst); |
| 613 | // +-hhmm |
| 614 | tm = {}; |
| 615 | ASSERT_EQ('\0', *strptime("+0130", "%z", &tm)); |
| 616 | EXPECT_EQ(5400, tm.tm_gmtoff); |
| 617 | EXPECT_TRUE(tm.tm_zone == nullptr); |
| 618 | EXPECT_EQ(0, tm.tm_isdst); |
| 619 | // +-hh:mm |
| 620 | tm = {}; |
| 621 | ASSERT_EQ('\0', *strptime("+01:30", "%z", &tm)); |
| 622 | EXPECT_EQ(5400, tm.tm_gmtoff); |
| 623 | EXPECT_TRUE(tm.tm_zone == nullptr); |
| 624 | EXPECT_EQ(0, tm.tm_isdst); |
| 625 | } |
| 626 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 627 | void SetTime(timer_t t, time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { |
| 628 | itimerspec ts; |
| 629 | ts.it_value.tv_sec = value_s; |
| 630 | ts.it_value.tv_nsec = value_ns; |
| 631 | ts.it_interval.tv_sec = interval_s; |
| 632 | ts.it_interval.tv_nsec = interval_ns; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 633 | ASSERT_EQ(0, timer_settime(t, 0, &ts, nullptr)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 636 | static void NoOpNotifyFunction(sigval) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | TEST(time, timer_create) { |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 640 | sigevent se; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 641 | memset(&se, 0, sizeof(se)); |
| 642 | se.sigev_notify = SIGEV_THREAD; |
| 643 | se.sigev_notify_function = NoOpNotifyFunction; |
| 644 | timer_t timer_id; |
| 645 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); |
| 646 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 647 | pid_t pid = fork(); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 648 | ASSERT_NE(-1, pid) << strerror(errno); |
| 649 | |
| 650 | if (pid == 0) { |
| 651 | // Timers are not inherited by the child. |
| 652 | ASSERT_EQ(-1, timer_delete(timer_id)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 653 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 654 | _exit(0); |
| 655 | } |
| 656 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 657 | AssertChildExited(pid, 0); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 658 | |
| 659 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 660 | } |
| 661 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 662 | static int timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 663 | static void timer_create_SIGEV_SIGNAL_signal_handler(int signal_number) { |
| 664 | ++timer_create_SIGEV_SIGNAL_signal_handler_invocation_count; |
| 665 | ASSERT_EQ(SIGUSR1, signal_number); |
| 666 | } |
| 667 | |
| 668 | TEST(time, timer_create_SIGEV_SIGNAL) { |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 669 | sigevent se; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 670 | memset(&se, 0, sizeof(se)); |
| 671 | se.sigev_notify = SIGEV_SIGNAL; |
| 672 | se.sigev_signo = SIGUSR1; |
| 673 | |
| 674 | timer_t timer_id; |
| 675 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); |
| 676 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 677 | timer_create_SIGEV_SIGNAL_signal_handler_invocation_count = 0; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 678 | ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler); |
| 679 | |
| 680 | ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); |
| 681 | |
| 682 | itimerspec ts; |
| 683 | ts.it_value.tv_sec = 0; |
| 684 | ts.it_value.tv_nsec = 1; |
| 685 | ts.it_interval.tv_sec = 0; |
| 686 | ts.it_interval.tv_nsec = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 687 | ASSERT_EQ(0, timer_settime(timer_id, 0, &ts, nullptr)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 688 | |
| 689 | usleep(500000); |
| 690 | ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); |
| 691 | } |
| 692 | |
| 693 | struct Counter { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 694 | private: |
| 695 | std::atomic<int> value; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 696 | timer_t timer_id; |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 697 | sigevent se; |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 698 | bool timer_valid; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 699 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 700 | void Create() { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 701 | ASSERT_FALSE(timer_valid); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 702 | ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &timer_id)); |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 703 | timer_valid = true; |
| 704 | } |
| 705 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 706 | public: |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 707 | explicit Counter(void (*fn)(sigval)) : value(0), timer_valid(false) { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 708 | memset(&se, 0, sizeof(se)); |
| 709 | se.sigev_notify = SIGEV_THREAD; |
| 710 | se.sigev_notify_function = fn; |
| 711 | se.sigev_value.sival_ptr = this; |
| 712 | Create(); |
| 713 | } |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 714 | void DeleteTimer() { |
| 715 | ASSERT_TRUE(timer_valid); |
| 716 | ASSERT_EQ(0, timer_delete(timer_id)); |
| 717 | timer_valid = false; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | ~Counter() { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 721 | if (timer_valid) { |
| 722 | DeleteTimer(); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 726 | int Value() const { |
| 727 | return value; |
| 728 | } |
| 729 | |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 730 | void SetTime(time_t value_s, time_t value_ns, time_t interval_s, time_t interval_ns) { |
| 731 | ::SetTime(timer_id, value_s, value_ns, interval_s, interval_ns); |
| 732 | } |
| 733 | |
| 734 | bool ValueUpdated() { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 735 | int current_value = value; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 736 | time_t start = time(nullptr); |
| 737 | while (current_value == value && (time(nullptr) - start) < 5) { |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 738 | } |
| 739 | return current_value != value; |
| 740 | } |
| 741 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 742 | static void CountNotifyFunction(sigval value) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 743 | Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); |
| 744 | ++cd->value; |
| 745 | } |
| 746 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 747 | static void CountAndDisarmNotifyFunction(sigval value) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 748 | Counter* cd = reinterpret_cast<Counter*>(value.sival_ptr); |
| 749 | ++cd->value; |
| 750 | |
| 751 | // Setting the initial expiration time to 0 disarms the timer. |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 752 | cd->SetTime(0, 0, 1, 0); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 753 | } |
| 754 | }; |
| 755 | |
| 756 | TEST(time, timer_settime_0) { |
| 757 | Counter counter(Counter::CountAndDisarmNotifyFunction); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 758 | ASSERT_EQ(0, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 759 | |
Yabin Cui | bf572d9 | 2015-08-11 11:23:16 -0700 | [diff] [blame] | 760 | counter.SetTime(0, 500000000, 1, 0); |
| 761 | sleep(1); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 762 | |
| 763 | // 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] | 764 | ASSERT_EQ(1, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | TEST(time, timer_settime_repeats) { |
| 768 | Counter counter(Counter::CountNotifyFunction); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 769 | ASSERT_EQ(0, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 770 | |
Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 771 | counter.SetTime(0, 1, 0, 10); |
| 772 | ASSERT_TRUE(counter.ValueUpdated()); |
| 773 | ASSERT_TRUE(counter.ValueUpdated()); |
| 774 | ASSERT_TRUE(counter.ValueUpdated()); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 775 | counter.DeleteTimer(); |
| 776 | // Add a sleep as other threads may be calling the callback function when the timer is deleted. |
| 777 | usleep(500000); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 780 | static int timer_create_NULL_signal_handler_invocation_count; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 781 | static void timer_create_NULL_signal_handler(int signal_number) { |
| 782 | ++timer_create_NULL_signal_handler_invocation_count; |
| 783 | ASSERT_EQ(SIGALRM, signal_number); |
| 784 | } |
| 785 | |
| 786 | TEST(time, timer_create_NULL) { |
| 787 | // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM. |
| 788 | timer_t timer_id; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 789 | ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, nullptr, &timer_id)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 790 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 791 | timer_create_NULL_signal_handler_invocation_count = 0; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 792 | ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler); |
| 793 | |
| 794 | ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count); |
| 795 | |
| 796 | SetTime(timer_id, 0, 1, 0, 0); |
| 797 | usleep(500000); |
| 798 | |
| 799 | ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count); |
| 800 | } |
| 801 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 802 | static int GetThreadCount() { |
| 803 | std::string status; |
| 804 | if (android::base::ReadFileToString("/proc/self/status", &status)) { |
| 805 | for (const auto& line : android::base::Split(status, "\n")) { |
| 806 | int thread_count; |
| 807 | if (sscanf(line.c_str(), "Threads: %d", &thread_count) == 1) { |
| 808 | return thread_count; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | return -1; |
| 813 | } |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 814 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 815 | TEST(time, timer_create_EINVAL) { |
| 816 | const clockid_t kInvalidClock = 16; |
| 817 | |
| 818 | // A SIGEV_SIGNAL timer failure is easy; that's the kernel's problem. |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 819 | timer_t timer_id; |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 820 | ASSERT_EQ(-1, timer_create(kInvalidClock, nullptr, &timer_id)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 821 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 822 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 823 | // A SIGEV_THREAD timer failure is more interesting because we have a thread |
| 824 | // to clean up (https://issuetracker.google.com/340125671). |
| 825 | sigevent se = {}; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 826 | se.sigev_notify = SIGEV_THREAD; |
| 827 | se.sigev_notify_function = NoOpNotifyFunction; |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 828 | ASSERT_EQ(-1, timer_create(kInvalidClock, &se, &timer_id)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 829 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame] | 830 | |
| 831 | // timer_create() doesn't guarantee that the thread will be dead _before_ |
| 832 | // it returns because that would require extra synchronization that's |
| 833 | // unnecessary in the normal (successful) case. A timeout here means we |
| 834 | // leaked a thread. |
| 835 | while (GetThreadCount() > 1) { |
| 836 | } |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 839 | TEST(time, timer_create_multiple) { |
| 840 | Counter counter1(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 841 | Counter counter2(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 842 | Counter counter3(Counter::CountNotifyFunction); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 843 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 844 | ASSERT_EQ(0, counter1.Value()); |
| 845 | ASSERT_EQ(0, counter2.Value()); |
| 846 | ASSERT_EQ(0, counter3.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 847 | |
Yabin Cui | 410c1ad | 2015-06-18 16:19:02 -0700 | [diff] [blame] | 848 | counter2.SetTime(0, 500000000, 0, 0); |
| 849 | sleep(1); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 850 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 851 | EXPECT_EQ(0, counter1.Value()); |
| 852 | EXPECT_EQ(1, counter2.Value()); |
| 853 | EXPECT_EQ(0, counter3.Value()); |
| 854 | } |
| 855 | |
| 856 | // Test to verify that disarming a repeatable timer disables the callbacks. |
| 857 | TEST(time, timer_disarm_terminates) { |
| 858 | Counter counter(Counter::CountNotifyFunction); |
| 859 | ASSERT_EQ(0, counter.Value()); |
| 860 | |
| 861 | counter.SetTime(0, 1, 0, 1); |
| 862 | ASSERT_TRUE(counter.ValueUpdated()); |
| 863 | ASSERT_TRUE(counter.ValueUpdated()); |
| 864 | ASSERT_TRUE(counter.ValueUpdated()); |
| 865 | |
| 866 | counter.SetTime(0, 0, 0, 0); |
| 867 | // Add a sleep as the kernel may have pending events when the timer is disarmed. |
| 868 | usleep(500000); |
| 869 | int value = counter.Value(); |
| 870 | usleep(500000); |
| 871 | |
| 872 | // Verify the counter has not been incremented. |
| 873 | ASSERT_EQ(value, counter.Value()); |
| 874 | } |
| 875 | |
| 876 | // Test to verify that deleting a repeatable timer disables the callbacks. |
| 877 | TEST(time, timer_delete_terminates) { |
| 878 | Counter counter(Counter::CountNotifyFunction); |
| 879 | ASSERT_EQ(0, counter.Value()); |
| 880 | |
| 881 | counter.SetTime(0, 1, 0, 1); |
| 882 | ASSERT_TRUE(counter.ValueUpdated()); |
| 883 | ASSERT_TRUE(counter.ValueUpdated()); |
| 884 | ASSERT_TRUE(counter.ValueUpdated()); |
| 885 | |
| 886 | counter.DeleteTimer(); |
| 887 | // Add a sleep as other threads may be calling the callback function when the timer is deleted. |
| 888 | usleep(500000); |
| 889 | int value = counter.Value(); |
| 890 | usleep(500000); |
| 891 | |
| 892 | // Verify the counter has not been incremented. |
| 893 | ASSERT_EQ(value, counter.Value()); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 894 | } |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 895 | |
| 896 | struct TimerDeleteData { |
| 897 | timer_t timer_id; |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 898 | pid_t tid; |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 899 | volatile bool complete; |
| 900 | }; |
| 901 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 902 | static void TimerDeleteCallback(sigval value) { |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 903 | TimerDeleteData* tdd = reinterpret_cast<TimerDeleteData*>(value.sival_ptr); |
| 904 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 905 | tdd->tid = gettid(); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 906 | timer_delete(tdd->timer_id); |
| 907 | tdd->complete = true; |
| 908 | } |
| 909 | |
| 910 | TEST(time, timer_delete_from_timer_thread) { |
| 911 | TimerDeleteData tdd; |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 912 | sigevent se; |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 913 | |
| 914 | memset(&se, 0, sizeof(se)); |
| 915 | se.sigev_notify = SIGEV_THREAD; |
| 916 | se.sigev_notify_function = TimerDeleteCallback; |
| 917 | se.sigev_value.sival_ptr = &tdd; |
| 918 | |
| 919 | tdd.complete = false; |
| 920 | ASSERT_EQ(0, timer_create(CLOCK_REALTIME, &se, &tdd.timer_id)); |
| 921 | |
| 922 | itimerspec ts; |
Christopher Ferris | 3da136a | 2015-02-18 17:11:47 -0800 | [diff] [blame] | 923 | ts.it_value.tv_sec = 1; |
| 924 | ts.it_value.tv_nsec = 0; |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 925 | ts.it_interval.tv_sec = 0; |
| 926 | ts.it_interval.tv_nsec = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 927 | ASSERT_EQ(0, timer_settime(tdd.timer_id, 0, &ts, nullptr)); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 928 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 929 | time_t cur_time = time(nullptr); |
| 930 | while (!tdd.complete && (time(nullptr) - cur_time) < 5); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 931 | ASSERT_TRUE(tdd.complete); |
| 932 | |
| 933 | #if defined(__BIONIC__) |
| 934 | // Since bionic timers are implemented by creating a thread to handle the |
| 935 | // callback, verify that the thread actually completes. |
| 936 | cur_time = time(NULL); |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 937 | while ((kill(tdd.tid, 0) != -1 || errno != ESRCH) && (time(NULL) - cur_time) < 5); |
| 938 | ASSERT_EQ(-1, kill(tdd.tid, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 939 | ASSERT_ERRNO(ESRCH); |
Christopher Ferris | 753ad77 | 2014-03-20 20:47:45 -0700 | [diff] [blame] | 940 | #endif |
| 941 | } |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 942 | |
Colin Cross | 35d469b | 2021-08-16 15:26:28 -0700 | [diff] [blame] | 943 | // Musl doesn't define __NR_clock_gettime on 32-bit architectures. |
| 944 | #if !defined(__NR_clock_gettime) |
| 945 | #define __NR_clock_gettime __NR_clock_gettime32 |
| 946 | #endif |
| 947 | |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 948 | TEST(time, clock_gettime) { |
| 949 | // Try to ensure that our vdso clock_gettime is working. |
Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 950 | timespec ts0; |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 951 | timespec ts1; |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 952 | timespec ts2; |
Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 953 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts0)); |
| 954 | ASSERT_EQ(0, syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts1)); |
| 955 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts2)); |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 956 | |
Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 957 | // Check we have a nice monotonic timestamp sandwich. |
| 958 | ASSERT_LE(ts0.tv_sec, ts1.tv_sec); |
| 959 | if (ts0.tv_sec == ts1.tv_sec) { |
| 960 | ASSERT_LE(ts0.tv_nsec, ts1.tv_nsec); |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 961 | } |
Giuliano Procida | 096f595 | 2021-04-08 10:51:58 +0100 | [diff] [blame] | 962 | ASSERT_LE(ts1.tv_sec, ts2.tv_sec); |
| 963 | if (ts1.tv_sec == ts2.tv_sec) { |
| 964 | ASSERT_LE(ts1.tv_nsec, ts2.tv_nsec); |
| 965 | } |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 966 | } |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 967 | |
Elliott Hughes | 3a8f75d | 2017-10-05 10:33:18 -0700 | [diff] [blame] | 968 | TEST(time, clock_gettime_CLOCK_REALTIME) { |
| 969 | timespec ts; |
| 970 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 971 | } |
| 972 | |
| 973 | TEST(time, clock_gettime_CLOCK_MONOTONIC) { |
| 974 | timespec ts; |
| 975 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 976 | } |
| 977 | |
| 978 | TEST(time, clock_gettime_CLOCK_PROCESS_CPUTIME_ID) { |
| 979 | timespec ts; |
| 980 | ASSERT_EQ(0, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 981 | } |
| 982 | |
| 983 | TEST(time, clock_gettime_CLOCK_THREAD_CPUTIME_ID) { |
| 984 | timespec ts; |
| 985 | ASSERT_EQ(0, clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts)); |
| 986 | } |
| 987 | |
| 988 | TEST(time, clock_gettime_CLOCK_BOOTTIME) { |
| 989 | timespec ts; |
| 990 | ASSERT_EQ(0, clock_gettime(CLOCK_BOOTTIME, &ts)); |
| 991 | } |
| 992 | |
Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame] | 993 | TEST(time, clock_gettime_unknown) { |
| 994 | errno = 0; |
| 995 | timespec ts; |
| 996 | ASSERT_EQ(-1, clock_gettime(-1, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 997 | ASSERT_ERRNO(EINVAL); |
Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | TEST(time, clock_getres_CLOCK_REALTIME) { |
| 1001 | timespec ts; |
| 1002 | ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, &ts)); |
| 1003 | ASSERT_EQ(1, ts.tv_nsec); |
| 1004 | ASSERT_EQ(0, ts.tv_sec); |
| 1005 | } |
| 1006 | |
| 1007 | TEST(time, clock_getres_CLOCK_MONOTONIC) { |
| 1008 | timespec ts; |
| 1009 | ASSERT_EQ(0, clock_getres(CLOCK_MONOTONIC, &ts)); |
| 1010 | ASSERT_EQ(1, ts.tv_nsec); |
| 1011 | ASSERT_EQ(0, ts.tv_sec); |
| 1012 | } |
| 1013 | |
| 1014 | TEST(time, clock_getres_CLOCK_PROCESS_CPUTIME_ID) { |
| 1015 | timespec ts; |
| 1016 | ASSERT_EQ(0, clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1017 | } |
| 1018 | |
| 1019 | TEST(time, clock_getres_CLOCK_THREAD_CPUTIME_ID) { |
| 1020 | timespec ts; |
| 1021 | ASSERT_EQ(0, clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts)); |
| 1022 | } |
| 1023 | |
| 1024 | TEST(time, clock_getres_CLOCK_BOOTTIME) { |
| 1025 | timespec ts; |
| 1026 | ASSERT_EQ(0, clock_getres(CLOCK_BOOTTIME, &ts)); |
| 1027 | ASSERT_EQ(1, ts.tv_nsec); |
| 1028 | ASSERT_EQ(0, ts.tv_sec); |
| 1029 | } |
| 1030 | |
| 1031 | TEST(time, clock_getres_unknown) { |
| 1032 | errno = 0; |
| 1033 | timespec ts = { -1, -1 }; |
| 1034 | ASSERT_EQ(-1, clock_getres(-1, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1035 | ASSERT_ERRNO(EINVAL); |
Mark Salyzyn | 6a5a99f | 2017-11-21 12:29:06 -0800 | [diff] [blame] | 1036 | ASSERT_EQ(-1, ts.tv_nsec); |
| 1037 | ASSERT_EQ(-1, ts.tv_sec); |
| 1038 | } |
| 1039 | |
zijunzhao | e620266 | 2023-01-03 23:32:18 +0000 | [diff] [blame] | 1040 | TEST(time, clock_getres_null_resolution) { |
| 1041 | ASSERT_EQ(0, clock_getres(CLOCK_REALTIME, nullptr)); |
| 1042 | } |
| 1043 | |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 1044 | TEST(time, clock) { |
Giuliano Procida | ebc88d2 | 2021-04-07 14:25:13 +0100 | [diff] [blame] | 1045 | // clock(3) is hard to test, but a 1s sleep should cost less than 10ms on average. |
| 1046 | static const clock_t N = 5; |
| 1047 | static const clock_t mean_limit_ms = 10; |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1048 | clock_t t0 = clock(); |
Giuliano Procida | ebc88d2 | 2021-04-07 14:25:13 +0100 | [diff] [blame] | 1049 | for (size_t i = 0; i < N; ++i) { |
| 1050 | sleep(1); |
| 1051 | } |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1052 | clock_t t1 = clock(); |
Giuliano Procida | ebc88d2 | 2021-04-07 14:25:13 +0100 | [diff] [blame] | 1053 | ASSERT_LT(t1 - t0, N * mean_limit_ms * (CLOCKS_PER_SEC / 1000)); |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1054 | } |
| 1055 | |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1056 | static pid_t GetInvalidPid() { |
| 1057 | std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/proc/sys/kernel/pid_max", "r"), fclose}; |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1058 | long pid_max; |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1059 | fscanf(fp.get(), "%ld", &pid_max); |
| 1060 | return static_cast<pid_t>(pid_max + 1); |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1063 | TEST(time, clock_getcpuclockid_current) { |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1064 | clockid_t clockid; |
| 1065 | ASSERT_EQ(0, clock_getcpuclockid(getpid(), &clockid)); |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1066 | timespec ts; |
| 1067 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1068 | } |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1069 | |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1070 | TEST(time, clock_getcpuclockid_parent) { |
| 1071 | clockid_t clockid; |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1072 | ASSERT_EQ(0, clock_getcpuclockid(getppid(), &clockid)); |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1073 | timespec ts; |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1074 | ASSERT_EQ(0, clock_gettime(clockid, &ts)); |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1075 | } |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1076 | |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1077 | TEST(time, clock_getcpuclockid_ESRCH) { |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1078 | // We can't use -1 for invalid pid here, because clock_getcpuclockid() can't detect it. |
| 1079 | errno = 0; |
Mark Salyzyn | 71b81a2 | 2017-11-28 13:48:45 -0800 | [diff] [blame] | 1080 | // If this fails, your kernel needs commit e1b6b6ce to be backported. |
Elliott Hughes | b441359 | 2017-11-29 18:17:06 -0800 | [diff] [blame] | 1081 | clockid_t clockid; |
Mark Salyzyn | 71b81a2 | 2017-11-28 13:48:45 -0800 | [diff] [blame] | 1082 | ASSERT_EQ(ESRCH, clock_getcpuclockid(GetInvalidPid(), &clockid)) << "\n" |
| 1083 | << "Please ensure that the following kernel patches or their replacements have been applied:\n" |
| 1084 | << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/" |
| 1085 | << "commit/?id=e1b6b6ce55a0a25c8aa8af019095253b2133a41a\n" |
| 1086 | << "* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/" |
| 1087 | << "commit/?id=c80ed088a519da53f27b798a69748eaabc66aadf\n"; |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1088 | ASSERT_ERRNO(0); |
Yabin Cui | d5c6527 | 2014-11-26 14:04:26 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1091 | TEST(time, clock_settime) { |
| 1092 | errno = 0; |
| 1093 | timespec ts; |
| 1094 | ASSERT_EQ(-1, clock_settime(-1, &ts)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1095 | ASSERT_ERRNO(EINVAL); |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1096 | } |
| 1097 | |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 1098 | TEST(time, clock_nanosleep_EINVAL) { |
Haruki Hasegawa | 1816025 | 2014-10-13 00:50:47 +0900 | [diff] [blame] | 1099 | timespec in; |
| 1100 | timespec out; |
| 1101 | ASSERT_EQ(EINVAL, clock_nanosleep(-1, 0, &in, &out)); |
Alex Van Brunt | 8d0b2db | 2014-09-26 13:32:47 -0700 | [diff] [blame] | 1102 | } |
Greg Hackmann | d15dfb2 | 2016-03-26 11:37:55 -0700 | [diff] [blame] | 1103 | |
| 1104 | TEST(time, clock_nanosleep_thread_cputime_id) { |
| 1105 | timespec in; |
| 1106 | in.tv_sec = 1; |
| 1107 | in.tv_nsec = 0; |
| 1108 | ASSERT_EQ(EINVAL, clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &in, nullptr)); |
| 1109 | } |
Elliott Hughes | 1244370 | 2016-10-19 16:02:31 -0700 | [diff] [blame] | 1110 | |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 1111 | TEST(time, clock_nanosleep) { |
| 1112 | auto t0 = std::chrono::steady_clock::now(); |
| 1113 | const timespec ts = {.tv_nsec = 5000000}; |
| 1114 | ASSERT_EQ(0, clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr)); |
| 1115 | auto t1 = std::chrono::steady_clock::now(); |
| 1116 | ASSERT_GE(t1-t0, 5000000ns); |
| 1117 | } |
| 1118 | |
| 1119 | TEST(time, nanosleep) { |
| 1120 | auto t0 = std::chrono::steady_clock::now(); |
| 1121 | const timespec ts = {.tv_nsec = 5000000}; |
| 1122 | ASSERT_EQ(0, nanosleep(&ts, nullptr)); |
| 1123 | auto t1 = std::chrono::steady_clock::now(); |
| 1124 | ASSERT_GE(t1-t0, 5000000ns); |
| 1125 | } |
| 1126 | |
| 1127 | TEST(time, nanosleep_EINVAL) { |
| 1128 | timespec ts = {.tv_sec = -1}; |
| 1129 | errno = 0; |
| 1130 | ASSERT_EQ(-1, nanosleep(&ts, nullptr)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1131 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Elliott Hughes | 1244370 | 2016-10-19 16:02:31 -0700 | [diff] [blame] | 1134 | TEST(time, bug_31938693) { |
| 1135 | // User-visible symptoms in N: |
| 1136 | // http://b/31938693 |
| 1137 | // https://code.google.com/p/android/issues/detail?id=225132 |
| 1138 | |
| 1139 | // Actual underlying bug (the code change, not the tzdata upgrade that first exposed the bug): |
| 1140 | // http://b/31848040 |
| 1141 | |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1142 | // This isn't a great test, because very few timezones were actually affected, and there's |
Elliott Hughes | 1244370 | 2016-10-19 16:02:31 -0700 | [diff] [blame] | 1143 | // no real logic to which ones were affected: it was just a coincidence of the data that came |
| 1144 | // after them in the tzdata file. |
| 1145 | |
| 1146 | time_t t = 1475619727; |
| 1147 | struct tm tm; |
| 1148 | |
| 1149 | setenv("TZ", "America/Los_Angeles", 1); |
| 1150 | tzset(); |
| 1151 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1152 | EXPECT_EQ(15, tm.tm_hour); |
| 1153 | |
| 1154 | setenv("TZ", "Europe/London", 1); |
| 1155 | tzset(); |
| 1156 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1157 | EXPECT_EQ(23, tm.tm_hour); |
| 1158 | |
| 1159 | setenv("TZ", "America/Atka", 1); |
| 1160 | tzset(); |
| 1161 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1162 | EXPECT_EQ(13, tm.tm_hour); |
| 1163 | |
| 1164 | setenv("TZ", "Pacific/Apia", 1); |
| 1165 | tzset(); |
| 1166 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1167 | EXPECT_EQ(12, tm.tm_hour); |
| 1168 | |
| 1169 | setenv("TZ", "Pacific/Honolulu", 1); |
| 1170 | tzset(); |
| 1171 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1172 | EXPECT_EQ(12, tm.tm_hour); |
| 1173 | |
| 1174 | setenv("TZ", "Asia/Magadan", 1); |
| 1175 | tzset(); |
| 1176 | ASSERT_TRUE(localtime_r(&t, &tm) != nullptr); |
| 1177 | EXPECT_EQ(9, tm.tm_hour); |
| 1178 | } |
Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 1179 | |
| 1180 | TEST(time, bug_31339449) { |
| 1181 | // POSIX says localtime acts as if it calls tzset. |
| 1182 | // tzset does two things: |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1183 | // 1. it sets the timezone ctime/localtime/mktime/strftime will use. |
Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 1184 | // 2. it sets the global `tzname`. |
| 1185 | // POSIX says localtime_r need not set `tzname` (2). |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1186 | // Q: should localtime_r set the timezone (1)? |
Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 1187 | // Upstream tzcode (and glibc) answer "no", everyone else answers "yes". |
| 1188 | |
| 1189 | // Pick a time, any time... |
| 1190 | time_t t = 1475619727; |
| 1191 | |
| 1192 | // Call tzset with a specific timezone. |
| 1193 | setenv("TZ", "America/Atka", 1); |
| 1194 | tzset(); |
| 1195 | |
| 1196 | // If we change the timezone and call localtime, localtime should use the new timezone. |
| 1197 | setenv("TZ", "America/Los_Angeles", 1); |
| 1198 | struct tm* tm_p = localtime(&t); |
| 1199 | EXPECT_EQ(15, tm_p->tm_hour); |
| 1200 | |
| 1201 | // Reset the timezone back. |
| 1202 | setenv("TZ", "America/Atka", 1); |
| 1203 | tzset(); |
| 1204 | |
| 1205 | #if defined(__BIONIC__) |
| 1206 | // If we change the timezone again and call localtime_r, localtime_r should use the new timezone. |
| 1207 | setenv("TZ", "America/Los_Angeles", 1); |
| 1208 | struct tm tm = {}; |
| 1209 | localtime_r(&t, &tm); |
| 1210 | EXPECT_EQ(15, tm.tm_hour); |
| 1211 | #else |
| 1212 | // The BSDs agree with us, but glibc gets this wrong. |
| 1213 | #endif |
| 1214 | } |
Elliott Hughes | 5a29d54 | 2017-12-07 16:05:57 -0800 | [diff] [blame] | 1215 | |
| 1216 | TEST(time, asctime) { |
| 1217 | const struct tm tm = {}; |
| 1218 | ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", asctime(&tm)); |
| 1219 | } |
| 1220 | |
| 1221 | TEST(time, asctime_r) { |
| 1222 | const struct tm tm = {}; |
| 1223 | char buf[256]; |
| 1224 | ASSERT_EQ(buf, asctime_r(&tm, buf)); |
| 1225 | ASSERT_STREQ("Sun Jan 0 00:00:00 1900\n", buf); |
| 1226 | } |
| 1227 | |
| 1228 | TEST(time, ctime) { |
| 1229 | setenv("TZ", "UTC", 1); |
| 1230 | const time_t t = 0; |
| 1231 | ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", ctime(&t)); |
| 1232 | } |
| 1233 | |
| 1234 | TEST(time, ctime_r) { |
| 1235 | setenv("TZ", "UTC", 1); |
| 1236 | const time_t t = 0; |
| 1237 | char buf[256]; |
| 1238 | ASSERT_EQ(buf, ctime_r(&t, buf)); |
| 1239 | ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buf); |
| 1240 | } |
Elliott Hughes | 81baaf2 | 2018-02-28 15:22:48 -0800 | [diff] [blame] | 1241 | |
| 1242 | // https://issuetracker.google.com/37128336 |
| 1243 | TEST(time, strftime_strptime_s) { |
| 1244 | char buf[32]; |
| 1245 | const struct tm tm0 = { .tm_year = 1982-1900, .tm_mon = 0, .tm_mday = 1 }; |
| 1246 | |
| 1247 | setenv("TZ", "America/Los_Angeles", 1); |
| 1248 | strftime(buf, sizeof(buf), "<%s>", &tm0); |
| 1249 | EXPECT_STREQ("<378720000>", buf); |
| 1250 | |
| 1251 | setenv("TZ", "UTC", 1); |
| 1252 | strftime(buf, sizeof(buf), "<%s>", &tm0); |
| 1253 | EXPECT_STREQ("<378691200>", buf); |
| 1254 | |
| 1255 | struct tm tm; |
| 1256 | |
| 1257 | setenv("TZ", "America/Los_Angeles", 1); |
| 1258 | tzset(); |
| 1259 | memset(&tm, 0xff, sizeof(tm)); |
| 1260 | char* p = strptime("378720000x", "%s", &tm); |
| 1261 | ASSERT_EQ('x', *p); |
| 1262 | EXPECT_EQ(0, tm.tm_sec); |
| 1263 | EXPECT_EQ(0, tm.tm_min); |
| 1264 | EXPECT_EQ(0, tm.tm_hour); |
| 1265 | EXPECT_EQ(1, tm.tm_mday); |
| 1266 | EXPECT_EQ(0, tm.tm_mon); |
| 1267 | EXPECT_EQ(82, tm.tm_year); |
| 1268 | EXPECT_EQ(5, tm.tm_wday); |
| 1269 | EXPECT_EQ(0, tm.tm_yday); |
| 1270 | EXPECT_EQ(0, tm.tm_isdst); |
| 1271 | |
| 1272 | setenv("TZ", "UTC", 1); |
| 1273 | tzset(); |
| 1274 | memset(&tm, 0xff, sizeof(tm)); |
| 1275 | p = strptime("378691200x", "%s", &tm); |
| 1276 | ASSERT_EQ('x', *p); |
| 1277 | EXPECT_EQ(0, tm.tm_sec); |
| 1278 | EXPECT_EQ(0, tm.tm_min); |
| 1279 | EXPECT_EQ(0, tm.tm_hour); |
| 1280 | EXPECT_EQ(1, tm.tm_mday); |
| 1281 | EXPECT_EQ(0, tm.tm_mon); |
| 1282 | EXPECT_EQ(82, tm.tm_year); |
| 1283 | EXPECT_EQ(5, tm.tm_wday); |
| 1284 | EXPECT_EQ(0, tm.tm_yday); |
| 1285 | EXPECT_EQ(0, tm.tm_isdst); |
| 1286 | } |
| 1287 | |
| 1288 | TEST(time, strptime_s_nothing) { |
| 1289 | struct tm tm; |
| 1290 | ASSERT_EQ(nullptr, strptime("x", "%s", &tm)); |
| 1291 | } |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1292 | |
| 1293 | TEST(time, timespec_get) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1294 | #if defined(__BIONIC__) |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1295 | timespec ts = {}; |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1296 | ASSERT_EQ(TIME_UTC, timespec_get(&ts, TIME_UTC)); |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1297 | ASSERT_EQ(TIME_MONOTONIC, timespec_get(&ts, TIME_MONOTONIC)); |
| 1298 | ASSERT_EQ(TIME_ACTIVE, timespec_get(&ts, TIME_ACTIVE)); |
| 1299 | ASSERT_EQ(TIME_THREAD_ACTIVE, timespec_get(&ts, TIME_THREAD_ACTIVE)); |
| 1300 | #else |
| 1301 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| 1302 | #endif |
| 1303 | } |
| 1304 | |
| 1305 | TEST(time, timespec_get_invalid) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1306 | #if defined(__BIONIC__) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1307 | timespec ts = {}; |
Elliott Hughes | 7db0a6c | 2023-05-03 15:37:46 -0700 | [diff] [blame] | 1308 | ASSERT_EQ(0, timespec_get(&ts, 123)); |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1309 | #else |
| 1310 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| 1311 | #endif |
| 1312 | } |
| 1313 | |
| 1314 | TEST(time, timespec_getres) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1315 | #if defined(__BIONIC__) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1316 | timespec ts = {}; |
| 1317 | ASSERT_EQ(TIME_UTC, timespec_getres(&ts, TIME_UTC)); |
| 1318 | ASSERT_EQ(1, ts.tv_nsec); |
| 1319 | ASSERT_EQ(0, ts.tv_sec); |
| 1320 | #else |
| 1321 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
| 1322 | #endif |
| 1323 | } |
| 1324 | |
| 1325 | TEST(time, timespec_getres_invalid) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1326 | #if defined(__BIONIC__) |
Elliott Hughes | 52541ee | 2023-04-24 17:04:49 -0700 | [diff] [blame] | 1327 | timespec ts = {}; |
| 1328 | ASSERT_EQ(0, timespec_getres(&ts, 123)); |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1329 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1330 | GTEST_SKIP() << "glibc doesn't have timespec_get until 2.21"; |
Elliott Hughes | f98d87b | 2018-07-17 13:21:05 -0700 | [diff] [blame] | 1331 | #endif |
| 1332 | } |
Elliott Hughes | 208fdd1 | 2020-06-05 17:19:00 -0700 | [diff] [blame] | 1333 | |
| 1334 | TEST(time, difftime) { |
| 1335 | ASSERT_EQ(1.0, difftime(1, 0)); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 1336 | ASSERT_EQ(-1.0, difftime(0, 1)); |
Elliott Hughes | 208fdd1 | 2020-06-05 17:19:00 -0700 | [diff] [blame] | 1337 | } |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1338 | |
| 1339 | TEST(time, tzfree_null) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1340 | #if defined(__BIONIC__) |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1341 | tzfree(nullptr); |
| 1342 | #else |
| 1343 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1344 | #endif |
| 1345 | } |
| 1346 | |
| 1347 | TEST(time, localtime_rz) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1348 | #if defined(__BIONIC__) |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1349 | setenv("TZ", "America/Los_Angeles", 1); |
| 1350 | tzset(); |
| 1351 | |
| 1352 | auto AssertTmEq = [](const struct tm& rhs, int hour) { |
| 1353 | ASSERT_EQ(93, rhs.tm_year); |
| 1354 | ASSERT_EQ(0, rhs.tm_mon); |
| 1355 | ASSERT_EQ(1, rhs.tm_mday); |
| 1356 | ASSERT_EQ(hour, rhs.tm_hour); |
| 1357 | ASSERT_EQ(0, rhs.tm_min); |
| 1358 | ASSERT_EQ(0, rhs.tm_sec); |
| 1359 | }; |
| 1360 | |
| 1361 | const time_t t = 725875200; |
| 1362 | |
| 1363 | // Spam localtime_r() while we use localtime_rz(). |
| 1364 | std::atomic<bool> done = false; |
| 1365 | std::thread thread{[&] { |
| 1366 | while (!done) { |
| 1367 | struct tm tm {}; |
| 1368 | ASSERT_EQ(&tm, localtime_r(&t, &tm)); |
| 1369 | AssertTmEq(tm, 0); |
| 1370 | } |
| 1371 | }}; |
| 1372 | |
| 1373 | struct tm tm; |
| 1374 | |
| 1375 | timezone_t london{tzalloc("Europe/London")}; |
| 1376 | tm = {}; |
| 1377 | ASSERT_EQ(&tm, localtime_rz(london, &t, &tm)); |
| 1378 | AssertTmEq(tm, 8); |
| 1379 | |
| 1380 | timezone_t seoul{tzalloc("Asia/Seoul")}; |
| 1381 | tm = {}; |
| 1382 | ASSERT_EQ(&tm, localtime_rz(seoul, &t, &tm)); |
| 1383 | AssertTmEq(tm, 17); |
| 1384 | |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1385 | // Just check that mktime()'s timezone didn't change. |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1386 | tm = {}; |
| 1387 | ASSERT_EQ(&tm, localtime_r(&t, &tm)); |
| 1388 | ASSERT_EQ(0, tm.tm_hour); |
| 1389 | AssertTmEq(tm, 0); |
| 1390 | |
| 1391 | done = true; |
| 1392 | thread.join(); |
| 1393 | |
| 1394 | tzfree(london); |
| 1395 | tzfree(seoul); |
| 1396 | #else |
| 1397 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1398 | #endif |
| 1399 | } |
| 1400 | |
| 1401 | TEST(time, mktime_z) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1402 | #if defined(__BIONIC__) |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1403 | setenv("TZ", "America/Los_Angeles", 1); |
| 1404 | tzset(); |
| 1405 | |
| 1406 | // Spam mktime() while we use mktime_z(). |
| 1407 | std::atomic<bool> done = false; |
| 1408 | std::thread thread{[&done] { |
| 1409 | while (!done) { |
| 1410 | struct tm tm { |
| 1411 | .tm_year = 93, .tm_mday = 1 |
| 1412 | }; |
| 1413 | ASSERT_EQ(725875200, mktime(&tm)); |
| 1414 | } |
| 1415 | }}; |
| 1416 | |
| 1417 | struct tm tm; |
| 1418 | |
| 1419 | timezone_t london{tzalloc("Europe/London")}; |
| 1420 | tm = {.tm_year = 93, .tm_mday = 1}; |
| 1421 | ASSERT_EQ(725846400, mktime_z(london, &tm)); |
| 1422 | |
| 1423 | timezone_t seoul{tzalloc("Asia/Seoul")}; |
| 1424 | tm = {.tm_year = 93, .tm_mday = 1}; |
| 1425 | ASSERT_EQ(725814000, mktime_z(seoul, &tm)); |
| 1426 | |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1427 | // Just check that mktime()'s timezone didn't change. |
Elliott Hughes | 2bd4316 | 2023-06-15 13:17:08 -0700 | [diff] [blame] | 1428 | tm = {.tm_year = 93, .tm_mday = 1}; |
| 1429 | ASSERT_EQ(725875200, mktime(&tm)); |
| 1430 | |
| 1431 | done = true; |
| 1432 | thread.join(); |
| 1433 | |
| 1434 | tzfree(london); |
| 1435 | tzfree(seoul); |
| 1436 | #else |
| 1437 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1438 | #endif |
| 1439 | } |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1440 | |
| 1441 | TEST(time, tzalloc_nullptr) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1442 | #if defined(__BIONIC__) |
Elliott Hughes | 31fc69f | 2023-06-20 15:36:11 -0700 | [diff] [blame] | 1443 | // tzalloc(nullptr) returns the system timezone. |
| 1444 | timezone_t default_tz = tzalloc(nullptr); |
| 1445 | ASSERT_NE(nullptr, default_tz); |
| 1446 | |
| 1447 | // Check that mktime_z() with the default timezone matches mktime(). |
| 1448 | // This assumes that the system timezone doesn't change during the test, |
| 1449 | // but that should be unlikely, and we don't have much choice if we |
| 1450 | // want to write a test at all. |
| 1451 | // We unset $TZ before calling mktime() because mktime() honors $TZ. |
| 1452 | unsetenv("TZ"); |
| 1453 | struct tm tm = {.tm_year = 93, .tm_mday = 1}; |
| 1454 | time_t t = mktime(&tm); |
| 1455 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1456 | |
| 1457 | // Check that changing $TZ doesn't affect the tzalloc() default in |
| 1458 | // the same way it would the mktime() default. |
| 1459 | setenv("TZ", "America/Los_Angeles", 1); |
| 1460 | tzset(); |
| 1461 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1462 | |
| 1463 | setenv("TZ", "Europe/London", 1); |
| 1464 | tzset(); |
| 1465 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1466 | |
| 1467 | setenv("TZ", "Asia/Seoul", 1); |
| 1468 | tzset(); |
| 1469 | ASSERT_EQ(t, mktime_z(default_tz, &tm)); |
| 1470 | |
| 1471 | tzfree(default_tz); |
| 1472 | #else |
| 1473 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1474 | #endif |
| 1475 | } |
Elliott Hughes | 5ea305b | 2023-06-22 20:53:00 +0000 | [diff] [blame] | 1476 | |
| 1477 | TEST(time, tzalloc_unique_ptr) { |
Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1478 | #if defined(__BIONIC__) |
Elliott Hughes | 5ea305b | 2023-06-22 20:53:00 +0000 | [diff] [blame] | 1479 | std::unique_ptr<std::remove_pointer_t<timezone_t>, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), |
| 1480 | tzfree}; |
| 1481 | #else |
| 1482 | GTEST_SKIP() << "glibc doesn't have timezone_t"; |
| 1483 | #endif |
| 1484 | } |