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 "obtaindata_kit_test.h"
17 
18 #include "file_ex.h"
19 
20 #include "sg_obtaindata_client.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::Security::SecurityGuardTest;
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28     int32_t RequestSecurityModelResultAsync(const DeviceIdentify *devId, uint32_t modelId,
29         SecurityGuardRiskCallback callback);
30 #ifdef __cplusplus
31 }
32 #endif
33 
34 namespace OHOS::Security::SecurityGuardTest {
35 
SetUpTestCase()36 void ObtainDataKitTest::SetUpTestCase()
37 {
38     string isEnforcing;
39     LoadStringFromFile("/sys/fs/selinux/enforce", isEnforcing);
40     if (isEnforcing.compare("1") == 0) {
41         ObtainDataKitTest::isEnforcing_ = true;
42         SaveStringToFile("/sys/fs/selinux/enforce", "0");
43     }
44 }
45 
TearDownTestCase()46 void ObtainDataKitTest::TearDownTestCase()
47 {
48     if (ObtainDataKitTest::isEnforcing_) {
49         SaveStringToFile("/sys/fs/selinux/enforce", "1");
50     }
51 }
52 
SetUp()53 void ObtainDataKitTest::SetUp()
54 {
55 }
56 
TearDown()57 void ObtainDataKitTest::TearDown()
58 {
59 }
60 
61 bool ObtainDataKitTest::isEnforcing_ = false;
62 
RequestSecurityEventInfoCallBackFunc(const DeviceIdentify * devId,const char * eventBuffList,uint32_t status)63 void ObtainDataKitTest::RequestSecurityEventInfoCallBackFunc(const DeviceIdentify *devId, const char *eventBuffList,
64     uint32_t status)
65 {
66     EXPECT_TRUE(devId != nullptr);
67     EXPECT_TRUE(eventBuffList != nullptr);
68 }
69 
70 /**
71  * @tc.name: RequestSecurityEventInfoAsync001
72  * @tc.desc: RequestSecurityEventInfoAsync with right param
73  * @tc.type: FUNC
74  * @tc.require: SR000H96FD
75  */
76 HWTEST_F(ObtainDataKitTest, RequestSecurityEventInfoAsync001, TestSize.Level1)
77 {
78     DeviceIdentify deviceIdentify = {};
79     static std::string eventList = "{\"eventId\":[1011009000]}";
80     int ret = RequestSecurityEventInfoAsync(&deviceIdentify, eventList.c_str(), RequestSecurityEventInfoCallBackFunc);
81     EXPECT_EQ(ret, SecurityGuard::NO_PERMISSION);
82 }
83 
84 /**
85  * @tc.name: RequestSecurityEventInfoAsync002
86  * @tc.desc: RequestSecurityEventInfoAsync with right param, get all info
87  * @tc.type: FUNC
88  * @tc.require: SR000H96FD
89  */
90 HWTEST_F(ObtainDataKitTest, RequestSecurityEventInfoAsync002, TestSize.Level1)
91 {
92     DeviceIdentify deviceIdentify = {};
93     static std::string eventList = "{\"eventId\":[-1]}";
94     int ret = RequestSecurityEventInfoAsync(&deviceIdentify, eventList.c_str(), RequestSecurityEventInfoCallBackFunc);
95     EXPECT_EQ(ret, SecurityGuard::NO_PERMISSION);
96 }
97 
98 /**
99  * @tc.name: RequestSecurityEventInfoAsync003
100  * @tc.desc: RequestSecurityEventInfoAsync with wrong eventList key
101  * @tc.type: FUNC
102  * @tc.require: SR000H96FD
103  */
104 HWTEST_F(ObtainDataKitTest, RequestSecurityEventInfoAsync003, TestSize.Level1)
105 {
106     DeviceIdentify deviceIdentify = {};
107     static std::string eventList = "{\"eventIds\":[1011009000]}";
108     int ret = RequestSecurityEventInfoAsync(&deviceIdentify, eventList.c_str(), RequestSecurityEventInfoCallBackFunc);
109     EXPECT_EQ(ret, SecurityGuard::NO_PERMISSION);
110 }
111 
112 /**
113  * @tc.name: RequestSecurityEventInfoAsync004
114  * @tc.desc: RequestSecurityEventInfoAsync with wrong eventList content
115  * @tc.type: FUNC
116  * @tc.require: SR000H96FD
117  */
118 HWTEST_F(ObtainDataKitTest, RequestSecurityEventInfoAsync004, TestSize.Level1)
119 {
120     DeviceIdentify deviceIdentify = {};
121     static std::string eventList = "{eventId:[1011009000]}";
122     int ret = RequestSecurityEventInfoAsync(&deviceIdentify, eventList.c_str(), RequestSecurityEventInfoCallBackFunc);
123     EXPECT_EQ(ret, SecurityGuard::NO_PERMISSION);
124 }
125 
126 /**
127  * @tc.name: RequestSecurityEventInfoAsync005
128  * @tc.desc: RequestSecurityEventInfoAsync with wrong eventList null
129  * @tc.type: FUNC
130  * @tc.require: SR000H96FD
131  */
132 HWTEST_F(ObtainDataKitTest, RequestSecurityEventInfoAsync005, TestSize.Level1)
133 {
134     DeviceIdentify deviceIdentify = {};
135     static std::string eventList = "{\"eventIds\":[]}";
136     int ret = RequestSecurityEventInfoAsync(&deviceIdentify, eventList.c_str(), RequestSecurityEventInfoCallBackFunc);
137     EXPECT_EQ(ret, SecurityGuard::NO_PERMISSION);
138 }
139 
140 /**
141  * @tc.name: RequestSecurityEventInfoAsync006
142  * @tc.desc: RequestSecurityEventInfoAsync with wrong eventList not contain right eventId
143  * @tc.type: FUNC
144  * @tc.require: SR000H96FD
145  */
146 HWTEST_F(ObtainDataKitTest, RequestSecurityEventInfoAsync006, TestSize.Level1)
147 {
148     DeviceIdentify deviceIdentify = {};
149     static std::string eventList = "{\"eventIds\":[0]}";
150     int ret = RequestSecurityEventInfoAsync(&deviceIdentify, eventList.c_str(), RequestSecurityEventInfoCallBackFunc);
151     EXPECT_EQ(ret, SecurityGuard::NO_PERMISSION);
152 }
153 
154 /**
155  * @tc.name: RequestSecurityEventInfoAsync007
156  * @tc.desc: RequestSecurityEventInfoAsync with null devId
157  * @tc.type: FUNC
158  * @tc.require: SR000H96FD
159  */
160 HWTEST_F(ObtainDataKitTest, RequestSecurityEventInfoAsync007, TestSize.Level1)
161 {
162     static std::string eventList = "{\"eventIds\":[0]}";
163     int ret = RequestSecurityEventInfoAsync(nullptr, eventList.c_str(), RequestSecurityEventInfoCallBackFunc);
164     EXPECT_EQ(ret, SecurityGuard::BAD_PARAM);
165 }
166 
167 /**
168  * @tc.name: RequestSecurityEventInfoAsync008
169  * @tc.desc: RequestSecurityEventInfoAsync with null eventList
170  * @tc.type: FUNC
171  * @tc.require: SR000H96FD
172  */
173 HWTEST_F(ObtainDataKitTest, RequestSecurityEventInfoAsync008, TestSize.Level1)
174 {
175     DeviceIdentify deviceIdentify = {};
176     int ret = RequestSecurityEventInfoAsync(&deviceIdentify, nullptr, RequestSecurityEventInfoCallBackFunc);
177     EXPECT_EQ(ret, SecurityGuard::BAD_PARAM);
178 }
179 }