1f6603c60Sopenharmony_ci/* 2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License. 5f6603c60Sopenharmony_ci * You may obtain a copy of the License at 6f6603c60Sopenharmony_ci * 7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f6603c60Sopenharmony_ci * 9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and 13f6603c60Sopenharmony_ci * limitations under the License. 14f6603c60Sopenharmony_ci */ 15f6603c60Sopenharmony_ci 16f6603c60Sopenharmony_ci#include <time.h> 17f6603c60Sopenharmony_ci#include <sys/timeb.h> 18f6603c60Sopenharmony_ci#include <sys/times.h> 19f6603c60Sopenharmony_ci#include <unistd.h> 20f6603c60Sopenharmony_ci#include <fcntl.h> 21f6603c60Sopenharmony_ci#include <errno.h> 22f6603c60Sopenharmony_ci#include <gtest/gtest.h> 23f6603c60Sopenharmony_ci#include "log.h" 24f6603c60Sopenharmony_ci#include "utils.h" 25f6603c60Sopenharmony_ci#include "ClockID.h" 26f6603c60Sopenharmony_ci 27f6603c60Sopenharmony_ciusing namespace testing::ext; 28f6603c60Sopenharmony_ci 29f6603c60Sopenharmony_ciconst char* DATEMSK_FILE = "/storage/getdate_mask"; 30f6603c60Sopenharmony_ci 31f6603c60Sopenharmony_ci// Resolution of: CLOCK_MONOTONIC, CLOCK_REALTIME, CLOCK_MONOTONIC_RAW: 1 us 32f6603c60Sopenharmony_ciconst int CLOCK_RESOLUTION_HIGH = 1000; 33f6603c60Sopenharmony_ci// Resolution of: CLOCK_MONOTONIC_COARSE, CLOCK_REALTIME_COARSE: 1 tick = 10 ms 34f6603c60Sopenharmony_ciconst int CLOCK_RESOLUTION_LOW = 1000 * 1000; 35f6603c60Sopenharmony_ci 36f6603c60Sopenharmony_ciclass ClockTimeTest : public testing::Test { 37f6603c60Sopenharmony_ciprotected: 38f6603c60Sopenharmony_ci static struct timespec mTestStatTime; 39f6603c60Sopenharmony_ci // SetUpTestCase: Testsuit setup, run before 1st testcase 40f6603c60Sopenharmony_ci static void SetUpTestCase(void) 41f6603c60Sopenharmony_ci { 42f6603c60Sopenharmony_ci clock_gettime(CLOCK_REALTIME, &mTestStatTime); 43f6603c60Sopenharmony_ci LOG("test start at %ld\n", (long)mTestStatTime.tv_sec); 44f6603c60Sopenharmony_ci } 45f6603c60Sopenharmony_ci // TearDownTestCase: Testsuit teardown, run after last testcase 46f6603c60Sopenharmony_ci static void TearDownTestCase(void) 47f6603c60Sopenharmony_ci { 48f6603c60Sopenharmony_ci struct timespec time1 = {0, 0}; 49f6603c60Sopenharmony_ci clock_gettime(CLOCK_REALTIME, &time1); 50f6603c60Sopenharmony_ci LOG("test end at %ld", (long)time1.tv_sec); 51f6603c60Sopenharmony_ci 52f6603c60Sopenharmony_ci mTestStatTime.tv_sec += 10; // approximate total test time 53f6603c60Sopenharmony_ci if (!clock_settime(CLOCK_REALTIME, &mTestStatTime)) { 54f6603c60Sopenharmony_ci LOG("restore time ok\n"); 55f6603c60Sopenharmony_ci } else { 56f6603c60Sopenharmony_ci LOG("restore time failed\n"); 57f6603c60Sopenharmony_ci } 58f6603c60Sopenharmony_ci } 59f6603c60Sopenharmony_ci}; 60f6603c60Sopenharmony_cistruct timespec ClockTimeTest::mTestStatTime; 61f6603c60Sopenharmony_ci 62f6603c60Sopenharmony_ci/** 63f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_TIME_API_CLOCK_GETRES_0100 64f6603c60Sopenharmony_ci * @tc.name test all supported clockid of clock_getres 65f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 66f6603c60Sopenharmony_ci */ 67f6603c60Sopenharmony_ciHWTEST_P(SupportedClockIDTest, testClockGetresAll, Function | MediumTest | Level1) 68f6603c60Sopenharmony_ci{ 69f6603c60Sopenharmony_ci clockid_t cid = GetParam(); 70f6603c60Sopenharmony_ci const char* cname = ALL_CLOCKS_NAME[cid]; 71f6603c60Sopenharmony_ci 72f6603c60Sopenharmony_ci struct timespec time1 = {0, 0}; 73f6603c60Sopenharmony_ci int rt = clock_getres(cid, &time1); 74f6603c60Sopenharmony_ci LOG("%s Resolution: %ld nanosecond\n", cname, time1.tv_nsec); 75f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "clock_getres of " << cname << "failed, errno =" <<errno; 76f6603c60Sopenharmony_ci 77f6603c60Sopenharmony_ci EXPECT_EQ(time1.tv_sec, 0); 78f6603c60Sopenharmony_ci if (cid == CLOCK_MONOTONIC || cid == CLOCK_REALTIME || cid == CLOCK_MONOTONIC_RAW) { 79f6603c60Sopenharmony_ci EXPECT_EQ(time1.tv_nsec, CLOCK_RESOLUTION_HIGH) << "Resolution check failed"; 80f6603c60Sopenharmony_ci } else { 81f6603c60Sopenharmony_ci EXPECT_EQ(time1.tv_nsec, CLOCK_RESOLUTION_LOW) << "Resolution check failed"; 82f6603c60Sopenharmony_ci } 83f6603c60Sopenharmony_ci} 84f6603c60Sopenharmony_ci 85f6603c60Sopenharmony_ci/** 86f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_TIME_API_CLOCK_GETTIME_0100 87f6603c60Sopenharmony_ci * @tc.name test all supported clockid of clock_gettime 88f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 89f6603c60Sopenharmony_ci */ 90f6603c60Sopenharmony_ciHWTEST_P(SupportedClockIDTest, testClockGettimeAll, Function | MediumTest | Level1) 91f6603c60Sopenharmony_ci{ 92f6603c60Sopenharmony_ci clockid_t cid = GetParam(); 93f6603c60Sopenharmony_ci const char* cname = ALL_CLOCKS_NAME[cid]; 94f6603c60Sopenharmony_ci 95f6603c60Sopenharmony_ci struct timespec time1 = {0, 0}; 96f6603c60Sopenharmony_ci int rt = clock_gettime(cid, &time1); 97f6603c60Sopenharmony_ci if (rt == 0) { 98f6603c60Sopenharmony_ci LOG("clock_gettime(%s) : tv_sec=%ld, tv_nsec=%ld\n", cname, time1.tv_sec, time1.tv_nsec); 99f6603c60Sopenharmony_ci } else { 100f6603c60Sopenharmony_ci LOG("%s return error, rt=%d, errno=%d:%s\n", cname, rt, errno, strerror(errno)); 101f6603c60Sopenharmony_ci } 102f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 103f6603c60Sopenharmony_ci} 104f6603c60Sopenharmony_ci 105f6603c60Sopenharmony_ciINSTANTIATE_TEST_CASE_P(ClockTimeTest, SupportedClockIDTest, ALL_SUPPORTED_IDS); 106f6603c60Sopenharmony_ci 107f6603c60Sopenharmony_ci 108f6603c60Sopenharmony_ci/** 109f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_TIME_API_CLOCK_SETTIME_0100 110f6603c60Sopenharmony_ci * @tc.name test clock_settime basic 111f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 112f6603c60Sopenharmony_ci */ 113f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testClockSettime, Function | MediumTest | Level1) 114f6603c60Sopenharmony_ci{ 115f6603c60Sopenharmony_ci struct timespec time1 = {0, 0}; 116f6603c60Sopenharmony_ci int rt = clock_gettime(CLOCK_REALTIME, &time1); 117f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "clock_gettime failed, errno=" << errno; 118f6603c60Sopenharmony_ci LOG("current time: sec=%llu, nsec=%ld", time1.tv_sec, time1.tv_nsec); 119f6603c60Sopenharmony_ci time_t sec = time1.tv_sec; 120f6603c60Sopenharmony_ci time1.tv_sec -= 1; 121f6603c60Sopenharmony_ci time1.tv_nsec = 1; 122f6603c60Sopenharmony_ci rt = clock_settime(CLOCK_REALTIME, &time1); 123f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "clock_settime failed, errno=" << errno; 124f6603c60Sopenharmony_ci sleep(1); 125f6603c60Sopenharmony_ci rt = clock_gettime(CLOCK_REALTIME, &time1); 126f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "clock_gettime failed, errno=" << errno; 127f6603c60Sopenharmony_ci ASSERT_EQ(sec, time1.tv_sec); 128f6603c60Sopenharmony_ci} 129f6603c60Sopenharmony_ci 130f6603c60Sopenharmony_ci/** 131f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_TIME_API_FTIME_0100 132f6603c60Sopenharmony_ci * @tc.name test ftime basic 133f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 134f6603c60Sopenharmony_ci */ 135f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testFtime, Function | MediumTest | Level1) 136f6603c60Sopenharmony_ci{ 137f6603c60Sopenharmony_ci Msleep(10); // hopefully to let the flowing code not scheduled-out 138f6603c60Sopenharmony_ci struct timeb tb = {0}; 139f6603c60Sopenharmony_ci struct timespec ts = {0}; 140f6603c60Sopenharmony_ci int rt = clock_gettime(CLOCK_REALTIME, &ts); 141f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "clock_gettime failed, errno=" << errno; 142f6603c60Sopenharmony_ci ts.tv_sec -= 1; 143f6603c60Sopenharmony_ci ts.tv_nsec = 1000000; 144f6603c60Sopenharmony_ci rt = clock_settime(CLOCK_REALTIME, &ts); 145f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "clock_settime failed, errno=" << errno; 146f6603c60Sopenharmony_ci rt = clock_gettime(CLOCK_REALTIME, &ts); 147f6603c60Sopenharmony_ci int rt2 = ftime(&tb); 148f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "clock_gettime failed, errno=" << errno; 149f6603c60Sopenharmony_ci EXPECT_EQ(rt2, 0) << "ftime failed, errno=" << errno; 150f6603c60Sopenharmony_ci LOG("current time: sec=%llu, nsec=%ld", ts.tv_sec, (long)ts.tv_nsec); 151f6603c60Sopenharmony_ci LOG("current time: sec=%llu, millitm=%d", tb.time, (int)tb.millitm); 152f6603c60Sopenharmony_ci EXPECT_EQ(ts.tv_sec, tb.time); 153f6603c60Sopenharmony_ci EXPECT_NEAR((int)ts.tv_nsec/1000000, (int)tb.millitm, 1); 154f6603c60Sopenharmony_ci} 155f6603c60Sopenharmony_ci 156f6603c60Sopenharmony_ci/** 157f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_TIME_API_STIME_0100 158f6603c60Sopenharmony_ci * @tc.name test stime basic 159f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 160f6603c60Sopenharmony_ci */ 161f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testStime, Function | MediumTest | Level1) 162f6603c60Sopenharmony_ci{ 163f6603c60Sopenharmony_ci Msleep(10); // hopefully to let the flowing code not scheduled-out 164f6603c60Sopenharmony_ci struct timespec ts = {0}; 165f6603c60Sopenharmony_ci int rt = clock_gettime(CLOCK_REALTIME, &ts); 166f6603c60Sopenharmony_ci LOG("current time: sec=%llu, nsec=%ld", ts.tv_sec, (long)ts.tv_nsec); 167f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "clock_gettime failed, errno=" << errno; 168f6603c60Sopenharmony_ci time_t t = ts.tv_sec + 1; 169f6603c60Sopenharmony_ci rt = stime(&t); 170f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "stime failed, errno=" << errno; 171f6603c60Sopenharmony_ci Msleep(10); 172f6603c60Sopenharmony_ci rt = clock_gettime(CLOCK_REALTIME, &ts); 173f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "clock_gettime failed, errno=" << errno; 174f6603c60Sopenharmony_ci LOG("current time: sec=%llu, nsec=%ld", ts.tv_sec, (long)ts.tv_nsec); 175f6603c60Sopenharmony_ci EXPECT_EQ(ts.tv_sec, t); 176f6603c60Sopenharmony_ci} 177f6603c60Sopenharmony_ci 178f6603c60Sopenharmony_ci/** 179f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_TIME_API_TIME_0100 180f6603c60Sopenharmony_ci * @tc.name test time basic 181f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 182f6603c60Sopenharmony_ci */ 183f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testTime, Function | MediumTest | Level1) 184f6603c60Sopenharmony_ci{ 185f6603c60Sopenharmony_ci Msleep(10); // hopefully to let the flowing code not scheduled-out 186f6603c60Sopenharmony_ci struct timespec ts = {0}; 187f6603c60Sopenharmony_ci int rt = clock_gettime(CLOCK_REALTIME, &ts); 188f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "clock_gettime failed, errno=" << errno; 189f6603c60Sopenharmony_ci rt = stime(&ts.tv_sec); // set ts.tv_nsec to 0 190f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "stime failed, errno=" << errno; 191f6603c60Sopenharmony_ci 192f6603c60Sopenharmony_ci time_t t1, t2; 193f6603c60Sopenharmony_ci t1 = time(&t2); 194f6603c60Sopenharmony_ci EXPECT_EQ(t1, ts.tv_sec) << "time failed"; 195f6603c60Sopenharmony_ci EXPECT_EQ(t1, t2) << "time failed"; 196f6603c60Sopenharmony_ci 197f6603c60Sopenharmony_ci t2 = 1; 198f6603c60Sopenharmony_ci rt = stime(&t2); 199f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "stime failed, errno=" << errno; 200f6603c60Sopenharmony_ci 201f6603c60Sopenharmony_ci t1 = time(&t2); 202f6603c60Sopenharmony_ci EXPECT_EQ(t1, 1); 203f6603c60Sopenharmony_ci EXPECT_EQ(t1, t2); 204f6603c60Sopenharmony_ci 205f6603c60Sopenharmony_ci t1 = time(nullptr); 206f6603c60Sopenharmony_ci EXPECT_EQ(t1, 1); 207f6603c60Sopenharmony_ci 208f6603c60Sopenharmony_ci rt = stime(&ts.tv_sec); // restore time 209f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "stime failed, errno=" << errno; 210f6603c60Sopenharmony_ci} 211f6603c60Sopenharmony_ci 212f6603c60Sopenharmony_ci/** 213f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_TIME_API_TIMES_0100 214f6603c60Sopenharmony_ci * @tc.name test times basic 215f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 216f6603c60Sopenharmony_ci */ 217f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testTimes, Function | MediumTest | Level1) 218f6603c60Sopenharmony_ci{ 219f6603c60Sopenharmony_ci // this test should run in a child process 220f6603c60Sopenharmony_ci pid_t pid = fork(); 221f6603c60Sopenharmony_ci ASSERT_TRUE(pid >= 0) << "======== Fork Error! ========="; 222f6603c60Sopenharmony_ci if (pid == 0) { // child 223f6603c60Sopenharmony_ci int exitCode = 0; 224f6603c60Sopenharmony_ci const int testClockt = 100; 225f6603c60Sopenharmony_ci const int msPerClock = 10; 226f6603c60Sopenharmony_ci struct tms start = {0}; 227f6603c60Sopenharmony_ci struct tms end = {0}; 228f6603c60Sopenharmony_ci clock_t stTime = times(&start); 229f6603c60Sopenharmony_ci LOG("start_clock: stTime: %ld", stTime); 230f6603c60Sopenharmony_ci LOG("start_clock: tms_utime: %ld, tms_stime: %ld, tms_cutime:%ld, tms_cstime:%ld", 231f6603c60Sopenharmony_ci start.tms_utime, start.tms_stime, start.tms_cutime, start.tms_cstime); 232f6603c60Sopenharmony_ci 233f6603c60Sopenharmony_ci KeepRun(testClockt * msPerClock); 234f6603c60Sopenharmony_ci 235f6603c60Sopenharmony_ci clock_t endTime = times(&end); 236f6603c60Sopenharmony_ci LOG("end_clock: endTime: %ld", endTime); 237f6603c60Sopenharmony_ci LOG("end_clock: tms_utime: %ld, tms_stime: %ld, tms_cutime:%ld, tms_cstime:%ld", 238f6603c60Sopenharmony_ci end.tms_utime, end.tms_stime, end.tms_cutime, end.tms_cstime); 239f6603c60Sopenharmony_ci 240f6603c60Sopenharmony_ci LOG("Real Time: %ld, User Time %ld, System Time %ld\n", (long)(endTime - stTime), 241f6603c60Sopenharmony_ci (long)(end.tms_utime - start.tms_utime), 242f6603c60Sopenharmony_ci (long)(end.tms_stime - start.tms_stime)); 243f6603c60Sopenharmony_ci 244f6603c60Sopenharmony_ci if (start.tms_utime != 0 || start.tms_stime != 0 || start.tms_cutime != 0 || start.tms_cstime != 0) { 245f6603c60Sopenharmony_ci LOG("init value check failed"); 246f6603c60Sopenharmony_ci } 247f6603c60Sopenharmony_ci if (!CheckValueClose(end.tms_utime, testClockt, 0.02)) { 248f6603c60Sopenharmony_ci LOG("tms_utime value check failed"); 249f6603c60Sopenharmony_ci } 250f6603c60Sopenharmony_ci if (!CheckValueClose((endTime - stTime), testClockt, 0.02)) { 251f6603c60Sopenharmony_ci LOG("Real Time value check failed"); 252f6603c60Sopenharmony_ci } 253f6603c60Sopenharmony_ci exit(exitCode); 254f6603c60Sopenharmony_ci } else { // parent 255f6603c60Sopenharmony_ci WaitProcExitedOK(pid); 256f6603c60Sopenharmony_ci } 257f6603c60Sopenharmony_ci} 258f6603c60Sopenharmony_ci 259f6603c60Sopenharmony_ci/** 260f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_TIME_API_GETTIMEOFDAY_0100 261f6603c60Sopenharmony_ci* @tc.name test gettimeofday api 262f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 263f6603c60Sopenharmony_ci*/ 264f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testGettimeofday, Function | MediumTest | Level1) { 265f6603c60Sopenharmony_ci int sleepSec = 1; 266f6603c60Sopenharmony_ci struct timeval tvalStart = {0}; 267f6603c60Sopenharmony_ci struct timeval tvalEnd = {0}; 268f6603c60Sopenharmony_ci struct timezone tzone; 269f6603c60Sopenharmony_ci 270f6603c60Sopenharmony_ci int ret1 = gettimeofday(&tvalStart, &tzone); 271f6603c60Sopenharmony_ci sleep(sleepSec); 272f6603c60Sopenharmony_ci int ret2 = gettimeofday(&tvalEnd, &tzone); 273f6603c60Sopenharmony_ci EXPECT_EQ(0, ret1); 274f6603c60Sopenharmony_ci EXPECT_EQ(0, ret2); 275f6603c60Sopenharmony_ci EXPECT_TRUE((tvalEnd.tv_sec - tvalStart.tv_sec) >= sleepSec) 276f6603c60Sopenharmony_ci << "check end-start>=1 fail, start[" << tvalStart.tv_sec << "],end[" << tvalEnd.tv_sec << "]"; 277f6603c60Sopenharmony_ci EXPECT_TRUE((tvalEnd.tv_sec - tvalStart.tv_sec) < sleepSec+1) 278f6603c60Sopenharmony_ci << "check end-start<2 fail, start[" << tvalStart.tv_sec << "],end[" << tvalEnd.tv_sec << "]"; 279f6603c60Sopenharmony_ci} 280f6603c60Sopenharmony_ci 281f6603c60Sopenharmony_ci/** 282f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_TIME_API_SETTIMEOFDAY_0100 283f6603c60Sopenharmony_ci* @tc.name test settimeofday api 284f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 285f6603c60Sopenharmony_ci*/ 286f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testSettimeofday, Function | MediumTest | Level1) { 287f6603c60Sopenharmony_ci int setSec = 100; 288f6603c60Sopenharmony_ci int sleepSec = 2; 289f6603c60Sopenharmony_ci struct timeval tvalStart = {0}; 290f6603c60Sopenharmony_ci struct timeval tvalEnd = {0}; 291f6603c60Sopenharmony_ci struct timeval set = {.tv_sec = setSec, .tv_usec = 0}; 292f6603c60Sopenharmony_ci 293f6603c60Sopenharmony_ci int ret1 = settimeofday(&set, NULL); 294f6603c60Sopenharmony_ci int ret2 = gettimeofday(&tvalStart, NULL); 295f6603c60Sopenharmony_ci sleep(sleepSec); 296f6603c60Sopenharmony_ci int ret3 = gettimeofday(&tvalEnd, NULL); 297f6603c60Sopenharmony_ci EXPECT_EQ(0, ret1); 298f6603c60Sopenharmony_ci EXPECT_EQ(0, ret2); 299f6603c60Sopenharmony_ci EXPECT_EQ(0, ret3); 300f6603c60Sopenharmony_ci EXPECT_EQ(setSec, tvalStart.tv_sec) 301f6603c60Sopenharmony_ci << "settimeofday set[" << setSec << "],get[" << tvalStart.tv_sec << "]"; 302f6603c60Sopenharmony_ci EXPECT_TRUE((tvalEnd.tv_sec - tvalStart.tv_sec) >= sleepSec) 303f6603c60Sopenharmony_ci << "check end-start>=2 fail, start[" << tvalStart.tv_sec << "],end[" << tvalEnd.tv_sec << "]"; 304f6603c60Sopenharmony_ci EXPECT_TRUE((tvalEnd.tv_sec - tvalStart.tv_sec) < sleepSec+1) 305f6603c60Sopenharmony_ci << "check end-start<3 fail, start[" << tvalStart.tv_sec << "],end[" << tvalEnd.tv_sec << "]"; 306f6603c60Sopenharmony_ci} 307f6603c60Sopenharmony_ci 308f6603c60Sopenharmony_ci/** 309f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_TIME_API_LOCALTIME_0100 310f6603c60Sopenharmony_ci* @tc.name test localtime api 311f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 312f6603c60Sopenharmony_ci*/ 313f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testLocaltime, Function | MediumTest | Level1) { 314f6603c60Sopenharmony_ci char cTime[32]; 315f6603c60Sopenharmony_ci time_t tStart; 316f6603c60Sopenharmony_ci time_t tEnd; 317f6603c60Sopenharmony_ci struct timeval tSet = {.tv_sec = 86399, .tv_usec = 0}; 318f6603c60Sopenharmony_ci 319f6603c60Sopenharmony_ci int ret = settimeofday(&tSet, NULL); 320f6603c60Sopenharmony_ci time(&tStart); 321f6603c60Sopenharmony_ci sleep(2); 322f6603c60Sopenharmony_ci time(&tEnd); 323f6603c60Sopenharmony_ci EXPECT_EQ(0, ret); 324f6603c60Sopenharmony_ci 325f6603c60Sopenharmony_ci struct tm *tmStart = localtime(&tStart); 326f6603c60Sopenharmony_ci ASSERT_NE(nullptr, tmStart); 327f6603c60Sopenharmony_ci strftime(cTime, sizeof(cTime), "%H:%M:%S", tmStart); 328f6603c60Sopenharmony_ci EXPECT_STREQ("23:59:59", cTime); 329f6603c60Sopenharmony_ci struct tm *tmEnd = localtime(&tEnd); 330f6603c60Sopenharmony_ci ASSERT_NE(nullptr, tmEnd); 331f6603c60Sopenharmony_ci strftime(cTime, sizeof(cTime), "%H:%M:%S", tmEnd); 332f6603c60Sopenharmony_ci EXPECT_STREQ("00:00:01", cTime); 333f6603c60Sopenharmony_ci} 334f6603c60Sopenharmony_ci 335f6603c60Sopenharmony_ci/** 336f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_TIME_API_LOCALTIMER_0100 337f6603c60Sopenharmony_ci* @tc.name test localtime_r api 338f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 339f6603c60Sopenharmony_ci*/ 340f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testLocaltimer, Function | MediumTest | Level1) { 341f6603c60Sopenharmony_ci char cTime[32]; 342f6603c60Sopenharmony_ci time_t tStart; 343f6603c60Sopenharmony_ci time_t tEnd; 344f6603c60Sopenharmony_ci struct tm tmrStart = {0}; 345f6603c60Sopenharmony_ci struct tm tmrEnd = {0}; 346f6603c60Sopenharmony_ci 347f6603c60Sopenharmony_ci struct timeval tSet = {.tv_sec = 86399, .tv_usec = 0}; 348f6603c60Sopenharmony_ci int ret = settimeofday(&tSet, NULL); 349f6603c60Sopenharmony_ci time(&tStart); 350f6603c60Sopenharmony_ci sleep(1); 351f6603c60Sopenharmony_ci time(&tEnd); 352f6603c60Sopenharmony_ci struct tm *tmrStartPtr = localtime_r(&tStart, &tmrStart); 353f6603c60Sopenharmony_ci ASSERT_NE(nullptr, tmrStartPtr); 354f6603c60Sopenharmony_ci struct tm *tmrEndPtr = localtime_r(&tEnd, &tmrEnd); 355f6603c60Sopenharmony_ci ASSERT_NE(nullptr, tmrEndPtr); 356f6603c60Sopenharmony_ci 357f6603c60Sopenharmony_ci EXPECT_EQ(0, ret); 358f6603c60Sopenharmony_ci strftime(cTime, sizeof(cTime), "%H:%M:%S", &tmrStart); 359f6603c60Sopenharmony_ci EXPECT_STREQ("23:59:59", cTime); 360f6603c60Sopenharmony_ci strftime(cTime, sizeof(cTime), "%H:%M:%S", tmrStartPtr); 361f6603c60Sopenharmony_ci EXPECT_STREQ("23:59:59", cTime); 362f6603c60Sopenharmony_ci strftime(cTime, sizeof(cTime), "%H:%M:%S", &tmrEnd); 363f6603c60Sopenharmony_ci EXPECT_STREQ("00:00:00", cTime); 364f6603c60Sopenharmony_ci strftime(cTime, sizeof(cTime), "%H:%M:%S", tmrEndPtr); 365f6603c60Sopenharmony_ci EXPECT_STREQ("00:00:00", cTime); 366f6603c60Sopenharmony_ci strftime(cTime, sizeof(cTime), "%F %T", &tmrStart); 367f6603c60Sopenharmony_ci LOG(" result[%s]", cTime); 368f6603c60Sopenharmony_ci} 369f6603c60Sopenharmony_ci 370f6603c60Sopenharmony_ci/** 371f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_TIME_API_GETDATE_0100 372f6603c60Sopenharmony_ci* @tc.name test getdate api 373f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 374f6603c60Sopenharmony_ci*/ 375f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testGetdateBasic, Function | MediumTest | Level1) { 376f6603c60Sopenharmony_ci // set DATEMSK env 377f6603c60Sopenharmony_ci FILE *fp = nullptr; 378f6603c60Sopenharmony_ci char mask[20] = "%Y-%m-%d %H:%M:%S"; 379f6603c60Sopenharmony_ci fp = fopen(DATEMSK_FILE, "w+"); 380f6603c60Sopenharmony_ci ASSERT_NE(nullptr, fp); 381f6603c60Sopenharmony_ci int ret = fwrite(mask, sizeof(mask), 1, fp); 382f6603c60Sopenharmony_ci EXPECT_TRUE(ret > 0); 383f6603c60Sopenharmony_ci ret = setenv("DATEMSK", DATEMSK_FILE, 1); 384f6603c60Sopenharmony_ci EXPECT_EQ(0, ret); 385f6603c60Sopenharmony_ci ret = fclose(fp); 386f6603c60Sopenharmony_ci EXPECT_NE(-1, ret); 387f6603c60Sopenharmony_ci 388f6603c60Sopenharmony_ci // test getdate 389f6603c60Sopenharmony_ci char cTime[30]; 390f6603c60Sopenharmony_ci struct tm *retTm = nullptr; 391f6603c60Sopenharmony_ci const char *cInput = "2020-10-26 00:01:01"; 392f6603c60Sopenharmony_ci retTm = getdate(cInput); 393f6603c60Sopenharmony_ci ASSERT_NE(nullptr, retTm) << " getdate fail errno:" << getdate_err; 394f6603c60Sopenharmony_ci strftime(cTime, sizeof(cTime), mask, retTm); 395f6603c60Sopenharmony_ci EXPECT_STREQ(cInput, cTime); 396f6603c60Sopenharmony_ci strftime(cTime, sizeof(cTime), "%D %A %H:%M:%S", retTm); 397f6603c60Sopenharmony_ci EXPECT_STREQ("10/26/20 Sunday 00:01:01", cTime); 398f6603c60Sopenharmony_ci 399f6603c60Sopenharmony_ci // restore 400f6603c60Sopenharmony_ci ret = remove(DATEMSK_FILE); 401f6603c60Sopenharmony_ci EXPECT_EQ(0, ret); 402f6603c60Sopenharmony_ci ret = unsetenv("DATEMSK"); 403f6603c60Sopenharmony_ci EXPECT_EQ(0, ret); 404f6603c60Sopenharmony_ci} 405f6603c60Sopenharmony_ci 406f6603c60Sopenharmony_ci/** 407f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_TIME_API_GETDATE_0200 408f6603c60Sopenharmony_ci* @tc.name getdate error test 409f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 410f6603c60Sopenharmony_ci*/ 411f6603c60Sopenharmony_ciHWTEST_F(ClockTimeTest, testGetdateError, Function | MediumTest | Level2) { 412f6603c60Sopenharmony_ci // no env 413f6603c60Sopenharmony_ci struct tm *retTm = nullptr; 414f6603c60Sopenharmony_ci const char *cInput = "2020-10-26 00:01:01"; 415f6603c60Sopenharmony_ci retTm = getdate(cInput); 416f6603c60Sopenharmony_ci EXPECT_EQ(nullptr, retTm); 417f6603c60Sopenharmony_ci EXPECT_EQ(1, getdate_err); 418f6603c60Sopenharmony_ci 419f6603c60Sopenharmony_ci // set env, but file not exist 420f6603c60Sopenharmony_ci int ret = setenv("DATEMSK", DATEMSK_FILE, 1); 421f6603c60Sopenharmony_ci EXPECT_EQ(0, ret); 422f6603c60Sopenharmony_ci retTm = getdate(cInput); 423f6603c60Sopenharmony_ci EXPECT_EQ(nullptr, retTm); 424f6603c60Sopenharmony_ci EXPECT_EQ(2, getdate_err); 425f6603c60Sopenharmony_ci 426f6603c60Sopenharmony_ci // creat file and set env 427f6603c60Sopenharmony_ci FILE *fp = nullptr; 428f6603c60Sopenharmony_ci char mask[10] = "%H:%M:%S"; 429f6603c60Sopenharmony_ci fp = fopen(DATEMSK_FILE, "w+"); 430f6603c60Sopenharmony_ci ASSERT_NE(nullptr, fp); 431f6603c60Sopenharmony_ci ret = fwrite(mask, sizeof(mask), 1, fp); 432f6603c60Sopenharmony_ci EXPECT_TRUE(ret > 0); 433f6603c60Sopenharmony_ci ret = fclose(fp); 434f6603c60Sopenharmony_ci EXPECT_NE(-1, ret); 435f6603c60Sopenharmony_ci 436f6603c60Sopenharmony_ci // test getdate 437f6603c60Sopenharmony_ci retTm = getdate("10/26/20 00:01:01"); 438f6603c60Sopenharmony_ci EXPECT_EQ(nullptr, retTm); 439f6603c60Sopenharmony_ci EXPECT_EQ(7, getdate_err); 440f6603c60Sopenharmony_ci 441f6603c60Sopenharmony_ci // restore 442f6603c60Sopenharmony_ci ret = remove(DATEMSK_FILE); 443f6603c60Sopenharmony_ci EXPECT_EQ(0, ret); 444f6603c60Sopenharmony_ci ret = unsetenv("DATEMSK"); 445f6603c60Sopenharmony_ci EXPECT_EQ(0, ret); 446f6603c60Sopenharmony_ci} 447