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