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 <memory>
17
18 #include <gtest/gtest.h>
19
20 #include "net_manager_constants.h"
21 #include "vpn_config.h"
22
23 #ifdef GTEST_API_
24 #define private public
25 #define protected public
26 #endif
27
28 #include "ipsec_vpn_ctl.h"
29
30 namespace OHOS {
31 namespace NetManagerStandard {
32 using namespace testing::ext;
33
34 class IpsecVpnCtlTest : public testing::Test {
35 public:
36 static inline std::unique_ptr<IpsecVpnCtl> ipsecControl_ = nullptr;
37 static void SetUpTestSuite();
38 };
39
SetUpTestSuite()40 void IpsecVpnCtlTest::SetUpTestSuite()
41 {
42 sptr<IpsecVpnConfig> ipsecConfig = new (std::nothrow) IpsecVpnConfig();
43 if (ipsecConfig == nullptr) {
44 return;
45 }
46 int32_t userId = 0;
47 std::vector<int32_t> activeUserIds;
48 ipsecControl_ = std::make_unique<IpsecVpnCtl>(ipsecConfig, "pkg", userId, activeUserIds);
49 if (ipsecControl_ == nullptr) {
50 return;
51 }
52 ipsecControl_->ipsecVpnConfig_ = ipsecConfig;
53 }
54
55 HWTEST_F(IpsecVpnCtlTest, SetUp001, TestSize.Level1)
56 {
57 if (ipsecControl_ == nullptr) {
58 return;
59 }
60 EXPECT_EQ(ipsecControl_->SetUp(), NETMANAGER_EXT_SUCCESS);
61 }
62
63 HWTEST_F(IpsecVpnCtlTest, Destroy001, TestSize.Level1)
64 {
65 if (ipsecControl_ == nullptr) {
66 return;
67 }
68 EXPECT_EQ(ipsecControl_->Destroy(), NETMANAGER_EXT_SUCCESS);
69 }
70
71 HWTEST_F(IpsecVpnCtlTest, IsInternalVpn001, TestSize.Level1)
72 {
73 if (ipsecControl_ == nullptr) {
74 return;
75 }
76 EXPECT_EQ(ipsecControl_->IsInternalVpn(), true);
77 }
78
79 HWTEST_F(IpsecVpnCtlTest, GetConnectedSysVpnConfigTest001, TestSize.Level1)
80 {
81 if (ipsecControl_ == nullptr) {
82 return;
83 }
84 sptr<SysVpnConfig> resConfig = nullptr;
85 int32_t ret = ipsecControl_->GetConnectedSysVpnConfig(resConfig);
86 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
87 ipsecControl_->state_ = IpsecVpnStateCode::STATE_CONNECTED;
88 ret = ipsecControl_->GetConnectedSysVpnConfig(resConfig);
89 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
90 sptr<IpsecVpnConfig> tmp = ipsecControl_->ipsecVpnConfig_;
91 ipsecControl_->ipsecVpnConfig_ = nullptr;
92 ret = ipsecControl_->GetConnectedSysVpnConfig(resConfig);
93 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
94 ipsecControl_->ipsecVpnConfig_ = tmp;
95 }
96
97 HWTEST_F(IpsecVpnCtlTest, NotifyConnectStageTest001, TestSize.Level1)
98 {
99 if (ipsecControl_ == nullptr) {
100 return;
101 }
102 std::string stage;
103 int32_t errorCode = 1;
104 int32_t ret = ipsecControl_->NotifyConnectStage(stage, errorCode);
105 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PARAMETER_ERROR);
106
107 stage = IPSEC_START_TAG;
108 ret = ipsecControl_->NotifyConnectStage(stage, errorCode);
109 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_INTERNAL);
110
111 errorCode = NETMANAGER_EXT_SUCCESS;
112 ipsecControl_->state_ = IpsecVpnStateCode::STATE_INIT;
113 ret = ipsecControl_->NotifyConnectStage(stage, errorCode);
114 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
115
116 ipsecControl_->state_ = IpsecVpnStateCode::STATE_STARTED;
117 stage = SWANCTL_START_TAG;
118 ret = ipsecControl_->NotifyConnectStage(stage, errorCode);
119 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
120
121 ipsecControl_->state_ = IpsecVpnStateCode::STATE_CONFIGED;
122 stage = IPSEC_CONNECT_TAG;
123 ret = ipsecControl_->NotifyConnectStage(stage, errorCode);
124 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
125
126 ipsecControl_->state_ = IpsecVpnStateCode::STATE_DISCONNECTED;
127 ret = ipsecControl_->NotifyConnectStage(stage, errorCode);
128 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_INTERNAL);
129
130 stage = "stageTest";
131 ipsecControl_->state_ = IpsecVpnStateCode::STATE_INIT;
132 EXPECT_EQ(ipsecControl_->NotifyConnectStage(stage, errorCode), NETMANAGER_EXT_SUCCESS);
133 ipsecControl_->state_ = IpsecVpnStateCode::STATE_STARTED;
134 EXPECT_EQ(ipsecControl_->NotifyConnectStage(stage, errorCode), NETMANAGER_EXT_SUCCESS);
135 ipsecControl_->state_ = IpsecVpnStateCode::STATE_CONFIGED;
136 EXPECT_EQ(ipsecControl_->NotifyConnectStage(stage, errorCode), NETMANAGER_EXT_SUCCESS);
137 }
138
139 HWTEST_F(IpsecVpnCtlTest, GetSysVpnCertUriTest001, TestSize.Level1)
140 {
141 sptr<IpsecVpnConfig> config = new (std::nothrow) IpsecVpnConfig();
142 if (config == nullptr) {
143 return;
144 }
145 if (ipsecControl_ == nullptr) {
146 return;
147 }
148 config->ipsecCaCertConf_ = "testCaUri";
149 ipsecControl_->ipsecVpnConfig_ = nullptr;
150 int32_t certType = 0;
151 std::string certUri;
152 EXPECT_EQ(ipsecControl_->GetSysVpnCertUri(certType, certUri), NETMANAGER_EXT_ERR_INTERNAL);
153 ipsecControl_->ipsecVpnConfig_ = config;
154 EXPECT_EQ(ipsecControl_->GetSysVpnCertUri(certType, certUri), NETMANAGER_EXT_SUCCESS);
155 EXPECT_EQ("testCaUri", certUri);
156 }
157
158 HWTEST_F(IpsecVpnCtlTest, GetSysVpnCertUriTest002, TestSize.Level1)
159 {
160 sptr<IpsecVpnConfig> config = new (std::nothrow) IpsecVpnConfig();
161 if (config == nullptr) {
162 return;
163 }
164 if (ipsecControl_ == nullptr) {
165 return;
166 }
167 config->ipsecPublicUserCertConf_ = "testUserUri";
168 ipsecControl_->ipsecVpnConfig_ = config;
169 std::string certUri;
170 int32_t certType = 1;
171 EXPECT_EQ(ipsecControl_->GetSysVpnCertUri(certType, certUri), NETMANAGER_EXT_SUCCESS);
172 EXPECT_EQ("testUserUri", certUri);
173 certType = 2;
174 EXPECT_EQ(ipsecControl_->GetSysVpnCertUri(certType, certUri), NETMANAGER_EXT_SUCCESS);
175 certType = -1;
176 EXPECT_EQ(ipsecControl_->GetSysVpnCertUri(certType, certUri), NETMANAGER_EXT_SUCCESS);
177 }
178 } // namespace NetManagerStandard
179 } // namespace OHOS