• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "sec_comp_stub_mock_test.h"
17 
18 #include "sec_comp_dialog_callback.h"
19 #include "sec_comp_log.h"
20 #include "sec_comp_err.h"
21 #include "sec_comp_click_event_parcel.h"
22 #include "service_test_common.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::Security::SecurityComponent;
27 
28 namespace {
29 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
30     LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompStubMockTest"};
31 }
32 
SetUpTestCase()33 void SecCompStubMockTest::SetUpTestCase()
34 {}
35 
TearDownTestCase()36 void SecCompStubMockTest::TearDownTestCase()
37 {}
38 
SetUp()39 void SecCompStubMockTest::SetUp()
40 {
41     SC_LOG_INFO(LABEL, "setup");
42     if (stub_ != nullptr) {
43         return;
44     }
45 
46     stub_ = new (std::nothrow) SecCompStubMock();
47     ASSERT_NE(nullptr, stub_);
48 }
49 
TearDown()50 void SecCompStubMockTest::TearDown()
51 {
52     stub_ = nullptr;
53 }
54 
55 /**
56  * @tc.name: OnRemoteRequestMock001
57  * @tc.desc: Test on remote request
58  * @tc.type: FUNC
59  * @tc.require:
60  */
61 HWTEST_F(SecCompStubMockTest, OnRemoteRequestMock001, TestSize.Level1)
62 {
63     MessageParcel data;
64     MessageParcel reply;
65     MessageOption option;
66 
67     data.WriteInterfaceToken(u"wrong");
68     ASSERT_EQ(SC_SERVICE_ERROR_IPC_REQUEST_FAIL, stub_->OnRemoteRequest(static_cast<uint32_t>(
69         SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option));
70     data.FlushBuffer();
71     reply.FlushBuffer();
72 
73     data.WriteInterfaceToken(u"ohos.security.ISecCompService");
74     ASSERT_EQ(305, stub_->OnRemoteRequest(1000, data, reply, option));
75 }
76 
77 /**
78  * @tc.name: RegisterSecurityComponentInnerMock001
79  * @tc.desc: Test register security component
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(SecCompStubMockTest, RegisterSecurityComponentInnerMock001, TestSize.Level1)
84 {
85     MessageParcel data;
86     MessageParcel reply;
87 
88     ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->RegisterSecurityComponentInner(data, reply));
89     data.FlushBuffer();
90     reply.FlushBuffer();
91 
92     data.WriteUint32(UNKNOWN_SC_TYPE);
93     ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->RegisterSecurityComponentInner(data, reply));
94     data.FlushBuffer();
95     reply.FlushBuffer();
96 
97     data.WriteUint32(MAX_SC_TYPE);
98     ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->RegisterSecurityComponentInner(data, reply));
99     data.FlushBuffer();
100     reply.FlushBuffer();
101 
102     data.WriteUint32(LOCATION_COMPONENT);
103     ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->RegisterSecurityComponentInner(data, reply));
104     data.FlushBuffer();
105     reply.FlushBuffer();
106 
107     data.WriteUint32(LOCATION_COMPONENT);
108     data.WriteString("");
109     ASSERT_EQ(SC_OK, stub_->RegisterSecurityComponentInner(data, reply));
110 }
111 
112 /**
113  * @tc.name: UpdateSecurityComponentInnerMock001
114  * @tc.desc: Test update security component
115  * @tc.type: FUNC
116  * @tc.require:
117  */
118 HWTEST_F(SecCompStubMockTest, UpdateSecurityComponentInnerMock001, TestSize.Level1)
119 {
120     MessageParcel data;
121     MessageParcel reply;
122 
123     ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->UpdateSecurityComponentInner(data, reply));
124     data.FlushBuffer();
125     reply.FlushBuffer();
126 
127     data.WriteInt32(-1);
128     ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->UpdateSecurityComponentInner(data, reply));
129     data.FlushBuffer();
130     reply.FlushBuffer();
131 
132     data.WriteInt32(1);
133     ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->UpdateSecurityComponentInner(data, reply));
134     data.FlushBuffer();
135     reply.FlushBuffer();
136 
137     data.WriteInt32(1);
138     data.WriteString("");
139     ASSERT_EQ(SC_OK, stub_->UpdateSecurityComponentInner(data, reply));
140 }
141 
142 /**
143  * @tc.name: UnregisterSecurityComponentInnerMock001
144  * @tc.desc: Test unregister security component
145  * @tc.type: FUNC
146  * @tc.require:
147  */
148 HWTEST_F(SecCompStubMockTest, UnregisterSecurityComponentInnerMock001, TestSize.Level1)
149 {
150     MessageParcel data;
151     MessageParcel reply;
152     ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->UnregisterSecurityComponentInner(data, reply));
153     data.FlushBuffer();
154     reply.FlushBuffer();
155 
156     data.WriteInt32(-1);
157     ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->UnregisterSecurityComponentInner(data, reply));
158     data.FlushBuffer();
159     reply.FlushBuffer();
160 
161     data.WriteInt32(1);
162     ASSERT_EQ(SC_OK, stub_->UnregisterSecurityComponentInner(data, reply));
163 }
164 
165 /**
166  * @tc.name: VerifySavePermissionInnerMock001
167  * @tc.desc: Test VerifySavePermissionInner
168  * @tc.type: FUNC
169  * @tc.require:
170  */
171 HWTEST_F(SecCompStubMockTest, VerifySavePermissionInnerMock001, TestSize.Level1)
172 {
173     MessageParcel data;
174     MessageParcel reply;
175     setuid(0);
176     ASSERT_TRUE(stub_->IsMediaLibraryCalling());
177     ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->VerifySavePermissionInner(data, reply));
178     data.FlushBuffer();
179     reply.FlushBuffer();
180     data.WriteInt32(0);
181     ASSERT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, stub_->VerifySavePermissionInner(data, reply));
182     data.FlushBuffer();
183     reply.FlushBuffer();
184     data.WriteInt32(1);
185     ASSERT_EQ(SC_OK, stub_->VerifySavePermissionInner(data, reply));
186     ASSERT_NE(SC_OK, stub_->GetEnhanceRemoteObjectInner(data, reply));
187 }
188 
189 /**
190  * @tc.name: MarshallingMock001
191  * @tc.desc: Test SecCompClickEventParcel::Marshalling
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(SecCompStubMockTest, MarshallingMock001, TestSize.Level1)
196 {
197     sptr<SecCompClickEventParcel> clickParcel = new (std::nothrow) SecCompClickEventParcel();
198     Parcel out;
199     EXPECT_FALSE(clickParcel->Marshalling(out));
200     clickParcel->clickInfoParams_.type = ClickEventType::UNKNOWN_EVENT_TYPE;
201     EXPECT_FALSE(clickParcel->Marshalling(out));
202 
203     clickParcel->clickInfoParams_.extraInfo.dataSize = 1;
204     clickParcel->clickInfoParams_.type = ClickEventType::POINT_EVENT_TYPE;
205     EXPECT_FALSE(clickParcel->Marshalling(out));
206     clickParcel->clickInfoParams_.type = ClickEventType::KEY_EVENT_TYPE;
207     EXPECT_FALSE(clickParcel->Marshalling(out));
208 
209     uint8_t data[32] = {0};
210     clickParcel->clickInfoParams_.extraInfo.dataSize = 32;
211     clickParcel->clickInfoParams_.extraInfo.data = data;
212     EXPECT_TRUE(clickParcel->Marshalling(out));
213 }
214 
215 /**
216  * @tc.name: UnmarshallingMock001
217  * @tc.desc: Test SecCompClickEventParcel::Unmarshalling
218  * @tc.type: FUNC
219  * @tc.require:
220  */
221 HWTEST_F(SecCompStubMockTest, UnmarshallingMock001, TestSize.Level1)
222 {
223     sptr<SecCompClickEventParcel> clickParcel = new (std::nothrow) SecCompClickEventParcel();
224     Parcel in;
225     in.WriteInt32(1);
226     EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
227     in.WriteInt32(2);
228     EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
229     in.WriteInt32(0);
230     EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
231 
232     in.WriteInt32(2);
233     in.WriteUint64(1);
234     in.WriteInt32(1);
235     int dataSize = MAX_EXTRA_SIZE + 1;
236     in.WriteUint32(dataSize);
237     EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
238 
239     in.WriteInt32(2);
240     in.WriteUint64(1);
241     in.WriteInt32(1);
242     in.WriteUint32(1);
243     EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
244 
245     in.WriteInt32(2);
246     in.WriteUint64(1);
247     in.WriteInt32(1);
248     in.WriteUint32(32);
249     uint8_t data[32] = {0};
250     in.WriteBuffer(data, 32);
251     EXPECT_NE(nullptr, clickParcel->Unmarshalling(in));
252 }
253 
254 /**
255  * @tc.name: PreRegisterSecCompProcessMock001
256  * @tc.desc: Test PreRegisterSecCompProcessInner
257  * @tc.type: FUNC
258  * @tc.require:
259  */
260 HWTEST_F(SecCompStubMockTest, PreRegisterSecCompProcessMock001, TestSize.Level1)
261 {
262     MessageParcel data;
263     MessageParcel reply;
264     data.FlushBuffer();
265     reply.FlushBuffer();
266     data.WriteInt32(1);
267     ASSERT_EQ(SC_OK, stub_->PreRegisterSecCompProcessInner(data, reply));
268 }