1 /*
2 * Copyright (c) 2021-2025 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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <csignal>
20 #include <string>
21 #include <vector>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <gtest/gtest.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <syscall.h>
28 #include "securec.h"
29
30 using namespace testing::ext;
31
32 enum SyslogActionType {
33 SYSLOG_ACTION_CLOSE,
34 SYSLOG_ACTION_OPEN,
35 SYSLOG_ACTION_READ,
36 SYSLOG_ACTION_READ_ALL,
37 SYSLOG_ACTION_READ_CLEAR,
38 SYSLOG_ACTION_CLEAR,
39 SYSLOG_ACTION_CONSOLE_OFF,
40 SYSLOG_ACTION_CONSOLE_ON,
41 SYSLOG_ACTION_CONSOLE_LEVEL,
42 SYSLOG_ACTION_SIZE_UNREAD,
43 SYSLOG_ACTION_SIZE_BUFFER,
44 };
45
46 class HatsSyslogTest : public testing::Test {
47 public:
48 static void SetUpTestCase();
49 static void TearDownTestCase();
50 void SetUp();
51 void TearDown();
52 private:
53 };
SetUp()54 void HatsSyslogTest::SetUp()
55 {
56 }
57
TearDown()58 void HatsSyslogTest::TearDown()
59 {
60 }
61
SetUpTestCase()62 void HatsSyslogTest::SetUpTestCase()
63 {
64 }
65
TearDownTestCase()66 void HatsSyslogTest::TearDownTestCase()
67 {
68 }
69
70 /*
71 * @tc.number : SUB_KERNEL_SYSCALL_SYSLOG_0100
72 * @tc.name : SyslogSuccess_0001
73 * @tc.desc : syslog open and close the log success.
74 * @tc.size : MediumTest
75 * @tc.type : Function
76 * @tc.level : Level 2
77 */
78 HWTEST_F(HatsSyslogTest, SyslogSuccess_0001, Function | MediumTest | Level2)
79 {
80 int ret = syscall(__NR_syslog, SYSLOG_ACTION_OPEN, nullptr, 0);
81 EXPECT_EQ(ret, 0);
82
83 ret = syscall(__NR_syslog, SYSLOG_ACTION_CLOSE, nullptr, 0);
84 EXPECT_EQ(ret, 0);
85 }
86
87 /*
88 * @tc.number : SUB_KERNEL_SYSCALL_SYSLOG_0200
89 * @tc.name : SyslogSuccess_0002
90 * @tc.desc : syslog size buffer the log success.
91 * @tc.size : MediumTest
92 * @tc.type : Function
93 * @tc.level : Level 1
94 */
95 HWTEST_F(HatsSyslogTest, SyslogSuccess_0002, Function | MediumTest | Level1)
96 {
97 int ret = syscall(__NR_syslog, SYSLOG_ACTION_SIZE_BUFFER, nullptr, 0);
98 EXPECT_GE(ret, 0);
99 }
100
101 /*
102 * @tc.number : SUB_KERNEL_SYSCALL_SYSLOG_0300
103 * @tc.name : SyslogSuccess_0003
104 * @tc.desc : syslog clear and readall the log success.
105 * @tc.size : MediumTest
106 * @tc.type : Function
107 * @tc.level : Level 1
108 */
109 HWTEST_F(HatsSyslogTest, SyslogSuccess_0003, Function | MediumTest | Level1)
110 {
111 char buffer[100];
112
113 int ret = syscall(__NR_syslog, SYSLOG_ACTION_CLEAR, nullptr, 0);
114 EXPECT_EQ(ret, 0);
115
116 ret = syscall(__NR_syslog, SYSLOG_ACTION_READ_ALL, buffer, sizeof(buffer));
117 EXPECT_GE(ret, strlen(buffer));
118 }
119
120 /*
121 * @tc.number : SUB_KERNEL_SYSCALL_SYSLOG_0400
122 * @tc.name : SyslogSuccess_0004
123 * @tc.desc : syslog clear and readclear the log success.
124 * @tc.size : MediumTest
125 * @tc.type : Function
126 * @tc.level : Level 1
127 */
128 HWTEST_F(HatsSyslogTest, SyslogSuccess_0004, Function | MediumTest | Level1)
129 {
130 char buffer[100];
131
132 int ret = syscall(__NR_syslog, SYSLOG_ACTION_CLEAR, nullptr, 0);
133 EXPECT_EQ(ret, 0);
134
135 ret = syscall(__NR_syslog, SYSLOG_ACTION_READ_CLEAR, buffer, sizeof(buffer));
136 EXPECT_GE(ret, strlen(buffer));
137 }
138
139 /*
140 * @tc.number : SUB_KERNEL_SYSCALL_SYSLOG_0500
141 * @tc.name : SyslogSuccess_0005
142 * @tc.desc : syslog console on and off the log success.
143 * @tc.size : MediumTest
144 * @tc.type : Function
145 * @tc.level : Level 2
146 */
147 HWTEST_F(HatsSyslogTest, SyslogSuccess_0005, Function | MediumTest | Level2)
148 {
149 int ret = syscall(__NR_syslog, SYSLOG_ACTION_CONSOLE_ON, nullptr, 0);
150 EXPECT_EQ(ret, 0);
151
152 ret = syscall(__NR_syslog, SYSLOG_ACTION_CONSOLE_OFF, nullptr, 0);
153 EXPECT_EQ(ret, 0);
154 }
155
156 /*
157 * @tc.number : SUB_KERNEL_SYSCALL_SYSLOG_0600
158 * @tc.name : SyslogSuccess_0006
159 * @tc.desc : syslog level success.
160 * @tc.size : MediumTest
161 * @tc.type : Function
162 * @tc.level : Level 1
163 */
164 HWTEST_F(HatsSyslogTest, SyslogSuccess_0006, Function | MediumTest | Level1)
165 {
166 int ret;
167 int i;
168
169 for (i = 1; i < 9; i++) {
170 ret = syscall(__NR_syslog, SYSLOG_ACTION_CONSOLE_LEVEL, nullptr, i);
171 EXPECT_EQ(ret, 0);
172 }
173 }
174
175 /*
176 * @tc.number : SUB_KERNEL_SYSCALL_SYSLOG_0700
177 * @tc.name : SyslogSuccess_0007
178 * @tc.desc : syslog unread the log success.
179 * @tc.size : MediumTest
180 * @tc.type : Function
181 * @tc.level : Level 1
182 */
183 HWTEST_F(HatsSyslogTest, SyslogSuccess_0007, Function | MediumTest | Level1)
184 {
185 int ret = syscall(__NR_syslog, SYSLOG_ACTION_CLEAR, nullptr, 0);
186 EXPECT_EQ(ret, 0);
187
188 errno = 0;
189 ret = syscall(__NR_syslog, SYSLOG_ACTION_SIZE_UNREAD, nullptr, 0);
190 if (ret != -1) {
191 EXPECT_GE(ret, 0);
192 } else {
193 EXPECT_EQ(errno, 38);
194 }
195 }