forked from erlang/otp
/
port_SUITE.erl
2295 lines (2044 loc) · 70.1 KB
/
port_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 1997-2010. 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(port_SUITE).
%%%
%%% Author: Bjorn Gustavsson; iter_max_ports contributed by Peter Hogfeldt.
%%%
%%
%% There are a lot of things to test with open_port(Name, Settings).
%%
%% Name can be
%%
%% {spawn, Command}
%% which according to The Book and the manual page starts an
%% external program. That is not true. It might very well be
%% a linked-in program (the notion of 'linked-in driver' is
%% silly, since any driver is 'linked-in').
%% [Spawn of external program is tested.]
%%
%% Atom
%% Read all contents of Atom, or write to it.
%%
%% {fd, In, Out}
%% Open file descriptors In and Out. [Not tested]
%%
%% PortSettings can be
%%
%% {packet, N}
%% N is 1, 2 or 4.
%%
%% stream (default)
%% Without packet length.
%%
%% use_stdio (default for spawned ports)
%% The spawned process use file descriptors 0 and 1 for I/O.
%%
%% nouse_stdio [Not tested]
%% Use filedescriptors 3 and 4. This option is probably only
%% meaningful on Unix.
%%
%% in (default for Atom)
%% Input only (from Erlang's point of view).
%%
%% out
%% Output only (from Erlang's point of view).
%%
%% binary
%% The port is a binary port, i.e. messages received and sent
%% to a port are binaries.
%%
%% eof
%% Port is not closed on eof and will not send an exit signal,
%% instead it will send a {Port, eof} to the controlling process
%% (output can still be sent to the port (??)).
%%
-export([all/1, init_per_testcase/2, fin_per_testcase/2,
init_per_suite/1, end_per_suite/1,
stream/1, stream_small/1, stream_big/1,
basic_ping/1, slow_writes/1, bad_packet/1, bad_port_messages/1,
multiple_packets/1, mul_basic/1, mul_slow_writes/1,
dying_port/1, port_program_with_path/1,
open_input_file_port/1, open_output_file_port/1,
iter_max_ports/1, eof/1, input_only/1, output_only/1,
name1/1,
t_binary/1, options/1, parallell/1, t_exit/1,
env/1, bad_env/1, cd/1, exit_status/1,
tps/1, tps_16_bytes/1, tps_1K/1, line/1, stderr_to_stdout/1,
otp_3906/1, otp_4389/1, win_massive/1, win_massive_client/1,
mix_up_ports/1, otp_5112/1, otp_5119/1, otp_6224/1,
exit_status_multi_scheduling_block/1, ports/1,
spawn_driver/1,spawn_executable/1,
unregister_name/1]).
-export([]).
%% Internal exports.
-export([tps/3]).
-export([otp_3906_forker/5, otp_3906_start_forker_starter/4]).
-export([env_slave_main/1]).
-include("test_server.hrl").
-include_lib("kernel/include/file.hrl").
all(suite) ->
[
otp_6224, stream, basic_ping, slow_writes, bad_packet,
bad_port_messages, options, multiple_packets, parallell,
dying_port, port_program_with_path,
open_input_file_port, open_output_file_port,
name1,
env, bad_env, cd, exit_status,
iter_max_ports, t_exit, tps, line, stderr_to_stdout,
otp_3906, otp_4389, win_massive, mix_up_ports,
otp_5112, otp_5119,
exit_status_multi_scheduling_block,
ports, spawn_driver, spawn_executable,
unregister_name
].
-define(DEFAULT_TIMEOUT, ?t:minutes(5)).
init_per_testcase(Case, Config) ->
[{testcase, Case} |Config].
fin_per_testcase(_Case, _Config) ->
ok.
init_per_suite(Config) when is_list(Config) ->
ignore_cores:init(Config).
end_per_suite(Config) when is_list(Config) ->
ignore_cores:fini(Config).
-define(WIN_MASSIVE_PORT, 50000).
%% Tests that you can open a massive amount of ports (sockets)
%% on a Windows machine given the correct environment.
win_massive(Config) when is_list(Config) ->
case os:type() of
{win32,_} ->
do_win_massive();
_ ->
{skip,"Only on Windows."}
end.
do_win_massive() ->
?line Dog = test_server:timetrap(test_server:seconds(360)),
?line SuiteDir = filename:dirname(code:which(?MODULE)),
?line Env = " -env ERL_MAX_PORTS 8192",
?line {ok, Node} =
test_server:start_node(win_massive,
slave,
[{args, " -pa " ++ SuiteDir ++ Env}]),
?line ok = rpc:call(Node,?MODULE,win_massive_client,[3000]),
?line test_server:stop_node(Node),
?line test_server:timetrap_cancel(Dog),
ok.
win_massive_client(N) ->
{ok,P}=gen_tcp:listen(?WIN_MASSIVE_PORT,[{reuseaddr,true}]),
L = win_massive_loop(P,N),
Len = length(L),
lists:foreach(fun(E) ->
gen_tcp:close(E)
end,
L),
case Len div 2 of
N ->
ok;
_Else ->
{too_few, Len}
end.
win_massive_loop(_,0) ->
[];
win_massive_loop(P,N) ->
case (catch gen_tcp:connect("localhost",?WIN_MASSIVE_PORT,[])) of
{ok,A} ->
case (catch gen_tcp:accept(P)) of
{ok,B} ->
%erlang:display(N),
[A,B|win_massive_loop(P,N-1)];
_Else ->
[A]
end;
_Else0 ->
[]
end.
stream(suite) -> [stream_small, stream_big].
%% Test that we can send a stream of bytes and get it back.
%% We will send only a small amount of data, to avoid deadlock.
stream_small(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(10)),
?line stream_ping(Config, 512, "", []),
?line stream_ping(Config, 1777, "", []),
?line stream_ping(Config, 1777, "-s512", []),
?line test_server:timetrap_cancel(Dog),
ok.
%% Send big amounts of data (much bigger than the buffer size in port test).
%% This will deadlock the emulator if the spawn driver haven't proper
%% non-blocking reads and writes.
stream_big(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(180)),
case os:type() of
vxworks ->
%% Don't stress VxWorks too much
?line stream_ping(Config, 43755, "", []),
?line stream_ping(Config, 51255, "", []),
?line stream_ping(Config, 52345, " -s40000", []);
_ ->
?line stream_ping(Config, 43755, "", []),
?line stream_ping(Config, 100000, "", []),
?line stream_ping(Config, 77777, " -s40000", [])
end,
?line test_server:timetrap_cancel(Dog),
ok.
%% Sends packet with header size of 1, 2, and 4, with packets of various
%% sizes.
basic_ping(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(120)),
?line ping(Config, sizes(1), 1, "", []),
?line ping(Config, sizes(2), 2, "", []),
?line ping(Config, sizes(4), 4, "", []),
?line test_server:timetrap_cancel(Dog),
ok.
%% Let the port program insert delays between characters sent back to
%% Erlang, to test that the Erlang emulator can handle a packet coming in
%% small chunks rather than all at once.
slow_writes(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(20)),
?line ping(Config, [8], 4, "-s1", []),
?line ping(Config, [10], 2, "-s2", []),
?line test_server:timetrap_cancel(Dog),
ok.
bad_packet(doc) ->
["Test that we get {'EXIT', Port, einval} if we try to send a bigger "
"packet than the packet header allows."];
bad_packet(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(10)),
?line PortTest = port_test(Config),
?line process_flag(trap_exit, true),
?line bad_packet(PortTest, 1, 256),
?line bad_packet(PortTest, 1, 257),
?line bad_packet(PortTest, 2, 65536),
?line bad_packet(PortTest, 2, 65537),
?line test_server:timetrap_cancel(Dog),
ok.
bad_packet(PortTest, HeaderSize, PacketSize) ->
%% Intentionally no ?line macros.
P = open_port({spawn, PortTest}, [{packet, HeaderSize}]),
P ! {self(), {command, make_zero_packet(PacketSize)}},
receive
{'EXIT', P, einval} -> ok;
Other -> test_server:fail({unexpected_message, Other})
end.
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].
%% Test sending bad messages to a port.
bad_port_messages(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(10)),
?line PortTest = port_test(Config),
?line process_flag(trap_exit, true),
?line bad_message(PortTest, {a,b}),
?line bad_message(PortTest, {a}),
?line bad_message(PortTest, {self(),{command,bad_command}}),
?line bad_message(PortTest, {self(),{connect,no_pid}}),
?line test_server:timetrap_cancel(Dog),
ok.
bad_message(PortTest, Message) ->
P = open_port({spawn,PortTest}, []),
P ! Message,
receive
{'EXIT',P,badsig} -> ok;
Other -> test_server:fail({unexpected_message, Other})
end.
%% Tests various options (stream and {packet, Number} are implicitly
%% tested in other test cases).
options(suite) -> [t_binary, eof, input_only, output_only].
%% Tests the 'binary' option for a port.
t_binary(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(300)),
%% Packet mode.
?line ping(Config, sizes(1), 1, "", [binary]),
?line ping(Config, sizes(2), 2, "", [binary]),
?line ping(Config, sizes(4), 4, "", [binary]),
%% Stream mode.
case os:type() of
vxworks ->
%% don't stress VxWorks too much
?line stream_ping(Config, 435, "", [binary]),
?line stream_ping(Config, 43755, "", [binary]),
?line stream_ping(Config, 50000, "", [binary]);
_ ->
?line stream_ping(Config, 435, "", [binary]),
?line stream_ping(Config, 43755, "", [binary]),
?line stream_ping(Config, 100000, "", [binary])
end,
?line test_server:timetrap_cancel(Dog),
ok.
name1(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(100)),
?line PortTest = port_test(Config),
?line Command = lists:concat([PortTest, " "]),
?line P = open_port({spawn, Command}, []),
?line register(myport, P),
?line P = whereis(myport),
Text = "hej",
?line myport ! {self(), {command, Text}},
?line receive
{P, {data, Text}} ->
ok
end,
?line myport ! {self(), close},
?line receive
{P, closed} -> ok
end,
?line undefined = whereis(myport),
?line test_server:timetrap_cancel(Dog),
ok.
%% Test that the 'eof' option works.
eof(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(100)),
?line PortTest = port_test(Config),
?line Command = lists:concat([PortTest, " -h0 -q"]),
?line P = open_port({spawn, Command}, [eof]),
?line receive
{P, eof} ->
ok
end,
?line P ! {self(), close},
?line receive
{P, closed} -> ok
end,
?line test_server:timetrap_cancel(Dog),
ok.
%% Tests that the 'in' option for a port works.
input_only(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(300)),
?line expect_input(Config, [0, 1, 10, 13, 127, 128, 255], 1, "", [in]),
?line expect_input(Config, [0, 1, 255, 2048], 2, "", [in]),
?line expect_input(Config, [0, 1, 255, 2048], 4, "", [in]),
?line expect_input(Config, [0, 1, 10, 13, 127, 128, 255],
1, "", [in, binary]),
?line test_server:timetrap_cancel(Dog),
ok.
%% Tests that the 'out' option for a port works.
output_only(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(100)),
?line Dir = ?config(priv_dir, Config),
?line Filename = filename:join(Dir, "output_only_stream"),
?line output_and_verify(Config, Filename, "-h0",
random_packet(35777, "echo")),
?line test_server:timetrap_cancel(Dog),
ok.
output_and_verify(Config, Filename, Options, Data) ->
?line PortTest = port_test(Config),
?line Command = lists:concat([PortTest, " ",
Options, " -o", Filename]),
?line Port = open_port({spawn, Command}, [out]),
?line Port ! {self(), {command, Data}},
?line Port ! {self(), close},
?line receive
{Port, closed} -> ok
end,
Wait_time = case os:type() of
vxworks -> 5000;
_ -> 500
end,
?line test_server:sleep(Wait_time),
?line {ok, Written} = file:read_file(Filename),
?line Data = binary_to_list(Written),
ok.
%% Test that receiving several packages written in the same
%% write operation works.
multiple_packets(suite) -> [mul_basic, mul_slow_writes].
%% Basic test of receiving multiple packages, written in
%% one operation by the other end.
mul_basic(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(600)),
case os:type() of
vxworks ->
%% don't stress vxworks too much
?line expect_input(Config, [0, 1, 255, 10, 13], 1, "", []),
?line expect_input(Config, [0, 10, 13, 1600, 8191, 16383], 2, "", []),
?line expect_input(Config, [10, 35000], 4, "", []);
_ ->
?line expect_input(Config, [0, 1, 255, 10, 13], 1, "", []),
?line expect_input(Config, [0, 10, 13, 1600, 32767, 65535], 2, "", []),
?line expect_input(Config, [10, 70000], 4, "", [])
end,
?line test_server:timetrap_cancel(Dog),
ok.
%% Test reading a buffer consisting of several packets, some
%% of which might be incomplete. (The port program builds
%% a buffer with several packets, but writes it in chunks with
%% delays in between.)
mul_slow_writes(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(250)),
?line expect_input(Config, [0, 20, 255, 10, 1], 1, "-s64", []),
?line test_server:timetrap_cancel(Dog),
ok.
%% Runs several port tests in parallell. Each individual test
%% finishes in about 5 seconds. Running in parallell, all tests
%% should also finish in about 5 seconds.
parallell(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(300)),
?line Testers =
[fun() -> stream_ping(Config, 1007, "-s100", []) end,
fun() -> stream_ping(Config, 10007, "-s1000", []) end,
fun() -> stream_ping(Config, 10007, "-s1000", []) end,
fun() -> expect_input(Config, [21, 22, 23, 24, 25], 1,
"-s10", [in]) end,
fun() -> ping(Config, [10], 1, "-d", []) end,
fun() -> ping(Config, [20000], 2, "-d", []) end,
fun() -> ping(Config, [101], 1, "-s10", []) end,
fun() -> ping(Config, [1001], 2, "-s100", []) end,
fun() -> ping(Config, [10001], 4, "-s1000", []) end,
fun() -> ping(Config, [501, 501], 2, "-s100", []) end,
fun() -> ping(Config, [11, 12, 13, 14, 11], 1, "-s5", []) end],
?line process_flag(trap_exit, true),
?line Pids = lists:map(fun fun_spawn/1, Testers),
?line wait_for(Pids),
?line test_server:timetrap_cancel(Dog),
ok.
wait_for([]) ->
ok;
wait_for(Pids) ->
io:format("Waiting for ~p", [Pids]),
receive
{'EXIT', Pid, normal} ->
wait_for(lists:delete(Pid, Pids));
Other ->
test_server:fail({bad_exit, Other})
end.
%% Tests starting port programs that terminate by themselves.
%% This used to cause problems on Windows.
dying_port(suite) -> [];
dying_port(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(150)),
?line process_flag(trap_exit, true),
?line P1 = make_dying_port(Config),
?line P2 = make_dying_port(Config),
?line P3 = make_dying_port(Config),
?line P4 = make_dying_port(Config),
?line P5 = make_dying_port(Config),
%% This should be big enough to be sure to block in the write.
?line Garbage = random_packet(16384),
?line P1 ! {self(), {command, Garbage}},
?line P3 ! {self(), {command, Garbage}},
?line P5 ! {self(), {command, Garbage}},
?line wait_for_port_exit(P1),
?line wait_for_port_exit(P2),
?line wait_for_port_exit(P3),
?line wait_for_port_exit(P4),
?line wait_for_port_exit(P5),
?line test_server:timetrap_cancel(Dog),
ok.
wait_for_port_exit(Port) ->
receive
{'EXIT', Port, _} ->
ok
end.
make_dying_port(Config) when is_list(Config) ->
PortTest = port_test(Config),
Command = lists:concat([PortTest, " -h0 -d -q"]),
open_port({spawn, Command}, [stream]).
%% Tests that port program with complete path (but without any
%% .exe extension) can be started, even if there is a file with
%% the same name but without the extension in the same directory.
%% (In practice, the file with the same name could be a Unix
%% executable.)
%%
%% This used to failed on Windows (the .exe extension had to be
%% explicitly given).
%%
%% This testcase works on Unix, but is not very useful.
port_program_with_path(suite) -> [];
port_program_with_path(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(100)),
?line DataDir = ?config(data_dir, Config),
?line PrivDir = ?config(priv_dir, Config),
%% Create a copy of the port test program in a directory not
%% included in PATH (i.e. in priv_dir), with the name 'my_port_test.exe'.
%% Also, place a file named 'my_port_test' in the same directory.
%% This used to confuse the CreateProcess() call in spawn driver.
%% (On Unix, there will be a single file created, which will be
%% a copy of the port program.)
?line PortTest = os:find_executable("port_test", DataDir),
io:format("os:find_executable(~p, ~p) returned ~p",
["port_test", DataDir, PortTest]),
?line {ok, PortTestPgm} = file:read_file(PortTest),
?line NewName = filename:join(PrivDir, filename:basename(PortTest)),
?line RedHerring = filename:rootname(NewName),
?line ok = file:write_file(RedHerring, "I'm just here to confuse.\n"),
?line ok = file:write_file(NewName, PortTestPgm),
?line ok = file:write_file_info(NewName, #file_info{mode=8#111}),
?line PgmWithPathAndNoExt = filename:rootname(NewName),
%% Open the port using the path to the copied port test program,
%% but without the .exe extension, and verified that it was started.
%%
%% If the bug is present the open_port call will fail with badarg.
?line Command = lists:concat([PgmWithPathAndNoExt, " -h2"]),
%% allow VxWorks time to write file
case os:type() of
vxworks -> test_server:sleep(2500);
_ -> time
end,
?line P = open_port({spawn, Command}, [{packet, 2}]),
?line Message = "echo back to me",
?line P ! {self(), {command, Message}},
?line receive
{P, {data, Message}} ->
ok
end,
?line test_server:timetrap_cancel(Dog),
ok.
%% Tests that files can be read using open_port(Filename, [in]).
%% This used to fail on Windows.
open_input_file_port(suite) -> [];
open_input_file_port(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(10)),
?line PrivDir = ?config(priv_dir, Config),
%% Create a file with the file driver and read it back using
%% open_port/2.
?line MyFile1 = filename:join(PrivDir, "my_input_file"),
?line FileData1 = "An input file",
?line ok = file:write_file(MyFile1, FileData1),
case os:type() of
vxworks ->
%% Can't open input file with vanilla driver on VxWorks
?line process_flag(trap_exit, true),
?line case catch open_port(MyFile1, [in]) of
{'EXIT', {badarg, _}} ->
ok
end;
_ ->
?line case open_port(MyFile1, [in]) of
InputPort when is_port(InputPort) ->
?line receive
{InputPort, {data, FileData1}} ->
ok
end
end
end,
?line test_server:timetrap_cancel(Dog),
ok.
%% Tests that files can be written using open_port(Filename, [out]).
open_output_file_port(suite) -> [];
open_output_file_port(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(100)),
?line PrivDir = ?config(priv_dir, Config),
%% Create a file with open_port/2 and read it back with
%% the file driver.
?line MyFile2 = filename:join(PrivDir, "my_output_file"),
?line FileData2_0 = "A file created ",
?line FileData2_1 = "with open_port/2.\n",
?line FileData2 = FileData2_0 ++ FileData2_1,
?line OutputPort = open_port(MyFile2, [out]),
?line OutputPort ! {self(), {command, FileData2_0}},
?line OutputPort ! {self(), {command, FileData2_1}},
?line OutputPort ! {self(), close},
?line {ok, Bin} = file:read_file(MyFile2),
?line FileData2 = binary_to_list(Bin),
?line test_server:timetrap_cancel(Dog),
ok.
%%
%% Open as many ports as possible. Do this several times and check
%% that we get the same number of ports every time.
%%
iter_max_ports(suite) -> [];
iter_max_ports(Config) when is_list(Config) ->
%% The child_setup program might dump core if we get out of memory.
%% This is hard to do anything about and is harmless. We run this test
%% in a working directory with an ignore_core_files file which will make
%% the search for core files ignore cores generated by this test.
%%
Config2 = ignore_cores:setup(?MODULE, iter_max_ports, Config, true),
try
iter_max_ports_test(Config2)
after
ignore_cores:restore(Config2)
end.
iter_max_ports_test(Config) ->
?line Dog = test_server:timetrap(test_server:minutes(20)),
?line PortTest = port_test(Config),
?line Command = lists:concat([PortTest, " -h0 -q"]),
?line Iters = case os:type() of
{win32,_} -> 4;
_ -> 10
end,
?line L = do_iter_max_ports(Iters, Command),
io:format("Result: ~p",[L]),
?line all_equal(L),
?line test_server:timetrap_cancel(Dog),
{comment, "Max ports: " ++ integer_to_list(hd(L))}.
do_iter_max_ports(N, Command) when N > 0 ->
[max_ports(Command)| do_iter_max_ports(N-1, Command)];
do_iter_max_ports(_, _) ->
[].
all_equal([E,E|T]) ->
all_equal([E|T]);
all_equal([_]) -> ok;
all_equal([]) -> ok.
max_ports(Command) ->
test_server:sleep(500),
?line Ps = open_ports({spawn, Command}, [eof]),
?line N = length(Ps),
?line close_ports(Ps),
io:format("Got ~p ports\n",[N]),
N.
close_ports([P|Ps]) ->
P ! {self(), close},
receive
{P,closed} ->
ok
end,
close_ports(Ps);
close_ports([]) ->
ok.
open_ports(Name, Settings) ->
test_server:sleep(50),
case catch open_port(Name, Settings) of
P when is_port(P) ->
[P| open_ports(Name, Settings)];
{'EXIT', {Code, _}} ->
case Code of
enfile ->
[];
emfile ->
[];
system_limit ->
[];
Other ->
?line test_server:fail({open_ports, Other})
end;
Other ->
?line test_server:fail({open_ports, Other})
end.
%% Tests that exit(Port, Term) works (has been known to crash the emulator).
t_exit(suite) -> [];
t_exit(Config) when is_list(Config) ->
?line process_flag(trap_exit, true),
?line Pid = fun_spawn(fun suicide_port/1, [Config]),
?line receive
{'EXIT', Pid, die} ->
ok;
Other ->
test_server:fail({bad_message, Other})
end.
suicide_port(Config) when is_list(Config) ->
?line Port = port_expect(Config, [], 0, "", []),
?line exit(Port, die),
?line receive after infinity -> ok end.
tps(suite) -> [tps_16_bytes, tps_1K].
tps_16_bytes(doc) -> "";
tps_16_bytes(suite) -> [];
tps_16_bytes(Config) when is_list(Config) ->
?line tps(16, Config).
tps_1K(doc) -> "";
tps_1K(suite) -> [];
tps_1K(Config) when is_list(Config) ->
?line tps(1024, Config).
tps(Size, Config) ->
?line Dog = test_server:timetrap(test_server:seconds(300)),
?line PortTest = port_test(Config),
?line Packet = list_to_binary(random_packet(Size, "e")),
?line Port = open_port({spawn, PortTest}, [binary, {packet, 2}]),
?line Transactions = 10000,
?line {Elapsed, ok} = test_server:timecall(?MODULE, tps,
[Port, Packet, Transactions]),
?line test_server:timetrap_cancel(Dog),
{comment, integer_to_list(trunc(Transactions/Elapsed+0.5)) ++ " transactions/s"}.
tps(_Port, _Packet, 0) -> ok;
tps(Port, Packet, N) ->
?line port_command(Port, Packet),
?line receive
{Port, {data, Packet}} ->
?line tps(Port, Packet, N-1);
Other ->
?line test_server:fail({bad_message, Other})
end.
%% Line I/O test
line(Config) when is_list(Config) ->
?line Siz = 110,
?line Dog = test_server:timetrap(test_server:seconds(300)),
?line Packet1 = random_packet(Siz),
?line Packet2 = random_packet(Siz div 2),
%% Test that packets are split into lines
?line port_expect(Config,[{lists:append([Packet1, io_lib:nl(), Packet2,
io_lib:nl()]),
[{eol, Packet1}, {eol, Packet2}]}],
0, "", [{line,Siz}]),
%% Test the same for binaries
?line port_expect(Config,[{lists:append([Packet1, io_lib:nl(), Packet2,
io_lib:nl()]),
[{eol, Packet1}, {eol, Packet2}]}],
0, "", [{line,Siz},binary]),
%% Test that too long lines get split
?line port_expect(Config,[{lists:append([Packet1, io_lib:nl(), Packet1,
Packet2, io_lib:nl()]),
[{eol, Packet1}, {noeol, Packet1},
{eol, Packet2}]}], 0, "", [{line,Siz}]),
%% Test that last output from closing port program gets received.
?line L1 = lists:append([Packet1, io_lib:nl(), Packet2]),
?line S1 = lists:flatten(io_lib:format("-l~w", [length(L1)])),
io:format("S1 = ~w, L1 = ~w~n", [S1,L1]),
?line port_expect(Config,[{L1,
[{eol, Packet1}, {noeol, Packet2}, eof]}], 0,
S1, [{line,Siz},eof]),
%% Test that lonely <CR> Don't get treated as newlines
?line port_expect(Config,[{lists:append([Packet1, [13], Packet2,
io_lib:nl()]),
[{noeol, Packet1}, {eol, [13 |Packet2]}]}],
0, "", [{line,Siz}]),
%% Test that packets get built up to lines (delayed output from
%% port program)
?line port_expect(Config,[{Packet2,[]},
{lists:append([Packet2, io_lib:nl(),
Packet1, io_lib:nl()]),
[{eol, lists:append(Packet2, Packet2)},
{eol, Packet1}]}], 0, "-d", [{line,Siz}]),
%% Test that we get badarg if trying both packet and line
?line bad_argument(Config, [{packet, 5}, {line, 5}]),
?line test_server:timetrap_cancel(Dog),
ok.
%%% Redirection of stderr test
stderr_to_stdout(suite) ->
[];
stderr_to_stdout(doc) ->
"Test that redirection of standard error to standard output works.";
stderr_to_stdout(Config) when is_list(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(60)),
%% See that it works
?line Packet = random_packet(10),
?line port_expect(Config,[{Packet,[Packet]}], 0, "-e -l10",
[stderr_to_stdout]),
%% ?line stream_ping(Config, 10, "-e", [stderr_to_stdout]),
%% See that it doesn't always happen (will generate garbage on stderr)
?line port_expect(Config,[{Packet,[eof]}], 0, "-e -l10", [line,eof]),
?line test_server:timetrap_cancel(Dog),
ok.
bad_argument(Config, ArgList) ->
PortTest = port_test(Config),
case catch open_port({spawn, PortTest}, ArgList) of
{'EXIT', {badarg, _}} ->
ok
end.
%% 'env' option
%% (Can perhaps be made smaller by calling the other utility functions
%% in this module.)
env(suite) ->
[];
env(doc) ->
["Test that the 'env' option works"];
env(Config) when is_list(Config) ->
case os:type() of
vxworks ->
{skipped,"Environments not implemented on VxWorks (could be...)"};
_ ->
env2(Config)
end.
env2(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(60)),
?line Priv = ?config(priv_dir, Config),
?line Temp = filename:join(Priv, "env_fun.bin"),
PluppVal = "dirty monkey",
?line env_slave(Temp, [{"plupp",PluppVal}]),
Long = "LongAndBoringEnvName",
?line os:putenv(Long, "nisse"),
?line env_slave(Temp, [{"plupp",PluppVal},
{"DIR_PLUPP","###glurfrik"}],
fun() ->
PluppVal = os:getenv("plupp"),
"###glurfrik" = os:getenv("DIR_PLUPP"),
"nisse" = os:getenv(Long)
end),
?line env_slave(Temp, [{"must_define_something","some_value"},
{"certainly_not_existing",false},
{"ends_with_equal", "value="},
{Long,false},
{"glurf","a glorfy string"}]),
?line test_server:timetrap_cancel(Dog),
ok.
env_slave(File, Env) ->
F = fun() ->
lists:foreach(fun({Name,Val}) ->
Val = os:getenv(Name)
end, Env)
end,
env_slave(File, Env, F).
env_slave(File, Env, Body) ->
file:write_file(File, term_to_binary(Body)),
Program = atom_to_list(lib:progname()),
Dir = filename:dirname(code:which(?MODULE)),
Cmd = Program ++ " -pz " ++ Dir ++
" -noinput -run " ++ ?MODULE_STRING ++ " env_slave_main " ++
File ++ " -run erlang halt",
Port = open_port({spawn, Cmd}, [{env,Env},{line,256}]),
receive
{Port,{data,{eol,"ok"}}} ->
ok;
{Port,{data,{eol,Error}}} ->
io:format("~p\n", [Error]),
test_server:fail();
Other ->
test_server:fail(Other)
end.
env_slave_main([File]) ->
{ok,Body0} = file:read_file(File),
Body = binary_to_term(Body0),
case Body() of
{'EXIT',Reason} ->
io:format("Error: ~p\n", [Reason]);
_ ->
io:format("ok\n")
end,
init:stop().
%% 'env' option
%% Test bad environments.
bad_env(Config) when is_list(Config) ->
case os:type() of
vxworks ->
{skipped,"Environments not implemented on VxWorks"};
_ ->
bad_env_1()
end.
bad_env_1() ->
?line try_bad_env([abbb]),
?line try_bad_env([{"key","value"}|{"another","value"}]),
?line try_bad_env([{"key","value","value2"}]),
?line try_bad_env([{"key",[a,b,c]}]),
?line try_bad_env([{"key",value}]),
?line try_bad_env({a,tuple}),
?line try_bad_env(42),
?line try_bad_env([a|b]),
?line try_bad_env(self()),
ok.
try_bad_env(Env) ->
try open_port({spawn,"ls"}, [{env,Env}])
catch
error:badarg -> ok
end.
%% 'cd' option
%% (Can perhaps be made smaller by calling the other utility functions
%% in this module.)
cd(suite) ->
[];
cd(doc) ->
["Test that the 'cd' option works"];
cd(Config) when is_list(Config) ->
case os:type() of
vxworks ->
{skipped,"Task specific directories does not exist on VxWorks"};
_ ->
cd2(Config)
end.
cd2(Config) ->
?line Dog = test_server:timetrap(test_server:seconds(60)),
?line Program = atom_to_list(lib:progname()),
?line DataDir = ?config(data_dir, Config),
?line TestDir = filename:join(DataDir, "dir"),
?line Cmd = Program ++ " -pz " ++ DataDir ++
" -noshell -s port_test pwd -s erlang halt",
?line _ = open_port({spawn, Cmd},
[{cd, TestDir},
{line, 256}]),
?line receive
{_, {data, {eol, String}}} ->
case filename_equal(String, TestDir) of
true ->
ok;
false ->
?line test_server:fail({cd, String})
end;
Other2 ->
?line test_server:fail({env, Other2})
end,
?line test_server:timetrap_cancel(Dog),
ok.
filename_equal(A, B) ->
case os:type() of
{win32, _} ->
win_filename_equal(A, B);