1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <errno.h>
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <sys/timex.h>
20 #include <time.h>
21 #include "functionalext.h"
22
exception_handler(int sig)23 void exception_handler(int sig)
24 {
25 exit(t_status);
26 }
27
28 extern int __clock_adjtime64(clockid_t, struct timex *);
29
30 /**
31 * @tc.name : clock_adjtime_0100
32 * @tc.desc : Verify that the kernel time can be adjusted (all parameters are valid, clock_id is CLOCK_REALTIME)
33 * @tc.level : Level 0
34 */
clock_adjtime_0100(void)35 void clock_adjtime_0100(void)
36 {
37 struct timex tx;
38 memset(&tx, 0, sizeof(tx));
39
40 int result = clock_adjtime(CLOCK_REALTIME, &tx);
41 EXPECT_NE("clock_adjtime_0100", result, -1);
42 }
43
44 /**
45 * @tc.name : clock_adjtime_0200
46 * @tc.desc : Verify that the kernel time can not be adjusted (each parameter is valid, clock_id is
47 * CLOCK_MONOTONIC)
48 * @tc.level : Level 2
49 */
clock_adjtime_0200(void)50 void clock_adjtime_0200(void)
51 {
52 struct timex tx = {ADJ_OFFSET_SS_READ};
53
54 int result = clock_adjtime(CLOCK_MONOTONIC, &tx);
55 EXPECT_EQ("clock_adjtime_0200", result, -1);
56 }
57
58 /**
59 * @tc.name : clock_adjtime_0300
60 * @tc.desc : Verify that the kernel time can not be adjusted (all parameters are valid, clock_id is
61 * CLOCK_PROCESS_CPUTIME_ID)
62 * @tc.level : Level 2
63 */
clock_adjtime_0300(void)64 void clock_adjtime_0300(void)
65 {
66 struct timex tx;
67 memset(&tx, 0, sizeof(tx));
68
69 int result = clock_adjtime(CLOCK_PROCESS_CPUTIME_ID, &tx);
70 EXPECT_EQ("clock_adjtime_0300", result, -1);
71 }
72
73 /**
74 * @tc.name : clock_adjtime_0400
75 * @tc.desc : Verify that the kernel time can not be adjusted (all parameters are valid, clock_id is
76 * CLOCK_THREAD_CPUTIME_ID)
77 * @tc.level : Level 2
78 */
clock_adjtime_0400(void)79 void clock_adjtime_0400(void)
80 {
81 struct timex tx = {0};
82 memset(&tx, 0, sizeof(tx));
83
84 int result = clock_adjtime(CLOCK_THREAD_CPUTIME_ID, &tx);
85 EXPECT_EQ("clock_adjtime_0400", result, -1);
86 }
87
88 /**
89 * @tc.name : clock_adjtime_0500
90 * @tc.desc : Verify that kernel time cannot be adjusted (tx parameter invalid)
91 * @tc.level : Level 2
92 */
clock_adjtime_0500(void)93 void clock_adjtime_0500(void)
94 {
95 signal(SIGSEGV, exception_handler);
96
97 errno = 0;
98 int result = clock_adjtime(CLOCK_REALTIME, NULL);
99 EXPECT_EQ("clock_adjtime_0500", result, -1);
100 EXPECT_EQ("clock_adjtime_0500", errno, EFAULT);
101 }
102
103 /**
104 * @tc.name : clock_adjtime64_0100
105 * @tc.desc : Verify that the kernel time can be adjusted (all parameters are valid, clock_id is CLOCK_REALTIME)
106 * @tc.level : Level 0
107 */
clock_adjtime64_0100(void)108 void clock_adjtime64_0100(void)
109 {
110 struct timex tx;
111 memset(&tx, 0, sizeof(tx));
112
113 int result = __clock_adjtime64(CLOCK_REALTIME, &tx);
114 EXPECT_NE("clock_adjtime64_0100", result, -1);
115 }
116
main(int argc,char * argv[])117 int main(int argc, char *argv[])
118 {
119 clock_adjtime_0100();
120 clock_adjtime_0200();
121 clock_adjtime_0300();
122 clock_adjtime_0400();
123 clock_adjtime_0500();
124 clock_adjtime64_0100();
125 return t_status;
126 }