mirror of
https://gitee.com/openharmony/communication_netstack
synced 2024-11-23 06:50:46 +00:00
commit
6ae9ea0b80
217
test/unittest/test_tls/clientTest/TlsCertificateTest.cpp
Normal file
217
test/unittest/test_tls/clientTest/TlsCertificateTest.cpp
Normal file
@ -0,0 +1,217 @@
|
||||
/*
|
||||
* 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 <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#include <openssl/pem.h>
|
||||
#include <string>
|
||||
|
||||
#define private public
|
||||
#include "tls.h"
|
||||
#include "TlsTest.h"
|
||||
#include "tls_certificate.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace NetStack {
|
||||
namespace TlsSocket {
|
||||
namespace {
|
||||
using namespace testing::ext;
|
||||
} // namespace
|
||||
|
||||
class TlsCertificateTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase() {}
|
||||
|
||||
static void TearDownTestCase() {}
|
||||
|
||||
virtual void SetUp() {}
|
||||
|
||||
virtual void TearDown() {}
|
||||
};
|
||||
|
||||
HWTEST_F(TlsCertificateTest, CertificateTest001, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
bool isCertFromData = tlsCertificate.CertificateFromData(CLIENT_FILE, LOCAL_CERT);
|
||||
EXPECT_EQ(isCertFromData, true);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, CertificateTest002, TestSize.Level2)
|
||||
{
|
||||
std::string data = "";
|
||||
TLSCertificate tlsCertificate = TLSCertificate(data, EncodingFormat::DER, LOCAL_CERT);
|
||||
bool ret = tlsCertificate.CertificateFromDer(CLIENT_FILE, LOCAL_CERT);
|
||||
EXPECT_EQ(ret, false);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, CertificateTest003, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, EncodingFormat::PEM, LOCAL_CERT);
|
||||
bool ret = tlsCertificate.CertificateFromPem(CA_CRT_FILE, LOCAL_CERT);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, CopyConstruction, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
TLSCertificate tlsCopy = TLSCertificate(tlsCertificate);
|
||||
bool isCertFromData = tlsCopy.CertificateFromData(CLIENT_FILE, LOCAL_CERT);
|
||||
EXPECT_EQ(isCertFromData, true);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, AssignmentConstruction, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
TLSCertificate tlsCert = tlsCertificate;
|
||||
bool isCertFromData = tlsCert.CertificateFromData(CLIENT_FILE, LOCAL_CERT);
|
||||
EXPECT_EQ(isCertFromData, true);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, GetLocalCertString, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
std::string localCert = tlsCertificate.GetLocalCertString();
|
||||
std::cout << "localCert:" << localCert << std::endl;
|
||||
EXPECT_NE(localCert.c_str(), nullptr);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, CertificateFromPemTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
bool ret = tlsCertificate.CertificateFromPem(CERTIFICAT, LOCAL_CERT);
|
||||
EXPECT_FALSE(ret);
|
||||
ret = tlsCertificate.CertificateFromPem("", LOCAL_CERT);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, CertificateFromDerTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
bool ret = tlsCertificate.CertificateFromDer(CA_CRT_FILE, LOCAL_CERT);
|
||||
EXPECT_FALSE(ret);
|
||||
ret = tlsCertificate.CertificateFromDer("", LOCAL_CERT);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, GetSignatureAlgorithmTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
tlsCertificate.CertificateFromDer(CERTIFICAT, CA_CERT);
|
||||
std::string ret = tlsCertificate.GetSignatureAlgorithm();
|
||||
EXPECT_FALSE(ret.empty());
|
||||
TLSCertificate tlsCertificate2 = TLSCertificate("", LOCAL_CERT);
|
||||
ret = tlsCertificate2.GetSignatureAlgorithm();
|
||||
EXPECT_TRUE(ret.empty());
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, CaFromData, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CA_CRT_FILE, CA_CERT);
|
||||
bool isFilePath = tlsCertificate.CertificateFromData(CA_CRT_FILE, CA_CERT);
|
||||
|
||||
BIO *bio = BIO_new_mem_buf(CA_CRT_FILE, -1);
|
||||
X509 *x509Ca = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr);
|
||||
BIO_free(bio);
|
||||
bool setLocalCertRawData = tlsCertificate.SetLocalCertRawData(x509Ca);
|
||||
EXPECT_TRUE(setLocalCertRawData);
|
||||
bool setX509Version = tlsCertificate.SetX509Version(x509Ca);
|
||||
EXPECT_TRUE(setX509Version);
|
||||
bool setSerialNumber = tlsCertificate.SetSerialNumber(x509Ca);
|
||||
EXPECT_TRUE(setSerialNumber);
|
||||
bool setNotValidTime = tlsCertificate.SetNotValidTime(x509Ca);
|
||||
EXPECT_TRUE(setNotValidTime);
|
||||
bool setSignatureAlgorithm = tlsCertificate.SetSignatureAlgorithm(x509Ca);
|
||||
EXPECT_TRUE(setSignatureAlgorithm);
|
||||
bool caCertToString = tlsCertificate.CaCertToString(x509Ca);
|
||||
EXPECT_TRUE(caCertToString);
|
||||
bool localCertToString = tlsCertificate.LocalCertToString(x509Ca);
|
||||
EXPECT_TRUE(localCertToString);
|
||||
|
||||
BIO *bioCrt = BIO_new_mem_buf(CLIENT_FILE, -1);
|
||||
X509 *x509Crt = PEM_read_bio_X509(bioCrt, nullptr, nullptr, nullptr);
|
||||
BIO_free(bioCrt);
|
||||
bool analysisCert = tlsCertificate.AnalysisCertificate(CertType::LOCAL_CERT, x509Crt);
|
||||
EXPECT_TRUE(analysisCert);
|
||||
bool analysisCa = tlsCertificate.AnalysisCertificate(CertType::CA_CERT, x509Ca);
|
||||
EXPECT_TRUE(analysisCa);
|
||||
EXPECT_EQ(isFilePath, true);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, AnalysisCertificateTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
X509 *x509 = nullptr;
|
||||
bool ret = tlsCertificate.AnalysisCertificate(CertType::LOCAL_CERT, x509);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, CaCertToStringTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
X509 *x509 = nullptr;
|
||||
bool ret = tlsCertificate.CaCertToString(x509);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, LocalCertToStringTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
X509 *x509 = nullptr;
|
||||
bool ret = tlsCertificate.LocalCertToString(x509);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, SetX509VersionTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
X509 *x509 = nullptr;
|
||||
bool ret = tlsCertificate.SetX509Version(x509);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, SetSerialNumberTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
X509 *x509 = nullptr;
|
||||
bool ret = tlsCertificate.SetSerialNumber(x509);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, SetNotValidTimeTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
X509 *x509 = nullptr;
|
||||
bool ret = tlsCertificate.SetNotValidTime(x509);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, SetSignatureAlgorithmTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
X509 *x509 = nullptr;
|
||||
bool ret = tlsCertificate.SetSignatureAlgorithm(x509);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsCertificateTest, SetLocalCertRawDataTest, TestSize.Level2)
|
||||
{
|
||||
TLSCertificate tlsCertificate = TLSCertificate(CLIENT_FILE, LOCAL_CERT);
|
||||
X509 *x509 = nullptr;
|
||||
bool ret = tlsCertificate.SetLocalCertRawData(x509);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
} // namespace TlsSocket
|
||||
} // namespace NetStack
|
||||
} // namespace OHOS
|
147
test/unittest/test_tls/clientTest/TlsConfigurationTest.cpp
Normal file
147
test/unittest/test_tls/clientTest/TlsConfigurationTest.cpp
Normal file
@ -0,0 +1,147 @@
|
||||
/*
|
||||
* 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 <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "tls_configuration.h"
|
||||
#include "tls.h"
|
||||
#include "TlsTest.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace NetStack {
|
||||
namespace TlsSocket {
|
||||
namespace {
|
||||
using namespace testing::ext;
|
||||
} // namespace
|
||||
|
||||
class TlsConfigurationTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase() {}
|
||||
|
||||
static void TearDownTestCase() {}
|
||||
|
||||
virtual void SetUp() {}
|
||||
|
||||
virtual void TearDown() {}
|
||||
};
|
||||
|
||||
HWTEST_F(TlsConfigurationTest, AssignmentConstruction, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration tlsConfiguration;
|
||||
TLSConfiguration configuration = tlsConfiguration;
|
||||
configuration.SetLocalCertificate(CLIENT_FILE);
|
||||
TLSCertificate tlsCertificate = configuration.GetLocalCertificate();
|
||||
EXPECT_NE(tlsCertificate.handle(), nullptr);
|
||||
X509CertRawData x509CertRawData = configuration.GetCertificate();
|
||||
EXPECT_NE(x509CertRawData.data.Length(), 0);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsConfigurationTest, CopyConstruction, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration tlsConfiguration;
|
||||
tlsConfiguration.SetLocalCertificate(CLIENT_FILE);
|
||||
TLSConfiguration configuration = TLSConfiguration(tlsConfiguration);
|
||||
TLSCertificate tlsCertificate = configuration.GetLocalCertificate();
|
||||
EXPECT_NE(tlsCertificate.handle(), nullptr);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsConfigurationTest, SetAndGetCa, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration tlsConfiguration;
|
||||
tlsConfiguration.SetLocalCertificate(CLIENT_FILE);
|
||||
std::vector<std::string> certificate;
|
||||
certificate.push_back(CA_CRT_FILE);
|
||||
tlsConfiguration.SetCaCertificate(certificate);
|
||||
std::vector<std::string> getCaCertificate;
|
||||
getCaCertificate = tlsConfiguration.GetCaCertificate();
|
||||
EXPECT_NE(getCaCertificate.size(), 0);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsConfigurationTest, SetPrivateKey, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration tlsConfiguration;
|
||||
tlsConfiguration.SetLocalCertificate(CLIENT_FILE);
|
||||
SecureData structureData(PRI_KEY_FILE);
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
tlsConfiguration.SetPrivateKey(structureData, keyPass);
|
||||
TLSKey tlsKey = tlsConfiguration.GetPrivateKey();
|
||||
SecureData tlsKeyData = tlsKey.GetKeyData();
|
||||
EXPECT_EQ(tlsKeyData.Length(), strlen(PRI_KEY_FILE));
|
||||
}
|
||||
|
||||
HWTEST_F(TlsConfigurationTest, SetProtocol, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration tlsConfiguration;
|
||||
std::vector<std::string> protocol;
|
||||
std::string protocolVer = "TLSv1.3";
|
||||
protocol.push_back(protocolVer);
|
||||
tlsConfiguration.SetProtocol(protocol);
|
||||
TLSProtocol tlsProtocol = tlsConfiguration.GetProtocol();
|
||||
EXPECT_EQ(tlsProtocol, TLS_V1_3);
|
||||
TLSProtocol minProtocol = tlsConfiguration.GetMinProtocol();
|
||||
EXPECT_EQ(minProtocol, TLS_V1_3);
|
||||
TLSProtocol maxProtocol = tlsConfiguration.GetMaxProtocol();
|
||||
EXPECT_EQ(maxProtocol, TLS_V1_3);
|
||||
|
||||
protocol.clear();
|
||||
protocolVer = "TLSv1.2";
|
||||
protocol.push_back(protocolVer);
|
||||
tlsConfiguration.SetProtocol(protocol);
|
||||
tlsProtocol = tlsConfiguration.GetProtocol();
|
||||
EXPECT_EQ(tlsProtocol, TLS_V1_2);
|
||||
minProtocol = tlsConfiguration.GetMinProtocol();
|
||||
EXPECT_EQ(minProtocol, TLS_V1_2);
|
||||
maxProtocol = tlsConfiguration.GetMaxProtocol();
|
||||
EXPECT_EQ(maxProtocol, TLS_V1_2);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsConfigurationTest, UseRemoteCipherPrefer, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration tlsConfiguration;
|
||||
tlsConfiguration.SetUseRemoteCipherPrefer(true);
|
||||
bool isUsePemoteCipherPrefer = tlsConfiguration.GetUseRemoteCipherPrefer();
|
||||
EXPECT_TRUE(isUsePemoteCipherPrefer);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsConfigurationTest, CipherSuite, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration tlsConfiguration;
|
||||
std::string cipherSuite = "AES256-SHA256";
|
||||
tlsConfiguration.SetCipherSuite(cipherSuite);
|
||||
std::string getCipherSuite;
|
||||
getCipherSuite = tlsConfiguration.GetCipherSuite();
|
||||
std::cout << "getCipherSuite:" << getCipherSuite << std::endl;
|
||||
int idx = getCipherSuite.find(cipherSuite);
|
||||
EXPECT_NE(idx, std::string::npos);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsConfigurationTest, SignatureAlgorithms, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration tlsConfiguration;
|
||||
std::string signatureAlgorithms = "rsa_pss_rsae_sha256:ECDSA+SHA256";
|
||||
tlsConfiguration.SetSignatureAlgorithms(signatureAlgorithms);
|
||||
std::string getSignatureAlgorithms;
|
||||
getSignatureAlgorithms = tlsConfiguration.GetSignatureAlgorithms();
|
||||
std::cout << "getSignatureAlgorithms:" << getSignatureAlgorithms << std::endl;
|
||||
std::string subStr = "ECDSA+SHA256";
|
||||
int idx = getSignatureAlgorithms.find(subStr);
|
||||
EXPECT_NE(idx, std::string::npos);
|
||||
}
|
||||
} // namespace TlsSocket
|
||||
} // namespace NetStack
|
||||
} // namespace OHOS
|
187
test/unittest/test_tls/clientTest/TlsContextTest.cpp
Normal file
187
test/unittest/test_tls/clientTest/TlsContextTest.cpp
Normal file
@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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 <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#define private public
|
||||
#include "tls_context.h"
|
||||
#include "tls.h"
|
||||
#include "TlsTest.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace NetStack {
|
||||
namespace TlsSocket {
|
||||
namespace {
|
||||
using namespace testing::ext;
|
||||
constexpr const char *PROTOCOL13 = "TLSv1.3";
|
||||
constexpr const char *PROTOCOL12 = "TLSv1.2";
|
||||
constexpr const char *PROTOCOL11 = "TLSv1.1";
|
||||
constexpr const char *CIPHER_SUITE = "AES256-SHA256";
|
||||
constexpr const char *SIGNATURE_ALGORITHMS = "rsa_pss_rsae_sha256:ECDSA+SHA256";
|
||||
} // namespace
|
||||
|
||||
class TlsContextTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase() {}
|
||||
|
||||
static void TearDownTestCase() {}
|
||||
|
||||
virtual void SetUp() {}
|
||||
|
||||
virtual void TearDown() {}
|
||||
};
|
||||
|
||||
HWTEST_F(TlsContextTest, ContextTest1, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration configuration;
|
||||
configuration.SetCipherSuite(CIPHER_SUITE);
|
||||
configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS);
|
||||
std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
|
||||
|
||||
EXPECT_NE(tlsContext, nullptr);
|
||||
tlsContext->CloseCtx();
|
||||
}
|
||||
|
||||
HWTEST_F(TlsContextTest, ContextTest2, TestSize.Level2)
|
||||
{
|
||||
std::vector<std::string> protocol;
|
||||
protocol.push_back(PROTOCOL13);
|
||||
protocol.push_back(PROTOCOL12);
|
||||
protocol.push_back(PROTOCOL11);
|
||||
TLSConfiguration configuration;
|
||||
std::vector<std::string> caVec = {CA_CRT_FILE};
|
||||
configuration.SetCaCertificate(caVec);
|
||||
configuration.SetProtocol(protocol);
|
||||
configuration.SetCipherSuite(CIPHER_SUITE);
|
||||
configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS);
|
||||
configuration.SetLocalCertificate(CLIENT_FILE);
|
||||
std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
|
||||
EXPECT_NE(tlsContext, nullptr);
|
||||
TLSContext::SetMinAndMaxProtocol(tlsContext.get());
|
||||
bool isInitTlsContext = TLSContext::InitTlsContext(tlsContext.get(), configuration);
|
||||
EXPECT_TRUE(isInitTlsContext);
|
||||
bool isSetCipherList = TLSContext::SetCipherList(tlsContext.get(), configuration);
|
||||
EXPECT_TRUE(isSetCipherList);
|
||||
bool isSetSignatureAlgorithms = TLSContext::SetSignatureAlgorithms(tlsContext.get(), configuration);
|
||||
EXPECT_TRUE(isSetSignatureAlgorithms);
|
||||
TLSContext::GetCiphers(tlsContext.get());
|
||||
TLSContext::UseRemoteCipher(tlsContext.get());
|
||||
bool setCaAndVerify = TLSContext::SetCaAndVerify(tlsContext.get(), configuration);
|
||||
EXPECT_TRUE(setCaAndVerify);
|
||||
bool setLocalCert = TLSContext::SetLocalCertificate(tlsContext.get(), configuration);
|
||||
EXPECT_TRUE(setLocalCert);
|
||||
bool setKeyAndCheck = TLSContext::SetKeyAndCheck(tlsContext.get(), configuration);
|
||||
EXPECT_FALSE(setKeyAndCheck);
|
||||
TLSContext::SetVerify(tlsContext.get());
|
||||
SSL *ssl = tlsContext->CreateSsl();
|
||||
EXPECT_NE(ssl, nullptr);
|
||||
SSL_free(ssl);
|
||||
ssl = nullptr;
|
||||
tlsContext->CloseCtx();
|
||||
}
|
||||
|
||||
HWTEST_F(TlsContextTest, ContextTest3, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration configuration;
|
||||
std::vector<std::string> caVec = {};
|
||||
configuration.SetCaCertificate(caVec);
|
||||
std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
|
||||
EXPECT_NE(tlsContext, nullptr);
|
||||
bool setCaAndVerify = TLSContext::SetCaAndVerify(tlsContext.get(), configuration);
|
||||
tlsContext->CloseCtx();
|
||||
EXPECT_TRUE(setCaAndVerify);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsContextTest, InitTlsContext3, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration configuration;
|
||||
std::string cipherSuite = "";
|
||||
configuration.SetCipherSuite(cipherSuite);
|
||||
std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
|
||||
|
||||
EXPECT_NE(tlsContext, nullptr);
|
||||
tlsContext->CloseCtx();
|
||||
}
|
||||
|
||||
HWTEST_F(TlsContextTest, InitTlsContext4, TestSize.Level2)
|
||||
{
|
||||
TLSConfiguration configuration;
|
||||
std::string signatureAlgorithms = "";
|
||||
configuration.SetCipherSuite(CIPHER_SUITE);
|
||||
configuration.SetSignatureAlgorithms(signatureAlgorithms);
|
||||
std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
|
||||
|
||||
EXPECT_NE(tlsContext, nullptr);
|
||||
tlsContext->CloseCtx();
|
||||
}
|
||||
|
||||
HWTEST_F(TlsContextTest, ContextNullTest, TestSize.Level2)
|
||||
{
|
||||
std::vector<std::string> protocol;
|
||||
protocol.push_back(PROTOCOL13);
|
||||
protocol.push_back(PROTOCOL12);
|
||||
protocol.push_back(PROTOCOL11);
|
||||
TLSConfiguration configuration;
|
||||
std::vector<std::string> caVec = {CA_CRT_FILE};
|
||||
configuration.SetCaCertificate(caVec);
|
||||
configuration.SetProtocol(protocol);
|
||||
configuration.SetCipherSuite(CIPHER_SUITE);
|
||||
configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS);
|
||||
configuration.SetLocalCertificate(CLIENT_FILE);
|
||||
std::unique_ptr<TLSContext> tlsContext = nullptr;
|
||||
EXPECT_EQ(tlsContext, nullptr);
|
||||
TLSContext::SetMinAndMaxProtocol(tlsContext.get());
|
||||
bool isInitTlsContext = TLSContext::InitTlsContext(tlsContext.get(), configuration);
|
||||
EXPECT_FALSE(isInitTlsContext);
|
||||
bool isSetCipherList = TLSContext::SetCipherList(tlsContext.get(), configuration);
|
||||
EXPECT_FALSE(isSetCipherList);
|
||||
bool isSetSignatureAlgorithms = TLSContext::SetSignatureAlgorithms(tlsContext.get(), configuration);
|
||||
EXPECT_FALSE(isSetSignatureAlgorithms);
|
||||
TLSContext::GetCiphers(tlsContext.get());
|
||||
TLSContext::UseRemoteCipher(tlsContext.get());
|
||||
bool setCaAndVerify = TLSContext::SetCaAndVerify(tlsContext.get(), configuration);
|
||||
EXPECT_FALSE(setCaAndVerify);
|
||||
bool setLocalCert = TLSContext::SetLocalCertificate(tlsContext.get(), configuration);
|
||||
EXPECT_FALSE(setLocalCert);
|
||||
bool setKeyAndCheck = TLSContext::SetKeyAndCheck(tlsContext.get(), configuration);
|
||||
EXPECT_FALSE(setKeyAndCheck);
|
||||
TLSContext::SetVerify(tlsContext.get());
|
||||
}
|
||||
|
||||
HWTEST_F(TlsContextTest, ContextFailTest1, TestSize.Level2)
|
||||
{
|
||||
std::vector<std::string> protocol;
|
||||
protocol.push_back("1.3");
|
||||
protocol.push_back("1.2");
|
||||
TLSConfiguration configuration;
|
||||
std::vector<std::string> caVec = {CA_CRT_FILE};
|
||||
configuration.SetCaCertificate(caVec);
|
||||
configuration.SetProtocol(protocol);
|
||||
configuration.SetCipherSuite(CIPHER_SUITE);
|
||||
configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS);
|
||||
configuration.SetLocalCertificate("certificate");
|
||||
SecureData key("key");
|
||||
SecureData keyPass("123456");
|
||||
configuration.SetPrivateKey(key, keyPass);
|
||||
std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
|
||||
EXPECT_NE(tlsContext, nullptr);
|
||||
}
|
||||
} // namespace TlsSocket
|
||||
} // namespace NetStack
|
||||
} // namespace OHOS
|
247
test/unittest/test_tls/clientTest/TlsKeyTest.cpp
Normal file
247
test/unittest/test_tls/clientTest/TlsKeyTest.cpp
Normal file
@ -0,0 +1,247 @@
|
||||
/*
|
||||
* 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 <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define private public
|
||||
#include "tls.h"
|
||||
#include "tls_key.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace NetStack {
|
||||
namespace TlsSocket {
|
||||
namespace {
|
||||
using namespace testing::ext;
|
||||
constexpr int FILE_READ_KEY_LEN = 4096;
|
||||
static char g_keyFile[] =
|
||||
"-----BEGIN RSA PRIVATE KEY-----\r\n"
|
||||
"MIIEowIBAAKCAQEAqVzrf6PkLu0uhp5yl2HPNm0vLyI1KLqgsdz5s+JvVdbPXNxD\r\n"
|
||||
"g6fmdwa64tJXZPKx7i1KwNs/Jx3xv1N6rqB0au+Ku0Zdq7zbMCqej63SbFW1XWvQ\r\n"
|
||||
"6RJ76GcitgrFMTlQN4AzfX0xLFaUJHRuDS4QC5UE9CmV3kD09BNgItu/hxPAHSwg\r\n"
|
||||
"q6myc1uufYCwCUIV3bzxd65M343zubTlwOSmsCSqQIl8C1Gd6NWT69tL4fq2hHc/\r\n"
|
||||
"09VAlcLvugztwM6NHwDCmRFEDz3RdRahAvCEde8OkY/Aor6UucYWzCJofLeyKVQg\r\n"
|
||||
"6J3CTsT/zUE6pdKTvuhQbpRCtWKWSa7qDv1WywIDAQABAoIBAFGpbCPvcmbuFjDy\r\n"
|
||||
"1W4Iy1EC9G1VoSwyUKlyUzRZSjWpjfLIggVJP+bEZ/hWU61pGEIvtIupK5pA5f/K\r\n"
|
||||
"0KzC0V9+gPYrx563QTjIVAwTVBLIgNq60dCQCQ7WK/Z62voRGIyqVCl94+ftFyE8\r\n"
|
||||
"wpO4UiRDhk/0fT7dMz882G32ZzNJmY9eHu+yOaRctJW2gRBROHpQfDGBCz7w8s2j\r\n"
|
||||
"ulIcnvwGOrvVllsL+vgY95M0LOq0W8ObbUSlawTnNTSRxFL68Hz5EaVJ19EYvEcC\r\n"
|
||||
"eWnpEqIfF8OhQ+mYbdrAutXCkqJLz3rdu5P2Lbk5Ht5ETfr7rtUzvb4+ExIcxVOs\r\n"
|
||||
"eys8EgECgYEA29tTxJOy2Cb4DKB9KwTErD1sFt9Ed+Z/A3RGmnM+/h75DHccqS8n\r\n"
|
||||
"g9DpvHVMcMWYFVYGlEHC1F+bupM9CgxqQcVhGk/ysJ5kXF6lSTnOQxORnku3HXnV\r\n"
|
||||
"4QzgKtLfHbukW1Y2RZM3aCz+Hg+bJrpacWyWZ4tRWNYsO58JRaubZjsCgYEAxTSP\r\n"
|
||||
"yUBleQejl5qO76PGUUs2W8+GPr492NJGb63mEiM1zTYLVN0uuDJ2JixzHb6o1NXZ\r\n"
|
||||
"6i00pSksT3+s0eiBTRnF6BJ0y/8J07ZnfQQXRAP8ypiZtd3jdOnUxEHfBw2QaIdP\r\n"
|
||||
"tVdUc2mpIhosAYT9sWpHYvlUqTCdeLwhkYfgeLECgYBoajjVcmQM3i0OKiZoCOKy\r\n"
|
||||
"/pTYI/8rho+p/04MylEPdXxIXEWDYD6/DrgDZh4ArQc2kt2bCcRTAnk+WfEyVYUd\r\n"
|
||||
"aXVdfry+/uqhJ94N8eMw3hlZeZIk8JkQQgIwtGd8goJjUoWB85Hr6vphIn5IHVcY\r\n"
|
||||
"6T5hPLxMmaL2SeioawDpwwKBgQCFXjDH6Hc3zQTEKND2HIqou/b9THH7yOlG056z\r\n"
|
||||
"NKZeKdXe/OfY8uT/yZDB7FnGCgVgO2huyTfLYvcGpNAZ/eZEYGPJuYGn3MmmlruS\r\n"
|
||||
"fsvFQfUahu2dY3zKusEcIXhV6sR5DNnJSFBi5VhvKcgNFwYDkF7K/thUu/4jgwgo\r\n"
|
||||
"xf33YQKBgDQffkP1jWqT/pzlVLFtF85/3eCC/uedBfxXknVMrWE+CM/Vsx9cvBZw\r\n"
|
||||
"hi15LA5+hEdbgvj87hmMiCOc75e0oz2Rd12ZoRlBVfbncH9ngfqBNQElM7Bueqoc\r\n"
|
||||
"JOpKV+gw0gQtiu4beIdFnYsdZoZwrTjC4rW7OI0WYoLJabMFFh3I\r\n"
|
||||
"-----END RSA PRIVATE KEY-----\r\n";
|
||||
} // namespace
|
||||
|
||||
class TlsKeyTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase() {}
|
||||
|
||||
static void TearDownTestCase() {}
|
||||
|
||||
virtual void SetUp() {}
|
||||
|
||||
virtual void TearDown() {}
|
||||
};
|
||||
|
||||
HWTEST_F(TlsKeyTest, AlgorithmTest, TestSize.Level2)
|
||||
{
|
||||
SecureData structureData(g_keyFile);
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass);
|
||||
KeyAlgorithm algorithm = tlsKey.Algorithm();
|
||||
EXPECT_EQ(algorithm, ALGORITHM_RSA);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, DecodePemTest, TestSize.Level2)
|
||||
{
|
||||
std::string fileName = "";
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKey1 = TLSKey(fileName, ALGORITHM_RSA, keyPass);
|
||||
TLSKey tlsKey2 = TLSKey(fileName, ALGORITHM_RSA, keyPass, EncodingFormat::DER);
|
||||
std::string fileName2 = "/system/lib";
|
||||
TLSKey tlsKey3 = TLSKey(fileName2, ALGORITHM_RSA, keyPass);
|
||||
TLSKey tlsKey4 = TLSKey(fileName2, ALGORITHM_RSA, keyPass, EncodingFormat::DER);
|
||||
SecureData structureData(g_keyFile);
|
||||
structureData.length_ = sizeof(g_keyFile);
|
||||
TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass);
|
||||
EXPECT_EQ(tlsKey.keyIsNull_, false);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, CopyConstruction, TestSize.Level2)
|
||||
{
|
||||
SecureData structureData(g_keyFile);
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass);
|
||||
TLSKey tlsKeyCopy = TLSKey(tlsKey);
|
||||
KeyAlgorithm algorithm = tlsKeyCopy.Algorithm();
|
||||
EXPECT_EQ(algorithm, ALGORITHM_RSA);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, AssignmentConstruction, TestSize.Level2)
|
||||
{
|
||||
SecureData structureData(g_keyFile);
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass);
|
||||
TLSKey key = tlsKey;
|
||||
KeyAlgorithm algorithm = key.Algorithm();
|
||||
EXPECT_EQ(algorithm, ALGORITHM_RSA);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, HandleTest, TestSize.Level2)
|
||||
{
|
||||
SecureData structureData(g_keyFile);
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass);
|
||||
Handle handle = tlsKey.handle();
|
||||
EXPECT_NE(handle, nullptr);
|
||||
tlsKey.Clear(true);
|
||||
TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass);
|
||||
Handle handleDsa = tlsKeyDsa.handle();
|
||||
EXPECT_EQ(handleDsa, nullptr);
|
||||
tlsKeyDsa.Clear(true);
|
||||
TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass);
|
||||
Handle handleEc = tlsKeyEc.handle();
|
||||
EXPECT_EQ(handleEc, nullptr);
|
||||
tlsKeyEc.Clear(true);
|
||||
TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass);
|
||||
Handle handleDh = tlsKeyDh.handle();
|
||||
EXPECT_EQ(handleDh, nullptr);
|
||||
tlsKeyDh.Clear(true);
|
||||
TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass);
|
||||
Handle handleOpaque = tlsKeyOpaque.handle();
|
||||
EXPECT_EQ(handleOpaque, nullptr);
|
||||
tlsKeyOpaque.Clear(true);
|
||||
TLSKey keyOpaque = tlsKeyOpaque;
|
||||
EXPECT_NE(handle, nullptr);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, GetKeyPassTest, TestSize.Level2)
|
||||
{
|
||||
SecureData structureData(g_keyFile);
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass);
|
||||
SecureData getKeyPass = tlsKey.GetKeyPass();
|
||||
EXPECT_EQ(getKeyPass.Length(), keyPass.Length());
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, GetKeyDataTest, TestSize.Level2)
|
||||
{
|
||||
SecureData structureData(g_keyFile);
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass);
|
||||
SecureData getKeyData = tlsKey.GetKeyData();
|
||||
EXPECT_EQ(getKeyData.Length(), structureData.Length());
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, AlgorithmTest2, TestSize.Level2)
|
||||
{
|
||||
SecureData structureData(g_keyFile);
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass);
|
||||
TLSKey keyDsa = tlsKeyDsa;
|
||||
TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass);
|
||||
TLSKey keyEc = tlsKeyEc;
|
||||
TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass);
|
||||
TLSKey keyDh = tlsKeyDh;
|
||||
TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass);
|
||||
TLSKey keyOpaque = tlsKeyOpaque;
|
||||
SecureData getKeyData = tlsKeyDsa.GetKeyData();
|
||||
EXPECT_EQ(getKeyData.Length(), structureData.Length());
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, SwitchAlgorithmTest, TestSize.Level2)
|
||||
{
|
||||
SecureData structureData(g_keyFile);
|
||||
std::string keyPassStr = "";
|
||||
|
||||
KeyType typePublic = KeyType::PUBLIC_KEY;
|
||||
SecureData keyPass(keyPassStr);
|
||||
KeyType typePrivate = KeyType::PRIVATE_KEY;
|
||||
char privateKey[FILE_READ_KEY_LEN] = {0};
|
||||
const char *privateKeyData = static_cast<const char *>(privateKey);
|
||||
BIO *bio = BIO_new_mem_buf(privateKeyData, -1);
|
||||
TLSKey tlsKeyRsa = TLSKey(structureData, ALGORITHM_RSA, keyPass);
|
||||
tlsKeyRsa.SwitchAlgorithm(typePrivate, ALGORITHM_RSA, bio);
|
||||
tlsKeyRsa.SwitchAlgorithm(typePublic, ALGORITHM_RSA, bio);
|
||||
TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass);
|
||||
tlsKeyDsa.SwitchAlgorithm(typePrivate, ALGORITHM_DSA, bio);
|
||||
tlsKeyDsa.SwitchAlgorithm(typePublic, ALGORITHM_DSA, bio);
|
||||
TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass);
|
||||
tlsKeyEc.SwitchAlgorithm(typePrivate, ALGORITHM_EC, bio);
|
||||
tlsKeyEc.SwitchAlgorithm(typePublic, ALGORITHM_EC, bio);
|
||||
TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass);
|
||||
tlsKeyDh.SwitchAlgorithm(typePrivate, ALGORITHM_DH, bio);
|
||||
tlsKeyDh.SwitchAlgorithm(typePublic, ALGORITHM_DH, bio);
|
||||
TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass);
|
||||
tlsKeyOpaque.SwitchAlgorithm(typePrivate, OPAQUE, bio);
|
||||
tlsKeyOpaque.SwitchAlgorithm(typePublic, OPAQUE, bio);
|
||||
SecureData getKeyData = tlsKeyDsa.GetKeyData();
|
||||
EXPECT_EQ(getKeyData.Length(), structureData.Length());
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, ClearTest, TestSize.Level2)
|
||||
{
|
||||
SecureData structureData(g_keyFile);
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass);
|
||||
tlsKeyDsa.dsa_ = DSA_new();
|
||||
tlsKeyDsa.Clear(true);
|
||||
TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass);
|
||||
tlsKeyDh.dh_ = DH_new();
|
||||
tlsKeyDh.Clear(true);
|
||||
TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass);
|
||||
tlsKeyEc.ec_ = EC_KEY_new();
|
||||
tlsKeyEc.Clear(true);
|
||||
TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass);
|
||||
tlsKeyOpaque.genericKey_ = EVP_PKEY_new();
|
||||
tlsKeyOpaque.Clear(true);
|
||||
EXPECT_EQ(tlsKeyOpaque.keyIsNull_, true);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsKeyTest, DecodeDataTest, TestSize.Level2)
|
||||
{
|
||||
SecureData data;
|
||||
std::string keyPassStr = "";
|
||||
SecureData keyPass(keyPassStr);
|
||||
TLSKey tlsKey = TLSKey(data, ALGORITHM_DSA, keyPass);
|
||||
tlsKey.DecodeData(data, ALGORITHM_DSA, keyPass);
|
||||
SecureData getKeyData = tlsKey.GetKeyData();
|
||||
EXPECT_EQ(getKeyData.Length(), data.Length());
|
||||
}
|
||||
} // namespace TlsSocket
|
||||
} // namespace NetStack
|
||||
} // namespace OHOS
|
344
test/unittest/test_tls/clientTest/TlsSocketBranchTest.cpp
Normal file
344
test/unittest/test_tls/clientTest/TlsSocketBranchTest.cpp
Normal file
@ -0,0 +1,344 @@
|
||||
/*
|
||||
* 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 <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#define private public
|
||||
#include "accesstoken_kit.h"
|
||||
#include "tls_socket.h"
|
||||
#include "socket_remote_info.h"
|
||||
#include "token_setproc.h"
|
||||
#include "tls.h"
|
||||
#include "TlsTest.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace NetStack {
|
||||
namespace TlsSocket {
|
||||
namespace {
|
||||
using namespace testing::ext;
|
||||
using namespace Security::AccessToken;
|
||||
using Security::AccessToken::AccessTokenID;
|
||||
static constexpr const char *KEY_PASS = "";
|
||||
static constexpr const char *PROTOCOL12 = "TLSv1.2";
|
||||
static constexpr const char *PROTOCOL13 = "TLSv1.3";
|
||||
static constexpr const char *IP_ADDRESS = "127.0.0.1";
|
||||
static constexpr const char *ALPN_PROTOCOL = "http/1.1";
|
||||
static constexpr const char *SIGNATURE_ALGORITHM = "rsa_pss_rsae_sha256:ECDSA+SHA256";
|
||||
static constexpr const char *CIPHER_SUITE = "AES256-SHA256";
|
||||
static constexpr const char *SEND_DATA = "How do you do";
|
||||
static constexpr const char *SEND_DATA_EMPTY = "";
|
||||
static constexpr const size_t MAX_BUFFER_SIZE = 8192;
|
||||
const int PORT = 7838;
|
||||
const int SOCKET_FD = 5;
|
||||
const int SSL_ERROR_RETURN = -1;
|
||||
|
||||
TLSConnectOptions BaseOption()
|
||||
{
|
||||
TLSSecureOptions secureOption;
|
||||
SecureData structureData(PRI_KEY_FILE);
|
||||
secureOption.SetKey(structureData);
|
||||
std::vector<std::string> caChain;
|
||||
caChain.push_back(CA_CRT_FILE);
|
||||
secureOption.SetCaChain(caChain);
|
||||
secureOption.SetCert(CLIENT_FILE);
|
||||
secureOption.SetCipherSuite(CIPHER_SUITE);
|
||||
secureOption.SetSignatureAlgorithms(SIGNATURE_ALGORITHM);
|
||||
std::vector<std::string> protocol;
|
||||
protocol.push_back(PROTOCOL13);
|
||||
secureOption.SetProtocolChain(protocol);
|
||||
|
||||
TLSConnectOptions connectOptions;
|
||||
connectOptions.SetTlsSecureOptions(secureOption);
|
||||
Socket::NetAddress netAddress;
|
||||
netAddress.SetAddress(IP_ADDRESS);
|
||||
netAddress.SetPort(0);
|
||||
netAddress.SetFamilyBySaFamily(AF_INET);
|
||||
connectOptions.SetNetAddress(netAddress);
|
||||
std::vector<std::string> alpnProtocols;
|
||||
alpnProtocols.push_back(ALPN_PROTOCOL);
|
||||
connectOptions.SetAlpnProtocols(alpnProtocols);
|
||||
return connectOptions;
|
||||
}
|
||||
|
||||
HapInfoParams testInfoParms = {.bundleName = "TlsSocketBranchTest",
|
||||
.userID = 1,
|
||||
.instIndex = 0,
|
||||
.appIDDesc = "test",
|
||||
.isSystemApp = true};
|
||||
|
||||
PermissionDef testPermDef = {
|
||||
.permissionName = "ohos.permission.INTERNET",
|
||||
.bundleName = "TlsSocketBranchTest",
|
||||
.grantMode = 1,
|
||||
.label = "label",
|
||||
.labelId = 1,
|
||||
.description = "Test Tls Socket Branch",
|
||||
.descriptionId = 1,
|
||||
.availableLevel = APL_SYSTEM_BASIC,
|
||||
};
|
||||
|
||||
PermissionStateFull testState = {
|
||||
.grantFlags = {2},
|
||||
.grantStatus = {PermissionState::PERMISSION_GRANTED},
|
||||
.isGeneral = true,
|
||||
.permissionName = "ohos.permission.INTERNET",
|
||||
.resDeviceID = {"local"},
|
||||
};
|
||||
|
||||
HapPolicyParams testPolicyPrams = {
|
||||
.apl = APL_SYSTEM_BASIC,
|
||||
.domain = "test.domain",
|
||||
.permList = {testPermDef},
|
||||
.permStateList = {testState},
|
||||
};
|
||||
} // namespace
|
||||
|
||||
class AccessToken {
|
||||
public:
|
||||
AccessToken() : currentID_(GetSelfTokenID())
|
||||
{
|
||||
AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
|
||||
accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
|
||||
SetSelfTokenID(tokenIdEx.tokenIDEx);
|
||||
}
|
||||
~AccessToken()
|
||||
{
|
||||
AccessTokenKit::DeleteToken(accessID_);
|
||||
SetSelfTokenID(currentID_);
|
||||
}
|
||||
|
||||
private:
|
||||
AccessTokenID currentID_;
|
||||
AccessTokenID accessID_ = 0;
|
||||
};
|
||||
|
||||
class TlsSocketBranchTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase() {}
|
||||
|
||||
static void TearDownTestCase() {}
|
||||
|
||||
virtual void SetUp() {}
|
||||
|
||||
virtual void TearDown() {}
|
||||
};
|
||||
|
||||
HWTEST_F(TlsSocketBranchTest, BranchTest1, TestSize.Level2)
|
||||
{
|
||||
TLSSecureOptions secureOption;
|
||||
SecureData structureData(PRI_KEY_FILE);
|
||||
secureOption.SetKey(structureData);
|
||||
|
||||
SecureData keyPass(KEY_PASS);
|
||||
secureOption.SetKeyPass(keyPass);
|
||||
SecureData secureData = secureOption.GetKey();
|
||||
EXPECT_EQ(structureData.Length(), strlen(PRI_KEY_FILE));
|
||||
std::vector<std::string> caChain;
|
||||
caChain.push_back(CA_CRT_FILE);
|
||||
secureOption.SetCaChain(caChain);
|
||||
std::vector<std::string> getCaChain = secureOption.GetCaChain();
|
||||
EXPECT_NE(getCaChain.data(), nullptr);
|
||||
|
||||
secureOption.SetCert(CLIENT_FILE);
|
||||
std::string getCert = secureOption.GetCert();
|
||||
EXPECT_NE(getCert.data(), nullptr);
|
||||
|
||||
std::vector<std::string> protocolVec = {PROTOCOL12, PROTOCOL13};
|
||||
secureOption.SetProtocolChain(protocolVec);
|
||||
std::vector<std::string> getProtocol;
|
||||
getProtocol = secureOption.GetProtocolChain();
|
||||
|
||||
TLSSecureOptions copyOption = TLSSecureOptions(secureOption);
|
||||
TLSSecureOptions equalOption = secureOption;
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketBranchTest, BranchTest2, TestSize.Level2)
|
||||
{
|
||||
TLSSecureOptions secureOption;
|
||||
secureOption.SetUseRemoteCipherPrefer(false);
|
||||
bool isUseRemoteCipher = secureOption.UseRemoteCipherPrefer();
|
||||
EXPECT_FALSE(isUseRemoteCipher);
|
||||
|
||||
secureOption.SetSignatureAlgorithms(SIGNATURE_ALGORITHM);
|
||||
std::string getSignatureAlgorithm = secureOption.GetSignatureAlgorithms();
|
||||
EXPECT_STREQ(getSignatureAlgorithm.data(), SIGNATURE_ALGORITHM);
|
||||
|
||||
secureOption.SetCipherSuite(CIPHER_SUITE);
|
||||
std::string getCipherSuite = secureOption.GetCipherSuite();
|
||||
EXPECT_STREQ(getCipherSuite.data(), CIPHER_SUITE);
|
||||
|
||||
TLSSecureOptions copyOption = TLSSecureOptions(secureOption);
|
||||
TLSSecureOptions equalOption = secureOption;
|
||||
|
||||
TLSConnectOptions connectOptions;
|
||||
connectOptions.SetTlsSecureOptions(secureOption);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketBranchTest, BranchTest3, TestSize.Level2)
|
||||
{
|
||||
TLSSecureOptions secureOption;
|
||||
TLSConnectOptions connectOptions;
|
||||
connectOptions.SetTlsSecureOptions(secureOption);
|
||||
|
||||
Socket::NetAddress netAddress;
|
||||
netAddress.SetAddress(IP_ADDRESS);
|
||||
netAddress.SetPort(PORT);
|
||||
connectOptions.SetNetAddress(netAddress);
|
||||
Socket::NetAddress getNetAddress = connectOptions.GetNetAddress();
|
||||
std::string address = getNetAddress.GetAddress();
|
||||
EXPECT_STREQ(IP_ADDRESS, address.data());
|
||||
int port = getNetAddress.GetPort();
|
||||
EXPECT_EQ(port, PORT);
|
||||
netAddress.SetFamilyBySaFamily(AF_INET6);
|
||||
sa_family_t getFamily = netAddress.GetSaFamily();
|
||||
EXPECT_EQ(getFamily, AF_INET6);
|
||||
|
||||
std::vector<std::string> alpnProtocols;
|
||||
alpnProtocols.push_back(ALPN_PROTOCOL);
|
||||
connectOptions.SetAlpnProtocols(alpnProtocols);
|
||||
std::vector<std::string> getAlpnProtocols;
|
||||
getAlpnProtocols = connectOptions.GetAlpnProtocols();
|
||||
EXPECT_STREQ(getAlpnProtocols[0].data(), alpnProtocols[0].data());
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketBranchTest, BranchTest4, TestSize.Level2)
|
||||
{
|
||||
TLSSecureOptions secureOption;
|
||||
SecureData structureData(PRI_KEY_FILE);
|
||||
secureOption.SetKey(structureData);
|
||||
std::vector<std::string> caChain;
|
||||
caChain.push_back(CA_CRT_FILE);
|
||||
secureOption.SetCaChain(caChain);
|
||||
secureOption.SetCert(CLIENT_FILE);
|
||||
|
||||
TLSConnectOptions connectOptions;
|
||||
connectOptions.SetTlsSecureOptions(secureOption);
|
||||
|
||||
Socket::NetAddress netAddress;
|
||||
netAddress.SetAddress(IP_ADDRESS);
|
||||
netAddress.SetPort(0);
|
||||
netAddress.SetFamilyBySaFamily(AF_INET);
|
||||
EXPECT_EQ(netAddress.GetSaFamily(), AF_INET);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketBranchTest, BranchTest5, TestSize.Level2)
|
||||
{
|
||||
TLSConnectOptions tlsConnectOptions = BaseOption();
|
||||
|
||||
AccessToken token;
|
||||
TLSSocket tlsSocket;
|
||||
tlsSocket.OnError(
|
||||
[](int32_t errorNumber, const std::string &errorString) { EXPECT_NE(TLSSOCKET_SUCCESS, errorNumber); });
|
||||
tlsSocket.Connect(tlsConnectOptions, [](int32_t errCode) { EXPECT_NE(TLSSOCKET_SUCCESS, errCode); });
|
||||
std::string getData;
|
||||
tlsSocket.OnMessage([&getData](const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) {
|
||||
EXPECT_STREQ(getData.data(), nullptr);
|
||||
});
|
||||
const std::string data = "how do you do?";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
tlsSocket.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_EQ(errCode, TLS_ERR_SSL_NULL); });
|
||||
tlsSocket.GetSignatureAlgorithms(
|
||||
[](int32_t errCode, const std::vector<std::string> &algorithms) { EXPECT_EQ(errCode, TLS_ERR_SSL_NULL); });
|
||||
tlsSocket.GetCertificate(
|
||||
[](int32_t errCode, const X509CertRawData &cert) { EXPECT_NE(errCode, TLSSOCKET_SUCCESS); });
|
||||
tlsSocket.GetCipherSuite(
|
||||
[](int32_t errCode, const std::vector<std::string> &suite) { EXPECT_EQ(errCode, TLS_ERR_SSL_NULL); });
|
||||
tlsSocket.GetProtocol([](int32_t errCode, const std::string &protocol) { EXPECT_EQ(errCode, TLSSOCKET_SUCCESS); });
|
||||
tlsSocket.GetRemoteCertificate(
|
||||
[](int32_t errCode, const X509CertRawData &cert) { EXPECT_EQ(errCode, TLS_ERR_SSL_NULL); });
|
||||
(void)tlsSocket.Close([](int32_t errCode) { EXPECT_FALSE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketBranchTest, BranchTest6, TestSize.Level2)
|
||||
{
|
||||
TLSConnectOptions connectOptions = BaseOption();
|
||||
|
||||
TLSSocket tlsSocket;
|
||||
TLSSocket::TLSSocketInternal *tlsSocketInternal = new TLSSocket::TLSSocketInternal();
|
||||
bool isConnectToHost = tlsSocketInternal->TlsConnectToHost(SOCKET_FD, connectOptions, false);
|
||||
EXPECT_FALSE(isConnectToHost);
|
||||
tlsSocketInternal->SetTlsConfiguration(connectOptions);
|
||||
|
||||
bool sendSslNull = tlsSocketInternal->Send(SEND_DATA);
|
||||
EXPECT_FALSE(sendSslNull);
|
||||
char buffer[MAX_BUFFER_SIZE];
|
||||
bzero(buffer, MAX_BUFFER_SIZE);
|
||||
int recvSslNull = tlsSocketInternal->Recv(buffer, MAX_BUFFER_SIZE);
|
||||
EXPECT_EQ(recvSslNull, SSL_ERROR_RETURN);
|
||||
bool closeSslNull = tlsSocketInternal->Close();
|
||||
EXPECT_FALSE(closeSslNull);
|
||||
tlsSocketInternal->ssl_ = SSL_new(SSL_CTX_new(TLS_client_method()));
|
||||
bool sendEmpty = tlsSocketInternal->Send(SEND_DATA_EMPTY);
|
||||
EXPECT_FALSE(sendEmpty);
|
||||
int recv = tlsSocketInternal->Recv(buffer, MAX_BUFFER_SIZE);
|
||||
EXPECT_EQ(recv, SSL_ERROR_RETURN);
|
||||
bool close = tlsSocketInternal->Close();
|
||||
EXPECT_FALSE(close);
|
||||
delete tlsSocketInternal;
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketBranchTest, BranchTest7, TestSize.Level2)
|
||||
{
|
||||
TLSSocket tlsSocket;
|
||||
TLSSocket::TLSSocketInternal *tlsSocketInternal = new TLSSocket::TLSSocketInternal();
|
||||
|
||||
std::vector<std::string> alpnProtocols;
|
||||
alpnProtocols.push_back(ALPN_PROTOCOL);
|
||||
bool alpnProSslNull = tlsSocketInternal->SetAlpnProtocols(alpnProtocols);
|
||||
EXPECT_FALSE(alpnProSslNull);
|
||||
std::vector<std::string> getCipherSuite = tlsSocketInternal->GetCipherSuite();
|
||||
EXPECT_EQ(getCipherSuite.size(), 0);
|
||||
bool setSharedSigals = tlsSocketInternal->SetSharedSigals();
|
||||
EXPECT_FALSE(setSharedSigals);
|
||||
tlsSocketInternal->ssl_ = SSL_new(SSL_CTX_new(TLS_client_method()));
|
||||
getCipherSuite = tlsSocketInternal->GetCipherSuite();
|
||||
EXPECT_NE(getCipherSuite.size(), 0);
|
||||
setSharedSigals = tlsSocketInternal->SetSharedSigals();
|
||||
EXPECT_FALSE(setSharedSigals);
|
||||
TLSConnectOptions connectOptions = BaseOption();
|
||||
bool alpnPro = tlsSocketInternal->SetAlpnProtocols(alpnProtocols);
|
||||
EXPECT_TRUE(alpnPro);
|
||||
|
||||
Socket::SocketRemoteInfo remoteInfo;
|
||||
tlsSocketInternal->hostName_ = IP_ADDRESS;
|
||||
tlsSocketInternal->port_ = PORT;
|
||||
tlsSocketInternal->family_ = AF_INET;
|
||||
tlsSocketInternal->MakeRemoteInfo(remoteInfo);
|
||||
getCipherSuite = tlsSocketInternal->GetCipherSuite();
|
||||
EXPECT_NE(getCipherSuite.size(), 0);
|
||||
|
||||
std::string getRemoteCert = tlsSocketInternal->GetRemoteCertificate();
|
||||
EXPECT_EQ(getRemoteCert, "");
|
||||
|
||||
std::vector<std::string> getSignatureAlgorithms = tlsSocketInternal->GetSignatureAlgorithms();
|
||||
EXPECT_EQ(getSignatureAlgorithms.size(), 0);
|
||||
|
||||
std::string getProtocol = tlsSocketInternal->GetProtocol();
|
||||
EXPECT_NE(getProtocol, "");
|
||||
|
||||
setSharedSigals = tlsSocketInternal->SetSharedSigals();
|
||||
EXPECT_FALSE(setSharedSigals);
|
||||
|
||||
ssl_st *ssl = tlsSocketInternal->GetSSL();
|
||||
EXPECT_NE(ssl, nullptr);
|
||||
delete tlsSocketInternal;
|
||||
}
|
||||
} // namespace TlsSocket
|
||||
} // namespace NetStack
|
||||
} // namespace OHOS
|
@ -0,0 +1,318 @@
|
||||
/*
|
||||
* Copyright (c) 2022-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 "net_address.h"
|
||||
#include "secure_data.h"
|
||||
#include "socket_error.h"
|
||||
#include "socket_state_base.h"
|
||||
#include "tls.h"
|
||||
#include "tls_certificate.h"
|
||||
#include "tls_configuration.h"
|
||||
#include "tls_key.h"
|
||||
#include "tls_socket.h"
|
||||
#include "tls_utils_test.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace NetStack {
|
||||
namespace TlsSocket {
|
||||
void MockCertChainOneWayNetAddress(Socket::NetAddress &address)
|
||||
{
|
||||
address.SetAddress(TlsUtilsTest::GetIp(TlsUtilsTest::ChangeToFile(IP_ADDRESS)));
|
||||
address.SetPort(std::atoi(TlsUtilsTest::ChangeToFile(PORT).c_str()));
|
||||
address.SetFamilyBySaFamily(AF_INET);
|
||||
}
|
||||
|
||||
void MockCertChainOneWayParamOptions(
|
||||
Socket::NetAddress &address, TLSSecureOptions &secureOption, TLSConnectOptions &options)
|
||||
{
|
||||
secureOption.SetKey(SecureData(TlsUtilsTest::ChangeToFile(PRIVATE_KEY_PEM_CHAIN)));
|
||||
secureOption.SetCert(TlsUtilsTest::ChangeToFile(CLIENT_CRT_CHAIN));
|
||||
|
||||
MockCertChainOneWayNetAddress(address);
|
||||
options.SetTlsSecureOptions(secureOption);
|
||||
options.SetNetAddress(address);
|
||||
}
|
||||
|
||||
void SetCertChainOneWayHwTestShortParam(TLSSocket &server)
|
||||
{
|
||||
TLSConnectOptions options;
|
||||
TLSSecureOptions secureOption;
|
||||
Socket::NetAddress address;
|
||||
|
||||
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
|
||||
TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
|
||||
secureOption.SetCaChain(caVec);
|
||||
MockCertChainOneWayParamOptions(address, secureOption, options);
|
||||
server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
void SetCertChainOneWayHwTestLongParam(TLSSocket &server)
|
||||
{
|
||||
TLSConnectOptions options;
|
||||
TLSSecureOptions secureOption;
|
||||
Socket::NetAddress address;
|
||||
|
||||
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
|
||||
TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
|
||||
std::string protocolV13 = "TLSv1.3";
|
||||
std::vector<std::string> protocolVec = { protocolV13 };
|
||||
secureOption.SetCaChain(caVec);
|
||||
secureOption.SetCipherSuite("AES256-SHA256");
|
||||
secureOption.SetProtocolChain(protocolVec);
|
||||
MockCertChainOneWayParamOptions(address, secureOption, options);
|
||||
|
||||
server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, bindInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("bindInterface")) {
|
||||
return;
|
||||
}
|
||||
|
||||
TLSSocket srv;
|
||||
Socket::NetAddress address;
|
||||
MockCertChainOneWayNetAddress(address);
|
||||
srv.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, connectInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("connectInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainOneWayService;
|
||||
SetCertChainOneWayHwTestShortParam(certChainOneWayService);
|
||||
|
||||
const std::string data = "how do you do? this is connectInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
certChainOneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
sleep(2);
|
||||
|
||||
(void)certChainOneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("closeInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainOneWayService;
|
||||
SetCertChainOneWayHwTestShortParam(certChainOneWayService);
|
||||
|
||||
const std::string data = "how do you do? this is closeInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
|
||||
certChainOneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
sleep(2);
|
||||
|
||||
(void)certChainOneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("sendInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainOneWayService;
|
||||
SetCertChainOneWayHwTestShortParam(certChainOneWayService);
|
||||
|
||||
const std::string data = "how do you do? this is sendInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
|
||||
certChainOneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
sleep(2);
|
||||
|
||||
(void)certChainOneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteAddressInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainOneWayService;
|
||||
TLSConnectOptions options;
|
||||
TLSSecureOptions secureOption;
|
||||
Socket::NetAddress address;
|
||||
|
||||
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
|
||||
TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
|
||||
secureOption.SetCaChain(caVec);
|
||||
MockCertChainOneWayParamOptions(address, secureOption, options);
|
||||
|
||||
certChainOneWayService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
certChainOneWayService.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
Socket::NetAddress netAddress;
|
||||
certChainOneWayService.GetRemoteAddress([&netAddress](int32_t errCode, const Socket::NetAddress &address) {
|
||||
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
|
||||
netAddress.SetPort(address.GetPort());
|
||||
netAddress.SetAddress(address.GetAddress());
|
||||
netAddress.SetFamilyBySaFamily(address.GetSaFamily());
|
||||
});
|
||||
EXPECT_STREQ(netAddress.GetAddress().c_str(),
|
||||
TlsUtilsTest::GetIp(TlsUtilsTest::ChangeToFile(IP_ADDRESS)).c_str());
|
||||
EXPECT_EQ(address.GetPort(), std::atoi(TlsUtilsTest::ChangeToFile(PORT).c_str()));
|
||||
EXPECT_EQ(netAddress.GetSaFamily(), AF_INET);
|
||||
|
||||
const std::string data = "how do you do? this is getRemoteAddressInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
|
||||
certChainOneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
sleep(2);
|
||||
|
||||
(void)certChainOneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteAddressInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainOneWayService;
|
||||
SetCertChainOneWayHwTestShortParam(certChainOneWayService);
|
||||
|
||||
Socket::SocketStateBase TlsSocketstate;
|
||||
certChainOneWayService.GetState([&TlsSocketstate](int32_t errCode, const Socket::SocketStateBase &state) {
|
||||
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
|
||||
TlsSocketstate = state;
|
||||
});
|
||||
std::cout << "TlsSocketOneWayTest TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl;
|
||||
EXPECT_TRUE(TlsSocketstate.IsBound());
|
||||
EXPECT_TRUE(!TlsSocketstate.IsClose());
|
||||
EXPECT_TRUE(TlsSocketstate.IsConnected());
|
||||
|
||||
const std::string tlsSocketOneWayTestData = "how do you do? this is getStateInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(tlsSocketOneWayTestData);
|
||||
certChainOneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
sleep(2);
|
||||
|
||||
(void)certChainOneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteCertificateInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainOneWayService;
|
||||
SetCertChainOneWayHwTestShortParam(certChainOneWayService);
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
|
||||
const std::string data = "how do you do? This is UT test getRemoteCertificateInterface";
|
||||
tcpSendOptions.SetData(data);
|
||||
certChainOneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
certChainOneWayService.GetRemoteCertificate(
|
||||
[](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
sleep(2);
|
||||
(void)certChainOneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("protocolInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainOneWayService;
|
||||
SetCertChainOneWayHwTestLongParam(certChainOneWayService);
|
||||
|
||||
const std::string testData = "how do you do? this is protocolInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(testData);
|
||||
|
||||
certChainOneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
std::string getProtocolResult;
|
||||
certChainOneWayService.GetProtocol([&getProtocolResult](int32_t errCode, const std::string &protocol) {
|
||||
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
|
||||
getProtocolResult = protocol;
|
||||
});
|
||||
EXPECT_STREQ(getProtocolResult.c_str(), "TLSv1.3");
|
||||
sleep(2);
|
||||
|
||||
(void)certChainOneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getCipherSuiteInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainOneWayService;
|
||||
SetCertChainOneWayHwTestLongParam(certChainOneWayService);
|
||||
|
||||
bool oneWayTestFlag = false;
|
||||
const std::string data = "how do you do? This is getCipherSuiteInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
certChainOneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
std::vector<std::string> oneWayTestSuite;
|
||||
certChainOneWayService.GetCipherSuite([&oneWayTestSuite](int32_t errCode, const std::vector<std::string> &suite) {
|
||||
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
|
||||
oneWayTestSuite = suite;
|
||||
});
|
||||
|
||||
for (auto const &iter : oneWayTestSuite) {
|
||||
if (iter == "AES256-SHA256") {
|
||||
oneWayTestFlag = true;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(oneWayTestFlag);
|
||||
sleep(2);
|
||||
|
||||
(void)certChainOneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, onMessageDataInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("tlsSocketOnMessageData")) {
|
||||
return;
|
||||
}
|
||||
std::string getData = "server->client";
|
||||
TLSSocket certChainOneWayService;
|
||||
SetCertChainOneWayHwTestLongParam(certChainOneWayService);
|
||||
|
||||
certChainOneWayService.OnMessage([&getData](const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) {
|
||||
if (data == getData) {
|
||||
EXPECT_TRUE(true);
|
||||
} else {
|
||||
EXPECT_TRUE(false);
|
||||
}
|
||||
});
|
||||
|
||||
const std::string data = "how do you do? this is tlsSocketOnMessageData";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
certChainOneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
sleep(2);
|
||||
(void)certChainOneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
} // namespace TlsSocket
|
||||
} // namespace NetStack
|
||||
} // namespace OHOS
|
380
test/unittest/test_tls/clientTest/TlsSocketCertChainTest.cpp
Normal file
380
test/unittest/test_tls/clientTest/TlsSocketCertChainTest.cpp
Normal file
@ -0,0 +1,380 @@
|
||||
/*
|
||||
* Copyright (c) 2022-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 "net_address.h"
|
||||
#include "secure_data.h"
|
||||
#include "socket_error.h"
|
||||
#include "socket_state_base.h"
|
||||
#include "tls.h"
|
||||
#include "tls_certificate.h"
|
||||
#include "tls_configuration.h"
|
||||
#include "tls_key.h"
|
||||
#include "tls_socket.h"
|
||||
#include "tls_utils_test.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace NetStack {
|
||||
namespace TlsSocket {
|
||||
void MockCertChainNetAddress(Socket::NetAddress &address)
|
||||
{
|
||||
address.SetAddress(TlsUtilsTest::GetIp(TlsUtilsTest::ChangeToFile(IP_ADDRESS)));
|
||||
address.SetPort(std::atoi(TlsUtilsTest::ChangeToFile(PORT).c_str()));
|
||||
address.SetFamilyBySaFamily(AF_INET);
|
||||
}
|
||||
|
||||
void MockCertChainParamOptions(Socket::NetAddress &address, TLSSecureOptions &secureOption, TLSConnectOptions &options)
|
||||
{
|
||||
secureOption.SetKey(SecureData(TlsUtilsTest::ChangeToFile(PRIVATE_KEY_PEM_CHAIN)));
|
||||
secureOption.SetCert(TlsUtilsTest::ChangeToFile(CLIENT_CRT_CHAIN));
|
||||
|
||||
MockCertChainNetAddress(address);
|
||||
options.SetNetAddress(address);
|
||||
options.SetTlsSecureOptions(secureOption);
|
||||
}
|
||||
|
||||
void SetCertChainHwTestShortParam(TLSSocket &server)
|
||||
{
|
||||
TLSConnectOptions options;
|
||||
TLSSecureOptions secureOption;
|
||||
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
|
||||
TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
|
||||
secureOption.SetCaChain(caVec);
|
||||
Socket::NetAddress address;
|
||||
MockCertChainParamOptions(address, secureOption, options);
|
||||
|
||||
server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
void SetCertChainHwTestLongParam(TLSSocket &server)
|
||||
{
|
||||
Socket::NetAddress address;
|
||||
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
|
||||
TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
|
||||
TLSSecureOptions secureOption;
|
||||
secureOption.SetCaChain(caVec);
|
||||
std::string protocolV13 = "TLSv1.3";
|
||||
std::vector<std::string> protocolVec = { protocolV13 };
|
||||
secureOption.SetProtocolChain(protocolVec);
|
||||
secureOption.SetCipherSuite("AES256-SHA256");
|
||||
|
||||
TLSConnectOptions options;
|
||||
MockCertChainParamOptions(address, secureOption, options);
|
||||
|
||||
server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, bindInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("bindInterface")) {
|
||||
return;
|
||||
}
|
||||
|
||||
TLSSocket testServer;
|
||||
Socket::NetAddress address;
|
||||
MockCertChainNetAddress(address);
|
||||
testServer.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, connectInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("connectInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainServer;
|
||||
SetCertChainHwTestShortParam(certChainServer);
|
||||
|
||||
const std::string data = "how do you do? this is connectInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
sleep(2);
|
||||
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("closeInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainServer;
|
||||
SetCertChainHwTestShortParam(certChainServer);
|
||||
|
||||
const std::string data = "how do you do? this is closeInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
|
||||
certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
sleep(2);
|
||||
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("sendInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainServer;
|
||||
SetCertChainHwTestShortParam(certChainServer);
|
||||
|
||||
const std::string data = "how do you do? this is sendInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
|
||||
certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
sleep(2);
|
||||
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteAddressInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainServer;
|
||||
TLSConnectOptions options;
|
||||
TLSSecureOptions secureOption;
|
||||
Socket::NetAddress address;
|
||||
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
|
||||
TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
|
||||
secureOption.SetCaChain(caVec);
|
||||
MockCertChainParamOptions(address, secureOption, options);
|
||||
|
||||
certChainServer.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
certChainServer.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
Socket::NetAddress netAddress;
|
||||
certChainServer.GetRemoteAddress([&netAddress](int32_t errCode, const Socket::NetAddress &address) {
|
||||
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
|
||||
netAddress.SetPort(address.GetPort());
|
||||
netAddress.SetFamilyBySaFamily(address.GetSaFamily());
|
||||
netAddress.SetAddress(address.GetAddress());
|
||||
});
|
||||
EXPECT_STREQ(netAddress.GetAddress().c_str(), TlsUtilsTest::GetIp(TlsUtilsTest::ChangeToFile(IP_ADDRESS)).c_str());
|
||||
EXPECT_EQ(address.GetPort(), std::atoi(TlsUtilsTest::ChangeToFile(PORT).c_str()));
|
||||
EXPECT_EQ(netAddress.GetSaFamily(), AF_INET);
|
||||
|
||||
const std::string data = "how do you do? this is getRemoteAddressInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
|
||||
certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteAddressInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainServer;
|
||||
SetCertChainHwTestShortParam(certChainServer);
|
||||
|
||||
Socket::SocketStateBase TlsSocketstate;
|
||||
certChainServer.GetState([&TlsSocketstate](int32_t errCode, const Socket::SocketStateBase &state) {
|
||||
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
|
||||
TlsSocketstate = state;
|
||||
});
|
||||
std::cout << "TlsSocketCertChainTest TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl;
|
||||
EXPECT_TRUE(TlsSocketstate.IsBound());
|
||||
EXPECT_TRUE(!TlsSocketstate.IsClose());
|
||||
EXPECT_TRUE(TlsSocketstate.IsConnected());
|
||||
|
||||
const std::string tlsSocketCertChainTestData = "how do you do? this is getStateInterface";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(tlsSocketCertChainTestData);
|
||||
certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
sleep(2);
|
||||
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getCertificateInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getCertificateInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainServer;
|
||||
SetCertChainHwTestShortParam(certChainServer);
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
const std::string data = "how do you do? This is UT test getCertificateInterface";
|
||||
|
||||
tcpSendOptions.SetData(data);
|
||||
certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
certChainServer.GetCertificate(
|
||||
[](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
sleep(2);
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteCertificateInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainServer;
|
||||
SetCertChainHwTestShortParam(certChainServer);
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
const std::string data = "how do you do? This is UT test getRemoteCertificateInterface";
|
||||
tcpSendOptions.SetData(data);
|
||||
|
||||
certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
certChainServer.GetRemoteCertificate(
|
||||
[](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
sleep(2);
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("protocolInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainServer;
|
||||
SetCertChainHwTestLongParam(certChainServer);
|
||||
|
||||
const std::string data = "how do you do? this is protocolInterface.";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
|
||||
certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
std::string getProtocolVal = "";
|
||||
certChainServer.GetProtocol([&getProtocolVal](int32_t errCode, const std::string &protocol) {
|
||||
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
|
||||
getProtocolVal = protocol;
|
||||
});
|
||||
EXPECT_STREQ(getProtocolVal.c_str(), "TLSv1.3");
|
||||
sleep(2);
|
||||
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getCipherSuiteInterface")) {
|
||||
return;
|
||||
}
|
||||
TLSSocket certChainServer;
|
||||
SetCertChainHwTestLongParam(certChainServer);
|
||||
|
||||
bool successFlag = false;
|
||||
const std::string data = "how do you do? This is getCipherSuiteInterface";
|
||||
Socket::TCPSendOptions testTcpSendOptions;
|
||||
testTcpSendOptions.SetData(data);
|
||||
certChainServer.Send(testTcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
std::vector<std::string> testCipherSuite;
|
||||
certChainServer.GetCipherSuite([&testCipherSuite](int32_t errCode, const std::vector<std::string> &suite) {
|
||||
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
|
||||
testCipherSuite = suite;
|
||||
});
|
||||
|
||||
for (auto const &iter : testCipherSuite) {
|
||||
if (iter == "AES256-SHA256") {
|
||||
successFlag = true;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(successFlag);
|
||||
sleep(2);
|
||||
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, getSignatureAlgorithmsInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("getSignatureAlgorithmsInterface")) {
|
||||
return;
|
||||
}
|
||||
|
||||
TLSSocket certChainServer;
|
||||
TLSSecureOptions secureOption;
|
||||
std::string signatureAlgorithmVec = {"rsa_pss_rsae_sha256:ECDSA+SHA256"};
|
||||
secureOption.SetSignatureAlgorithms(signatureAlgorithmVec);
|
||||
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
|
||||
TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
|
||||
secureOption.SetCaChain(caVec);
|
||||
std::string protocolV13 = "TLSv1.3";
|
||||
std::vector<std::string> protocolVec = {protocolV13};
|
||||
secureOption.SetProtocolChain(protocolVec);
|
||||
Socket::NetAddress address;
|
||||
TLSConnectOptions options;
|
||||
MockCertChainParamOptions(address, secureOption, options);
|
||||
|
||||
bool successFlag = false;
|
||||
certChainServer.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
certChainServer.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
const std::string data = "how do you do? this is getSignatureAlgorithmsInterface";
|
||||
Socket::TCPSendOptions testOptions;
|
||||
testOptions.SetData(data);
|
||||
certChainServer.Send(testOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
std::vector<std::string> testSignatureAlgorithms;
|
||||
certChainServer.GetSignatureAlgorithms(
|
||||
[&testSignatureAlgorithms](int32_t errCode, const std::vector<std::string> &algorithms) {
|
||||
if (errCode == TLSSOCKET_SUCCESS) {
|
||||
testSignatureAlgorithms = algorithms;
|
||||
}
|
||||
});
|
||||
|
||||
for (auto const &iter : testSignatureAlgorithms) {
|
||||
if (iter == "ECDSA+SHA256") {
|
||||
successFlag = true;
|
||||
}
|
||||
}
|
||||
EXPECT_TRUE(successFlag);
|
||||
sleep(2);
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
|
||||
HWTEST_F(TlsSocketTest, onMessageDataInterface, testing::ext::TestSize.Level2)
|
||||
{
|
||||
if (!TlsUtilsTest::CheckCaPathChainExistence("tlsSocketOnMessageData")) {
|
||||
return;
|
||||
}
|
||||
std::string getData = "server->client";
|
||||
TLSSocket certChainServer;
|
||||
SetCertChainHwTestLongParam(certChainServer);
|
||||
certChainServer.OnMessage([&getData](const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) {
|
||||
if (data == getData) {
|
||||
EXPECT_TRUE(true);
|
||||
} else {
|
||||
EXPECT_TRUE(false);
|
||||
}
|
||||
});
|
||||
|
||||
const std::string data = "how do you do? this is tlsSocketOnMessageData";
|
||||
Socket::TCPSendOptions tcpSendOptions;
|
||||
tcpSendOptions.SetData(data);
|
||||
certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
|
||||
sleep(2);
|
||||
(void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
|
||||
}
|
||||
} // namespace TlsSocket
|
||||
} // namespace NetStack
|
||||
} // namespace OHOS
|
Loading…
Reference in New Issue
Block a user