• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "alarm_test.h"
32 
33 LITE_TEST_SUIT(TIME, TimeClockTimeTest, TimeClockTimeTestSuite);
34 
TimeClockTimeTestSuiteSetUp(void)35 static BOOL TimeClockTimeTestSuiteSetUp(void)
36 {
37     return TRUE;
38 }
39 
TimeClockTimeTestSuiteTearDown(void)40 static BOOL TimeClockTimeTestSuiteTearDown(void)
41 {
42     return TRUE;
43 }
44 
45 /**
46  * @tc.number SUB_KERNEL_TIME_API_CLOCK_GETTIME_0100
47  * @tc.name   test all supported clockid of clock_gettime
48  * @tc.desc   [C- SOFTWARE -0200]
49  */
50 LITE_TEST_CASE(TimeClockTimeTestSuite, testClockGettimeAll, Function | MediumTest | Level1)
51 {
52     clockid_t cid = CLOCK_REALTIME;
53     struct timespec time1 = {0, 0};
54     int rt = clock_gettime(cid, &time1);
55     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
56     return 0;
57 }
58 
59 /**
60  * @tc.number SUB_KERNEL_TIME_API_CLOCK_SETTIME_0100
61  * @tc.name   test clock_settime basic
62  * @tc.desc   [C- SOFTWARE -0200]
63  */
64 LITE_TEST_CASE(TimeClockTimeTestSuite, testClockSettime, Function | MediumTest | Level1)
65 {
66     struct timespec time1 = {0, 0};
67     sleep(1); /* 1, common data for test, no special meaning */
68     int rt = clock_gettime(CLOCK_REALTIME, &time1);
69     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
70     time_t sec = time1.tv_sec;
71     time1.tv_sec -= 1; /* 1, common data for test, no special meaning */
72     time1.tv_nsec = 1; /* 1, common data for test, no special meaning */
73     rt = clock_settime(CLOCK_REALTIME, &time1);
74     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
75     sleep(1); /* 1, common data for test, no special meaning */
76     rt = clock_gettime(CLOCK_REALTIME, &time1);
77     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
78     ICUNIT_ASSERT_EQUAL(time1.tv_sec, sec, time1.tv_sec);
79     return 0;
80 }
81 
82 /**
83 * @tc.number     SUB_KERNEL_TIME_API_GETTIMEOFDAY_0100
84 * @tc.name       test gettimeofday api
85 * @tc.desc       [C- SOFTWARE -0200]
86 */
87 LITE_TEST_CASE(TimeClockTimeTestSuite, testGettimeofday, Function | MediumTest | Level1)
88 {
89     int ret;
90     int sleepSec = 1; /* 1, common data for test, no special meaning */
91     struct timeval tValStart = {0};
92     struct timeval tValEnd = {0};
93     struct timezone tZone;
94 
95     int ret1 = gettimeofday(&tValStart, &tZone);
96     sleep(sleepSec);
97     int ret2 = gettimeofday(&tValEnd, &tZone);
98     ICUNIT_ASSERT_EQUAL(ret1, 0, ret1);
99     ICUNIT_ASSERT_EQUAL(ret2, 0, ret2);
100 
101     ret = (int)(tValEnd.tv_sec - tValStart.tv_sec);
102     ICUNIT_ASSERT_EQUAL(ret, sleepSec, ret);
103     ICUNIT_ASSERT_WITHIN_EQUAL(ret, sleepSec, INT_MAX, ret);
104     ret = (int)(tValEnd.tv_sec - tValStart.tv_sec);
105     ICUNIT_ASSERT_WITHIN_EQUAL(ret, INT_MIN, sleepSec + 1, ret); /* 1, common data for test, no special meaning */
106     return 0;
107 }
108 
109 /**
110 * @tc.number     SUB_KERNEL_TIME_API_SETTIMEOFDAY_0100
111 * @tc.name       test settimeofday api
112 * @tc.desc       [C- SOFTWARE -0200]
113 */
114 LITE_TEST_CASE(TimeClockTimeTestSuite, testSettimeofday, Function | MediumTest | Level1)
115 {
116     int ret;
117     int setSec = 100; /* 100, common data for test, no special meaning */
118     int sleepSec = 2; /* 2, common data for test, no special meaning */
119     struct timeval tValStart = {0};
120     struct timeval tValEnd = {0};
121     struct timeval set = {.tv_sec = setSec, .tv_usec = 0};
122 
123     int ret1 = settimeofday(&set, NULL);
124     int ret2 = gettimeofday(&tValStart, NULL);
125     sleep(sleepSec);
126     int ret3 = gettimeofday(&tValEnd, NULL);
127     ICUNIT_ASSERT_EQUAL(ret1, 0, ret1);
128     ICUNIT_ASSERT_EQUAL(ret2, 0, ret2);
129     ICUNIT_ASSERT_EQUAL(ret3, 0, ret3);
130     ICUNIT_ASSERT_EQUAL(tValStart.tv_sec, setSec, tValStart.tv_sec);
131 
132     ret = (int)(tValEnd.tv_sec - tValStart.tv_sec);
133     ICUNIT_ASSERT_EQUAL(ret, sleepSec, ret);
134     ICUNIT_ASSERT_WITHIN_EQUAL(ret, sleepSec, INT_MAX, ret);
135     ret = (int)(tValEnd.tv_sec - tValStart.tv_sec);
136     ICUNIT_ASSERT_WITHIN_EQUAL(ret, INT_MIN, sleepSec + 1, ret); /* 1, common data for test, no special meaning */
137     return 0;
138 }
139 
140 RUN_TEST_SUITE(TimeClockTimeTestSuite);
141 
ClockTimeTest(void)142 void ClockTimeTest(void)
143 {
144     RUN_ONE_TESTCASE(testClockGettimeAll);
145     RUN_ONE_TESTCASE(testClockSettime);
146     RUN_ONE_TESTCASE(testGettimeofday);
147     RUN_ONE_TESTCASE(testSettimeofday);
148 }
149