新增测试用例

Signed-off-by: maqianli <15735184237@163.com>
This commit is contained in:
m30063213 2024-11-14 16:06:47 +08:00
parent 6ae9ea0b80
commit 94aafd6490
15 changed files with 3401 additions and 0 deletions

View File

@ -0,0 +1,58 @@
/*
* 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 "secure_data.h"
namespace OHOS {
namespace NetStack {
namespace TlsSocket {
namespace {
using namespace testing::ext;
} // namespace
class SecureDataTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
HWTEST_F(SecureDataTest, stringData, TestSize.Level2)
{
std::string testString = "Secure Data string Test";
SecureData structureData(testString);
EXPECT_STREQ(structureData.Data(), "Secure Data string Test");
SecureData copyData = structureData;
EXPECT_STREQ(copyData.Data(), "Secure Data string Test");
SecureData equalData;
equalData = structureData;
EXPECT_STREQ(equalData.Data(), "Secure Data string Test");
SecureData defaultData;
EXPECT_EQ(defaultData.Length(), 0);
}
} // namespace TlsSocket
} // namespace NetStack
} // namespace OHOS

View File

@ -0,0 +1,53 @@
/*
* 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 <gtest/gtest.h>
#include <regex>
#include "socket_error.h"
namespace OHOS {
namespace NetStack {
namespace TlsSocket {
namespace {
using namespace testing::ext;
const int32_t ERROR_NOT_EXIT = 2303800;
} // namespace
class SocketErrorTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
HWTEST_F(SocketErrorTest, SocketError, TestSize.Level2)
{
std::string errorMsg = MakeErrorMessage(TLS_ERR_SYS_EINTR);
EXPECT_STREQ(errorMsg.data(), "Interrupted system call");
}
HWTEST_F(SocketErrorTest, SocketError2, TestSize.Level2)
{
std::string errorMsg = MakeErrorMessage(ERROR_NOT_EXIT);
std::regex value("^error:000002BC:lib.{5,12}reason.{5}");
EXPECT_TRUE(std::regex_match(errorMsg, value) == true);
}
} // namespace TlsSocket
} // namespace NetStack
} // namespace OHOS

View 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

View 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

View 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

View File

@ -0,0 +1,247 @@
/*
* 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>
#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

View 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

View File

@ -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

View 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

View File

@ -0,0 +1,326 @@
/*
* 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 MockOneWayNetAddress(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 MockOneWayParamOptions(Socket::NetAddress &address, TLSSecureOptions &secureOption, TLSConnectOptions &options)
{
secureOption.SetKey(SecureData(TlsUtilsTest::ChangeToFile(PRIVATE_KEY_PEM)));
secureOption.SetCert(TlsUtilsTest::ChangeToFile(CLIENT_CRT));
MockOneWayNetAddress(address);
options.SetTlsSecureOptions(secureOption);
options.SetNetAddress(address);
}
void SetOneWayHwTestShortParam(TLSSocket &server)
{
TLSSecureOptions secureOption;
Socket::NetAddress address;
TLSConnectOptions options;
std::vector<std::string> caVec = {TlsUtilsTest::ChangeToFile(CA_DER)};
secureOption.SetCaChain(caVec);
MockOneWayParamOptions(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 SetOneWayHwTestLongParam(TLSSocket &server)
{
Socket::NetAddress address;
TLSSecureOptions secureOption;
std::vector<std::string> caVec = {TlsUtilsTest::ChangeToFile(CA_DER)};
secureOption.SetCaChain(caVec);
secureOption.SetCipherSuite("AES256-SHA256");
std::string protocolV13 = "TLSv1.3";
std::vector<std::string> protocolVec = {protocolV13};
secureOption.SetProtocolChain(protocolVec);
TLSConnectOptions options;
MockOneWayParamOptions(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::CheckCaFileExistence("bindInterface")) {
return;
}
TLSSocket oneWayService;
Socket::NetAddress address;
MockOneWayNetAddress(address);
oneWayService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, connectInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("connectInterface")) {
return;
}
TLSSocket oneWayService;
SetOneWayHwTestShortParam(oneWayService);
const std::string data = "how do you do? this is connectInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
oneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)oneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
}
HWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("closeInterface")) {
return;
}
TLSSocket oneWayService;
SetOneWayHwTestShortParam(oneWayService);
const std::string data = "how do you do? this is closeInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
oneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)oneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("sendInterface")) {
return;
}
TLSSocket oneWayService;
SetOneWayHwTestShortParam(oneWayService);
const std::string data = "how do you do? this is sendInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
oneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)oneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getRemoteAddressInterface")) {
return;
}
TLSSocket oneWayService;
TLSConnectOptions options;
TLSSecureOptions secureOption;
Socket::NetAddress address;
std::vector<std::string> caVec = {TlsUtilsTest::ChangeToFile(CA_DER)};
secureOption.SetCaChain(caVec);
MockOneWayParamOptions(address, secureOption, options);
oneWayService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
oneWayService.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
Socket::NetAddress netAddress;
oneWayService.GetRemoteAddress([&netAddress](int32_t errCode, const Socket::NetAddress &address) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
netAddress.SetAddress(address.GetAddress());
netAddress.SetPort(address.GetPort());
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);
oneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
(void)oneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getRemoteAddressInterface")) {
return;
}
TLSSocket oneWayService;
SetOneWayHwTestShortParam(oneWayService);
Socket::SocketStateBase TlsSocketstate;
oneWayService.GetState([&TlsSocketstate](int32_t errCode, const Socket::SocketStateBase &state) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
TlsSocketstate = state;
});
std::cout << "TlsSocketTest TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl;
EXPECT_TRUE(TlsSocketstate.IsBound());
EXPECT_TRUE(!TlsSocketstate.IsClose());
EXPECT_TRUE(TlsSocketstate.IsConnected());
const std::string tlsSocketTestData = "how do you do? this is getStateInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(tlsSocketTestData);
oneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)oneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getRemoteCertificateInterface")) {
return;
}
TLSSocket oneWayService;
SetOneWayHwTestShortParam(oneWayService);
const std::string data = "how do you do? This is UT test getRemoteCertificateInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
oneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
oneWayService.GetRemoteCertificate(
[](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)oneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("protocolInterface")) {
return;
}
TLSSocket oneWayService;
SetOneWayHwTestLongParam(oneWayService);
const std::string data = "how do you do? this is protocolInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
oneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
std::string protocolVal;
oneWayService.GetProtocol([&protocolVal](int32_t errCode, const std::string &protocol) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
protocolVal = protocol;
});
EXPECT_STREQ(protocolVal.c_str(), "TLSv1.3");
Socket::SocketStateBase stateBase;
oneWayService.GetState([&stateBase](int32_t errCode, Socket::SocketStateBase state) {
if (errCode == TLSSOCKET_SUCCESS) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
stateBase.SetIsBound(state.IsBound());
stateBase.SetIsClose(state.IsClose());
stateBase.SetIsConnected(state.IsConnected());
}
});
EXPECT_TRUE(stateBase.IsConnected());
sleep(2);
(void)oneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getCipherSuiteInterface")) {
return;
}
TLSSocket oneWayService;
SetOneWayHwTestLongParam(oneWayService);
bool flag = false;
const std::string data = "how do you do? This is getCipherSuiteInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
oneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
std::vector<std::string> testSuite;
oneWayService.GetCipherSuite([&testSuite](int32_t errCode, const std::vector<std::string> &suite) {
if (errCode == TLSSOCKET_SUCCESS) {
testSuite = suite;
}
});
for (auto const &iter : testSuite) {
if (iter == "AES256-SHA256") {
flag = true;
}
}
EXPECT_TRUE(flag);
sleep(2);
(void)oneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, onMessageDataInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("tlsSocketOnMessageData")) {
return;
}
std::string getData = "server->client";
TLSSocket oneWayService;
SetOneWayHwTestLongParam(oneWayService);
oneWayService.OnMessage([&getData](const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) {
EXPECT_TRUE(data == getData);
});
const std::string data = "how do you do? this is tlsSocketOnMessageData";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
oneWayService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)oneWayService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
} // namespace TlsSocket
} // namespace NetStack
} // namespace OHOS

View File

@ -0,0 +1,457 @@
/*
* 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 MockNetAddress(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 MockTlsSocketParamOptions(Socket::NetAddress &address, TLSSecureOptions &secureOption, TLSConnectOptions &options)
{
secureOption.SetKey(SecureData(TlsUtilsTest::ChangeToFile(PRIVATE_KEY_PEM)));
secureOption.SetCert(TlsUtilsTest::ChangeToFile(CLIENT_CRT));
MockNetAddress(address);
options.SetTlsSecureOptions(secureOption);
options.SetNetAddress(address);
}
void SetSocketHwTestShortParam(TLSSocket &server)
{
TLSConnectOptions options;
Socket::NetAddress address;
TLSSecureOptions secureOption;
std::vector<std::string> caVec1 = {TlsUtilsTest::ChangeToFile(CA_DER)};
secureOption.SetCaChain(caVec1);
MockTlsSocketParamOptions(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 SetSocketHwTestLongParam(TLSSocket &server)
{
TLSConnectOptions options;
TLSSecureOptions secureOption;
secureOption.SetCipherSuite("AES256-SHA256");
std::string protocolV13 = "TLSv1.3";
std::vector<std::string> protocolVec = {protocolV13};
secureOption.SetProtocolChain(protocolVec);
std::vector<std::string> caVect = {TlsUtilsTest::ChangeToFile(CA_DER)};
secureOption.SetCaChain(caVect);
Socket::NetAddress address;
MockTlsSocketParamOptions(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::CheckCaFileExistence("bindInterface")) {
return;
}
Socket::NetAddress address;
TLSSocket bindTestServer;
MockNetAddress(address);
bindTestServer.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, connectInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("connectInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestShortParam(testService);
const std::string data = "how do you do? this is connectInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
}
HWTEST_F(TlsSocketTest, startReadMessageInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("startReadMessageInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestShortParam(testService);
const std::string data = "how do you do? this is startReadMessageInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, readMessageInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("readMessageInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestShortParam(testService);
const std::string data = "how do you do? this is readMessageInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("closeInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestShortParam(testService);
const std::string data = "how do you do? this is closeInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("sendInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestShortParam(testService);
const std::string data = "how do you do? this is sendInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getRemoteAddressInterface")) {
return;
}
TLSConnectOptions options;
TLSSocket testService;
TLSSecureOptions secureOption;
Socket::NetAddress address;
std::vector<std::string> caVec = {TlsUtilsTest::ChangeToFile(CA_DER)};
secureOption.SetCaChain(caVec);
MockTlsSocketParamOptions(address, secureOption, options);
testService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
testService.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
Socket::NetAddress netAddress;
testService.GetRemoteAddress([&netAddress](int32_t errCode, const Socket::NetAddress &address) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
netAddress.SetAddress(address.GetAddress());
netAddress.SetFamilyBySaFamily(address.GetSaFamily());
netAddress.SetPort(address.GetPort());
});
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);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getRemoteAddressInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestShortParam(testService);
Socket::SocketStateBase TlsSocketstate;
testService.GetState([&TlsSocketstate](int32_t errCode, const Socket::SocketStateBase &state) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
TlsSocketstate = state;
});
std::cout << "TlsSocketTest TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl;
EXPECT_TRUE(TlsSocketstate.IsBound());
EXPECT_TRUE(!TlsSocketstate.IsClose());
EXPECT_TRUE(TlsSocketstate.IsConnected());
const std::string tlsSocketTestData = "how do you do? this is getStateInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(tlsSocketTestData);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getCertificateInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getCertificateInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestShortParam(testService);
const std::string data = "how do you do? This is UT test getCertificateInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
testService.GetCertificate(
[](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getRemoteCertificateInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestShortParam(testService);
Socket::TCPSendOptions tcpSendOptions;
const std::string data = "how do you do? This is UT test getRemoteCertificateInterface";
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
testService.GetRemoteCertificate(
[](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("protocolInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestLongParam(testService);
const std::string data = "how do you do? this is protocolInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
std::string protocolVal;
testService.GetProtocol([&protocolVal](int32_t errCode, const std::string &protocol) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
protocolVal = protocol;
});
EXPECT_STREQ(protocolVal.c_str(), "TLSv1.3");
Socket::SocketStateBase socketStateBase;
testService.GetState([&socketStateBase](int32_t errCode, Socket::SocketStateBase state) {
if (errCode == TLSSOCKET_SUCCESS) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
socketStateBase.SetIsBound(state.IsBound());
socketStateBase.SetIsClose(state.IsClose());
socketStateBase.SetIsConnected(state.IsConnected());
}
});
EXPECT_TRUE(socketStateBase.IsConnected());
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getCipherSuiteInterface")) {
return;
}
TLSSocket testService;
SetSocketHwTestLongParam(testService);
bool flag = false;
const std::string data = "how do you do? This is getCipherSuiteInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
std::vector<std::string> cipherSuite;
testService.GetCipherSuite([&cipherSuite](int32_t errCode, const std::vector<std::string> &suite) {
if (errCode == TLSSOCKET_SUCCESS) {
cipherSuite = suite;
}
});
for (auto const &iter : cipherSuite) {
if (iter == "AES256-SHA256") {
flag = true;
}
}
EXPECT_TRUE(flag);
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getSignatureAlgorithmsInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("getSignatureAlgorithmsInterface")) {
return;
}
TLSSocket testService;
TLSConnectOptions options;
TLSSecureOptions secureOption;
Socket::NetAddress address;
std::string signatureAlgorithmVec = {"rsa_pss_rsae_sha256:ECDSA+SHA256"};
secureOption.SetSignatureAlgorithms(signatureAlgorithmVec);
std::string protocolV13 = "TLSv1.3";
std::vector<std::string> protocolVec = {protocolV13};
secureOption.SetProtocolChain(protocolVec);
std::vector<std::string> caVec = {TlsUtilsTest::ChangeToFile(CA_DER)};
secureOption.SetCaChain(caVec);
MockTlsSocketParamOptions(address, secureOption, options);
testService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
testService.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
bool flag = false;
const std::string data = "how do you do? this is getSignatureAlgorithmsInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
std::vector<std::string> signatureAlgorithms;
testService.GetSignatureAlgorithms(
[&signatureAlgorithms](int32_t errCode, const std::vector<std::string> &algorithms) {
if (errCode == TLSSOCKET_SUCCESS) {
signatureAlgorithms = algorithms;
}
});
for (auto const &iter : signatureAlgorithms) {
if (iter == "ECDSA+SHA256") {
flag = true;
}
}
EXPECT_TRUE(flag);
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, onMessageDataInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("tlsSocketOnMessageData")) {
return;
}
std::string getData = "server->client";
TLSSocket testService;
SetSocketHwTestLongParam(testService);
testService.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);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, upgradeInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaFileExistence("upgradeInterface")) {
return;
}
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
EXPECT_TRUE(sock > 0);
sockaddr_in addr4 = {0};
Socket::NetAddress address;
MockNetAddress(address);
addr4.sin_family = AF_INET;
addr4.sin_port = htons(address.GetPort());
addr4.sin_addr.s_addr = inet_addr(address.GetAddress().c_str());
int ret = connect(sock, reinterpret_cast<sockaddr *>(&addr4), sizeof(sockaddr_in));
EXPECT_TRUE(ret >= 0);
TLSSocket testService(sock);
SetSocketHwTestShortParam(testService);
const std::string data = "how do you do? this is upgradeInterface";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
(void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
sleep(2);
}
} // namespace TlsSocket
} // namespace NetStack
} // namespace OHOS

View File

@ -0,0 +1,367 @@
/*
* 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 "accesstoken_kit.h"
#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"
#include "token_setproc.h"
namespace OHOS {
namespace NetStack {
namespace TlsSocket {
namespace {
using namespace testing::ext;
using namespace Security::AccessToken;
} // namespace
void MockConnectionNetAddress(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 MockConnectionParamOptions(Socket::NetAddress &address, TLSSecureOptions &secureOption, TLSConnectOptions &options)
{
secureOption.SetKey(SecureData(TlsUtilsTest::ChangeToFile(PRIVATE_KEY_PEM_CHAIN)));
secureOption.SetCert(TlsUtilsTest::ChangeToFile(CLIENT_CRT_CHAIN));
MockConnectionNetAddress(address);
options.SetTlsSecureOptions(secureOption);
options.SetNetAddress(address);
}
void SetUnilateralHwTestShortParam(TLSSocket &server)
{
TLSConnectOptions options;
TLSSecureOptions secureOption;
Socket::NetAddress address;
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(ROOT_CA_PATH_CHAIN),
TlsUtilsTest::ChangeToFile(MID_CA_CHAIN) };
secureOption.SetCaChain(caVec);
MockConnectionParamOptions(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); });
}
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},
};
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(TlsSocketTest, bindInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaPathChainExistence("bindInterface")) {
return;
}
TLSSocket tlsService;
Socket::NetAddress address;
MockConnectionNetAddress(address);
AccessToken token;
tlsService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, connectInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaPathChainExistence("connectInterface")) {
return;
}
TLSSocket tlsService;
SetUnilateralHwTestShortParam(tlsService);
AccessToken token;
const std::string data = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
tlsService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
(void)tlsService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaPathChainExistence("closeInterface")) {
return;
}
TLSSocket tlsService;
SetUnilateralHwTestShortParam(tlsService);
AccessToken token;
const std::string data = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n";
;
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
tlsService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
(void)tlsService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaPathChainExistence("sendInterface")) {
return;
}
TLSSocket tlsService;
SetUnilateralHwTestShortParam(tlsService);
AccessToken token;
const std::string data = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
tlsService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
(void)tlsService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteAddressInterface")) {
return;
}
TLSSocket tlsService;
TLSConnectOptions options;
TLSSecureOptions secureOption;
Socket::NetAddress address;
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(ROOT_CA_PATH_CHAIN),
TlsUtilsTest::ChangeToFile(MID_CA_CHAIN) };
secureOption.SetCaChain(caVec);
MockConnectionParamOptions(address, secureOption, options);
tlsService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
tlsService.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
AccessToken token;
Socket::NetAddress netAddress;
tlsService.GetRemoteAddress([&netAddress](int32_t errCode, const Socket::NetAddress &address) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
netAddress.SetFamilyBySaFamily(address.GetSaFamily());
netAddress.SetAddress(address.GetAddress());
netAddress.SetPort(address.GetPort());
});
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 = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
tlsService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
(void)tlsService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteAddressInterface")) {
return;
}
TLSSocket tlsService;
SetUnilateralHwTestShortParam(tlsService);
AccessToken token;
Socket::SocketStateBase TlsSocketstate;
tlsService.GetState([&TlsSocketstate](int32_t errCode, const Socket::SocketStateBase &state) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
TlsSocketstate = state;
});
std::cout << "TlsSocketUnilateralConnection TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl;
EXPECT_TRUE(TlsSocketstate.IsBound());
EXPECT_TRUE(!TlsSocketstate.IsClose());
EXPECT_TRUE(TlsSocketstate.IsConnected());
const std::string connectionData = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(connectionData);
tlsService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
(void)tlsService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteCertificateInterface")) {
return;
}
TLSSocket tlsService;
SetUnilateralHwTestShortParam(tlsService);
Socket::TCPSendOptions tcpSendOptions;
const std::string data = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n";
AccessToken token;
tcpSendOptions.SetData(data);
tlsService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
tlsService.GetRemoteCertificate(
[](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
(void)tlsService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaPathChainExistence("protocolInterface")) {
return;
}
TLSSocket tlsService;
TLSConnectOptions options;
TLSSecureOptions secureOption;
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(ROOT_CA_PATH_CHAIN),
TlsUtilsTest::ChangeToFile(MID_CA_CHAIN) };
secureOption.SetCaChain(caVec);
std::string protocolV13 = "TLSv1.2";
std::vector<std::string> protocolVec = { protocolV13 };
secureOption.SetProtocolChain(protocolVec);
Socket::NetAddress address;
MockConnectionParamOptions(address, secureOption, options);
AccessToken token;
tlsService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
tlsService.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
const std::string data = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
tlsService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
std::string getProtocolVal;
tlsService.GetProtocol([&getProtocolVal](int32_t errCode, const std::string &protocol) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
getProtocolVal = protocol;
});
EXPECT_STREQ(getProtocolVal.c_str(), "TLSv1.2");
(void)tlsService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
HWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2)
{
if (!TlsUtilsTest::CheckCaPathChainExistence("getCipherSuiteInterface")) {
return;
}
TLSConnectOptions options;
TLSSocket tlsService;
TLSSecureOptions secureOption;
std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(ROOT_CA_PATH_CHAIN),
TlsUtilsTest::ChangeToFile(MID_CA_CHAIN) };
secureOption.SetCaChain(caVec);
secureOption.SetCipherSuite("ECDHE-RSA-AES128-GCM-SHA256");
Socket::NetAddress address;
MockConnectionParamOptions(address, secureOption, options);
bool flag = false;
AccessToken token;
tlsService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
tlsService.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
const std::string data = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n";
Socket::TCPSendOptions tcpSendOptions;
tcpSendOptions.SetData(data);
tlsService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
std::vector<std::string> cipherSuite;
tlsService.GetCipherSuite([&cipherSuite](int32_t errCode, const std::vector<std::string> &suite) {
EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
cipherSuite = suite;
});
for (auto const &iter : cipherSuite) {
if (iter == "ECDHE-RSA-AES128-GCM-SHA256") {
flag = true;
}
}
EXPECT_TRUE(flag);
(void)tlsService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
}
} // namespace TlsSocket
} // namespace NetStack
} // namespace OHOS

View File

@ -0,0 +1,175 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TLS_TEST_H
#define TLS_TEST_H
#include <string>
namespace OHOS {
namespace NetStack {
namespace TlsSocket {
constexpr const char CLIENT_FILE[] =
"-----BEGIN CERTIFICATE-----\r\n"
"MIIDezCCAmMCFD6h5R4QvySV9q9mC6s31qQFLX14MA0GCSqGSIb3DQEBCwUAMHgx\r\n"
"CzAJBgNVBAYTAkNOMQswCQYDVQQIDAJHRDELMAkGA1UEBwwCU1oxDDAKBgNVBAoM\r\n"
"A0NPTTEMMAoGA1UECwwDTlNQMQswCQYDVQQDDAJDQTEmMCQGCSqGSIb3DQEJARYX\r\n"
"emhhbmd6aGV3ZWkwMTAzQDE2My5jb20wHhcNMjIwNDI0MDIwMjU3WhcNMjMwNDI0\r\n"
"MDIwMjU3WjB8MQswCQYDVQQGEwJDTjELMAkGA1UECAwCR0QxCzAJBgNVBAcMAlNa\r\n"
"MQwwCgYDVQQKDANDT00xDDAKBgNVBAsMA05TUDEPMA0GA1UEAwwGQ0xJRU5UMSYw\r\n"
"JAYJKoZIhvcNAQkBFhd6aGFuZ3poZXdlaTAxMDNAMTYzLmNvbTCCASIwDQYJKoZI\r\n"
"hvcNAQEBBQADggEPADCCAQoCggEBAKlc63+j5C7tLoaecpdhzzZtLy8iNSi6oLHc\r\n"
"+bPib1XWz1zcQ4On5ncGuuLSV2Tyse4tSsDbPycd8b9Teq6gdGrvirtGXau82zAq\r\n"
"no+t0mxVtV1r0OkSe+hnIrYKxTE5UDeAM319MSxWlCR0bg0uEAuVBPQpld5A9PQT\r\n"
"YCLbv4cTwB0sIKupsnNbrn2AsAlCFd288XeuTN+N87m05cDkprAkqkCJfAtRnejV\r\n"
"k+vbS+H6toR3P9PVQJXC77oM7cDOjR8AwpkRRA890XUWoQLwhHXvDpGPwKK+lLnG\r\n"
"FswiaHy3silUIOidwk7E/81BOqXSk77oUG6UQrVilkmu6g79VssCAwEAATANBgkq\r\n"
"hkiG9w0BAQsFAAOCAQEAOeqp+hFVRs4YB3UjU/3bvAUFQLS97gapCp2lk6jS88jt\r\n"
"uNeyvwulOAtZEbcoIIvzzNxvBDOVibTJ6gZU9P9g0WyRu2RTgy+UggNwH8u8KZzM\r\n"
"DT8sxuoYvRcEWbOhlNQgACa7AlQSLQifo8nvEMS2i9o8WHoHu42MRDYOHYVIwWXH\r\n"
"h6mZzfo+zrPyv3NFlwlWqaNiTGgnGCXzlVK3p5YYqLbNVYpy0U5FBxQ7fITsqcbK\r\n"
"PusAAEZzPxm8Epo647M28gNkdEEM/7bqhSTJO+jfkojgyQt2ghlw+NGCmG4dJGZb\r\n"
"yA7Z3PBj8aqEwmRUF8SAR1bxWBGk2IYRwgStuwvusg==\r\n"
"-----END CERTIFICATE-----\r\n";
constexpr const char CERTIFICAT[] =
"-----BEGIN CERTIFICATE-----\r\n"
"MIIDNzCCAh8CFDtvcMez0hxPAfnZQnWFGukh69e4MA0GCSqGSIb3DQEBCwUAMFgx\r\n"
"CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl\r\n"
"cm5ldCBXaWRnaXRzIFB0eSBMdGQxETAPBgNVBAMMCHRlc3R0X2NhMB4XDTIzMDEw\r\n"
"NDExMTc0NloXDTIzMDIwMzExMTc0NlowWDELMAkGA1UEBhMCQVUxEzARBgNVBAgM\r\n"
"ClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDER\r\n"
"MA8GA1UEAwwIdGVzdHRfY2EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB\r\n"
"AQCl4TyBVbZZKBQDGtrc/lx/L+i7eYoTajWikrwbF4OO/w0TJJOaU1McwpuDlkZk\r\n"
"H+S5DvL78Sua3p5GbkS1YYV31ugLypXGpKa8v+H4ZqC4GkTvTjtyiVGWXVHZQCK7\r\n"
"LeEvCEEQ8zn5YaC99+/KRYyuQkiuRVKEuogfZjLUZ5xF6B4JwevFCGZtKkLX02FH\r\n"
"U4d8DycWujP+dGuUAcwX4IGPShOZyKu1G9+wVr6W4uXwrZ2cdo+UMmeBr0/hLWbl\r\n"
"qldFxuyME3xBB74mR6s+fMry/vUNyEfxTfYTczM/ONpl7UjybieV7WJLWiPLaQnA\r\n"
"BV3jkhYmXpw3UmsHVytXmv3JAgMBAAEwDQYJKoZIhvcNAQELBQADggEBABkwt8Xy\r\n"
"5g48xCYZaF8sQ0N/dmkTAV+ysudylZpLQrY5vFwlpYONM9VmcGUcQK9zSwxWNB1y\r\n"
"c/AuXbr8DAZ742bzRzma8uJCAWWJJwfY5Elglf1gnSCLLaE0FvxaX0jTch/sxCYV\r\n"
"Wn0lkWTgCEJGjokCaegdEVZ50XahAwnZcv2k+nsS4awN8w4F1n5YaMHXOylIDl8L\r\n"
"CTjeQZEcw7CEMbgRt1P6Sl3nsOUoaTNDO8NIO72yC+nX8Jexw7n9EQL2lOyobliL\r\n"
"axz/WYkhLK8HxDyHl/mgMPnna8jpVcgx591f2QGtsth8iToi/C+Zpf+NC6BQhOTd\r\n"
"ed2YEa3zgjRGFIE=\r\n"
"-----END CERTIFICATE-----\r\n";
constexpr const char CA_CRT_FILE[] =
"Certificate:\r\n"
" Data:\r\n"
" Version: 3 (0x2)\r\n"
" Serial Number: 1 (0x1)\r\n"
" Signature Algorithm: sha256WithRSAEncryption\r\n"
" Issuer: C=CN, ST=beijing, O=ahaha Inc, OU=Root CA, CN=ahaha CA\r\n"
" Validity\r\n"
" Not Before: Aug 23 07:33:55 2022 GMT\r\n"
" Not After : Aug 23 07:33:55 2023 GMT\r\n"
" Subject: C=CN, ST=beijing, O=ahaha CA Inc, OU=Root CA, CN=ahaha CA\r\n"
" Subject Public Key Info:\r\n"
" Public Key Algorithm: rsaEncryption\r\n"
" RSA Public-Key: (2048 bit)\r\n"
" Modulus:\r\n"
" 00:9d:df:68:f7:7b:78:0b:21:f3:6f:24:60:ef:ce:\r\n"
" 02:90:24:df:c4:d3:f3:e4:26:6c:c7:12:bf:28:cd:\r\n"
" 38:2d:3f:ab:76:11:64:ce:6b:f6:07:fd:35:1e:b9:\r\n"
" ec:22:72:03:4d:eb:d2:94:49:2d:82:44:6c:72:59:\r\n"
" 14:ab:e7:0c:72:32:3e:ad:fa:9d:52:da:24:8d:e9:\r\n"
" a4:10:d7:dd:34:66:df:7e:e0:0e:66:53:8b:ee:91:\r\n"
" 07:9a:ce:2a:85:25:09:77:3d:5f:75:1c:a1:b3:ab:\r\n"
" 86:3b:21:28:f8:43:aa:f0:0b:7d:4d:f9:df:85:33:\r\n"
" 4a:3b:ff:e4:03:59:25:62:a1:e9:da:92:63:02:93:\r\n"
" bd:f9:df:6e:c6:57:a7:d2:e6:7b:37:14:a9:ba:69:\r\n"
" 71:0c:c5:4f:66:fe:67:66:5c:8d:d7:04:4d:d7:f3:\r\n"
" 0b:c0:0b:7d:49:eb:68:94:28:f6:31:0f:0d:2a:03:\r\n"
" 70:a7:97:f9:38:90:36:d4:4b:39:4b:53:a5:2c:32:\r\n"
" 72:f2:41:86:32:13:3c:40:2d:3f:e8:63:d3:8c:8a:\r\n"
" 83:79:d3:20:f6:bc:cd:97:3e:94:91:4e:3c:74:8d:\r\n"
" 9a:fa:29:de:c4:a5:f7:e1:e2:06:55:e6:6c:41:0f:\r\n"
" 60:3b:90:de:3a:84:ef:3a:77:79:27:00:23:55:66:\r\n"
" ca:81\r\n"
" Exponent: 65537 (0x10001)\r\n"
" X509v3 extensions:\r\n"
" X509v3 Basic Constraints:\r\n"
" CA:TRUE\r\n"
" Signature Algorithm: sha256WithRSAEncryption\r\n"
" 61:3e:39:71:7f:b1:50:dd:71:97:cd:dc:a9:4b:72:96:0a:12:\r\n"
" c1:18:fd:35:b5:e0:97:1b:76:58:22:8d:cd:75:51:0f:ba:04:\r\n"
" 00:94:6a:46:d5:3a:c5:ac:ea:7d:9c:ec:6f:19:b6:f1:2b:06:\r\n"
" e9:bb:cb:49:24:34:0b:55:bd:02:19:24:19:85:bb:e4:a4:80:\r\n"
" f4:d6:90:82:7e:81:5c:9b:89:d4:15:ed:3a:b7:a2:37:59:40:\r\n"
" db:b4:18:25:90:2e:ae:82:f9:a8:0c:9d:bd:c7:8c:54:85:ed:\r\n"
" 07:d1:70:1d:ee:a1:92:bd:12:97:83:4d:9e:9e:b7:01:b5:56:\r\n"
" a5:1f:31:6e:a1:48:68:a4:4f:1c:fa:b0:38:27:47:12:eb:55:\r\n"
" a3:45:f7:e3:18:ba:d7:85:3c:1f:2c:1e:5e:38:75:5e:80:8a:\r\n"
" fd:1c:84:4f:9b:ef:85:b7:79:89:d7:43:eb:d4:fb:c5:51:5b:\r\n"
" 84:6f:0e:06:32:54:13:e4:a7:e2:20:2d:b8:fa:2d:09:f8:8f:\r\n"
" dd:01:19:39:cc:23:c0:d1:39:19:9a:f7:7c:53:63:bf:ea:be:\r\n"
" 04:9b:af:3e:6e:1e:77:c8:b9:0b:78:e9:0e:62:a7:51:db:1e:\r\n"
" c0:63:4d:4d:14:ff:ca:44:7f:15:e4:fa:98:1e:3d:58:c2:b6:\r\n"
" 5a:64:68:d0\r\n"
"-----BEGIN CERTIFICATE-----\r\n"
"MIIDazCCAlOgAwIBAgIBATANBgkqhkiG9w0BAQsFADBwMQswCQYDVQQGEwJDTjEQ\r\n"
"MA4GA1UECAwHYmVpamluZzEdMBsGA1UECgwUR2xvYmFsIEdvb2dsZSBDQSBJbmMx\r\n"
"EDAOBgNVBAsMB1Jvb3QgQ0ExHjAcBgNVBAMMFUdsb2JhbCBHb29nbGUgUm9vdCBD\r\n"
"QTAeFw0yMjA4MjMwNzMzNTVaFw0yMzA4MjMwNzMzNTVaMHAxCzAJBgNVBAYTAkNO\r\n"
"MRAwDgYDVQQIDAdiZWlqaW5nMR0wGwYDVQQKDBRHbG9iYWwgR29vZ2xlIENBIElu\r\n"
"YzEQMA4GA1UECwwHUm9vdCBDQTEeMBwGA1UEAwwVR2xvYmFsIEdvb2dsZSBSb290\r\n"
"IENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnd9o93t4CyHzbyRg\r\n"
"784CkCTfxNPz5CZsxxK/KM04LT+rdhFkzmv2B/01HrnsInIDTevSlEktgkRsclkU\r\n"
"q+cMcjI+rfqdUtokjemkENfdNGbffuAOZlOL7pEHms4qhSUJdz1fdRyhs6uGOyEo\r\n"
"+EOq8At9TfnfhTNKO//kA1klYqHp2pJjApO9+d9uxlen0uZ7NxSpumlxDMVPZv5n\r\n"
"ZlyN1wRN2PMLwAt9SetolCj2MQ8NKgNwp5f5OJA21Es5S1OlLDJy8kGGMhM8QC0/\r\n"
"6GPTjIqDedMg9rzNlz6UkU48dI2a+inexKX34eIGVeZsQQ9gO5DeOoTvOnd5JwAj\r\n"
"VWbKgQIDAQABoxAwDjAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBh\r\n"
"Pjlxf7FQ3XGXzdypS3KWChLBGP01teCXG3ZYIo3NdVEPugQAlGpG1TrFrOp9nOxv\r\n"
"GbbxKwbpu8tJJDQLVb0CGSQZhbvkpID01pCCfoFcm4nUFe06t6I3WUDbtBglkC6u\r\n"
"gvmoDJ29x4xUhe0H0XAd7qGSvRKXg02enrcBtValHzFuoUhopE8c+rA4J0cS61Wj\r\n"
"RffjGLrXhTwfLB5eOHVegIr9HIRPm++Ft3mJ10Pr1PvFUVuEbw4GMlQT5KfiIC24\r\n"
"+i0J+I/dARk5zCPA0TkZmvd8U2O/6r4Em68+bh53yLkLeOkOYqdR2x7AY01NFP/K\r\n"
"RH8V5PqYHj1YwrZaZGjQ\r\n"
"-----END CERTIFICATE-----\r\n";
constexpr const char PRI_KEY_FILE[] =
"-----BEGIN RSA PRIVATE KEY-----"
"MIIEowIBAAKCAQEAqVzrf6PkLu0uhp5yl2HPNm0vLyI1KLqgsdz5s+JvVdbPXNxD"
"g6fmdwa64tJXZPKx7i1KwNs/Jx3xv1N6rqB0au+Ku0Zdq7zbMCqej63SbFW1XWvQ"
"6RJ76GcitgrFMTlQN4AzfX0xLFaUJHRuDS4QC5UE9CmV3kD09BNgItu/hxPAHSwg"
"q6myc1uufYCwCUIV3bzxd65M343zubTlwOSmsCSqQIl8C1Gd6NWT69tL4fq2hHc/"
"09VAlcLvugztwM6NHwDCmRFEDz3RdRahAvCEde8OkY/Aor6UucYWzCJofLeyKVQg"
"6J3CTsT/zUE6pdKTvuhQbpRCtWKWSa7qDv1WywIDAQABAoIBAFGpbCPvcmbuFjDy"
"1W4Iy1EC9G1VoSwyUKlyUzRZSjWpjfLIggVJP+bEZ/hWU61pGEIvtIupK5pA5f/K"
"0KzC0V9+gPYrx563QTjIVAwTVBLIgNq60dCQCQ7WK/Z62voRGIyqVCl94+ftFyE8"
"wpO4UiRDhk/0fT7dMz882G32ZzNJmY9eHu+yOaRctJW2gRBROHpQfDGBCz7w8s2j"
"ulIcnvwGOrvVllsL+vgY95M0LOq0W8ObbUSlawTnNTSRxFL68Hz5EaVJ19EYvEcC"
"eWnpEqIfF8OhQ+mYbdrAutXCkqJLz3rdu5P2Lbk5Ht5ETfr7rtUzvb4+ExIcxVOs"
"eys8EgECgYEA29tTxJOy2Cb4DKB9KwTErD1sFt9Ed+Z/A3RGmnM+/h75DHccqS8n"
"g9DpvHVMcMWYFVYGlEHC1F+bupM9CgxqQcVhGk/ysJ5kXF6lSTnOQxORnku3HXnV"
"4QzgKtLfHbukW1Y2RZM3aCz+Hg+bJrpacWyWZ4tRWNYsO58JRaubZjsCgYEAxTSP"
"yUBleQejl5qO76PGUUs2W8+GPr492NJGb63mEiM1zTYLVN0uuDJ2JixzHb6o1NXZ"
"6i00pSksT3+s0eiBTRnF6BJ0y/8J07ZnfQQXRAP8ypiZtd3jdOnUxEHfBw2QaIdP"
"tVdUc2mpIhosAYT9sWpHYvlUqTCdeLwhkYfgeLECgYBoajjVcmQM3i0OKiZoCOKy"
"/pTYI/8rho+p/04MylEPdXxIXEWDYD6/DrgDZh4ArQc2kt2bCcRTAnk+WfEyVYUd"
"aXVdfry+/uqhJ94N8eMw3hlZeZIk8JkQQgIwtGd8goJjUoWB85Hr6vphIn5IHVcY"
"6T5hPLxMmaL2SeioawDpwwKBgQCFXjDH6Hc3zQTEKND2HIqou/b9THH7yOlG056z"
"NKZeKdXe/OfY8uT/yZDB7FnGCgVgO2huyTfLYvcGpNAZ/eZEYGPJuYGn3MmmlruS"
"fsvFQfUahu2dY3zKusEcIXhV6sR5DNnJSFBi5VhvKcgNFwYDkF7K/thUu/4jgwgo"
"xf33YQKBgDQffkP1jWqT/pzlVLFtF85/3eCC/uedBfxXknVMrWE+CM/Vsx9cvBZw"
"hi15LA5+hEdbgvj87hmMiCOc75e0oz2Rd12ZoRlBVfbncH9ngfqBNQElM7Bueqoc"
"JOpKV+gw0gQtiu4beIdFnYsdZoZwrTjC4rW7OI0WYoLJabMFFh3I"
"-----END RSA PRIVATE KEY-----";
} // namespace TlsSocket
} // namespace NetStack
} // namespace OHOS
#endif // TLS_TEST_H

View File

@ -0,0 +1,56 @@
/*
* 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 "tls_utils_test.h"
namespace OHOS {
namespace NetStack {
namespace TlsSocket {
std::string TlsUtilsTest::ChangeToFile(const std::string_view fileName)
{
std::ifstream file;
file.open(fileName);
std::stringstream ss;
ss << file.rdbuf();
std::string infos = ss.str();
file.close();
return infos;
}
std::string TlsUtilsTest::GetIp(std::string ip)
{
return ip.substr(0, ip.length() - 1);
}
bool TlsUtilsTest::CheckCaFileExistence(const char *function)
{
if (access(CA_DER.data(), 0)) {
std::cout << "CA file does not exist! (" << function << ")";
return false;
}
return true;
}
bool TlsUtilsTest::CheckCaPathChainExistence(const char *function)
{
if (access(CA_PATH_CHAIN.data(), 0)) {
std::cout << "CA file does not exist! (" << function << ")";
return false;
}
return true;
}
} // namespace TlsSocket
} // namespace NetStack
} // namespace OHOS

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TLS_UTILS_TEST_H
#define TLS_UTILS_TEST_H
#include <fstream>
#include <gtest/gtest.h>
#include <iostream>
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include <sstream>
#include <string>
#include <string_view>
#include <unistd.h>
#include <vector>
namespace OHOS {
namespace NetStack {
namespace TlsSocket {
const std::string_view PRIVATE_KEY_PEM = "/data/ClientCert/client_rsa_private.pem.unsecure";
const std::string_view CA_DER = "/data/ClientCert/ca.crt";
const std::string_view CLIENT_CRT = "/data/ClientCert/client.crt";
const std::string_view IP_ADDRESS = "/data/Ip/address.txt";
const std::string_view PORT = "/data/Ip/port.txt";
const std::string_view PRIVATE_KEY_PEM_CHAIN = "/data/ClientCertChain/privekey.pem.unsecure";
const std::string_view CA_PATH_CHAIN = "/data/ClientCertChain/cacert.crt";
const std::string_view MID_CA_PATH_CHAIN = "/data/ClientCertChain/caMidcert.crt";
const std::string_view CLIENT_CRT_CHAIN = "/data/ClientCertChain/secondServer.crt";
const std::string_view ROOT_CA_PATH_CHAIN = "/data/ClientCertChain/RootCa.pem";
const std::string_view MID_CA_CHAIN = "/data/ClientCertChain/MidCa.pem";
class TlsUtilsTest {
public:
TlsUtilsTest();
~TlsUtilsTest();
static std::string ChangeToFile(const std::string_view fileName);
static std::string GetIp(std::string ip);
static bool CheckCaFileExistence(const char *function);
static bool CheckCaPathChainExistence(const char *function);
};
class TlsSocketTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
virtual void SetUp() {}
virtual void TearDown() {}
};
} // namespace TlsSocket
} // namespace NetStack
} // namespace OHOS
#endif // TLS_UTILS_TEST_H