11e934351Sopenharmony_ci/* 21e934351Sopenharmony_ci * Copyright (c) 2022-2024 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 "net_address.h" 171e934351Sopenharmony_ci#include "secure_data.h" 181e934351Sopenharmony_ci#include "socket_error.h" 191e934351Sopenharmony_ci#include "socket_state_base.h" 201e934351Sopenharmony_ci#include "tls.h" 211e934351Sopenharmony_ci#include "tls_certificate.h" 221e934351Sopenharmony_ci#include "tls_configuration.h" 231e934351Sopenharmony_ci#include "tls_key.h" 241e934351Sopenharmony_ci#include "tls_socket.h" 251e934351Sopenharmony_ci#include "tls_utils_test.h" 261e934351Sopenharmony_ci 271e934351Sopenharmony_cinamespace OHOS { 281e934351Sopenharmony_cinamespace NetStack { 291e934351Sopenharmony_cinamespace TlsSocket { 301e934351Sopenharmony_civoid MockNetAddress(Socket::NetAddress &address) 311e934351Sopenharmony_ci{ 321e934351Sopenharmony_ci address.SetAddress(TlsUtilsTest::GetIp(TlsUtilsTest::ChangeToFile(IP_ADDRESS))); 331e934351Sopenharmony_ci address.SetPort(std::atoi(TlsUtilsTest::ChangeToFile(PORT).c_str())); 341e934351Sopenharmony_ci address.SetFamilyBySaFamily(AF_INET); 351e934351Sopenharmony_ci} 361e934351Sopenharmony_ci 371e934351Sopenharmony_civoid MockTlsSocketParamOptions(Socket::NetAddress &address, TLSSecureOptions &secureOption, TLSConnectOptions &options) 381e934351Sopenharmony_ci{ 391e934351Sopenharmony_ci secureOption.SetKey(SecureData(TlsUtilsTest::ChangeToFile(PRIVATE_KEY_PEM))); 401e934351Sopenharmony_ci secureOption.SetCert(TlsUtilsTest::ChangeToFile(CLIENT_CRT)); 411e934351Sopenharmony_ci 421e934351Sopenharmony_ci MockNetAddress(address); 431e934351Sopenharmony_ci options.SetTlsSecureOptions(secureOption); 441e934351Sopenharmony_ci options.SetNetAddress(address); 451e934351Sopenharmony_ci} 461e934351Sopenharmony_ci 471e934351Sopenharmony_civoid SetSocketHwTestShortParam(TLSSocket &server) 481e934351Sopenharmony_ci{ 491e934351Sopenharmony_ci TLSConnectOptions options; 501e934351Sopenharmony_ci Socket::NetAddress address; 511e934351Sopenharmony_ci TLSSecureOptions secureOption; 521e934351Sopenharmony_ci std::vector<std::string> caVec1 = {TlsUtilsTest::ChangeToFile(CA_DER)}; 531e934351Sopenharmony_ci secureOption.SetCaChain(caVec1); 541e934351Sopenharmony_ci MockTlsSocketParamOptions(address, secureOption, options); 551e934351Sopenharmony_ci 561e934351Sopenharmony_ci server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 571e934351Sopenharmony_ci server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 581e934351Sopenharmony_ci} 591e934351Sopenharmony_ci 601e934351Sopenharmony_civoid SetSocketHwTestLongParam(TLSSocket &server) 611e934351Sopenharmony_ci{ 621e934351Sopenharmony_ci TLSConnectOptions options; 631e934351Sopenharmony_ci TLSSecureOptions secureOption; 641e934351Sopenharmony_ci secureOption.SetCipherSuite("AES256-SHA256"); 651e934351Sopenharmony_ci std::string protocolV13 = "TLSv1.3"; 661e934351Sopenharmony_ci std::vector<std::string> protocolVec = {protocolV13}; 671e934351Sopenharmony_ci secureOption.SetProtocolChain(protocolVec); 681e934351Sopenharmony_ci std::vector<std::string> caVect = {TlsUtilsTest::ChangeToFile(CA_DER)}; 691e934351Sopenharmony_ci secureOption.SetCaChain(caVect); 701e934351Sopenharmony_ci Socket::NetAddress address; 711e934351Sopenharmony_ci MockTlsSocketParamOptions(address, secureOption, options); 721e934351Sopenharmony_ci 731e934351Sopenharmony_ci server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 741e934351Sopenharmony_ci server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 751e934351Sopenharmony_ci} 761e934351Sopenharmony_ci 771e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, bindInterface, testing::ext::TestSize.Level2) 781e934351Sopenharmony_ci{ 791e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("bindInterface")) { 801e934351Sopenharmony_ci return; 811e934351Sopenharmony_ci } 821e934351Sopenharmony_ci 831e934351Sopenharmony_ci Socket::NetAddress address; 841e934351Sopenharmony_ci TLSSocket bindTestServer; 851e934351Sopenharmony_ci MockNetAddress(address); 861e934351Sopenharmony_ci bindTestServer.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 871e934351Sopenharmony_ci} 881e934351Sopenharmony_ci 891e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, connectInterface, testing::ext::TestSize.Level2) 901e934351Sopenharmony_ci{ 911e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("connectInterface")) { 921e934351Sopenharmony_ci return; 931e934351Sopenharmony_ci } 941e934351Sopenharmony_ci TLSSocket testService; 951e934351Sopenharmony_ci SetSocketHwTestShortParam(testService); 961e934351Sopenharmony_ci 971e934351Sopenharmony_ci const std::string data = "how do you do? this is connectInterface"; 981e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 991e934351Sopenharmony_ci tcpSendOptions.SetData(data); 1001e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1011e934351Sopenharmony_ci sleep(2); 1021e934351Sopenharmony_ci 1031e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1041e934351Sopenharmony_ci sleep(2); 1051e934351Sopenharmony_ci} 1061e934351Sopenharmony_ci 1071e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, startReadMessageInterface, testing::ext::TestSize.Level2) 1081e934351Sopenharmony_ci{ 1091e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("startReadMessageInterface")) { 1101e934351Sopenharmony_ci return; 1111e934351Sopenharmony_ci } 1121e934351Sopenharmony_ci TLSSocket testService; 1131e934351Sopenharmony_ci SetSocketHwTestShortParam(testService); 1141e934351Sopenharmony_ci 1151e934351Sopenharmony_ci const std::string data = "how do you do? this is startReadMessageInterface"; 1161e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 1171e934351Sopenharmony_ci tcpSendOptions.SetData(data); 1181e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1191e934351Sopenharmony_ci sleep(2); 1201e934351Sopenharmony_ci 1211e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1221e934351Sopenharmony_ci} 1231e934351Sopenharmony_ci 1241e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, readMessageInterface, testing::ext::TestSize.Level2) 1251e934351Sopenharmony_ci{ 1261e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("readMessageInterface")) { 1271e934351Sopenharmony_ci return; 1281e934351Sopenharmony_ci } 1291e934351Sopenharmony_ci TLSSocket testService; 1301e934351Sopenharmony_ci SetSocketHwTestShortParam(testService); 1311e934351Sopenharmony_ci 1321e934351Sopenharmony_ci const std::string data = "how do you do? this is readMessageInterface"; 1331e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 1341e934351Sopenharmony_ci tcpSendOptions.SetData(data); 1351e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1361e934351Sopenharmony_ci sleep(2); 1371e934351Sopenharmony_ci 1381e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1391e934351Sopenharmony_ci} 1401e934351Sopenharmony_ci 1411e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2) 1421e934351Sopenharmony_ci{ 1431e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("closeInterface")) { 1441e934351Sopenharmony_ci return; 1451e934351Sopenharmony_ci } 1461e934351Sopenharmony_ci 1471e934351Sopenharmony_ci TLSSocket testService; 1481e934351Sopenharmony_ci SetSocketHwTestShortParam(testService); 1491e934351Sopenharmony_ci 1501e934351Sopenharmony_ci const std::string data = "how do you do? this is closeInterface"; 1511e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 1521e934351Sopenharmony_ci tcpSendOptions.SetData(data); 1531e934351Sopenharmony_ci 1541e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1551e934351Sopenharmony_ci sleep(2); 1561e934351Sopenharmony_ci 1571e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1581e934351Sopenharmony_ci} 1591e934351Sopenharmony_ci 1601e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2) 1611e934351Sopenharmony_ci{ 1621e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("sendInterface")) { 1631e934351Sopenharmony_ci return; 1641e934351Sopenharmony_ci } 1651e934351Sopenharmony_ci TLSSocket testService; 1661e934351Sopenharmony_ci SetSocketHwTestShortParam(testService); 1671e934351Sopenharmony_ci 1681e934351Sopenharmony_ci const std::string data = "how do you do? this is sendInterface"; 1691e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 1701e934351Sopenharmony_ci tcpSendOptions.SetData(data); 1711e934351Sopenharmony_ci 1721e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1731e934351Sopenharmony_ci sleep(2); 1741e934351Sopenharmony_ci 1751e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1761e934351Sopenharmony_ci} 1771e934351Sopenharmony_ci 1781e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2) 1791e934351Sopenharmony_ci{ 1801e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("getRemoteAddressInterface")) { 1811e934351Sopenharmony_ci return; 1821e934351Sopenharmony_ci } 1831e934351Sopenharmony_ci 1841e934351Sopenharmony_ci TLSConnectOptions options; 1851e934351Sopenharmony_ci TLSSocket testService; 1861e934351Sopenharmony_ci TLSSecureOptions secureOption; 1871e934351Sopenharmony_ci Socket::NetAddress address; 1881e934351Sopenharmony_ci std::vector<std::string> caVec = {TlsUtilsTest::ChangeToFile(CA_DER)}; 1891e934351Sopenharmony_ci secureOption.SetCaChain(caVec); 1901e934351Sopenharmony_ci MockTlsSocketParamOptions(address, secureOption, options); 1911e934351Sopenharmony_ci 1921e934351Sopenharmony_ci testService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1931e934351Sopenharmony_ci testService.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 1941e934351Sopenharmony_ci 1951e934351Sopenharmony_ci Socket::NetAddress netAddress; 1961e934351Sopenharmony_ci testService.GetRemoteAddress([&netAddress](int32_t errCode, const Socket::NetAddress &address) { 1971e934351Sopenharmony_ci EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); 1981e934351Sopenharmony_ci netAddress.SetAddress(address.GetAddress()); 1991e934351Sopenharmony_ci netAddress.SetFamilyBySaFamily(address.GetSaFamily()); 2001e934351Sopenharmony_ci netAddress.SetPort(address.GetPort()); 2011e934351Sopenharmony_ci }); 2021e934351Sopenharmony_ci EXPECT_STREQ(netAddress.GetAddress().c_str(), TlsUtilsTest::GetIp(TlsUtilsTest::ChangeToFile(IP_ADDRESS)).c_str()); 2031e934351Sopenharmony_ci EXPECT_EQ(address.GetPort(), std::atoi(TlsUtilsTest::ChangeToFile(PORT).c_str())); 2041e934351Sopenharmony_ci EXPECT_EQ(netAddress.GetSaFamily(), AF_INET); 2051e934351Sopenharmony_ci 2061e934351Sopenharmony_ci const std::string data = "how do you do? this is getRemoteAddressInterface"; 2071e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 2081e934351Sopenharmony_ci tcpSendOptions.SetData(data); 2091e934351Sopenharmony_ci 2101e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2111e934351Sopenharmony_ci 2121e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2131e934351Sopenharmony_ci} 2141e934351Sopenharmony_ci 2151e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2) 2161e934351Sopenharmony_ci{ 2171e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("getRemoteAddressInterface")) { 2181e934351Sopenharmony_ci return; 2191e934351Sopenharmony_ci } 2201e934351Sopenharmony_ci 2211e934351Sopenharmony_ci TLSSocket testService; 2221e934351Sopenharmony_ci SetSocketHwTestShortParam(testService); 2231e934351Sopenharmony_ci 2241e934351Sopenharmony_ci Socket::SocketStateBase TlsSocketstate; 2251e934351Sopenharmony_ci testService.GetState([&TlsSocketstate](int32_t errCode, const Socket::SocketStateBase &state) { 2261e934351Sopenharmony_ci EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); 2271e934351Sopenharmony_ci TlsSocketstate = state; 2281e934351Sopenharmony_ci }); 2291e934351Sopenharmony_ci std::cout << "TlsSocketTest TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl; 2301e934351Sopenharmony_ci EXPECT_TRUE(TlsSocketstate.IsBound()); 2311e934351Sopenharmony_ci EXPECT_TRUE(!TlsSocketstate.IsClose()); 2321e934351Sopenharmony_ci EXPECT_TRUE(TlsSocketstate.IsConnected()); 2331e934351Sopenharmony_ci 2341e934351Sopenharmony_ci const std::string tlsSocketTestData = "how do you do? this is getStateInterface"; 2351e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 2361e934351Sopenharmony_ci tcpSendOptions.SetData(tlsSocketTestData); 2371e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2381e934351Sopenharmony_ci 2391e934351Sopenharmony_ci sleep(2); 2401e934351Sopenharmony_ci 2411e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2421e934351Sopenharmony_ci} 2431e934351Sopenharmony_ci 2441e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getCertificateInterface, testing::ext::TestSize.Level2) 2451e934351Sopenharmony_ci{ 2461e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("getCertificateInterface")) { 2471e934351Sopenharmony_ci return; 2481e934351Sopenharmony_ci } 2491e934351Sopenharmony_ci TLSSocket testService; 2501e934351Sopenharmony_ci SetSocketHwTestShortParam(testService); 2511e934351Sopenharmony_ci 2521e934351Sopenharmony_ci const std::string data = "how do you do? This is UT test getCertificateInterface"; 2531e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 2541e934351Sopenharmony_ci tcpSendOptions.SetData(data); 2551e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2561e934351Sopenharmony_ci 2571e934351Sopenharmony_ci testService.GetCertificate( 2581e934351Sopenharmony_ci [](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2591e934351Sopenharmony_ci 2601e934351Sopenharmony_ci sleep(2); 2611e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2621e934351Sopenharmony_ci} 2631e934351Sopenharmony_ci 2641e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2) 2651e934351Sopenharmony_ci{ 2661e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("getRemoteCertificateInterface")) { 2671e934351Sopenharmony_ci return; 2681e934351Sopenharmony_ci } 2691e934351Sopenharmony_ci TLSSocket testService; 2701e934351Sopenharmony_ci SetSocketHwTestShortParam(testService); 2711e934351Sopenharmony_ci 2721e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 2731e934351Sopenharmony_ci const std::string data = "how do you do? This is UT test getRemoteCertificateInterface"; 2741e934351Sopenharmony_ci tcpSendOptions.SetData(data); 2751e934351Sopenharmony_ci 2761e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2771e934351Sopenharmony_ci 2781e934351Sopenharmony_ci testService.GetRemoteCertificate( 2791e934351Sopenharmony_ci [](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2801e934351Sopenharmony_ci 2811e934351Sopenharmony_ci sleep(2); 2821e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2831e934351Sopenharmony_ci} 2841e934351Sopenharmony_ci 2851e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2) 2861e934351Sopenharmony_ci{ 2871e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("protocolInterface")) { 2881e934351Sopenharmony_ci return; 2891e934351Sopenharmony_ci } 2901e934351Sopenharmony_ci TLSSocket testService; 2911e934351Sopenharmony_ci SetSocketHwTestLongParam(testService); 2921e934351Sopenharmony_ci 2931e934351Sopenharmony_ci const std::string data = "how do you do? this is protocolInterface"; 2941e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 2951e934351Sopenharmony_ci tcpSendOptions.SetData(data); 2961e934351Sopenharmony_ci 2971e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 2981e934351Sopenharmony_ci std::string protocolVal; 2991e934351Sopenharmony_ci testService.GetProtocol([&protocolVal](int32_t errCode, const std::string &protocol) { 3001e934351Sopenharmony_ci EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); 3011e934351Sopenharmony_ci protocolVal = protocol; 3021e934351Sopenharmony_ci }); 3031e934351Sopenharmony_ci EXPECT_STREQ(protocolVal.c_str(), "TLSv1.3"); 3041e934351Sopenharmony_ci 3051e934351Sopenharmony_ci Socket::SocketStateBase socketStateBase; 3061e934351Sopenharmony_ci testService.GetState([&socketStateBase](int32_t errCode, Socket::SocketStateBase state) { 3071e934351Sopenharmony_ci if (errCode == TLSSOCKET_SUCCESS) { 3081e934351Sopenharmony_ci EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); 3091e934351Sopenharmony_ci socketStateBase.SetIsBound(state.IsBound()); 3101e934351Sopenharmony_ci socketStateBase.SetIsClose(state.IsClose()); 3111e934351Sopenharmony_ci socketStateBase.SetIsConnected(state.IsConnected()); 3121e934351Sopenharmony_ci } 3131e934351Sopenharmony_ci }); 3141e934351Sopenharmony_ci EXPECT_TRUE(socketStateBase.IsConnected()); 3151e934351Sopenharmony_ci sleep(2); 3161e934351Sopenharmony_ci 3171e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 3181e934351Sopenharmony_ci} 3191e934351Sopenharmony_ci 3201e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2) 3211e934351Sopenharmony_ci{ 3221e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("getCipherSuiteInterface")) { 3231e934351Sopenharmony_ci return; 3241e934351Sopenharmony_ci } 3251e934351Sopenharmony_ci TLSSocket testService; 3261e934351Sopenharmony_ci SetSocketHwTestLongParam(testService); 3271e934351Sopenharmony_ci 3281e934351Sopenharmony_ci bool flag = false; 3291e934351Sopenharmony_ci const std::string data = "how do you do? This is getCipherSuiteInterface"; 3301e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 3311e934351Sopenharmony_ci tcpSendOptions.SetData(data); 3321e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 3331e934351Sopenharmony_ci 3341e934351Sopenharmony_ci std::vector<std::string> cipherSuite; 3351e934351Sopenharmony_ci testService.GetCipherSuite([&cipherSuite](int32_t errCode, const std::vector<std::string> &suite) { 3361e934351Sopenharmony_ci if (errCode == TLSSOCKET_SUCCESS) { 3371e934351Sopenharmony_ci cipherSuite = suite; 3381e934351Sopenharmony_ci } 3391e934351Sopenharmony_ci }); 3401e934351Sopenharmony_ci 3411e934351Sopenharmony_ci for (auto const &iter : cipherSuite) { 3421e934351Sopenharmony_ci if (iter == "AES256-SHA256") { 3431e934351Sopenharmony_ci flag = true; 3441e934351Sopenharmony_ci } 3451e934351Sopenharmony_ci } 3461e934351Sopenharmony_ci 3471e934351Sopenharmony_ci EXPECT_TRUE(flag); 3481e934351Sopenharmony_ci sleep(2); 3491e934351Sopenharmony_ci 3501e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 3511e934351Sopenharmony_ci} 3521e934351Sopenharmony_ci 3531e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getSignatureAlgorithmsInterface, testing::ext::TestSize.Level2) 3541e934351Sopenharmony_ci{ 3551e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("getSignatureAlgorithmsInterface")) { 3561e934351Sopenharmony_ci return; 3571e934351Sopenharmony_ci } 3581e934351Sopenharmony_ci TLSSocket testService; 3591e934351Sopenharmony_ci TLSConnectOptions options; 3601e934351Sopenharmony_ci TLSSecureOptions secureOption; 3611e934351Sopenharmony_ci Socket::NetAddress address; 3621e934351Sopenharmony_ci std::string signatureAlgorithmVec = {"rsa_pss_rsae_sha256:ECDSA+SHA256"}; 3631e934351Sopenharmony_ci secureOption.SetSignatureAlgorithms(signatureAlgorithmVec); 3641e934351Sopenharmony_ci std::string protocolV13 = "TLSv1.3"; 3651e934351Sopenharmony_ci std::vector<std::string> protocolVec = {protocolV13}; 3661e934351Sopenharmony_ci secureOption.SetProtocolChain(protocolVec); 3671e934351Sopenharmony_ci std::vector<std::string> caVec = {TlsUtilsTest::ChangeToFile(CA_DER)}; 3681e934351Sopenharmony_ci secureOption.SetCaChain(caVec); 3691e934351Sopenharmony_ci MockTlsSocketParamOptions(address, secureOption, options); 3701e934351Sopenharmony_ci 3711e934351Sopenharmony_ci testService.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 3721e934351Sopenharmony_ci testService.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 3731e934351Sopenharmony_ci 3741e934351Sopenharmony_ci bool flag = false; 3751e934351Sopenharmony_ci const std::string data = "how do you do? this is getSignatureAlgorithmsInterface"; 3761e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 3771e934351Sopenharmony_ci tcpSendOptions.SetData(data); 3781e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 3791e934351Sopenharmony_ci 3801e934351Sopenharmony_ci std::vector<std::string> signatureAlgorithms; 3811e934351Sopenharmony_ci testService.GetSignatureAlgorithms( 3821e934351Sopenharmony_ci [&signatureAlgorithms](int32_t errCode, const std::vector<std::string> &algorithms) { 3831e934351Sopenharmony_ci if (errCode == TLSSOCKET_SUCCESS) { 3841e934351Sopenharmony_ci signatureAlgorithms = algorithms; 3851e934351Sopenharmony_ci } 3861e934351Sopenharmony_ci }); 3871e934351Sopenharmony_ci 3881e934351Sopenharmony_ci for (auto const &iter : signatureAlgorithms) { 3891e934351Sopenharmony_ci if (iter == "ECDSA+SHA256") { 3901e934351Sopenharmony_ci flag = true; 3911e934351Sopenharmony_ci } 3921e934351Sopenharmony_ci } 3931e934351Sopenharmony_ci EXPECT_TRUE(flag); 3941e934351Sopenharmony_ci sleep(2); 3951e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 3961e934351Sopenharmony_ci} 3971e934351Sopenharmony_ci 3981e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, onMessageDataInterface, testing::ext::TestSize.Level2) 3991e934351Sopenharmony_ci{ 4001e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("tlsSocketOnMessageData")) { 4011e934351Sopenharmony_ci return; 4021e934351Sopenharmony_ci } 4031e934351Sopenharmony_ci std::string getData = "server->client"; 4041e934351Sopenharmony_ci TLSSocket testService; 4051e934351Sopenharmony_ci SetSocketHwTestLongParam(testService); 4061e934351Sopenharmony_ci 4071e934351Sopenharmony_ci testService.OnMessage([&getData](const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) { 4081e934351Sopenharmony_ci if (data == getData) { 4091e934351Sopenharmony_ci EXPECT_TRUE(true); 4101e934351Sopenharmony_ci } else { 4111e934351Sopenharmony_ci EXPECT_TRUE(false); 4121e934351Sopenharmony_ci } 4131e934351Sopenharmony_ci }); 4141e934351Sopenharmony_ci 4151e934351Sopenharmony_ci const std::string data = "how do you do? this is tlsSocketOnMessageData"; 4161e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 4171e934351Sopenharmony_ci tcpSendOptions.SetData(data); 4181e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 4191e934351Sopenharmony_ci 4201e934351Sopenharmony_ci sleep(2); 4211e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 4221e934351Sopenharmony_ci} 4231e934351Sopenharmony_ci 4241e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, upgradeInterface, testing::ext::TestSize.Level2) 4251e934351Sopenharmony_ci{ 4261e934351Sopenharmony_ci if (!TlsUtilsTest::CheckCaFileExistence("upgradeInterface")) { 4271e934351Sopenharmony_ci return; 4281e934351Sopenharmony_ci } 4291e934351Sopenharmony_ci 4301e934351Sopenharmony_ci int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 4311e934351Sopenharmony_ci EXPECT_TRUE(sock > 0); 4321e934351Sopenharmony_ci 4331e934351Sopenharmony_ci sockaddr_in addr4 = {0}; 4341e934351Sopenharmony_ci Socket::NetAddress address; 4351e934351Sopenharmony_ci MockNetAddress(address); 4361e934351Sopenharmony_ci addr4.sin_family = AF_INET; 4371e934351Sopenharmony_ci addr4.sin_port = htons(address.GetPort()); 4381e934351Sopenharmony_ci addr4.sin_addr.s_addr = inet_addr(address.GetAddress().c_str()); 4391e934351Sopenharmony_ci 4401e934351Sopenharmony_ci int ret = connect(sock, reinterpret_cast<sockaddr *>(&addr4), sizeof(sockaddr_in)); 4411e934351Sopenharmony_ci EXPECT_TRUE(ret >= 0); 4421e934351Sopenharmony_ci 4431e934351Sopenharmony_ci TLSSocket testService(sock); 4441e934351Sopenharmony_ci SetSocketHwTestShortParam(testService); 4451e934351Sopenharmony_ci 4461e934351Sopenharmony_ci const std::string data = "how do you do? this is upgradeInterface"; 4471e934351Sopenharmony_ci Socket::TCPSendOptions tcpSendOptions; 4481e934351Sopenharmony_ci tcpSendOptions.SetData(data); 4491e934351Sopenharmony_ci testService.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 4501e934351Sopenharmony_ci sleep(2); 4511e934351Sopenharmony_ci 4521e934351Sopenharmony_ci (void)testService.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); }); 4531e934351Sopenharmony_ci sleep(2); 4541e934351Sopenharmony_ci} 4551e934351Sopenharmony_ci} // namespace TlsSocket 4561e934351Sopenharmony_ci} // namespace NetStack 4571e934351Sopenharmony_ci} // namespace OHOS 458