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: AR000HO9J7
59 */
60 HWTEST_F(SecCompStubTest, OnRemoteRequest001, TestSize.Level1)
61 {
62 MessageParcel data;
63 MessageParcel reply;
64 MessageOption option;
65
66 data.WriteInterfaceToken(u"wrong");
67 ASSERT_EQ(SC_SERVICE_ERROR_IPC_REQUEST_FAIL, stub_->OnRemoteRequest(static_cast<uint32_t>(
68 SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option));
69 data.FlushBuffer();
70 reply.FlushBuffer();
71
72 data.WriteInterfaceToken(u"ohos.security.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: AR000HO9J7
81 */
82 HWTEST_F(SecCompStubTest, RegisterSecurityComponentInner001, TestSize.Level1)
83 {
84 MessageParcel data;
85 MessageParcel reply;
86
87 ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->RegisterSecurityComponentInner(data, reply));
88 }
89
90 /**
91 * @tc.name: UpdateSecurityComponentInner001
92 * @tc.desc: Test update security component
93 * @tc.type: FUNC
94 * @tc.require: AR000HO9J7
95 */
96 HWTEST_F(SecCompStubTest, UpdateSecurityComponentInner001, TestSize.Level1)
97 {
98 MessageParcel data;
99 MessageParcel reply;
100
101 ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->UpdateSecurityComponentInner(data, reply));
102 }
103
104 /**
105 * @tc.name: UnregisterSecurityComponentInner001
106 * @tc.desc: Test unregister security component
107 * @tc.type: FUNC
108 * @tc.require: AR000HO9J7
109 */
110 HWTEST_F(SecCompStubTest, UnregisterSecurityComponentInner001, TestSize.Level1)
111 {
112 MessageParcel data;
113 MessageParcel reply;
114 ASSERT_EQ(SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL, stub_->UnregisterSecurityComponentInner(data, reply));
115 }
116
117 /**
118 * @tc.name: Marshalling001
119 * @tc.desc: Test SecCompClickEventParcel::Marshalling
120 * @tc.type: FUNC
121 * @tc.require: AR000HO9J7
122 */
123 HWTEST_F(SecCompStubTest, Marshalling001, TestSize.Level1)
124 {
125 sptr<SecCompClickEventParcel> clickParcel = new (std::nothrow) SecCompClickEventParcel();
126 Parcel out;
127 EXPECT_FALSE(clickParcel->Marshalling(out));
128 clickParcel->clickInfoParams_.type = ClickEventType::UNKNOWN_EVENT_TYPE;
129 EXPECT_FALSE(clickParcel->Marshalling(out));
130
131 clickParcel->clickInfoParams_.extraInfo.dataSize = 1;
132 clickParcel->clickInfoParams_.type = ClickEventType::POINT_EVENT_TYPE;
133 EXPECT_FALSE(clickParcel->Marshalling(out));
134 clickParcel->clickInfoParams_.type = ClickEventType::KEY_EVENT_TYPE;
135 EXPECT_FALSE(clickParcel->Marshalling(out));
136
137 uint8_t data[32] = {0};
138 clickParcel->clickInfoParams_.extraInfo.dataSize = 32;
139 clickParcel->clickInfoParams_.extraInfo.data = data;
140 EXPECT_TRUE(clickParcel->Marshalling(out));
141 }
142
143 /**
144 * @tc.name: Unmarshalling001
145 * @tc.desc: Test SecCompClickEventParcel::Unmarshalling
146 * @tc.type: FUNC
147 * @tc.require: AR000HO9J7
148 */
149 HWTEST_F(SecCompStubTest, Unmarshalling001, TestSize.Level1)
150 {
151 sptr<SecCompClickEventParcel> clickParcel = new (std::nothrow) SecCompClickEventParcel();
152 Parcel in;
153 in.WriteInt32(1);
154 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
155 in.WriteInt32(2);
156 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
157 in.WriteInt32(0);
158 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
159
160 in.WriteInt32(2);
161 in.WriteUint64(1);
162 in.WriteInt32(1);
163 int dataSize = MAX_EXTRA_SIZE + 1;
164 in.WriteUint32(dataSize);
165 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
166
167 in.WriteInt32(2);
168 in.WriteUint64(1);
169 in.WriteInt32(1);
170 in.WriteUint32(1);
171 EXPECT_EQ(nullptr, clickParcel->Unmarshalling(in));
172
173 in.WriteInt32(2);
174 in.WriteUint64(1);
175 in.WriteInt32(1);
176 in.WriteUint32(32);
177 uint8_t data[32] = {0};
178 in.WriteBuffer(data, 32);
179 EXPECT_NE(nullptr, clickParcel->Unmarshalling(in));
180 }
181