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_enhance_adapter_test.h"
17 #include <unistd.h>
18 #include "sec_comp_err.h"
19 #include "sec_comp_log.h"
20 #include "sec_comp_info.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::Security::SecurityComponent;
24 
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
27     LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEnhanceAdapterTest"};
28 static constexpr uint32_t SEC_COMP_ENHANCE_CFG_SIZE = 76;
29 static constexpr uint32_t MAX_HMAC_SIZE = 64;
30 }  // namespace
31 
SetUpTestCase()32 void SecCompEnhanceAdapterTest::SetUpTestCase()
33 {
34     SC_LOG_INFO(LABEL, "SetUpTestCase.");
35 }
36 
TearDownTestCase()37 void SecCompEnhanceAdapterTest::TearDownTestCase()
38 {
39     SC_LOG_INFO(LABEL, "TearDownTestCase.");
40 }
41 
SetUp()42 void SecCompEnhanceAdapterTest::SetUp()
43 {
44     SC_LOG_INFO(LABEL, "SetUp ok.");
45 }
46 
TearDown()47 void SecCompEnhanceAdapterTest::TearDown()
48 {
49     SC_LOG_INFO(LABEL, "TearDown.");
50 }
51 
52 /**
53  * @tc.name: EnhanceAdapter001
54  * @tc.desc: test enhance adapter fail
55  * @tc.type: FUNC
56  * @tc.require:
57  */
58 HWTEST_F(SecCompEnhanceAdapterTest, EnhanceAdapter001, TestSize.Level1)
59 {
60     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
61     EXPECT_EQ(SecCompEnhanceAdapter::EnableInputEnhance(), SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE);
62     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
63     EXPECT_EQ(SecCompEnhanceAdapter::DisableInputEnhance(), SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE);
64 
65     SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
66     uint8_t cfgData[SEC_COMP_ENHANCE_CFG_SIZE] = { 0 };
67     EXPECT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE,
68         SecCompEnhanceAdapter::SetEnhanceCfg(cfgData, SEC_COMP_ENHANCE_CFG_SIZE));
69 
70     SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
71     uint8_t originData[MAX_HMAC_SIZE] = { 0 };
72     uint32_t enHancedataLen = MAX_HMAC_SIZE;
73     EXPECT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE,
74         SecCompEnhanceAdapter::GetPointerEventEnhanceData(originData, MAX_HMAC_SIZE, nullptr, enHancedataLen));
75 
76     SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
77     SecCompClickEvent touchInfo;
78     ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE, SecCompEnhanceAdapter::CheckExtraInfo(touchInfo));
79 
80     SecCompEnhanceAdapter::isEnhanceClientHandlerInit = false;
81     std::string componentInfo;
82     SecCompEnhanceAdapter::EnhanceDataPreprocess(1, componentInfo);
83 
84     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
85     ASSERT_EQ(nullptr, SecCompEnhanceAdapter::GetEnhanceRemoteObject());
86 
87     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
88     SecCompEnhanceAdapter::StartEnhanceService();
89     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
90     SecCompEnhanceAdapter::ExitEnhanceService();
91     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
92     SecCompEnhanceAdapter::NotifyProcessDied(0);
93     SecCompEnhanceAdapter::isEnhanceClientHandlerInit = false;
94     SecCompEnhanceAdapter::RegisterScIdEnhance(0);
95     SecCompEnhanceAdapter::isEnhanceClientHandlerInit = false;
96     SecCompEnhanceAdapter::UnregisterScIdEnhance(0);
97     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
98     SecCompEnhanceAdapter::AddSecurityComponentProcess(0);
99     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
100 
101     OHOS::MessageParcel input;
102     OHOS::MessageParcel output;
103     OHOS::MessageParcel reply;
104     SecCompEnhanceAdapter::isEnhanceClientHandlerInit = false;
105     SecCompEnhanceAdapter::EnhanceClientSerialize(input, output);
106     SecCompEnhanceAdapter::isEnhanceClientHandlerInit = false;
107     SecCompEnhanceAdapter::EnhanceClientDeserialize(input, output);
108     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
109     SecCompEnhanceAdapter::EnhanceSrvSerialize(input, output);
110     SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
111     SecCompEnhanceAdapter::EnhanceSrvDeserialize(input, output, reply);
112     std::shared_ptr<SecCompBase> compInfo;
113     const nlohmann::json jsonComponent;
114     ASSERT_EQ(SC_OK, SecCompEnhanceAdapter::CheckComponentInfoEnhance(0, compInfo, jsonComponent));
115 }
116