• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetSocketHwTestShortParam(TLSSocket & server)84 void SetSocketHwTestShortParam(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 
SetSocketHwTestLongParam(TLSSocket & server)106 void SetSocketHwTestLongParam(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 
__anone59ad4830602(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     TLSSocket server;
154     SetSocketHwTestShortParam(server);
155 
156     const std::string data = "how do you do? this is connectInterface";
157     Socket::TCPSendOptions tcpSendOptions;
158     tcpSendOptions.SetData(data);
__anone59ad4830702(int32_t errCode) 159     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
160     sleep(2);
161 
__anone59ad4830802(int32_t errCode) 162     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
163     sleep(2);
164 }
165 
166 HWTEST_F(TlsSocketTest, closeInterface, testing::ext::TestSize.Level2)
167 {
168     if (!CheckCaFileExistence("closeInterface")) {
169         return;
170     }
171 
172     TLSSocket server;
173     SetSocketHwTestShortParam(server);
174 
175     const std::string data = "how do you do? this is closeInterface";
176     Socket::TCPSendOptions tcpSendOptions;
177     tcpSendOptions.SetData(data);
178 
__anone59ad4830902(int32_t errCode) 179     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
180     sleep(2);
181 
__anone59ad4830a02(int32_t errCode) 182     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
183 }
184 
185 HWTEST_F(TlsSocketTest, sendInterface, testing::ext::TestSize.Level2)
186 {
187     if (!CheckCaFileExistence("sendInterface")) {
188         return;
189     }
190     TLSSocket server;
191     SetSocketHwTestShortParam(server);
192 
193     const std::string data = "how do you do? this is sendInterface";
194     Socket::TCPSendOptions tcpSendOptions;
195     tcpSendOptions.SetData(data);
196 
__anone59ad4830b02(int32_t errCode) 197     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
198     sleep(2);
199 
__anone59ad4830c02(int32_t errCode) 200     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
201 }
202 
203 HWTEST_F(TlsSocketTest, getRemoteAddressInterface, testing::ext::TestSize.Level2)
204 {
205     if (!CheckCaFileExistence("getRemoteAddressInterface")) {
206         return;
207     }
208 
209     TLSSocket server;
210     TLSConnectOptions options;
211     TLSSecureOptions secureOption;
212     Socket::NetAddress address;
213 
214     address.SetAddress(GetIp(ChangeToFile(IP_ADDRESS)));
215     address.SetPort(std::atoi(ChangeToFile(PORT).c_str()));
216     address.SetFamilyBySaFamily(AF_INET);
217 
218     secureOption.SetKey(SecureData(ChangeToFile(PRIVATE_KEY_PEM)));
219     std::vector<std::string> caVec = {ChangeToFile(CA_DER)};
220     secureOption.SetCaChain(caVec);
221     secureOption.SetCert(ChangeToFile(CLIENT_CRT));
222 
223     options.SetNetAddress(address);
224     options.SetTlsSecureOptions(secureOption);
225 
__anone59ad4830d02(int32_t errCode) 226     server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
__anone59ad4830e02(int32_t errCode) 227     server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
228 
229     Socket::NetAddress netAddress;
__anone59ad4830f02(int32_t errCode, const Socket::NetAddress &address) 230     server.GetRemoteAddress([&netAddress](int32_t errCode, const Socket::NetAddress &address) {
231         EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
232         netAddress.SetAddress(address.GetAddress());
233         netAddress.SetPort(address.GetPort());
234         netAddress.SetFamilyBySaFamily(address.GetSaFamily());
235     });
236     EXPECT_STREQ(netAddress.GetAddress().c_str(), GetIp(ChangeToFile(IP_ADDRESS)).c_str());
237     EXPECT_EQ(address.GetPort(), std::atoi(ChangeToFile(PORT).c_str()));
238     EXPECT_EQ(netAddress.GetSaFamily(), AF_INET);
239 
240     const std::string data = "how do you do? this is getRemoteAddressInterface";
241     Socket::TCPSendOptions tcpSendOptions;
242     tcpSendOptions.SetData(data);
243 
__anone59ad4831002(int32_t errCode) 244     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
245 
__anone59ad4831102(int32_t errCode) 246     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
247 }
248 
249 HWTEST_F(TlsSocketTest, getStateInterface, testing::ext::TestSize.Level2)
250 {
251     if (!CheckCaFileExistence("getRemoteAddressInterface")) {
252         return;
253     }
254 
255     TLSSocket server;
256     SetSocketHwTestShortParam(server);
257 
258     Socket::SocketStateBase TlsSocketstate;
__anone59ad4831202(int32_t errCode, const Socket::SocketStateBase &state) 259     server.GetState([&TlsSocketstate](int32_t errCode, const Socket::SocketStateBase &state) {
260         EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
261         TlsSocketstate = state;
262     });
263     std::cout << "TlsSocketstate.IsClose(): " << TlsSocketstate.IsClose() << std::endl;
264     EXPECT_TRUE(TlsSocketstate.IsBound());
265     EXPECT_TRUE(!TlsSocketstate.IsClose());
266     EXPECT_TRUE(TlsSocketstate.IsConnected());
267 
268     const std::string data = "how do you do? this is getStateInterface";
269     Socket::TCPSendOptions tcpSendOptions;
270     tcpSendOptions.SetData(data);
__anone59ad4831302(int32_t errCode) 271     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
272 
273     sleep(2);
274 
__anone59ad4831402(int32_t errCode) 275     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
276 }
277 
278 HWTEST_F(TlsSocketTest, getCertificateInterface, testing::ext::TestSize.Level2)
279 {
280     if (!CheckCaFileExistence("getCertificateInterface")) {
281         return;
282     }
283     TLSSocket server;
284     SetSocketHwTestShortParam(server);
285 
286     const std::string data = "how do you do? This is UT test getCertificateInterface";
287     Socket::TCPSendOptions tcpSendOptions;
288     tcpSendOptions.SetData(data);
__anone59ad4831502(int32_t errCode) 289     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
290 
291     server.GetCertificate(
__anone59ad4831602(int32_t errCode, const X509CertRawData &cert) 292         [](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
293 
294     sleep(2);
__anone59ad4831702(int32_t errCode) 295     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
296 }
297 
298 HWTEST_F(TlsSocketTest, getRemoteCertificateInterface, testing::ext::TestSize.Level2)
299 {
300     if (!CheckCaFileExistence("getRemoteCertificateInterface")) {
301         return;
302     }
303     TLSSocket server;
304     SetSocketHwTestShortParam(server);
305 
306     Socket::TCPSendOptions tcpSendOptions;
307     const std::string data = "how do you do? This is UT test getRemoteCertificateInterface";
308     tcpSendOptions.SetData(data);
309 
__anone59ad4831802(int32_t errCode) 310     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
311 
312     server.GetRemoteCertificate(
__anone59ad4831902(int32_t errCode, const X509CertRawData &cert) 313         [](int32_t errCode, const X509CertRawData &cert) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
314 
315     sleep(2);
__anone59ad4831a02(int32_t errCode) 316     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
317 }
318 
319 HWTEST_F(TlsSocketTest, protocolInterface, testing::ext::TestSize.Level2)
320 {
321     if (!CheckCaFileExistence("protocolInterface")) {
322         return;
323     }
324     TLSSocket server;
325     SetSocketHwTestLongParam(server);
326 
327     const std::string data = "how do you do? this is protocolInterface";
328     Socket::TCPSendOptions tcpSendOptions;
329     tcpSendOptions.SetData(data);
330 
__anone59ad4831b02(int32_t errCode) 331     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
332     std::string getProtocolVal;
__anone59ad4831c02(int32_t errCode, const std::string &protocol) 333     server.GetProtocol([&getProtocolVal](int32_t errCode, const std::string &protocol) {
334         EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
335         getProtocolVal = protocol;
336     });
337     EXPECT_STREQ(getProtocolVal.c_str(), "TLSv1.3");
338 
339     Socket::SocketStateBase stateBase;
__anone59ad4831d02(int32_t errCode, Socket::SocketStateBase state) 340     server.GetState([&stateBase](int32_t errCode, Socket::SocketStateBase state) {
341         if (errCode == TLSSOCKET_SUCCESS) {
342             EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS);
343             stateBase.SetIsBound(state.IsBound());
344             stateBase.SetIsClose(state.IsClose());
345             stateBase.SetIsConnected(state.IsConnected());
346         }
347     });
348     EXPECT_TRUE(stateBase.IsConnected());
349     sleep(2);
350 
__anone59ad4831e02(int32_t errCode) 351     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
352 }
353 
354 HWTEST_F(TlsSocketTest, getCipherSuiteInterface, testing::ext::TestSize.Level2)
355 {
356     if (!CheckCaFileExistence("getCipherSuiteInterface")) {
357         return;
358     }
359     TLSSocket server;
360     SetSocketHwTestLongParam(server);
361 
362     bool flag = false;
363     const std::string data = "how do you do? This is getCipherSuiteInterface";
364     Socket::TCPSendOptions tcpSendOptions;
365     tcpSendOptions.SetData(data);
__anone59ad4831f02(int32_t errCode) 366     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
367 
368     std::vector<std::string> cipherSuite;
__anone59ad4832002(int32_t errCode, const std::vector<std::string> &suite) 369     server.GetCipherSuite([&cipherSuite](int32_t errCode, const std::vector<std::string> &suite) {
370         if (errCode == TLSSOCKET_SUCCESS) {
371             cipherSuite = suite;
372         }
373     });
374 
375     for (auto const &iter : cipherSuite) {
376         if (iter == "AES256-SHA256") {
377             flag = true;
378         }
379     }
380 
381     EXPECT_TRUE(flag);
382     sleep(2);
383 
__anone59ad4832102(int32_t errCode) 384     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
385 }
386 
387 HWTEST_F(TlsSocketTest, getSignatureAlgorithmsInterface, testing::ext::TestSize.Level2)
388 {
389     if (!CheckCaFileExistence("getSignatureAlgorithmsInterface")) {
390         return;
391     }
392     TLSConnectOptions options;
393     TLSSocket server;
394     TLSSecureOptions secureOption;
395     Socket::NetAddress address;
396 
397     address.SetAddress(GetIp(ChangeToFile(IP_ADDRESS)));
398     address.SetPort(std::atoi(ChangeToFile(PORT).c_str()));
399     address.SetFamilyBySaFamily(AF_INET);
400 
401     std::string signatureAlgorithmVec = {"rsa_pss_rsae_sha256:ECDSA+SHA256"};
402     secureOption.SetSignatureAlgorithms(signatureAlgorithmVec);
403     secureOption.SetKey(SecureData(ChangeToFile(PRIVATE_KEY_PEM)));
404     std::vector<std::string> caVec = {ChangeToFile(CA_DER)};
405     secureOption.SetCaChain(caVec);
406     secureOption.SetCert(ChangeToFile(CLIENT_CRT));
407     std::string protocolV13 = "TLSv1.3";
408     std::vector<std::string> protocolVec = {protocolV13};
409     secureOption.SetProtocolChain(protocolVec);
410 
411     options.SetNetAddress(address);
412     options.SetTlsSecureOptions(secureOption);
413 
__anone59ad4832202(int32_t errCode) 414     server.Bind(address, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
__anone59ad4832302(int32_t errCode) 415     server.Connect(options, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
416 
417     bool flag = false;
418     const std::string data = "how do you do? this is getSignatureAlgorithmsInterface";
419     Socket::TCPSendOptions tcpSendOptions;
420     tcpSendOptions.SetData(data);
__anone59ad4832402(int32_t errCode) 421     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
422 
423     std::vector<std::string> signatureAlgorithms;
__anone59ad4832502(int32_t errCode, const std::vector<std::string> &algorithms) 424     server.GetSignatureAlgorithms([&signatureAlgorithms](int32_t errCode, const std::vector<std::string> &algorithms) {
425         if (errCode == TLSSOCKET_SUCCESS) {
426             signatureAlgorithms = algorithms;
427         }
428     });
429     for (auto const &iter : signatureAlgorithms) {
430         if (iter == "ECDSA+SHA256") {
431             flag = true;
432         }
433     }
434     EXPECT_TRUE(flag);
435     sleep(2);
__anone59ad4832602(int32_t errCode) 436     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
437 }
438 
439 HWTEST_F(TlsSocketTest, onMessageDataInterface, testing::ext::TestSize.Level2)
440 {
441     if (!CheckCaFileExistence("tlsSocketOnMessageData")) {
442         return;
443     }
444     std::string getData = "server->client";
445     TLSSocket server;
446     SetSocketHwTestLongParam(server);
447 
__anone59ad4832702(const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) 448     server.OnMessage([&getData](const std::string &data, const Socket::SocketRemoteInfo &remoteInfo) {
449         if (data == getData) {
450             EXPECT_TRUE(true);
451         } else {
452             EXPECT_TRUE(false);
453         }
454     });
455 
456     const std::string data = "how do you do? this is tlsSocketOnMessageData";
457     Socket::TCPSendOptions tcpSendOptions;
458     tcpSendOptions.SetData(data);
__anone59ad4832802(int32_t errCode) 459     server.Send(tcpSendOptions, [](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
460 
461     sleep(2);
__anone59ad4832902(int32_t errCode) 462     (void)server.Close([](int32_t errCode) { EXPECT_TRUE(errCode == TLSSOCKET_SUCCESS); });
463 }
464 } // namespace TlsSocket
465 } // namespace NetStack
466 } // namespace OHOS
467