1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <gtest/gtest.h>
17#include <iostream>
18#include <string>
19
20#include "tls_configuration.h"
21#include "tls.h"
22#include "TlsTest.h"
23
24namespace OHOS {
25namespace NetStack {
26namespace TlsSocket {
27namespace {
28using namespace testing::ext;
29} // namespace
30
31class TlsConfigurationTest : public testing::Test {
32public:
33    static void SetUpTestCase() {}
34
35    static void TearDownTestCase() {}
36
37    virtual void SetUp() {}
38
39    virtual void TearDown() {}
40};
41
42HWTEST_F(TlsConfigurationTest, AssignmentConstruction, TestSize.Level2)
43{
44    TLSConfiguration tlsConfiguration;
45    TLSConfiguration configuration = tlsConfiguration;
46    configuration.SetLocalCertificate(CLIENT_FILE);
47    TLSCertificate tlsCertificate = configuration.GetLocalCertificate();
48    EXPECT_NE(tlsCertificate.handle(), nullptr);
49    X509CertRawData x509CertRawData = configuration.GetCertificate();
50    EXPECT_NE(x509CertRawData.data.Length(), 0);
51}
52
53HWTEST_F(TlsConfigurationTest, CopyConstruction, TestSize.Level2)
54{
55    TLSConfiguration tlsConfiguration;
56    tlsConfiguration.SetLocalCertificate(CLIENT_FILE);
57    TLSConfiguration configuration = TLSConfiguration(tlsConfiguration);
58    TLSCertificate tlsCertificate = configuration.GetLocalCertificate();
59    EXPECT_NE(tlsCertificate.handle(), nullptr);
60}
61
62HWTEST_F(TlsConfigurationTest, SetAndGetCa, TestSize.Level2)
63{
64    TLSConfiguration tlsConfiguration;
65    tlsConfiguration.SetLocalCertificate(CLIENT_FILE);
66    std::vector<std::string> certificate;
67    certificate.push_back(CA_CRT_FILE);
68    tlsConfiguration.SetCaCertificate(certificate);
69    std::vector<std::string> getCaCertificate;
70    getCaCertificate = tlsConfiguration.GetCaCertificate();
71    EXPECT_NE(getCaCertificate.size(), 0);
72}
73
74HWTEST_F(TlsConfigurationTest, SetPrivateKey, TestSize.Level2)
75{
76    TLSConfiguration tlsConfiguration;
77    tlsConfiguration.SetLocalCertificate(CLIENT_FILE);
78    SecureData structureData(PRI_KEY_FILE);
79    std::string keyPassStr = "";
80    SecureData keyPass(keyPassStr);
81    tlsConfiguration.SetPrivateKey(structureData, keyPass);
82    TLSKey tlsKey = tlsConfiguration.GetPrivateKey();
83    SecureData tlsKeyData = tlsKey.GetKeyData();
84    EXPECT_EQ(tlsKeyData.Length(), strlen(PRI_KEY_FILE));
85}
86
87HWTEST_F(TlsConfigurationTest, SetProtocol, TestSize.Level2)
88{
89    TLSConfiguration tlsConfiguration;
90    std::vector<std::string> protocol;
91    std::string protocolVer = "TLSv1.3";
92    protocol.push_back(protocolVer);
93    tlsConfiguration.SetProtocol(protocol);
94    TLSProtocol tlsProtocol = tlsConfiguration.GetProtocol();
95    EXPECT_EQ(tlsProtocol, TLS_V1_3);
96    TLSProtocol minProtocol = tlsConfiguration.GetMinProtocol();
97    EXPECT_EQ(minProtocol, TLS_V1_3);
98    TLSProtocol maxProtocol = tlsConfiguration.GetMaxProtocol();
99    EXPECT_EQ(maxProtocol, TLS_V1_3);
100
101    protocol.clear();
102    protocolVer = "TLSv1.2";
103    protocol.push_back(protocolVer);
104    tlsConfiguration.SetProtocol(protocol);
105    tlsProtocol = tlsConfiguration.GetProtocol();
106    EXPECT_EQ(tlsProtocol, TLS_V1_2);
107    minProtocol = tlsConfiguration.GetMinProtocol();
108    EXPECT_EQ(minProtocol, TLS_V1_2);
109    maxProtocol = tlsConfiguration.GetMaxProtocol();
110    EXPECT_EQ(maxProtocol, TLS_V1_2);
111}
112
113HWTEST_F(TlsConfigurationTest, UseRemoteCipherPrefer, TestSize.Level2)
114{
115    TLSConfiguration tlsConfiguration;
116    tlsConfiguration.SetUseRemoteCipherPrefer(true);
117    bool isUsePemoteCipherPrefer = tlsConfiguration.GetUseRemoteCipherPrefer();
118    EXPECT_TRUE(isUsePemoteCipherPrefer);
119}
120
121HWTEST_F(TlsConfigurationTest, CipherSuite, TestSize.Level2)
122{
123    TLSConfiguration tlsConfiguration;
124    std::string cipherSuite = "AES256-SHA256";
125    tlsConfiguration.SetCipherSuite(cipherSuite);
126    std::string getCipherSuite;
127    getCipherSuite = tlsConfiguration.GetCipherSuite();
128    std::cout << "getCipherSuite:" << getCipherSuite << std::endl;
129    int idx = getCipherSuite.find(cipherSuite);
130    EXPECT_NE(idx, std::string::npos);
131}
132
133HWTEST_F(TlsConfigurationTest, SignatureAlgorithms, TestSize.Level2)
134{
135    TLSConfiguration tlsConfiguration;
136    std::string signatureAlgorithms = "rsa_pss_rsae_sha256:ECDSA+SHA256";
137    tlsConfiguration.SetSignatureAlgorithms(signatureAlgorithms);
138    std::string getSignatureAlgorithms;
139    getSignatureAlgorithms = tlsConfiguration.GetSignatureAlgorithms();
140    std::cout << "getSignatureAlgorithms:" << getSignatureAlgorithms << std::endl;
141    std::string subStr = "ECDSA+SHA256";
142    int idx = getSignatureAlgorithms.find(subStr);
143    EXPECT_NE(idx, std::string::npos);
144}
145} // namespace TlsSocket
146} // namespace NetStack
147} // namespace OHOS
148