From abea4b4a0e8754d98feb13671c4c7188078316bf Mon Sep 17 00:00:00 2001 From: lcc Date: Wed, 5 Jun 2024 10:32:55 +0800 Subject: [PATCH] =?UTF-8?q?TDD=E8=A6=86=E7=9B=96=E7=8E=87=20=E6=8F=90?= =?UTF-8?q?=E5=8D=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lcc --- test/unittest/BUILD.gn | 2 + ...ypto_dh_asy_key_generator_by_spec_test.cpp | 6 +- test/unittest/src/crypto_hkdf_test.cpp | 6 + test/unittest/src/crypto_key_utils_test.cpp | 145 ++++++++++++++++++ .../src/crypto_openssl_common_test.cpp | 78 ++++++++++ test/unittest/src/crypto_pbkdf2_test.cpp | 6 + 6 files changed, 238 insertions(+), 5 deletions(-) create mode 100644 test/unittest/src/crypto_key_utils_test.cpp create mode 100644 test/unittest/src/crypto_openssl_common_test.cpp diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index add2867..5af30b2 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -99,6 +99,8 @@ ohos_unittest("crypto_framework_test") { "src/crypto_x25519_asy_key_generator_by_spec_test.cpp", "src/crypto_x25519_asy_key_generator_test.cpp", "src/crypto_x25519_key_agreement_test.cpp", + "src/crypto_key_utils_test.cpp", + "src/crypto_openssl_common_test.cpp", "src/ecc/crypto_ecc_asy_key_generator_by_spec_sub_four_test.cpp", "src/ecc/crypto_ecc_asy_key_generator_by_spec_sub_one_test.cpp", "src/ecc/crypto_ecc_asy_key_generator_by_spec_sub_three_test.cpp", diff --git a/test/unittest/src/crypto_dh_asy_key_generator_by_spec_test.cpp b/test/unittest/src/crypto_dh_asy_key_generator_by_spec_test.cpp index 5e183d5..6396a12 100644 --- a/test/unittest/src/crypto_dh_asy_key_generator_by_spec_test.cpp +++ b/test/unittest/src/crypto_dh_asy_key_generator_by_spec_test.cpp @@ -1552,15 +1552,11 @@ HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest046 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest047, TestSize.Level0) { + HcfDhKeyUtilErrBranch(); HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH10001, SKLEN_EQZERO); ASSERT_EQ(res, HCF_ERR_MALLOC); res = HcfDhKeyUtilCreateTest(PLEN_DH511, SKLEN_EQZERO); ASSERT_EQ(res, HCF_ERR_MALLOC); } - -HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorErrTest01, TestSize.Level0) -{ - HcfDhKeyUtilErrBranch(); -} } \ No newline at end of file diff --git a/test/unittest/src/crypto_hkdf_test.cpp b/test/unittest/src/crypto_hkdf_test.cpp index 8dded31..f6027dc 100644 --- a/test/unittest/src/crypto_hkdf_test.cpp +++ b/test/unittest/src/crypto_hkdf_test.cpp @@ -596,4 +596,10 @@ HWTEST_F(CryptoHkdfTest, CryptoHkdfErr11, TestSize.Level0) ASSERT_EQ(algName, nullptr); HcfObjDestroy(generator); } + +HWTEST_F(CryptoHkdfTest, CryptoHkdfErr12, TestSize.Level0) +{ + HcfResult ret = HcfKdfHkdfSpiCreate(nullptr, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} } diff --git a/test/unittest/src/crypto_key_utils_test.cpp b/test/unittest/src/crypto_key_utils_test.cpp new file mode 100644 index 0000000..325418f --- /dev/null +++ b/test/unittest/src/crypto_key_utils_test.cpp @@ -0,0 +1,145 @@ +/* + * 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 "key_utils.h" +#include "ecc_key_util.h" + +#include +#include "blob.h" +#include "memory.h" +#include "memory_mock.h" +#include "openssl_common.h" +#include "detailed_ecc_key_params.h" +#include "ecc_common.h" +#include "ecc_openssl_common_param_spec.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoKeyUtilsTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void CryptoKeyUtilsTest::SetUpTestCase() {} +void CryptoKeyUtilsTest::TearDownTestCase() {} +void CryptoKeyUtilsTest::SetUp() {} +void CryptoKeyUtilsTest::TearDown() {} + +static const bool IS_BIG_ENDIAN = IsBigEndian(); + +static HcfResult ConstructEcc224CommParamsSpec(HcfEccCommParamsSpec **spec) +{ + HcfEccCommParamsSpec *eccCommSpec = + reinterpret_cast(HcfMalloc(sizeof(HcfEccCommParamsSpec), 0)); + if (eccCommSpec == nullptr) { + return HCF_ERR_MALLOC; + } + HcfECFieldFp *tmpField = reinterpret_cast(HcfMalloc(sizeof(HcfECFieldFp), 0)); + if (tmpField == nullptr) { + HcfFree(eccCommSpec); + return HCF_ERR_MALLOC; + } + + eccCommSpec->base.algName = const_cast(g_eccAlgName.c_str()); + eccCommSpec->base.specType = HCF_COMMON_PARAMS_SPEC; + eccCommSpec->field = reinterpret_cast(tmpField); + eccCommSpec->field->fieldType = const_cast(g_eccFieldType.c_str()); + ((HcfECFieldFp *)(eccCommSpec->field))->p.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigP : g_ecc224CorrectLittleP); + ((HcfECFieldFp *)(eccCommSpec->field))->p.len = NID_secp224r1_len; + eccCommSpec->a.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigA : g_ecc224CorrectLittleA); + eccCommSpec->a.len = NID_secp224r1_len; + eccCommSpec->b.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigB : g_ecc224CorrectLittleB); + eccCommSpec->b.len = NID_secp224r1_len; + eccCommSpec->g.x.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigGX : g_ecc224CorrectLittleGX); + eccCommSpec->g.x.len = NID_secp224r1_len; + eccCommSpec->g.y.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigGY : g_ecc224CorrectLittleGY); + eccCommSpec->g.y.len = NID_secp224r1_len; + eccCommSpec->n.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigN : g_ecc224CorrectLittleN); + eccCommSpec->n.len = NID_secp224r1_len; + eccCommSpec->h = g_ecc224CorrectH; + + *spec = eccCommSpec; + return HCF_SUCCESS; +} + +HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest01, TestSize.Level0) +{ + HcfResult ret = CopyAsyKeyParamsSpec(nullptr, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest02, TestSize.Level0) +{ + HcfEccCommParamsSpec *srcSpec = nullptr; + HcfResult ret = ConstructEcc224CommParamsSpec(&srcSpec); + EXPECT_EQ(ret, HCF_SUCCESS); + + ret = CopyPoint(&(srcSpec->g), nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest03, TestSize.Level0) +{ + HcfEccCommParamsSpec *srcSpec = nullptr; + HcfResult ret = ConstructEcc224CommParamsSpec(&srcSpec); + EXPECT_EQ(ret, HCF_SUCCESS); + + ret = CopyEccCommonSpec(srcSpec, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest04, TestSize.Level0) +{ + HcfEccCommParamsSpec *srcSpec = nullptr; + HcfResult ret = ConstructEcc224CommParamsSpec(&srcSpec); + EXPECT_EQ(ret, HCF_SUCCESS); + + ret = CreateEccCommonSpecImpl(srcSpec, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest05, TestSize.Level0) +{ + HcfResult ret = CopyDhCommonSpec(nullptr, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest06, TestSize.Level0) +{ + HcfResult ret = CreateDhCommonSpecImpl(nullptr, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest07, TestSize.Level0) +{ + HcfBlob dataBlob = { .data = nullptr, .len = 0 }; + const char *curveName = nullptr; + HcfResult ret = HcfConvertPoint(curveName, &dataBlob, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest08, TestSize.Level0) +{ + HcfBlob dataBlob = { .data = nullptr, .len = 0 }; + const char *curveName = nullptr; + HcfResult ret = HcfGetEncodedPoint(curveName, nullptr, nullptr, &dataBlob); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} +} \ No newline at end of file diff --git a/test/unittest/src/crypto_openssl_common_test.cpp b/test/unittest/src/crypto_openssl_common_test.cpp new file mode 100644 index 0000000..08e58f8 --- /dev/null +++ b/test/unittest/src/crypto_openssl_common_test.cpp @@ -0,0 +1,78 @@ +/* + * 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 "openssl_common.h" + +#include + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoOsslCommonTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void CryptoOsslCommonTest::SetUpTestCase() {} +void CryptoOsslCommonTest::TearDownTestCase() {} +void CryptoOsslCommonTest::SetUp() {} +void CryptoOsslCommonTest::TearDown() {} + +HWTEST_F(CryptoOsslCommonTest, CryptoOsslCommonTest01, TestSize.Level0) +{ + HcfResult ret = GetCurveNameByCurveId(0, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoOsslCommonTest, CryptoOsslCommonTest02, TestSize.Level0) +{ + HcfResult ret = GetNidByCurveNameValue(0, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoOsslCommonTest, CryptoOsslCommonTest03, TestSize.Level0) +{ + HcfResult ret = GetGroupNameByNid(0, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoOsslCommonTest, CryptoOsslCommonTest04, TestSize.Level0) +{ + HcfResult ret = GetFormatTypeByFormatValue(0, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoOsslCommonTest, CryptoOsslCommonTest05, TestSize.Level0) +{ + HcfResult ret = GetAlgNameByBits(0, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoOsslCommonTest, CryptoOsslCommonTest06, TestSize.Level0) +{ + HcfResult ret = GetOpensslCurveId(0, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoOsslCommonTest, CryptoOsslCommonTest07, TestSize.Level0) +{ + HcfResult ret = GetSm2SpecStringSm3(nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} +} \ No newline at end of file diff --git a/test/unittest/src/crypto_pbkdf2_test.cpp b/test/unittest/src/crypto_pbkdf2_test.cpp index c6d8a52..77d93b5 100644 --- a/test/unittest/src/crypto_pbkdf2_test.cpp +++ b/test/unittest/src/crypto_pbkdf2_test.cpp @@ -491,4 +491,10 @@ HWTEST_F(CryptoPbkdf2Test, CryptoPbkdf2TestError12, TestSize.Level1) HcfResult ret = HcfKdfCreate(nullptr, nullptr); EXPECT_NE(ret, HCF_SUCCESS); } + +HWTEST_F(CryptoPbkdf2Test, CryptoPbkdf2TestError13, TestSize.Level0) +{ + HcfResult ret = HcfKdfPBKDF2SpiCreate(nullptr, nullptr); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} } -- Gitee