-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
gen_tcp_misc_SUITE.erl
2542 lines (2297 loc) · 78 KB
/
gen_tcp_misc_SUITE.erl
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1998-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(gen_tcp_misc_SUITE).
-include_lib("test_server/include/test_server.hrl").
%-compile(export_all).
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
init_per_group/2,end_per_group/2,
controlling_process/1, no_accept/1, close_with_pending_output/1,
data_before_close/1, iter_max_socks/1, get_status/1,
passive_sockets/1, accept_closed_by_other_process/1,
init_per_testcase/2, end_per_testcase/2,
otp_3924/1, otp_3924_sender/4, closed_socket/1,
shutdown_active/1, shutdown_passive/1, shutdown_pending/1,
default_options/1, http_bad_packet/1,
busy_send/1, busy_disconnect_passive/1, busy_disconnect_active/1,
fill_sendq/1, partial_recv_and_close/1,
partial_recv_and_close_2/1,partial_recv_and_close_3/1,so_priority/1,
% Accept tests
primitive_accept/1,multi_accept_close_listen/1,accept_timeout/1,
accept_timeouts_in_order/1,accept_timeouts_in_order2/1,
accept_timeouts_in_order3/1,accept_timeouts_mixed/1,
killing_acceptor/1,killing_multi_acceptors/1,killing_multi_acceptors2/1,
several_accepts_in_one_go/1,active_once_closed/1, send_timeout/1, send_timeout_active/1,
otp_7731/1, zombie_sockets/1, otp_7816/1, otp_8102/1,
otp_9389/1]).
%% Internal exports.
-export([sender/3, not_owner/1, passive_sockets_server/2, priority_server/1,
otp_7731_server/1, zombie_server/2]).
init_per_testcase(_Func, Config) when is_list(Config) ->
Dog = test_server:timetrap(test_server:seconds(240)),
[{watchdog, Dog}|Config].
end_per_testcase(_Func, Config) ->
Dog = ?config(watchdog, Config),
test_server:timetrap_cancel(Dog).
suite() -> [{ct_hooks,[ts_install_cth]}].
all() ->
[controlling_process, no_accept,
close_with_pending_output, data_before_close,
iter_max_socks, passive_sockets,
accept_closed_by_other_process, otp_3924, closed_socket,
shutdown_active, shutdown_passive, shutdown_pending,
default_options, http_bad_packet, busy_send,
busy_disconnect_passive, busy_disconnect_active,
fill_sendq, partial_recv_and_close,
partial_recv_and_close_2, partial_recv_and_close_3,
so_priority, primitive_accept,
multi_accept_close_listen, accept_timeout,
accept_timeouts_in_order, accept_timeouts_in_order2,
accept_timeouts_in_order3, accept_timeouts_mixed,
killing_acceptor, killing_multi_acceptors,
killing_multi_acceptors2, several_accepts_in_one_go,
active_once_closed, send_timeout, send_timeout_active, otp_7731,
zombie_sockets, otp_7816, otp_8102, otp_9389].
groups() ->
[].
init_per_suite(Config) ->
Config.
end_per_suite(_Config) ->
ok.
init_per_group(_GroupName, Config) ->
Config.
end_per_group(_GroupName, Config) ->
Config.
default_options(doc) ->
["Tests kernel application variables inet_default_listen_options and "
"inet_default_connect_options"];
default_options(suite) ->
[];
default_options(Config) when is_list(Config) ->
%% First check the delay_send option
?line {true,true,true}=do_delay_send_1(),
?line {false,false,false}=do_delay_send_2(),
?line {true,false,false}=do_delay_send_3(),
?line {false,false,false}=do_delay_send_4(),
?line {false,false,false}=do_delay_send_5(),
?line {false,true,true}=do_delay_send_6(),
%% Now lets start some nodes with different combinations of options:
?line {true,true,true} = do_delay_on_other_node("",
fun do_delay_send_1/0),
?line {true,false,false} =
do_delay_on_other_node("-kernel inet_default_connect_options "
"\"[{delay_send,true}]\"",
fun do_delay_send_2/0),
?line {false,true,true} =
do_delay_on_other_node("-kernel inet_default_listen_options "
"\"[{delay_send,true}]\"",
fun do_delay_send_2/0),
?line {true,true,true} =
do_delay_on_other_node("-kernel inet_default_listen_options "
"\"[{delay_send,true}]\"",
fun do_delay_send_3/0),
?line {true,true,true} =
do_delay_on_other_node("-kernel inet_default_connect_options "
"\"[{delay_send,true}]\"",
fun do_delay_send_6/0),
?line {false,false,false} =
do_delay_on_other_node("-kernel inet_default_connect_options "
"\"[{delay_send,true}]\"",
fun do_delay_send_5/0),
?line {false,true,true} =
do_delay_on_other_node("-kernel inet_default_connect_options "
"\"[{delay_send,true}]\" "
"-kernel inet_default_listen_options "
"\"[{delay_send,true}]\"",
fun do_delay_send_5/0),
?line {true,false,false} =
do_delay_on_other_node("-kernel inet_default_connect_options "
"\"[{delay_send,true}]\" "
"-kernel inet_default_listen_options "
"\"[{delay_send,true}]\"",
fun do_delay_send_4/0),
?line {true,true,true} =
do_delay_on_other_node("-kernel inet_default_connect_options "
"\"{delay_send,true}\" "
"-kernel inet_default_listen_options "
"\"{delay_send,true}\"",
fun do_delay_send_2/0),
%% Active is to dangerous and is supressed
?line {true,true,true} =
do_delay_on_other_node("-kernel inet_default_connect_options "
"\"{active,false}\" "
"-kernel inet_default_listen_options "
"\"{active,false}\"",
fun do_delay_send_7/0),
?line {true,true,true} =
do_delay_on_other_node("-kernel inet_default_connect_options "
"\"[{active,false},{delay_send,true}]\" "
"-kernel inet_default_listen_options "
"\"[{active,false},{delay_send,true}]\"",
fun do_delay_send_7/0),
?line {true,true,true} =
do_delay_on_other_node("-kernel inet_default_connect_options "
"\"[{active,false},{delay_send,true}]\" "
"-kernel inet_default_listen_options "
"\"[{active,false},{delay_send,true}]\"",
fun do_delay_send_2/0),
ok.
do_delay_on_other_node(XArgs, Function) ->
Dir = filename:dirname(code:which(?MODULE)),
{ok,Node} = test_server:start_node(test_default_options_slave,slave,
[{args,"-pa " ++ Dir ++ " " ++
XArgs}]),
Res = rpc:call(Node,erlang,apply,[Function,[]]),
test_server:stop_node(Node),
Res.
do_delay_send_1() ->
{ok,LS}=gen_tcp:listen(0,[{delay_send,true}]),
{ok,{{0,0,0,0},PortNum}}=inet:sockname(LS),
{ok,S}=gen_tcp:connect("localhost",PortNum,[{delay_send,true}]),
{ok,S2}= gen_tcp:accept(LS),
{ok,[{delay_send,B1}]}=inet:getopts(S,[delay_send]),
{ok,[{delay_send,B2}]}=inet:getopts(LS,[delay_send]),
{ok,[{delay_send,B3}]}=inet:getopts(S2,[delay_send]),
gen_tcp:close(S2),
gen_tcp:close(S),
gen_tcp:close(LS),
{B1,B2,B3}.
do_delay_send_2() ->
{ok,LS}=gen_tcp:listen(0,[]),
{ok,{{0,0,0,0},PortNum}}=inet:sockname(LS),
{ok,S}=gen_tcp:connect("localhost",PortNum,[]),
{ok,S2}= gen_tcp:accept(LS),
{ok,[{delay_send,B1}]}=inet:getopts(S,[delay_send]),
{ok,[{delay_send,B2}]}=inet:getopts(LS,[delay_send]),
{ok,[{delay_send,B3}]}=inet:getopts(S2,[delay_send]),
gen_tcp:close(S2),
gen_tcp:close(S),
gen_tcp:close(LS),
{B1,B2,B3}.
do_delay_send_3() ->
{ok,LS}=gen_tcp:listen(0,[]),
{ok,{{0,0,0,0},PortNum}}=inet:sockname(LS),
{ok,S}=gen_tcp:connect("localhost",PortNum,[{delay_send,true}]),
{ok,S2}= gen_tcp:accept(LS),
{ok,[{delay_send,B1}]}=inet:getopts(S,[delay_send]),
{ok,[{delay_send,B2}]}=inet:getopts(LS,[delay_send]),
{ok,[{delay_send,B3}]}=inet:getopts(S2,[delay_send]),
gen_tcp:close(S2),
gen_tcp:close(S),
gen_tcp:close(LS),
{B1,B2,B3}.
do_delay_send_4() ->
{ok,LS}=gen_tcp:listen(0,[{delay_send,false}]),
{ok,{{0,0,0,0},PortNum}}=inet:sockname(LS),
{ok,S}=gen_tcp:connect("localhost",PortNum,[]),
{ok,S2}= gen_tcp:accept(LS),
{ok,[{delay_send,B1}]}=inet:getopts(S,[delay_send]),
{ok,[{delay_send,B2}]}=inet:getopts(LS,[delay_send]),
{ok,[{delay_send,B3}]}=inet:getopts(S2,[delay_send]),
gen_tcp:close(S2),
gen_tcp:close(S),
gen_tcp:close(LS),
{B1,B2,B3}.
do_delay_send_5() ->
{ok,LS}=gen_tcp:listen(0,[]),
{ok,{{0,0,0,0},PortNum}}=inet:sockname(LS),
{ok,S}=gen_tcp:connect("localhost",PortNum,[{delay_send,false}]),
{ok,S2}= gen_tcp:accept(LS),
{ok,[{delay_send,B1}]}=inet:getopts(S,[delay_send]),
{ok,[{delay_send,B2}]}=inet:getopts(LS,[delay_send]),
{ok,[{delay_send,B3}]}=inet:getopts(S2,[delay_send]),
gen_tcp:close(S2),
gen_tcp:close(S),
gen_tcp:close(LS),
{B1,B2,B3}.
do_delay_send_6() ->
{ok,LS}=gen_tcp:listen(0,[{delay_send,true}]),
{ok,{{0,0,0,0},PortNum}}=inet:sockname(LS),
{ok,S}=gen_tcp:connect("localhost",PortNum,[]),
{ok,S2}= gen_tcp:accept(LS),
{ok,[{delay_send,B1}]}=inet:getopts(S,[delay_send]),
{ok,[{delay_send,B2}]}=inet:getopts(LS,[delay_send]),
{ok,[{delay_send,B3}]}=inet:getopts(S2,[delay_send]),
gen_tcp:close(S2),
gen_tcp:close(S),
gen_tcp:close(LS),
{B1,B2,B3}.
do_delay_send_7() ->
{ok,LS}=gen_tcp:listen(0,[]),
{ok,{{0,0,0,0},PortNum}}=inet:sockname(LS),
{ok,S}=gen_tcp:connect("localhost",PortNum,[]),
{ok,S2}= gen_tcp:accept(LS),
{ok,[{active,B1}]}=inet:getopts(S,[active]),
{ok,[{active,B2}]}=inet:getopts(LS,[active]),
{ok,[{active,B3}]}=inet:getopts(S2,[active]),
gen_tcp:close(S2),
gen_tcp:close(S),
gen_tcp:close(LS),
{B1,B2,B3}.
controlling_process(doc) ->
["Open a listen port and change controlling_process for it",
"The result should be ok of done by the owner process,"
"Otherwise is should return {error,not_owner} or similar"];
controlling_process(suite) -> [];
controlling_process(Config) when is_list(Config) ->
{ok,S} = gen_tcp:listen(0,[]),
Pid2 = spawn(?MODULE,not_owner,[S]),
Pid2 ! {self(),2,control},
?line {error, E} = receive {2,_E} ->
_E
after 10000 -> timeout
end,
io:format("received ~p~n",[E]),
Pid = spawn(?MODULE,not_owner,[S]),
?line ok = gen_tcp:controlling_process(S,Pid),
Pid ! {self(),1,control},
?line ok = receive {1,ok} ->
ok
after 1000 -> timeout
end,
Pid ! close.
not_owner(S) ->
receive
{From,Tag,control} ->
From ! {Tag,gen_tcp:controlling_process(S,self())};
close ->
gen_tcp:close(S)
after 1000 ->
ok
end.
no_accept(doc) ->
["Open a listen port and connect to it, then close the listen port ",
"without doing any accept. The connected socket should receive ",
"a tcp_closed message."];
no_accept(suite) -> [];
no_accept(Config) when is_list(Config) ->
case os:type() of
vxworks ->
{skip,"Too tough for vxworks"};
_ ->
no_accept2()
end.
no_accept2() ->
?line {ok, L} = gen_tcp:listen(0, []),
?line {ok, {_, Port}} = inet:sockname(L),
?line {ok, Client} = gen_tcp:connect(localhost, Port, []),
?line ok = gen_tcp:close(L),
?line receive
{tcp_closed, Client} ->
ok
after 5000 ->
?line test_server:fail(never_closed)
end.
close_with_pending_output(doc) ->
["Send several packets to a socket and close it. All packets should arrive ",
"to the other end."];
close_with_pending_output(suite) -> [];
close_with_pending_output(Config) when is_list(Config) ->
case os:type() of
vxworks ->
{skipped,"Too tough for vxworks"};
_ ->
close_with_pending_output2()
end.
close_with_pending_output2() ->
?line {ok, L} = gen_tcp:listen(0, [binary, {active, false}]),
?line {ok, {_, Port}} = inet:sockname(L),
?line Packets = 16,
?line Total = 2048*Packets,
case start_remote(close_pending) of
{ok, Node} ->
?line {ok, Host} = inet:gethostname(),
?line spawn_link(Node, ?MODULE, sender, [Port, Packets, Host]),
?line {ok, A} = gen_tcp:accept(L),
?line case gen_tcp:recv(A, Total) of
{ok, Bin} when byte_size(Bin) == Total ->
gen_tcp:close(A),
gen_tcp:close(L);
{ok, Bin} ->
?line test_server:fail({small_packet,
byte_size(Bin)});
Error ->
?line test_server:fail({unexpected, Error})
end,
ok;
{error, no_remote_hosts} ->
{skipped,"No remote hosts"};
{error, Other} ->
?line ?t:fail({failed_to_start_slave_node, Other})
end.
sender(Port, Packets, Host) ->
X256 = lists:seq(0, 255),
X512 = [X256|X256],
X1K = [X512|X512],
Bin = list_to_binary([X1K|X1K]),
{ok, Sock} = gen_tcp:connect(Host, Port, []),
send_loop(Sock, Bin, Packets),
ok = gen_tcp:close(Sock).
send_loop(_Sock, _Data, 0) -> ok;
send_loop(Sock, Data, Left) ->
ok = gen_tcp:send(Sock, Data),
send_loop(Sock, Data, Left-1).
-define(OTP_3924_MAX_DELAY, 100).
%% Taken out of the blue, but on intra host connections
%% I expect propagation of a close to be quite fast
%% so 100 ms seems reasonable.
otp_3924(doc) ->
["Tests that a socket can be closed fast enough."];
otp_3924(suite) -> [];
otp_3924(Config) when is_list(Config) ->
MaxDelay = (case has_superfluous_schedulers() of
true -> 4;
false -> 1
end
* case {erlang:system_info(debug_compiled),
erlang:system_info(lock_checking)} of
{true, _} -> 6;
{_, true} -> 2;
_ -> 1
end * ?OTP_3924_MAX_DELAY),
case os:type() of
vxworks ->
%% {skip,"Too tough for vxworks"};
otp_3924_1(MaxDelay);
_ ->
otp_3924_1(MaxDelay)
end.
otp_3924_1(MaxDelay) ->
Dog = test_server:timetrap(test_server:seconds(240)),
?line {ok, Node} = start_node(otp_3924),
?line DataLen = 100*1024,
?line Data = otp_3924_data(DataLen),
% Repeat the test a couple of times to prevent the test from passing
% by chance.
repeat(10,
fun (N) ->
?line ok = otp_3924(MaxDelay, Node, Data, DataLen, N)
end),
?line test_server:stop_node(Node),
test_server:timetrap_cancel(Dog),
ok.
otp_3924(MaxDelay, Node, Data, DataLen, N) ->
?line {ok, L} = gen_tcp:listen(0, [list, {active, false}]),
?line {ok, {_, Port}} = inet:sockname(L),
?line {ok, Host} = inet:gethostname(),
?line Sender = spawn_link(Node,
?MODULE,
otp_3924_sender,
[self(), Host, Port, Data]),
?line Data = otp_3924_receive_data(L, Sender, MaxDelay, DataLen, N),
?line ok = gen_tcp:close(L).
otp_3924_receive_data(LSock, Sender, MaxDelay, Len, N) ->
?line OP = process_flag(priority, max),
?line OTE = process_flag(trap_exit, true),
?line TimeoutRef = make_ref(),
?line Data = (catch begin
?line Sender ! start,
?line {ok, Sock} = gen_tcp:accept(LSock),
?line D = otp_3924_receive_data(Sock,
TimeoutRef,
MaxDelay,
Len,
[],
0),
?line ok = gen_tcp:close(Sock),
D
end),
?line unlink(Sender),
?line process_flag(trap_exit, OTE),
?line process_flag(priority, OP),
receive
{'EXIT', _, TimeoutRef} ->
?line test_server:fail({close_not_fast_enough,MaxDelay,N});
{'EXIT', Sender, Reason} ->
?line test_server:fail({sender_exited, Reason});
{'EXIT', _Other, Reason} ->
?line test_server:fail({linked_process_exited, Reason})
after 0 ->
case Data of
{'EXIT', {A,B}} ->
?line test_server:fail({A,B,N});
{'EXIT', Failure} ->
?line test_server:fail(Failure);
_ ->
?line Data
end
end.
otp_3924_receive_data(Sock, TimeoutRef, MaxDelay, Len, Acc, AccLen) ->
case gen_tcp:recv(Sock, 0) of
{ok, Data} ->
NewAccLen = AccLen + length(Data),
if
NewAccLen == Len ->
?line {ok, TRef} = timer:exit_after(MaxDelay,
self(),
TimeoutRef),
?line {error, closed} = gen_tcp:recv(Sock, 0),
?line timer:cancel(TRef),
?line lists:flatten([Acc, Data]);
NewAccLen > Len ->
exit({received_too_much, NewAccLen});
true ->
otp_3924_receive_data(Sock,
TimeoutRef,
MaxDelay,
Len,
[Acc, Data],
NewAccLen)
end;
{error, closed} ->
exit({premature_close, AccLen});
Error ->
exit({unexpected_error, Error})
end.
otp_3924_data(Size) ->
Block =
"This is a sequence of characters that will be repeated "
"again and again and again and again and again and ... ",
L = length(Block),
otp_3924_data(Block, [], Size div L, Size rem L).
otp_3924_data(_, Acc, 0, 0) ->
lists:flatten(Acc);
otp_3924_data(_, Acc, 0, SingleLeft) ->
otp_3924_data(false, ["."|Acc], 0, SingleLeft-1);
otp_3924_data(Block, Acc, BlockLeft, SingleLeft) ->
otp_3924_data(Block, [Block|Acc], BlockLeft-1, SingleLeft).
otp_3924_sender(Receiver, Host, Port, Data) ->
receive
start ->
{ok, Sock} = gen_tcp:connect(Host, Port, [list]),
gen_tcp:send(Sock, Data),
ok = gen_tcp:close(Sock),
unlink(Receiver)
end.
data_before_close(doc) ->
["Tests that a huge amount of data can be received before a close."];
data_before_close(Config) when is_list(Config) ->
case os:type() of
vxworks ->
{skip,"Too tough for vxworks"};
_ ->
data_before_close2()
end.
data_before_close2() ->
?line {ok, L} = gen_tcp:listen(0, [binary]),
?line {ok, {_, TcpPort}} = inet:sockname(L),
?line Bytes = 256*1024,
?line spawn_link(fun() -> huge_sender(TcpPort, Bytes) end),
?line {ok, A} = gen_tcp:accept(L),
?line case count_bytes_recv(A, 0) of
{Bytes, Result} ->
io:format("Result: ~p", [Result]);
{Wrong, Result} ->
io:format("Result: ~p", [Result]),
test_server:fail({wrong_count, Wrong})
end,
ok.
count_bytes_recv(Sock, Total) ->
receive
{tcp, Sock, Bin} ->
count_bytes_recv(Sock, Total+byte_size(Bin));
Other ->
{Total, Other}
end.
huge_sender(TcpPort, Bytes) ->
{ok, Client} = gen_tcp:connect(localhost, TcpPort, []),
receive after 500 -> ok end,
gen_tcp:send(Client, make_zero_packet(Bytes)),
gen_tcp:close(Client).
make_zero_packet(0) -> [];
make_zero_packet(N) when N rem 2 == 0 ->
P = make_zero_packet(N div 2),
[P|P];
make_zero_packet(N) ->
P = make_zero_packet(N div 2),
[0, P|P].
get_status(doc) ->
["OTP-2924",
"test that the socket process does not crash when sys:get_status(Pid)",
"is called."];
get_status(suite) -> [];
get_status(Config) when is_list(Config) ->
?line {ok,{socket,Pid,_,_}} = gen_tcp:listen(5678,[]),
?line {status,Pid,_,_} = sys:get_status(Pid).
iter_max_socks(doc) ->
["Open as many sockets as possible. Do this several times and check ",
"that we get the same number of sockets every time."];
iter_max_socks(Config) when is_list(Config) ->
case os:type() of
vxworks ->
{skip,"Too tough for vxworks"};
_ ->
iter_max_socks2()
end.
-define(RECOVER_SLEEP, 60000).
-define(RETRY_SLEEP, 15000).
iter_max_socks2() ->
?line N =
case os:type() of
vxworks ->
10;
_ ->
20
end,
L = do_iter_max_socks(N, initalize),
?line io:format("Result: ~p",[L]),
?line all_equal(L),
?line {comment, "Max sockets: " ++ integer_to_list(hd(L))}.
do_iter_max_socks(0, _) ->
[];
do_iter_max_socks(N, initalize) ->
MS = max_socks(),
[MS|do_iter_max_socks(N-1, MS)];
do_iter_max_socks(N, failed) ->
MS = max_socks(),
[MS|do_iter_max_socks(N-1, failed)];
do_iter_max_socks(N, First) when is_integer(First) ->
?line MS = max_socks(),
if MS == First ->
?line [MS|do_iter_max_socks(N-1, First)];
true ->
?line io:format("Sleeping for ~p seconds...~n",
[?RETRY_SLEEP/1000]),
?line ?t:sleep(?RETRY_SLEEP),
?line io:format("Trying again...~n", []),
?line RetryMS = max_socks(),
?line if RetryMS == First ->
?line [RetryMS|do_iter_max_socks(N-1, First)];
true ->
?line [RetryMS|do_iter_max_socks(N-1, failed)]
end
end.
all_equal([]) ->
ok;
all_equal([Rule | T]) ->
all_equal(Rule, T).
all_equal(Rule, [Rule | T]) ->
all_equal(Rule, T);
all_equal(_, [_ | _]) ->
?line ?t:sleep(?RECOVER_SLEEP), % Wait a while and *hope* that we'll
% recover so other tests won't be
% affected.
?t:fail(max_socket_mismatch);
all_equal(_Rule, []) ->
ok.
max_socks() ->
?line Socks = open_socks(),
?line N = length(Socks),
?line lists:foreach(fun(S) -> ok = gen_tcp:close(S) end, Socks),
io:format("Got ~p sockets", [N]),
N.
open_socks() ->
case gen_tcp:listen(0, []) of
{ok, L} ->
{ok, {_, Port}} = inet:sockname(L),
[L| connect_accept(L, Port)];
_ ->
[]
end.
connect_accept(L, Port) ->
case gen_tcp:connect(localhost, Port, []) of
{ok, C} ->
[C| do_accept(L, Port)];
_ ->
[]
end.
do_accept(L, Port) ->
case gen_tcp:accept(L) of
{ok, A} -> [A| connect_accept(L, Port)];
_ -> []
end.
start_node(Name) ->
Pa = filename:dirname(code:which(?MODULE)),
test_server:start_node(Name, slave, [{args, "-pa " ++ Pa}]).
start_remote(Name) ->
Pa = filename:dirname(code:which(?MODULE)),
test_server:start_node(Name, slave, [{remote, true}, {args, "-pa " ++ Pa}]).
passive_sockets(doc) ->
["Tests that when 'the other side' on a passive socket closes, the connecting",
"side still can read until the end of data."];
passive_sockets(Config) when is_list(Config) ->
?line spawn_link(?MODULE, passive_sockets_server,
[[{active,false}],self()]),
?line receive
{socket,Port} -> ok
end,
?t:sleep(500),
?line case gen_tcp:connect("localhost", Port, [{active, false}]) of
{ok, Sock} ->
passive_sockets_read(Sock);
Error ->
?t:fail({"Could not connect to server", Error})
end.
%%
%% Read until we get an {error, closed}. If we get another error, this test case
%% should fail.
%%
passive_sockets_read(Sock) ->
case gen_tcp:recv(Sock, 0, 2000) of
{ok, Data} ->
io:format("Received ~p bytes~n", [length(Data)]),
passive_sockets_read(Sock);
{error, closed} ->
gen_tcp:close(Sock);
Error ->
gen_tcp:close(Sock),
?t:fail({"Did not get {error, closed} before other error", Error})
end.
passive_sockets_server(Opts, Parent) ->
?line case gen_tcp:listen(0, Opts) of
{ok, LSock} ->
{ok,{_,Port}} = inet:sockname(LSock),
Parent ! {socket,Port},
passive_sockets_server_accept(LSock);
Error ->
?t:fail({"Could not create listen socket", Error})
end.
passive_sockets_server_accept(Sock) ->
?line case gen_tcp:accept(Sock) of
{ok, Socket} ->
?t:sleep(500), % Simulate latency
passive_sockets_server_send(Socket, 5),
passive_sockets_server_accept(Sock);
Error ->
?t:fail({"Could not accept connection", Error})
end.
passive_sockets_server_send(Socket, 0) ->
io:format("Closing other end..~n", []),
gen_tcp:close(Socket);
passive_sockets_server_send(Socket, X) ->
?line Data = lists:duplicate(1024*X, $a),
?line case gen_tcp:send(Socket, Data) of
ok ->
?t:sleep(50), % Simulate some processing.
passive_sockets_server_send(Socket, X-1);
{error, _Reason} ->
?t:fail("Failed to send data")
end.
accept_closed_by_other_process(doc) ->
["Tests the return value from gen_tcp:accept when ",
"the socket is closed from an other process. (OTP-3817)"];
accept_closed_by_other_process(Config) when is_list(Config) ->
?line Parent = self(),
?line {ok, ListenSocket} = gen_tcp:listen(0, []),
?line Child =
spawn_link(
fun() ->
Parent ! {self(), gen_tcp:accept(ListenSocket)}
end),
?line receive after 1000 -> ok end,
?line ok = gen_tcp:close(ListenSocket),
?line receive
{Child, {error, closed}} ->
ok;
{Child, Other} ->
?t:fail({"Wrong result of gen_tcp:accept", Other})
end.
repeat(N, Fun) ->
repeat(N, N, Fun).
repeat(N, T, Fun) when is_integer(N), N > 0 ->
Fun(T-N),
repeat(N-1, T, Fun);
repeat(_, _, _) ->
ok.
closed_socket(suite) ->
[];
closed_socket(doc) ->
["Tests the response when using a closed socket as argument"];
closed_socket(Config) when is_list(Config) ->
?line {ok, LS1} = gen_tcp:listen(0, []),
?line erlang:yield(),
?line ok = gen_tcp:close(LS1),
%% If the following delay is uncommented, the result error values
%% below will change from {error, einval} to {error, closed} since
%% inet_db then will have noticed that the socket is closed.
%% This is a scheduling issue, i.e when the gen_server in
%% in inet_db processes the 'EXIT' message from the port,
%% the socket is unregistered.
%%
%% ?line test_server:sleep(test_server:seconds(2)),
%%
?line {error, R_send} = gen_tcp:send(LS1, "data"),
?line {error, R_recv} = gen_tcp:recv(LS1, 17),
?line {error, R_accept} = gen_tcp:accept(LS1),
?line {error, R_controlling_process} =
gen_tcp:controlling_process(LS1, self()),
%%
?line ok = io:format("R_send = ~p~n", [R_send]),
?line ok = io:format("R_recv = ~p~n", [R_recv]),
?line ok = io:format("R_accept = ~p~n", [R_accept]),
?line ok = io:format("R_controlling_process = ~p~n",
[R_controlling_process]),
ok.
%%%
%%% Test using the gen_tcp:shutdown/2 function using a sort server.
%%%
shutdown_active(Config) when is_list(Config) ->
?line shutdown_common(true).
shutdown_passive(Config) when is_list(Config) ->
?line shutdown_common(false).
shutdown_common(Active) ->
?line P = sort_server(Active),
io:format("Sort server port: ~p\n", [P]),
?line do_sort(P, []),
?line do_sort(P, ["glurf"]),
?line do_sort(P, ["abc","nisse","dum"]),
?line do_sort(P, [lists:reverse(integer_to_list(I)) || I <- lists:seq(25, 255)]),
?line do_sort(P, [lists:reverse(integer_to_list(I)) || I <- lists:seq(77, 999)]),
?line do_sort(P, [lists:reverse(integer_to_list(I)) || I <- lists:seq(25, 55)]),
?line do_sort(P, []),
?line do_sort(P, ["apa"]),
?line do_sort(P, ["kluns","gorilla"]),
?line do_sort(P, [lists:reverse(integer_to_list(I)) || I <- lists:seq(25, 1233)]),
?line do_sort(P, []),
receive
Any ->
?t:fail({unexpected_message,Any})
after 0 -> ok
end.
do_sort(P, List0) ->
List = [El++"\n" || El <- List0],
{ok,S} = gen_tcp:connect(localhost, P, [{packet,line}]),
send_lines(S, List),
gen_tcp:shutdown(S, write),
Lines = collect_lines(S, true),
io:format("~p\n", [Lines]),
Lines = lists:sort(List),
ok = gen_tcp:close(S).
sort_server(Active) ->
Opts = [{exit_on_close,false},{packet,line},{active,Active}],
?line {ok,L} = gen_tcp:listen(0, Opts),
Go = make_ref(),
?line Pid = spawn_link(fun() ->
receive Go -> sort_server_1(L, Active) end
end),
?line ok = gen_tcp:controlling_process(L, Pid),
?line Pid ! Go,
?line {ok,Port} = inet:port(L),
Port.
sort_server_1(L, Active) ->
{ok,S} = gen_tcp:accept(L),
Go = make_ref(),
Sorter = spawn(fun() -> receive Go -> sorter(S, Active) end end),
ok = gen_tcp:controlling_process(S, Sorter),
Sorter ! Go,
sort_server_1(L, Active).
sorter(S, Active) ->
Lines = collect_lines(S, Active),
send_lines(S, lists:sort(Lines)),
gen_tcp:shutdown(S, write),
gen_tcp:close(S).
collect_lines(S, true) ->
collect_lines_1(S, []);
collect_lines(S, false) ->
passive_collect_lines_1(S, []).
collect_lines_1(S, Acc) ->
receive
{tcp,S,Line} -> collect_lines_1(S, [Line|Acc]);
{tcp_closed,S} -> lists:reverse(Acc)
end.
passive_collect_lines_1(S, Acc) ->
case gen_tcp:recv(S, 0) of
{ok,Line} -> passive_collect_lines_1(S, [Line|Acc]);
{error,closed} -> lists:reverse(Acc)
end.
send_lines(S, Lines) ->
lists:foreach(fun(Line) ->
gen_tcp:send(S, Line)
end, Lines).
%%%
%%% Shutdown pending.
%%%
shutdown_pending(Config) when is_list(Config) ->
N = 512*1024+17,
io:format("~p\n", [N]),
Data = [<<N:32>>,ones(N),42],
P = a_server(),
io:format("Server port: ~p\n", [P]),
?line {ok,S} = gen_tcp:connect(localhost, P, []),
?line gen_tcp:send(S, Data),
?line gen_tcp:shutdown(S, write),
?line receive
{tcp,S,Msg} ->
io:format("~p\n", [Msg]),
?line N = list_to_integer(Msg) - 5;
Other ->
?t:fail({unexpected,Other})
end,
ok.
ones(0) -> [];
ones(1) -> [1];
ones(N) ->
Half = N div 2,
Ones = ones(Half),
case 2*Half of
N -> [Ones|Ones];
_ -> [1,Ones|Ones]
end.
a_server() ->
?line {ok,L} = gen_tcp:listen(0, [{exit_on_close,false},{active,false}]),
?line Pid = spawn_link(fun() -> a_server(L) end),
?line ok = gen_tcp:controlling_process(L, Pid),
?line {ok,Port} = inet:port(L),
Port.
a_server(L) ->
{ok,S} = gen_tcp:accept(L),
do_recv(S, []).
do_recv(S, Bs0) ->
case gen_tcp:recv(S, 0) of
{ok,B} ->
do_recv(S, [Bs0,B]);
{error,closed} ->
Bs = list_to_binary(Bs0),
gen_tcp:send(S, integer_to_list(byte_size(Bs))),
gen_tcp:close(S)
end.
%% Thanks to Luke Gorrie. Tests for a very specific problem with
%% corrupt data. The testcase will be killed by the timetrap timeout
%% if the bug is present.
http_bad_packet(Config) when is_list(Config) ->
?line {ok,L} = gen_tcp:listen(0,
[{active, false},
binary,
{reuseaddr, true},
{packet, http}]),
?line {ok,Port} = inet:port(L),
?line spawn_link(fun() -> erlang:yield(), http_bad_client(Port) end),
?line case gen_tcp:accept(L) of
{ok,S} ->
http_worker(S);
Err ->
exit({accept,Err})
end.
http_worker(S) ->
case gen_tcp:recv(S, 0, 30000) of
{ok,{http_error,Error}} ->
io:format("Http error: ~s\n", [Error]);
{ok,Data} ->
io:format("Data: ~p\n", [Data]),
http_worker(S)
end.
http_bad_client(Port) ->
{ok,S} = gen_tcp:connect("localhost", Port, [{active,false}, binary]),
ok = gen_tcp:send(S, "\r\n"),
ok = gen_tcp:close(S).
%% Fill send queue and then start receiving.
%%
busy_send(Config) when is_list(Config) ->
?line Master = self(),