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