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 <cstring>
17 #include <sys/utsname.h>
18
19 #include "gtest/gtest.h"
20
21 #include "../include/qos_interface.h"
22
23 namespace OHOS {
24 namespace FFRT_TEST {
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS::FFRT_TEST;
28 using namespace std;
29 constexpr unsigned int AF_QOS_ALL = 0x0003;
30
31 class QosInterfaceTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp();
36 void TearDown();
37 bool IsLinuxOs();
38 };
39
IsLinuxOs()40 bool QosInterfaceTest::IsLinuxOs()
41 {
42 struct utsname nameData;
43 uname(&nameData);
44 int cmpNum = 5;
45 return strncmp(nameData.sysname, "Linux", cmpNum) == 0 ? true : false;
46 }
47
SetUpTestCase()48 void QosInterfaceTest::SetUpTestCase()
49 {
50 }
51
TearDownTestCase()52 void QosInterfaceTest::TearDownTestCase()
53 {
54 }
55
SetUp()56 void QosInterfaceTest::SetUp()
57 {
58 }
59
TearDown()60 void QosInterfaceTest::TearDown()
61 {
62 }
63
64 extern "C" {
65 /**
66 * @tc.name: EnableRtgTest
67 * @tc.desc: Test whether the OnRemoteRequest interface are normal.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(QosInterfaceTest, EnableRtgTest, TestSize.Level1)
71 {
72 bool flag = true;
73 int ret = EnableRtg(flag);
74 EXPECT_EQ(ret, 0);
75 }
76
77 /**
78 * @tc.name: AuthEnableTest
79 * @tc.desc: Test whether the OnRemoteRequest interface are normal.
80 * @tc.type: FUNC
81 */
82 HWTEST_F(QosInterfaceTest, AuthEnableTest, TestSize.Level1)
83 {
84 unsigned int pid = getpid();
85 unsigned int uaFlag = AF_RTG_ALL;
86 unsigned int status = static_cast<unsigned int>(AuthStatus::AUTH_STATUS_BACKGROUND);
87 int ret = AuthEnable(pid, uaFlag, status);
88 EXPECT_EQ(ret, 0);
89 }
90
91 /**
92 * @tc.name: AuthSwitchTest
93 * @tc.desc: Test whether the AuthSwitch interface are normal.
94 * @tc.type: FUNC
95 */
96 HWTEST_F(QosInterfaceTest, AuthSwitchTest, TestSize.Level1)
97 {
98 unsigned int pid = getpid();
99 unsigned int rtgFlag = AF_RTG_ALL;
100 unsigned int qosFlag = AF_QOS_ALL;
101 unsigned int status = static_cast<unsigned int>(AuthStatus::AUTH_STATUS_BACKGROUND);
102 AuthEnable(pid, rtgFlag, status);
103 status = static_cast<unsigned int>(AuthStatus::AUTH_STATUS_FOREGROUND);
104 int ret = AuthSwitch(pid, rtgFlag, qosFlag, status);
105 EXPECT_EQ(ret, 0);
106 }
107
108 /**
109 * @tc.name: AuthDeleteTest
110 * @tc.desc: Test whether the AuthDelete interface are normal.
111 * @tc.type: FUNC
112 */
113 HWTEST_F(QosInterfaceTest, AuthDeleteTest, TestSize.Level1)
114 {
115 unsigned int pid = getpid();
116 unsigned int uaFlag = AF_RTG_ALL;
117 unsigned int status = static_cast<unsigned int>(AuthStatus::AUTH_STATUS_BACKGROUND);
118 AuthEnable(pid, uaFlag, status);
119 int ret = AuthDelete(pid);
120 EXPECT_EQ(ret, 0);
121 AuthEnable(pid, uaFlag, status);
122 }
123
124 /**
125 * @tc.name: AuthPauseTest
126 * @tc.desc: Test whether the AuthPause interface are normal.
127 * @tc.type: FUNC
128 */
129 HWTEST_F(QosInterfaceTest, AuthPauseTest, TestSize.Level1)
130 {
131 unsigned int pid = getpid();
132 unsigned int uaFlag = AF_RTG_ALL;
133 unsigned int status = static_cast<unsigned int>(AuthStatus::AUTH_STATUS_BACKGROUND);
134 AuthEnable(pid, uaFlag, status);
135 int ret = AuthPause(pid);
136 EXPECT_EQ(ret, 0);
137 AuthEnable(pid, uaFlag, status);
138 }
139
140 /**
141 * @tc.name: QosApplyTest
142 * @tc.desc: Test whether the QosApply interface are normal.
143 * @tc.type: FUNC
144 */
145 HWTEST_F(QosInterfaceTest, QosApplyTest, TestSize.Level1)
146 {
147 unsigned int level = 1;
148 int ret = -1;
149 ret = QosApply(level);
150 #if defined(ARM64_TEST) && ARM64_TEST
151 EXPECT_EQ(ret, 0);
152 #else
153 (void)ret;
154 #endif
155 }
156
157 /**
158 * @tc.name: AuthGetTest
159 * @tc.desc: Test whether the AuthGet interface are normal.
160 * @tc.type: FUNC
161 */
162 HWTEST_F(QosInterfaceTest, AuthGetTest, TestSize.Level1)
163 {
164 unsigned int pid = getpid();
165 unsigned int uaFlag1 = 0;
166 unsigned int *uaFlag = &uaFlag1;
167 unsigned int status1 = 0;
168 unsigned int *status = &status1;
169 int ret = AuthGet(pid);
170 if (!IsLinuxOs()) {
171 return;
172 }
173 EXPECT_GE(ret, 0);
174 pid = -1;
175 ret = AuthGet(pid);
176 EXPECT_EQ(ret, -1);
177 }
178
179 /**
180 * @tc.name: AuthEnhanceTest
181 * @tc.desc: Test whether the AuthEnhance interface are normal.
182 * @tc.type: FUNC
183 */
184 HWTEST_F(QosInterfaceTest, AuthEnhanceTest, TestSize.Level1)
185 {
186 unsigned int pid = getpid();
187 bool enhanceStatus = false;
188 int ret = AuthEnhance(pid, enhanceStatus);
189 EXPECT_EQ(ret, 0);
190 enhanceStatus = false;
191 ret = AuthEnhance(pid, enhanceStatus);
192 EXPECT_EQ(ret, 0);
193 }
194
195 /**
196 * @tc.name: QosApplyForOtherTest
197 * @tc.desc: Test whether the QosApplyForOther interface are normal.
198 * @tc.type: FUNC
199 */
200 HWTEST_F(QosInterfaceTest, QosApplyForOtherTest, TestSize.Level1)
201 {
202 unsigned int level = 1;
203 int tid = gettid();
204 int ret = -1;
205 ret = QosApplyForOther(level, tid);
206 #if defined(ARM64_TEST) && ARM64_TEST
207 EXPECT_EQ(ret, 0);
208 #else
209 (void)ret;
210 #endif
211 }
212
213 /**
214 * @tc.name: QosLeaveTest
215 * @tc.desc: Test whether the QosLeave interface are normal.
216 * @tc.type: FUNC
217 */
218 HWTEST_F(QosInterfaceTest, QosLeaveTest, TestSize.Level1)
219 {
220 int ret = -1;
221 ret = QosLeave();
222 #if defined(ARM64_TEST) && ARM64_TEST
223 EXPECT_EQ(ret, 0);
224 #else
225 (void)ret;
226 #endif
227 }
228
229 /**
230 * @tc.name: QosLeaveForOtherTest
231 * @tc.desc: Test whether the QosLeaveForOther interface are normal.
232 * @tc.type: FUNC
233 */
234 HWTEST_F(QosInterfaceTest, QosLeaveForOtherTest, TestSize.Level1)
235 {
236 int ret = -1;
237 int tid = gettid();
238 int level = 1;
239 ret = QosApplyForOther(level, tid);
240 ret = QosLeaveForOther(tid);
241 #if defined(ARM64_TEST) && ARM64_TEST
242 EXPECT_EQ(ret, 0);
243 #else
244 (void)ret;
245 #endif
246 }
247
248 /**
249 * @tc.name: QosPolicyTest
250 * @tc.desc: Test whether the QosPolicy interface are normal.
251 * @tc.type: FUNC
252 */
253
254 static struct QosPolicyDatas g_defaultQosPolicy = {
255 .policyType = QOS_POLICY_DEFAULT,
256 .policyFlag = QOS_FLAG_ALL,
257 .policys = {
258 {0, 0, 0, 1024, 0},
259 {0, 0, 0, 1024, 0},
260 {0, 0, 0, 1024, 0},
261 {0, 0, 0, 1024, 0},
262 {0, 0, 0, 1024, 0},
263 {0, 0, 0, 1024, 0},
264 {0, 0, 0, 1024, 0},
265 }
266 };
267
268 HWTEST_F(QosInterfaceTest, QosPolicyTest, TestSize.Level1)
269 {
270 int ret = -1;
271 struct QosPolicyDatas *policyDatas = nullptr;
272 ret = QosPolicySet(policyDatas);
273 EXPECT_EQ(ret, -1);
274 #if defined(ARM64_TEST) && ARM64_TEST
275 ret = QosPolicySet(&g_defaultQosPolicy);
276 EXPECT_EQ(ret, 0);
277 #endif
278 }
279 }
280 }
281 }
282