refactor: 内核测试套整改,简化分类

close: #I4OX3O
Signed-off-by: arvinzzz <zhaotianyu9@huawei.com>

Change-Id: I3ba65509135cee2ae3af82fec923a01e00ffdbe8
This commit is contained in:
arvinzzz
2022-01-14 11:50:31 +08:00
parent d6dc0642c5
commit a1ae2ecc13
2402 changed files with 225539 additions and 1054 deletions

View File

@@ -0,0 +1,46 @@
# Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
# to endorse or promote products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import("//kernel/liteos_a/testsuites/unittest/config.gni")
time_timer_include_dirs = [ "$TEST_UNITTEST_DIR/libc/time/timer" ]
time_timer_sources_entry =
[ "$TEST_UNITTEST_DIR/libc/time/timer/time_timer_test.cpp" ]
time_timer_sources_smoke = [
"$TEST_UNITTEST_DIR/libc/time/timer/smoke/timer_test_001.cpp",
"$TEST_UNITTEST_DIR/libc/time/timer/smoke/timer_test_002.cpp",
"$TEST_UNITTEST_DIR/libc/time/timer/smoke/timer_test_003.cpp",
"$TEST_UNITTEST_DIR/libc/time/timer/smoke/timer_test_004.cpp",
"$TEST_UNITTEST_DIR/libc/time/timer/smoke/timer_test_005.cpp",
"$TEST_UNITTEST_DIR/libc/time/timer/smoke/timer_test_tzset_001.cpp",
"$TEST_UNITTEST_DIR/libc/time/timer/smoke/timer_test_tzset_002.cpp",
]
time_timer_sources_full = []

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TIME_TIMER_LT_TIMER_TEST_H
#define TIME_TIMER_LT_TIMER_TEST_H
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include "osTest.h"
void TimerTest001(void);
void TimerTest002(void);
void TimerTest003(void);
void TimerTest004(void);
void TimerTest005(void);
void TIME_TEST_TZSET_001(void);
void TIME_TEST_TZSET_002(void);
#endif /* TIME_TIMER_LT_TIMER_TEST_H */

View File

@@ -0,0 +1,160 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <osTest.h>
#include "lt_timer_test.h"
#define SIG SIGALRM
#define CLOCKID CLOCK_REALTIME
static int g_sigHdlCnt;
static int g_overRunCnt;
static timer_t g_timerID;
static void SigHandler01(int sig)
{
g_sigHdlCnt++;
g_overRunCnt += timer_getoverrun(g_timerID);
LogPrintln("signo %d g_sigHdlCnt %d, overrun %d,%d\n", sig, g_sigHdlCnt, g_overRunCnt, timer_getoverrun(g_timerID));
}
static int TimerTest(void)
{
int interval = 3; // 3, seconds
timer_t timerid01, timerid02;
struct sigevent sev;
struct itimerspec its;
sigset_t mask;
struct sigaction sa;
int ret;
sa.sa_flags = 0;
sa.sa_handler = SigHandler01;
sigemptyset(&sa.sa_mask);
ret = sigaction(SIG, &sa, nullptr);
LogPrintln("sigaction %d: %d", SIG, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Block timer signal */
sigemptyset(&mask);
sigaddset(&mask, SIG);
ret = sigprocmask(SIG_BLOCK, &mask, nullptr);
LogPrintln("sigprocmask setmask %d: %d", SIG, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Create the timer */
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid01;
ret = timer_create(CLOCKID, &sev, &timerid01);
LogPrintln("timer_create %p: %d", timerid01, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_timerID = timerid01;
/* Start the timer */
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 900000000; // 900000000, 0.9s
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
ret = timer_settime(timerid01, 0, &its, nullptr);
LogPrintln("timer_settime %p: %d", timerid01, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Test of evp is NULL */
ret = timer_create(CLOCKID, NULL, &timerid02);
LogPrintln("timer_settime %p: %d", timerid02, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
its.it_value.tv_sec = 1;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
ret = timer_settime(timerid02, 0, &its, nullptr);
LogPrintln("timer_settime %p: %d", timerid02, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
sleep(6);
/* Sleep for a while */
LogPrintln("sleep %ds", interval);
sleep(interval); // timer signal is blocked, this sleep should not be interrupted
ICUNIT_ASSERT_EQUAL(g_sigHdlCnt, 0, g_sigHdlCnt);
/* Get the timer's time */
ret = timer_gettime(timerid01, &its);
LogPrintln("timer_gettime %p: %d", timerid01, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Get the timer's overruns */
ret = timer_getoverrun(timerid01);
LogPrintln("timer_getoverrun %p: %d", timerid01, ret);
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); // before timer deliver, return value of timer_getoverrun is unspecified
LogPrintln("unblock signal %d", SIG);
/* Unlock the timer signal */
ret = sigprocmask(SIG_UNBLOCK, &mask, nullptr);
LogPrintln("sigprocmask unblock %d: %d", SIG, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
interval = 1;
LogPrintln("sleep another %ds", interval);
sleep(interval); // this sleep may be interrupted by the timer
LogPrintln("sleep time over, g_sigHdlCnt = %d", g_sigHdlCnt);
LogPrintln("sleep another %ds", interval);
sleep(interval); // this sleep may be interrupted by the timer
LogPrintln("sleep time over, g_sigHdlCnt = %d", g_sigHdlCnt);
ret = timer_delete(timerid01);
LogPrintln("timer_delete %p %d", timerid01, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = timer_delete(timerid02);
LogPrintln("timer_delete %p %d", timerid02, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_NOT_EQUAL(g_sigHdlCnt, 0, g_sigHdlCnt);
ICUNIT_ASSERT_NOT_EQUAL(g_overRunCnt, 0, g_overRunCnt);
return 0;
}
void TimerTest001(void)
{
TEST_ADD_CASE(__FUNCTION__, TimerTest, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "lt_timer_test.h"
static int g_almHandlerFlag;
static void SigAlmHandler(int sig)
{
g_almHandlerFlag++;
}
static int TimerTest(void)
{
struct itimerval itv;
int ret;
ret = getitimer(ITIMER_REAL, &itv);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
itv.it_value.tv_sec = 2; // 2, seconds.
itv.it_value.tv_usec = 500000; // 500000, 500ms.
itv.it_interval.tv_sec = 1;
itv.it_interval.tv_usec = 500000; // 500000, 500ms.
(void)signal(SIGALRM, SigAlmHandler);
ret = setitimer(ITIMER_REAL, &itv, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
LogPrintln("sleep %ds", 3); // 3, this sleep may be interrupted by the timer
sleep(3); // 3, this sleep may be interrupted by the timer
LogPrintln("sleep end, almHandlerFlag %d\n", g_almHandlerFlag);
ICUNIT_ASSERT_EQUAL(g_almHandlerFlag, 1, g_almHandlerFlag);
LogPrintln("sleep %ds", 2); // 2, this sleep may be interrupted by the timer
sleep(2); // 2, this sleep may be interrupted by the timer
LogPrintln("sleep end, almHandlerFlag %d\n", g_almHandlerFlag);
ICUNIT_ASSERT_EQUAL(g_almHandlerFlag, 2, g_almHandlerFlag); // 2, assert the g_almHandlerFlag.
LogPrintln("sleep %ds", 2); // 2, this sleep may be interrupted by the timer
sleep(2); // 2, this sleep may be interrupted by the timer
LogPrintln("sleep end, almHandlerFlag %d\n", g_almHandlerFlag);
ICUNIT_ASSERT_EQUAL(g_almHandlerFlag, 3, g_almHandlerFlag); // 3, assert the g_almHandlerFlag.
ret = getitimer(ITIMER_REAL, &itv);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(itv.it_interval.tv_sec, 1, itv.it_interval.tv_sec);
ICUNIT_ASSERT_EQUAL(itv.it_interval.tv_usec, 500000, itv.it_interval.tv_usec); // 500000, assert the tv_usec.
/* stop this timer */
itv.it_value.tv_sec = 0;
itv.it_value.tv_usec = 0;
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 0;
ret = setitimer(ITIMER_REAL, &itv, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return 0;
}
void TimerTest002(void)
{
TEST_ADD_CASE(__FUNCTION__, TimerTest, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <osTest.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <inttypes.h>
#include "lt_timer_test.h"
#define SIG SIGALRM
#define CLOCKID CLOCK_REALTIME
static int SetTimerTest(void)
{
int ret = 0;
int sig = 0;
int failed = 0;
timer_t timerid;
sigset_t set, oldSet;
struct sigevent sev;
ret = sigemptyset(&set);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sigaddset(&set, SIG);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sigprocmask(SIG_BLOCK, &set, &oldSet);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Create the timer */
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
ret = timer_create(CLOCKID, &sev, &timerid);
LogPrintln("timer_create %p: %d", timerid, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
struct timespec testcases[] = {
{0, 30000000},
{1, 0},
{1, 5},
{1, 5000},
{1, 30000000},
{2, 0},
}, zero = {0, 0};
for (int i = 0; i < sizeof(testcases) / sizeof(testcases[0]); ++i) {
struct timespec start, end;
struct itimerspec its;
int64_t expected, escaped;
its.it_interval = zero;
its.it_value = testcases[i];
ret = clock_gettime(CLOCKID, &start);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = timer_settime(timerid, 0, &its, nullptr);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sigwait(&set, &sig);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = clock_gettime(CLOCKID, &end);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
expected = its.it_value.tv_sec * (int64_t)1e9 + its.it_value.tv_nsec;
escaped = end.tv_sec * (int64_t)1e9 + end.tv_nsec - start.tv_sec * (int64_t)1e9 - start.tv_nsec;
failed += (escaped < expected || (escaped - expected) >= 20000000); // 20000000, 2 ticks.
}
ret = timer_delete(timerid);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sigprocmask(SIG_SETMASK, &oldSet, nullptr);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(failed, 0, failed);
return 0;
}
void TimerTest003(void)
{
TEST_ADD_CASE(__FUNCTION__, SetTimerTest, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <osTest.h>
#include "lt_timer_test.h"
/* signo should be in the range SIGRTMIN to SIGRTMAX when SA_SIGINFO flag is set. */
#define SIG SIGRTMIN
#define CLOCKID CLOCK_REALTIME
static int g_handlerFlag;
static int g_tmrOverrun;
static void SigHandler(int sig, siginfo_t *si, void *uc)
{
if (si == nullptr) {
LogPrintln("[ERROR]sig %d, si %p, uc %p\n", sig, si, uc);
return;
}
#ifdef TEST_ON_LINUX
timer_t timerid = *(timer_t *)si->si_value.sival_ptr;
#else // SA_SIGINFO not compatible with POSIX on HMOS
timer_t timerid = *(timer_t *)si;
#endif
g_tmrOverrun += timer_getoverrun(timerid);
LogPrintln("signo %d handlerFlag %d timer %p overrun %d\n", sig, ++g_handlerFlag, timerid, g_tmrOverrun);
}
static int SigInfoTimerTest(void)
{
int interval = 3; // 3 seconds
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
sigset_t mask;
struct sigaction sa;
int ret;
/* Install handler for timer signal. */
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = SigHandler;
sigemptyset(&sa.sa_mask);
ret = sigaction(SIG, &sa, nullptr);
LogPrintln("sigaction %d: %d", SIG, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Block timer signal */
sigemptyset(&mask);
sigaddset(&mask, SIG);
ret = sigprocmask(SIG_BLOCK, &mask, nullptr);
LogPrintln("sigprocmask setmask %d: %d", SIG, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Create the timer */
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
ret = timer_create(CLOCKID, &sev, &timerid);
LogPrintln("timer_create %p: %d", timerid, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Start the timer */
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 990000000; // 990000000, 0.99s
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
ret = timer_settime(timerid, 0, &its, nullptr);
LogPrintln("timer_settime %p: %d", timerid, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Sleep for a while */
LogPrintln("sleep %ds", interval);
sleep(interval); // should not be interrupted
/* Get the timer's time */
ret = timer_gettime(timerid, &its);
LogPrintln("timer_gettime %p: %d", timerid, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
/* Get the timer's overruns */
ret = timer_getoverrun(timerid);
LogPrintln("timer_getoverrun %p: %d", timerid, ret);
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
/* Unlock the timer signal */
ret = sigprocmask(SIG_UNBLOCK, &mask, nullptr);
LogPrintln("sigprocmask unblock %d: %d", SIG, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
LogPrintln("sleep another %ds", interval);
sleep(interval); // should be interrupted
LogPrintln("sleep time over, g_handlerFlag = %d", g_handlerFlag);
ret = timer_delete(timerid);
LogPrintln("timer_delete %p %d", timerid, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_NOT_EQUAL(g_handlerFlag, 0, g_handlerFlag);
ICUNIT_ASSERT_NOT_EQUAL(g_tmrOverrun, 0, g_tmrOverrun);
return 0;
}
void TimerTest004(void)
{
TEST_ADD_CASE(__FUNCTION__, SigInfoTimerTest, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include "osTest.h"
#include <stdio.h>
#include <string.h>
#include "lt_timer_test.h"
static int g_sigHdlCnt01;
static int g_sigHdlCnt02;
static int g_sigHdlCnt03;
static void TempSigHandler(union sigval v)
{
LogPrintln("This is TempSigHandler ...\r\n");
(*(void(*)(void))(v.sival_ptr))();
}
static void TempSigHandler01(void)
{
g_sigHdlCnt01++;
}
static void TempSigHandler02(void)
{
g_sigHdlCnt02++;
}
static int TimerTest(void)
{
timer_t timerid01, timerid02, timerid03;
struct sigevent sev;
struct itimerspec its;
int ret;
int i;
(void)memset(&sev, 0, sizeof(struct sigevent));
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = TempSigHandler;
sev.sigev_value.sival_ptr = (void *)TempSigHandler01;
/* Start the timer */
its.it_value.tv_sec = 3; // 3, timer time 3 seconds.
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
ret = timer_create(CLOCK_REALTIME, &sev, &timerid01);
LogPrintln("timer_settime %p: %d", timerid01, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = timer_settime(timerid01, 0, &its, nullptr);
LogPrintln("timer_create %p: %d", timerid01, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
its.it_value.tv_sec = 4; // 4, timer time 4 seconds.
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
sev.sigev_value.sival_ptr = (void *)TempSigHandler02;
ret = timer_create(CLOCK_REALTIME, &sev, &timerid02);
LogPrintln("timer_settime %p: %d", timerid02, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = timer_settime(timerid02, 0, &its, nullptr);
LogPrintln("timer_settime %p: %d", timerid02, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
its.it_value.tv_sec = 5; // 5, timer time 5 seconds.
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
sleep(20); // 20, sleep seconds for timer.
ret = timer_delete(timerid01);
LogPrintln("timer_delete %p %d", timerid01, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = timer_delete(timerid02);
LogPrintln("timer_delete %p %d", timerid02, ret);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_NOT_EQUAL(g_sigHdlCnt01, 0, g_sigHdlCnt01);
ICUNIT_ASSERT_NOT_EQUAL(g_sigHdlCnt02, 0, g_sigHdlCnt02);
return 0;
}
void TimerTest005(void)
{
TEST_ADD_CASE(__FUNCTION__, TimerTest, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <osTest.h>
#include "lt_timer_test.h"
#include <stdlib.h>
static UINT32 testcase(VOID)
{
time_t td;
int ret = 0;
errno = 0;
ret = putenv("TZ=GMT-3");
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
tzset();
errno = 0;
ret = time(&td);
TEST_PRINT("[INFO]%s:%d,%s,Current time = %s\n", __FILE__, __LINE__, __func__, asctime(localtime(&td)));
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
ICUNIT_GOTO_EQUAL(errno, 0, errno, OUT);
return LOS_OK;
OUT:
return LOS_NOK;
}
void TIME_TEST_TZSET_001(void)
{
TEST_ADD_CASE(__FUNCTION__, testcase, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <osTest.h>
#include "lt_timer_test.h"
#include <stdlib.h>
static UINT32 testcase(VOID)
{
time_t td;
int ret = 0;
errno = 0;
ret = putenv((char *) "TZ=GMT-100");
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
tzset();
errno = 0;
ret = time(&td);
TEST_PRINT("[INFO]%s:%d,%s,Current time = %s\n", __FILE__, __LINE__, __func__, asctime(localtime(&td)));
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
ICUNIT_GOTO_EQUAL(errno, 0, errno, OUT);
return LOS_OK;
OUT:
return LOS_NOK;
}
void TIME_TEST_TZSET_002(void)
{
TEST_ADD_CASE(__FUNCTION__, testcase, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdio.h"
#include <climits>
#include <gtest/gtest.h>
#include "lt_timer_test.h"
using namespace testing::ext;
namespace OHOS {
class TimeTimerTest : public testing::Test {
public:
static void SetUpTestCase(void) {}
static void TearDownTestCase(void) {}
};
#if defined(LOSCFG_USER_TEST_SMOKE)
/* *
* @tc.name: TimerTest001
* @tc.desc: function for TimeTimerTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(TimeTimerTest, TimerTest001, TestSize.Level0)
{
TimerTest001();
}
/* *
* @tc.name: TimerTest002
* @tc.desc: function for TimeTimerTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(TimeTimerTest, TimerTest002, TestSize.Level0)
{
TimerTest002();
}
/* *
* @tc.name: TimerTest003
* @tc.desc: function for TimeTimerTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(TimeTimerTest, TimerTest003, TestSize.Level0)
{
TimerTest003();
}
/* *
* @tc.name: TimerTest004
* @tc.desc: function for TimeTimerTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
/*HWTEST_F(TimeTimerTest, TimerTest004, TestSize.Level0)
{
TimerTest004(); // TODO: musl sigaction handler have only one param.
}*/
/* *
* @tc.name: TimerTest005
* @tc.desc: function for timer_create SIGEV_THREAD.
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(TimeTimerTest, TimerTest005, TestSize.Level0)
{
TimerTest005();
}
/* *
* @tc.name: TIME_TEST_TZSET_001
* @tc.desc: function for TIME_TEST_TZSET_001
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(TimeTimerTest, TIME_TEST_TZSET_001, TestSize.Level0)
{
TIME_TEST_TZSET_001();
}
/* *
* @tc.name: TIME_TEST_TZSET_002
* @tc.desc: function for TimeTimerTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(TimeTimerTest, TIME_TEST_TZSET_002, TestSize.Level0)
{
TIME_TEST_TZSET_002();
}
#endif
} // namespace OHOS