aboutsummaryrefslogblamecommitdiffstats
path: root/erts/emulator/test/port_SUITE.erl
blob: 93f9534138385f0e01014e007152b20c6a2a34f5 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302

                   


                                                        




                                                                      
  



                                                                         
  









































































                                                                           
                                                               
                             






















                                                              
                                                            
                    


























































































































































































































































































































































































































































































































































































































































































































































































                                                                                     
    
                                                                  
                                                             
                                                         


                                                         






                                                                                    







































































































































































































































































































































































































































































































































































                                                                                                        



                                                                      



















































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                            

 

                                                                                              

                                               


                                                               


                                                                 
 


                                           




                                                       
                                                                     




                                                                         
                   



                                                                                             
%%
%% %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, close_deaf_port/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, close_deaf_port,
     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"}]),

    %% A lot of non existing variables (mingled with existing)
    NotExistingList = [{lists:flatten(io_lib:format("V~p_not_existing",[X])),false} 
                        ||  X <- lists:seq(1,150)],
    ExistingList = [{lists:flatten(io_lib:format("V~p_existing",[X])),"a_value"} 
                        ||  X <- lists:seq(1,150)],
    ?line env_slave(Temp, lists:sort(ExistingList ++ NotExistingList)),

    ?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);
	_ ->
	    A == B
    end.

win_filename_equal([], []) ->
    true;
win_filename_equal([], _) ->
    false;
win_filename_equal(_, []) ->
    false;
win_filename_equal([C1 | Rest1], [C2 | Rest2]) ->
    case tolower(C1) == tolower(C2) of
	true ->
	    win_filename_equal(Rest1, Rest2);
	false ->
	    false
    end.

tolower(C) when C >= $A, C =< $Z ->
    C + 32;
tolower(C) ->
    C.

otp_3906(suite) ->
    [];
otp_3906(doc) ->
    ["Tests that child process deaths are managed correctly when there are "
     " a large amount of concurrently dying children. See ticket OTP-3906."];
otp_3906(Config)  when is_list(Config) ->
    case os:type() of
	{unix, OSName} ->
	    otp_3906(Config, OSName);
	_ ->
	    {skipped, "Only run on Unix systems"}
    end.

-define(OTP_3906_CHILDREN,     1000).
-define(OTP_3906_EXIT_STATUS,  17).
-define(OTP_3906_PROGNAME,     "otp_3906").
-define(OTP_3906_TICK_TIMEOUT, 5000).
-define(OTP_3906_OSP_P_ERLP,   10).
-define(OTP_3906_MAX_CONC_OSP, 50).

otp_3906(Config, OSName) ->
    ?line TSDir = filename:dirname(code:which(test_server)),
    ?line {ok, Variables} = file:consult(filename:join(TSDir, "variables")),
    case lists:keysearch('CC', 1, Variables) of
	{value,{'CC', CC}} ->
	    SuiteDir = filename:dirname(code:which(?MODULE)),
	    PrivDir = ?config(priv_dir, Config),
	    Prog = otp_3906_make_prog(CC, PrivDir),
	    {ok, Node} = test_server:start_node(otp_3906,
						slave,
						[{args, " -pa " ++ SuiteDir},
						 {linked, false}]),
	    OP = process_flag(priority, max),
	    OTE = process_flag(trap_exit, true),
	    FS = spawn_link(Node,
			    ?MODULE,
			    otp_3906_start_forker_starter,
			    [?OTP_3906_CHILDREN, [], self(), Prog]),
	    Result = receive
			 {'EXIT', _ForkerStarter, Reason} ->
			     {failed, Reason};
			 {emulator_pid, EmPid} ->
			     case otp_3906_wait_result(FS, 0, 0) of
				 {succeded,
				  ?OTP_3906_CHILDREN,
				  ?OTP_3906_CHILDREN} ->
				     succeded;
				 {succeded, Forked, Exited} ->
				     otp_3906_list_defunct(EmPid, OSName),
				     {failed,
				      {mismatch,
				       {forked, Forked},
				       {exited, Exited}}};
				 Res ->
				     otp_3906_list_defunct(EmPid, OSName),
				     Res
			     end
		     end,
	    process_flag(trap_exit, OTE),
	    process_flag(priority, OP),
	    test_server:stop_node(Node),
	    case Result of
		succeded ->
		    ok;
		_ ->
		    ?line test_server:fail(Result)
	    end;
	_ ->
	    {skipped, "No C compiler found"}
    end.

otp_3906_list_defunct(EmPid, OSName) ->
    % Guess ps switches to use and what to grep for (could be improved)
    {Switches, Zombie} = case OSName of
			     BSD when BSD == darwin;
				      BSD == openbsd;
				      BSD == netbsd;
				      BSD == freebsd ->
				 {"-ajx", "Z"};
			     _ ->
				 {"-ef", "[dD]efunct"}
			 end,
    test_server:format("Emulator pid: ~s~n"
		       "Listing of zombie processes:~n"
		       "~s~n",
		       [EmPid,
			otp_3906_htmlize(os:cmd("ps "
						++ Switches
						++ " | grep "
						++ Zombie))]).

otp_3906_htmlize([]) ->
    [];
otp_3906_htmlize([C | Cs]) ->
    case [C] of
	"<" -> "&lt;" ++ otp_3906_htmlize(Cs);
	">" -> "&gt;" ++ otp_3906_htmlize(Cs);
	_ ->   [C | otp_3906_htmlize(Cs)]
    end.

otp_3906_make_prog(CC, PrivDir) ->
    SrcFileName = filename:join(PrivDir, ?OTP_3906_PROGNAME ++ ".c"),
    TrgtFileName = filename:join(PrivDir, ?OTP_3906_PROGNAME),
    {ok, SrcFile} = file:open(SrcFileName, write),
    io:format(SrcFile,
	      "int           ~n"
	      "main(void)    ~n"
	      "{             ~n"
	      "   return ~p; ~n"
	      "}             ~n",
	      [?OTP_3906_EXIT_STATUS]),
    file:close(SrcFile),
    os:cmd(CC ++ " " ++ SrcFileName ++ " -o " ++ TrgtFileName),
    TrgtFileName.


otp_3906_wait_result(ForkerStarter, F, E) ->
    receive
	{'EXIT', ForkerStarter, Reason} ->
	    {failed, {Reason, {forked, F}, {exited, E}}};
	forked ->
	    otp_3906_wait_result(ForkerStarter, F+1, E);
	exited ->
	    otp_3906_wait_result(ForkerStarter, F, E+1);
	tick ->
	    otp_3906_wait_result(ForkerStarter, F, E);
	succeded ->
	    {succeded, F, E}
    after
	?OTP_3906_TICK_TIMEOUT ->
	    unlink(ForkerStarter),
	    exit(ForkerStarter, timeout),
	    {failed, {timeout, {forked, F}, {exited, E}}}
    end.

otp_3906_collect([], _) ->
    done;
otp_3906_collect(RefList, Sup) ->
    otp_3906_collect(otp_3906_collect_one(RefList, Sup), Sup).

otp_3906_collect_one(RefList, Sup) ->
    receive
	Ref when is_reference(Ref) ->
	    Sup ! tick,
	    lists:delete(Ref, RefList)
    end.
    
otp_3906_start_forker(N, Sup, Prog) ->
    Ref = make_ref(),
    spawn_opt(?MODULE,
	      otp_3906_forker,
	      [N, self(), Ref, Sup, Prog],
	      [link, {priority, max}]),
    Ref.

otp_3906_start_forker_starter(N, RefList, Sup, Prog) ->
    process_flag(priority, max),
    EmPid = os:getpid(),
    Sup ! {emulator_pid, EmPid},
    otp_3906_forker_starter(N, RefList, Sup, Prog).

otp_3906_forker_starter(0, RefList, Sup, _) ->
    otp_3906_collect(RefList, Sup),
    unlink(Sup),
    Sup ! succeded;
otp_3906_forker_starter(N, RefList, Sup, Prog)
  when length(RefList) >= ?OTP_3906_MAX_CONC_OSP ->
    otp_3906_forker_starter(N, otp_3906_collect_one(RefList, Sup), Sup, Prog);
otp_3906_forker_starter(N, RefList, Sup, Prog)
  when is_integer(N), N > ?OTP_3906_OSP_P_ERLP ->
    otp_3906_forker_starter(N-?OTP_3906_OSP_P_ERLP,
			    [otp_3906_start_forker(?OTP_3906_OSP_P_ERLP,
						   Sup,
						   Prog)|RefList],
			    Sup,
			    Prog);
otp_3906_forker_starter(N, RefList, Sup, Prog) when is_integer(N) ->
    otp_3906_forker_starter(0,
			    [otp_3906_start_forker(N,
						   Sup,
						   Prog)|RefList],
			    Sup,
			    Prog).

otp_3906_forker(0, Parent, Ref, _, _) ->
    unlink(Parent),
    Parent ! Ref;
otp_3906_forker(N, Parent, Ref, Sup, Prog) ->
    Port = erlang:open_port({spawn, Prog}, [exit_status, in]),
    Sup ! forked,
    receive
	{Port, {exit_status, ?OTP_3906_EXIT_STATUS}} ->
	    Sup ! exited,
	    otp_3906_forker(N-1, Parent, Ref, Sup, Prog);
	{Port, Res} ->
	    exit(Res);
	Other ->
	    exit(Other)
    end.


otp_4389(suite) -> [];
otp_4389(doc) -> [];
otp_4389(Config)  when is_list(Config) ->
    case {os:type(),erlang:system_info(heap_type)} of
	{{unix, _},private} ->
	    ?line Dog = test_server:timetrap(test_server:seconds(240)),
	    ?line TCR = self(),
	    case get_true_cmd() of
		True when is_list(True) ->
		    ?line lists:foreach(
			    fun (P) ->
				    ?line receive
					      {P, ok} ->  ?line ok;
					      {P, Err} -> ?line ?t:fail(Err)
					  end
			    end,
			    lists:map(
			      fun(_) ->
				      spawn_link(
					fun() ->
						process_flag(trap_exit, true),
						case catch open_port({spawn, True},
								     [stream,exit_status]) of
						    P when is_port(P) ->
							receive
							    {P,{exit_status,_}} ->
								TCR ! {self(),ok};
							    {'EXIT',_,{R2,_}} when R2 == emfile;
										   R2 == eagain ->
								TCR ! {self(),ok};
							    Err2 ->
								TCR ! {self(),{msg,Err2}}
							end;
						    {'EXIT',{R1,_}} when R1 == emfile;
									 R1 == eagain ->
							TCR ! {self(),ok};
						    Err1 ->
							TCR ! {self(), {open_port,Err1}}
						end
					end)
			      end,
			      lists:duplicate(1000,[]))),
		    ?line test_server:timetrap_cancel(Dog),
		    {comment,
		     "This test case doesn't always fail when the bug that "
		     "it tests for is present (it is most likely to fail on"
		     " a multi processor machine). If the test case fails it"
		     " will fail by deadlocking the emulator."};
		_ ->
		    ?line {skipped, "\"true\" command not found"}
	    end;
	_ ->
	    {skip,"Only run on Unix and private heaps"}
    end.

get_true_cmd() ->
    DoFileExist = fun (FileName) ->
 			  case file:read_file_info(FileName) of
 			      {ok, _} -> throw(FileName);
 			      _ -> not_found
 			  end
 		  end,
    catch begin
 	      %% First check in /usr/bin and /bin
 	      DoFileExist("/usr/bin/true"),
 	      DoFileExist("/bin/true"),
 	      %% Try which
 	      case filename:dirname(os:cmd("which true")) of
 		  "." -> not_found;
 		  TrueDir -> filename:join(TrueDir, "true")
 	      end
 	  end.

%% 'exit_status' option
exit_status(suite) ->
    [];
exit_status(doc) ->
    ["Test that the 'exit_status' option works"];
exit_status(Config)  when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(60)),
    ?line port_expect(Config,[{"x",
			       [{exit_status, 5}]}],
		      1, "", [exit_status]),
    ?line test_server:timetrap_cancel(Dog),
    ok.

spawn_driver(suite) ->
    [];
spawn_driver(doc) ->
    ["Test spawning a driver specifically"];
spawn_driver(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(10)),
    ?line Path = ?config(data_dir, Config),
    ?line ok = load_driver(Path, "echo_drv"),
    ?line Port = erlang:open_port({spawn_driver, "echo_drv"}, []),
    ?line Port ! {self(), {command, "Hello port!"}},
    ?line receive 
	      {Port, {data, "Hello port!"}} = Msg1 -> 
		  io:format("~p~n", [Msg1]),
		  ok; 
	      Other ->
		  test_server:fail({unexpected, Other})
	  end,
    ?line Port ! {self(), close},
    ?line receive {Port, closed} -> ok end,

    ?line Port2 = erlang:open_port({spawn_driver, "echo_drv -Hello port?"}, 
				   []),
    ?line receive 
	      {Port2, {data, "Hello port?"}} = Msg2 -> 
		  io:format("~p~n", [Msg2]),
		  ok; 
	      Other2 ->
		  test_server:fail({unexpected2, Other2})
	  end,
    ?line Port2 ! {self(), close},
    ?line receive {Port2, closed} -> ok end,
    ?line {'EXIT',{badarg,_}} = (catch erlang:open_port({spawn_driver, "ls"}, [])),
    ?line {'EXIT',{badarg,_}} = (catch erlang:open_port({spawn_driver, "cmd"}, [])),
    ?line {'EXIT',{badarg,_}} = (catch erlang:open_port({spawn_driver, os:find_executable("erl")}, [])),
    ?line test_server:timetrap_cancel(Dog),
    ok.

spawn_executable(suite) ->
    [];
spawn_executable(doc) ->
    ["Test spawning an executable specifically"];
spawn_executable(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(10)),
    ?line DataDir = ?config(data_dir, Config),
    ?line EchoArgs1 = filename:join([DataDir,"echo_args"]),
    ?line ExactFile1 = filename:nativename(os:find_executable(EchoArgs1)),
    ?line [ExactFile1] = run_echo_args(DataDir,[]),
    ?line ["echo_args"] = run_echo_args(DataDir,["echo_args"]),
    ?line ["echo_arguments"] = run_echo_args(DataDir,["echo_arguments"]),
    ?line [ExactFile1,"hello world","dlrow olleh"] = 
	run_echo_args(DataDir,[ExactFile1,"hello world","dlrow olleh"]),
    ?line [ExactFile1] = run_echo_args(DataDir,[default]),
    ?line [ExactFile1,"hello world","dlrow olleh"] = 
	run_echo_args(DataDir,[switch_order,ExactFile1,"hello world",
			       "dlrow olleh"]),
    ?line [ExactFile1,"hello world","dlrow olleh"] = 
	run_echo_args(DataDir,[default,"hello world","dlrow olleh"]),

    ?line [ExactFile1,"hello world","dlrow olleh"] = 
	run_echo_args_2("\""++ExactFile1++"\" "++"\"hello world\" \"dlrow olleh\""),

    ?line PrivDir = ?config(priv_dir, Config),
    ?line SpaceDir =filename:join([PrivDir,"With Spaces"]),
    ?line file:make_dir(SpaceDir),
    ?line Executable = filename:basename(ExactFile1),
    ?line file:copy(ExactFile1,filename:join([SpaceDir,Executable])),
    ?line ExactFile2 = filename:nativename(filename:join([SpaceDir,Executable])),
    ?line chmodplusx(ExactFile2),
    io:format("|~s|~n",[ExactFile2]),
    ?line [ExactFile2] = run_echo_args(SpaceDir,[]),
    ?line ["echo_args"] = run_echo_args(SpaceDir,["echo_args"]),
    ?line ["echo_arguments"] = run_echo_args(SpaceDir,["echo_arguments"]),
    ?line [ExactFile2,"hello world","dlrow olleh"] = 
	run_echo_args(SpaceDir,[ExactFile2,"hello world","dlrow olleh"]),
    ?line [ExactFile2] = run_echo_args(SpaceDir,[default]),
    ?line [ExactFile2,"hello world","dlrow olleh"] = 
	run_echo_args(SpaceDir,[switch_order,ExactFile2,"hello world",
			       "dlrow olleh"]),
    ?line [ExactFile2,"hello world","dlrow olleh"] = 
	run_echo_args(SpaceDir,[default,"hello world","dlrow olleh"]),
    ?line [ExactFile2,"hello world","dlrow olleh"] = 
	run_echo_args_2("\""++ExactFile2++"\" "++"\"hello world\" \"dlrow olleh\""),

    ?line ExeExt = 
	case string:to_lower(lists:last(string:tokens(ExactFile2,"."))) of
	    "exe" ->
		".exe";
	    _ ->
		""
	end,
    Executable2 = "spoky name"++ExeExt,
    ?line file:copy(ExactFile1,filename:join([SpaceDir,Executable2])),
    ?line ExactFile3 = filename:nativename(filename:join([SpaceDir,Executable2])),
    ?line chmodplusx(ExactFile3),
    ?line [ExactFile3] = run_echo_args(SpaceDir,Executable2,[]),
    ?line ["echo_args"] = run_echo_args(SpaceDir,Executable2,["echo_args"]),
    ?line ["echo_arguments"] = run_echo_args(SpaceDir,Executable2,["echo_arguments"]),
    ?line [ExactFile3,"hello world","dlrow olleh"] = 
	run_echo_args(SpaceDir,Executable2,[ExactFile3,"hello world","dlrow olleh"]),
    ?line [ExactFile3] = run_echo_args(SpaceDir,Executable2,[default]),
    ?line [ExactFile3,"hello world","dlrow olleh"] = 
	run_echo_args(SpaceDir,Executable2,
		      [switch_order,ExactFile3,"hello world",
		       "dlrow olleh"]),
    ?line [ExactFile3,"hello world","dlrow olleh"] = 
	run_echo_args(SpaceDir,Executable2,
		      [default,"hello world","dlrow olleh"]),
    ?line [ExactFile3,"hello world","dlrow olleh"] = 
	run_echo_args_2("\""++ExactFile3++"\" "++"\"hello world\" \"dlrow olleh\""),
    ?line {'EXIT',{enoent,_}} = (catch run_echo_args(SpaceDir,"fnurflmonfi",
						     [default,"hello world",
						      "dlrow olleh"])),
    NonExec = "kronxfrt"++ExeExt,
    ?line file:write_file(filename:join([SpaceDir,NonExec]),
			  <<"Not an executable">>),
    ?line {'EXIT',{eacces,_}} = (catch run_echo_args(SpaceDir,NonExec,
						     [default,"hello world",
						      "dlrow olleh"])),
    ?line {'EXIT',{enoent,_}} = (catch open_port({spawn_executable,"cmd"},[])),
    ?line {'EXIT',{enoent,_}} = (catch open_port({spawn_executable,"sh"},[])),
    case os:type() of
	{win32,_} ->
	    test_bat_file(SpaceDir);
	{unix,_} ->
	    test_sh_file(SpaceDir)
    end,
    ?line test_server:timetrap_cancel(Dog),
    ok.

unregister_name(Config) when is_list(Config) ->
    ?line true = register(crash, open_port({spawn, "sleep 100"}, [])),
    ?line true = unregister(crash).

test_bat_file(Dir) ->
    FN = "tf.bat",
    Full = filename:join([Dir,FN]),
    D = [<<"@echo off\r\n">>,
	 <<"echo argv[0]:^|%0^|\r\n">>,
	 <<"if \"%1\" == \"\" goto done\r\n">>,
	 <<"echo argv[1]:^|%1^|\r\n">>,
	 <<"if \"%2\" == \"\" goto done\r\n">>,
	 <<"echo argv[2]:^|%2^|\r\n">>,
	 <<"if \"%3\" == \"\" goto done\r\n">>,
	 <<"echo argv[3]:^|%3^|\r\n">>,
	 <<"if \"%4\" == \"\" goto done\r\n">>,
	 <<"echo argv[4]:^|%4^|\r\n">>,
	 <<"if \"%5\" == \"\" goto done\r\n">>,
	 <<"echo argv[5]:^|%5^|\r\n">>,
	 <<"\r\n">>,
	 <<":done\r\n">>,
	 <<"\r\n">>],
    ?line file:write_file(Full,list_to_binary(D)),
    ?line EF = filename:basename(FN),
    ?line [DN,"hello","world"] = 
	run_echo_args(Dir,FN,
		      [default,"hello","world"]),
    %% The arg0 argumant should be ignored when running batch files
    ?line [DN,"hello","world"] = 
	run_echo_args(Dir,FN,
		      ["knaskurt","hello","world"]),
    ?line EF = filename:basename(DN),
    ok.

test_sh_file(Dir) ->
    FN = "tf.sh",
    Full = filename:join([Dir,FN]),
    D = [<<"#! /bin/sh\n">>,
	 <<"echo 'argv[0]:|'$0'|'\n">>,
	 <<"i=1\n">>,
	 <<"while [ '!' -z \"$1\" ]; do\n">>,
	 <<"    echo 'argv['$i']:|'\"$1\"'|'\n">>,
	 <<"    shift\n">>,
	 <<"    i=`expr $i + 1`\n">>,
	 <<"done\n">>],
    ?line file:write_file(Full,list_to_binary(D)),
    ?line chmodplusx(Full),
    ?line [Full,"hello","world"] = 
	run_echo_args(Dir,FN,
		      [default,"hello","world"]),
    ?line [Full,"hello","world of spaces"] = 
	run_echo_args(Dir,FN,
		      [default,"hello","world of spaces"]),
    ?line file:write_file(filename:join([Dir,"testfile1"]),<<"testdata1">>),
    ?line file:write_file(filename:join([Dir,"testfile2"]),<<"testdata2">>),
    ?line Pattern = filename:join([Dir,"testfile*"]),
    ?line L = filelib:wildcard(Pattern),
    ?line 2 = length(L),
    ?line [Full,"hello",Pattern] = 
	run_echo_args(Dir,FN,
		      [default,"hello",Pattern]),
     ok.

    

chmodplusx(Filename) ->
    case file:read_file_info(Filename) of
	{ok,FI} ->
	    FI2 = FI#file_info{mode = ((FI#file_info.mode) bor 8#00100)},
	    file:write_file_info(Filename,FI2);
	_ ->
	    ok
    end.

run_echo_args_2(FullnameAndArgs) ->
    Port = open_port({spawn,FullnameAndArgs},[eof]),
    Data = collect_data(Port),
    Port ! {self(), close},
    receive {Port, closed} -> ok end,
    parse_echo_args_output(Data).
    

run_echo_args(Where,Args) ->
    run_echo_args(Where,"echo_args",Args).   
run_echo_args(Where,Prog,Args) ->
    ArgvArg = case Args of
		  [] ->
		      [];
		  [default|T] ->
		      [{args,T}];
		  [switch_order,H|T] ->
		      [{args,T},{arg0,H}];
		  [H|T] ->
		      [{arg0,H},{args,T}]
	      end,
    Command = filename:join([Where,Prog]),
    Port = open_port({spawn_executable,Command},ArgvArg++[eof]),
    Data = collect_data(Port),
    Port ! {self(), close},
    receive {Port, closed} -> ok end,
    parse_echo_args_output(Data).
    
collect_data(Port) ->
    receive
	{Port, {data, Data}} ->
	    Data ++ collect_data(Port);
	{Port, eof} ->
	    []
    end.

parse_echo_args_output(Data) ->
    [lists:last(string:tokens(S,"|")) || S <- string:tokens(Data,"\r\n")].

mix_up_ports(suite) ->
    [];
mix_up_ports(doc) ->
    ["Test that the emulator does not mix up ports when the port table wraps"];
mix_up_ports(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(10)),
    ?line Path = ?config(data_dir, Config),
    ?line ok = load_driver(Path, "echo_drv"),
    ?line Port = erlang:open_port({spawn, "echo_drv"}, []),
    ?line Port ! {self(), {command, "Hello port!"}},
    ?line receive 
	      {Port, {data, "Hello port!"}} = Msg1 -> 
		  io:format("~p~n", [Msg1]),
		  ok; 
	      Other ->
		  test_server:fail({unexpected, Other})
	  end,
    ?line Port ! {self(), close},
    ?line receive {Port, closed} -> ok end,
    ?line loop(start, done,
	       fun(P) ->
		       ?line Q = 
			   (catch erlang:open_port({spawn, "echo_drv"}, [])),
%%		       ?line io:format("~p ", [Q]),
		       if is_port(Q) ->
			       Q;
			  true ->
			       io:format("~p~n", [P]),
			       done
		       end
	       end),
    ?line Port ! {self(), {command, "Hello again port!"}},
    ?line receive 
	      Msg2 ->
		  test_server:fail({unexpected, Msg2})
	  after 1000 ->
		  ok
	  end,
    ?line test_server:timetrap_cancel(Dog),
    ok.

loop(Stop, Stop, Fun) when is_function(Fun) ->
    ok;
loop(Start, Stop, Fun) when is_function(Fun) ->
    loop(Fun(Start), Stop, Fun).


otp_5112(suite) ->
    [];
otp_5112(doc) ->
    ["Test that link to connected process is taken away when port calls",
     "driver_exit() also when the port index has wrapped"];
otp_5112(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(10)),
    ?line Path = ?config(data_dir, Config),
    ?line ok = load_driver(Path, "exit_drv"),
    ?line Port = otp_5112_get_wrapped_port(),
    ?line ?t:format("Max ports: ~p~n",[max_ports()]),
    ?line ?t:format("Port: ~p~n",[Port]),
    ?line {links, Links1} = process_info(self(),links),
    ?line ?t:format("Links1: ~p~n",[Links1]),
    ?line true = lists:member(Port, Links1),
    ?line Port ! {self(), {command, ""}},
    ?line {links, Links2} = process_info(self(),links),
    ?line ?t:format("Links2: ~p~n",[Links2]),
    ?line false = lists:member(Port, Links2), %% This used to fail
    ?line test_server:timetrap_cancel(Dog),
    ok.

otp_5112_get_wrapped_port() ->
    ?line P1 = erlang:open_port({spawn, "exit_drv"}, []),
    ?line case port_ix(P1) < max_ports() of
	      true ->
		  ?line ?t:format("Need to wrap port index (~p)~n", [P1]),
		  ?line otp_5112_wrap_port_ix([P1]),
		  ?line P2 = erlang:open_port({spawn, "exit_drv"}, []),
		  ?line false = port_ix(P2) < max_ports(),
		  ?line P2;
	      false ->
		  ?line ?t:format("Port index already wrapped (~p)~n", [P1]),
		  ?line P1
	  end.

otp_5112_wrap_port_ix(Ports) ->
    ?line case (catch erlang:open_port({spawn, "exit_drv"}, [])) of
	      Port when is_port(Port) ->
		  ?line otp_5112_wrap_port_ix([Port|Ports]);
	      _ ->
		  %% Port table now full; empty port table
		  ?line lists:foreach(fun (P) ->  P ! {self(), close} end,
				      Ports),
		  ?line ok
	  end.


otp_5119(suite) ->
    [];
otp_5119(doc) ->
    ["Test that port index is not unnecessarily wrapped"];
otp_5119(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(10)),
    ?line Path = ?config(data_dir, Config),
    ?line ok = load_driver(Path, "exit_drv"),
    ?line PI1 = port_ix(otp_5119_fill_empty_port_tab([])),
    ?line PI2 = port_ix(erlang:open_port({spawn, "exit_drv"}, [])),
    ?line {PortIx1, PortIx2}
	= case PI2 > PI1 of
	      true ->
		  ?line {PI1, PI2};
	      false ->
		  ?line {port_ix(otp_5119_fill_empty_port_tab([PI2])),
			 port_ix(erlang:open_port({spawn, "exit_drv"}, []))}
	  end,
    ?line MaxPorts = max_ports(),
    ?line ?t:format("PortIx1 = ~p ~p~n", [PI1, PortIx1]),
    ?line ?t:format("PortIx2 = ~p ~p~n", [PI2, PortIx2]),
    ?line ?t:format("MaxPorts = ~p~n", [MaxPorts]),
    ?line true = PortIx2 > PortIx1,
    ?line true = PortIx2 =< PortIx1 + MaxPorts,
    ?line test_server:timetrap_cancel(Dog),
    ?line ok.

otp_5119_fill_empty_port_tab(Ports) ->
    ?line case (catch erlang:open_port({spawn, "exit_drv"}, [])) of
	      Port when is_port(Port) ->
		  ?line otp_5119_fill_empty_port_tab([Port|Ports]);
	      _ ->
		  %% Port table now full; empty port table
		  ?line lists:foreach(fun (P) ->  P ! {self(), close} end,
				      Ports),
		  ?line [LastPort|_] = Ports,
		  ?line LastPort
	  end.

-define(DEF_MAX_PORTS, 1024).

max_ports_env() ->
    ?line case os:getenv("ERL_MAX_PORTS") of
	      EMP when is_list(EMP) ->
		  case catch list_to_integer(EMP) of
		      Int when is_integer(Int) -> ?line Int;
		      _ -> ?line false
		  end;
	      _ -> ?line false
	  end.

max_ports() ->
    ?line PreMaxPorts
	= case max_ports_env() of
	      Env when is_integer(Env) -> ?line Env;
	      _ ->
		  ?line case os:type() of
			    {unix, _} ->
				?line UlimStr = string:strip(os:cmd("ulimit -n")
							     -- "\n"),
				?line case catch list_to_integer(UlimStr) of
					  Ulim when is_integer(Ulim) -> ?line Ulim;
					  _ -> ?line ?DEF_MAX_PORTS
				      end;
			    _ -> ?line ?DEF_MAX_PORTS
			end
	  end,
    ?line case PreMaxPorts > ?DEF_MAX_PORTS of
	      true -> ?line PreMaxPorts;
	      false -> ?line ?DEF_MAX_PORTS
	  end.

port_ix(Port) when is_port(Port) ->
    ?line ["#Port",_,PortIxStr] = string:tokens(erlang:port_to_list(Port),
						"<.>"),
    ?line list_to_integer(PortIxStr).


otp_6224(doc) -> ["Check that port command failure doesn't crash the emulator"];
otp_6224(suite) -> [];
otp_6224(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(10)),
    ?line Path = ?config(data_dir, Config),
    ?line ok = load_driver(Path, "failure_drv"),
    ?line Go = make_ref(),
    ?line Failer = spawn(fun () ->
				 receive Go -> ok end,
				 ?line Port = open_port({spawn, "failure_drv"},
							[]),
				 Port ! {self(), {command, "Fail, please!"}},
				 otp_6224_loop()
			 end),
    ?line Mon = erlang:monitor(process, Failer),
    ?line Failer ! Go,
    ?line receive
	      {'DOWN', Mon, process, Failer, Reason} ->
		  ?line case Reason of
			    {driver_failed, _} -> ?line ok;
			    driver_failed -> ?line ok;
			    _ -> ?line ?t:fail({unexpected_exit_reason,
						Reason})
			end
	  end,
    ?line test_server:timetrap_cancel(Dog),
    ?line ok.
    
otp_6224_loop() ->
    receive _ -> ok after 0 -> ok end,
    otp_6224_loop().


-define(EXIT_STATUS_MSB_MAX_PROCS, 64).
-define(EXIT_STATUS_MSB_MAX_PORTS, 300).

exit_status_multi_scheduling_block(doc) -> [];
exit_status_multi_scheduling_block(suite) -> [];
exit_status_multi_scheduling_block(Config) when is_list(Config) ->
    ?line Repeat = 3,
    ?line case ?t:os_type() of
	      {unix, _} ->
		  ?line Dog = ?t:timetrap(test_server:minutes(2*Repeat)),
		  ?line SleepSecs = 6,
		  try
		      lists:foreach(fun (_) ->
					    exit_status_msb_test(Config,
								 SleepSecs)
				    end,
				    lists:seq(1, Repeat))
		  after
		      %% Wait for the system to recover (regardless
		      %% of success or not) otherwise later testcases
		      %% may unnecessarily fail.
		      ?t:timetrap_cancel(Dog),
		      receive after SleepSecs+500 -> ok end
		  end;
	      _ -> ?line {skip, "Not implemented for this OS"}
	  end.

exit_status_msb_test(Config, SleepSecs) when is_list(Config) ->
    %%
    %% We want to start port programs from as many schedulers as possible
    %% and we want these port programs to terminate while multi-scheduling
    %% is blocked.
    %% 
    ?line NoSchedsOnln = erlang:system_info(schedulers_online),
    ?line Parent = self(),
    ?line ?t:format("SleepSecs = ~p~n", [SleepSecs]),
    ?line PortProg = "sleep " ++ integer_to_list(SleepSecs),
    ?line Start = now(),
    ?line NoProcs = case NoSchedsOnln of
			NProcs when NProcs < ?EXIT_STATUS_MSB_MAX_PROCS ->
			    NProcs;
			_ ->
			    ?EXIT_STATUS_MSB_MAX_PROCS
		    end,
    ?line NoPortsPerProc = case 20*NoProcs of
			       TNPorts when TNPorts < ?EXIT_STATUS_MSB_MAX_PORTS -> 20;
			       _ -> ?EXIT_STATUS_MSB_MAX_PORTS div NoProcs
			   end,
    ?line ?t:format("NoProcs = ~p~nNoPortsPerProc = ~p~n",
		    [NoProcs, NoPortsPerProc]),
    ProcFun
	= fun () ->
		  PrtSIds = lists:map(
			      fun (_) ->
				      erlang:yield(),
				      case catch open_port({spawn, PortProg},
							   [exit_status]) of
					  Prt when is_port(Prt) ->
					      {Prt,
					       erlang:system_info(scheduler_id)};
					  {'EXIT', {Err, _}} when Err == eagain;
								  Err == emfile ->
					      noop;
					  {'EXIT', Err} when Err == eagain;
							     Err == emfile ->
					      noop;
					  Error ->
					      ?t:fail(Error)
				      end
			      end,
			      lists:seq(1, NoPortsPerProc)),
		  SIds = lists:filter(fun (noop) -> false;
					  (_) -> true
				      end,
				      lists:map(fun (noop) -> noop;
						    ({_, SId}) -> SId
						end,
						PrtSIds)),
		  process_flag(scheduler, 0),
		  Parent ! {self(), started, SIds},
		  lists:foreach(
		    fun (noop) ->
			    noop;
			({Port, _}) ->
			    receive
				{Port, {exit_status, 0}} ->
				    ok;
				{Port, {exit_status, Status}} when Status > 128 ->
				    %% Sometimes happens when we have created
				    %% too many ports.
				    ok;
				{Port, {exit_status, _}} = ESMsg ->
				    {Port, {exit_status, 0}} = ESMsg
			    end
		    end,
		    PrtSIds),
		  Parent ! {self(), done}
	  end,
    ?line Procs = lists:map(fun (N) ->
				    spawn_opt(ProcFun,
					      [link,
					       {scheduler,
						(N rem NoSchedsOnln)+1}])
			    end,
			    lists:seq(1, NoProcs)),
    ?line SIds = lists:map(fun (P) ->
				   receive {P, started, SIds} -> SIds end
			   end,
			   Procs),
    ?line StartedTime = timer:now_diff(now(), Start)/1000000,
    ?line ?t:format("StartedTime = ~p~n", [StartedTime]),
    ?line true = StartedTime < SleepSecs,
    ?line erlang:system_flag(multi_scheduling, block),
    ?line lists:foreach(fun (P) -> receive {P, done} -> ok end end, Procs),
    ?line DoneTime = timer:now_diff(now(), Start)/1000000,
    ?line ?t:format("DoneTime = ~p~n", [DoneTime]),
    ?line true = DoneTime > SleepSecs,
    ?line ok = verify_multi_scheduling_blocked(),
    ?line erlang:system_flag(multi_scheduling, unblock),
    ?line case {length(lists:usort(lists:flatten(SIds))), NoSchedsOnln} of
	      {N, N} ->
		  ?line ok;
	      {N, M} ->
		  ?line ?t:fail("Failed to create ports on all"
				++ integer_to_list(M) ++ " available"
				"schedulers. Only created ports on "
				++ integer_to_list(N) ++ " schedulers.")
	  end.

save_sid(SIds) ->
    SId = erlang:system_info(scheduler_id),
    case lists:member(SId, SIds) of
	true -> SIds;
	false -> [SId|SIds]
    end.

sid_proc(SIds) ->
    NewSIds = save_sid(SIds),
    receive
	{From, want_sids} ->
	    From ! {self(), sids, NewSIds}
    after 0 ->
	    sid_proc(NewSIds)
    end.

verify_multi_scheduling_blocked() ->
    ?line Procs = lists:map(fun (_) ->
				    spawn_link(fun () -> sid_proc([]) end)
			    end,
			    lists:seq(1, 3*erlang:system_info(schedulers_online))),
    ?line receive after 1000 -> ok end,
    ?line SIds = lists:map(fun (P) ->
				   P ! {self(), want_sids},
				   receive {P, sids, PSIds} -> PSIds end
			   end,
			   Procs),
    ?line 1 = length(lists:usort(lists:flatten(SIds))),
    ?line ok.
		      
    
%%% Pinging functions.

stream_ping(Config, Size, CmdLine, Options) ->
    Data = random_packet(Size),
    port_expect(Config, [{Data, [Data]}], 0, CmdLine, Options).

ping(Config, Sizes, HSize, CmdLine, Options) ->
    Actions = lists:map(fun(Size) ->
				[$p|Packet] = random_packet(Size, "ping"),
				{[$p|Packet], [[$P|Packet]]}
				end,
			Sizes),
    port_expect(Config, Actions, HSize, CmdLine, Options).

%% expect_input(Sizes, HSize, CmdLine, Options)
%%
%% Sizes = Size of packets to generated.
%% HSize = Header size: 1, 2, or 4
%% CmdLine = Additional command line options.
%% Options = Addtional port options.

expect_input(Config, Sizes, HSize, CmdLine, Options) ->
    expect_input1(Config, Sizes, {HSize, CmdLine, Options}, [], []).

expect_input1(Config, [0|Rest], Params, Expect, ReplyCommand) ->
    expect_input1(Config, Rest, Params, [""|Expect], ["x0"|ReplyCommand]);
expect_input1(Config, [Size|Rest], Params, Expect, ReplyCommand) ->
    Packet = random_packet(Size),
    Fmt = io_lib:format("~c~p", [hd(Packet), Size]),
    expect_input1(Config, Rest, Params, [Packet|Expect], [Fmt|ReplyCommand]);
expect_input1(Config, [], {HSize, CmdLine0, Options}, Expect, ReplyCommand) ->
    CmdLine = build_cmd_line(CmdLine0, ReplyCommand, []),
    port_expect(Config, [{false, lists:reverse(Expect)}],
		HSize, CmdLine, Options).

build_cmd_line(FixedCmdLine, [Cmd|Rest], []) ->
    build_cmd_line(FixedCmdLine, Rest, [Cmd]);
build_cmd_line(FixedCmdLine, [Cmd|Rest], Result) ->
    build_cmd_line(FixedCmdLine, Rest, [Cmd, $:|Result]);
build_cmd_line(FixedCmdLine, [], Result) ->
    lists:flatten([FixedCmdLine, " -r", Result, " -n"]).

%% port_expect(Actions, HSize, CmdLine, Options)
%%
%% Actions = [{Send, ExpectList}|Rest]
%% HSize = 0 (stream), or 1, 2, 4   (header size aka "packet bytes")
%% CmdLine = Command line for port_test.  Don't include -h<digit>.
%% Options = Options for open_port/2.  Don't include {packet, Number} or
%%           or stream.
%%
%% Send = false | list()
%% ExpectList = List of lists or binaries.
%%
%% Returns the port.

port_expect(Config, Actions, HSize, CmdLine, Options0) ->
%    io:format("port_expect(~p, ~p, ~p, ~p)",
%		[Actions, HSize, CmdLine, Options0]),
    ?line PortTest = port_test(Config),
    ?line Cmd = lists:concat([PortTest, " -h", HSize, " ", CmdLine]),
    ?line PortType =
	case HSize of
	    0 -> stream;
	    _ -> {packet, HSize}
	end,
    ?line Options = [PortType|Options0],
    ?line io:format("open_port({spawn, ~p}, ~p)", [Cmd, Options]),
    ?line Port = open_port({spawn, Cmd}, Options),
    ?line port_expect(Port, Actions, Options),
    Port.

port_expect(Port, [{Send, Expects}|Rest], Options) when is_list(Expects) ->
    ?line port_send(Port, Send),
    ?line IsBinaryPort = lists:member(binary, Options),
    ?line Receiver =
	case {lists:member(stream, Options), line_option(Options)} of
	    {false, _} -> fun receive_all/2;
	    {true,false}  -> fun stream_receive_all/2;
	    {_, true} -> fun receive_all/2
	end,
    ?line Receiver(Port, maybe_to_binary(Expects, IsBinaryPort)),
    ?line port_expect(Port, Rest, Options);
port_expect(_, [], _) ->
    ok.

%%% Check for either line or {line,N} in option list
line_option([{line,_}|_]) ->
    true;
line_option([line|_]) ->
    true;
line_option([_|T]) ->
    line_option(T);
line_option([]) ->
    false.

any_list_to_binary({Atom, List}) ->
    {Atom, list_to_binary(List)};
any_list_to_binary(List) ->
    list_to_binary(List).

maybe_to_binary(Expects, true) ->
    lists:map(fun any_list_to_binary/1, Expects);
maybe_to_binary(Expects, false) ->
    Expects.

port_send(_Port, false) -> ok;
port_send(Port, Send) when is_list(Send) ->
%    io:format("port_send(~p, ~p)", [Port, Send]),
    Port ! {self(), {command, Send}}.

receive_all(Port, [Expect|Rest]) ->
%    io:format("receive_all(~p, [~p|Rest])", [Port, Expect]),
    receive
	{Port, {data, Expect}} ->
	    io:format("Received ~s", [format(Expect)]),
	    ok;
	{Port, {data, Other}} ->
	    io:format("Received ~s; expected ~s",
		      [format(Other), format(Expect)]),
	    test_server:fail(bad_message);
	Other ->
	    %% (We're not yet prepared for receiving both 'eol' and
	    %% 'exit_status'; remember that they may appear in any order.)
	    case {Expect, Rest, Other} of
		{eof, [], {Port, eof}} ->
		    io:format("Received soft EOF.",[]),
		    ok;
		{{exit_status, S}, [], {Port, {exit_status, S}}} ->
		    io:format("Received exit status ~p.",[S]),
		    ok;
		_ ->
%%%	            io:format("Unexpected message: ~s", [format(Other)]),
		    io:format("Unexpected message: ~w", [Other]),
		    ?line test_server:fail(unexpected_message)
	    end
    end,
    receive_all(Port, Rest);
receive_all(_Port, []) ->
    ok.

stream_receive_all(Port, [Expect]) ->
    stream_receive_all1(Port, Expect).

stream_receive_all1(_Port, Empty) when is_binary(Empty), size(Empty) == 0 ->
    ok;
stream_receive_all1(_Port, []) ->
    ok;
stream_receive_all1(Port, Expect) ->
    receive
	{Port, {data, Data}} ->
	    Remaining = compare(Data, Expect),
	    stream_receive_all1(Port, Remaining);
	Other ->
	    test_server:fail({bad_message, Other})
    end.

compare(B1, B2) when is_binary(B1), is_binary(B2), byte_size(B1) =< byte_size(B2) ->
    case split_binary(B2, size(B1)) of
	{B1,Remaining} ->
	    Remaining;
	_Other ->
	    test_server:fail(nomatch)
    end;
compare(B1, B2) when is_binary(B1), is_binary(B2) ->
    test_server:fail(too_much_data);
compare([X|Rest1], [X|Rest2]) ->
    compare(Rest1, Rest2);
compare([_|_], [_|_]) ->
    test_server:fail(nomatch);
compare([], Remaining) ->
    Remaining;
compare(_Data, []) ->
    test_server:fail(too_much_data).

maybe_to_list(Bin) when is_binary(Bin) ->
    binary_to_list(Bin);
maybe_to_list(List) ->
    List.

format({Eol,List}) ->
    io_lib:format("tuple<~w,~s>",[Eol, maybe_to_list(List)]);
format(List) when is_list(List) ->
    case list_at_least(50, List) of
	true ->
	    io_lib:format("\"~-50s...\"", [List]);
	false ->
	    io_lib:format("~p", [List])
    end;
format(Bin) when is_binary(Bin), size(Bin) >= 50 ->
    io_lib:format("binary<~-50s...>", [binary_to_list(Bin, 1, 50)]);
format(Bin) when is_binary(Bin) ->
    io_lib:format("binary<~s>", [binary_to_list(Bin)]).


list_at_least(Number, [_|Rest]) when Number > 0 ->
    list_at_least(Number-1, Rest);
list_at_least(Number, []) when Number > 0 ->
    false;
list_at_least(0, _List) -> true.


%%% Utility functions.

random_packet(Size) ->
    random_packet(Size, "").

random_packet(Size, Prefix) ->
    build_packet(Size-length(Prefix), lists:reverse(Prefix), random_char()).

build_packet(0, Result, _NextChar) ->
    lists:reverse(Result);
build_packet(Left, Result, NextChar0) ->
    NextChar =
	if
	    NextChar0 >= 126 ->
		33;
	    true ->
		NextChar0+1
	end,
    build_packet(Left-1, [NextChar0|Result], NextChar).

sizes() ->
    case os:type() of
	vxworks ->
	    % don't stress VxWorks too much 
	    [10, 13, 64, 127, 128, 255, 256, 1023, 1024,
	     8191, 8192, 16383, 16384];
	_ ->
	    [10, 13, 64, 127, 128, 255, 256, 1023, 1024,
	     32767, 32768, 65535, 65536]
    end.

sizes(Header_Size) ->
    sizes(Header_Size, sizes(), []).

sizes(1, [Packet_Size|Rest], Result) when Packet_Size < 256 ->
    sizes(1, Rest, [Packet_Size|Result]);
sizes(2, [Packet_Size|Rest], Result) when Packet_Size < 65536 ->
    sizes(2, Rest, [Packet_Size|Result]);
sizes(4, [Packet_Size|Rest], Result) ->
    sizes(4, Rest, [Packet_Size|Result]);
sizes(_, _, Result) ->
    Result.

random_char() ->
    random_char("abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789").

random_char(Chars) ->
    lists:nth(uniform(length(Chars)), Chars).

uniform(N) ->
    case get(random_seed) of
	undefined ->	    
	    {X, Y, Z} = Seed = time(),
	    io:format("Random seed = ~p\n",[Seed]),
	    random:seed(X, Y, Z);
	_ ->
	    ok
    end,
    random:uniform(N).

fun_spawn(Fun) ->
    fun_spawn(Fun, []).

fun_spawn(Fun, Args) ->
    spawn_link(erlang, apply, [Fun, Args]).

port_test(Config) when is_list(Config) ->
    ?line filename:join(?config(data_dir, Config), "port_test").


ports(doc) -> "Test that erlang:ports/0 returns a consistent snapshot of ports";
ports(suite) -> [];
ports(Config) when is_list(Config) ->
    ?line Path = ?config(data_dir, Config),
    ?line ok = load_driver(Path, "exit_drv"),

    receive after 1000 -> ok end, % Wait for other ports to stabilize

    ?line OtherPorts = erlang:ports(),
    io:format("Other ports: ~p\n",[OtherPorts]),
    MaxPorts = 1024 - length(OtherPorts),

    TrafficPid = spawn_link(fun() -> ports_traffic(MaxPorts) end),

    ports_snapshots(100, TrafficPid, OtherPorts),
    TrafficPid ! {self(),die},
    ?line receive {TrafficPid, dead} -> ok end,
    ok.

ports_snapshots(0, _, _) ->
    ok;
ports_snapshots(Iter, TrafficPid, OtherPorts) ->

    TrafficPid ! start,    
    ?line receive after 1 -> ok end,

    Snapshot = erlang:ports(),

    TrafficPid ! {self(), stop},
    ?line receive {TrafficPid, EventList, TrafficPorts} -> ok end,
    
    %%io:format("Snapshot=~p\n", [Snapshot]),
    ports_verify(Snapshot, OtherPorts ++ TrafficPorts, EventList),

    ports_snapshots(Iter-1, TrafficPid, OtherPorts).


ports_traffic(MaxPorts) ->
    ports_traffic_stopped(MaxPorts, {[],0}).

ports_traffic_stopped(MaxPorts, {PortList, PortCnt}) ->
    receive
	start ->
	    %%io:format("Traffic started in ~p\n",[self()]),
	    ports_traffic_started(MaxPorts, {PortList, PortCnt}, []);
	{Pid,die} ->
	    ?line lists:foreach(fun(Port)-> erlang:port_close(Port) end,
				PortList),
	    Pid ! {self(),dead}
    end.

ports_traffic_started(MaxPorts, {PortList, PortCnt}, EventList) ->
    receive 
	{Pid, stop} ->
	    %%io:format("Traffic stopped in ~p\n",[self()]),
	    Pid ! {self(), EventList, PortList},
	    ports_traffic_stopped(MaxPorts, {PortList, PortCnt})

    after 0 ->
	    ports_traffic_do(MaxPorts, {PortList, PortCnt}, EventList)
    end.

ports_traffic_do(MaxPorts, {PortList, PortCnt}, EventList) ->
    N = uniform(MaxPorts),
    case N > PortCnt of
	true -> % Open port	    
	    ?line P = open_port({spawn, "exit_drv"}, []),
	    %%io:format("Created port ~p\n",[P]),
	    ports_traffic_started(MaxPorts, {[P|PortList], PortCnt+1},
				  [{open,P}|EventList]);
	
	false -> % Close port
	    ?line P = lists:nth(N, PortList),
	    %%io:format("Close port ~p\n",[P]),
	    ?line true = erlang:port_close(P),
	    ports_traffic_started(MaxPorts, {lists:delete(P,PortList), PortCnt-1},
				  [{close,P}|EventList])
    end.

ports_verify(Ports, PortsAfter, EventList) ->    
    %%io:format("Candidate=~p\nEvents=~p\n", [PortsAfter, EventList]),
    case lists:sort(Ports) =:= lists:sort(PortsAfter) of
	true ->
	    io:format("Snapshot of ~p ports verified ok.\n",[length(Ports)]),
	    ok;
	false ->
	    %% Note that we track the event list "backwards", undoing open/close:
	    case EventList of
		[{open,P} | Tail] ->
		    ports_verify(Ports, lists:delete(P,PortsAfter), Tail);		    

		[{close,P} | Tail] ->
		    ports_verify(Ports, [P | PortsAfter], Tail);		    

		[] ->
		    ?line test_server:fail("Inconsistent snapshot from erlang:ports()")
	    end
    end.

load_driver(Dir, Driver) ->
    case erl_ddll:load_driver(Dir, Driver) of
	ok -> ok;
	{error, Error} = Res ->
	    io:format("~s\n", [erl_ddll:format_error(Error)]),
	    Res
    end.


close_deaf_port(doc) -> ["Send data to port program that does not read it, then close port."
			 "Primary targeting Windows to test threaded_handle_closer in sys.c"];
close_deaf_port(suite) -> [];
close_deaf_port(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(100)),
    ?line DataDir = ?config(data_dir, Config),
    ?line DeadPort = os:find_executable("dead_port", DataDir),
    ?line Port = open_port({spawn,DeadPort++" 60"},[]),
    ?line erlang:port_command(Port,"Hello, can you hear me!?!?"),
    ?line port_close(Port),

    Res = close_deaf_port_1(0, DeadPort),
    ?line test_server:timetrap_cancel(Dog),
    Res.

close_deaf_port_1(1000, _) ->
    ok;
close_deaf_port_1(N, Cmd) ->
    Timeout = integer_to_list(random:uniform(10*1000)),
    ?line try open_port({spawn_executable,Cmd},[{args,[Timeout]}]) of
    	Port ->
	    ?line erlang:port_command(Port,"Hello, can you hear me!?!?"),
	    ?line port_close(Port),
	    close_deaf_port_1(N+1, Cmd)
    catch
    	_:eagain ->
	    {comment, "Could not spawn more than " ++ integer_to_list(N) ++ " OS processes."}
    end.