• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #include "sec_comp_enhance_test.h"
16 #include <dlfcn.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, "SecCompEnhanceTest"};
28 static bool g_inputEnhanceExist = false;
29 static bool g_srvEnhanceExist = false;
30 static constexpr uint32_t SEC_COMP_ENHANCE_CFG_SIZE = 76;
31 static const std::string ENHANCE_INPUT_INTERFACE_LIB = "libsecurity_component_client_enhance.z.so";
32 static const std::string ENHANCE_SRV_INTERFACE_LIB = "libsecurity_component_service_enhance.z.so";
33 static constexpr uint32_t MAX_HMAC_SIZE = 64;
34 }  // namespace
35 
SetUpTestCase()36 void SecCompEnhanceTest::SetUpTestCase()
37 {
38     void *handle = dlopen(ENHANCE_INPUT_INTERFACE_LIB.c_str(), RTLD_LAZY);
39     if (handle != nullptr) {
40         g_inputEnhanceExist = true;
41     }
42     dlclose(handle);
43 
44     handle = dlopen(ENHANCE_SRV_INTERFACE_LIB.c_str(), RTLD_LAZY);
45     if (handle != nullptr) {
46         g_srvEnhanceExist = true;
47     }
48     dlclose(handle);
49     system("kill -9 `pidof security_component_service`");
50     SC_LOG_INFO(LABEL, "SetUpTestCase.");
51 }
52 
TearDownTestCase()53 void SecCompEnhanceTest::TearDownTestCase()
54 {
55     SC_LOG_INFO(LABEL, "TearDownTestCase.");
56 }
57 
SetUp()58 void SecCompEnhanceTest::SetUp()
59 {
60     SC_LOG_INFO(LABEL, "SetUp ok.");
61 }
62 
TearDown()63 void SecCompEnhanceTest::TearDown()
64 {
65     SC_LOG_INFO(LABEL, "TearDown.");
66 }
67 
68 /**
69  * @tc.name: SetEnhanceCfg001
70  * @tc.desc: test SetEnhanceCfg
71  * @tc.type: FUNC
72  * @tc.require: AR000HO9IN
73  */
74 HWTEST_F(SecCompEnhanceTest, SetEnhanceCfg001, TestSize.Level1)
75 {
76     uint8_t cfgData[SEC_COMP_ENHANCE_CFG_SIZE] = { 0 };
77     int32_t result = SecCompEnhanceKit::SetEnhanceCfg(cfgData, SEC_COMP_ENHANCE_CFG_SIZE);
78     if (g_inputEnhanceExist) {
79         EXPECT_EQ(result, SC_OK);
80     } else {
81         EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE);
82     }
83 }
84 
85 /**
86  * @tc.name: GetPoniterEventEnhanceData001
87  * @tc.desc: test GetPoniterEventEnhanceData
88  * @tc.type: FUNC
89  * @tc.require: AR000HO9IN
90  */
91 HWTEST_F(SecCompEnhanceTest, GetPoniterEventEnhanceData001, TestSize.Level1)
92 {
93     uint8_t originData[16] = { 0 };
94     uint32_t dataLen = 16;
95     uint8_t* enhanceData = nullptr;
96     uint32_t enHancedataLen = MAX_HMAC_SIZE;
97 
98     int32_t result = SecCompEnhanceKit::GetPointerEventEnhanceData(originData, dataLen, enhanceData, enHancedataLen);
99     if (g_inputEnhanceExist) {
100         EXPECT_EQ(result, SC_SERVICE_ERROR_SERVICE_NOT_EXIST);
101     } else {
102         EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE);
103     }
104 }
105