1 /*
2 * Copyright (c) 2023 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 <fstream>
17
18 #include "gtest/gtest.h"
19 #include "openssl/ssl.h"
20 #include "openssl/crypto.h"
21
22 #define private public
23 #include "tls_context.h"
24
25 namespace OHOS::NetStack::TlsSocket {
26 class TlsContextTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp() override;
31 void TearDown() override;
32 };
33
SetUpTestCase()34 void TlsContextTest::SetUpTestCase() {}
TearDownTestCase()35 void TlsContextTest::TearDownTestCase() {}
SetUp()36 void TlsContextTest::SetUp() {}
TearDown()37 void TlsContextTest::TearDown() {}
38
HWTEST_F(TlsContextTest, TlsContextTest001, testing::ext::TestSize.Level1)39 HWTEST_F(TlsContextTest, TlsContextTest001, testing::ext::TestSize.Level1)
40 {
41 EXPECT_NE(TLSContext::CreateConfiguration({}), nullptr);
42 TLSConfiguration configuration;
43 configuration.signatureAlgorithms_ = "VALID";
44 configuration.useRemoteCipherPrefer_ = true;
45 TLSContext context;
46 context.ctx_ = SSL_CTX_new(TLS_client_method());
47 context.tlsConfiguration_ = configuration;
48 context.tlsConfiguration_.useRemoteCipherPrefer_ = true;
49 TLSContext::UseRemoteCipher(&context);
50 context.tlsConfiguration_.minProtocol_ = static_cast<TLSProtocol>(100);
51 TLSContext::SetMinAndMaxProtocol(&context);
52 context.tlsConfiguration_.minProtocol_ = static_cast<TLSProtocol>(UNKNOW_PROTOCOL);
53 TLSContext::SetMinAndMaxProtocol(&context);
54 context.tlsConfiguration_.minProtocol_ = static_cast<TLSProtocol>(TLS_ANY_VERSION);
55 TLSContext::SetMinAndMaxProtocol(&context);
56 context.tlsConfiguration_.minProtocol_ = static_cast<TLSProtocol>(999999999999999);
57 TLSContext::SetMinAndMaxProtocol(&context);
58 context.tlsConfiguration_.minProtocol_ = static_cast<TLSProtocol>(-1);
59 TLSContext::SetMinAndMaxProtocol(&context);
60
61 context.tlsConfiguration_.maxProtocol_ = static_cast<TLSProtocol>(100);
62 TLSContext::SetMinAndMaxProtocol(&context);
63 context.tlsConfiguration_.maxProtocol_ = static_cast<TLSProtocol>(UNKNOW_PROTOCOL);
64 TLSContext::SetMinAndMaxProtocol(&context);
65 context.tlsConfiguration_.maxProtocol_ = static_cast<TLSProtocol>(TLS_ANY_VERSION);
66 TLSContext::SetMinAndMaxProtocol(&context);
67 context.tlsConfiguration_.maxProtocol_ = static_cast<TLSProtocol>(-1);
68 TLSContext::SetMinAndMaxProtocol(&context);
69 EXPECT_EQ(TLSContext::CreateConfiguration(configuration), nullptr);
70 }
71
HWTEST_F(TlsContextTest, TlsContextTest002, testing::ext::TestSize.Level1)72 HWTEST_F(TlsContextTest, TlsContextTest002, testing::ext::TestSize.Level1)
73 {
74 TLSContext context;
75 TLSContext::GetCiphers(nullptr);
76 TLSContext::GetCiphers(&context);
77 EXPECT_FALSE(TLSContext::SetCipherList(nullptr, {}));
78 context.ctx_ = SSL_CTX_new(TLS_client_method());
79 EXPECT_FALSE(TLSContext::SetCipherList(&context, {}));
80 EXPECT_FALSE(context.SetSignatureAlgorithms(nullptr, {}));
81 EXPECT_FALSE(context.SetSignatureAlgorithms(nullptr, {}));
82 TLSConfiguration configuration;
83 configuration.signatureAlgorithms_ = "VALID";
84 EXPECT_FALSE(context.SetSignatureAlgorithms(nullptr, configuration));
85 EXPECT_FALSE(context.SetSignatureAlgorithms(&context, {}));
86 EXPECT_FALSE(context.SetSignatureAlgorithms(&context, {}));
87 EXPECT_FALSE(context.SetSignatureAlgorithms(&context, configuration));
88 }
89
HWTEST_F(TlsContextTest, TlsContextTest003, testing::ext::TestSize.Level1)90 HWTEST_F(TlsContextTest, TlsContextTest003, testing::ext::TestSize.Level1)
91 {
92 TLSContext context;
93 TLSConfiguration configuration;
94 configuration.signatureAlgorithms_ = "VALID";
95 context.ctx_ = SSL_CTX_new(TLS_client_method());
96 TLSKey key;
97
98 key.keyAlgorithm_ = ALGORITHM_RSA;
99 configuration.privateKey_ = key;
100 EXPECT_FALSE(TLSContext::SetKeyAndCheck(&context, configuration));
101
102 key.keyAlgorithm_ = ALGORITHM_DSA;
103 configuration.privateKey_ = key;
104 EXPECT_FALSE(TLSContext::SetKeyAndCheck(&context, configuration));
105
106 key.keyAlgorithm_ = ALGORITHM_DH;
107 configuration.privateKey_ = key;
108 EXPECT_FALSE(TLSContext::SetKeyAndCheck(&context, configuration));
109
110 key.keyAlgorithm_ = ALGORITHM_EC;
111 configuration.privateKey_ = key;
112 EXPECT_FALSE(TLSContext::SetKeyAndCheck(&context, configuration));
113 }
114 } // namespace OHOS::NetStack::TlsSocket