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 <fstream>
17 #include <gtest/gtest.h>
18 #include <iostream>
19 #include <openssl/rsa.h>
20 #include <openssl/ssl.h>
21 #include <sstream>
22 #include <string>
23 #include <string_view>
24 #include <unistd.h>
25 #include <vector>
26
27 #include "net_address.h"
28 #include "secure_data.h"
29 #include "socket_error.h"
30 #include "socket_state_base.h"
31 #include "tls.h"
32 #include "tls_certificate.h"
33 #include "tls_configuration.h"
34 #include "tls_key.h"
35 #include "tls_socket.h"
36
37 namespace OHOS {
38 namespace NetStack {
39 namespace TlsSocket {
40 namespace {
41 const std::string_view PRIVATE_KEY_PEM = "/data/ClientCert/client_rsa_private.pem.unsecure";
42 const std::string_view CA_DER = "/data/ClientCert/ca.crt";
43 const std::string_view CLIENT_CRT = "/data/ClientCert/client.crt";
44 const std::string_view IP_ADDRESS = "/data/Ip/address.txt";
45 const std::string_view PORT = "/data/Ip/port.txt";
46
CheckCaFileExistence(const char * function)47 inline bool CheckCaFileExistence(const char *function)
48 {
49 if (access(CA_DER.data(), 0)) {
50 std::cout << "CA file does not exist! (" << function << ")";
51 return false;
52 }
53 return true;
54 }
55
ChangeToFile(std::string_view fileName)56 std::string ChangeToFile(std::string_view fileName)
57 {
58 std::ifstream file;
59 file.open(fileName);
60 std::stringstream ss;
61 ss << file.rdbuf();
62 std::string infos = ss.str();
63 file.close();
64 return infos;
65 }
66
GetIp(std::string ip)67 std::string GetIp(std::string ip)
68 {
69 return ip.substr(0, ip.length() - 1);
70 }
71 } // namespace
72
73 class TlsSocketTest : public testing::Test {
74 public:
SetUpTestCase()75 static void SetUpTestCase() {}
76
TearDownTestCase()77 static void TearDownTestCase() {}
78
SetUp()79 virtual void SetUp() {}
80
TearDown()81 virtual void TearDown() {}
82 };
83
SetOneWayHwTestShortParam(TLSSocket & server)84 void SetOneWayHwTestShortParam(TLSSocket &server)
85 {
86 TLSConnectOptions options;
87 TLSSecureOptions secureOption;
88 Socket::NetAddress address;
89
90 address.SetAddress(GetIp(ChangeToFile(IP_ADDRESS)));
91 address.SetPort(std::atoi(ChangeToFile(PORT).c_str()));
92 address.SetFamilyBySaFamily(AF_INET);
93
94 secureOption.SetKey(SecureData(ChangeToFile(PRIVATE_KEY_PEM)));
95 std::vector<std::string> caVec = {ChangeToFile(CA_DER)};
96 secureOption.SetCaChain(caVec);
97 secureOption.SetCert(ChangeToFile(CLIENT_CRT));
98
99 options.SetNetAddress(address);
100 options.SetTlsSecureOptions(secureOption);
101
102 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
103 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
104 }
105
SetOneWayHwTestLongParam(TLSSocket & server)106 void SetOneWayHwTestLongParam(TLSSocket &server)
107 {
108 TLSConnectOptions options;
109 TLSSecureOptions secureOption;
110 Socket::NetAddress address;
111
112 address.SetAddress(GetIp(ChangeToFile(IP_ADDRESS)));
113 address.SetPort(std::atoi(ChangeToFile(PORT).c_str()));
114 address.SetFamilyBySaFamily(AF_INET);
115
116 secureOption.SetKey(SecureData(ChangeToFile(PRIVATE_KEY_PEM)));
117 std::vector<std::string> caVec = {ChangeToFile(CA_DER)};
118 secureOption.SetCaChain(caVec);
119 secureOption.SetCert(ChangeToFile(CLIENT_CRT));
120 secureOption.SetCipherSuite("AES256-SHA256");
121 std::string protocolV13 = "TLSv1.3";
122 std::vector<std::string> protocolVec = {protocolV13};
123 secureOption.SetProtocolChain(protocolVec);
124
125 options.SetNetAddress(address);
126 options.SetTlsSecureOptions(secureOption);
127
128 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
129 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
130 }
131
132 HWTEST_F(TlsSocketTest, bindInterface, testing::ext::TestSize.Level2)
133 {
134 if (!CheckCaFileExistence("bindInterface")) {
135 return;
136 }
137
138 TLSSocket server;
139 Socket::NetAddress address;
140
141 address.SetAddress(GetIp(ChangeToFile(IP_ADDRESS)));
142 address.SetPort(std::atoi(ChangeToFile(PORT).c_str()));
143 address.SetFamilyBySaFamily(AF_INET);
144
__anonf57686770602(int32_t errCode) 145 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
146 }
147
148 HWTEST_F(TlsSocketTest, connectInterface, testing::ext::TestSize.Level2)
149 {
150 if (!CheckCaFileExistence("connectInterface")) {
151 return;
152 }
153
154 TLSSocket server;
155 SetOneWayHwTestShortParam(server);
156
157 const std::string data = "how do you do? this is connectInterface";
158 Socket::TCPSendOptions tcpSendOptions;
159 tcpSendOptions.SetData(data);
__anonf57686770702(int32_t errCode) 160 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
161 sleep(2);
162
__anonf57686770802(int32_t errCode) 163 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
164 sleep(2);
165 }
166
167 HWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2)
168 {
169 if (!CheckCaFileExistence("closeInterface")) {
170 return;
171 }
172
173 TLSSocket server;
174 SetOneWayHwTestShortParam(server);
175
176 const std::string data = "how do you do? this is closeInterface";
177 Socket::TCPSendOptions tcpSendOptions;
178 tcpSendOptions.SetData(data);
179
__anonf57686770902(int32_t errCode) 180 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
181 sleep(2);
182
__anonf57686770a02(int32_t errCode) 183 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
184 }
185
186 HWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2)
187 {
188 if (!CheckCaFileExistence("sendInterface")) {
189 return;
190 }
191
192 TLSSocket server;
193 SetOneWayHwTestShortParam(server);
194
195 const std::string data = "how do you do? this is sendInterface";
196 Socket::TCPSendOptions tcpSendOptions;
197 tcpSendOptions.SetData(data);
198
__anonf57686770b02(int32_t errCode) 199 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
200 sleep(2);
201
__anonf57686770c02(int32_t errCode) 202 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
203 }
204
205 HWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2)
206 {
207 if (!CheckCaFileExistence("getRemoteAddressInterface")) {
208 return;
209 }
210
211 TLSSocket server;
212 TLSConnectOptions options;
213 TLSSecureOptions secureOption;
214 Socket::NetAddress address;
215
216 address.SetAddress(GetIp(ChangeToFile(IP_ADDRESS)));
217 address.SetPort(std::atoi(ChangeToFile(PORT).c_str()));
218 address.SetFamilyBySaFamily(AF_INET);
219
220 secureOption.SetKey(SecureData(ChangeToFile(PRIVATE_KEY_PEM)));
221 std::vector<std::string> caVec = {ChangeToFile(CA_DER)};
222 secureOption.SetCaChain(caVec);
223 secureOption.SetCert(ChangeToFile(CLIENT_CRT));
224
225 options.SetNetAddress(address);
226 options.SetTlsSecureOptions(secureOption);
227
__anonf57686770d02(int32_t errCode) 228 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
__anonf57686770e02(int32_t errCode) 229 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
230
231 Socket::NetAddress netAddress;
__anonf57686770f02(int32_t errCode, const Socket::NetAddress &address) 232 server.GetRemoteAddress([&netAddress](int32_t errCode, const Socket::NetAddress &address) {
233 EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
234 netAddress.SetAddress(address.GetAddress());
235 netAddress.SetPort(address.GetPort());
236 netAddress.SetFamilyBySaFamily(address.GetSaFamily());
237 });
238 EXPECT_STREQ(netAddress.GetAddress().c_str(), GetIp(ChangeToFile(IP_ADDRESS)).c_str());
239 EXPECT_EQ(address.GetPort(), std::atoi(ChangeToFile(PORT).c_str()));
240 EXPECT_EQ(netAddress.GetSaFamily(), AF_INET);
241
242 const std::string data = "how do you do? this is getRemoteAddressInterface";
243 Socket::TCPSendOptions tcpSendOptions;
244 tcpSendOptions.SetData(data);
245
__anonf57686771002(int32_t errCode) 246 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
247
__anonf57686771102(int32_t errCode) 248 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
249 }
250
251 HWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2)
252 {
253 if (!CheckCaFileExistence("getRemoteAddressInterface")) {
254 return;
255 }
256
257 TLSSocket server;
258 SetOneWayHwTestShortParam(server);
259
260 Socket::SocketStateBase TlsSocketstate;
__anonf57686771202(int32_t errCode, const Socket::SocketStateBase &state) 261 server.GetState([&TlsSocketstate](int32_t errCode, const Socket::SocketStateBase &state) {
262 EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
263 TlsSocketstate = state;
264 });
265 std::cout << "TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl;
266 EXPECT_TRUE(TlsSocketstate.IsBound());
267 EXPECT_TRUE(!TlsSocketstate.IsClose());
268 EXPECT_TRUE(TlsSocketstate.IsConnected());
269
270 const std::string data = "how do you do? this is getStateInterface";
271 Socket::TCPSendOptions tcpSendOptions;
272 tcpSendOptions.SetData(data);
__anonf57686771302(int32_t errCode) 273 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
274
275 sleep(2);
276
__anonf57686771402(int32_t errCode) 277 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
278 }
279
280 HWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2)
281 {
282 if (!CheckCaFileExistence("getRemoteCertificateInterface")) {
283 return;
284 }
285 TLSSocket server;
286 SetOneWayHwTestShortParam(server);
287
288 const std::string data = "how do you do? This is UT test getRemoteCertificateInterface";
289 Socket::TCPSendOptions tcpSendOptions;
290 tcpSendOptions.SetData(data);
291
__anonf57686771502(int32_t errCode) 292 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
293
294 server.GetRemoteCertificate(
__anonf57686771602(int32_t errCode, const X509CertRawData &cert) 295 [](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
296
297 sleep(2);
__anonf57686771702(int32_t errCode) 298 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
299 }
300
301 HWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2)
302 {
303 if (!CheckCaFileExistence("protocolInterface")) {
304 return;
305 }
306 TLSSocket server;
307 SetOneWayHwTestLongParam(server);
308
309 const std::string data = "how do you do? this is protocolInterface";
310 Socket::TCPSendOptions tcpSendOptions;
311 tcpSendOptions.SetData(data);
312
__anonf57686771802(int32_t errCode) 313 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
314 std::string getProtocolVal;
__anonf57686771902(int32_t errCode, const std::string &protocol) 315 server.GetProtocol([&getProtocolVal](int32_t errCode, const std::string &protocol) {
316 EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
317 getProtocolVal = protocol;
318 });
319 EXPECT_STREQ(getProtocolVal.c_str(), "TLSv1.3");
320
321 Socket::SocketStateBase stateBase;
__anonf57686771a02(int32_t errCode, Socket::SocketStateBase state) 322 server.GetState([&stateBase](int32_t errCode, Socket::SocketStateBase state) {
323 if (errCode == TLSSOCKET_SUCCESS) {
324 EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
325 stateBase.SetIsBound(state.IsBound());
326 stateBase.SetIsClose(state.IsClose());
327 stateBase.SetIsConnected(state.IsConnected());
328 }
329 });
330 EXPECT_TRUE(stateBase.IsConnected());
331 sleep(2);
332
__anonf57686771b02(int32_t errCode) 333 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
334 }
335
336 HWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2)
337 {
338 if (!CheckCaFileExistence("getCipherSuiteInterface")) {
339 return;
340 }
341 TLSSocket server;
342 SetOneWayHwTestLongParam(server);
343
344 bool flag = false;
345 const std::string data = "how do you do? This is getCipherSuiteInterface";
346 Socket::TCPSendOptions tcpSendOptions;
347 tcpSendOptions.SetData(data);
__anonf57686771c02(int32_t errCode) 348 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
349
350 std::vector<std::string> cipherSuite;
__anonf57686771d02(int32_t errCode, const std::vector<std::string> &suite) 351 server.GetCipherSuite([&cipherSuite](int32_t errCode, const std::vector<std::string> &suite) {
352 if (errCode == TLSSOCKET_SUCCESS) {
353 cipherSuite = suite;
354 }
355 });
356
357 for (auto const &iter : cipherSuite) {
358 if (iter == "AES256-SHA256") {
359 flag = true;
360 }
361 }
362
363 EXPECT_TRUE(flag);
364 sleep(2);
365
__anonf57686771e02(int32_t errCode) 366 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
367 }
368
369 HWTEST_F(TlsSocketTest, onMessageDataInterface, testing::ext::TestSize.Level2)
370 {
371 if (!CheckCaFileExistence("tlsSocketOnMessageData")) {
372 return;
373 }
374 std::string getData = "server->client";
375 TLSSocket server;
376 SetOneWayHwTestLongParam(server);
377
__anonf57686771f02(const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) 378 server.OnMessage([&getData](const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) {
379 EXPECT_TRUE(data == getData);
380 });
381
382 const std::string data = "how do you do? this is tlsSocketOnMessageData";
383 Socket::TCPSendOptions tcpSendOptions;
384 tcpSendOptions.SetData(data);
__anonf57686772002(int32_t errCode) 385 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
386
387 sleep(2);
__anonf57686772102(int32_t errCode) 388 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
389 }
390 } // namespace TlsSocket
391 } // namespace NetStack
392 } // namespace OHOS
393