diff --git a/interfaces/innerkits/appverify/test/BUILD.gn b/interfaces/innerkits/appverify/test/BUILD.gn index b784982b3cb0ea1f55f0ea8ec1b2ad81ff986ea7..2338324d40f35b0be9161539c67399f19f3f30dc 100644 --- a/interfaces/innerkits/appverify/test/BUILD.gn +++ b/interfaces/innerkits/appverify/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. +# Copyright (c) 2021 - 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 @@ -32,6 +32,7 @@ ohos_unittest("verify_test") { "unittest/src/hap_byte_buffer_test.cpp", "unittest/src/hap_cert_verify_openssl_utils_test.cpp", "unittest/src/hap_crl_manager_test.cpp", + "unittest/src/hap_profile_verify_utils_test.cpp", "unittest/src/hap_signing_block_utils_test.cpp", "unittest/src/hap_verify_openssl_utils_test.cpp", "unittest/src/hap_verify_result_test.cpp", @@ -41,6 +42,7 @@ ohos_unittest("verify_test") { "unittest/src/random_access_file_test.cpp", "unittest/src/ticket_verify_test.cpp", "unittest/src/trusted_root_ca_test.cpp", + "unittest/src/trusted_source_manager_test.cpp", "unittest/src/trusted_ticket_test.cpp", ] diff --git a/interfaces/innerkits/appverify/test/unittest/include/hap_cert_verify_openssl_utils_test.h b/interfaces/innerkits/appverify/test/unittest/include/hap_cert_verify_openssl_utils_test.h index e343f0cce6a8c4937386aba00768fbfe186981e5..5f54c106b6a5992796bffed9c0c5541e7c2d174c 100644 --- a/interfaces/innerkits/appverify/test/unittest/include/hap_cert_verify_openssl_utils_test.h +++ b/interfaces/innerkits/appverify/test/unittest/include/hap_cert_verify_openssl_utils_test.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-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 @@ -41,4 +41,6 @@ int32_t VerifyCrlTest001(void); int32_t CompareX509CertTest001(void); int32_t WriteX509CrlToStreamTest001(void); int32_t PrivateFuncInvalidInputTest001(void); +int32_t GetPublickeyBase64Test001(void); +int32_t GetOrganizationFromPemCertTest001(void); #endif // HAP_CERT_VERIFY_OPENSSL_UTILS_TEST_H diff --git a/interfaces/innerkits/appverify/test/unittest/include/hap_crl_manager_test.h b/interfaces/innerkits/appverify/test/unittest/include/hap_crl_manager_test.h index 4f43dd59d6abcd56c0df1533d0b4a6a4e2de5d20..f2870eecd0a1e4054850a64e783b4fcbd2502634 100644 --- a/interfaces/innerkits/appverify/test/unittest/include/hap_crl_manager_test.h +++ b/interfaces/innerkits/appverify/test/unittest/include/hap_crl_manager_test.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-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 @@ -20,4 +20,8 @@ #include "test_const.h" int32_t WriteAndReadCrlsFileTest001(void); +int32_t InitTest001(void); +int32_t ParseCrlsTest001(void); +int32_t ParseCrlsTest002(void); +int32_t CrlCheckTest001(void); #endif // HAP_CRL_MANAGER_TEST_H diff --git a/interfaces/innerkits/appverify/test/unittest/include/test_common.h b/interfaces/innerkits/appverify/test/unittest/include/test_common.h new file mode 100644 index 0000000000000000000000000000000000000000..adc9050f6e8f8b35f04aa76b8b8f25353eed6d3b --- /dev/null +++ b/interfaces/innerkits/appverify/test/unittest/include/test_common.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 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 HAPVERIFY_TEST_COMMON_H +#define HAPVERIFY_TEST_COMMON_H + +#include +#include +#include + +namespace OHOS { +namespace Security { +namespace Verify { +inline bool CreatTestJsonFile(const std::string& filePath, const std::string& content) +{ + std::ofstream fileStream(filePath.c_str(), std::ios::binary | std::ios::out | std::ios::trunc); + if (!fileStream.is_open()) { + return false; + } + fileStream.write(content.c_str(), content.size()); + fileStream.close(); + return true; +} + +inline bool RenameJsonFile(const std::string& oldPath, const std::string& newPath) +{ + if (rename(oldPath.c_str(), newPath.c_str()) != 0) { + return false; + } + return true; +} +} // namespace Verify +} // namespace Security +} // namespace OHOS +#endif // HAPVERIFY_TEST_COMMON_H diff --git a/interfaces/innerkits/appverify/test/unittest/include/test_const.h b/interfaces/innerkits/appverify/test/unittest/include/test_const.h index 166e41adb258a0d871a31b7bba55c2a75901d032..f033fcbac25f49b1c2f39b741a643d719cea4327 100644 --- a/interfaces/innerkits/appverify/test/unittest/include/test_const.h +++ b/interfaces/innerkits/appverify/test/unittest/include/test_const.h @@ -309,6 +309,23 @@ const std::string BASE64_CRL = "HAYDVQQDDBVIdWF3ZWkgQ0JHIFJvb3QgQ0EgRzIXDTIwMDMyNjE2NDkwNVoXDTMwMDMyNTAxMzgzM1qgLzAtMB8GA1UdIwQYMBaAFKOO" "WvVavHGMKmolcn5IkuKS3CAAMAoGA1UdFAQDAgEAMAoGCCqGSM49BAMDA2kAMGYCMQCuQOz7IvwENw/Mf1ytoXT5AHiknrrOaw/dqTuZ" "RR0wcxP5IJqI1I3Bg5WNbUmTomMCMQDIs23wc9iLgvuDOmMQia4srWunydbbslpFNfV3SIIhtyWDIPVch7RTf/s92HqnrVw="; + +const std::string APP_TRUSTED_SOURCE_FILE_PATH = "/system/etc/security/trusted_apps_sources.json"; + +const std::string APP_TRUSTED_SOURCE_BACK_UP_FILE_PATH = "/system/etc/security/trusted_apps_sources_backup.json"; + +const std::string TICKET_TRUSTED_SOURCE_FILE_PATH = "/system/etc/security/trusted_tickets_sources.json"; + +const std::string TICKET_TRUSTED_SOURCE_BACK_UP_FILE_PATH = "/system/etc/security/trusted_tickets_sources_backup.json"; + +const std::string VERSION_ERROR_TEST_JSON_STRING = R"({ + "version": {} +})"; + +const std::string TRUSTED_APP_SOURCE_ERROR_TEST_JSON_STRING = R"({ + "version": {}, + "trust-app-source": [] +})"; } // Verify } // Security } // OHOS diff --git a/interfaces/innerkits/appverify/test/unittest/src/hap_cert_verify_openssl_utils_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/hap_cert_verify_openssl_utils_test.cpp index f90d830a5f96900ca66052681d90c07a053c95bf..766ac86a6f361fd0469c5bfed49fd157e97fdcd8 100644 --- a/interfaces/innerkits/appverify/test/unittest/src/hap_cert_verify_openssl_utils_test.cpp +++ b/interfaces/innerkits/appverify/test/unittest/src/hap_cert_verify_openssl_utils_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-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 @@ -443,4 +443,30 @@ HWTEST_F(HapCertVerifyOpensslUtilsTest, PrivateFuncInvalidInputTest001, TestSize ASSERT_FALSE(HapCertVerifyOpensslUtils::CheckAsn1TimeIsValid(nullptr)); ASSERT_FALSE(HapCertVerifyOpensslUtils::CheckAsn1TypeIsValid(nullptr)); } + +/** + * @tc.name: GetPublickeyBase64Test001 + * @tc.desc: GetPublickeyBase64Test + * @tc.type: FUNC + */ +HWTEST_F(HapCertVerifyOpensslUtilsTest, GetPublickeyBase64Test001, TestSize.Level1) +{ + const X509* cert = nullptr; + std::string publicKey = ""; + bool ret = HapCertVerifyOpensslUtils::GetPublickeyBase64(cert, publicKey); + EXPECT_FALSE(ret); +} + +/** + * @tc.name: GetOrganizationFromPemCertTest001 + * @tc.desc: GetOrganizationFromPemCertTest + * @tc.type: FUNC + */ +HWTEST_F(HapCertVerifyOpensslUtilsTest, GetOrganizationFromPemCertTest001, TestSize.Level1) +{ + std::string certStr = ""; + std::string organization = ""; + bool ret = HapCertVerifyOpensslUtils::GetOrganizationFromPemCert(certStr, organization); + EXPECT_FALSE(ret); +} } diff --git a/interfaces/innerkits/appverify/test/unittest/src/hap_crl_manager_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/hap_crl_manager_test.cpp index 1d126ffffadd608e5f2b3404bb0123c8483d0882..50bb5bccaaa974cd785c377527ceca27e229b33b 100644 --- a/interfaces/innerkits/appverify/test/unittest/src/hap_crl_manager_test.cpp +++ b/interfaces/innerkits/appverify/test/unittest/src/hap_crl_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-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 @@ -18,10 +18,12 @@ #include #include +#define private public #include "init/hap_crl_manager.h" #include "util/hap_cert_verify_openssl_utils.h" #include "hap_cert_verify_openssl_utils_test.h" +#undef private using namespace testing::ext; using namespace OHOS::Security::Verify; @@ -144,4 +146,62 @@ HWTEST_F(HapCrlManagerTest, WriteAndReadCrlsFileTest001, TestSize.Level1) ASSERT_TRUE(CopyFile(HAP_CRL_TEST_BACKUP_FILE_PATH, HapCrlManager::HAP_CRL_FILE_PATH) == COPY_SUC); } } + +/** + * @tc.name: InitTest001 + * @tc.desc: InitTest + * @tc.type: FUNC + */ +HWTEST_F(HapCrlManagerTest, InitTest001, TestSize.Level1) +{ + HapCrlManager& hapCrlManager = HapCrlManager::GetInstance(); + hapCrlManager.Init(); + hapCrlManager.Init(); + EXPECT_TRUE(hapCrlManager.isInit); +} + +/** + * @tc.name: ParseCrlsTest001 + * @tc.desc: ParseCrlsTest + * @tc.type: FUNC + */ +HWTEST_F(HapCrlManagerTest, ParseCrlsTest001, TestSize.Level1) +{ + HapByteBuffer crlBuffer; + HapCrlManager& hapCrlManager = HapCrlManager::GetInstance(); + bool ret = hapCrlManager.ParseCrls(crlBuffer); + EXPECT_FALSE(ret); +} + +/** + * @tc.name: ParseCrlsTest002 + * @tc.desc: ParseCrlsTest + * @tc.type: FUNC + */ +HWTEST_F(HapCrlManagerTest, ParseCrlsTest002, TestSize.Level1) +{ + HapByteBuffer crlBuffer; + uint32_t numOfCrl = 1; + crlBuffer.SetCapacity(4); + crlBuffer.PutInt32(0, numOfCrl); + + HapCrlManager& hapCrlManager = HapCrlManager::GetInstance(); + bool ret = hapCrlManager.ParseCrls(crlBuffer); + EXPECT_FALSE(ret); +} + +/** + * @tc.name: CrlCheckTest001 + * @tc.desc: CrlCheckTest + * @tc.type: FUNC + */ +HWTEST_F(HapCrlManagerTest, CrlCheckTest001, TestSize.Level1) +{ + HapCrlManager& hapCrlManager = HapCrlManager::GetInstance(); + X509* cert = nullptr; + X509_CRL* targetCrl = nullptr; + Pkcs7Context pkcs7Context; + bool ret = hapCrlManager.CrlCheck(cert, targetCrl, pkcs7Context); + EXPECT_FALSE(ret); +} } diff --git a/interfaces/innerkits/appverify/test/unittest/src/hap_profile_verify_utils_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/hap_profile_verify_utils_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..374a990b055ec3c4d34a645641f442a639509b3e --- /dev/null +++ b/interfaces/innerkits/appverify/test/unittest/src/hap_profile_verify_utils_test.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (C) 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. + */ + +#include + +#include "util/hap_profile_verify_utils.h" + +using namespace testing::ext; +using namespace OHOS::Security::Verify; +namespace { +class HapProfileVerifyUtilsTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HapProfileVerifyUtilsTest::SetUpTestCase(void) +{ +} + +void HapProfileVerifyUtilsTest::TearDownTestCase(void) +{ +} + +void HapProfileVerifyUtilsTest::SetUp() +{ +} + +void HapProfileVerifyUtilsTest::TearDown() +{ +} + +/** + * @tc.name: ParseProfileTest001 + * @tc.desc: ParseProfileTest + * @tc.type: FUNC + */ +HWTEST_F(HapProfileVerifyUtilsTest, ParseProfileTest001, TestSize.Level1) +{ + Pkcs7Context profilePkcs7Context = {}; + Pkcs7Context hapPkcs7Context = {}; + HapByteBuffer pkcs7ProfileBlock = {}; + std::string profile = ""; + hapPkcs7Context.matchResult.matchState = MATCH_WITH_SIGN; + hapPkcs7Context.matchResult.source = APP_GALLARY; + bool ret = HapProfileVerifyUtils::ParseProfile(profilePkcs7Context, hapPkcs7Context, pkcs7ProfileBlock, profile); + EXPECT_TRUE(ret); +} + +/** + * @tc.name: ParseProfileTest002 + * @tc.desc: ParseProfileTest + * @tc.type: FUNC + */ +HWTEST_F(HapProfileVerifyUtilsTest, ParseProfileTest002, TestSize.Level1) +{ + Pkcs7Context profilePkcs7Context = {}; + Pkcs7Context hapPkcs7Context = {}; + HapByteBuffer pkcs7ProfileBlock = {}; + std::string profile = ""; + bool ret = HapProfileVerifyUtils::ParseProfile(profilePkcs7Context, hapPkcs7Context, pkcs7ProfileBlock, profile); + EXPECT_FALSE(ret); +} +} diff --git a/interfaces/innerkits/appverify/test/unittest/src/hap_signing_block_utils_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/hap_signing_block_utils_test.cpp index 2efd921d1ad4d9c6e9c7dbb4a2ab3a53ce3d8e75..032d400aebb4ddfe223497682def1544ba102270 100644 --- a/interfaces/innerkits/appverify/test/unittest/src/hap_signing_block_utils_test.cpp +++ b/interfaces/innerkits/appverify/test/unittest/src/hap_signing_block_utils_test.cpp @@ -179,6 +179,17 @@ HWTEST_F(HapSigningBlockUtilsTest, FindHapSignatureTest001, TestSize.Level1) hapFile2.write(reinterpret_cast(&errorEocdCommentLen), sizeof(errorEocdCommentLen)); hapFile2.close(); ASSERT_FALSE(hapSignBlockUtils.FindHapSignature(hapTestFile, hapSignInfo)); + /* + * @tc.steps: step5. make eocdCommentLen error, and test FindHapSignature function + * @tc.expected: step5. the return will be false. + */ + std::ofstream hapFile3; + hapFile3.open(pathFile.c_str(), std::ios::binary | std::ios::out); + ASSERT_TRUE(hapFile3.is_open()); + hapFile3.write(reinterpret_cast(&errorEocdCommentLen), sizeof(errorEocdCommentLen)); + hapFile3.close(); + signInfo.hapCentralDirOffset = 1; + ASSERT_FALSE(hapSignBlockUtils.FindHapSignature(hapTestFile, hapSignInfo)); } /** @@ -204,6 +215,13 @@ HWTEST_F(HapSigningBlockUtilsTest, VerifyHapIntegrityTest001, TestSize.Level1) hapTestFile.Init(pathFile); HapSigningBlockUtils hapSignBlockUtils; ASSERT_FALSE(hapSignBlockUtils.VerifyHapIntegrity(digestInfo, hapTestFile, signInfo)); + + Pkcs7Context digestInfo1; + SignatureInfo signInfo1; + signInfo1.hapSigningBlockOffset = -1; + RandomAccessFile hapTestFile1; + hapTestFile.Init(pathFile); + ASSERT_FALSE(hapSignBlockUtils.VerifyHapIntegrity(digestInfo1, hapTestFile1, signInfo)); } /** @@ -288,6 +306,20 @@ HWTEST_F(HapSigningBlockUtilsTest, ClassifyHapSubSigningBlock001, TestSize.Level hapSignBlockUtils.ClassifyHapSubSigningBlock(signInfo, subBlock, type); ASSERT_FALSE(signInfo.optionBlocks.empty()); ASSERT_TRUE(signInfo.optionBlocks[0].optionalType == PROPERTY_BLOB); + /* + * @tc.steps: step2. run function with input of property block + * @tc.expected: step2. the return will be false. + */ + SignatureInfo signInfo1; + HapByteBuffer subBlock1; + uint32_t type1 = HAP_SIGN_BLOB; + signInfo1.hapSignatureBlock.SetCapacity(1); + ASSERT_FALSE(hapSignBlockUtils.ClassifyHapSubSigningBlock(signInfo1, subBlock1, type1)); + + SignatureInfo signInfo2; + HapByteBuffer subBlock2; + uint32_t type2 = 0; + ASSERT_FALSE(hapSignBlockUtils.ClassifyHapSubSigningBlock(signInfo2, subBlock2, type2)); } /** @@ -348,4 +380,23 @@ HWTEST_F(HapSigningBlockUtilsTest, GetSumOfChunkDigestLenTest001, TestSize.Level TEST_ZIP_BLOCKS_NUM_NEED_DIGEST, INT_MAX, chunkCount, sumOfChunkDigestLen); ASSERT_FALSE(ret); } + +/** + * @tc.name: Test GetOptionalBlockIndex function + * @tc.desc: Test GetOptionalBlockIndex with some error inputs + * @tc.type: FUNC + */ +HWTEST_F(HapSigningBlockUtilsTest, GetOptionalBlockIndexTest001, TestSize.Level1) +{ + /* + * @tc.steps: step1. input some error input to test GetOptionalBlockIndex + * @tc.expected: step1. the return will be false. + */ + OptionalBlock optionBlock; + optionBlock.optionalType = 0; + std::vector optionBlocks = { optionBlock }; + int32_t type = 1; + int index = 0; + ASSERT_FALSE(HapSigningBlockUtils::GetOptionalBlockIndex(optionBlocks, type, index)); +} } diff --git a/interfaces/innerkits/appverify/test/unittest/src/hap_verify_v2_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/hap_verify_v2_test.cpp index dbe9642b0789be9664fe95a44387ca2bd4144ff5..375fa0f261ed3783f35015b34b7042de0e5b476c 100644 --- a/interfaces/innerkits/appverify/test/unittest/src/hap_verify_v2_test.cpp +++ b/interfaces/innerkits/appverify/test/unittest/src/hap_verify_v2_test.cpp @@ -20,10 +20,10 @@ #include #include "init/trusted_source_manager.h" +#include "interfaces/hap_verify.h" #include "provision/provision_info.h" -#include "verify/hap_verify_v2.h" - #include "test_hap_file_data.h" +#include "verify/hap_verify_v2.h" using namespace testing::ext; using namespace OHOS::Security::Verify; @@ -260,4 +260,109 @@ HWTEST_F(HapVerifyV2Test, GetDigestAndAlgorithmTest001, TestSize.Level1) digest.content.SetCapacity(TEST_FILE_BLOCK_LENGTH); ASSERT_FALSE(v2.GetDigestAndAlgorithm(digest)); } + +/** + * @tc.name: Test GetDigestAndAlgorithm function + * @tc.desc: The static function will return result of GetDigestAndAlgorithm; + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyV2Test, GetDigestAndAlgorithmTest002, TestSize.Level1) +{ + HapVerifyV2 hapVerifyV2; + Pkcs7Context digest; + digest.content.SetCapacity(TEST_FILE_BLOCK_LENGTH); + digest.digestAlgorithm = DEBUG; + ASSERT_FALSE(hapVerifyV2.GetDigestAndAlgorithm(digest)); +} + +/** + * @tc.name: Test GetDigestAndAlgorithm function + * @tc.desc: The static function will return result of GetDigestAndAlgorithm; + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyV2Test, GetDigestAndAlgorithmTest003, TestSize.Level1) +{ + HapVerifyV2 hapVerifyV2; + Pkcs7Context digest; + digest.content.SetCapacity(TEST_FILE_BLOCK_LENGTH); + digest.digestAlgorithm = RELEASE; + ASSERT_FALSE(hapVerifyV2.GetDigestAndAlgorithm(digest)); +} + +/** + * @tc.name: Test ParseHapSignatureInfo function + * @tc.desc: The static function will return result of ParseHapSignatureInfo; + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyV2Test, ParseHapSignatureInfoTest001, TestSize.Level1) +{ + std::string filePath = HAP_FILE_ECC_SIGN_BASE64; + SignatureInfo signatureInfo; + std::string standardFilePath; + ASSERT_EQ(ParseHapSignatureInfo(filePath, signatureInfo), FILE_PATH_INVALID); +} + +/** + * @tc.name: Test ParseHapSignatureInfo function + * @tc.desc: The static function will return result of ParseHapSignatureInfo; + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyV2Test, ParseHapSignatureInfoTest002, TestSize.Level1) +{ + std::string filePath = "invalid/file/path"; + SignatureInfo signatureInfo; + ASSERT_EQ(ParseHapSignatureInfo(filePath, signatureInfo), FILE_PATH_INVALID); +} + +/** + * @tc.name: Test ParseHapSignatureInfo function + * @tc.desc: The static function will return result of ParseHapSignatureInfo; + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyV2Test, ParseHapSignatureInfoTest003, TestSize.Level1) +{ + std::string filePath = "valid/path/to/file/without/signature"; + SignatureInfo signatureInfo; + ASSERT_EQ(ParseHapSignatureInfo(filePath, signatureInfo), FILE_PATH_INVALID); +} + +/** + * @tc.name: Test ParseHapProfile function + * @tc.desc: The static function will return result of ParseHapProfile; + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyV2Test, ParseHapProfileTest001, TestSize.Level1) +{ + std::string filePath = HAP_FILE_ECC_SIGN_BASE64; + HapVerifyResult hapVerifyResult; + std::string standardFilePath; + ASSERT_EQ(ParseHapProfile(filePath, hapVerifyResult), FILE_PATH_INVALID); +} + +/** + * @tc.name: Test ParseHapProfile function + * @tc.desc: The static function will return result of ParseHapProfile; + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyV2Test, ParseHapProfileTest002, TestSize.Level1) +{ + std::string filePath = HAP_FILE_ECC_SIGN_BASE64; + std::string standardFilePath; + HapVerifyResult hapVerifyResult; + ASSERT_EQ(ParseHapProfile(filePath, hapVerifyResult), FILE_PATH_INVALID); +} + +/** + * @tc.name: Test ParseHapProfile function + * @tc.desc: The static function will return result of ParseHapProfile; + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyV2Test, ParseHapProfileTest003, TestSize.Level1) +{ + std::string filePath = HAP_FILE_ECC_SIGN_BASE64; + HapVerifyResult hapVerifyResult; + SignatureInfo hapSignInfo; + hapSignInfo.hapSigningBlockOffset = DEBUG; + ASSERT_EQ(ParseHapProfile(filePath, hapVerifyResult), FILE_PATH_INVALID); +} } diff --git a/interfaces/innerkits/appverify/test/unittest/src/trusted_source_manager_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/trusted_source_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f8cb349797ff6bf412bfc824047964cb19d39fe --- /dev/null +++ b/interfaces/innerkits/appverify/test/unittest/src/trusted_source_manager_test.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (C) 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. + */ + +#include + +#define private public +#include "init/trusted_source_manager.h" +#undef private + +#include "test_common.h" +#include "test_const.h" + +using namespace testing::ext; +using namespace OHOS::Security::Verify; + +namespace { +class TrustedSourceManagerTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void TrustedSourceManagerTest::SetUpTestCase(void) {} + +void TrustedSourceManagerTest::TearDownTestCase(void) {} + +void TrustedSourceManagerTest::SetUp() {} + +void TrustedSourceManagerTest::TearDown() {} + +/** + * @tc.name: Init_0100 + * @tc.desc: Test Init function; + * @tc.type: FUNC + */ +HWTEST_F(TrustedSourceManagerTest, Init_0100, Function | MediumTest | Level1) +{ + TrustedSourceManager& trustedSourceManager = TrustedSourceManager::GetInstance(); + trustedSourceManager.isInit = true; + ASSERT_TRUE(trustedSourceManager.Init()); +} + +/** + * @tc.name: Recovery_0100 + * @tc.desc: Test Recovery function; + * @tc.type: FUNC + */ +HWTEST_F(TrustedSourceManagerTest, Recovery_0100, Function | MediumTest | Level1) +{ + TrustedSourceManager& trustedSourceManager = TrustedSourceManager::GetInstance(); + trustedSourceManager.Recovery(); + ASSERT_FALSE(trustedSourceManager.isInit); +} + +/** + * @tc.name: GetAppTrustedSources_0100 + * @tc.desc: Test GetAppTrustedSources function; + * @tc.type: FUNC + */ +HWTEST_F(TrustedSourceManagerTest, GetAppTrustedSources_0100, Function | MediumTest | Level1) +{ + TrustedSourceManager& trustedSourceManager = TrustedSourceManager::GetInstance(); + ASSERT_TRUE(RenameJsonFile(APP_TRUSTED_SOURCE_FILE_PATH, APP_TRUSTED_SOURCE_BACK_UP_FILE_PATH)); + ASSERT_FALSE(trustedSourceManager.Init()); + ASSERT_TRUE(RenameJsonFile(APP_TRUSTED_SOURCE_BACK_UP_FILE_PATH, APP_TRUSTED_SOURCE_FILE_PATH)); +} + +/** + * @tc.name: GetAppTrustedSources_0200 + * @tc.desc: Test GetAppTrustedSources function; + * @tc.type: FUNC + */ +HWTEST_F(TrustedSourceManagerTest, GetAppTrustedSources_0200, Function | MediumTest | Level1) +{ + TrustedSourceManager& trustedSourceManager = TrustedSourceManager::GetInstance(); + ASSERT_TRUE(RenameJsonFile(APP_TRUSTED_SOURCE_FILE_PATH, APP_TRUSTED_SOURCE_BACK_UP_FILE_PATH)); + ASSERT_TRUE(CreatTestJsonFile(APP_TRUSTED_SOURCE_FILE_PATH, VERSION_ERROR_TEST_JSON_STRING)); + ASSERT_FALSE(trustedSourceManager.Init()); + ASSERT_TRUE(RenameJsonFile(APP_TRUSTED_SOURCE_BACK_UP_FILE_PATH, APP_TRUSTED_SOURCE_FILE_PATH)); +} + +/** + * @tc.name: GetAppTrustedSources_0300 + * @tc.desc: Test GetAppTrustedSources function; + * @tc.type: FUNC + */ +HWTEST_F(TrustedSourceManagerTest, GetAppTrustedSources_0300, Function | MediumTest | Level1) +{ + TrustedSourceManager& trustedSourceManager = TrustedSourceManager::GetInstance(); + ASSERT_TRUE(RenameJsonFile(APP_TRUSTED_SOURCE_FILE_PATH, APP_TRUSTED_SOURCE_BACK_UP_FILE_PATH)); + ASSERT_TRUE(CreatTestJsonFile(APP_TRUSTED_SOURCE_FILE_PATH, TRUSTED_APP_SOURCE_ERROR_TEST_JSON_STRING)); + ASSERT_FALSE(trustedSourceManager.Init()); + ASSERT_TRUE(RenameJsonFile(APP_TRUSTED_SOURCE_BACK_UP_FILE_PATH, APP_TRUSTED_SOURCE_FILE_PATH)); +} +} // namespace diff --git a/interfaces/innerkits/appverify/test/unittest/src/trusted_ticket_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/trusted_ticket_test.cpp index 859332719a50cd421d9cdb2f9f6b639b36d41fea..b15fc06eaed80209cdad9fa16814b9e98236f8b2 100644 --- a/interfaces/innerkits/appverify/test/unittest/src/trusted_ticket_test.cpp +++ b/interfaces/innerkits/appverify/test/unittest/src/trusted_ticket_test.cpp @@ -17,7 +17,12 @@ #include +#define private public #include "init/trusted_ticket_manager.h" +#undef private + +#include "test_common.h" +#include "test_const.h" using namespace testing::ext; using namespace OHOS::Security::Verify; @@ -171,4 +176,58 @@ HWTEST_F(TrustedTicketTest, IsTrustedSourceTest002, TestSize.Level1) std::string certSubject9 = "C=CN, O=Other, OU=MemberCenter, CN=MemberShip ticket Release V9a"; ASSERT_TRUE(trustedticketsource.IsTrustedSource(certSubject9, certIssuer, 3).matchState == DO_NOT_MATCH); } + +/** + * @tc.name: Init_0100 + * @tc.desc: Test Init function; + * @tc.type: FUNC + */ +HWTEST_F(TrustedTicketTest, Init_0100, Function | MediumTest | Level1) +{ + TrustedTicketManager& trustedTicketManager = TrustedTicketManager::GetInstance(); + trustedTicketManager.isInit = true; + ASSERT_TRUE(trustedTicketManager.Init()); + trustedTicketManager.Recovery(); +} + +/** + * @tc.name: GetTicketTrustedSources_0100 + * @tc.desc: Test GetTicketTrustedSources function; + * @tc.type: FUNC + */ +HWTEST_F(TrustedTicketTest, GetTicketTrustedSources_0100, Function | MediumTest | Level1) +{ + TrustedTicketManager& trustedTicketManager = TrustedTicketManager::GetInstance(); + ASSERT_TRUE(RenameJsonFile(TICKET_TRUSTED_SOURCE_FILE_PATH, TICKET_TRUSTED_SOURCE_BACK_UP_FILE_PATH)); + ASSERT_FALSE(trustedTicketManager.Init()); + ASSERT_TRUE(RenameJsonFile(TICKET_TRUSTED_SOURCE_BACK_UP_FILE_PATH, TICKET_TRUSTED_SOURCE_FILE_PATH)); +} + +/** + * @tc.name: GetTicketTrustedSources_0200 + * @tc.desc: Test GetTicketTrustedSources function; + * @tc.type: FUNC + */ +HWTEST_F(TrustedTicketTest, GetTicketTrustedSources_0200, Function | MediumTest | Level1) +{ + TrustedTicketManager& trustedTicketManager = TrustedTicketManager::GetInstance(); + ASSERT_TRUE(RenameJsonFile(TICKET_TRUSTED_SOURCE_FILE_PATH, TICKET_TRUSTED_SOURCE_BACK_UP_FILE_PATH)); + ASSERT_TRUE(CreatTestJsonFile(TICKET_TRUSTED_SOURCE_FILE_PATH, VERSION_ERROR_TEST_JSON_STRING)); + ASSERT_FALSE(trustedTicketManager.Init()); + ASSERT_TRUE(RenameJsonFile(TICKET_TRUSTED_SOURCE_BACK_UP_FILE_PATH, TICKET_TRUSTED_SOURCE_FILE_PATH)); +} + +/** + * @tc.name: GetTicketTrustedSources_0300 + * @tc.desc: Test GetTicketTrustedSources function; + * @tc.type: FUNC + */ +HWTEST_F(TrustedTicketTest, GetTicketTrustedSources_0300, Function | MediumTest | Level1) +{ + TrustedTicketManager& trustedTicketManager = TrustedTicketManager::GetInstance(); + ASSERT_TRUE(RenameJsonFile(TICKET_TRUSTED_SOURCE_FILE_PATH, TICKET_TRUSTED_SOURCE_BACK_UP_FILE_PATH)); + ASSERT_TRUE(CreatTestJsonFile(TICKET_TRUSTED_SOURCE_FILE_PATH, TRUSTED_APP_SOURCE_ERROR_TEST_JSON_STRING)); + ASSERT_FALSE(trustedTicketManager.Init()); + ASSERT_TRUE(RenameJsonFile(TICKET_TRUSTED_SOURCE_BACK_UP_FILE_PATH, TICKET_TRUSTED_SOURCE_FILE_PATH)); +} }