11e934351Sopenharmony_ci/* 21e934351Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 31e934351Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 41e934351Sopenharmony_ci * you may not use this file except in compliance with the License. 51e934351Sopenharmony_ci * You may obtain a copy of the License at 61e934351Sopenharmony_ci * 71e934351Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 81e934351Sopenharmony_ci * 91e934351Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 101e934351Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 111e934351Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121e934351Sopenharmony_ci * See the License for the specific language governing permissions and 131e934351Sopenharmony_ci * limitations under the License. 141e934351Sopenharmony_ci */ 151e934351Sopenharmony_ci 161e934351Sopenharmony_ci#include <gtest/gtest.h> 171e934351Sopenharmony_ci#include <iostream> 181e934351Sopenharmony_ci#include <string> 191e934351Sopenharmony_ci 201e934351Sopenharmony_ci#include <openssl/ssl.h> 211e934351Sopenharmony_ci 221e934351Sopenharmony_ci#define private public 231e934351Sopenharmony_ci#include "tls_context.h" 241e934351Sopenharmony_ci#include "tls.h" 251e934351Sopenharmony_ci#include "TlsTest.h" 261e934351Sopenharmony_ci 271e934351Sopenharmony_cinamespace OHOS { 281e934351Sopenharmony_cinamespace NetStack { 291e934351Sopenharmony_cinamespace TlsSocket { 301e934351Sopenharmony_cinamespace { 311e934351Sopenharmony_ciusing namespace testing::ext; 321e934351Sopenharmony_ciconstexpr const char *PROTOCOL13 = "TLSv1.3"; 331e934351Sopenharmony_ciconstexpr const char *PROTOCOL12 = "TLSv1.2"; 341e934351Sopenharmony_ciconstexpr const char *PROTOCOL11 = "TLSv1.1"; 351e934351Sopenharmony_ciconstexpr const char *CIPHER_SUITE = "AES256-SHA256"; 361e934351Sopenharmony_ciconstexpr const char *SIGNATURE_ALGORITHMS = "rsa_pss_rsae_sha256:ECDSA+SHA256"; 371e934351Sopenharmony_ci} // namespace 381e934351Sopenharmony_ci 391e934351Sopenharmony_ciclass TlsContextTest : public testing::Test { 401e934351Sopenharmony_cipublic: 411e934351Sopenharmony_ci static void SetUpTestCase() {} 421e934351Sopenharmony_ci 431e934351Sopenharmony_ci static void TearDownTestCase() {} 441e934351Sopenharmony_ci 451e934351Sopenharmony_ci virtual void SetUp() {} 461e934351Sopenharmony_ci 471e934351Sopenharmony_ci virtual void TearDown() {} 481e934351Sopenharmony_ci}; 491e934351Sopenharmony_ci 501e934351Sopenharmony_ciHWTEST_F(TlsContextTest, ContextTest1, TestSize.Level2) 511e934351Sopenharmony_ci{ 521e934351Sopenharmony_ci TLSConfiguration configuration; 531e934351Sopenharmony_ci configuration.SetCipherSuite(CIPHER_SUITE); 541e934351Sopenharmony_ci configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS); 551e934351Sopenharmony_ci std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration); 561e934351Sopenharmony_ci 571e934351Sopenharmony_ci EXPECT_NE(tlsContext, nullptr); 581e934351Sopenharmony_ci tlsContext->CloseCtx(); 591e934351Sopenharmony_ci} 601e934351Sopenharmony_ci 611e934351Sopenharmony_ciHWTEST_F(TlsContextTest, ContextTest2, TestSize.Level2) 621e934351Sopenharmony_ci{ 631e934351Sopenharmony_ci std::vector<std::string> protocol; 641e934351Sopenharmony_ci protocol.push_back(PROTOCOL13); 651e934351Sopenharmony_ci protocol.push_back(PROTOCOL12); 661e934351Sopenharmony_ci protocol.push_back(PROTOCOL11); 671e934351Sopenharmony_ci TLSConfiguration configuration; 681e934351Sopenharmony_ci std::vector<std::string> caVec = {CA_CRT_FILE}; 691e934351Sopenharmony_ci configuration.SetCaCertificate(caVec); 701e934351Sopenharmony_ci configuration.SetProtocol(protocol); 711e934351Sopenharmony_ci configuration.SetCipherSuite(CIPHER_SUITE); 721e934351Sopenharmony_ci configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS); 731e934351Sopenharmony_ci configuration.SetLocalCertificate(CLIENT_FILE); 741e934351Sopenharmony_ci std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration); 751e934351Sopenharmony_ci EXPECT_NE(tlsContext, nullptr); 761e934351Sopenharmony_ci TLSContext::SetMinAndMaxProtocol(tlsContext.get()); 771e934351Sopenharmony_ci bool isInitTlsContext = TLSContext::InitTlsContext(tlsContext.get(), configuration); 781e934351Sopenharmony_ci EXPECT_TRUE(isInitTlsContext); 791e934351Sopenharmony_ci bool isSetCipherList = TLSContext::SetCipherList(tlsContext.get(), configuration); 801e934351Sopenharmony_ci EXPECT_TRUE(isSetCipherList); 811e934351Sopenharmony_ci bool isSetSignatureAlgorithms = TLSContext::SetSignatureAlgorithms(tlsContext.get(), configuration); 821e934351Sopenharmony_ci EXPECT_TRUE(isSetSignatureAlgorithms); 831e934351Sopenharmony_ci TLSContext::GetCiphers(tlsContext.get()); 841e934351Sopenharmony_ci TLSContext::UseRemoteCipher(tlsContext.get()); 851e934351Sopenharmony_ci bool setCaAndVerify = TLSContext::SetCaAndVerify(tlsContext.get(), configuration); 861e934351Sopenharmony_ci EXPECT_TRUE(setCaAndVerify); 871e934351Sopenharmony_ci bool setLocalCert = TLSContext::SetLocalCertificate(tlsContext.get(), configuration); 881e934351Sopenharmony_ci EXPECT_TRUE(setLocalCert); 891e934351Sopenharmony_ci bool setKeyAndCheck = TLSContext::SetKeyAndCheck(tlsContext.get(), configuration); 901e934351Sopenharmony_ci EXPECT_FALSE(setKeyAndCheck); 911e934351Sopenharmony_ci TLSContext::SetVerify(tlsContext.get()); 921e934351Sopenharmony_ci SSL *ssl = tlsContext->CreateSsl(); 931e934351Sopenharmony_ci EXPECT_NE(ssl, nullptr); 941e934351Sopenharmony_ci SSL_free(ssl); 951e934351Sopenharmony_ci ssl = nullptr; 961e934351Sopenharmony_ci tlsContext->CloseCtx(); 971e934351Sopenharmony_ci} 981e934351Sopenharmony_ci 991e934351Sopenharmony_ciHWTEST_F(TlsContextTest, ContextTest3, TestSize.Level2) 1001e934351Sopenharmony_ci{ 1011e934351Sopenharmony_ci TLSConfiguration configuration; 1021e934351Sopenharmony_ci std::vector<std::string> caVec = {}; 1031e934351Sopenharmony_ci configuration.SetCaCertificate(caVec); 1041e934351Sopenharmony_ci std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration); 1051e934351Sopenharmony_ci EXPECT_NE(tlsContext, nullptr); 1061e934351Sopenharmony_ci bool setCaAndVerify = TLSContext::SetCaAndVerify(tlsContext.get(), configuration); 1071e934351Sopenharmony_ci tlsContext->CloseCtx(); 1081e934351Sopenharmony_ci EXPECT_TRUE(setCaAndVerify); 1091e934351Sopenharmony_ci} 1101e934351Sopenharmony_ci 1111e934351Sopenharmony_ciHWTEST_F(TlsContextTest, InitTlsContext3, TestSize.Level2) 1121e934351Sopenharmony_ci{ 1131e934351Sopenharmony_ci TLSConfiguration configuration; 1141e934351Sopenharmony_ci std::string cipherSuite = ""; 1151e934351Sopenharmony_ci configuration.SetCipherSuite(cipherSuite); 1161e934351Sopenharmony_ci std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration); 1171e934351Sopenharmony_ci 1181e934351Sopenharmony_ci EXPECT_NE(tlsContext, nullptr); 1191e934351Sopenharmony_ci tlsContext->CloseCtx(); 1201e934351Sopenharmony_ci} 1211e934351Sopenharmony_ci 1221e934351Sopenharmony_ciHWTEST_F(TlsContextTest, InitTlsContext4, TestSize.Level2) 1231e934351Sopenharmony_ci{ 1241e934351Sopenharmony_ci TLSConfiguration configuration; 1251e934351Sopenharmony_ci std::string signatureAlgorithms = ""; 1261e934351Sopenharmony_ci configuration.SetCipherSuite(CIPHER_SUITE); 1271e934351Sopenharmony_ci configuration.SetSignatureAlgorithms(signatureAlgorithms); 1281e934351Sopenharmony_ci std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration); 1291e934351Sopenharmony_ci 1301e934351Sopenharmony_ci EXPECT_NE(tlsContext, nullptr); 1311e934351Sopenharmony_ci tlsContext->CloseCtx(); 1321e934351Sopenharmony_ci} 1331e934351Sopenharmony_ci 1341e934351Sopenharmony_ciHWTEST_F(TlsContextTest, ContextNullTest, TestSize.Level2) 1351e934351Sopenharmony_ci{ 1361e934351Sopenharmony_ci std::vector<std::string> protocol; 1371e934351Sopenharmony_ci protocol.push_back(PROTOCOL13); 1381e934351Sopenharmony_ci protocol.push_back(PROTOCOL12); 1391e934351Sopenharmony_ci protocol.push_back(PROTOCOL11); 1401e934351Sopenharmony_ci TLSConfiguration configuration; 1411e934351Sopenharmony_ci std::vector<std::string> caVec = {CA_CRT_FILE}; 1421e934351Sopenharmony_ci configuration.SetCaCertificate(caVec); 1431e934351Sopenharmony_ci configuration.SetProtocol(protocol); 1441e934351Sopenharmony_ci configuration.SetCipherSuite(CIPHER_SUITE); 1451e934351Sopenharmony_ci configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS); 1461e934351Sopenharmony_ci configuration.SetLocalCertificate(CLIENT_FILE); 1471e934351Sopenharmony_ci std::unique_ptr<TLSContext> tlsContext = nullptr; 1481e934351Sopenharmony_ci EXPECT_EQ(tlsContext, nullptr); 1491e934351Sopenharmony_ci TLSContext::SetMinAndMaxProtocol(tlsContext.get()); 1501e934351Sopenharmony_ci bool isInitTlsContext = TLSContext::InitTlsContext(tlsContext.get(), configuration); 1511e934351Sopenharmony_ci EXPECT_FALSE(isInitTlsContext); 1521e934351Sopenharmony_ci bool isSetCipherList = TLSContext::SetCipherList(tlsContext.get(), configuration); 1531e934351Sopenharmony_ci EXPECT_FALSE(isSetCipherList); 1541e934351Sopenharmony_ci bool isSetSignatureAlgorithms = TLSContext::SetSignatureAlgorithms(tlsContext.get(), configuration); 1551e934351Sopenharmony_ci EXPECT_FALSE(isSetSignatureAlgorithms); 1561e934351Sopenharmony_ci TLSContext::GetCiphers(tlsContext.get()); 1571e934351Sopenharmony_ci TLSContext::UseRemoteCipher(tlsContext.get()); 1581e934351Sopenharmony_ci bool setCaAndVerify = TLSContext::SetCaAndVerify(tlsContext.get(), configuration); 1591e934351Sopenharmony_ci EXPECT_FALSE(setCaAndVerify); 1601e934351Sopenharmony_ci bool setLocalCert = TLSContext::SetLocalCertificate(tlsContext.get(), configuration); 1611e934351Sopenharmony_ci EXPECT_FALSE(setLocalCert); 1621e934351Sopenharmony_ci bool setKeyAndCheck = TLSContext::SetKeyAndCheck(tlsContext.get(), configuration); 1631e934351Sopenharmony_ci EXPECT_FALSE(setKeyAndCheck); 1641e934351Sopenharmony_ci TLSContext::SetVerify(tlsContext.get()); 1651e934351Sopenharmony_ci} 1661e934351Sopenharmony_ci 1671e934351Sopenharmony_ciHWTEST_F(TlsContextTest, ContextFailTest1, TestSize.Level2) 1681e934351Sopenharmony_ci{ 1691e934351Sopenharmony_ci std::vector<std::string> protocol; 1701e934351Sopenharmony_ci protocol.push_back("1.3"); 1711e934351Sopenharmony_ci protocol.push_back("1.2"); 1721e934351Sopenharmony_ci TLSConfiguration configuration; 1731e934351Sopenharmony_ci std::vector<std::string> caVec = {CA_CRT_FILE}; 1741e934351Sopenharmony_ci configuration.SetCaCertificate(caVec); 1751e934351Sopenharmony_ci configuration.SetProtocol(protocol); 1761e934351Sopenharmony_ci configuration.SetCipherSuite(CIPHER_SUITE); 1771e934351Sopenharmony_ci configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS); 1781e934351Sopenharmony_ci configuration.SetLocalCertificate("certificate"); 1791e934351Sopenharmony_ci SecureData key("key"); 1801e934351Sopenharmony_ci SecureData keyPass("123456"); 1811e934351Sopenharmony_ci configuration.SetPrivateKey(key, keyPass); 1821e934351Sopenharmony_ci std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration); 1831e934351Sopenharmony_ci EXPECT_NE(tlsContext, nullptr); 1841e934351Sopenharmony_ci} 1851e934351Sopenharmony_ci} // namespace TlsSocket 1861e934351Sopenharmony_ci} // namespace NetStack 1871e934351Sopenharmony_ci} // namespace OHOS