• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "sec_comp_kit_test.h"
16 
17 #include "location_button.h"
18 #define private public
19 #include "sec_comp_caller_authorization.h"
20 #undef private
21 #include "sec_comp_err.h"
22 #include "sec_comp_info.h"
23 #include "sec_comp_log.h"
24 #include "sec_comp_tool.h"
25 #include "test_common.h"
26 
27 using namespace testing::ext;
28 using namespace OHOS::Security::SecurityComponent;
29 
30 namespace {
31 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
32     LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompKitTest"};
33 
TestInCallerNotCheckList()34 static void TestInCallerNotCheckList()
35 {
36     int32_t scId = -1;
37     struct SecCompClickEvent touch;
38     std::string emptyStr = "";
39     int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, emptyStr, scId);
40     int updateRes = SecCompKit::UpdateSecurityComponent(scId, emptyStr);
41     int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, touch, nullptr);
42 
43     EXPECT_EQ(registerRes, SC_SERVICE_ERROR_CALLER_INVALID);
44     EXPECT_EQ(updateRes, SC_SERVICE_ERROR_CALLER_INVALID);
45     EXPECT_EQ(reportRes, SC_SERVICE_ERROR_CALLER_INVALID);
46 }
47 
TestInCallerCheckList()48 static void TestInCallerCheckList()
49 {
50     int32_t scId = -1;
51     struct SecCompClickEvent touch;
52     std::string emptyStr = "";
53     int registerRes = SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, emptyStr, scId);
54     int updateRes = SecCompKit::UpdateSecurityComponent(scId, emptyStr);
55     int reportRes = SecCompKit::ReportSecurityComponentClickEvent(scId, emptyStr, touch, nullptr);
56 
57     EXPECT_NE(registerRes, SC_SERVICE_ERROR_CALLER_INVALID);
58     EXPECT_NE(updateRes, SC_SERVICE_ERROR_CALLER_INVALID);
59     EXPECT_NE(reportRes, SC_SERVICE_ERROR_CALLER_INVALID);
60 }
61 }  // namespace
62 
SetUpTestCase()63 void SecCompKitTest::SetUpTestCase()
64 {
65     SC_LOG_INFO(LABEL, "SetUpTestCase.");
66 }
67 
TearDownTestCase()68 void SecCompKitTest::TearDownTestCase()
69 {
70     SC_LOG_INFO(LABEL, "TearDownTestCase.");
71 }
72 
SetUp()73 void SecCompKitTest::SetUp()
74 {
75     SC_LOG_INFO(LABEL, "SetUp ok.");
76 }
77 
TearDown()78 void SecCompKitTest::TearDown()
79 {
80     SC_LOG_INFO(LABEL, "TearDown.");
81 }
82 
83 /**
84  * @tc.name: ExceptCall001
85  * @tc.desc: do kit except call.
86  * @tc.type: FUNC
87  * @tc.require: AR000HO9IN
88  */
89 HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1)
90 {
91     LocationButton comp;
92     comp.fontSize_ = TestCommon::TEST_SIZE;
93     comp.iconSize_ = TestCommon::TEST_SIZE;
94     comp.padding_.top = TestCommon::TEST_DIMENSION;
95     comp.padding_.right = TestCommon::TEST_DIMENSION;
96     comp.padding_.bottom = TestCommon::TEST_DIMENSION;
97     comp.padding_.left = TestCommon::TEST_DIMENSION;
98     comp.textIconSpace_ = TestCommon::TEST_SIZE;
99     comp.bgColor_.value = TestCommon::TEST_COLOR;
100     comp.fontColor_.value = TestCommon::TEST_COLOR;
101     comp.iconColor_.value = TestCommon::TEST_COLOR;
102     comp.borderWidth_ = TestCommon::TEST_SIZE;
103     comp.parentEffect_ = true;
104     comp.type_ = LOCATION_COMPONENT;
105     comp.rect_.x_ = TestCommon::TEST_COORDINATE;
106     comp.rect_.y_ = TestCommon::TEST_COORDINATE;
107     comp.rect_.width_ = TestCommon::TEST_COORDINATE;
108     comp.rect_.height_ = TestCommon::TEST_COORDINATE;
109 
110     nlohmann::json jsonRes;
111     comp.ToJson(jsonRes);
112     int32_t scId = -1;
113     std::string jsonStr = jsonRes.dump();
114     ASSERT_NE(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, jsonStr, scId));
115     ASSERT_EQ(-1, scId);
116     ASSERT_NE(SC_OK, SecCompKit::UpdateSecurityComponent(scId, jsonStr));
117 
118     struct SecCompClickEvent touch = {
119         .touchX = TestCommon::TEST_COORDINATE,
120         .touchY = TestCommon::TEST_COORDINATE,
121         .timestamp = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count())
122     };
123     EXPECT_NE(SC_OK, SecCompKit::ReportSecurityComponentClickEvent(scId, jsonStr, touch, nullptr));
124     EXPECT_NE(SC_OK, SecCompKit::UnregisterSecurityComponent(scId));
125 }
126 
127 /**
128  * @tc.name: ExceptCall001
129  * @tc.desc: test caller check.
130  * @tc.type: FUNC
131  * @tc.require: AR000HO9JS
132  */
133 HWTEST_F(SecCompKitTest, TestCallerCheck001, TestSize.Level1)
134 {
135     std::vector<uintptr_t> callerList = {
136         reinterpret_cast<uintptr_t>(TestInCallerCheckList),
137     };
138     SecCompUiRegister registerCallback(callerList, nullptr);
139     TestInCallerCheckList();
140     TestInCallerNotCheckList();
141 
142     // prohibit init caller list repeately
143     std::vector<uintptr_t> callerList1 = {
144         reinterpret_cast<uintptr_t>(TestInCallerNotCheckList),
145     };
146     SecCompUiRegister registerCallback1(callerList1, nullptr);
147     TestInCallerNotCheckList();
148     SecCompCallerAuthorization::GetInstance().kitCallerList_.clear();
149     SecCompCallerAuthorization::GetInstance().isInit_ = false;
150 }
151 
152 /**
153  * @tc.name: ExceptCall002
154  * @tc.desc: test invalid caller register.
155  * @tc.type: FUNC
156  * @tc.require: AR000HO9JS
157  */
158 HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1)
159 {
160     std::vector<uintptr_t> callerList;
161     SecCompUiRegister registerCallback(callerList, nullptr);
162     TestInCallerNotCheckList();
163     SecCompCallerAuthorization::GetInstance().kitCallerList_.clear();
164     SecCompCallerAuthorization::GetInstance().isInit_ = false;
165 
166     for (size_t i = 0; i < TestCommon::MAX_CALLER_SIZE + 1; i++) {
167         callerList.emplace_back(reinterpret_cast<uintptr_t>(TestInCallerNotCheckList));
168     }
169     SecCompUiRegister registerCallback2(callerList, nullptr);
170     TestInCallerNotCheckList();
171     SecCompCallerAuthorization::GetInstance().kitCallerList_.clear();
172     SecCompCallerAuthorization::GetInstance().isInit_ = false;
173 }
174 
175 /**
176  * @tc.name: RegisterWithoutCallback001
177  * @tc.desc: test register without callback.
178  * @tc.type: FUNC
179  * @tc.require: AR000HO9JM
180  */
181 HWTEST_F(SecCompKitTest, RegisterWithoutCallback001, TestSize.Level1)
182 {
183     nlohmann::json jsonRes;
184     TestCommon::BuildLocationComponentInfo(jsonRes);
185     std::string locationInfo = jsonRes.dump();
186 
187     int32_t scId;
188 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
189     ASSERT_EQ(SC_ENHANCE_ERROR_CALLBACK_NOT_EXIST,
190         SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
191     ASSERT_EQ(-1, scId);
192 #else
193     ASSERT_EQ(SC_OK,
194         SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, locationInfo, scId));
195     ASSERT_NE(-1, scId);
196 #endif
197 }
198