1 /*
2 * Copyright (c) 2024 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 <gmock/gmock.h>
18 #include <openssl/ssl.h>
19 
20 #ifdef GTEST_API_
21 #define private public
22 #endif
23 
24 #include "tls_socket_server.h"
25 
26 namespace OHOS {
27 namespace NetStack {
28 namespace TlsSocketServer {
29 class MockSSL {
30     MOCK_METHOD(int, SSL_shutdown, (SSL*));
31     MOCK_METHOD(void, SSL_free, (SSL*));
32     MOCK_METHOD(void, SSL_CTX_free, (SSL_CTX*));
33 };
34 
35 
36 class TlsSocketServerMockBranchTest : public testing::Test {
37 protected:
38     TLSSocketServer::Connection *connection;
SetUp()39     void SetUp() override
40     {
41         connection = new TLSSocketServer::Connection();
42     }
43 
TearDown()44     void TearDown() override
45     {
46         delete connection;
47         connection = nullptr;
48     }
49 };
50 
51 HWTEST_F(TlsSocketServerMockBranchTest, TestClose001, testing::ext::TestSize.Level2)
52 {
53     MockSSL mockSsl;
54     connection->ssl_ = nullptr;
55     EXPECT_FALSE(connection->Close());
56 }
57 
58 HWTEST_F(TlsSocketServerMockBranchTest, TestClose002, testing::ext::TestSize.Level2)
59 {
60     MockSSL mockSsl;
61     SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
62     SSL *ssl = SSL_new(ctx);
63     connection->ssl_ = (SSL *)ssl;
64     EXPECT_FALSE(connection->Close());
65 }
66 
67 HWTEST_F(TlsSocketServerMockBranchTest, TestClose004, testing::ext::TestSize.Level2)
68 {
69     MockSSL mockSsl;
70     SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
71     SSL *ssl = SSL_new(ctx);
72     connection->ssl_ = (SSL *)ssl;
73     connection->socketFd_ = socket(AF_INET, SOCK_STREAM, 0);
74     EXPECT_FALSE(connection->Close());
75 }
76 
77 HWTEST_F(TlsSocketServerMockBranchTest, TestClose005, testing::ext::TestSize.Level2)
78 {
79     MockSSL mockSsl;
80     SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
81     SSL *ssl = SSL_new(ctx);
82     connection->ssl_ = (SSL *)ssl;
83     connection->tlsContextServerPointer_ = nullptr;
84     EXPECT_FALSE(connection->Close());
85 }
86 
87 HWTEST_F(TlsSocketServerMockBranchTest, TestClose006, testing::ext::TestSize.Level2)
88 {
89     MockSSL mockSsl;
90     SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
91     SSL *ssl = SSL_new(ctx);
92     connection->ssl_ = (SSL *)ssl;
93     TlsSocket::TLSConfiguration connectionConfiguration_;
94     connection->tlsContextServerPointer_ = TlsSocket::TLSContextServer::CreateConfiguration(connectionConfiguration_);
95     EXPECT_FALSE(connection->Close());
96 }
97 
98 
99 HWTEST_F(TlsSocketServerMockBranchTest, TlsSocketServerBranchTest020, testing::ext::TestSize.Level2)
100 {
101     SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
102     SSL *ssl = SSL_new(ctx);
103     connection->ssl_ = (SSL *)ssl;
104     connection->connectionConfiguration_.protocol_ = TlsSocket::TLS_V1_2;
105     EXPECT_EQ(connection->GetProtocol(), TlsSocket::PROTOCOL_TLS_V12);
106     connection->connectionConfiguration_.protocol_ = TlsSocket::TLS_V1_3;
107     EXPECT_EQ(connection->GetProtocol(), TlsSocket::PROTOCOL_TLS_V13);
108     SSL_free(ssl);
109     SSL_CTX_free(ctx);
110 }
111 } // namespace TlsSocketServer
112 } // namespace NetStack
113 } // namespace OHOS
114