From 4e63a25b7d8bac652b42175b602c23380ce213c7 Mon Sep 17 00:00:00 2001 From: zhenghui Date: Wed, 14 Aug 2024 19:08:34 +0800 Subject: [PATCH] =?UTF-8?q?tdd=E7=94=A8=E4=BE=8B=E8=A1=A5=E5=81=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhenghui --- .../include/local_code_sign_client.h | 2 +- .../src/local_code_sign_client.cpp | 9 ++ test/unittest/BUILD.gn | 3 + test/unittest/code_sign_utils_test.cpp | 30 ++++ test/unittest/local_code_sign_test.cpp | 17 +++ .../local_code_sign_utils_mock_test.cpp | 125 +++++++++++++++ .../mock/include/byte_buffer_mock_helper.h | 142 ++++++++++++++++++ test/unittest/mock/src/hks_api_mock_test.cpp | 2 +- utils/include/fsverity_utils_helper.h | 2 +- 9 files changed, 329 insertions(+), 3 deletions(-) create mode 100644 test/unittest/mock/include/byte_buffer_mock_helper.h diff --git a/interfaces/innerkits/local_code_sign/include/local_code_sign_client.h b/interfaces/innerkits/local_code_sign/include/local_code_sign_client.h index a20e609..b5552e0 100644 --- a/interfaces/innerkits/local_code_sign/include/local_code_sign_client.h +++ b/interfaces/innerkits/local_code_sign/include/local_code_sign_client.h @@ -49,7 +49,7 @@ private: }; LocalCodeSignClient(); - ~LocalCodeSignClient() = default; + ~LocalCodeSignClient(); LocalCodeSignClient(const LocalCodeSignClient &source) = delete; LocalCodeSignClient &operator = (const LocalCodeSignClient &source) = delete; diff --git a/interfaces/innerkits/local_code_sign/src/local_code_sign_client.cpp b/interfaces/innerkits/local_code_sign/src/local_code_sign_client.cpp index 10abb5b..b599ece 100644 --- a/interfaces/innerkits/local_code_sign/src/local_code_sign_client.cpp +++ b/interfaces/innerkits/local_code_sign/src/local_code_sign_client.cpp @@ -36,6 +36,15 @@ LocalCodeSignClient::LocalCodeSignClient() } } +LocalCodeSignClient::~LocalCodeSignClient() +{ + std::lock_guard lock(proxyMutex_); + if (localCodeSignProxy_ != nullptr && localCodeSignSvrRecipient_ != nullptr) { + localCodeSignProxy_->AsObject()->RemoveDeathRecipient(localCodeSignSvrRecipient_); + } + localCodeSignSvrRecipient_ = nullptr; +} + void LocalCodeSignClient::LocalCodeSignSvrRecipient::OnRemoteDied(const wptr &remote) { if (remote == nullptr) { diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index c8c42dc..4c9e6ba 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -180,6 +180,7 @@ ohos_unittest("local_code_sign_utils_mock_unittest") { module_out_path = "security/code_signature" resource_config_file = "resources/ohos_test.xml" sources = [ + "${code_signature_root_dir}/services/key_enable/utils/src/cert_path.cpp", "${code_signature_root_dir}/services/local_code_sign/src/local_sign_key.cpp", "${code_signature_root_dir}/utils/src/cert_utils.cpp", "local_code_sign_utils_mock_test.cpp", @@ -192,6 +193,7 @@ ohos_unittest("local_code_sign_utils_mock_unittest") { "mock/include", "utils/include", "${code_signature_root_dir}/services/local_code_sign/include", + "${code_signature_root_dir}/services/key_enable/utils/include", ] configs = [ "${code_signature_root_dir}:common_utils_config" ] @@ -200,6 +202,7 @@ ohos_unittest("local_code_sign_utils_mock_unittest") { "fsverity-utils:libfsverity_utils", "hilog:libhilog", "huks:libhukssdk", + "init:libbegetutil", "openssl:libcrypto_shared", ] } diff --git a/test/unittest/code_sign_utils_test.cpp b/test/unittest/code_sign_utils_test.cpp index 5494331..b89812e 100644 --- a/test/unittest/code_sign_utils_test.cpp +++ b/test/unittest/code_sign_utils_test.cpp @@ -787,6 +787,36 @@ HWTEST_F(CodeSignUtilsTest, CodeSignUtilsTest_0032, TestSize.Level0) int32_t ret = codeSignBlock.ProcessExtension(extensionAddr, blockAddrEnd, arg); EXPECT_EQ(ret, CS_ERR_INVALID_PAGE_INFO_EXTENSION); } + +/** + * @tc.name: CodeSignUtilsTest_0033 + * @tc.desc: enable code signature for app, entryPath is nullptr + * @tc.type: Func + * @tc.require: + */ +HWTEST_F(CodeSignUtilsTest, CodeSignUtilsTest_0033, TestSize.Level0) +{ + EntryMap entryPath = {}; + int32_t ret = CodeSignUtils::EnforceCodeSignForApp(entryPath, g_sigWithMultiLibRetSucPath); + EXPECT_EQ(ret, CS_SUCCESS); +} + +/** + * @tc.name: CodeSignUtilsTest_0034 + * @tc.desc: enable code signature for file successfully, path is O_WRONLY + * @tc.type: Func + * @tc.require: + */ +HWTEST_F(CodeSignUtilsTest, CodeSignUtilsTest_0034, TestSize.Level0) +{ + ByteBuffer buffer; + bool flag = ReadSignatureFromFile(g_filesigEnablePath, buffer); + EXPECT_EQ(flag, true); + + open(g_filesigEnablePath.c_str(), O_WRONLY); + int32_t ret = CodeSignUtils::EnforceCodeSignForFile(g_fileEnableSuc, buffer); + EXPECT_EQ(ret, CS_SUCCESS); +} } // namespace CodeSign } // namespace Security } // namespace OHOS diff --git a/test/unittest/local_code_sign_test.cpp b/test/unittest/local_code_sign_test.cpp index 57c3af8..3e546b5 100644 --- a/test/unittest/local_code_sign_test.cpp +++ b/test/unittest/local_code_sign_test.cpp @@ -291,6 +291,23 @@ HWTEST_F(LocalCodeSignTest, LocalCodeSignTest_0015, TestSize.Level0) LocalCodeSignKit::SignLocalCode(ownerID, DEMO_AN_PATH2, sig); NativeTokenReset(selfTokenId); } + +/** + * @tc.name: LocalCodeSignTest_0016 + * @tc.desc: load sa success and return remote object is not null + * @tc.type: Func + * @tc.require: + */ +HWTEST_F(LocalCodeSignTest, LocalCodeSignTest_0016, TestSize.Level0) +{ + LocalCodeSignLoadCallback cb; + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_NE(systemAbilityManager, nullptr); + sptr remoteObject = + systemAbilityManager->GetSystemAbility(LOCAL_CODE_SIGN_SA_ID); + cb.OnLoadSystemAbilitySuccess(LOCAL_CODE_SIGN_SA_ID, remoteObject); +} } // namespace CodeSign } // namespace Security } // namespace OHOS diff --git a/test/unittest/local_code_sign_utils_mock_test.cpp b/test/unittest/local_code_sign_utils_mock_test.cpp index d46a100..f7b897f 100644 --- a/test/unittest/local_code_sign_utils_mock_test.cpp +++ b/test/unittest/local_code_sign_utils_mock_test.cpp @@ -24,6 +24,9 @@ #include "log.h" #include "pkcs7_generator.h" #include "hks_api.h" +#include "byte_buffer_mock_helper.h" +#include "cert_path.h" + using namespace OHOS::Security::CodeSign; using namespace testing::ext; @@ -147,6 +150,128 @@ HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0003, TestSize.L int32_t iRet = key.GetFormattedCertChain(*challenge); EXPECT_EQ(iRet, 0); } + +/** + * @tc.name: LocalCodeSignUtilsMockTest_0004 + * @tc.desc: LocalSignKey GetSignCert test + * @tc.type: Func + * @tc.require: issueI8FCGF + */ +HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0004, TestSize.Level0) +{ + LocalSignKey &key = LocalSignKey::GetInstance(); + byte_type = 0; + (void)key.GetSignCert(); + byte_type = 1; + (void)key.GetSignCert(); +} + +/** + * @tc.name: LocalCodeSignUtilsMockTest_0005 + * @tc.desc: cert_utils FreeCertChain certChain is nullptr or certChain->certs is nullptr + * @tc.type: Func + * @tc.require: issueI8FCGF + */ +HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0005, TestSize.Level0) +{ + struct HksCertChain *certChain = nullptr; + uint32_t pos = 0; + (void)OHOS::Security::CodeSign::FreeCertChain(&certChain, pos); + + certChain = static_cast(malloc(sizeof(struct HksCertChain))); + certChain->certs = nullptr; + (void)OHOS::Security::CodeSign::FreeCertChain(&certChain, pos); +} + +/** + * @tc.name: LocalCodeSignUtilsMockTest_0006 + * @tc.desc: cert_utils CheckChallengeSize func test + * @tc.type: Func + * @tc.require: issueI8FCGF + */ +HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0006, TestSize.Level0) +{ + uint32_t size = 0; + bool bRet = OHOS::Security::CodeSign::CheckChallengeSize(size); + EXPECT_EQ(bRet, true); + + size = 33; + bRet = OHOS::Security::CodeSign::CheckChallengeSize(size); + EXPECT_EQ(bRet, false); +} + +/** + * @tc.name: LocalCodeSignUtilsMockTest_0007 + * @tc.desc: cert_utils FormattedCertChain func test + * @tc.type: Func + * @tc.require: issueI8FCGF + */ +HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0007, TestSize.Level0) +{ + const HksCertChain *certChain = LocalSignKey::GetInstance().GetCertChain(); + ByteBufferMockHelper buffer; + byte_type = PUTDATA; + bool bRet = OHOS::Security::CodeSign::FormattedCertChain(certChain, buffer); + EXPECT_EQ(bRet, false); +} + +/** + * @tc.name: LocalCodeSignUtilsMockTest_0008 + * @tc.desc: cert_utils GetCertChainFormBuffer func test + * @tc.type: Func + * @tc.require: issueI8FCGF + */ +HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0008, TestSize.Level0) +{ + ByteBufferMockHelper certChainBuffer; + ByteBufferMockHelper signCert; + ByteBufferMockHelper issuer; + std::vector chain; + byte_type = 0; + bool bRet = OHOS::Security::CodeSign::GetCertChainFormBuffer(certChainBuffer, signCert, issuer, chain); + EXPECT_EQ(bRet, false); + + byte_type = GETBUFFER; + bRet = OHOS::Security::CodeSign::GetCertChainFormBuffer(certChainBuffer, signCert, issuer, chain); + EXPECT_EQ(bRet, false); + + byte_type = GETSIZE; + bRet = OHOS::Security::CodeSign::GetCertChainFormBuffer(certChainBuffer, signCert, issuer, chain); + EXPECT_EQ(bRet, false); +} + +/** + * @tc.name: LocalCodeSignUtilsMockTest_0009 + * @tc.desc: FsverityUtilsHelper GetCertChainFormBuffer func test + * @tc.type: Func + * @tc.require: issueI8FCGF + */ +HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0009, TestSize.Level0) +{ + (void)FsverityUtilsHelper::GetInstance().ErrorMsgLogCallback(nullptr); +} + +/** + * @tc.name: LocalCodeSignUtilsMockTest_0010 + * @tc.desc: cert_path IsDeveloperModeOn func test + * @tc.type: Func + * @tc.require: issueI8FCGF + */ +HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0010, TestSize.Level0) +{ + EXPECT_EQ(IsDeveloperModeOn(), false); +} + +/** + * @tc.name: LocalCodeSignUtilsMockTest_0011 + * @tc.desc: cert_path CodeSignGetUdid func test + * @tc.type: Func + * @tc.require: issueI8FCGF + */ +HWTEST_F(LocalCodeSignUtilsMockTest, LocalCodeSignUtilsMockTest_0011, TestSize.Level0) +{ + EXPECT_EQ(CodeSignGetUdid(nullptr), false); +} } // namespace CodeSign } // namespace Security } // namespace OHOS diff --git a/test/unittest/mock/include/byte_buffer_mock_helper.h b/test/unittest/mock/include/byte_buffer_mock_helper.h new file mode 100644 index 0000000..ca27ee5 --- /dev/null +++ b/test/unittest/mock/include/byte_buffer_mock_helper.h @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CODE_SIGN_BYTE_BUFFER_MOCK_HELPER_H +#define CODE_SIGN_BYTE_BUFFER_MOCK_HELPER_H + +#include "byte_buffer.h" + +namespace OHOS { +namespace Security { +namespace CodeSign { +enum Byte_Buffer { + COPYFORM = 1, + PUTDATA = 2, + RESIZE = 3, + GETBUFFER = 4, + GETSIZE = 5, + EMPTY = 6, +}; +int byte_type = 0; +class ByteBufferMockHelper : public ByteBuffer { +public: + ByteBufferMockHelper(): data(nullptr), size(0) + { + } + + ByteBufferMockHelper(uint32_t bufferSize): data(nullptr), size(0) + { + Init(bufferSize); + } + + ByteBufferMockHelper(const ByteBufferMockHelper &other): data(nullptr), size(0) + { + CopyFrom(other.GetBuffer(), other.GetSize()); + } + + ~ByteBufferMockHelper() + { + if (data != nullptr) { + data.reset(nullptr); + data = nullptr; + } + size = 0; + } + bool CopyFrom(const uint8_t *srcData, uint32_t srcSize) + { + if (byte_type == COPYFORM) { + return false; + } + if (srcData == nullptr) { + return false; + } + if (!Resize(srcSize)) { + return false; + } + if (memcpy_s(data.get(), size, srcData, srcSize) != EOK) { + return false; + } + return true; + } + + bool PutData(uint32_t pos, const uint8_t *srcData, uint32_t srcSize) + { + if (byte_type == PUTDATA) { + return false; + } + if (pos >= size) { + return false; + } + if (memcpy_s(data.get() + pos, size - pos, srcData, srcSize) != EOK) { + return false; + } + return true; + } + + bool Resize(uint32_t newSize) + { + if (byte_type == RESIZE) { + return false; + } + if (data != nullptr) { + data.reset(nullptr); + } + return Init(newSize); + } + + uint8_t *GetBuffer() const + { + if (byte_type == GETBUFFER) { + return 0; + } + return data.get(); + } + + uint32_t GetSize() const + { + if (byte_type == GETSIZE) { + return false; + } + return size; + } + + bool Empty() const + { + if (byte_type == EMPTY) { + return false; + } + return (size == 0) || (data == nullptr); + } +private: + bool Init(uint32_t bufferSize) + { + if (bufferSize == 0) { + return false; + } + data = std::make_unique(bufferSize); + if (data == nullptr) { + return false; + } + size = bufferSize; + return true; + } + + std::unique_ptr data; + uint32_t size; +}; +} +} +} +#endif \ No newline at end of file diff --git a/test/unittest/mock/src/hks_api_mock_test.cpp b/test/unittest/mock/src/hks_api_mock_test.cpp index 01bf6be..74df370 100644 --- a/test/unittest/mock/src/hks_api_mock_test.cpp +++ b/test/unittest/mock/src/hks_api_mock_test.cpp @@ -53,7 +53,7 @@ int32_t HksGenerateKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn, struct HksParamSet *paramSetOut) { LOG_INFO("Mock HksGenerateKey"); - if (g_count == GENERATEKEY) { + if (g_count == GENERATEKEY || g_count == ERROR) { return -1; } return HKS_SUCCESS; diff --git a/utils/include/fsverity_utils_helper.h b/utils/include/fsverity_utils_helper.h index 5c2bbe0..33f7286 100644 --- a/utils/include/fsverity_utils_helper.h +++ b/utils/include/fsverity_utils_helper.h @@ -36,6 +36,7 @@ class FsverityUtilsHelper { public: static FsverityUtilsHelper &GetInstance(); bool GenerateFormattedDigest(const char *path, ByteBuffer &ret); + static void ErrorMsgLogCallback(const char *msg); private: FsverityUtilsHelper(); @@ -47,7 +48,6 @@ private: void Init(); bool ComputeDigest(const char *path, struct libfsverity_digest **digest); bool FormatDigest(libfsverity_digest *digest, uint8_t *buffer); - static void ErrorMsgLogCallback(const char *msg); class FileReader { public: -- Gitee