/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include "log.h" #include "utils.h" #include "ClockID.h" using namespace testing::ext; const char* DATEMSK_FILE = "/storage/getdate_mask"; // Resolution of: CLOCK_MONOTONIC, CLOCK_REALTIME, CLOCK_MONOTONIC_RAW: 1 us const int CLOCK_RESOLUTION_HIGH = 1000; // Resolution of: CLOCK_MONOTONIC_COARSE, CLOCK_REALTIME_COARSE: 1 tick = 10 ms const int CLOCK_RESOLUTION_LOW = 1000 * 1000; class ClockTimeTest : public testing::Test { protected: static struct timespec mTestStatTime; // SetUpTestCase: Testsuit setup, run before 1st testcase static void SetUpTestCase(void) { clock_gettime(CLOCK_REALTIME, &mTestStatTime); LOG("test start at %ld\n", (long)mTestStatTime.tv_sec); } // TearDownTestCase: Testsuit teardown, run after last testcase static void TearDownTestCase(void) { struct timespec time1 = {0, 0}; clock_gettime(CLOCK_REALTIME, &time1); LOG("test end at %ld", (long)time1.tv_sec); mTestStatTime.tv_sec += 10; // approximate total test time if (!clock_settime(CLOCK_REALTIME, &mTestStatTime)) { LOG("restore time ok\n"); } else { LOG("restore time failed\n"); } } }; struct timespec ClockTimeTest::mTestStatTime; /** * @tc.number SUB_KERNEL_TIME_API_CLOCK_GETRES_0100 * @tc.name test all supported clockid of clock_getres * @tc.desc [C- SOFTWARE -0200] */ HWTEST_P(SupportedClockIDTest, testClockGetresAll, Function | MediumTest | Level1) { clockid_t cid = GetParam(); const char* cname = ALL_CLOCKS_NAME[cid]; struct timespec time1 = {0, 0}; int rt = clock_getres(cid, &time1); LOG("%s Resolution: %ld nanosecond\n", cname, time1.tv_nsec); EXPECT_EQ(rt, 0) << "clock_getres of " << cname << "failed, errno =" <= sleepSec) << "check end-start>=1 fail, start[" << tvalStart.tv_sec << "],end[" << tvalEnd.tv_sec << "]"; EXPECT_TRUE((tvalEnd.tv_sec - tvalStart.tv_sec) < sleepSec+1) << "check end-start<2 fail, start[" << tvalStart.tv_sec << "],end[" << tvalEnd.tv_sec << "]"; } /** * @tc.number SUB_KERNEL_TIME_API_SETTIMEOFDAY_0100 * @tc.name test settimeofday api * @tc.desc [C- SOFTWARE -0200] */ HWTEST_F(ClockTimeTest, testSettimeofday, Function | MediumTest | Level1) { int setSec = 100; int sleepSec = 2; struct timeval tvalStart = {0}; struct timeval tvalEnd = {0}; struct timeval set = {.tv_sec = setSec, .tv_usec = 0}; int ret1 = settimeofday(&set, NULL); int ret2 = gettimeofday(&tvalStart, NULL); sleep(sleepSec); int ret3 = gettimeofday(&tvalEnd, NULL); EXPECT_EQ(0, ret1); EXPECT_EQ(0, ret2); EXPECT_EQ(0, ret3); EXPECT_EQ(setSec, tvalStart.tv_sec) << "settimeofday set[" << setSec << "],get[" << tvalStart.tv_sec << "]"; EXPECT_TRUE((tvalEnd.tv_sec - tvalStart.tv_sec) >= sleepSec) << "check end-start>=2 fail, start[" << tvalStart.tv_sec << "],end[" << tvalEnd.tv_sec << "]"; EXPECT_TRUE((tvalEnd.tv_sec - tvalStart.tv_sec) < sleepSec+1) << "check end-start<3 fail, start[" << tvalStart.tv_sec << "],end[" << tvalEnd.tv_sec << "]"; } /** * @tc.number SUB_KERNEL_TIME_API_LOCALTIME_0100 * @tc.name test localtime api * @tc.desc [C- SOFTWARE -0200] */ HWTEST_F(ClockTimeTest, testLocaltime, Function | MediumTest | Level1) { char cTime[32]; time_t tStart; time_t tEnd; struct timeval tSet = {.tv_sec = 86399, .tv_usec = 0}; int ret = settimeofday(&tSet, NULL); time(&tStart); sleep(2); time(&tEnd); EXPECT_EQ(0, ret); struct tm *tmStart = localtime(&tStart); strftime(cTime, sizeof(cTime), "%H:%M:%S", tmStart); EXPECT_STREQ("23:59:59", cTime); struct tm *tmEnd = localtime(&tEnd); strftime(cTime, sizeof(cTime), "%H:%M:%S", tmEnd); EXPECT_STREQ("00:00:01", cTime); } /** * @tc.number SUB_KERNEL_TIME_API_LOCALTIMER_0100 * @tc.name test localtime_r api * @tc.desc [C- SOFTWARE -0200] */ HWTEST_F(ClockTimeTest, testLocaltimer, Function | MediumTest | Level1) { char cTime[32]; time_t tStart; time_t tEnd; struct tm tmrStart = {0}; struct tm tmrEnd = {0}; struct timeval tSet = {.tv_sec = 86399, .tv_usec = 0}; int ret = settimeofday(&tSet, NULL); time(&tStart); sleep(1); time(&tEnd); struct tm *tmrStartPtr = localtime_r(&tStart, &tmrStart); struct tm *tmrEndPtr = localtime_r(&tEnd, &tmrEnd); EXPECT_EQ(0, ret); strftime(cTime, sizeof(cTime), "%H:%M:%S", &tmrStart); EXPECT_STREQ("23:59:59", cTime); strftime(cTime, sizeof(cTime), "%H:%M:%S", tmrStartPtr); EXPECT_STREQ("23:59:59", cTime); strftime(cTime, sizeof(cTime), "%H:%M:%S", &tmrEnd); EXPECT_STREQ("00:00:00", cTime); strftime(cTime, sizeof(cTime), "%H:%M:%S", tmrEndPtr); EXPECT_STREQ("00:00:00", cTime); strftime(cTime, sizeof(cTime), "%F %T", &tmrStart); LOG(" result[%s]", cTime); } /** * @tc.number SUB_KERNEL_TIME_API_GETDATE_0100 * @tc.name test getdate api * @tc.desc [C- SOFTWARE -0200] */ HWTEST_F(ClockTimeTest, testGetdateBasic, Function | MediumTest | Level1) { // set DATEMSK env FILE *fp = nullptr; char mask[20] = "%Y-%m-%d %H:%M:%S"; fp = fopen(DATEMSK_FILE, "w+"); ASSERT_NE(nullptr, fp); int ret = fwrite(mask, sizeof(mask), 1, fp); EXPECT_TRUE(ret > 0); ret = setenv("DATEMSK", DATEMSK_FILE, 1); EXPECT_EQ(0, ret); ret = fclose(fp); EXPECT_NE(-1, ret); // test getdate char cTime[30]; struct tm *retTm = nullptr; const char *cInput = "2020-10-26 00:01:01"; retTm = getdate(cInput); ASSERT_NE(nullptr, retTm) << " getdate fail errno:" << getdate_err; strftime(cTime, sizeof(cTime), mask, retTm); EXPECT_STREQ(cInput, cTime); strftime(cTime, sizeof(cTime), "%D %A %H:%M:%S", retTm); EXPECT_STREQ("10/26/20 Sunday 00:01:01", cTime); // restore ret = remove(DATEMSK_FILE); EXPECT_EQ(0, ret); ret = unsetenv("DATEMSK"); EXPECT_EQ(0, ret); } /** * @tc.number SUB_KERNEL_TIME_API_GETDATE_0200 * @tc.name getdate error test * @tc.desc [C- SOFTWARE -0200] */ HWTEST_F(ClockTimeTest, testGetdateError, Function | MediumTest | Level2) { // no env struct tm *retTm = nullptr; const char *cInput = "2020-10-26 00:01:01"; retTm = getdate(cInput); EXPECT_EQ(nullptr, retTm); EXPECT_EQ(1, getdate_err); // set env, but file not exist int ret = setenv("DATEMSK", DATEMSK_FILE, 1); EXPECT_EQ(0, ret); retTm = getdate(cInput); EXPECT_EQ(nullptr, retTm); EXPECT_EQ(2, getdate_err); // creat file and set env FILE *fp = nullptr; char mask[10] = "%H:%M:%S"; fp = fopen(DATEMSK_FILE, "w+"); ASSERT_NE(nullptr, fp); ret = fwrite(mask, sizeof(mask), 1, fp); EXPECT_TRUE(ret > 0); ret = fclose(fp); EXPECT_NE(-1, ret); // test getdate retTm = getdate("10/26/20 00:01:01"); EXPECT_EQ(nullptr, retTm); EXPECT_EQ(7, getdate_err); // restore ret = remove(DATEMSK_FILE); EXPECT_EQ(0, ret); ret = unsetenv("DATEMSK"); EXPECT_EQ(0, ret); }