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 "tls_configuration.h"
211e934351Sopenharmony_ci#include "tls.h"
221e934351Sopenharmony_ci#include "TlsTest.h"
231e934351Sopenharmony_ci
241e934351Sopenharmony_cinamespace OHOS {
251e934351Sopenharmony_cinamespace NetStack {
261e934351Sopenharmony_cinamespace TlsSocket {
271e934351Sopenharmony_cinamespace {
281e934351Sopenharmony_ciusing namespace testing::ext;
291e934351Sopenharmony_ci} // namespace
301e934351Sopenharmony_ci
311e934351Sopenharmony_ciclass TlsConfigurationTest : public testing::Test {
321e934351Sopenharmony_cipublic:
331e934351Sopenharmony_ci    static void SetUpTestCase() {}
341e934351Sopenharmony_ci
351e934351Sopenharmony_ci    static void TearDownTestCase() {}
361e934351Sopenharmony_ci
371e934351Sopenharmony_ci    virtual void SetUp() {}
381e934351Sopenharmony_ci
391e934351Sopenharmony_ci    virtual void TearDown() {}
401e934351Sopenharmony_ci};
411e934351Sopenharmony_ci
421e934351Sopenharmony_ciHWTEST_F(TlsConfigurationTest, AssignmentConstruction, TestSize.Level2)
431e934351Sopenharmony_ci{
441e934351Sopenharmony_ci    TLSConfiguration tlsConfiguration;
451e934351Sopenharmony_ci    TLSConfiguration configuration = tlsConfiguration;
461e934351Sopenharmony_ci    configuration.SetLocalCertificate(CLIENT_FILE);
471e934351Sopenharmony_ci    TLSCertificate tlsCertificate = configuration.GetLocalCertificate();
481e934351Sopenharmony_ci    EXPECT_NE(tlsCertificate.handle(), nullptr);
491e934351Sopenharmony_ci    X509CertRawData x509CertRawData = configuration.GetCertificate();
501e934351Sopenharmony_ci    EXPECT_NE(x509CertRawData.data.Length(), 0);
511e934351Sopenharmony_ci}
521e934351Sopenharmony_ci
531e934351Sopenharmony_ciHWTEST_F(TlsConfigurationTest, CopyConstruction, TestSize.Level2)
541e934351Sopenharmony_ci{
551e934351Sopenharmony_ci    TLSConfiguration tlsConfiguration;
561e934351Sopenharmony_ci    tlsConfiguration.SetLocalCertificate(CLIENT_FILE);
571e934351Sopenharmony_ci    TLSConfiguration configuration = TLSConfiguration(tlsConfiguration);
581e934351Sopenharmony_ci    TLSCertificate tlsCertificate = configuration.GetLocalCertificate();
591e934351Sopenharmony_ci    EXPECT_NE(tlsCertificate.handle(), nullptr);
601e934351Sopenharmony_ci}
611e934351Sopenharmony_ci
621e934351Sopenharmony_ciHWTEST_F(TlsConfigurationTest, SetAndGetCa, TestSize.Level2)
631e934351Sopenharmony_ci{
641e934351Sopenharmony_ci    TLSConfiguration tlsConfiguration;
651e934351Sopenharmony_ci    tlsConfiguration.SetLocalCertificate(CLIENT_FILE);
661e934351Sopenharmony_ci    std::vector<std::string> certificate;
671e934351Sopenharmony_ci    certificate.push_back(CA_CRT_FILE);
681e934351Sopenharmony_ci    tlsConfiguration.SetCaCertificate(certificate);
691e934351Sopenharmony_ci    std::vector<std::string> getCaCertificate;
701e934351Sopenharmony_ci    getCaCertificate = tlsConfiguration.GetCaCertificate();
711e934351Sopenharmony_ci    EXPECT_NE(getCaCertificate.size(), 0);
721e934351Sopenharmony_ci}
731e934351Sopenharmony_ci
741e934351Sopenharmony_ciHWTEST_F(TlsConfigurationTest, SetPrivateKey, TestSize.Level2)
751e934351Sopenharmony_ci{
761e934351Sopenharmony_ci    TLSConfiguration tlsConfiguration;
771e934351Sopenharmony_ci    tlsConfiguration.SetLocalCertificate(CLIENT_FILE);
781e934351Sopenharmony_ci    SecureData structureData(PRI_KEY_FILE);
791e934351Sopenharmony_ci    std::string keyPassStr = "";
801e934351Sopenharmony_ci    SecureData keyPass(keyPassStr);
811e934351Sopenharmony_ci    tlsConfiguration.SetPrivateKey(structureData, keyPass);
821e934351Sopenharmony_ci    TLSKey tlsKey = tlsConfiguration.GetPrivateKey();
831e934351Sopenharmony_ci    SecureData tlsKeyData = tlsKey.GetKeyData();
841e934351Sopenharmony_ci    EXPECT_EQ(tlsKeyData.Length(), strlen(PRI_KEY_FILE));
851e934351Sopenharmony_ci}
861e934351Sopenharmony_ci
871e934351Sopenharmony_ciHWTEST_F(TlsConfigurationTest, SetProtocol, TestSize.Level2)
881e934351Sopenharmony_ci{
891e934351Sopenharmony_ci    TLSConfiguration tlsConfiguration;
901e934351Sopenharmony_ci    std::vector<std::string> protocol;
911e934351Sopenharmony_ci    std::string protocolVer = "TLSv1.3";
921e934351Sopenharmony_ci    protocol.push_back(protocolVer);
931e934351Sopenharmony_ci    tlsConfiguration.SetProtocol(protocol);
941e934351Sopenharmony_ci    TLSProtocol tlsProtocol = tlsConfiguration.GetProtocol();
951e934351Sopenharmony_ci    EXPECT_EQ(tlsProtocol, TLS_V1_3);
961e934351Sopenharmony_ci    TLSProtocol minProtocol = tlsConfiguration.GetMinProtocol();
971e934351Sopenharmony_ci    EXPECT_EQ(minProtocol, TLS_V1_3);
981e934351Sopenharmony_ci    TLSProtocol maxProtocol = tlsConfiguration.GetMaxProtocol();
991e934351Sopenharmony_ci    EXPECT_EQ(maxProtocol, TLS_V1_3);
1001e934351Sopenharmony_ci
1011e934351Sopenharmony_ci    protocol.clear();
1021e934351Sopenharmony_ci    protocolVer = "TLSv1.2";
1031e934351Sopenharmony_ci    protocol.push_back(protocolVer);
1041e934351Sopenharmony_ci    tlsConfiguration.SetProtocol(protocol);
1051e934351Sopenharmony_ci    tlsProtocol = tlsConfiguration.GetProtocol();
1061e934351Sopenharmony_ci    EXPECT_EQ(tlsProtocol, TLS_V1_2);
1071e934351Sopenharmony_ci    minProtocol = tlsConfiguration.GetMinProtocol();
1081e934351Sopenharmony_ci    EXPECT_EQ(minProtocol, TLS_V1_2);
1091e934351Sopenharmony_ci    maxProtocol = tlsConfiguration.GetMaxProtocol();
1101e934351Sopenharmony_ci    EXPECT_EQ(maxProtocol, TLS_V1_2);
1111e934351Sopenharmony_ci}
1121e934351Sopenharmony_ci
1131e934351Sopenharmony_ciHWTEST_F(TlsConfigurationTest, UseRemoteCipherPrefer, TestSize.Level2)
1141e934351Sopenharmony_ci{
1151e934351Sopenharmony_ci    TLSConfiguration tlsConfiguration;
1161e934351Sopenharmony_ci    tlsConfiguration.SetUseRemoteCipherPrefer(true);
1171e934351Sopenharmony_ci    bool isUsePemoteCipherPrefer = tlsConfiguration.GetUseRemoteCipherPrefer();
1181e934351Sopenharmony_ci    EXPECT_TRUE(isUsePemoteCipherPrefer);
1191e934351Sopenharmony_ci}
1201e934351Sopenharmony_ci
1211e934351Sopenharmony_ciHWTEST_F(TlsConfigurationTest, CipherSuite, TestSize.Level2)
1221e934351Sopenharmony_ci{
1231e934351Sopenharmony_ci    TLSConfiguration tlsConfiguration;
1241e934351Sopenharmony_ci    std::string cipherSuite = "AES256-SHA256";
1251e934351Sopenharmony_ci    tlsConfiguration.SetCipherSuite(cipherSuite);
1261e934351Sopenharmony_ci    std::string getCipherSuite;
1271e934351Sopenharmony_ci    getCipherSuite = tlsConfiguration.GetCipherSuite();
1281e934351Sopenharmony_ci    std::cout << "getCipherSuite:" << getCipherSuite << std::endl;
1291e934351Sopenharmony_ci    int idx = getCipherSuite.find(cipherSuite);
1301e934351Sopenharmony_ci    EXPECT_NE(idx, std::string::npos);
1311e934351Sopenharmony_ci}
1321e934351Sopenharmony_ci
1331e934351Sopenharmony_ciHWTEST_F(TlsConfigurationTest, SignatureAlgorithms, TestSize.Level2)
1341e934351Sopenharmony_ci{
1351e934351Sopenharmony_ci    TLSConfiguration tlsConfiguration;
1361e934351Sopenharmony_ci    std::string signatureAlgorithms = "rsa_pss_rsae_sha256:ECDSA+SHA256";
1371e934351Sopenharmony_ci    tlsConfiguration.SetSignatureAlgorithms(signatureAlgorithms);
1381e934351Sopenharmony_ci    std::string getSignatureAlgorithms;
1391e934351Sopenharmony_ci    getSignatureAlgorithms = tlsConfiguration.GetSignatureAlgorithms();
1401e934351Sopenharmony_ci    std::cout << "getSignatureAlgorithms:" << getSignatureAlgorithms << std::endl;
1411e934351Sopenharmony_ci    std::string subStr = "ECDSA+SHA256";
1421e934351Sopenharmony_ci    int idx = getSignatureAlgorithms.find(subStr);
1431e934351Sopenharmony_ci    EXPECT_NE(idx, std::string::npos);
1441e934351Sopenharmony_ci}
1451e934351Sopenharmony_ci} // namespace TlsSocket
1461e934351Sopenharmony_ci} // namespace NetStack
1471e934351Sopenharmony_ci} // namespace OHOS
148