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 MockCertChainNetAddress(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 MockCertChainParamOptions(Socket::NetAddress &address, TLSSecureOptions &secureOption, TLSConnectOptions &options)
381e934351Sopenharmony_ci{
391e934351Sopenharmony_ci    secureOption.SetKey(SecureData(TlsUtilsTest::ChangeToFile(PRIVATE_KEY_PEM_CHAIN)));
401e934351Sopenharmony_ci    secureOption.SetCert(TlsUtilsTest::ChangeToFile(CLIENT_CRT_CHAIN));
411e934351Sopenharmony_ci
421e934351Sopenharmony_ci    MockCertChainNetAddress(address);
431e934351Sopenharmony_ci    options.SetNetAddress(address);
441e934351Sopenharmony_ci    options.SetTlsSecureOptions(secureOption);
451e934351Sopenharmony_ci}
461e934351Sopenharmony_ci
471e934351Sopenharmony_civoid SetCertChainHwTestShortParam(TLSSocket &server)
481e934351Sopenharmony_ci{
491e934351Sopenharmony_ci    TLSConnectOptions options;
501e934351Sopenharmony_ci    TLSSecureOptions secureOption;
511e934351Sopenharmony_ci    std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
521e934351Sopenharmony_ci        TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
531e934351Sopenharmony_ci    secureOption.SetCaChain(caVec);
541e934351Sopenharmony_ci    Socket::NetAddress address;
551e934351Sopenharmony_ci    MockCertChainParamOptions(address, secureOption, options);
561e934351Sopenharmony_ci
571e934351Sopenharmony_ci    server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
581e934351Sopenharmony_ci    server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
591e934351Sopenharmony_ci}
601e934351Sopenharmony_ci
611e934351Sopenharmony_civoid SetCertChainHwTestLongParam(TLSSocket &server)
621e934351Sopenharmony_ci{
631e934351Sopenharmony_ci    Socket::NetAddress address;
641e934351Sopenharmony_ci    std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
651e934351Sopenharmony_ci        TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
661e934351Sopenharmony_ci    TLSSecureOptions secureOption;
671e934351Sopenharmony_ci    secureOption.SetCaChain(caVec);
681e934351Sopenharmony_ci    std::string protocolV13 = "TLSv1.3";
691e934351Sopenharmony_ci    std::vector<std::string> protocolVec = { protocolV13 };
701e934351Sopenharmony_ci    secureOption.SetProtocolChain(protocolVec);
711e934351Sopenharmony_ci    secureOption.SetCipherSuite("AES256-SHA256");
721e934351Sopenharmony_ci
731e934351Sopenharmony_ci    TLSConnectOptions options;
741e934351Sopenharmony_ci    MockCertChainParamOptions(address, secureOption, options);
751e934351Sopenharmony_ci
761e934351Sopenharmony_ci    server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
771e934351Sopenharmony_ci    server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
781e934351Sopenharmony_ci}
791e934351Sopenharmony_ci
801e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, bindInterface, testing::ext::TestSize.Level2)
811e934351Sopenharmony_ci{
821e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("bindInterface")) {
831e934351Sopenharmony_ci        return;
841e934351Sopenharmony_ci    }
851e934351Sopenharmony_ci
861e934351Sopenharmony_ci    TLSSocket testServer;
871e934351Sopenharmony_ci    Socket::NetAddress address;
881e934351Sopenharmony_ci    MockCertChainNetAddress(address);
891e934351Sopenharmony_ci    testServer.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
901e934351Sopenharmony_ci}
911e934351Sopenharmony_ci
921e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, connectInterface, testing::ext::TestSize.Level2)
931e934351Sopenharmony_ci{
941e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("connectInterface")) {
951e934351Sopenharmony_ci        return;
961e934351Sopenharmony_ci    }
971e934351Sopenharmony_ci    TLSSocket certChainServer;
981e934351Sopenharmony_ci    SetCertChainHwTestShortParam(certChainServer);
991e934351Sopenharmony_ci
1001e934351Sopenharmony_ci    const std::string data = "how do you do? this is connectInterface";
1011e934351Sopenharmony_ci    Socket::TCPSendOptions tcpSendOptions;
1021e934351Sopenharmony_ci    tcpSendOptions.SetData(data);
1031e934351Sopenharmony_ci    certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1041e934351Sopenharmony_ci    sleep(2);
1051e934351Sopenharmony_ci
1061e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1071e934351Sopenharmony_ci    sleep(2);
1081e934351Sopenharmony_ci}
1091e934351Sopenharmony_ci
1101e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2)
1111e934351Sopenharmony_ci{
1121e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("closeInterface")) {
1131e934351Sopenharmony_ci        return;
1141e934351Sopenharmony_ci    }
1151e934351Sopenharmony_ci    TLSSocket certChainServer;
1161e934351Sopenharmony_ci    SetCertChainHwTestShortParam(certChainServer);
1171e934351Sopenharmony_ci
1181e934351Sopenharmony_ci    const std::string data = "how do you do? this is closeInterface";
1191e934351Sopenharmony_ci    Socket::TCPSendOptions tcpSendOptions;
1201e934351Sopenharmony_ci    tcpSendOptions.SetData(data);
1211e934351Sopenharmony_ci
1221e934351Sopenharmony_ci    certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1231e934351Sopenharmony_ci    sleep(2);
1241e934351Sopenharmony_ci
1251e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1261e934351Sopenharmony_ci}
1271e934351Sopenharmony_ci
1281e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2)
1291e934351Sopenharmony_ci{
1301e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("sendInterface")) {
1311e934351Sopenharmony_ci        return;
1321e934351Sopenharmony_ci    }
1331e934351Sopenharmony_ci    TLSSocket certChainServer;
1341e934351Sopenharmony_ci    SetCertChainHwTestShortParam(certChainServer);
1351e934351Sopenharmony_ci
1361e934351Sopenharmony_ci    const std::string data = "how do you do? this is sendInterface";
1371e934351Sopenharmony_ci    Socket::TCPSendOptions tcpSendOptions;
1381e934351Sopenharmony_ci    tcpSendOptions.SetData(data);
1391e934351Sopenharmony_ci
1401e934351Sopenharmony_ci    certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1411e934351Sopenharmony_ci    sleep(2);
1421e934351Sopenharmony_ci
1431e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1441e934351Sopenharmony_ci}
1451e934351Sopenharmony_ci
1461e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2)
1471e934351Sopenharmony_ci{
1481e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteAddressInterface")) {
1491e934351Sopenharmony_ci        return;
1501e934351Sopenharmony_ci    }
1511e934351Sopenharmony_ci    TLSSocket certChainServer;
1521e934351Sopenharmony_ci    TLSConnectOptions options;
1531e934351Sopenharmony_ci    TLSSecureOptions secureOption;
1541e934351Sopenharmony_ci    Socket::NetAddress address;
1551e934351Sopenharmony_ci    std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
1561e934351Sopenharmony_ci        TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
1571e934351Sopenharmony_ci    secureOption.SetCaChain(caVec);
1581e934351Sopenharmony_ci    MockCertChainParamOptions(address, secureOption, options);
1591e934351Sopenharmony_ci
1601e934351Sopenharmony_ci    certChainServer.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1611e934351Sopenharmony_ci    certChainServer.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1621e934351Sopenharmony_ci
1631e934351Sopenharmony_ci    Socket::NetAddress netAddress;
1641e934351Sopenharmony_ci    certChainServer.GetRemoteAddress([&netAddress](int32_t errCode, const Socket::NetAddress &address) {
1651e934351Sopenharmony_ci        EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
1661e934351Sopenharmony_ci        netAddress.SetPort(address.GetPort());
1671e934351Sopenharmony_ci        netAddress.SetFamilyBySaFamily(address.GetSaFamily());
1681e934351Sopenharmony_ci        netAddress.SetAddress(address.GetAddress());
1691e934351Sopenharmony_ci    });
1701e934351Sopenharmony_ci    EXPECT_STREQ(netAddress.GetAddress().c_str(), TlsUtilsTest::GetIp(TlsUtilsTest::ChangeToFile(IP_ADDRESS)).c_str());
1711e934351Sopenharmony_ci    EXPECT_EQ(address.GetPort(), std::atoi(TlsUtilsTest::ChangeToFile(PORT).c_str()));
1721e934351Sopenharmony_ci    EXPECT_EQ(netAddress.GetSaFamily(), AF_INET);
1731e934351Sopenharmony_ci
1741e934351Sopenharmony_ci    const std::string data = "how do you do? this is getRemoteAddressInterface";
1751e934351Sopenharmony_ci    Socket::TCPSendOptions tcpSendOptions;
1761e934351Sopenharmony_ci    tcpSendOptions.SetData(data);
1771e934351Sopenharmony_ci
1781e934351Sopenharmony_ci    certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1791e934351Sopenharmony_ci
1801e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
1811e934351Sopenharmony_ci}
1821e934351Sopenharmony_ci
1831e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2)
1841e934351Sopenharmony_ci{
1851e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteAddressInterface")) {
1861e934351Sopenharmony_ci        return;
1871e934351Sopenharmony_ci    }
1881e934351Sopenharmony_ci    TLSSocket certChainServer;
1891e934351Sopenharmony_ci    SetCertChainHwTestShortParam(certChainServer);
1901e934351Sopenharmony_ci
1911e934351Sopenharmony_ci    Socket::SocketStateBase TlsSocketstate;
1921e934351Sopenharmony_ci    certChainServer.GetState([&TlsSocketstate](int32_t errCode, const Socket::SocketStateBase &state) {
1931e934351Sopenharmony_ci        EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
1941e934351Sopenharmony_ci        TlsSocketstate = state;
1951e934351Sopenharmony_ci    });
1961e934351Sopenharmony_ci    std::cout << "TlsSocketCertChainTest TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl;
1971e934351Sopenharmony_ci    EXPECT_TRUE(TlsSocketstate.IsBound());
1981e934351Sopenharmony_ci    EXPECT_TRUE(!TlsSocketstate.IsClose());
1991e934351Sopenharmony_ci    EXPECT_TRUE(TlsSocketstate.IsConnected());
2001e934351Sopenharmony_ci
2011e934351Sopenharmony_ci    const std::string tlsSocketCertChainTestData = "how do you do? this is getStateInterface";
2021e934351Sopenharmony_ci    Socket::TCPSendOptions tcpSendOptions;
2031e934351Sopenharmony_ci    tcpSendOptions.SetData(tlsSocketCertChainTestData);
2041e934351Sopenharmony_ci    certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2051e934351Sopenharmony_ci
2061e934351Sopenharmony_ci    sleep(2);
2071e934351Sopenharmony_ci
2081e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2091e934351Sopenharmony_ci}
2101e934351Sopenharmony_ci
2111e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getCertificateInterface, testing::ext::TestSize.Level2)
2121e934351Sopenharmony_ci{
2131e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("getCertificateInterface")) {
2141e934351Sopenharmony_ci        return;
2151e934351Sopenharmony_ci    }
2161e934351Sopenharmony_ci    TLSSocket certChainServer;
2171e934351Sopenharmony_ci    SetCertChainHwTestShortParam(certChainServer);
2181e934351Sopenharmony_ci    Socket::TCPSendOptions tcpSendOptions;
2191e934351Sopenharmony_ci    const std::string data = "how do you do? This is UT test getCertificateInterface";
2201e934351Sopenharmony_ci
2211e934351Sopenharmony_ci    tcpSendOptions.SetData(data);
2221e934351Sopenharmony_ci    certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2231e934351Sopenharmony_ci
2241e934351Sopenharmony_ci    certChainServer.GetCertificate(
2251e934351Sopenharmony_ci        [](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2261e934351Sopenharmony_ci
2271e934351Sopenharmony_ci    sleep(2);
2281e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2291e934351Sopenharmony_ci}
2301e934351Sopenharmony_ci
2311e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2)
2321e934351Sopenharmony_ci{
2331e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("getRemoteCertificateInterface")) {
2341e934351Sopenharmony_ci        return;
2351e934351Sopenharmony_ci    }
2361e934351Sopenharmony_ci    TLSSocket certChainServer;
2371e934351Sopenharmony_ci    SetCertChainHwTestShortParam(certChainServer);
2381e934351Sopenharmony_ci    Socket::TCPSendOptions tcpSendOptions;
2391e934351Sopenharmony_ci    const std::string data = "how do you do? This is UT test getRemoteCertificateInterface";
2401e934351Sopenharmony_ci    tcpSendOptions.SetData(data);
2411e934351Sopenharmony_ci
2421e934351Sopenharmony_ci    certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2431e934351Sopenharmony_ci
2441e934351Sopenharmony_ci    certChainServer.GetRemoteCertificate(
2451e934351Sopenharmony_ci        [](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2461e934351Sopenharmony_ci
2471e934351Sopenharmony_ci    sleep(2);
2481e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2491e934351Sopenharmony_ci}
2501e934351Sopenharmony_ci
2511e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2)
2521e934351Sopenharmony_ci{
2531e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("protocolInterface")) {
2541e934351Sopenharmony_ci        return;
2551e934351Sopenharmony_ci    }
2561e934351Sopenharmony_ci    TLSSocket certChainServer;
2571e934351Sopenharmony_ci    SetCertChainHwTestLongParam(certChainServer);
2581e934351Sopenharmony_ci
2591e934351Sopenharmony_ci    const std::string data = "how do you do? this is protocolInterface.";
2601e934351Sopenharmony_ci    Socket::TCPSendOptions tcpSendOptions;
2611e934351Sopenharmony_ci    tcpSendOptions.SetData(data);
2621e934351Sopenharmony_ci
2631e934351Sopenharmony_ci    certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2641e934351Sopenharmony_ci    std::string getProtocolVal = "";
2651e934351Sopenharmony_ci    certChainServer.GetProtocol([&getProtocolVal](int32_t errCode, const std::string &protocol) {
2661e934351Sopenharmony_ci        EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
2671e934351Sopenharmony_ci        getProtocolVal = protocol;
2681e934351Sopenharmony_ci    });
2691e934351Sopenharmony_ci    EXPECT_STREQ(getProtocolVal.c_str(), "TLSv1.3");
2701e934351Sopenharmony_ci    sleep(2);
2711e934351Sopenharmony_ci
2721e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2731e934351Sopenharmony_ci}
2741e934351Sopenharmony_ci
2751e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2)
2761e934351Sopenharmony_ci{
2771e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("getCipherSuiteInterface")) {
2781e934351Sopenharmony_ci        return;
2791e934351Sopenharmony_ci    }
2801e934351Sopenharmony_ci    TLSSocket certChainServer;
2811e934351Sopenharmony_ci    SetCertChainHwTestLongParam(certChainServer);
2821e934351Sopenharmony_ci
2831e934351Sopenharmony_ci    bool successFlag = false;
2841e934351Sopenharmony_ci    const std::string data = "how do you do? This is getCipherSuiteInterface";
2851e934351Sopenharmony_ci    Socket::TCPSendOptions testTcpSendOptions;
2861e934351Sopenharmony_ci    testTcpSendOptions.SetData(data);
2871e934351Sopenharmony_ci    certChainServer.Send(testTcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
2881e934351Sopenharmony_ci
2891e934351Sopenharmony_ci    std::vector<std::string> testCipherSuite;
2901e934351Sopenharmony_ci    certChainServer.GetCipherSuite([&testCipherSuite](int32_t errCode, const std::vector<std::string> &suite) {
2911e934351Sopenharmony_ci        EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
2921e934351Sopenharmony_ci        testCipherSuite = suite;
2931e934351Sopenharmony_ci    });
2941e934351Sopenharmony_ci
2951e934351Sopenharmony_ci    for (auto const &iter : testCipherSuite) {
2961e934351Sopenharmony_ci        if (iter == "AES256-SHA256") {
2971e934351Sopenharmony_ci            successFlag = true;
2981e934351Sopenharmony_ci        }
2991e934351Sopenharmony_ci    }
3001e934351Sopenharmony_ci
3011e934351Sopenharmony_ci    EXPECT_TRUE(successFlag);
3021e934351Sopenharmony_ci    sleep(2);
3031e934351Sopenharmony_ci
3041e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
3051e934351Sopenharmony_ci}
3061e934351Sopenharmony_ci
3071e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, getSignatureAlgorithmsInterface, testing::ext::TestSize.Level2)
3081e934351Sopenharmony_ci{
3091e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("getSignatureAlgorithmsInterface")) {
3101e934351Sopenharmony_ci        return;
3111e934351Sopenharmony_ci    }
3121e934351Sopenharmony_ci
3131e934351Sopenharmony_ci    TLSSocket certChainServer;
3141e934351Sopenharmony_ci    TLSSecureOptions secureOption;
3151e934351Sopenharmony_ci    std::string signatureAlgorithmVec = {"rsa_pss_rsae_sha256:ECDSA+SHA256"};
3161e934351Sopenharmony_ci    secureOption.SetSignatureAlgorithms(signatureAlgorithmVec);
3171e934351Sopenharmony_ci    std::vector<std::string> caVec = { TlsUtilsTest::ChangeToFile(CA_PATH_CHAIN),
3181e934351Sopenharmony_ci        TlsUtilsTest::ChangeToFile(MID_CA_PATH_CHAIN) };
3191e934351Sopenharmony_ci    secureOption.SetCaChain(caVec);
3201e934351Sopenharmony_ci    std::string protocolV13 = "TLSv1.3";
3211e934351Sopenharmony_ci    std::vector<std::string> protocolVec = {protocolV13};
3221e934351Sopenharmony_ci    secureOption.SetProtocolChain(protocolVec);
3231e934351Sopenharmony_ci    Socket::NetAddress address;
3241e934351Sopenharmony_ci    TLSConnectOptions options;
3251e934351Sopenharmony_ci    MockCertChainParamOptions(address, secureOption, options);
3261e934351Sopenharmony_ci
3271e934351Sopenharmony_ci    bool successFlag = false;
3281e934351Sopenharmony_ci    certChainServer.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
3291e934351Sopenharmony_ci    certChainServer.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
3301e934351Sopenharmony_ci
3311e934351Sopenharmony_ci    const std::string data = "how do you do? this is getSignatureAlgorithmsInterface";
3321e934351Sopenharmony_ci    Socket::TCPSendOptions testOptions;
3331e934351Sopenharmony_ci    testOptions.SetData(data);
3341e934351Sopenharmony_ci    certChainServer.Send(testOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
3351e934351Sopenharmony_ci
3361e934351Sopenharmony_ci    std::vector<std::string> testSignatureAlgorithms;
3371e934351Sopenharmony_ci    certChainServer.GetSignatureAlgorithms(
3381e934351Sopenharmony_ci        [&testSignatureAlgorithms](int32_t errCode, const std::vector<std::string> &algorithms) {
3391e934351Sopenharmony_ci            if (errCode == TLSSOCKET_SUCCESS) {
3401e934351Sopenharmony_ci                testSignatureAlgorithms = algorithms;
3411e934351Sopenharmony_ci            }
3421e934351Sopenharmony_ci        });
3431e934351Sopenharmony_ci
3441e934351Sopenharmony_ci    for (auto const &iter : testSignatureAlgorithms) {
3451e934351Sopenharmony_ci        if (iter == "ECDSA+SHA256") {
3461e934351Sopenharmony_ci            successFlag = true;
3471e934351Sopenharmony_ci        }
3481e934351Sopenharmony_ci    }
3491e934351Sopenharmony_ci    EXPECT_TRUE(successFlag);
3501e934351Sopenharmony_ci    sleep(2);
3511e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
3521e934351Sopenharmony_ci}
3531e934351Sopenharmony_ci
3541e934351Sopenharmony_ciHWTEST_F(TlsSocketTest, onMessageDataInterface, testing::ext::TestSize.Level2)
3551e934351Sopenharmony_ci{
3561e934351Sopenharmony_ci    if (!TlsUtilsTest::CheckCaPathChainExistence("tlsSocketOnMessageData")) {
3571e934351Sopenharmony_ci        return;
3581e934351Sopenharmony_ci    }
3591e934351Sopenharmony_ci    std::string getData = "server->client";
3601e934351Sopenharmony_ci    TLSSocket certChainServer;
3611e934351Sopenharmony_ci    SetCertChainHwTestLongParam(certChainServer);
3621e934351Sopenharmony_ci    certChainServer.OnMessage([&getData](const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) {
3631e934351Sopenharmony_ci        if (data == getData) {
3641e934351Sopenharmony_ci            EXPECT_TRUE(true);
3651e934351Sopenharmony_ci        } else {
3661e934351Sopenharmony_ci            EXPECT_TRUE(false);
3671e934351Sopenharmony_ci        }
3681e934351Sopenharmony_ci    });
3691e934351Sopenharmony_ci
3701e934351Sopenharmony_ci    const std::string data = "how do you do? this is tlsSocketOnMessageData";
3711e934351Sopenharmony_ci    Socket::TCPSendOptions tcpSendOptions;
3721e934351Sopenharmony_ci    tcpSendOptions.SetData(data);
3731e934351Sopenharmony_ci    certChainServer.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
3741e934351Sopenharmony_ci
3751e934351Sopenharmony_ci    sleep(2);
3761e934351Sopenharmony_ci    (void)certChainServer.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
3771e934351Sopenharmony_ci}
3781e934351Sopenharmony_ci} // namespace TlsSocket
3791e934351Sopenharmony_ci} // namespace NetStack
3801e934351Sopenharmony_ci} // namespace OHOS
381