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 "data_collect_kit_test.h"
17 
18 #include "file_ex.h"
19 #include "nativetoken_kit.h"
20 #include "securec.h"
21 #include "token_setproc.h"
22 #define private public
23 #include "security_guard_define.h"
24 #include "sg_collect_client.h"
25 #undef private
26 
27 using namespace testing::ext;
28 using namespace OHOS::Security::SecurityGuardTest;
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33     int32_t ReportSecurityInfo(const struct EventInfoSt *info);
34     int32_t ReportSecurityInfoAsync(const struct EventInfoSt *info);
35 #ifdef __cplusplus
36 }
37 #endif
38 
39 namespace OHOS::Security::SecurityGuardTest {
40 
SetUpTestCase()41 void DataCollectKitTest::SetUpTestCase()
42 {
43     static const char *permission[] = { "ohos.permission.securityguard.REPORT_SECURITY_INFO" };
44     uint64_t tokenId;
45     NativeTokenInfoParams infoParams = {
46         .dcapsNum = 0,
47         .permsNum = 1,
48         .aclsNum = 0,
49         .dcaps = nullptr,
50         .perms = permission,
51         .acls = nullptr,
52         .processName = "security_guard",
53         .aplStr = "system_basic",
54     };
55     tokenId = GetAccessTokenId(&infoParams);
56     SetSelfTokenID(tokenId);
57     string isEnforcing;
58     LoadStringFromFile("/sys/fs/selinux/enforce", isEnforcing);
59     if (isEnforcing.compare("1") == 0) {
60         DataCollectKitTest::isEnforcing_ = true;
61         SaveStringToFile("/sys/fs/selinux/enforce", "0");
62     }
63 }
64 
TearDownTestCase()65 void DataCollectKitTest::TearDownTestCase()
66 {
67     if (DataCollectKitTest::isEnforcing_) {
68         SaveStringToFile("/sys/fs/selinux/enforce", "1");
69     }
70 }
71 
SetUp()72 void DataCollectKitTest::SetUp()
73 {
74 }
75 
TearDown()76 void DataCollectKitTest::TearDown()
77 {
78 }
79 
80 bool DataCollectKitTest::isEnforcing_ = false;
81 
82 /**
83  * @tc.name: ReportSecurityInfo001
84  * @tc.desc: ReportSecurityInfo with right param
85  * @tc.type: FUNC
86  * @tc.require: SR000H96L5
87  */
88 HWTEST_F(DataCollectKitTest, ReportSecurityInfo001, TestSize.Level1)
89 {
90     static int64_t eventId = 1011009000;
91     static std::string version = "0";
92     static std::string content = "{\"cred\":0,\"extra\":\"\",\"status\":0}";
93     EventInfoSt info;
94     info.eventId = eventId;
95     info.version = version.c_str();
96     (void) memset_s(info.content, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
97     errno_t rc = memcpy_s(info.content, CONTENT_MAX_LEN, content.c_str(), content.length());
98     EXPECT_TRUE(rc == EOK);
99     info.contentLen = static_cast<uint32_t>(content.length());
100     int ret = ReportSecurityInfo(&info);
101     EXPECT_EQ(ret, SecurityGuard::SUCCESS);
102 }
103 
104 /**
105  * @tc.name: ReportSecurityInfo002
106  * @tc.desc: ReportSecurityInfo with wrong cred
107  * @tc.type: FUNC
108  * @tc.require: SR000H96L5
109  */
110 HWTEST_F(DataCollectKitTest, ReportSecurityInfo002, TestSize.Level1)
111 {
112     static int64_t eventId = 1011009000;
113     static std::string version = "0";
114     static std::string content = "{\"cred\":\"0\",\"extra\":\"\",\"status\":0}";
115     EventInfoSt info;
116     info.eventId = eventId;
117     info.version = version.c_str();
118     (void) memset_s(info.content, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
119     errno_t rc = memcpy_s(info.content, CONTENT_MAX_LEN, content.c_str(), content.length());
120     EXPECT_TRUE(rc == EOK);
121     info.contentLen = static_cast<uint32_t>(content.length());
122     int ret = ReportSecurityInfo(&info);
123     EXPECT_EQ(ret, SecurityGuard::SUCCESS);
124 }
125 
126 /**
127  * @tc.name: ReportSecurityInfo003
128  * @tc.desc: ReportSecurityInfo with wrong extra
129  * @tc.type: FUNC
130  * @tc.require: SR000H96L5
131  */
132 HWTEST_F(DataCollectKitTest, ReportSecurityInfo003, TestSize.Level1)
133 {
134     static int64_t eventId = 1011009000;
135     static std::string version = "0";
136     static std::string content = "{\"cred\":0,\"extra\":0,\"status\":0}";
137     EventInfoSt info;
138     info.eventId = eventId;
139     info.version = version.c_str();
140     (void) memset_s(info.content, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
141     errno_t rc = memcpy_s(info.content, CONTENT_MAX_LEN, content.c_str(), content.length());
142     EXPECT_TRUE(rc == EOK);
143     info.contentLen = static_cast<uint32_t>(content.length());
144     int ret = ReportSecurityInfo(&info);
145     EXPECT_EQ(ret, SecurityGuard::SUCCESS);
146 }
147 
148 /**
149  * @tc.name: ReportSecurityInfo004
150  * @tc.desc: ReportSecurityInfo with wrong status
151  * @tc.type: FUNC
152  * @tc.require: SR000H96L5
153  */
154 HWTEST_F(DataCollectKitTest, ReportSecurityInfo004, TestSize.Level1)
155 {
156     static int64_t eventId = 1011009000;
157     static std::string version = "0";
158     static std::string content = "{\"cred\":0,\"extra\":\"\",\"status\":\"0\"}";
159     EventInfoSt info;
160     info.eventId = eventId;
161     info.version = version.c_str();
162     (void) memset_s(info.content, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
163     errno_t rc = memcpy_s(info.content, CONTENT_MAX_LEN, content.c_str(), content.length());
164     EXPECT_TRUE(rc == EOK);
165     info.contentLen = static_cast<uint32_t>(content.length());
166     int ret = ReportSecurityInfo(&info);
167     EXPECT_EQ(ret, SecurityGuard::SUCCESS);
168 }
169 
170 /**
171  * @tc.name: ReportSecurityInfo005
172  * @tc.desc: ReportSecurityInfo with wrong eventId
173  * @tc.type: FUNC
174  * @tc.require: SR000H96L5
175  */
176 HWTEST_F(DataCollectKitTest, ReportSecurityInfo005, TestSize.Level1)
177 {
178     static int64_t eventId = 0;
179     static std::string version = "0";
180     static std::string content = "{\"cred\":0,\"extra\":\"\",\"status\":0}";
181     EventInfoSt info;
182     info.eventId = eventId;
183     info.version = version.c_str();
184     (void) memset_s(info.content, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
185     errno_t rc = memcpy_s(info.content, CONTENT_MAX_LEN, content.c_str(), content.length());
186     EXPECT_TRUE(rc == EOK);
187     info.contentLen = static_cast<uint32_t>(content.length());
188     int ret = ReportSecurityInfo(&info);
189     EXPECT_EQ(ret, SecurityGuard::SUCCESS);
190 }
191 
192 /**
193  * @tc.name: ReportSecurityInfo006
194  * @tc.desc: ReportSecurityInfo with null info
195  * @tc.type: FUNC
196  * @tc.require: SR000H96L5
197  */
198 HWTEST_F(DataCollectKitTest, ReportSecurityInfo006, TestSize.Level1)
199 {
200     int ret = ReportSecurityInfo(nullptr);
201     EXPECT_EQ(ret, SecurityGuard::BAD_PARAM);
202 }
203 
204 /**
205  * @tc.name: ReportSecurityInfoAsync001
206  * @tc.desc: ReportSecurityInfoAsync with right param
207  * @tc.type: FUNC
208  * @tc.require: SR000H96L5
209  */
210 HWTEST_F(DataCollectKitTest, ReportSecurityInfoAsync001, TestSize.Level1)
211 {
212     static int64_t eventId = 1011009000;
213     static std::string version = "0";
214     static std::string content = "{\"cred\":0,\"extra\":\"\",\"status\":0}";
215     EventInfoSt info;
216     info.eventId = eventId;
217     info.version = version.c_str();
218     (void) memset_s(info.content, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
219     errno_t rc = memcpy_s(info.content, CONTENT_MAX_LEN, content.c_str(), content.length());
220     EXPECT_TRUE(rc == EOK);
221     info.contentLen = static_cast<uint32_t>(content.length());
222     int ret = ReportSecurityInfoAsync(&info);
223     EXPECT_EQ(ret, SecurityGuard::SUCCESS);
224 }
225 
226 /**
227  * @tc.name: ReleaseProxy001
228  * @tc.desc: SgCollectClient ReleaseProxy
229  * @tc.type: FUNC
230  * @tc.require: SR000H96L5
231  */
232 HWTEST_F(DataCollectKitTest, ReleaseProxy001, TestSize.Level1)
233 {
234     SecurityGuard::SgCollectClient::GetInstance().ReleaseProxy();
235     ASSERT_NE(nullptr, &SecurityGuard::SgCollectClient::GetInstance().proxy_);
236 }
237 
238 /**
239  * @tc.name: ReportSecurityInfo007
240  * @tc.desc: SgCollectClient DeathRecipient OnRemoteDied
241  * @tc.type: FUNC
242  * @tc.require: SR000H96L5
243  */
244 HWTEST_F(DataCollectKitTest, DeathRecipient001, TestSize.Level1)
245 {
246     SecurityGuard::SgCollectClientDeathRecipient recipient =
247         SecurityGuard::SgCollectClientDeathRecipient();
248     recipient.OnRemoteDied(nullptr);
249 }
250 }