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 "gtest/gtest.h"
17 
18 #define private public
19 #include "tls_key.h"
20 
21 namespace OHOS::NetStack::TlsSocket {
22 class TLSKeyTest : public testing::Test {
23 public:
24     static void SetUpTestCase();
25     static void TearDownTestCase();
26     void SetUp() override;
27     void TearDown() override;
28 };
29 
SetUpTestCase()30 void TLSKeyTest::SetUpTestCase() {}
TearDownTestCase()31 void TLSKeyTest::TearDownTestCase() {}
SetUp()32 void TLSKeyTest::SetUp() {}
TearDown()33 void TLSKeyTest::TearDown() {}
34 
HWTEST_F(TLSKeyTest, TLSKeyTestt001, testing::ext::TestSize.Level1)35 HWTEST_F(TLSKeyTest, TLSKeyTestt001, testing::ext::TestSize.Level1)
36 {
37     TLSKey key;
38     TLSKey other;
39     key = other;
40     EXPECT_EQ(key.opaque_, nullptr);
41     EXPECT_EQ(key.rsa_, nullptr);
42     EXPECT_EQ(key.dsa_, nullptr);
43     EXPECT_EQ(key.dh_, nullptr);
44     EXPECT_EQ(key.ec_, nullptr);
45     EXPECT_EQ(key.genericKey_, nullptr);
46     other.rsa_ = RSA_new();
47     other.dsa_ = DSA_new();
48     other.dh_ = DH_new();
49     other.ec_ = EC_KEY_new();
50     other.genericKey_ = EVP_PKEY_new();
51     key = other;
52     EXPECT_NE(key.rsa_, nullptr);
53     EXPECT_NE(key.dsa_, nullptr);
54     EXPECT_NE(key.dh_, nullptr);
55     EXPECT_NE(key.ec_, nullptr);
56     EXPECT_NE(key.genericKey_, nullptr);
57 }
58 
HWTEST_F(TLSKeyTest, TLSKeyTestt002, testing::ext::TestSize.Level1)59 HWTEST_F(TLSKeyTest, TLSKeyTestt002, testing::ext::TestSize.Level1)
60 {
61     TLSKey key;
62     key.rsa_ = RSA_new();
63     key.dsa_ = DSA_new();
64     key.dh_ = DH_new();
65     key.ec_ = EC_KEY_new();
66     key.genericKey_ = EVP_PKEY_new();
67     SecureData data;
68     SecureData phrase;
69     key.DecodeData(data, phrase);
70 
71     key.keyAlgorithm_ = OPAQUE;
72     EXPECT_EQ(key.handle(), nullptr);
73     key.keyAlgorithm_ = ALGORITHM_RSA;
74     EXPECT_NE(key.handle(), nullptr);
75     key.keyAlgorithm_ = ALGORITHM_DSA;
76     EXPECT_NE(key.handle(), nullptr);
77     key.keyAlgorithm_ = ALGORITHM_DH;
78     EXPECT_NE(key.handle(), nullptr);
79     key.keyAlgorithm_ = ALGORITHM_EC;
80     EXPECT_NE(key.handle(), nullptr);
81     key.keyAlgorithm_ = static_cast<KeyAlgorithm>(9999999);
82     EXPECT_EQ(key.handle(), nullptr);
83 }
84 } // namespace OHOS::NetStack::TlsSocket
85