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 {
40 const std::string_view PRIVATE_KEY_PEM_CHAIN = "/data/ClientCertChain/privekey.pem.unsecure";
41 const std::string_view CA_PATH_CHAIN = "/data/ClientCertChain/cacert.crt";
42 const std::string_view MID_CA_PATH_CHAIN = "/data/ClientCertChain/caMidcert.crt";
43 const std::string_view CLIENT_CRT_CHAIN = "/data/ClientCertChain/secondServer.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_PATH_CHAIN.data(), 0)) {
50 std::cout << "CA file does not exist! (" << function << ")";
51 return false;
52 }
53 return true;
54 }
55
ReadFileContent(const std::string_view fileName)56 std::string ReadFileContent(const 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
84 HWTEST_F(TlsSocketTest, bindInterface, testing::ext::TestSize.Level2)
85 {
86 if (!CheckCaFileExistence("bindInterface")) {
87 return;
88 }
89
90 TLSSocket server;
91 NetAddress address;
92
93 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
94 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
95 address.SetFamilyBySaFamily(AF_INET);
96
__anon14ae48160202(int32_t errCode) 97 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
98 }
99
100 HWTEST_F(TlsSocketTest, connectInterface, testing::ext::TestSize.Level2)
101 {
102 if (!CheckCaFileExistence("connectInterface")) {
103 return;
104 }
105 TLSConnectOptions options;
106 TLSSocket server;
107
108 TLSSecureOptions secureOption;
109 NetAddress address;
110
111 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
112 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
113 address.SetFamilyBySaFamily(AF_INET);
114
115 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
116 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
117 secureOption.SetCaChain(caVec);
118 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
119
120 options.SetNetAddress(address);
121 options.SetTlsSecureOptions(secureOption);
122
__anon14ae48160302(int32_t errCode) 123 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
124
__anon14ae48160402(int32_t errCode) 125 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
126
127 const std::string data = "how do you do? this is connectInterface";
128 TCPSendOptions tcpSendOptions;
129 tcpSendOptions.SetData(data);
__anon14ae48160502(int32_t errCode) 130 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
131 sleep(2);
132
__anon14ae48160602(int32_t errCode) 133 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
134 sleep(2);
135 }
136
137 HWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2)
138 {
139 if (!CheckCaFileExistence("closeInterface")) {
140 return;
141 }
142
143 TLSConnectOptions options;
144 TLSSocket server;
145
146 TLSSecureOptions secureOption;
147 NetAddress address;
148
149 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
150 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
151 address.SetFamilyBySaFamily(AF_INET);
152
153 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
154 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
155 secureOption.SetCaChain(caVec);
156 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
157
158 options.SetNetAddress(address);
159 options.SetTlsSecureOptions(secureOption);
160
__anon14ae48160702(int32_t errCode) 161 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
162
__anon14ae48160802(int32_t errCode) 163 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
164
165 const std::string data = "how do you do? this is closeInterface";
166 TCPSendOptions tcpSendOptions;
167 tcpSendOptions.SetData(data);
168
__anon14ae48160902(int32_t errCode) 169 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
170 sleep(2);
171
__anon14ae48160a02(int32_t errCode) 172 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
173 }
174
175 HWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2)
176 {
177 if (!CheckCaFileExistence("sendInterface")) {
178 return;
179 }
180 TLSConnectOptions options;
181 TLSSocket server;
182
183 TLSSecureOptions secureOption;
184 NetAddress address;
185
186 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
187 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
188 address.SetFamilyBySaFamily(AF_INET);
189
190 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
191 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
192 secureOption.SetCaChain(caVec);
193 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
194
195 options.SetNetAddress(address);
196 options.SetTlsSecureOptions(secureOption);
197
__anon14ae48160b02(int32_t errCode) 198 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
199
__anon14ae48160c02(int32_t errCode) 200 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
201
202 const std::string data = "how do you do? this is sendInterface";
203 TCPSendOptions tcpSendOptions;
204 tcpSendOptions.SetData(data);
205
__anon14ae48160d02(int32_t errCode) 206 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
207 sleep(2);
208
__anon14ae48160e02(int32_t errCode) 209 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
210 }
211
212 HWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2)
213 {
214 if (!CheckCaFileExistence("getRemoteAddressInterface")) {
215 return;
216 }
217
218 TLSConnectOptions options;
219 TLSSocket server;
220 TLSSecureOptions secureOption;
221 NetAddress address;
222
223 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
224 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
225 address.SetFamilyBySaFamily(AF_INET);
226
227 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
228 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
229 secureOption.SetCaChain(caVec);
230 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
231
232 options.SetNetAddress(address);
233 options.SetTlsSecureOptions(secureOption);
234
__anon14ae48160f02(int32_t errCode) 235 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
236
__anon14ae48161002(int32_t errCode) 237 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
238
239 NetAddress netAddress;
__anon14ae48161102(int32_t errCode, const NetAddress &address) 240 server.GetRemoteAddress([&netAddress](int32_t errCode, const NetAddress &address) {
241 EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
242 netAddress.SetAddress(address.GetAddress());
243 netAddress.SetPort(address.GetPort());
244 netAddress.SetFamilyBySaFamily(address.GetSaFamily());
245 });
246 EXPECT_STREQ(netAddress.GetAddress().c_str(), GetIp(ReadFileContent(IP_ADDRESS)).c_str());
247 EXPECT_EQ(address.GetPort(), std::atoi(ReadFileContent(PORT).c_str()));
248 EXPECT_EQ(netAddress.GetSaFamily(), AF_INET);
249
250 const std::string data = "how do you do? this is getRemoteAddressInterface";
251 TCPSendOptions tcpSendOptions;
252 tcpSendOptions.SetData(data);
253
__anon14ae48161202(int32_t errCode) 254 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
255
__anon14ae48161302(int32_t errCode) 256 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
257 }
258
259 HWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2)
260 {
261 if (!CheckCaFileExistence("getRemoteAddressInterface")) {
262 return;
263 }
264
265 TLSConnectOptions options;
266 TLSSocket server;
267 TLSSecureOptions secureOption;
268 NetAddress address;
269
270 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
271 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
272 address.SetFamilyBySaFamily(AF_INET);
273
274 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
275 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
276 secureOption.SetCaChain(caVec);
277 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
278
279 options.SetNetAddress(address);
280 options.SetTlsSecureOptions(secureOption);
281
__anon14ae48161402(int32_t errCode) 282 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
__anon14ae48161502(int32_t errCode) 283 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
284
285 SocketStateBase TlsSocketstate;
__anon14ae48161602(int32_t errCode, const SocketStateBase &state) 286 server.GetState([&TlsSocketstate](int32_t errCode, const SocketStateBase &state) {
287 EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
288 TlsSocketstate = state;
289 });
290 std::cout << "TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl;
291 EXPECT_TRUE(TlsSocketstate.IsBound());
292 EXPECT_TRUE(!TlsSocketstate.IsClose());
293 EXPECT_TRUE(TlsSocketstate.IsConnected());
294
295 const std::string data = "how do you do? this is getStateInterface";
296 TCPSendOptions tcpSendOptions;
297 tcpSendOptions.SetData(data);
__anon14ae48161702(int32_t errCode) 298 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
299
300 sleep(2);
301
__anon14ae48161802(int32_t errCode) 302 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
303 }
304
305 HWTEST_F(TlsSocketTest, getCertificateInterface, testing::ext::TestSize.Level2)
306 {
307 if (!CheckCaFileExistence("getCertificateInterface")) {
308 return;
309 }
310 TLSSocket server;
311 TLSConnectOptions options;
312 TCPSendOptions tcpSendOptions;
313 TLSSecureOptions secureOption;
314 NetAddress address;
315 const std::string data = "how do you do? This is UT test getCertificateInterface";
316
317 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
318 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
319 address.SetFamilyBySaFamily(AF_INET);
320
321 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
322 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
323 secureOption.SetCaChain(caVec);
324 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
325
326 options.SetNetAddress(address);
327 options.SetTlsSecureOptions(secureOption);
328
__anon14ae48161902(int32_t errCode) 329 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
330
__anon14ae48161a02(int32_t errCode) 331 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
332
333 tcpSendOptions.SetData(data);
__anon14ae48161b02(int32_t errCode) 334 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
335
336 server.GetCertificate(
__anon14ae48161c02(int32_t errCode, const X509CertRawData &cert) 337 [](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
338
339 sleep(2);
__anon14ae48161d02(int32_t errCode) 340 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
341 }
342
343 HWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2)
344 {
345 if (!CheckCaFileExistence("getRemoteCertificateInterface")) {
346 return;
347 }
348 TLSSocket server;
349 TLSConnectOptions options;
350 TCPSendOptions tcpSendOptions;
351 TLSSecureOptions secureOption;
352 NetAddress address;
353 const std::string data = "how do you do? This is UT test getRemoteCertificateInterface";
354
355 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
356 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
357 address.SetFamilyBySaFamily(AF_INET);
358
359 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
360 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
361 secureOption.SetCaChain(caVec);
362 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
363
364 options.SetNetAddress(address);
365 options.SetTlsSecureOptions(secureOption);
366
__anon14ae48161e02(int32_t errCode) 367 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
368
__anon14ae48161f02(int32_t errCode) 369 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
370
371 tcpSendOptions.SetData(data);
372
__anon14ae48162002(int32_t errCode) 373 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
374
375 server.GetRemoteCertificate(
__anon14ae48162102(int32_t errCode, const X509CertRawData &cert) 376 [](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
377
378 sleep(2);
__anon14ae48162202(int32_t errCode) 379 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
380 }
381
382 HWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2)
383 {
384 if (!CheckCaFileExistence("protocolInterface")) {
385 return;
386 }
387 TLSConnectOptions options;
388 TLSSocket server;
389 TLSSecureOptions secureOption;
390 NetAddress address;
391
392 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
393 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
394 address.SetFamilyBySaFamily(AF_INET);
395
396 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
397 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
398 secureOption.SetCaChain(caVec);
399 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
400 secureOption.SetCipherSuite("AES256-SHA256");
401 std::string protocolV1_3 = "TLSv1.3";
402 std::vector<std::string> protocolVec = {protocolV1_3};
403 secureOption.SetProtocolChain(protocolVec);
404
405 options.SetNetAddress(address);
406 options.SetTlsSecureOptions(secureOption);
407
__anon14ae48162302(int32_t errCode) 408 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
409
__anon14ae48162402(int32_t errCode) 410 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
411
412 const std::string data = "how do you do? this is protocolInterface";
413 TCPSendOptions tcpSendOptions;
414 tcpSendOptions.SetData(data);
415
__anon14ae48162502(int32_t errCode) 416 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
417 std::string getProtocolVal;
__anon14ae48162602(int32_t errCode, const std::string &protocol) 418 server.GetProtocol([&getProtocolVal](int32_t errCode, const std::string &protocol) {
419 EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
420 getProtocolVal = protocol;
421 });
422 EXPECT_STREQ(getProtocolVal.c_str(), "TLSv1.3");
423 sleep(2);
424
__anon14ae48162702(int32_t errCode) 425 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
426 }
427
428 HWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2)
429 {
430 if (!CheckCaFileExistence("getCipherSuiteInterface")) {
431 return;
432 }
433
434 TLSConnectOptions options;
435 TLSSocket server;
436 TLSSecureOptions secureOption;
437 NetAddress address;
438
439 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
440 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
441 address.SetFamilyBySaFamily(AF_INET);
442
443 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
444 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
445 secureOption.SetCaChain(caVec);
446 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
447 secureOption.SetCipherSuite("AES256-SHA256");
448 std::string protocolV1_3 = "TLSv1.3";
449 std::vector<std::string> protocolVec = {protocolV1_3};
450 secureOption.SetProtocolChain(protocolVec);
451
452 options.SetNetAddress(address);
453 options.SetTlsSecureOptions(secureOption);
454
455 bool flag = false;
__anon14ae48162802(int32_t errCode) 456 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
__anon14ae48162902(int32_t errCode) 457 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
458
459 const std::string data = "how do you do? This is getCipherSuiteInterface";
460 TCPSendOptions tcpSendOptions;
461 tcpSendOptions.SetData(data);
__anon14ae48162a02(int32_t errCode) 462 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
463
464 std::vector<std::string> cipherSuite;
__anon14ae48162b02(int32_t errCode, const std::vector<std::string> &suite) 465 server.GetCipherSuite([&cipherSuite](int32_t errCode, const std::vector<std::string> &suite) {
466 EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
467 cipherSuite = suite;
468 });
469
470 for (auto const &iter : cipherSuite) {
471 if (iter == "AES256-SHA256") {
472 flag = true;
473 }
474 }
475
476 EXPECT_TRUE(flag);
477 sleep(2);
478
__anon14ae48162c02(int32_t errCode) 479 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
480 }
481
482 HWTEST_F(TlsSocketTest, getSignatureAlgorithmsInterface, testing::ext::TestSize.Level2)
483 {
484 if (!CheckCaFileExistence("getSignatureAlgorithmsInterface")) {
485 return;
486 }
487 TLSConnectOptions options;
488 TLSSocket server;
489 TLSSecureOptions secureOption;
490 NetAddress address;
491
492 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
493 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
494 address.SetFamilyBySaFamily(AF_INET);
495
496 std::string signatureAlgorithmVec = {"rsa_pss_rsae_sha256:ECDSA+SHA256"};
497 secureOption.SetSignatureAlgorithms(signatureAlgorithmVec);
498 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
499 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
500 secureOption.SetCaChain(caVec);
501 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
502 std::string protocolV1_3 = "TLSv1.3";
503 std::vector<std::string> protocolVec = {protocolV1_3};
504 secureOption.SetProtocolChain(protocolVec);
505
506 options.SetNetAddress(address);
507 options.SetTlsSecureOptions(secureOption);
508
509 bool flag = false;
__anon14ae48162d02(int32_t errCode) 510 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
511
__anon14ae48162e02(int32_t errCode) 512 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
513
514 const std::string data = "how do you do? this is getSignatureAlgorithmsInterface";
515 TCPSendOptions tcpSendOptions;
516 tcpSendOptions.SetData(data);
__anon14ae48162f02(int32_t errCode) 517 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
518
519 std::vector<std::string> signatureAlgorithms;
__anon14ae48163002(int32_t errCode, const std::vector<std::string> &algorithms) 520 server.GetSignatureAlgorithms([&signatureAlgorithms](int32_t errCode, const std::vector<std::string> &algorithms) {
521 if (errCode == TLSSOCKET_SUCCESS) {
522 signatureAlgorithms = algorithms;
523 }
524 });
525 for (auto const &iter : signatureAlgorithms) {
526 if (iter == "ECDSA+SHA256") {
527 flag = true;
528 }
529 }
530 EXPECT_TRUE(flag);
531 sleep(2);
__anon14ae48163102(int32_t errCode) 532 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
533 }
534
535 HWTEST_F(TlsSocketTest, onMessageDataInterface, testing::ext::TestSize.Level2)
536 {
537 if (!CheckCaFileExistence("tlsSocketOnMessageData")) {
538 return;
539 }
540 std::string getData = "server->client";
541 TLSConnectOptions options;
542 TLSSocket server;
543 TLSSecureOptions secureOption;
544 NetAddress address;
545
546 address.SetAddress(GetIp(ReadFileContent(IP_ADDRESS)));
547 address.SetPort(std::atoi(ReadFileContent(PORT).c_str()));
548 address.SetFamilyBySaFamily(AF_INET);
549
550 secureOption.SetKey(SecureData(ReadFileContent(PRIVATE_KEY_PEM_CHAIN)));
551 std::vector<std::string> caVec = {ReadFileContent(CA_PATH_CHAIN), ReadFileContent(MID_CA_PATH_CHAIN)};
552 secureOption.SetCaChain(caVec);
553 secureOption.SetCert(ReadFileContent(CLIENT_CRT_CHAIN));
554 secureOption.SetCipherSuite("AES256-SHA256");
555 std::string protocolV1_3 = "TLSv1.3";
556 std::vector<std::string> protocolVec = {protocolV1_3};
557 secureOption.SetProtocolChain(protocolVec);
558
559 options.SetNetAddress(address);
560 options.SetTlsSecureOptions(secureOption);
561
__anon14ae48163202(int32_t errCode) 562 server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
563
__anon14ae48163302(int32_t errCode) 564 server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
__anon14ae48163402(const std::string &data, const SocketRemoteInfo &remoteInfo) 565 server.OnMessage([&getData](const std::string &data, const SocketRemoteInfo &remoteInfo) {
566 if (data == getData) {
567 EXPECT_TRUE(true);
568 } else {
569 EXPECT_TRUE(false);
570 }
571 });
572
573 const std::string data = "how do you do? this is tlsSocketOnMessageData";
574 TCPSendOptions tcpSendOptions;
575 tcpSendOptions.SetData(data);
__anon14ae48163502(int32_t errCode) 576 server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
577
578 sleep(2);
__anon14ae48163602(int32_t errCode) 579 (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
580 }
581 } // namespace NetStack
582 } // namespace OHOS
583