-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathomrsockTest.cpp
1326 lines (1132 loc) · 55.1 KB
/
omrsockTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright IBM Corp. and others 2019
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "omrcfg.h"
#include "omrport.h"
#include "omrporterror.h"
#include "omrportsock.h"
#include "omrportsocktypes.h"
#include "testHelpers.hpp"
/**
* Start a server which creates a socket, binds to an address/port and
* listens for clients.
*
* @param[in] portLibrary
* @param[in] addrStr The address of the server.
* @param[in] port The server port.
* @param[in] family Socket address family wanted.
* @param[out] serverSocket A pointer to the server socket.
* @param[out] serverAddr The socket address of the server created.
*
* @return on success, report an error otherwise.
*/
void
start_server(struct OMRPortLibrary *portLibrary, int32_t family, int32_t socktype, omrsock_socket_t *serverSocket, omrsock_sockaddr_t serverSockAddr)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
int32_t flag = 1;
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, serverSocket, family, socktype, OMRSOCK_IPPROTO_DEFAULT), 0);
/** Set socket address to be reusable in case this address is in TIME_WAIT state. */
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, *serverSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_REUSEADDR, &flag), 0);
ASSERT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, *serverSocket, serverSockAddr), 0);
if (OMRSOCK_STREAM == (socktype & 0x00FF)) {
ASSERT_EQ(OMRPORTLIB->sock_listen(OMRPORTLIB, *serverSocket, 10), 0);
}
return;
}
/**
* Create the client socket, and then connect to the server.
*
* @param[in] portLibrary
* @param[in] addrStr The address of the server.
* @param[in] port The server port.
* @param[in] family Socket address family wanted.
* @param[in] sessionClientSocket A pointer to the client socket.
* @param[in] sessionClientAddr The socket address of the client created.
*
* @return on success, report an error otherwise.
*/
void
connect_client_to_server(struct OMRPortLibrary *portLibrary, const char *addrStr, const char *port, int32_t family, int32_t socktype, omrsock_socket_t *clientSocket, omrsock_sockaddr_t clientSockAddr, omrsock_sockaddr_t serverSockAddr)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
omrsock_addrinfo_t hints = NULL;
OMRAddrInfoNode result;
uint32_t length = 0;
int32_t resultFamily;
int32_t resultSocktype;
int32_t resultProtocol;
EXPECT_EQ(OMRPORTLIB->sock_getaddrinfo_create_hints(OMRPORTLIB, &hints, family, socktype, OMRSOCK_IPPROTO_DEFAULT, 0), 0);
EXPECT_EQ(OMRPORTLIB->sock_getaddrinfo(OMRPORTLIB, addrStr, port, hints, &result), 0);
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_length(OMRPORTLIB, &result, &length), 0);
EXPECT_NE(length, uint32_t(0));
/* Create a socket with results. */
for (uint32_t i = 0; i < length; i++) {
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_family(OMRPORTLIB, &result, i, &resultFamily), 0);
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_socktype(OMRPORTLIB, &result, i, &resultSocktype), 0);
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_protocol(OMRPORTLIB, &result, i, &resultProtocol), 0);
if(0 == OMRPORTLIB->sock_socket(OMRPORTLIB, clientSocket, resultFamily, resultSocktype, resultProtocol)) {
ASSERT_NE(clientSocket, (void *)NULL);
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_address(OMRPORTLIB, &result, i, clientSockAddr), 0);
ASSERT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, *clientSocket, clientSockAddr), 0);
break;
}
}
EXPECT_EQ(OMRPORTLIB->sock_freeaddrinfo(OMRPORTLIB, &result), 0);
if(OMRSOCK_STREAM == resultSocktype) {
ASSERT_EQ(OMRPORTLIB->sock_connect(OMRPORTLIB, *clientSocket, serverSockAddr), 0);
}
return;
}
/**
* Test if port library is properly set up to run OMRSock tests.
*
* This will iterate through all OMRSock functions in @ref omrsock.c and simply check
* that the location of the function is not NULL.
*
* @note Errors such as an OMRSock function call with a NULL address will be reported.
*/
TEST(PortSockTest, library_function_pointers_not_null)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
EXPECT_NE(OMRPORTLIB->sock_getaddrinfo_create_hints, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_getaddrinfo, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_addrinfo_length, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_addrinfo_family, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_addrinfo_socktype, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_addrinfo_protocol, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_freeaddrinfo, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_socket, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_sockaddr_init, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_sockaddr_init6, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_socket_getfd, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_bind, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_listen, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_accept, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_send, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_sendto, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_recv, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_recvfrom, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_pollfd_init, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_get_pollfd_info, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_poll, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_fdset_zero, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_fdset_set, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_fdset_clr, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_fdset_isset, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_select, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_close, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_htons, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_htonl, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_inet_pton, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_fcntl, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_timeval_init, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_linger_init, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_setsockopt_int, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_setsockopt_linger, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_setsockopt_timeval, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_getsockopt_int, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_getsockopt_linger, (void *)NULL);
EXPECT_NE(OMRPORTLIB->sock_getsockopt_timeval, (void *)NULL);
}
/**
* Test the omrsock per thread buffer using @ref omrsock_getaddrinfo_create_hints
* and all functions that extract details from the per thread buffer.
*
* In this test, per thread buffer related functions set up a buffer to store
* information such as the hints structures created in
* @ref omrsock_getaddrinfo_create_hints. Since the per thread buffer can't be easily
* directly tested, it will be tested with @ref omrsock_getaddrinfo_create_hints.
*
* @note Errors such as invalid arguments, and/or returning the wrong family or
* socket type from hints compared to the ones passed into hints, will be reported.
*/
TEST(PortSockTest, create_hints_and_element_extraction)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
omrsock_addrinfo_t hints = NULL;
uint32_t length = 0;
int32_t family = 0;
int32_t sockType = 0;
int32_t protocol = 0;
int32_t hintsFamily = OMRSOCK_AF_UNSPEC;
int32_t hintsSockType = OMRSOCK_STREAM;
int32_t hintsProtocol = OMRSOCK_IPPROTO_TCP;
int32_t hintsFlags = 0;
OMRPORTLIB->sock_getaddrinfo_create_hints(OMRPORTLIB, &hints, hintsFamily, hintsSockType, hintsProtocol, hintsFlags);
ASSERT_NE(hints, (void *)NULL);
/* Testing invalid arguments: Pointer to OMRAddrInfoNode that is NULL */
int32_t rc;
rc = OMRPORTLIB->sock_addrinfo_length(OMRPORTLIB, NULL, &length);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
rc = OMRPORTLIB->sock_addrinfo_family(OMRPORTLIB, NULL, 0, &family);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
rc = OMRPORTLIB->sock_addrinfo_socktype(OMRPORTLIB, NULL, 0, &sockType);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
rc = OMRPORTLIB->sock_addrinfo_protocol(OMRPORTLIB, NULL, 0, &protocol);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
/* Testing invalid arguments: Index is bigger than the length when querying */
rc = OMRPORTLIB->sock_addrinfo_length(OMRPORTLIB, hints, &length);
EXPECT_EQ(rc, 0);
rc = OMRPORTLIB->sock_addrinfo_family(OMRPORTLIB, hints, length, &family);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
rc = OMRPORTLIB->sock_addrinfo_socktype(OMRPORTLIB, hints, length, &sockType);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
rc = OMRPORTLIB->sock_addrinfo_protocol(OMRPORTLIB, hints, length, &protocol);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
/* Get and verify elements of the newly created hints. */
rc = OMRPORTLIB->sock_addrinfo_length(OMRPORTLIB, hints, &length);
EXPECT_EQ(rc, 0);
EXPECT_EQ(length, uint32_t(1));
rc = OMRPORTLIB->sock_addrinfo_family(OMRPORTLIB, hints, 0, &family);
EXPECT_EQ(rc, 0);
EXPECT_EQ(family, hintsFamily);
rc = OMRPORTLIB->sock_addrinfo_socktype(OMRPORTLIB, hints, 0, &sockType);
EXPECT_EQ(rc, 0);
EXPECT_EQ(sockType, hintsSockType);
rc = OMRPORTLIB->sock_addrinfo_protocol(OMRPORTLIB, hints, 0, &protocol);
EXPECT_EQ(rc, 0);
EXPECT_EQ(protocol, hintsProtocol);
/* Recreate hints with different parameters, see if hints elements are overwriten properly. */
hintsProtocol = OMRSOCK_IPPROTO_DEFAULT;
hintsFlags = 6;
OMRPORTLIB->sock_getaddrinfo_create_hints(OMRPORTLIB, &hints, hintsFamily, hintsSockType, hintsProtocol, hintsFlags);
rc = OMRPORTLIB->sock_addrinfo_length(OMRPORTLIB, hints, &length);
EXPECT_EQ(rc, 0);
EXPECT_EQ(length, uint32_t(1));
rc = OMRPORTLIB->sock_addrinfo_family(OMRPORTLIB, hints, 0, &family);
EXPECT_EQ(rc, 0);
EXPECT_EQ(family, hintsFamily);
rc = OMRPORTLIB->sock_addrinfo_socktype(OMRPORTLIB, hints, 0, &sockType);
EXPECT_EQ(rc, 0);
EXPECT_EQ(sockType, hintsSockType);
rc = OMRPORTLIB->sock_addrinfo_protocol(OMRPORTLIB, hints, 0, &protocol);
EXPECT_EQ(rc, 0);
EXPECT_EQ(protocol, hintsProtocol);
/* Testing invalid arguments: User exposes API and changes length, then query. */
hints->length = 5;
rc = OMRPORTLIB->sock_addrinfo_family(OMRPORTLIB, hints, 3, &family);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
rc = OMRPORTLIB->sock_addrinfo_socktype(OMRPORTLIB, hints, 3, &sockType);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
rc = OMRPORTLIB->sock_addrinfo_protocol(OMRPORTLIB, hints, 3, &protocol);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
}
/**
* Test @ref omrsock_getaddrinfo, @ref omrsock_freeaddrinfo and all functions
* that extract details from @ref omrsock_getaddrinfo results.
*
* In this test, the relevant variables are passed into
* @ref omrsock_getaddrinfo_create_hints. The generated hints are passed into
* @ref omrsock_getaddrinfo. The results generated are used to create a socket.
*
* Address families tested will include IPv4, IPv6(if supported on machine),
* unspecified family. Socket types tested will include Stream and Datagram.
*
* @note Errors such as failed function calls, and/or returning the wrong family or
* socket type from @ref omrsock_getaddrinfo compared to the ones passed into hints,
* will be reported.
*/
TEST(PortSockTest, getaddrinfo_and_freeaddrinfo)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRAddrInfoNode result;
OMRSockAddrStorage sockAddr;
omrsock_addrinfo_t hints = NULL;
omrsock_socket_t socket = NULL;
int32_t rc = 0;
uint32_t length = 0;
int32_t family = 0;
int32_t sockType = 0;
int32_t protocol = 0;
int32_t hintsFamily = OMRSOCK_AF_INET;
int32_t hintsSockType = OMRSOCK_STREAM;
int32_t hintsProtocol = OMRSOCK_IPPROTO_DEFAULT;
int32_t hintsFlags = 0;
rc = OMRPORTLIB->sock_getaddrinfo_create_hints(OMRPORTLIB, &hints, hintsFamily, hintsSockType, hintsProtocol, hintsFlags);
EXPECT_EQ(rc, 0);
/* Testing invalid arguments: getaddrinfo pointer to result is NULL. */
omrsock_addrinfo_t resultPtr = NULL;
rc = OMRPORTLIB->sock_getaddrinfo(OMRPORTLIB, "localhost", NULL, hints, resultPtr);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
/* Testing invalid arguments: freeaddrinfo pointer to result is NULL. */
rc = OMRPORTLIB->sock_freeaddrinfo(OMRPORTLIB, resultPtr);
EXPECT_EQ(rc, OMRPORT_ERROR_INVALID_ARGUMENTS);
/* Verify that omrsock_getaddrinfo and omrsock_freeaddrinfo works with NULL hints. */
rc = OMRPORTLIB->sock_getaddrinfo(OMRPORTLIB, "localhost", NULL, NULL, &result);
ASSERT_EQ(rc, 0);
OMRPORTLIB->sock_addrinfo_length(OMRPORTLIB, &result, &length);
ASSERT_NE(length, uint32_t(0));
OMRPORTLIB->sock_freeaddrinfo(OMRPORTLIB, &result);
/* Get and verify that omrsock_getaddrinfo and omrsock_freeaddrinfo works. */
rc = OMRPORTLIB->sock_getaddrinfo(OMRPORTLIB, "localhost", NULL, hints, &result);
ASSERT_EQ(rc, 0);
OMRPORTLIB->sock_addrinfo_length(OMRPORTLIB, &result, &length);
ASSERT_NE(length, uint32_t(0));
for (uint32_t i = 0; i < length; i++) {
rc = OMRPORTLIB->sock_addrinfo_family(OMRPORTLIB, &result, i, &family);
EXPECT_EQ(rc, 0);
EXPECT_EQ(family, hintsFamily);
rc = OMRPORTLIB->sock_addrinfo_socktype(OMRPORTLIB, &result, i, &sockType);
EXPECT_EQ(rc, 0);
EXPECT_EQ(sockType, hintsSockType);
rc = OMRPORTLIB->sock_addrinfo_protocol(OMRPORTLIB, &result, i, &protocol);
EXPECT_EQ(rc, 0);
}
/* Try to create a socket with results. */
for (uint32_t i = 0; i < length; i++) {
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_family(OMRPORTLIB, &result, i, &family), 0);
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_socktype(OMRPORTLIB, &result, i, &sockType), 0);
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_protocol(OMRPORTLIB, &result, i, &protocol), 0);
rc = OMRPORTLIB->sock_socket(OMRPORTLIB, &socket, family, sockType, protocol);
if(0 == rc) {
ASSERT_NE(socket, (void *)NULL);
ASSERT_EQ(OMRPORTLIB->sock_addrinfo_address(OMRPORTLIB, &result, i, &sockAddr), 0);
break;
}
}
EXPECT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, socket, &sockAddr), 0);
EXPECT_EQ(OMRPORTLIB->sock_listen(OMRPORTLIB, socket, 10), 0);
rc = OMRPORTLIB->sock_close(OMRPORTLIB, &socket);
EXPECT_EQ(rc, 0);
rc = OMRPORTLIB->sock_freeaddrinfo(OMRPORTLIB, &result);
EXPECT_EQ(rc, 0);
}
/**
* Test functions involving IPv4 and IPv6 socket addresses:
* To Create a Socket Address with INADDR_ANY IPv4 address
*
* After creating the address, their ability to bind and listen will be tested.
*
* Address families tested is IPv4.
*/
TEST(PortSockTest, create_addressany_IPv4_socket_address)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage sockAddr;
omrsock_socket_t socket = NULL;
uint16_t port = 4930;
int32_t flag = 1;
uint8_t addr[4];
uint32_t inaddrAny = OMRPORTLIB->sock_htonl(OMRPORTLIB, OMRSOCK_INADDR_ANY);
memcpy(addr, &inaddrAny, 4);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init(OMRPORTLIB, &sockAddr, OMRSOCK_AF_INET, addr, OMRPORTLIB->sock_htons(OMRPORTLIB, port)), 0);
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, &socket, OMRSOCK_AF_INET, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT), 0);
/** Set socket address to be reusable in case this address is in TIME_WAIT state. */
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_REUSEADDR, &flag), 0);
ASSERT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, socket, &sockAddr), 0);
ASSERT_EQ(OMRPORTLIB->sock_listen(OMRPORTLIB, socket, OMRSOCK_MAXCONN), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &socket), 0);
}
/**
* Test functions involving IPv4 and IPv6 socket addresses:
* To Create a Socket Address with dotted-decimal IPv4 address.
*
* After creating the address, their ability to bind and listen will be tested.
*
* Address families tested is IPv4.
*/
TEST(PortSockTest, create_dotted_decimal_IPv4_socket_address)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage sockAddr;
omrsock_socket_t socket = NULL;
uint16_t port = 4930;
int32_t flag = 1;
uint8_t addr[4];
EXPECT_EQ(OMRPORTLIB->sock_inet_pton(OMRPORTLIB, OMRSOCK_AF_INET, "127.0.0.1", addr), 0);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init(OMRPORTLIB, &sockAddr, OMRSOCK_AF_INET, addr, OMRPORTLIB->sock_htons(OMRPORTLIB, port)), 0);
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, &socket, OMRSOCK_AF_INET, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT), 0);
/** Set socket address to be reusable in case this address is in TIME_WAIT state. */
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_REUSEADDR, &flag), 0);
ASSERT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, socket, &sockAddr), 0);
ASSERT_EQ(OMRPORTLIB->sock_listen(OMRPORTLIB, socket, 10), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &socket), 0);
}
/**
* Test functions involving IPv4 and IPv6 socket addresses:
* To Create a Socket Address with the loopback IPv6 address.
*
* After creating the address, their ability to bind and listen will be tested.
*
* Address families tested include IPv4 and IPv6.
*/
TEST(PortSockTest, create_IPv6_socket_address)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage sockAddr;
omrsock_socket_t socket = NULL;
uint16_t port = 4930;
int32_t flag = 1;
uint8_t addr6[16];
EXPECT_EQ(OMRPORTLIB->sock_inet_pton(OMRPORTLIB, OMRSOCK_AF_INET6, "::1", addr6), 0);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init6(OMRPORTLIB, &sockAddr, OMRSOCK_AF_INET6, addr6, OMRPORTLIB->sock_htons(OMRPORTLIB, port), 0, 0), 0);
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, &socket, OMRSOCK_AF_INET6, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT), 0);
/** Set socket address to be reusable in case this address is in TIME_WAIT state. */
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_REUSEADDR, &flag), 0);
if (0 != (OMRPORTLIB->sock_bind(OMRPORTLIB, socket, &sockAddr))) {
portTestEnv->log(LEVEL_ERROR, "WARNING: Cannot test IPV6: Failed to find to IPV6 interface. \n");
EXPECT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &socket), 0);
return;
}
ASSERT_EQ(OMRPORTLIB->sock_listen(OMRPORTLIB, socket, 10), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &socket), 0);
}
/**
* Test functions involving IPv4 and IPv6 socket addresses:
* To Create a Socket Address with the in6addr_any IPv6 address.
*
* After creating the address, their ability to bind and listen will be tested.
*
* Address families tested include IPv4 and IPv6.
*/
TEST(PortSockTest, create_in6addrany_IPv6_socket_address)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage sockAddr;
omrsock_socket_t socket = NULL;
uint16_t port = 4930;
int32_t flag = 1;
uint8_t addr6[16];
EXPECT_EQ(OMRPORTLIB->sock_inet_pton(OMRPORTLIB, OMRSOCK_AF_INET6, "::0", addr6), 0);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init6(OMRPORTLIB, &sockAddr, OMRSOCK_AF_INET6, addr6, OMRPORTLIB->sock_htons(OMRPORTLIB, port), 0, 0), 0);
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, &socket, OMRSOCK_AF_INET6, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT), 0);
/** Set socket address to be reusable in case this address is in TIME_WAIT state. */
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_REUSEADDR, &flag), 0);
ASSERT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, socket, &sockAddr), 0);
ASSERT_EQ(OMRPORTLIB->sock_listen(OMRPORTLIB, socket, OMRSOCK_MAXCONN), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &socket), 0);
}
/**
* Test functions involving IPv4 and IPv6 socket addresses:
* To Create a Socket Address with IPv4-mapped IPv6 address.
*
* After creating the address, their ability to bind and listen will be tested.
*
* Address families tested include IPv4 and IPv6.
*/
TEST(PortSockTest, create_IPv4_mapped_IPv6_Socket_Address)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage sockAddr;
omrsock_socket_t socket = NULL;
uint16_t port = 4930;
int32_t flag = 1;
uint8_t addr[4];
EXPECT_EQ(OMRPORTLIB->sock_inet_pton(OMRPORTLIB, OMRSOCK_AF_INET, "127.0.0.1", addr), 0);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init6(OMRPORTLIB, &sockAddr, OMRSOCK_AF_INET, addr, OMRPORTLIB->sock_htons(OMRPORTLIB, port), 0, 0), 0);
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, &socket, OMRSOCK_AF_INET6, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT), 0);
/** Set socket address to be reusable in case this address is in TIME_WAIT state. */
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_REUSEADDR, &flag), 0);
ASSERT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, socket, &sockAddr), 0);
ASSERT_EQ(OMRPORTLIB->sock_listen(OMRPORTLIB, socket, 10), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &socket), 0);
}
/**
* Test functions to set up a stream connection by using two sockets, which talk to each other.
*
* First, the server starts and listens for connections. Then, the
* client starts and sends a request to connect to the server. The messages are
* sent both ways, and it is checked if they were sent correctly.
*
* Address families tested include IPv4 and socket types tested is stream.
*
* @note Errors such as failed function calls, failure to create server and/or client, wrong
* message sent/received, will be reported.
*/
TEST(PortSockTest, two_socket_stream_communication)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage serverStreamSockAddr;
omrsock_socket_t serverStreamSocket = NULL;
uint16_t port = 4930;
uint8_t serverAddr[4];
/* To Create a Server Socket and Address */
uint32_t inaddrAny = OMRPORTLIB->sock_htonl(OMRPORTLIB, OMRSOCK_INADDR_ANY);
memcpy(serverAddr, &inaddrAny, 4);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init(OMRPORTLIB, &serverStreamSockAddr, OMRSOCK_AF_INET, serverAddr, OMRPORTLIB->sock_htons(OMRPORTLIB, port)), 0);
start_server(OMRPORTLIB, OMRSOCK_AF_INET, OMRSOCK_STREAM, &serverStreamSocket, &serverStreamSockAddr);
/* To Create a Client Socket and Address */
OMRSockAddrStorage clientStreamSockAddr;
omrsock_socket_t clientStreamSocket = NULL;
connect_client_to_server(OMRPORTLIB, "localhost", NULL, OMRSOCK_AF_INET, OMRSOCK_STREAM, &clientStreamSocket, &clientStreamSockAddr, &serverStreamSockAddr);
/* Accept Connection */
OMRSockAddrStorage connectedClientStreamSockAddr;
omrsock_socket_t connectedClientStreamSocket = NULL;
ASSERT_EQ(OMRPORTLIB->sock_accept(OMRPORTLIB, serverStreamSocket, &connectedClientStreamSockAddr, &connectedClientStreamSocket), 0);
const char *msg = "This is an omrsock test for 2 socket stream communications.";
int32_t bytesLeft = strlen(msg) + 1;
uint8_t *cursor = (uint8_t *)msg;
int32_t bytesSent = 0;
while (0 != bytesLeft) {
bytesSent = OMRPORTLIB->sock_send(OMRPORTLIB, connectedClientStreamSocket, cursor, bytesLeft, 0);
ASSERT_GE(bytesSent, 0);
bytesLeft -= bytesSent;
cursor += bytesSent;
}
char buf[100] = {0};
bytesLeft = strlen(msg) + 1;
cursor = (uint8_t *)buf;
int32_t bytesRecv = 0;
while (0 != bytesLeft) {
bytesRecv = OMRPORTLIB->sock_recv(OMRPORTLIB, clientStreamSocket, cursor, bytesLeft, 0);
ASSERT_GE(bytesRecv, 0);
bytesLeft -= bytesRecv;
cursor += bytesRecv;
}
EXPECT_STREQ(msg, buf);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &connectedClientStreamSocket), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &clientStreamSocket), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverStreamSocket), 0);
}
/**
* Test functions to set up a datagram connection by using two sockets, which talk to each other.
*
* First, the server is set up, and then, the client is set up sends a request to
* connect to the server (this is optional). The messages are sent both ways, and
* it is checked if they were sent correctly.
*
* Address families tested include IPv4 in this test case. Socket types
* tested is datagram.
*
* @note Errors such as failed function calls, failure to create server and/or client, wrong
* message sent/received, will be reported.
*/
TEST(PortSockTest, two_socket_datagram_communication)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage serverSockAddr;
omrsock_socket_t serverSocket = NULL;
uint16_t port = 4930;
uint8_t serverAddr[4];
/* To Create a Server Socket and Address */
EXPECT_EQ(OMRPORTLIB->sock_inet_pton(OMRPORTLIB, OMRSOCK_AF_INET, "127.0.0.1", serverAddr), 0);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init(OMRPORTLIB, &serverSockAddr, OMRSOCK_AF_INET, serverAddr, OMRPORTLIB->sock_htons(OMRPORTLIB, port)), 0);
/* Datagram server sockets does not need to listen and accept */
start_server(OMRPORTLIB, OMRSOCK_AF_INET, OMRSOCK_DGRAM, &serverSocket, &serverSockAddr);
OMRSockAddrStorage clientSockAddr;
omrsock_socket_t clientSocket = NULL;
/* Connect is optional for datagram clients */
connect_client_to_server(OMRPORTLIB, "localhost", "16403", OMRSOCK_AF_INET, OMRSOCK_DGRAM, &clientSocket, &clientSockAddr, &serverSockAddr);
/* Datagram Sendto and Recvfrom test. Sendto and Recvfrom are called multiple tests to ensure it has been
* sent and received.
*/
OMRTimeval timeSend;
OMRTimeval timeRecv;
EXPECT_EQ(OMRPORTLIB->sock_timeval_init(OMRPORTLIB, &timeSend, 2, 0), 0);
EXPECT_EQ(OMRPORTLIB->sock_timeval_init(OMRPORTLIB, &timeRecv, 3, 0), 0);
ASSERT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, serverSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeSend), 0);
ASSERT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, clientSocket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeRecv), 0);
/* Send 50 times. This should be enough for the message to be sent. */
char msgDgram[100] = "This is an omrsock test for datagram communications.";
int32_t bytesTotal = strlen(msgDgram) + 1;
for (int32_t i = 0; i < 50; i++) {
OMRPORTLIB->sock_sendto(OMRPORTLIB, serverSocket, (uint8_t *)msgDgram, bytesTotal, 0, &clientSockAddr);
}
/* Receive */
char bufDgram[100] = "";
int32_t bytesRecv = 0;
for (int32_t i = 0; i < 200; i++) {
bytesRecv = OMRPORTLIB->sock_recvfrom(OMRPORTLIB, clientSocket, (uint8_t *)bufDgram, bytesTotal, 0, &serverSockAddr);
if (bytesRecv == bytesTotal) {
break;
}
}
EXPECT_EQ(strcmp(msgDgram, bufDgram), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &clientSocket), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverSocket), 0);
}
/**
* Equal operator to compare contents of OMRTimeval struct socket option values.
*/
bool
operator==(const OMRTimeval &lhs, const OMRTimeval &rhs)
{
return (lhs.data.tv_sec == rhs.data.tv_sec) && (lhs.data.tv_usec == rhs.data.tv_usec);
}
/**
* Equal operator to compare contents of OMRLinger struct socket option values.
*/
bool
operator==(const OMRLinger &lhs, const OMRLinger &rhs)
{
if (lhs.data.l_onoff != 0) {
return (rhs.data.l_onoff != 0) && (lhs.data.l_linger == rhs.data.l_linger);
}
return rhs.data.l_onoff == 0;
}
/**
* Custom print for contents of OMRTimeval struct when comparing contents in test unit.
*/
void
PrintTo(const OMRTimeval &x, std::ostream *os)
{
*os << "{" << x.data.tv_sec << ", " << x.data.tv_usec << "}";
}
/**
* Custome print for contents of OMRLinger struct when comparing contents in test unit.
*/
void
PrintTo(const OMRLinger &x, std::ostream *os)
{
*os << "{" << x.data.l_onoff << ", " << x.data.l_linger << "}";
}
/**
* Test socket options functions by test setting and getting the socket options.
*
* First, the linger and timeval structs will be allocated and the corresponding
* init functions will be called to set up those structs. Then, an arbitrary IPv4
* stream socket will be opened and the socket options will be set and get. The
* test will check if the set and get option values are the same.
*/
TEST(PortSockTest, socket_options)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRTimeval timeVal;
OMRLinger lingerVal;
omrsock_socket_t sock = NULL;
EXPECT_EQ(OMRPORTLIB->sock_timeval_init(OMRPORTLIB, &timeVal, 2, 0), 0);
EXPECT_EQ(OMRPORTLIB->sock_linger_init(OMRPORTLIB, &lingerVal, 1, 2), 0);
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, &sock, OMRSOCK_AF_INET, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT), 0);
/* Call omrsock_setsockopt to set socket options. */
int32_t flag = 1;
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_KEEPALIVE, &flag), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_linger(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_LINGER, &lingerVal), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeVal), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_timeval(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeVal), 0);
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, sock, OMRSOCK_IPPROTO_TCP, OMRSOCK_TCP_NODELAY, &flag), 0);
/* Call omrsock_getsockopt to check if the results matches the inputs earlier. */
OMRTimeval timeResult;
OMRLinger lingerResult;
int32_t flagResult;
EXPECT_EQ(OMRPORTLIB->sock_getsockopt_int(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_KEEPALIVE, &flagResult), 0);
EXPECT_NE(flagResult, 0);
EXPECT_EQ(OMRPORTLIB->sock_getsockopt_linger(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_LINGER, &lingerResult), 0);
EXPECT_EQ(lingerResult, lingerVal);
EXPECT_EQ(OMRPORTLIB->sock_getsockopt_timeval(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeResult), 0);
EXPECT_EQ(timeResult, timeVal);
EXPECT_EQ(OMRPORTLIB->sock_getsockopt_timeval(OMRPORTLIB, sock, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeResult), 0);
EXPECT_EQ(timeResult, timeVal);
EXPECT_EQ(OMRPORTLIB->sock_getsockopt_int(OMRPORTLIB, sock, OMRSOCK_IPPROTO_TCP, OMRSOCK_TCP_NODELAY, &flagResult), 0);
EXPECT_NE(flagResult, 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &sock), 0);
}
TEST(PortSockTest, socket_flags)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage sockAddr;
omrsock_socket_t socket = NULL;
uint32_t inaddrAny;
uint16_t port = 4930;
uint8_t addr[4];
int32_t flag = 1;
/* Set up address */
inaddrAny = OMRPORTLIB->sock_htonl(OMRPORTLIB, OMRSOCK_INADDR_ANY);
memcpy(addr, &inaddrAny, 4);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init(OMRPORTLIB, &sockAddr, OMRSOCK_AF_INET, addr, OMRPORTLIB->sock_htons(OMRPORTLIB, port)), 0);
/* Test setting non-blocking using OR in socktype field. */
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, &socket, OMRSOCK_AF_INET, OMRSOCK_STREAM | OMRSOCK_O_NONBLOCK, OMRSOCK_IPPROTO_DEFAULT), 0);
/** Set socket address to be reusable in case this address is in TIME_WAIT state. */
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_REUSEADDR, &flag), 0);
ASSERT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, socket, &sockAddr), 0);
ASSERT_EQ(OMRPORTLIB->sock_listen(OMRPORTLIB, socket, 10), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &socket), 0);
/* Test setting non-blocking using omrsock_fcntl. */
ASSERT_EQ(OMRPORTLIB->sock_socket(OMRPORTLIB, &socket, OMRSOCK_AF_INET, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT), 0);
/** Set socket address to be reusable in case this address is in TIME_WAIT state. */
EXPECT_EQ(OMRPORTLIB->sock_setsockopt_int(OMRPORTLIB, socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_REUSEADDR, &flag), 0);
EXPECT_EQ(OMRPORTLIB->sock_fcntl(OMRPORTLIB, socket, OMRSOCK_O_NONBLOCK), 0);
ASSERT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, socket, &sockAddr), 0);
ASSERT_EQ(OMRPORTLIB->sock_listen(OMRPORTLIB, socket, 10), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &socket), 0);
}
/**
* Test nonblocking functionality by connecting two sockets while it is nonblocking.
*
* The server is started. The client uses getaddrinfo to create a nonblocking socket
* The socket then tries to connect to server using nonblocking connect, which returns
* OMRPORT_ERROR_SOCKET_INPROGRESS or OMRPORT_ERROR_SOCKET_ALREADY_CONNECTED.
*/
TEST(PortSockTest, socket_nonblock_connection)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage serverSockAddr;
omrsock_socket_t serverSocket = NULL;
uint16_t port = 4930;
uint8_t serverAddr[4];
int32_t rc = 0;
/* To Create a Server Socket and Address */
EXPECT_EQ(OMRPORTLIB->sock_inet_pton(OMRPORTLIB, OMRSOCK_AF_INET, "127.0.0.1", serverAddr), 0);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init(OMRPORTLIB, &serverSockAddr, OMRSOCK_AF_INET, serverAddr, OMRPORTLIB->sock_htons(OMRPORTLIB, port)), 0);
start_server(OMRPORTLIB, OMRSOCK_AF_INET, OMRSOCK_STREAM, &serverSocket, &serverSockAddr);
/* To Create a Client Socket and Address */
OMRSockAddrStorage clientSockAddr;
omrsock_socket_t clientSocket = NULL;
omrsock_addrinfo_t hints = NULL;
OMRAddrInfoNode result;
uint32_t length = 0;
EXPECT_EQ(OMRPORTLIB->sock_getaddrinfo_create_hints(OMRPORTLIB, &hints, OMRSOCK_AF_INET, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT, 0), 0);
EXPECT_EQ(OMRPORTLIB->sock_getaddrinfo(OMRPORTLIB, "localhost", NULL, hints, &result), 0);
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_length(OMRPORTLIB, &result, &length), 0);
ASSERT_NE(length, uint32_t(0));
/* Create a nonblocking socket with results */
for (uint32_t i = 0; i < length; i++) {
if (0 == OMRPORTLIB->sock_socket(OMRPORTLIB, &clientSocket, OMRSOCK_AF_INET, OMRSOCK_STREAM | OMRSOCK_O_NONBLOCK, OMRSOCK_IPPROTO_DEFAULT)) {
ASSERT_NE(clientSocket, (void *)NULL);
EXPECT_EQ(OMRPORTLIB->sock_addrinfo_address(OMRPORTLIB, &result, i, &clientSockAddr), 0);
ASSERT_EQ(OMRPORTLIB->sock_bind(OMRPORTLIB, clientSocket, &clientSockAddr), 0);
break;
}
}
ASSERT_NE(clientSocket, (void *)NULL);
EXPECT_EQ(OMRPORTLIB->sock_freeaddrinfo(OMRPORTLIB, &result), 0);
/* Client Attempts Connection */
while (0 != (rc = OMRPORTLIB->sock_connect(OMRPORTLIB, clientSocket, &serverSockAddr))) {
if (OMRPORT_ERROR_SOCKET_INPROGRESS == rc) {
// Still in the middle of attempting connection.
continue;
}
else if (OMRPORT_ERROR_SOCKET_ALREADY_CONNECTED == rc) {
// Connected
break;
}
else {
FAIL() << "Error other than INPROGRESS and ALREADY_CONNECTED were returned for connect: " << ::testing::PrintToString(rc);
}
}
/* Server Accepts Connection */
OMRSockAddrStorage connectedServerSockAddr;
omrsock_socket_t connectedServerSocket = NULL;
ASSERT_EQ(OMRPORTLIB->sock_accept(OMRPORTLIB, serverSocket, &connectedServerSockAddr, &connectedServerSocket), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &connectedServerSocket), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &clientSocket), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverSocket), 0);
}
/**
* Test nonblocking functionality by two nonblocking socket communication.
*
* First, a connection is established between server and client. The server
* and client is then set to be nonblocking. Communication between server and
* client is then tested using OMRPORT_ERROR_SOCKET_WOULDBLOCK.
*/
TEST(PortSockTest, two_socket_nonblock_communication_basic)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage serverSockAddr;
omrsock_socket_t serverSocket = NULL;
OMRSockAddrStorage clientSockAddr;
omrsock_socket_t clientSocket = NULL;
OMRSockAddrStorage connectedServerSockAddr;
omrsock_socket_t connectedServerSocket = NULL;
uint16_t port = 4930;
uint32_t inaddrAny;
uint8_t serverAddr[4];
/* To Create a Server Socket and Address */
inaddrAny = OMRPORTLIB->sock_htonl(OMRPORTLIB, OMRSOCK_INADDR_ANY);
memcpy(serverAddr, &inaddrAny, 4);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init(OMRPORTLIB, &serverSockAddr, OMRSOCK_AF_INET, serverAddr, OMRPORTLIB->sock_htons(OMRPORTLIB, port)), 0);
start_server(OMRPORTLIB, OMRSOCK_AF_INET, OMRSOCK_STREAM, &serverSocket, &serverSockAddr);
/* To Create a Client Socket and Address */
connect_client_to_server(OMRPORTLIB, "localhost", NULL, OMRSOCK_AF_INET, OMRSOCK_STREAM, &clientSocket, &clientSockAddr, &serverSockAddr);
ASSERT_EQ(OMRPORTLIB->sock_fcntl(OMRPORTLIB, clientSocket, OMRSOCK_O_NONBLOCK), 0);
/* Accept Connection */
ASSERT_EQ(OMRPORTLIB->sock_accept(OMRPORTLIB, serverSocket, &connectedServerSockAddr, &connectedServerSocket), 0);
ASSERT_EQ(OMRPORTLIB->sock_fcntl(OMRPORTLIB, connectedServerSocket, OMRSOCK_O_NONBLOCK), 0);
/* Send and Recv using non-blocking sockets. */
char buf[100] = {0};
const char *msg = "This is an omrsock test for 2 socket stream communications.";
uint8_t *cursor = NULL;
int32_t bytesSent = 0;
int32_t bytesRecv = 0;
int32_t bytesLeft = 0;
/* Trying to receive on client side when nothing has been sent from the server. */
cursor = (uint8_t *)buf;
bytesRecv = OMRPORTLIB->sock_recv(OMRPORTLIB, clientSocket, cursor, strlen(msg) + 1, 0);
ASSERT_GE(bytesRecv, -1);
ASSERT_EQ(OMRPORTLIB->error_last_error_number(OMRPORTLIB), OMRPORT_ERROR_SOCKET_WOULDBLOCK);
/* Send from the server using non-blocking server socket. */
cursor = (uint8_t *)msg;
bytesLeft = strlen(msg) + 1;
bytesSent = 0;
while (0 != bytesLeft) {
bytesSent = OMRPORTLIB->sock_send(OMRPORTLIB, connectedServerSocket, cursor, bytesLeft, 0);
if (0 > bytesSent) {
// If bytesSent is -1, then it must mean it is still waiting to send.
ASSERT_EQ(OMRPORTLIB->error_last_error_number(OMRPORTLIB), OMRPORT_ERROR_SOCKET_WOULDBLOCK);
bytesSent = 0;
}
bytesLeft -= bytesSent;
cursor += bytesSent;
}
/* Receive on the client side using non-blocking client socket. */
cursor = (uint8_t *)buf;
bytesLeft = strlen(msg) + 1;
bytesRecv = 0;
while (0 != bytesLeft) {
bytesRecv = OMRPORTLIB->sock_recv(OMRPORTLIB, clientSocket, cursor, bytesLeft, 0);
if (0 >= bytesRecv) {
// If bytesRecv is -1, then it must mean it is still waiting to receive the message.
ASSERT_EQ(OMRPORTLIB->error_last_error_number(OMRPORTLIB), OMRPORT_ERROR_SOCKET_WOULDBLOCK);
bytesRecv = 0;
}
bytesLeft -= bytesRecv;
cursor += bytesRecv;
}
EXPECT_STREQ(msg, buf);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &connectedServerSocket), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &clientSocket), 0);
ASSERT_EQ(OMRPORTLIB->sock_close(OMRPORTLIB, &serverSocket), 0);
}
/**
* Test nonblocking functionality by two nonblocking socket communication.
* The nonblocking is assigned to server at the very beginning. Accept
* nonblocking is performed using OMRPORT_ERROR_SOCKET_WOULDBLOCK.
* Communication between server and client is then tested using
* OMRPORT_ERROR_SOCKET_WOULDBLOCK.
*/
TEST(PortSockTest, two_socket_nonblock_server_communication)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
OMRSockAddrStorage serverSockAddr;
omrsock_socket_t serverSocket = NULL;
OMRSockAddrStorage clientSockAddr;
omrsock_socket_t clientSocket = NULL;
OMRSockAddrStorage connectedServerSockAddr;
omrsock_socket_t connectedServerSocket = NULL;
uint16_t port = 4930;
uint32_t inaddrAny;
uint8_t serverAddr[4];
int32_t rc = 0;
/* To Create a Server Socket and Address */
inaddrAny = OMRPORTLIB->sock_htonl(OMRPORTLIB, OMRSOCK_INADDR_ANY);
memcpy(serverAddr, &inaddrAny, 4);
EXPECT_EQ(OMRPORTLIB->sock_sockaddr_init(OMRPORTLIB, &serverSockAddr, OMRSOCK_AF_INET, serverAddr, OMRPORTLIB->sock_htons(OMRPORTLIB, port)), 0);
start_server(OMRPORTLIB, OMRSOCK_AF_INET, OMRSOCK_STREAM | OMRSOCK_O_NONBLOCK, &serverSocket, &serverSockAddr);
/* To Create a Client Socket and Address */
connect_client_to_server(OMRPORTLIB, "localhost", NULL, OMRSOCK_AF_INET, OMRSOCK_STREAM, &clientSocket, &clientSockAddr, &serverSockAddr);
ASSERT_EQ(OMRPORTLIB->sock_fcntl(OMRPORTLIB, clientSocket, OMRSOCK_O_NONBLOCK), 0);
/* Accept Connection */
while (0 != (rc = OMRPORTLIB->sock_accept(OMRPORTLIB, serverSocket, &connectedServerSockAddr, &connectedServerSocket))) {
if (OMRPORT_ERROR_SOCKET_WOULDBLOCK == rc) {
// Still in the middle of accepting next connection request.
continue;
}
else {
FAIL() << "Error other than OMRPORT_ERROR_SOCKET_WOULDBLOCK was returned for accept: " << ::testing::PrintToString(rc);
}
}
/* Send and Recv using non-blocking sockets. */
char buf[100] = {0};
const char *msg = "This is an omrsock test for 2 socket stream communications.";
uint8_t *cursor = NULL;
int32_t bytesSent = 0;
int32_t bytesRecv = 0;
int32_t bytesLeft = 0;
/* Trying to receive on client side when nothing has been sent from the server. */
cursor = (uint8_t *)buf;
bytesRecv = OMRPORTLIB->sock_recv(OMRPORTLIB, clientSocket, cursor, strlen(msg) + 1, 0);
ASSERT_GE(bytesRecv, -1);
ASSERT_EQ(OMRPORTLIB->error_last_error_number(OMRPORTLIB), OMRPORT_ERROR_SOCKET_WOULDBLOCK);
/* Send from the server using non-blocking server socket. */
cursor = (uint8_t *)msg;
bytesLeft = strlen(msg) + 1;
bytesSent = 0;
while (0 != bytesLeft) {
bytesSent = OMRPORTLIB->sock_send(OMRPORTLIB, connectedServerSocket, cursor, bytesLeft, 0);
if (0 > bytesSent) {