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 */ 15import { describe, it, expect, Level, Size, TestType } from '@ohos/hypium' 16import socket from "@ohos.net.socket"; 17import { BusinessError, Callback } from '@ohos.base'; 18import { ArrayBufferToString } from './utils'; 19 20 21function expectSuccess(): void { 22 expect(true).assertTrue(); 23} 24 25 26function expectFail(info: string = ''): void { 27 try { 28 expect(false).assertTrue(); 29 } catch (err) { 30 console.info(`${info} test failed`); 31 } 32} 33 34 35function expectTrue(exp: boolean, info: string = ''): void { 36 try { 37 expect(exp).assertTrue(); 38 } catch (err) { 39 console.info(`${info} test failed`); 40 } 41} 42 43function expectFalse(exp: boolean, info: string = ''): void { 44 try { 45 expect(exp).assertFalse(); 46 } catch (err) { 47 console.info(`${info} test failed`); 48 } 49} 50 51function expectEqual(exp: string | number | boolean, assert: string | number | boolean, info: string = ''): void { 52 try { 53 expect(exp).assertEqual(assert); 54 } catch (err) { 55 console.info(`${info} test failed`); 56 } 57} 58 59 60export default function TCPSocketConnectionTest() { 61 describe('ActsTCPSocketConnectionTest', () => { 62 63 /* * 64 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_0100 65 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend0100 66 * @tc.desc : Sending data through TCP Socket Connection connection,Sending data as a string;callback 67 * @tc.size : MediumTest 68 * @tc.type : Function 69 * @tc.level : level 0 70 */ 71 it('testNetworkMgrSocketTCPSocketConnectionSend0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async (done: Function) => { 72 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend0100'; 73 try { 74 console.info(`${caseName} test start`); 75 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 76 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 77 let listenAddress: socket.NetAddress = { 78 address: '127.0.0.1', 79 port: 14000, 80 family: 1 81 }; 82 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 83 console.info('fail to listen' + err.code.toString()); 84 }); 85 let tcpConnectOptions: socket.TCPConnectOptions = { 86 address: listenAddress 87 }; 88 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 89 let tcpSendOption: socket.TCPSendOptions = { 90 data: 'Hello, client!' 91 }; 92 client.send(tcpSendOption, (err: BusinessError) => { 93 if (err) { 94 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 95 expectFail(); 96 done(); 97 } else { 98 console.info(`${caseName} success`); 99 expectSuccess(); 100 done(); 101 }; 102 tcpServer.off('connect'); 103 console.info(`${caseName} test end`); 104 done(); 105 }); 106 }); 107 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 108 console.info('fail to connect' + err.code.toString()); 109 }); 110 } catch (err) { 111 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 112 expectFail(); 113 console.info(`${caseName} test end`); 114 done(); 115 } 116 }); 117 118 /* * 119 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_0200 120 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend0200 121 * @tc.desc : Sending data through TCP Socket Connection connection,input parameter type is arrayBuffer;callback 122 * @tc.size : MediumTest 123 * @tc.type : Function 124 * @tc.level : level 2 125 */ 126 it('testNetworkMgrSocketTCPSocketConnectionSend0200', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 127 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend0200'; 128 try { 129 console.info(`${caseName} test start`); 130 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 131 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 132 let listenAddress: socket.NetAddress = { 133 address: '127.0.0.1', 134 port: 14001, 135 family: 1 136 }; 137 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 138 console.info('fail to listen' + err.code.toString()); 139 }); 140 let tcpConnectOptions: socket.TCPConnectOptions = { 141 address: listenAddress 142 }; 143 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 144 let tcpSendOption: socket.TCPSendOptions = { 145 data: new ArrayBuffer(234) 146 }; 147 client.send(tcpSendOption, (err: BusinessError) => { 148 if (err) { 149 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 150 expectFail(); 151 done(); 152 } else { 153 console.info(`${caseName} success`); 154 expectSuccess(); 155 done(); 156 }; 157 tcpServer.off('connect'); 158 console.info(`${caseName} test end`); 159 done(); 160 }); 161 }); 162 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 163 console.info('fail to connect' + err.code.toString()); 164 }); 165 } catch (err) { 166 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 167 expectFail(); 168 console.info(`${caseName} test end`); 169 done(); 170 } 171 }); 172 173 /* * 174 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_0300 175 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend0300 176 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is an empty string; callback 177 * @tc.size : MediumTest 178 * @tc.type : Function 179 * @tc.level : level 0 180 */ 181 it('testNetworkMgrSocketTCPSocketConnectionSend0300', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async (done: Function) => { 182 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend0300'; 183 try { 184 console.info(`${caseName} test start`); 185 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 186 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 187 let listenAddress: socket.NetAddress = { 188 address: '127.0.0.1', 189 port: 14002, 190 family: 1 191 }; 192 let tcpConnectOptions: socket.TCPConnectOptions = { 193 address: listenAddress 194 }; 195 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 196 console.info('fail to listen' + err.code.toString()); 197 }); 198 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 199 let tcpSendOption: socket.TCPSendOptions = { 200 data: 'Hello, client!', 201 encoding: '' 202 }; 203 client.send(tcpSendOption, (err: BusinessError) => { 204 if (err) { 205 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 206 expectFail(); 207 done(); 208 } else { 209 console.info(`${caseName} success`); 210 expectSuccess(); 211 done(); 212 }; 213 tcpServer.off('connect'); 214 console.info(`${caseName} test end`); 215 done(); 216 }); 217 }); 218 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 219 console.info('fail to connect' + err.code.toString()); 220 }); 221 } catch (err) { 222 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 223 expectFail(); 224 console.info(`${caseName} test end`); 225 done(); 226 } 227 }); 228 229 /* * 230 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_0400 231 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend0400 232 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is UTF-8; callback 233 * @tc.size : MediumTest 234 * @tc.type : Function 235 * @tc.level : level 2 236 */ 237 it('testNetworkMgrSocketTCPSocketConnectionSend0400', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 238 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend0400'; 239 try { 240 console.info(`${caseName} test start`); 241 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 242 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 243 let listenAddress: socket.NetAddress = { 244 address: '127.0.0.1', 245 port: 14003, 246 family: 1 247 }; 248 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 249 console.info('fail to listen' + err.code.toString()); 250 }); 251 let tcpConnectOptions: socket.TCPConnectOptions = { 252 address: listenAddress 253 }; 254 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 255 let tcpSendOption: socket.TCPSendOptions = { 256 data: 'Hello, client!', 257 encoding: 'UTF-8' 258 }; 259 client.send(tcpSendOption, (err: BusinessError) => { 260 if (err) { 261 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 262 expectFail(); 263 done(); 264 } else { 265 console.info(`${caseName} success`); 266 expectSuccess(); 267 done(); 268 }; 269 tcpServer.off('connect'); 270 console.info(`${caseName} test end`); 271 done(); 272 }); 273 }); 274 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 275 console.info('fail to connect' + err.code.toString()); 276 }); 277 } catch (err) { 278 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 279 expectFail(); 280 console.info(`${caseName} test end`); 281 done(); 282 } 283 }); 284 285 /* * 286 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_0500 287 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend0500 288 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is UTF-16BE; callback 289 * @tc.size : MediumTest 290 * @tc.type : Function 291 * @tc.level : level 2 292 */ 293 it('testNetworkMgrSocketTCPSocketConnectionSend0500', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 294 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend0500'; 295 try { 296 console.info(`${caseName} test start`); 297 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 298 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 299 let listenAddress: socket.NetAddress = { 300 address: '127.0.0.1', 301 port: 14004, 302 family: 1 303 }; 304 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 305 console.info('fail to listen' + err.code.toString()); 306 }); 307 let tcpConnectOptions: socket.TCPConnectOptions = { 308 address: listenAddress 309 }; 310 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 311 let tcpSendOption: socket.TCPSendOptions = { 312 data: 'Hello, client!', 313 encoding: 'UTF-16BE' 314 }; 315 client.send(tcpSendOption, (err: BusinessError) => { 316 if (err) { 317 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 318 expectFail(); 319 done(); 320 } else { 321 console.info(`${caseName} success`); 322 expectSuccess(); 323 done(); 324 }; 325 tcpServer.off('connect'); 326 console.info(`${caseName} test end`); 327 done(); 328 }); 329 }); 330 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 331 console.info('fail to connect' + err.code.toString()); 332 }); 333 } catch (err) { 334 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 335 expectFail(); 336 console.info(`${caseName} test end`); 337 done(); 338 } 339 }); 340 341 /* * 342 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_0600 343 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend0600 344 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is UTF-16LE; callback 345 * @tc.size : MediumTest 346 * @tc.type : Function 347 * @tc.level : level 2 348 */ 349 it('testNetworkMgrSocketTCPSocketConnectionSend0600', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 350 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend0600'; 351 try { 352 console.info(`${caseName} test start`); 353 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 354 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 355 let listenAddress: socket.NetAddress = { 356 address: '127.0.0.1', 357 port: 14005, 358 family: 1 359 }; 360 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 361 console.info('fail to listen' + err.code.toString()); 362 }); 363 let tcpConnectOptions: socket.TCPConnectOptions = { 364 address: listenAddress 365 }; 366 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 367 let tcpSendOption: socket.TCPSendOptions = { 368 data: 'Hello, client!', 369 encoding: 'UTF-16LE' 370 }; 371 client.send(tcpSendOption, (err: BusinessError) => { 372 if (err) { 373 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 374 expectFail(); 375 done(); 376 } else { 377 console.info(`${caseName} success`); 378 expectSuccess(); 379 done(); 380 }; 381 tcpServer.off('connect'); 382 console.info(`${caseName} test end`); 383 done(); 384 }); 385 }); 386 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 387 console.info('fail to connect' + err.code.toString()); 388 }); 389 } catch (err) { 390 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 391 expectFail(); 392 console.info(`${caseName} test end`); 393 done(); 394 } 395 }); 396 397 /* * 398 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_0700 399 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend0700 400 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is UTF-16; callback 401 * @tc.size : MediumTest 402 * @tc.type : Function 403 * @tc.level : level 0 404 */ 405 it('testNetworkMgrSocketTCPSocketConnectionSend0700', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async (done: Function) => { 406 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend0700'; 407 try { 408 console.info(`${caseName} test start`); 409 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 410 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 411 let listenAddress: socket.NetAddress = { 412 address: '127.0.0.1', 413 port: 14006, 414 family: 1 415 }; 416 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 417 console.info('fail to listen' + err.code.toString()); 418 }); 419 let tcpConnectOptions: socket.TCPConnectOptions = { 420 address: listenAddress 421 }; 422 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 423 let tcpSendOption: socket.TCPSendOptions = { 424 data: 'Hello, client!', 425 encoding: 'UTF-16' 426 }; 427 client.send(tcpSendOption, (err: BusinessError) => { 428 if (err) { 429 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 430 expectFail(); 431 done(); 432 } else { 433 console.info(`${caseName} success`); 434 expectSuccess(); 435 done(); 436 }; 437 tcpServer.off('connect'); 438 console.info(`${caseName} test end`); 439 done(); 440 }); 441 }); 442 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 443 console.info('fail to connect' + err.code.toString()); 444 }); 445 } catch (err) { 446 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 447 expectFail(); 448 console.info(`${caseName} test end`); 449 done(); 450 } 451 }); 452 453 /* * 454 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_0800 455 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend0800 456 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is US-AECII; callback 457 * @tc.size : MediumTest 458 * @tc.type : Function 459 * @tc.level : level 2 460 */ 461 it('testNetworkMgrSocketTCPSocketConnectionSend0800', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 462 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend0800'; 463 try { 464 console.info(`${caseName} test start`); 465 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 466 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 467 let listenAddress: socket.NetAddress = { 468 address: '127.0.0.1', 469 port: 14007, 470 family: 1 471 }; 472 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 473 console.info('fail to listen' + err.code.toString()); 474 }); 475 let tcpConnectOptions: socket.TCPConnectOptions = { 476 address: listenAddress 477 }; 478 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 479 let tcpSendOption: socket.TCPSendOptions = { 480 data: 'Hello, client!', 481 encoding: 'US-AECII' 482 }; 483 client.send(tcpSendOption, (err: BusinessError) => { 484 if (err) { 485 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 486 expectFail(); 487 done(); 488 } else { 489 console.info(`${caseName} success`); 490 expectSuccess(); 491 done(); 492 }; 493 tcpServer.off('connect'); 494 console.info(`${caseName} test end`); 495 done(); 496 }); 497 }); 498 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 499 console.info('fail to connect' + err.code.toString()); 500 }); 501 } catch (err) { 502 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 503 expectFail(); 504 console.info(`${caseName} test end`); 505 done(); 506 } 507 }); 508 509 /* * 510 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_0900 511 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend0900 512 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is ISO-8859-1; callback 513 * @tc.size : MediumTest 514 * @tc.type : Function 515 * @tc.level : level 0 516 */ 517 it('testNetworkMgrSocketTCPSocketConnectionSend0900', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async (done: Function) => { 518 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend0900'; 519 try { 520 console.info(`${caseName} test start`); 521 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 522 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 523 let listenAddress: socket.NetAddress = { 524 address: '127.0.0.1', 525 port: 14008, 526 family: 1 527 }; 528 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 529 console.info('fail to listen' + err.code.toString()); 530 }); 531 let tcpConnectOptions: socket.TCPConnectOptions = { 532 address: listenAddress 533 }; 534 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 535 let tcpSendOption: socket.TCPSendOptions = { 536 data: 'Hello, client!', 537 encoding: 'ISO-8859-1' 538 }; 539 client.send(tcpSendOption, (err: BusinessError) => { 540 if (err) { 541 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 542 expectFail(); 543 done(); 544 } else { 545 console.info(`${caseName} success`); 546 expectSuccess(); 547 done(); 548 }; 549 tcpServer.off('connect'); 550 console.info(`${caseName} test end`); 551 done(); 552 }); 553 }); 554 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 555 console.info('fail to connect' + err.code.toString()); 556 }); 557 } catch (err) { 558 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 559 expectFail(); 560 console.info(`${caseName} test end`); 561 done(); 562 } 563 }); 564 565 /* * 566 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1000 567 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1000 568 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is GB2312; callback 569 * @tc.size : MediumTest 570 * @tc.type : Function 571 * @tc.level : level 2 572 */ 573 it('testNetworkMgrSocketTCPSocketConnectionSend1000', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 574 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1000'; 575 try { 576 console.info(`${caseName} test start`); 577 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 578 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 579 let listenAddress: socket.NetAddress = { 580 address: '127.0.0.1', 581 port: 14009, 582 family: 1 583 }; 584 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 585 console.info('fail to listen' + err.code.toString()); 586 }); 587 let tcpConnectOptions: socket.TCPConnectOptions = { 588 address: listenAddress 589 }; 590 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 591 let tcpSendOption: socket.TCPSendOptions = { 592 data: 'Hello, client!', 593 encoding: 'GB2312' 594 }; 595 client.send(tcpSendOption, (err: BusinessError) => { 596 if (err) { 597 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 598 expectFail(); 599 done(); 600 } else { 601 console.info(`${caseName} success`); 602 expectSuccess(); 603 done(); 604 }; 605 tcpServer.off('connect'); 606 console.info(`${caseName} test end`); 607 done(); 608 }); 609 }); 610 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 611 console.info('fail to connect' + err.code.toString()); 612 }); 613 } catch (err) { 614 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 615 expectFail(); 616 console.info(`${caseName} test end`); 617 done(); 618 } 619 }); 620 621 /* * 622 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1100 623 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1100 624 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is undefined; callback 625 * @tc.size : MediumTest 626 * @tc.type : Function 627 * @tc.level : level 2 628 */ 629 it('testNetworkMgrSocketTCPSocketConnectionSend1100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 630 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1100'; 631 try { 632 console.info(`${caseName} test start`); 633 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 634 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 635 let listenAddress: socket.NetAddress = { 636 address: '127.0.0.1', 637 port: 14010, 638 family: 1 639 }; 640 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 641 console.info('fail to listen' + err.code.toString()); 642 }); 643 let tcpConnectOptions: socket.TCPConnectOptions = { 644 address: listenAddress 645 }; 646 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 647 let tcpSendOption: socket.TCPSendOptions = { 648 data: 'Hello, client!', 649 encoding: undefined 650 }; 651 client.send(tcpSendOption, (err: BusinessError) => { 652 if (err) { 653 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 654 expectFail(); 655 done(); 656 } else { 657 console.info(`${caseName} success`); 658 expectSuccess(); 659 done(); 660 }; 661 tcpServer.off('connect'); 662 console.info(`${caseName} test end`); 663 done(); 664 }); 665 }); 666 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 667 console.info('fail to connect' + err.code.toString()); 668 }); 669 } catch (err) { 670 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 671 expectFail(); 672 console.info(`${caseName} test end`); 673 done(); 674 } 675 }); 676 677 /* * 678 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1200 679 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1200 680 * @tc.desc : Sending data through TCP Socket Connection connection,Sending data as a string; promise 681 * @tc.size : MediumTest 682 * @tc.type : Function 683 * @tc.level : level 2 684 */ 685 it('testNetworkMgrSocketTCPSocketConnectionSend1200', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 686 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1200'; 687 try { 688 console.info(`${caseName} test start`); 689 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 690 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 691 let listenAddress: socket.NetAddress = { 692 address: '127.0.0.1', 693 port: 14011, 694 family: 1 695 }; 696 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 697 console.info('fail to listen' + err.code.toString()); 698 }); 699 let tcpConnectOptions: socket.TCPConnectOptions = { 700 address: listenAddress 701 }; 702 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 703 let tcpSendOption: socket.TCPSendOptions = { 704 data: 'Hello, client!' 705 }; 706 client.send(tcpSendOption).then(() => { 707 console.info(`${caseName} success`); 708 expectSuccess(); 709 done(); 710 }).catch((err: BusinessError) => { 711 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 712 expectFail(); 713 done(); 714 }).finally(() => { 715 tcpServer.off('connect'); 716 console.info(`${caseName} test end`); 717 done(); 718 }); 719 }); 720 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 721 console.info('fail to connect' + err.code.toString()); 722 }); 723 } catch (err) { 724 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 725 expectFail(); 726 console.info(`${caseName} test end`); 727 done(); 728 } 729 }); 730 731 /* * 732 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1300 733 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1300 734 * @tc.desc : Sending data through TCP Socket Connection connection,input parameter type is arrayBuffer; promise 735 * @tc.size : MediumTest 736 * @tc.type : Function 737 * @tc.level : level 2 738 */ 739 it('testNetworkMgrSocketTCPSocketConnectionSend1300', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 740 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1300'; 741 try { 742 console.info(`${caseName} test start`); 743 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 744 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 745 let listenAddress: socket.NetAddress = { 746 address: '127.0.0.1', 747 port: 14012, 748 family: 1 749 }; 750 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 751 console.info('fail to listen' + err.code.toString()); 752 }); 753 let tcpConnectOptions: socket.TCPConnectOptions = { 754 address: listenAddress 755 }; 756 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 757 let tcpSendOption: socket.TCPSendOptions = { 758 data: new ArrayBuffer(234) 759 }; 760 client.send(tcpSendOption).then(() => { 761 console.info(`${caseName} success`); 762 expectSuccess(); 763 done(); 764 }).catch((err: BusinessError) => { 765 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 766 expectFail(); 767 done(); 768 }).finally(() => { 769 tcpServer.off('connect'); 770 console.info(`${caseName} test end`); 771 done(); 772 }); 773 }); 774 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 775 console.info('fail to connect' + err.code.toString()); 776 }); 777 } catch (err) { 778 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 779 expectFail(); 780 console.info(`${caseName} test end`); 781 done(); 782 } 783 }); 784 785 /* * 786 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1400 787 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1400 788 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is an empty string; promise 789 * @tc.size : MediumTest 790 * @tc.type : Function 791 * @tc.level : level 2 792 */ 793 it('testNetworkMgrSocketTCPSocketConnectionSend1400', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 794 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1400'; 795 try { 796 console.info(`${caseName} test start`); 797 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 798 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 799 let listenAddress: socket.NetAddress = { 800 address: '127.0.0.1', 801 port: 14013, 802 family: 1 803 }; 804 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 805 console.info('fail to listen' + err.code.toString()); 806 }); 807 let tcpConnectOptions: socket.TCPConnectOptions = { 808 address: listenAddress 809 }; 810 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 811 let tcpSendOption: socket.TCPSendOptions = { 812 data: 'Hello, client!', 813 encoding: '' 814 }; 815 client.send(tcpSendOption).then(() => { 816 console.info(`${caseName} success`); 817 expectSuccess(); 818 done(); 819 }).catch((err: BusinessError) => { 820 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 821 expectFail(); 822 done(); 823 }).finally(() => { 824 tcpServer.off('connect'); 825 console.info(`${caseName} test end`); 826 done(); 827 }); 828 }); 829 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 830 console.info('fail to connect' + err.code.toString()); 831 }); 832 } catch (err) { 833 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 834 expectFail(); 835 console.info(`${caseName} test end`); 836 done(); 837 } 838 }); 839 840 /* * 841 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1500 842 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1500 843 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is UTF-8; promise 844 * @tc.size : MediumTest 845 * @tc.type : Function 846 * @tc.level : level 2 847 */ 848 it('testNetworkMgrSocketTCPSocketConnectionSend1500', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 849 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1500'; 850 try { 851 console.info(`${caseName} test start`); 852 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 853 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 854 let listenAddress: socket.NetAddress = { 855 address: '127.0.0.1', 856 port: 14014, 857 family: 1 858 }; 859 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 860 console.info('fail to listen' + err.code.toString()); 861 }); 862 let tcpConnectOptions: socket.TCPConnectOptions = { 863 address: listenAddress 864 }; 865 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 866 let tcpSendOption: socket.TCPSendOptions = { 867 data: 'Hello, client!', 868 encoding: 'UTF-8' 869 }; 870 client.send(tcpSendOption).then(() => { 871 console.info(`${caseName} success`); 872 expectSuccess(); 873 done(); 874 }).catch((err: BusinessError) => { 875 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 876 expectFail(); 877 done(); 878 }).finally(() => { 879 tcpServer.off('connect'); 880 console.info(`${caseName} test end`); 881 done(); 882 }); 883 }); 884 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 885 console.info('fail to connect' + err.code.toString()); 886 }); 887 } catch (err) { 888 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 889 expectFail(); 890 console.info(`${caseName} test end`); 891 done(); 892 } 893 }); 894 895 /* * 896 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1600 897 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1600 898 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is UTF-16BE; promise 899 * @tc.size : MediumTest 900 * @tc.type : Function 901 * @tc.level : level 2 902 */ 903 it('testNetworkMgrSocketTCPSocketConnectionSend1600', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 904 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1600'; 905 try { 906 console.info(`${caseName} test start`); 907 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 908 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 909 let listenAddress: socket.NetAddress = { 910 address: '127.0.0.1', 911 port: 14015, 912 family: 1 913 }; 914 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 915 console.info('fail to listen' + err.code.toString()); 916 }); 917 let tcpConnectOptions: socket.TCPConnectOptions = { 918 address: listenAddress 919 }; 920 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 921 let tcpSendOption: socket.TCPSendOptions = { 922 data: 'Hello, client!', 923 encoding: 'UTF-16BE' 924 }; 925 client.send(tcpSendOption).then(() => { 926 console.info(`${caseName} success`); 927 expectSuccess(); 928 done(); 929 }).catch((err: BusinessError) => { 930 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 931 expectFail(); 932 done(); 933 }).finally(() => { 934 tcpServer.off('connect'); 935 console.info(`${caseName} test end`); 936 done(); 937 }); 938 }); 939 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 940 console.info('fail to connect' + err.code.toString()); 941 }); 942 } catch (err) { 943 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 944 expectFail(); 945 console.info(`${caseName} test end`); 946 done(); 947 } 948 }); 949 950 /* * 951 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1700 952 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1700 953 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is UTF-16LE; promise 954 * @tc.size : MediumTest 955 * @tc.type : Function 956 * @tc.level : level 2 957 */ 958 it('testNetworkMgrSocketTCPSocketConnectionSend1700', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 959 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1700'; 960 try { 961 console.info(`${caseName} test start`); 962 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 963 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 964 let listenAddress: socket.NetAddress = { 965 address: '127.0.0.1', 966 port: 14016, 967 family: 1 968 }; 969 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 970 console.info('fail to listen' + err.code.toString()); 971 }); 972 let tcpConnectOptions: socket.TCPConnectOptions = { 973 address: listenAddress 974 }; 975 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 976 let tcpSendOption: socket.TCPSendOptions = { 977 data: 'Hello, client!', 978 encoding: 'UTF-16LE' 979 }; 980 client.send(tcpSendOption).then(() => { 981 console.info(`${caseName} success`); 982 expectSuccess(); 983 done(); 984 }).catch((err: BusinessError) => { 985 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 986 expectFail(); 987 done(); 988 }).finally(() => { 989 tcpServer.off('connect'); 990 console.info(`${caseName} test end`); 991 done(); 992 }); 993 }); 994 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 995 console.info('fail to connect' + err.code.toString()); 996 }); 997 } catch (err) { 998 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 999 expectFail(); 1000 console.info(`${caseName} test end`); 1001 done(); 1002 } 1003 }); 1004 1005 /* * 1006 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1800 1007 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1800 1008 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is UTF-16; promise 1009 * @tc.size : MediumTest 1010 * @tc.type : Function 1011 * @tc.level : level 2 1012 */ 1013 it('testNetworkMgrSocketTCPSocketConnectionSend1800', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1014 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1800'; 1015 try { 1016 console.info(`${caseName} test start`); 1017 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1018 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1019 let listenAddress: socket.NetAddress = { 1020 address: '127.0.0.1', 1021 port: 14017, 1022 family: 1 1023 }; 1024 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1025 console.info('fail to listen' + err.code.toString()); 1026 }); 1027 let tcpConnectOptions: socket.TCPConnectOptions = { 1028 address: listenAddress 1029 }; 1030 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 1031 let tcpSendOption: socket.TCPSendOptions = { 1032 data: 'Hello, client!', 1033 encoding: 'UTF-16' 1034 }; 1035 client.send(tcpSendOption).then(() => { 1036 console.info(`${caseName} success`); 1037 expectSuccess(); 1038 done(); 1039 }).catch((err: BusinessError) => { 1040 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 1041 expectFail(); 1042 done(); 1043 }).finally(() => { 1044 tcpServer.off('connect'); 1045 console.info(`${caseName} test end`); 1046 done(); 1047 }); 1048 }); 1049 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1050 console.info('fail to connect' + err.code.toString()); 1051 }); 1052 } catch (err) { 1053 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1054 expectFail(); 1055 console.info(`${caseName} test end`); 1056 done(); 1057 } 1058 }); 1059 1060 /* * 1061 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_1900 1062 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend1900 1063 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is US-AECII;promise 1064 * @tc.size : MediumTest 1065 * @tc.type : Function 1066 * @tc.level : level 2 1067 */ 1068 it('testNetworkMgrSocketTCPSocketConnectionSend1900', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1069 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend1900'; 1070 try { 1071 console.info(`${caseName} test start`); 1072 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1073 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1074 let listenAddress: socket.NetAddress = { 1075 address: '127.0.0.1', 1076 port: 14018, 1077 family: 1 1078 }; 1079 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1080 console.info('fail to listen' + err.code.toString()); 1081 }); 1082 let tcpConnectOptions: socket.TCPConnectOptions = { 1083 address: listenAddress 1084 }; 1085 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 1086 let tcpSendOption: socket.TCPSendOptions = { 1087 data: 'Hello, client!', 1088 encoding: 'US-AECII' 1089 }; 1090 client.send(tcpSendOption).then(() => { 1091 console.info(`${caseName} success`); 1092 expectSuccess(); 1093 done(); 1094 }).catch((err: BusinessError) => { 1095 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 1096 expectFail(); 1097 done(); 1098 }).finally(() => { 1099 tcpServer.off('connect'); 1100 console.info(`${caseName} test end`); 1101 done(); 1102 }); 1103 }); 1104 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1105 console.info('fail to connect' + err.code.toString()); 1106 }); 1107 } catch (err) { 1108 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1109 expectFail(); 1110 console.info(`${caseName} test end`); 1111 done(); 1112 } 1113 }); 1114 1115 /* * 1116 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_2000 1117 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend2000 1118 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is ISO-8859-1;promise 1119 * @tc.size : MediumTest 1120 * @tc.type : Function 1121 * @tc.level : level 2 1122 */ 1123 it('testNetworkMgrSocketTCPSocketConnectionSend2000', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1124 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend2000'; 1125 try { 1126 console.info(`${caseName} test start`); 1127 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1128 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1129 let listenAddress: socket.NetAddress = { 1130 address: '127.0.0.1', 1131 port: 14019, 1132 family: 1 1133 }; 1134 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1135 console.info('fail to listen' + err.code.toString()); 1136 }); 1137 let tcpConnectOptions: socket.TCPConnectOptions = { 1138 address: listenAddress 1139 }; 1140 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 1141 let tcpSendOption: socket.TCPSendOptions = { 1142 data: 'Hello, client!', 1143 encoding: 'US-AECII' 1144 }; 1145 client.send(tcpSendOption).then(() => { 1146 console.info(`${caseName} success`); 1147 expectSuccess(); 1148 done(); 1149 }).catch((err: BusinessError) => { 1150 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 1151 expectFail(); 1152 done(); 1153 }).finally(() => { 1154 tcpServer.off('connect'); 1155 console.info(`${caseName} test end`); 1156 done(); 1157 }); 1158 }); 1159 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1160 console.info('fail to connect' + err.code.toString()); 1161 }); 1162 } catch (err) { 1163 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1164 expectFail(); 1165 console.info(`${caseName} test end`); 1166 done(); 1167 } 1168 }); 1169 1170 /* * 1171 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_2100 1172 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend2100 1173 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is GB2312; promise 1174 * @tc.size : MediumTest 1175 * @tc.type : Function 1176 * @tc.level : level 2 1177 */ 1178 it('testNetworkMgrSocketTCPSocketConnectionSend2100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1179 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend2100'; 1180 try { 1181 console.info(`${caseName} test start`); 1182 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1183 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1184 let listenAddress: socket.NetAddress = { 1185 address: '127.0.0.1', 1186 port: 14020, 1187 family: 1 1188 }; 1189 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1190 console.info('fail to listen' + err.code.toString()); 1191 }); 1192 let tcpConnectOptions: socket.TCPConnectOptions = { 1193 address: listenAddress 1194 }; 1195 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 1196 let tcpSendOption: socket.TCPSendOptions = { 1197 data: 'Hello, client!', 1198 encoding: 'GB2312' 1199 }; 1200 client.send(tcpSendOption).then(() => { 1201 console.info(`${caseName} success`); 1202 expectSuccess(); 1203 done(); 1204 }).catch((err: BusinessError) => { 1205 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 1206 expectFail(); 1207 done(); 1208 }).finally(() => { 1209 tcpServer.off('connect'); 1210 console.info(`${caseName} test end`); 1211 done(); 1212 }); 1213 }); 1214 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1215 console.info('fail to connect' + err.code.toString()); 1216 }); 1217 } catch (err) { 1218 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1219 expectFail(); 1220 console.info(`${caseName} test end`); 1221 done(); 1222 } 1223 }); 1224 1225 /* * 1226 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Send_2200 1227 * @tc.name : testNetworkMgrSocketTCPSocketConnectionSend2200 1228 * @tc.desc : Sending data through TCP Socket Connection connection,Encoding format is undefined; promise 1229 * @tc.size : MediumTest 1230 * @tc.type : Function 1231 * @tc.level : level 2 1232 */ 1233 it('testNetworkMgrSocketTCPSocketConnectionSend2200', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1234 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionSend2200'; 1235 try { 1236 console.info(`${caseName} test start`); 1237 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1238 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1239 let listenAddress: socket.NetAddress = { 1240 address: '127.0.0.1', 1241 port: 14021, 1242 family: 1 1243 }; 1244 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1245 console.info('fail to listen' + err.code.toString()); 1246 }); 1247 let tcpConnectOptions: socket.TCPConnectOptions = { 1248 address: listenAddress 1249 }; 1250 tcpServer.on('connect', (client: socket.TCPSocketConnection) => { 1251 let tcpSendOption: socket.TCPSendOptions = { 1252 data: 'Hello, client!', 1253 encoding: undefined 1254 }; 1255 client.send(tcpSendOption).then(() => { 1256 console.info(`${caseName} success`); 1257 expectSuccess(); 1258 done(); 1259 }).catch((err: BusinessError) => { 1260 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 1261 expectFail(); 1262 done(); 1263 }).finally(() => { 1264 tcpServer.off('connect'); 1265 console.info(`${caseName} test end`); 1266 done(); 1267 }); 1268 }); 1269 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1270 console.info('fail to connect' + err.code.toString()); 1271 }); 1272 } catch (err) { 1273 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1274 expectFail(); 1275 console.info(`${caseName} test end`); 1276 done(); 1277 } 1278 }); 1279 1280 /* * 1281 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Close_0100 1282 * @tc.name : testNetworkMgrSocketTCPSocketConnectionClose0100 1283 * @tc.desc : Close a connection established with TCP Socket; callback 1284 * @tc.size : MediumTest 1285 * @tc.type : Function 1286 * @tc.level : level 2 1287 */ 1288 it('testNetworkMgrSocketTCPSocketConnectionClose0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1289 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionClose0100'; 1290 try { 1291 console.info(`${caseName} test start`); 1292 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1293 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1294 let listenAddress: socket.NetAddress = { 1295 address: '127.0.0.1', 1296 port: 14022, 1297 family: 1 1298 }; 1299 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1300 console.info('fail to listen' + err.code.toString()); 1301 }); 1302 let tcpConnectOptions: socket.TCPConnectOptions = { 1303 address: listenAddress 1304 }; 1305 tcpServer.on('connect', async (client: socket.TCPSocketConnection) => { 1306 client.close((err: BusinessError) => { 1307 if (err) { 1308 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 1309 expectFail(); 1310 done(); 1311 } else { 1312 console.info(`${caseName} success`); 1313 expectSuccess(); 1314 done(); 1315 }; 1316 tcpServer.off('connect'); 1317 console.info(`${caseName} test end`); 1318 done(); 1319 }); 1320 }); 1321 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1322 console.info('fail to connect' + err.code.toString()); 1323 }); 1324 } catch (err) { 1325 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1326 expectFail(); 1327 console.info(`${caseName} test end`); 1328 done(); 1329 } 1330 }); 1331 1332 /* * 1333 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_Close_0200 1334 * @tc.name : testNetworkMgrSocketTCPSocketConnectionClose0200 1335 * @tc.desc : Close a connection established with TCP Socket; promise 1336 * @tc.size : MediumTest 1337 * @tc.type : Function 1338 * @tc.level : level 2 1339 */ 1340 it('testNetworkMgrSocketTCPSocketConnectionClose0200', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1341 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionClose0200'; 1342 try { 1343 console.info(`${caseName} test start`); 1344 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1345 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1346 let listenAddress: socket.NetAddress = { 1347 address: '127.0.0.1', 1348 port: 14023, 1349 family: 1 1350 }; 1351 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1352 console.info('fail to listen' + err.code.toString()); 1353 }); 1354 let tcpConnectOptions: socket.TCPConnectOptions = { 1355 address: listenAddress 1356 }; 1357 tcpServer.on('connect', async (client: socket.TCPSocketConnection) => { 1358 client.close().then(() => { 1359 console.info(`${caseName} success`); 1360 expectSuccess(); 1361 done(); 1362 }).catch((err: BusinessError) => { 1363 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 1364 expectFail(); 1365 done(); 1366 }).finally(() => { 1367 tcpServer.off('connect'); 1368 console.info(`${caseName} test end`); 1369 done(); 1370 }); 1371 }); 1372 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1373 console.info('fail to connect' + err.code.toString()); 1374 }); 1375 } catch (err) { 1376 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1377 expectFail(); 1378 console.info(`${caseName} test end`); 1379 done(); 1380 } 1381 }); 1382 1383 /* * 1384 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_GetRemoteAddress_0100 1385 * @tc.name : testNetworkMgrSocketTCPSocketConnectionGetRemoteAddress0100 1386 * @tc.desc : Get the Opposite Socket Address; callback 1387 * @tc.size : MediumTest 1388 * @tc.type : Function 1389 * @tc.level : level 2 1390 */ 1391 it('testNetworkMgrSocketTCPSocketConnectionGetRemoteAddress0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1392 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionGetRemoteAddress0100'; 1393 try { 1394 console.info(`${caseName} test start`); 1395 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1396 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1397 let listenAddress: socket.NetAddress = { 1398 address: '127.0.0.1', 1399 port: 14024, 1400 family: 1 1401 }; 1402 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1403 console.info('fail to listen' + err.code.toString()); 1404 }); 1405 let tcpConnectOptions: socket.TCPConnectOptions = { 1406 address: listenAddress 1407 }; 1408 tcpServer.on('connect', async (client: socket.TCPSocketConnection) => { 1409 client.getRemoteAddress((err: BusinessError, data: socket.NetAddress) => { 1410 if (err) { 1411 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 1412 expectFail(); 1413 done(); 1414 } else { 1415 console.info(`${caseName} success data:${JSON.stringify(data)}`); 1416 expectSuccess(); 1417 done(); 1418 }; 1419 tcpServer.off('connect'); 1420 console.info(`${caseName} test end`); 1421 done(); 1422 }); 1423 }); 1424 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1425 console.info('fail to connect' + err.code.toString()); 1426 }); 1427 } catch (err) { 1428 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1429 expectFail(); 1430 console.info(`${caseName} test end`); 1431 done(); 1432 } 1433 }); 1434 1435 /* * 1436 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_GetRemoteAddress_0200 1437 * @tc.name : testNetworkMgrSocketTCPSocketConnectionGetRemoteAddress0200 1438 * @tc.desc : Get the Opposite Socket Address; promise 1439 * @tc.size : MediumTest 1440 * @tc.type : Function 1441 * @tc.level : level 2 1442 */ 1443 it('testNetworkMgrSocketTCPSocketConnectionGetRemoteAddress0200', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1444 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionGetRemoteAddress0200'; 1445 try { 1446 console.info(`${caseName} test start`); 1447 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1448 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1449 let listenAddress: socket.NetAddress = { 1450 address: '127.0.0.1', 1451 port: 14025, 1452 family: 1 1453 }; 1454 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1455 console.info('fail to listen' + err.code.toString()); 1456 }); 1457 let tcpConnectOptions: socket.TCPConnectOptions = { 1458 address: listenAddress 1459 }; 1460 tcpServer.on('connect', async (client: socket.TCPSocketConnection) => { 1461 client.getRemoteAddress().then((data: socket.NetAddress) => { 1462 console.info(`${caseName} success data:${JSON.stringify(data)}`); 1463 expectSuccess(); 1464 done(); 1465 }).catch((err: BusinessError) => { 1466 console.info(`${caseName} fail err:${JSON.stringify(err)}`); 1467 expectFail(); 1468 done(); 1469 }).finally(() => { 1470 tcpServer.off('connect'); 1471 console.info(`${caseName} test end`); 1472 done(); 1473 }); 1474 }); 1475 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1476 console.info('fail to connect' + err.code.toString()); 1477 }); 1478 } catch (err) { 1479 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1480 expectFail(); 1481 console.info(`${caseName} test end`); 1482 done(); 1483 } 1484 }); 1485 1486 /* * 1487 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_OffMessage_0100 1488 * @tc.name : testNetworkMgrSocketTCPSocketConnectionOffMessage0100 1489 * @tc.desc : UnSubscription to receive message events for TCPSocketConnection connections 1490 * @tc.size : MediumTest 1491 * @tc.type : Function 1492 * @tc.level : level 2 1493 */ 1494 it('testNetworkMgrSocketTCPSocketConnectionOffMessage0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1495 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionOffMessage0100'; 1496 try { 1497 console.info(`${caseName} test start`); 1498 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1499 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1500 let listenAddress: socket.NetAddress = { 1501 address: '127.0.0.1', 1502 port: 14026, 1503 family: 1 1504 }; 1505 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1506 console.info('fail to listen' + err.code.toString()); 1507 }); 1508 let tcpConnectOptions: socket.TCPConnectOptions = { 1509 address: listenAddress 1510 }; 1511 tcpServer.on('connect', async (client: socket.TCPSocketConnection) => { 1512 let clientSendOptions: socket.TCPSendOptions = { 1513 data: 'Hello, server!' 1514 }; 1515 const callback: Callback<socket.SocketMessageInfo> = () => { 1516 expectFail(); 1517 console.info(`${caseName} test end`); 1518 done(); 1519 }; 1520 client.on('message', callback); 1521 client.off('message', callback); 1522 await tcp.send(clientSendOptions).catch((err:BusinessError) => { 1523 console.info('fail to send' + err.code.toString()); 1524 }); 1525 await client.close().catch((err:BusinessError) => { 1526 console.info('fail to close' + err.code.toString()); 1527 }); 1528 expectSuccess(); 1529 done(); 1530 }); 1531 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1532 console.info('fail to connect' + err.code.toString()); 1533 }); 1534 } catch (err) { 1535 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1536 expectFail(); 1537 console.info(`${caseName} test end`); 1538 done(); 1539 } 1540 }); 1541 1542 /* * 1543 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_OffMessage_0200 1544 * @tc.name : testNetworkMgrSocketTCPSocketConnectionOffMessage0200 1545 * @tc.desc : Cancel multiple subscriptions to receive message events for TCP Socket Connection connections 1546 * @tc.size : MediumTest 1547 * @tc.type : Function 1548 * @tc.level : level 2 1549 */ 1550 it('testNetworkMgrSocketTCPSocketConnectionOffMessage0200', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1551 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionOffMessage0200'; 1552 try { 1553 console.info(`${caseName} test start`); 1554 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1555 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1556 let listenAddress: socket.NetAddress = { 1557 address: '127.0.0.1', 1558 port: 14027, 1559 family: 1 1560 }; 1561 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1562 console.info('fail to listen' + err.code.toString()); 1563 }); 1564 let tcpConnectOptions: socket.TCPConnectOptions = { 1565 address: listenAddress 1566 }; 1567 tcpServer.on('connect', async (client: socket.TCPSocketConnection) => { 1568 let clientSendOptions: socket.TCPSendOptions = { 1569 data: 'Hello, server!' 1570 }; 1571 const callback1: Callback<socket.SocketMessageInfo> = () => { 1572 expectFail(); 1573 console.info(`${caseName} test end`); 1574 done(); 1575 }; 1576 const callback2: Callback<socket.SocketMessageInfo> = () => { 1577 expectFail(); 1578 console.info(`${caseName} test end`); 1579 done(); 1580 }; 1581 client.on('message', callback1); 1582 client.on('message', callback2); 1583 client.off('message'); 1584 await tcp.send(clientSendOptions).catch((err:BusinessError) => { 1585 console.info('fail to send' + err.code.toString()); 1586 }); 1587 await client.close().catch((err:BusinessError) => { 1588 console.info('fail to close' + err.code.toString()); 1589 }); 1590 expectSuccess(); 1591 done(); 1592 }); 1593 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1594 console.info('fail to connect' + err.code.toString()); 1595 }); 1596 } catch (err) { 1597 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1598 expectFail(); 1599 console.info(`${caseName} test end`); 1600 done(); 1601 } 1602 }); 1603 1604 /* * 1605 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_OnClose_0100 1606 * @tc.name : testNetworkMgrSocketTCPSocketConnectionOnClose0100 1607 * @tc.desc : Subscription to the closure event of TCPSocketConnection 1608 * @tc.size : MediumTest 1609 * @tc.type : Function 1610 * @tc.level : level 2 1611 */ 1612 it('testNetworkMgrSocketTCPSocketConnectionOnClose0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1613 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionOnClose0100'; 1614 try { 1615 console.info(`${caseName} test start`); 1616 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1617 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1618 let listenAddress: socket.NetAddress = { 1619 address: '127.0.0.1', 1620 port: 14028, 1621 family: 1 1622 }; 1623 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1624 console.info('fail to listen' + err.code.toString()); 1625 }); 1626 let tcpConnectOptions: socket.TCPConnectOptions = { 1627 address: listenAddress 1628 }; 1629 tcpServer.on('connect', async (client: socket.TCPSocketConnection) => { 1630 const callback: Callback<void> = () => { 1631 expectSuccess(); 1632 tcpServer.off('connect'); 1633 console.info(`${caseName} test end`); 1634 done(); 1635 }; 1636 client.on('close', callback); 1637 await client.close().catch((err:BusinessError) => { 1638 console.info('fail to close' + err.code.toString()); 1639 }); 1640 console.info(`${caseName} test end`); 1641 done(); 1642 }); 1643 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1644 console.info('fail to connect' + err.code.toString()); 1645 }); 1646 } catch (err) { 1647 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1648 expectFail(); 1649 console.info(`${caseName} test end`); 1650 done(); 1651 } 1652 }); 1653 1654 /* * 1655 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_OffClose_0100 1656 * @tc.name : testNetworkMgrSocketTCPSocketConnectionOffClose0100 1657 * @tc.desc : UnSubscription to the closure event of TCPSocketConnection 1658 * @tc.size : MediumTest 1659 * @tc.type : Function 1660 * @tc.level : level 2 1661 */ 1662 it('testNetworkMgrSocketTCPSocketConnectionOffClose0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1663 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionOffClose0100'; 1664 try { 1665 console.info(`${caseName} test start`); 1666 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1667 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1668 let listenAddress: socket.NetAddress = { 1669 address: '127.0.0.1', 1670 port: 14029, 1671 family: 1 1672 }; 1673 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1674 console.info('fail to listen' + err.code.toString()); 1675 }); 1676 let tcpConnectOptions: socket.TCPConnectOptions = { 1677 address: listenAddress 1678 }; 1679 tcpServer.on('connect', async (client: socket.TCPSocketConnection) => { 1680 const callback: Callback<void> = () => { 1681 expectFail(); 1682 tcpServer.off('connect'); 1683 console.info(`${caseName} test end`); 1684 done(); 1685 }; 1686 client.on('close', callback); 1687 client.off('close', callback); 1688 await client.close().catch((err:BusinessError) => { 1689 console.info('fail to close' + err.code.toString()); 1690 }); 1691 expectSuccess(); 1692 console.info(`${caseName} test end`); 1693 done(); 1694 }); 1695 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1696 console.info('fail to connect' + err.code.toString()); 1697 }); 1698 } catch (err) { 1699 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1700 expectFail(); 1701 console.info(`${caseName} test end`); 1702 done(); 1703 } 1704 }); 1705 1706 /* * 1707 * @tc.number: SUB_NetworkMgr_Socket_TCPSocketConnection_OffClose_0200 1708 * @tc.name : testNetworkMgrSocketTCPSocketConnectionOffClose0200 1709 * @tc.desc : Cancel multiple subscriptions to the TCP Socket Connection shutdown event 1710 * @tc.size : MediumTest 1711 * @tc.type : Function 1712 * @tc.level : level 2 1713 */ 1714 it('testNetworkMgrSocketTCPSocketConnectionOffClose0200', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async (done: Function) => { 1715 let caseName: string = 'testNetworkMgrSocketTCPSocketConnectionOffClose0200'; 1716 try { 1717 console.info(`${caseName} test start`); 1718 let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 1719 let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 1720 let listenAddress: socket.NetAddress = { 1721 address: '127.0.0.1', 1722 port: 14030, 1723 family: 1 1724 }; 1725 await tcpServer.listen(listenAddress).catch((err:BusinessError) => { 1726 console.info('fail to listen' + err.code.toString()); 1727 }); 1728 let tcpConnectOptions: socket.TCPConnectOptions = { 1729 address: listenAddress 1730 }; 1731 tcpServer.on('connect', async (client: socket.TCPSocketConnection) => { 1732 const callback1: Callback<void> = () => { 1733 expectFail(); 1734 tcpServer.off('connect'); 1735 console.info(`${caseName} test end`); 1736 done(); 1737 }; 1738 const callback2: Callback<void> = () => { 1739 expectFail(); 1740 tcpServer.off('connect'); 1741 console.info(`${caseName} test end`); 1742 done(); 1743 }; 1744 client.on('close', callback1); 1745 client.on('close', callback2); 1746 client.off('close'); 1747 await client.close().catch((err:BusinessError) => { 1748 console.info('fail to close' + err.code.toString()); 1749 }); 1750 expectSuccess(); 1751 console.info(`${caseName} test end`); 1752 done(); 1753 }); 1754 await tcp.connect(tcpConnectOptions).catch((err:BusinessError) => { 1755 console.info('fail to connect' + err.code.toString()); 1756 }); 1757 } catch (err) { 1758 console.info(`${caseName}_catch fail err:${JSON.stringify(err)}`); 1759 expectFail(); 1760 console.info(`${caseName} test end`); 1761 done(); 1762 } 1763 }); 1764 }) 1765}