1 /*
2 * Copyright (c) 2023 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_test.h"
17
18 #include "sec_comp_log.h"
19 #include "sec_comp_err.h"
20 #include "sec_comp_click_event_parcel.h"
21 #include "service_test_common.h"
22
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::Security::SecurityComponent;
26
27 namespace {
28 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
29 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompStubTest"};
30 }
31
SetUpTestCase()32 void SecCompStubTest::SetUpTestCase()
33 {}
34
TearDownTestCase()35 void SecCompStubTest::TearDownTestCase()
36 {}
37
SetUp()38 void SecCompStubTest::SetUp()
39 {
40 SC_LOG_INFO(LABEL, "setup");
41 if (stub_ != nullptr) {
42 return;
43 }
44
45 stub_ = new (std::nothrow) SecCompStubMock();
46 ASSERT_NE(nullptr, stub_);
47 }
48
TearDown()49 void SecCompStubTest::TearDown()
50 {
51 stub_ = nullptr;
52 }
53
54 /**
55 * @tc.name: OnRemoteRequest001
56 * @tc.desc: Test on remote request
57 * @tc.type: FUNC
58 * @tc.require:
59 */
60 HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level0)
61 {
62 MessageParcel data;
63 MessageParcel reply;
64 MessageOption option;
65
66 data.WriteInterfaceToken(u"wrong");
67 ASSERT_EQ(ERR_TRANSACTION_FAILED, stub_->OnRemoteRequest(static_cast<uint32_t>(
68 ISecCompServiceIpcCode::COMMAND_REGISTER_SECURITY_COMPONENT), data, reply, option));
69 data.FlushBuffer();
70 reply.FlushBuffer();
71
72 data.WriteInterfaceToken(u"OHOS.Security.SecurityComponent.ISecCompService");
73 ASSERT_EQ(305, stub_->OnRemoteRequest(1000, data, reply, option));
74 }
75
76 /**
77 * @tc.name: RegisterSecurityComponentInner001
78 * @tc.desc: Test register security component
79 * @tc.type: FUNC
80 * @tc.require:
81 */
82 HWTEST_F(SecCompStubTest, RegisterSecurityComponentInner001, TestSize.Level0)
83 {
84 MessageParcel data;
85 MessageParcel reply;
86 MessageOption option(MessageOption::TF_SYNC);
87 data.WriteInterfaceToken(u"OHOS.Security.SecurityComponent.ISecCompService");
88 ASSERT_EQ(ERR_INVALID_DATA, stub_->OnRemoteRequest(static_cast<uint32_t>(
89 ISecCompServiceIpcCode::COMMAND_REGISTER_SECURITY_COMPONENT), data, reply, option));
90 }
91
92 /**
93 * @tc.name: UpdateSecurityComponentInner001
94 * @tc.desc: Test update security component
95 * @tc.type: FUNC
96 * @tc.require:
97 */
98 HWTEST_F(SecCompStubTest, UpdateSecurityComponentInner001, TestSize.Level0)
99 {
100 MessageParcel data;
101 MessageParcel reply;
102 MessageOption option(MessageOption::TF_SYNC);
103 data.WriteInterfaceToken(u"OHOS.Security.SecurityComponent.ISecCompService");
104 ASSERT_EQ(ERR_INVALID_DATA, stub_->OnRemoteRequest(static_cast<uint32_t>(
105 ISecCompServiceIpcCode::COMMAND_UPDATE_SECURITY_COMPONENT), data, reply, option));
106 }
107
108 /**
109 * @tc.name: UnregisterSecurityComponentInner001
110 * @tc.desc: Test unregister security component
111 * @tc.type: FUNC
112 * @tc.require:
113 */
114 HWTEST_F(SecCompStubTest, UnregisterSecurityComponentInner001, TestSize.Level0)
115 {
116 MessageParcel data;
117 MessageParcel reply;
118 MessageOption option(MessageOption::TF_SYNC);
119 data.WriteInterfaceToken(u"OHOS.Security.SecurityComponent.ISecCompService");
120 ASSERT_EQ(ERR_INVALID_DATA, stub_->OnRemoteRequest(static_cast<uint32_t>(
121 ISecCompServiceIpcCode::COMMAND_UNREGISTER_SECURITY_COMPONENT), data, reply, option));
122 }
123
124 /**
125 * @tc.name: Marshalling001
126 * @tc.desc: Test SecCompClickEventParcel::Marshalling
127 * @tc.type: FUNC
128 * @tc.require:
129 */
130 HWTEST_F(SecCompStubTest, Marshalling001, TestSize.Level0)
131 {
132 sptr<SecCompClickEventParcel> clickParcel = new (std::nothrow) SecCompClickEventParcel();
133 Parcel out;
134 EXPECT_FALSE(clickParcel->Marshalling(out));
135 clickParcel->clickInfoParams_.type = ClickEventType::UNKNOWN_EVENT_TYPE;
136 EXPECT_FALSE(clickParcel->Marshalling(out));
137
138 clickParcel->clickInfoParams_.extraInfo.dataSize = 1;
139 clickParcel->clickInfoParams_.type = ClickEventType::POINT_EVENT_TYPE;
140 EXPECT_FALSE(clickParcel->Marshalling(out));
141 clickParcel->clickInfoParams_.type = ClickEventType::KEY_EVENT_TYPE;
142 EXPECT_FALSE(clickParcel->Marshalling(out));
143 clickParcel->clickInfoParams_.type = ClickEventType::ACCESSIBILITY_EVENT_TYPE;
144 EXPECT_FALSE(clickParcel->Marshalling(out));
145
146 uint8_t data[32] = {0};
147 clickParcel->clickInfoParams_.extraInfo.dataSize = 32;
148 clickParcel->clickInfoParams_.extraInfo.data = data;
149 EXPECT_TRUE(clickParcel->Marshalling(out));
150 }
151
152 /**
153 * @tc.name: Unmarshalling001
154 * @tc.desc: Test SecCompClickEventParcel::Unmarshalling
155 * @tc.type: FUNC
156 * @tc.require:
157 */
158 HWTEST_F(SecCompStubTest, Unmarshalling001, TestSize.Level0)
159 {
160 sptr<SecCompClickEventParcel> clickParcel = new (std::nothrow) SecCompClickEventParcel();
161 Parcel in;
162 in.WriteInt32(1); // ClickEventType::POINT_EVENT_TYPE
163 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
164 in.WriteInt32(2); // ClickEventType::KEY_EVENT_TYPE
165 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
166 in.WriteInt32(0); // ClickEventType::UNKNOWN_EVENT_TYPE
167 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
168 in.WriteInt32(3); // ClickEventType::ACCESSIBILITY_EVENT_TYPE
169 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
170
171 in.WriteInt32(2); // ClickEventType::KEY_EVENT_TYPE
172 in.WriteUint64(1);
173 in.WriteInt32(1);
174 int dataSize = MAX_EXTRA_SIZE + 1;
175 in.WriteUint32(dataSize);
176 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
177
178 in.WriteInt32(2); // ClickEventType::KEY_EVENT_TYPE
179 in.WriteUint64(1);
180 in.WriteInt32(1);
181 in.WriteUint32(1);
182 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
183
184 in.WriteInt32(2); // ClickEventType::KEY_EVENT_TYPE
185 in.WriteUint64(1);
186 in.WriteInt32(1);
187 in.WriteUint32(32); // dataLen
188 uint8_t data[32] = {0};
189 in.WriteBuffer(data, 32);
190 EXPECT_NE(nullptr, clickParcel->Unmarshalling(in));
191
192 in.FlushBuffer();
193 in.WriteInt32(3); // ClickEventType::ACCESSIBILITY_EVENT_TYPE
194 in.WriteInt64(1);
195 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
196
197 in.FlushBuffer();
198 in.WriteInt32(3); // ClickEventType::ACCESSIBILITY_EVENT_TYPE
199 in.WriteInt64(1);
200 in.WriteInt64(1);
201 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
202
203 in.FlushBuffer();
204 in.WriteInt32(3); // ClickEventType::ACCESSIBILITY_EVENT_TYPE
205 in.WriteInt64(1);
206 in.WriteInt64(1);
207 in.WriteUint32(32); // dataLen
208 in.WriteBuffer(data, 32);
209 EXPECT_NE(nullptr, clickParcel->Unmarshalling(in));
210 }
211