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 static const char *ALL_CLOCKS_NAME[CLOCK_TAI + 1] = {
34 "CLOCK_REALTIME",
35 "CLOCK_MONOTONIC",
36 "CLOCK_PROCESS_CPUTIME_ID",
37 "CLOCK_THREAD_CPUTIME_ID",
38 "CLOCK_MONOTONIC_RAW",
39 "CLOCK_REALTIME_COARSE",
40 "CLOCK_MONOTONIC_COARSE",
41 "CLOCK_BOOTTIME",
42 "CLOCK_REALTIME_ALARM",
43 "CLOCK_BOOTTIME_ALARM",
44 "CLOCK_SGI_CYCLE",
45 "CLOCK_TAI",
46 };
47
48 LITE_TEST_SUIT(TIME, TimeSleepTest, TimeSleepTestSuite);
49
TimeSleepTestSuiteSetUp(void)50 static BOOL TimeSleepTestSuiteSetUp(void)
51 {
52 return TRUE;
53 }
54
TimeSleepTestSuiteTearDown(void)55 static BOOL TimeSleepTestSuiteTearDown(void)
56 {
57 return TRUE;
58 }
59
60 /**
61 * @tc.number SUB_KERNEL_TIME_API_CLOCK_NANOSLEEP_0200
62 * @tc.name clock_nanosleep fail test - non-support clock_id
63 * @tc.desc [C- SOFTWARE -0200]
64 */
65 LITE_TEST_CASE(TimeSleepTestSuite, testClockNanosleepInvalidID, Reliability | SmallTest | Level2)
66 {
67 clockid_t cid = 1; /* 1, common data for test, no special meaning */
68 const char *cname = ALL_CLOCKS_NAME[cid];
69 struct timespec req = {0, 100}; /* 100, common data for test, no special meaning */
70 struct timespec rem = {0};
71 int rt = clock_nanosleep(cid, 0, &req, &rem);
72 if (cid == CLOCK_SGI_CYCLE) {
73 ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
74 }
75 return 0;
76 }
77
78 /**
79 * @tc.number SUB_KERNEL_TIME_API_CLOCK_NANOSLEEP_0300
80 * @tc.name clock_nanosleep fail test - invalid parameter
81 * @tc.desc [C- SOFTWARE -0200]
82 */
83 LITE_TEST_CASE(TimeSleepTestSuite, testClockNanosleepInvalidPara, Reliability | SmallTest | Level2)
84 {
85 struct timespec req = {0, 100}; /* 100, common data for test, no special meaning */
86 struct timespec rem = {0};
87 int rt;
88
89 int id = GetRandom(1000) + 12; /* 1000, 12, common data for test, no special meaning */
90 rt = clock_nanosleep(id, 0, &req, &rem);
91 ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
92
93 id = -GetRandom(1000) - 12; /* 1000, 12, common data for test, no special meaning */
94 rt = clock_nanosleep(id, 0, &req, &rem);
95 ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
96
97 int flag = TIMER_ABSTIME;
98 rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem);
99 ICUNIT_ASSERT_EQUAL(rt, ENOTSUP, rt);
100 flag = GetRandom(100) + 1; /* 100, 1, common data for test, no special meaning */
101 rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem);
102 ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
103 flag = -GetRandom(100) - 1; /* 100, 1, common data for test, no special meaning */
104 rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem);
105 ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
106
107 req.tv_sec = -1; /* -1, common data for test, no special meaning */
108 req.tv_nsec = 1; /* 1, common data for test, no special meaning */
109 rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
110 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
111 req.tv_sec = 1; /* 1, common data for test, no special meaning */
112 req.tv_nsec = -1; /* -1, common data for test, no special meaning */
113 rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
114 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
115 req.tv_sec = 1; /* 1, common data for test, no special meaning */
116 req.tv_nsec = 1000 * 1000 * 1000 + 1; /* 1000, 1, common data for test, no special meaning */
117 rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
118 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
119 return 0;
120 }
121
122 RUN_TEST_SUITE(TimeSleepTestSuite);
123
SleepTest(void)124 void SleepTest(void)
125 {
126 RUN_ONE_TESTCASE(testClockNanosleepInvalidID);
127 RUN_ONE_TESTCASE(testClockNanosleepInvalidPara);
128 }
129