mirror of
https://gitee.com/openharmony/communication_dsoftbus
synced 2024-11-23 16:59:54 +00:00
feat: rsa and aes encrypt and decrypt unittest
Signed-off-by: weiqian <weiqian22@huawei.com>
This commit is contained in:
parent
e22534fbc5
commit
88276c9cce
@ -313,7 +313,7 @@ static int32_t OpensslAesGcmEncrypt(
|
||||
HILOG_ERROR(SOFTBUS_HILOG_ID, "GcmOpensslEvpInit failed.");
|
||||
return SOFTBUS_ERR;
|
||||
}
|
||||
if (EVP_DecryptInit_ex(ctx, NULL, NULL, cipherKey->key, cipherKey->iv) != 1) {
|
||||
if (EVP_EncryptInit_ex(ctx, NULL, NULL, cipherKey->key, cipherKey->iv) != 1) {
|
||||
HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_EncryptInit_ex failed.");
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return SOFTBUS_ERR;
|
||||
|
@ -77,6 +77,61 @@ if (defined(ohos_lite)) {
|
||||
}
|
||||
}
|
||||
|
||||
ohos_unittest("AdapterDsoftbusRsaCryptoTest") {
|
||||
module_out_path = module_output_path
|
||||
sources = [ "dsoftbus_rsa_crypto_test.cpp" ]
|
||||
include_dirs = [
|
||||
"$dsoftbus_root_path/adapter/common/include",
|
||||
"$dsoftbus_root_path/core/common/include/",
|
||||
"$dsoftbus_root_path/interfaces/kits/common",
|
||||
"//third_party/bounds_checking_function/include",
|
||||
]
|
||||
deps = [
|
||||
"../../../adapter:softbus_adapter",
|
||||
"//third_party/bounds_checking_function:libsec_static",
|
||||
"//third_party/googletest:gtest_main",
|
||||
]
|
||||
if (is_standard_system) {
|
||||
external_deps = [
|
||||
"c_utils:utils",
|
||||
"hilog:libhilog",
|
||||
"huks:libhukssdk",
|
||||
]
|
||||
} else {
|
||||
external_deps = [
|
||||
"c_utils:utils",
|
||||
"hilog:libhilog",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
ohos_unittest("AdapterDsoftbusAesCryptoTest") {
|
||||
module_out_path = module_output_path
|
||||
sources = [ "dsoftbus_aes_crypto_test.cpp" ]
|
||||
include_dirs = [
|
||||
"$dsoftbus_root_path/adapter/common/include",
|
||||
"$dsoftbus_root_path/core/common/include/",
|
||||
"$dsoftbus_root_path/interfaces/kits/common",
|
||||
"//third_party/bounds_checking_function/include",
|
||||
]
|
||||
deps = [
|
||||
"../../../adapter:softbus_adapter",
|
||||
"//third_party/bounds_checking_function:libsec_static",
|
||||
"//third_party/googletest:gtest_main",
|
||||
]
|
||||
if (is_standard_system) {
|
||||
external_deps = [
|
||||
"c_utils:utils",
|
||||
"hilog:libhilog",
|
||||
]
|
||||
} else {
|
||||
external_deps = [
|
||||
"c_utils:utils",
|
||||
"hilog:libhilog",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
ohos_unittest("AdapterDsoftbusDfxTest") {
|
||||
module_out_path = module_output_path
|
||||
sources = [ "softbus_dfx_test.cpp" ]
|
||||
@ -386,9 +441,11 @@ if (defined(ohos_lite)) {
|
||||
group("unittest") {
|
||||
testonly = true
|
||||
deps = [
|
||||
":AdapterDsoftbusAesCryptoTest",
|
||||
":AdapterDsoftbusDfxTest",
|
||||
":AdapterDsoftbusOtherTest",
|
||||
":AdapterDsoftbusRangeTest",
|
||||
":AdapterDsoftbusRsaCryptoTest",
|
||||
":AdapterDsoftbusSocketTest",
|
||||
":AdapterWlanExtendTest",
|
||||
":AdaptorDsoftbusCryptTest",
|
||||
|
460
tests/adapter/unittest/dsoftbus_aes_crypto_test.cpp
Executable file
460
tests/adapter/unittest/dsoftbus_aes_crypto_test.cpp
Executable file
@ -0,0 +1,460 @@
|
||||
/*
|
||||
* Copyright (c) 2023 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 "softbus_aes_encrypt.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <securec.h>
|
||||
|
||||
#include "softbus_adapter_crypto.h"
|
||||
#include "softbus_adapter_log.h"
|
||||
#include "softbus_adapter_mem.h"
|
||||
#include "softbus_errcode.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace testing::ext;
|
||||
|
||||
namespace OHOS {
|
||||
class AdapterDsoftbusAesCryptoTest : public testing::Test {
|
||||
protected:
|
||||
static void SetUpTestCase(void);
|
||||
static void TearDownTestCase(void);
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
};
|
||||
void AdapterDsoftbusAesCryptoTest::SetUpTestCase(void) { }
|
||||
void AdapterDsoftbusAesCryptoTest::TearDownTestCase(void) { }
|
||||
void AdapterDsoftbusAesCryptoTest::SetUp() { }
|
||||
void AdapterDsoftbusAesCryptoTest::TearDown() { }
|
||||
|
||||
/*
|
||||
* @tc.name: SoftBusGenerateHmacHash001
|
||||
* @tc.desc: parameters are Legal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftBusGenerateHmacHash001, TestSize.Level0)
|
||||
{
|
||||
uint32_t randLen = 8;
|
||||
uint32_t rootKeyLen = 16;
|
||||
uint32_t hashLen = 32;
|
||||
uint8_t randStr[randLen];
|
||||
uint8_t rootKey[rootKeyLen];
|
||||
uint8_t hash[hashLen];
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(randStr, randLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(rootKey, rootKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
EncryptKey randomKey = { randStr, randLen };
|
||||
|
||||
ret = SoftBusGenerateHmacHash(&randomKey, rootKey, rootKeyLen, hash, hashLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftBusGenerateHmacHash002
|
||||
* @tc.desc: parameter is nullptr
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftBusGenerateHmacHash002, TestSize.Level0)
|
||||
{
|
||||
uint32_t randLen = 8;
|
||||
uint32_t rootKeyLen = 16;
|
||||
uint32_t hashLen = 32;
|
||||
uint8_t randStr[randLen];
|
||||
uint8_t rootKey[rootKeyLen];
|
||||
uint8_t hash[hashLen];
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(randStr, randLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(rootKey, rootKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
EncryptKey randomKey = { randStr, randLen };
|
||||
|
||||
ret = SoftBusGenerateHmacHash(nullptr, rootKey, rootKeyLen, hash, hashLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftBusGenerateHmacHash(&randomKey, nullptr, rootKeyLen, hash, hashLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftBusGenerateHmacHash(&randomKey, rootKey, rootKeyLen, nullptr, hashLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftBusGenerateHmacHash003
|
||||
* @tc.desc: rootKeyLen or hashLen is illegal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftBusGenerateHmacHash003, TestSize.Level0)
|
||||
{
|
||||
uint32_t randLen = 8;
|
||||
uint32_t rootKeyLen = 16;
|
||||
uint32_t hashLen = 32;
|
||||
uint32_t rootKeyLen1 = 0;
|
||||
uint32_t hashLen1 = 0;
|
||||
uint8_t randStr[randLen];
|
||||
uint8_t rootKey[rootKeyLen];
|
||||
uint8_t hash[hashLen];
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(randStr, randLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(rootKey, rootKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
EncryptKey randomKey = { randStr, randLen };
|
||||
|
||||
ret = SoftBusGenerateHmacHash(&randomKey, rootKey, rootKeyLen1, hash, hashLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftBusGenerateHmacHash(&randomKey, rootKey, rootKeyLen, hash, hashLen1);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusAesCfbRootEncrypt001
|
||||
* @tc.desc: parameters are Legal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftbusAesCfbRootEncrypt001, TestSize.Level0)
|
||||
{
|
||||
uint32_t randLen = 8;
|
||||
uint32_t inDataLen = 10;
|
||||
uint32_t rootKeyLen = 16;
|
||||
uint8_t randStr[randLen];
|
||||
uint8_t inData[inDataLen];
|
||||
uint8_t rKey[rootKeyLen];
|
||||
AesOutputData encryptOutData = { 0 };
|
||||
AesOutputData decryptOutData = { 0 };
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(inData, inDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randStr, randLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(rKey, rootKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
AesInputData encryptInData = { inData, inDataLen };
|
||||
EncryptKey randomKey = { randStr, randLen };
|
||||
EncryptKey rootKey = { rKey, rootKeyLen };
|
||||
|
||||
ret = SoftbusAesCfbRootEncrypt(&encryptInData, &randomKey, &rootKey, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftbusAesCfbRootEncrypt(
|
||||
(const AesInputData *)&encryptOutData, &randomKey, &rootKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = memcmp((const char *)decryptOutData.data, (const char *)encryptInData.data, decryptOutData.len);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
SoftBusFree(encryptOutData.data);
|
||||
SoftBusFree(decryptOutData.data);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusAesCfbRootEncrypt002
|
||||
* @tc.desc: encrypt parameter is nullptr
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftbusAesCfbRootEncrypt002, TestSize.Level0)
|
||||
{
|
||||
uint32_t randLen = 8;
|
||||
uint32_t inDataLen = 10;
|
||||
uint32_t rootKeyLen = 16;
|
||||
uint8_t randStr[randLen];
|
||||
uint8_t inData[inDataLen];
|
||||
uint8_t rKey[rootKeyLen];
|
||||
AesOutputData encryptOutData = { 0 };
|
||||
AesOutputData decryptOutData = { 0 };
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(inData, inDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randStr, randLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(rKey, rootKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
AesInputData encryptInData = { inData, inDataLen };
|
||||
EncryptKey randomKey = { randStr, randLen };
|
||||
EncryptKey rootKey = { rKey, rootKeyLen };
|
||||
|
||||
ret = SoftbusAesCfbRootEncrypt(nullptr, &randomKey, &rootKey, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesCfbRootEncrypt(
|
||||
(const AesInputData *)&encryptOutData, &randomKey, &rootKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
ret = SoftbusAesCfbRootEncrypt(&encryptInData, nullptr, &rootKey, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesCfbRootEncrypt(
|
||||
(const AesInputData *)&encryptOutData, &randomKey, &rootKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
ret = SoftbusAesCfbRootEncrypt(&encryptInData, &randomKey, nullptr, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesCfbRootEncrypt(
|
||||
(const AesInputData *)&encryptOutData, &randomKey, &rootKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
ret = SoftbusAesCfbRootEncrypt(&encryptInData, &randomKey, &rootKey, ENCRYPT_MODE, nullptr);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesCfbRootEncrypt(
|
||||
(const AesInputData *)&encryptOutData, &randomKey, &rootKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusAesCfbRootEncrypt003
|
||||
* @tc.desc: decrypt parameter is nullptr
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftbusAesCfbRootEncrypt003, TestSize.Level0)
|
||||
{
|
||||
uint32_t randLen = 8;
|
||||
uint32_t inDataLen = 10;
|
||||
uint32_t rootKeyLen = 16;
|
||||
uint8_t randStr[randLen];
|
||||
uint8_t inData[inDataLen];
|
||||
uint8_t rKey[rootKeyLen];
|
||||
AesOutputData encryptOutData = { 0 };
|
||||
AesOutputData decryptOutData = { 0 };
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(inData, inDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randStr, randLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(rKey, rootKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
AesInputData encryptInData = { inData, inDataLen };
|
||||
EncryptKey randomKey = { randStr, randLen };
|
||||
EncryptKey rootKey = { rKey, rootKeyLen };
|
||||
|
||||
ret = SoftbusAesCfbRootEncrypt(&encryptInData, &randomKey, &rootKey, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = SoftbusAesCfbRootEncrypt(nullptr, &randomKey, &rootKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesCfbRootEncrypt(
|
||||
(const AesInputData *)&encryptOutData, nullptr, &rootKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesCfbRootEncrypt(
|
||||
(const AesInputData *)&encryptOutData, &randomKey, nullptr, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesCfbRootEncrypt((const AesInputData *)&encryptOutData, &randomKey, &rootKey, DECRYPT_MODE, nullptr);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
SoftBusFree(encryptOutData.data);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusAesCfbRootEncrypt004
|
||||
* @tc.desc: encMode is illegal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftbusAesCfbRootEncrypt004, TestSize.Level0)
|
||||
{
|
||||
uint32_t randLen = 8;
|
||||
uint32_t inDataLen = 10;
|
||||
uint32_t rootKeyLen = 16;
|
||||
int32_t encMode = 2;
|
||||
uint8_t randStr[randLen];
|
||||
uint8_t inData[inDataLen];
|
||||
uint8_t rKey[rootKeyLen];
|
||||
AesOutputData encryptOutData = { 0 };
|
||||
AesOutputData decryptOutData = { 0 };
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(inData, inDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randStr, randLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(rKey, rootKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
AesInputData encryptInData = { inData, inDataLen };
|
||||
EncryptKey randomKey = { randStr, randLen };
|
||||
EncryptKey rootKey = { rKey, rootKeyLen };
|
||||
|
||||
ret = SoftbusAesCfbRootEncrypt(&encryptInData, &randomKey, &rootKey, encMode, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesCfbRootEncrypt(
|
||||
(const AesInputData *)&encryptOutData, &randomKey, &rootKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
ret = SoftbusAesCfbRootEncrypt(&encryptInData, &randomKey, &rootKey, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret =
|
||||
SoftbusAesCfbRootEncrypt((const AesInputData *)&encryptOutData, &randomKey, &rootKey, encMode, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
SoftBusFree(encryptOutData.data);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusAesGcmEncrypt001
|
||||
* @tc.desc: parameters are Legal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftbusAesGcmEncrypt001, TestSize.Level0)
|
||||
{
|
||||
uint32_t randKeyLen = 32;
|
||||
uint32_t randIvLen = 16;
|
||||
uint32_t inDataLen = 10;
|
||||
uint8_t randSession[randKeyLen];
|
||||
uint8_t randIv[randIvLen];
|
||||
uint8_t inData[inDataLen];
|
||||
AesOutputData encryptOutData = { 0 };
|
||||
AesOutputData decryptOutData = { 0 };
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(inData, inDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randSession, randKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randIv, randIvLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
AesInputData encryptInData = { inData, inDataLen };
|
||||
AesCipherKey cipherKey = { randSession, randKeyLen, randIv, randIvLen };
|
||||
|
||||
ret = SoftbusAesGcmEncrypt(&encryptInData, &cipherKey, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftbusAesGcmEncrypt((const AesInputData *)&encryptOutData, &cipherKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = memcmp((const char *)decryptOutData.data, (const char *)encryptInData.data, decryptOutData.len);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
SoftBusFree(encryptOutData.data);
|
||||
SoftBusFree(decryptOutData.data);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusAesGcmEncrypt002
|
||||
* @tc.desc: encrypt parameter is nullptr
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftbusAesGcmEncrypt002, TestSize.Level0)
|
||||
{
|
||||
uint32_t randKeyLen = 32;
|
||||
uint32_t randIvLen = 16;
|
||||
uint32_t inDataLen = 10;
|
||||
uint8_t randSession[randKeyLen];
|
||||
uint8_t randIv[randIvLen];
|
||||
uint8_t inData[inDataLen];
|
||||
AesOutputData encryptOutData = { 0 };
|
||||
AesOutputData decryptOutData = { 0 };
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(inData, inDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randSession, randKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randIv, randIvLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
AesInputData encryptInData = { inData, inDataLen };
|
||||
AesCipherKey cipherKey = { randSession, randKeyLen, randIv, randIvLen };
|
||||
|
||||
ret = SoftbusAesGcmEncrypt(nullptr, &cipherKey, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesGcmEncrypt((const AesInputData *)&encryptOutData, &cipherKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
ret = SoftbusAesGcmEncrypt(&encryptInData, nullptr, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesGcmEncrypt((const AesInputData *)&encryptOutData, &cipherKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
ret = SoftbusAesGcmEncrypt(&encryptInData, &cipherKey, ENCRYPT_MODE, nullptr);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesGcmEncrypt((const AesInputData *)&encryptOutData, &cipherKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusAesGcmEncrypt003
|
||||
* @tc.desc: decrypt parameter is nullptr
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftbusAesGcmEncrypt003, TestSize.Level0)
|
||||
{
|
||||
uint32_t randKeyLen = 32;
|
||||
uint32_t randIvLen = 16;
|
||||
uint32_t inDataLen = 10;
|
||||
uint8_t randSession[randKeyLen];
|
||||
uint8_t randIv[randIvLen];
|
||||
uint8_t inData[inDataLen];
|
||||
AesOutputData encryptOutData = { 0 };
|
||||
AesOutputData decryptOutData = { 0 };
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(inData, inDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randSession, randKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randIv, randIvLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
AesInputData encryptInData = { inData, inDataLen };
|
||||
AesCipherKey cipherKey = { randSession, randKeyLen, randIv, randIvLen };
|
||||
|
||||
ret = SoftbusAesGcmEncrypt(&encryptInData, &cipherKey, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = SoftbusAesGcmEncrypt(nullptr, &cipherKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesGcmEncrypt((const AesInputData *)&encryptOutData, nullptr, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesGcmEncrypt((const AesInputData *)&encryptOutData, &cipherKey, DECRYPT_MODE, nullptr);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
SoftBusFree(encryptOutData.data);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusAesGcmEncrypt004
|
||||
* @tc.desc: decrypt parameter is nullptr
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusAesCryptoTest, SoftbusAesGcmEncrypt004, TestSize.Level0)
|
||||
{
|
||||
uint32_t randKeyLen = 32;
|
||||
uint32_t randIvLen = 16;
|
||||
uint32_t inDataLen = 10;
|
||||
int32_t encMode = 2;
|
||||
uint8_t randSession[randKeyLen];
|
||||
uint8_t randIv[randIvLen];
|
||||
uint8_t inData[inDataLen];
|
||||
AesOutputData encryptOutData = { 0 };
|
||||
AesOutputData decryptOutData = { 0 };
|
||||
|
||||
int32_t ret = SoftBusGenerateRandomArray(inData, inDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randSession, randKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(randIv, randIvLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
AesInputData encryptInData = { inData, inDataLen };
|
||||
AesCipherKey cipherKey = { randSession, randKeyLen, randIv, randIvLen };
|
||||
|
||||
ret = SoftbusAesGcmEncrypt(&encryptInData, &cipherKey, encMode, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusAesGcmEncrypt((const AesInputData *)&encryptOutData, &cipherKey, DECRYPT_MODE, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
ret = SoftbusAesGcmEncrypt(&encryptInData, &cipherKey, ENCRYPT_MODE, &encryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftbusAesGcmEncrypt((const AesInputData *)&encryptOutData, &cipherKey, encMode, &decryptOutData);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
SoftBusFree(encryptOutData.data);
|
||||
}
|
||||
} // namespace OHOS
|
259
tests/adapter/unittest/dsoftbus_rsa_crypto_test.cpp
Executable file
259
tests/adapter/unittest/dsoftbus_rsa_crypto_test.cpp
Executable file
@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Copyright (c) 2023 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 "softbus_rsa_encrypt.h"
|
||||
|
||||
#include <securec.h>
|
||||
|
||||
#include "softbus_adapter_crypto.h"
|
||||
#include "softbus_adapter_log.h"
|
||||
#include "softbus_adapter_mem.h"
|
||||
#include "softbus_errcode.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace testing::ext;
|
||||
|
||||
namespace OHOS {
|
||||
class AdapterDsoftbusRsaCryptoTest : public testing::Test {
|
||||
protected:
|
||||
static void SetUpTestCase(void);
|
||||
static void TearDownTestCase(void);
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
};
|
||||
void AdapterDsoftbusRsaCryptoTest::SetUpTestCase(void) { }
|
||||
void AdapterDsoftbusRsaCryptoTest::TearDownTestCase(void) { }
|
||||
void AdapterDsoftbusRsaCryptoTest::SetUp() { }
|
||||
void AdapterDsoftbusRsaCryptoTest::TearDown() { }
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusGetPublicKey001
|
||||
* @tc.desc: parameters are Legal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusRsaCryptoTest, SoftbusGetPublicKey001, TestSize.Level0)
|
||||
{
|
||||
uint32_t pKeyLen = SOFTBUS_RSA_LEN;
|
||||
uint8_t publicKey[pKeyLen];
|
||||
int32_t ret = SoftbusGetPublicKey(publicKey, pKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusGetPublicKey002
|
||||
* @tc.desc: parameter is nullptr
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusRsaCryptoTest, SoftbusGetPublicKey002, TestSize.Level0)
|
||||
{
|
||||
uint32_t pKeyLen = SOFTBUS_RSA_LEN;
|
||||
int32_t ret = SoftbusGetPublicKey(nullptr, pKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusGetPublicKey003
|
||||
* @tc.desc: len is illegal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusRsaCryptoTest, SoftbusGetPublicKey003, TestSize.Level0)
|
||||
{
|
||||
uint8_t publicKey[SOFTBUS_RSA_LEN];
|
||||
uint32_t pKeyLen = 0;
|
||||
int32_t ret = SoftbusGetPublicKey(publicKey, pKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusRsaEncrypt001
|
||||
* @tc.desc: parameters are Legal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusRsaCryptoTest, SoftbusRsaEncrypt001, TestSize.Level0)
|
||||
{
|
||||
uint32_t pKeyLen = SOFTBUS_RSA_LEN;
|
||||
uint8_t publicKey[pKeyLen];
|
||||
uint32_t srcDataLen = 5;
|
||||
uint8_t srcData[srcDataLen];
|
||||
uint32_t encryptedDataLen = 0;
|
||||
uint8_t *encryptedData = NULL;
|
||||
|
||||
int32_t ret = SoftbusGetPublicKey(publicKey, pKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(srcData, srcDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = SoftbusRsaEncrypt(srcData, srcDataLen, publicKey, &encryptedData, &encryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
SoftBusFree(encryptedData);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusRsaEncrypt002
|
||||
* @tc.desc: parameter is nullptr
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusRsaCryptoTest, SoftbusRsaEncrypt002, TestSize.Level0)
|
||||
{
|
||||
uint32_t pKeyLen = SOFTBUS_RSA_LEN;
|
||||
uint8_t publicKey[pKeyLen];
|
||||
uint32_t srcDataLen = 5;
|
||||
uint8_t srcData[srcDataLen];
|
||||
uint32_t encryptedDataLen = 0;
|
||||
uint8_t *encryptedData = NULL;
|
||||
|
||||
int32_t ret = SoftbusGetPublicKey(publicKey, pKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(srcData, srcDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = SoftbusRsaEncrypt(nullptr, srcDataLen, publicKey, &encryptedData, &encryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusRsaEncrypt(srcData, srcDataLen, nullptr, &encryptedData, &encryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusRsaEncrypt(srcData, srcDataLen, publicKey, nullptr, &encryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusRsaEncrypt(srcData, srcDataLen, publicKey, &encryptedData, nullptr);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusRsaEncrypt003
|
||||
* @tc.desc: srcDataLen is illegal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusRsaCryptoTest, SoftbusRsaEncrypt003, TestSize.Level0)
|
||||
{
|
||||
uint32_t pKeyLen = SOFTBUS_RSA_LEN;
|
||||
uint8_t publicKey[pKeyLen];
|
||||
uint32_t inDataLen = 0;
|
||||
uint32_t srcDataLen = 5;
|
||||
uint8_t srcData[srcDataLen];
|
||||
uint32_t encryptedDataLen = 0;
|
||||
uint8_t *encryptedData = NULL;
|
||||
|
||||
int32_t ret = SoftbusGetPublicKey(publicKey, pKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(srcData, srcDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = SoftbusRsaEncrypt(srcData, inDataLen, publicKey, &encryptedData, &encryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusRsaDecrypt001
|
||||
* @tc.desc: parameters are Legal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusRsaCryptoTest, SoftbusRsaDecrypt001, TestSize.Level0)
|
||||
{
|
||||
uint32_t pKeyLen = SOFTBUS_RSA_LEN;
|
||||
uint8_t publicKey[pKeyLen];
|
||||
uint32_t srcDataLen = 5;
|
||||
uint8_t srcData[srcDataLen];
|
||||
uint32_t encryptedDataLen = 0;
|
||||
uint8_t *encryptedData = NULL;
|
||||
uint32_t decryptedDataLen = 0;
|
||||
uint8_t *decryptedData = NULL;
|
||||
|
||||
int32_t ret = SoftbusGetPublicKey(publicKey, pKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(srcData, srcDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = SoftbusRsaEncrypt(srcData, srcDataLen, publicKey, &encryptedData, &encryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftbusRsaDecrypt(encryptedData, encryptedDataLen, &decryptedData, &decryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = memcmp((const char *)decryptedData, (const char *)srcData, decryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
SoftBusFree(encryptedData);
|
||||
SoftBusFree(decryptedData);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusRsaDecrypt002
|
||||
* @tc.desc: parameter is nullptr
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusRsaCryptoTest, SoftbusRsaDecrypt002, TestSize.Level0)
|
||||
{
|
||||
uint32_t pKeyLen = SOFTBUS_RSA_LEN;
|
||||
uint8_t publicKey[pKeyLen];
|
||||
uint32_t srcDataLen = 5;
|
||||
uint8_t srcData[srcDataLen];
|
||||
uint32_t encryptedDataLen = 0;
|
||||
uint8_t *encryptedData = NULL;
|
||||
uint32_t decryptedDataLen = 0;
|
||||
uint8_t *decryptedData = NULL;
|
||||
|
||||
int32_t ret = SoftbusGetPublicKey(publicKey, pKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(srcData, srcDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftbusRsaEncrypt(srcData, srcDataLen, publicKey, &encryptedData, &encryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = SoftbusRsaDecrypt(nullptr, encryptedDataLen, &decryptedData, &decryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusRsaDecrypt(encryptedData, encryptedDataLen, nullptr, &decryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
ret = SoftbusRsaDecrypt(encryptedData, encryptedDataLen, &decryptedData, nullptr);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
|
||||
SoftBusFree(encryptedData);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: SoftbusRsaDecrypt003
|
||||
* @tc.desc: inDatalen is illegal
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I5OHDE
|
||||
*/
|
||||
HWTEST_F(AdapterDsoftbusRsaCryptoTest, SoftbusRsaDecrypt003, TestSize.Level0)
|
||||
{
|
||||
uint32_t pKeyLen = SOFTBUS_RSA_LEN;
|
||||
uint8_t publicKey[pKeyLen];
|
||||
uint32_t srcDataLen = 5;
|
||||
uint8_t srcData[srcDataLen];
|
||||
uint32_t encryptedDataLen = 0;
|
||||
uint8_t *encryptedData = NULL;
|
||||
uint32_t decryptedDataLen = 0;
|
||||
uint8_t *decryptedData = NULL;
|
||||
uint32_t srcDataLen1 = 0;
|
||||
|
||||
int32_t ret = SoftbusGetPublicKey(publicKey, pKeyLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftBusGenerateRandomArray(srcData, srcDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
ret = SoftbusRsaEncrypt(srcData, srcDataLen, publicKey, &encryptedData, &encryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_OK, ret);
|
||||
|
||||
ret = SoftbusRsaDecrypt(encryptedData, srcDataLen1, &decryptedData, &decryptedDataLen);
|
||||
EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
|
||||
SoftBusFree(encryptedData);
|
||||
}
|
||||
} // namespace OHOS
|
Loading…
Reference in New Issue
Block a user