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 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 1447 1448 1449 1450 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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1998-2024. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(gen_tcp_api_SUITE).
%% Tests the documented API for the gen_tcp functions. The "normal" cases
%% are not tested here, because they are tested indirectly in this and
%% and other test suites.
-include_lib("common_test/include/ct.hrl").
-include_lib("kernel/include/inet.hrl").
-include("kernel_test_lib.hrl").
-export([
all/0, suite/0, groups/0,
init_per_suite/1, end_per_suite/1,
init_per_group/2, end_per_group/2,
init_per_testcase/2, end_per_testcase/2,
t_connect_timeout/1, t_accept_timeout/1,
t_connect_src_port/1, t_connect_bad/1,
t_recv_timeout/1, t_recv_eof/1, t_recv_delim/1,
t_shutdown_write/1, t_shutdown_both/1, t_shutdown_error/1,
t_shutdown_async/1,
t_fdopen/1, t_fdconnect/1, t_implicit_inet6/1,
t_local_basic/1, t_local_unbound/1, t_local_fdopen/1,
t_local_fdopen_listen/1, t_local_fdopen_listen_unbound/1,
t_local_fdopen_connect/1, t_local_fdopen_connect_unbound/1,
t_local_abstract/1, t_accept_inet6_tclass/1,
s_accept_with_explicit_socket_backend/1,
t_simple_local_sockaddr_in_send_recv/1,
t_simple_link_local_sockaddr_in_send_recv/1,
t_simple_local_sockaddr_in6_send_recv/1,
t_simple_link_local_sockaddr_in6_send_recv/1
]).
-export([getsockfd/0, closesockfd/1]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
suite() ->
[
{ct_hooks,[ts_install_cth]},
{timetrap,{minutes,1}}
].
all() ->
%% This is a temporary measure to ensure that we can
%% test the socket backend without effecting *all*
%% applications on *all* machines.
%% This flag is set only for *one* host.
case ?TEST_INET_BACKENDS() of
true ->
[
{group, inet_backend_default},
{group, inet_backend_inet},
{group, inet_backend_socket},
{group, s_misc}
];
_ ->
[
{group, inet_backend_default},
{group, s_misc}
]
end.
groups() ->
[
{inet_backend_default, [], inet_backend_default_cases()},
{inet_backend_inet, [], inet_backend_inet_cases()},
{inet_backend_socket, [], inet_backend_socket_cases()},
{t_accept, [], t_accept_cases()},
{t_connect, [], t_connect_cases()},
{t_recv, [], t_recv_cases()},
{t_shutdown, [], t_shutdown_cases()},
{t_misc, [], t_misc_cases()},
{sockaddr, [], sockaddr_cases()},
{t_local, [], t_local_cases()},
{s_misc, [], s_misc_cases()}
].
inet_backend_default_cases() ->
[
{group, t_accept},
{group, t_connect},
{group, t_recv},
{group, t_shutdown},
{group, t_misc},
{group, t_local}
].
inet_backend_inet_cases() ->
inet_backend_default_cases().
inet_backend_socket_cases() ->
inet_backend_default_cases().
t_accept_cases() ->
[
t_accept_timeout
].
t_connect_cases() ->
[
t_connect_timeout,
t_connect_src_port,
t_connect_bad
].
t_recv_cases() ->
[
t_recv_timeout,
t_recv_eof,
t_recv_delim
].
t_shutdown_cases() ->
[
t_shutdown_write,
t_shutdown_both,
t_shutdown_error,
t_shutdown_async
].
t_misc_cases() ->
[
t_fdopen,
t_fdconnect,
t_implicit_inet6,
t_accept_inet6_tclass,
{group, sockaddr}
].
sockaddr_cases() ->
[
t_simple_local_sockaddr_in_send_recv,
t_simple_link_local_sockaddr_in_send_recv,
t_simple_local_sockaddr_in6_send_recv,
t_simple_link_local_sockaddr_in6_send_recv
].
t_local_cases() ->
[
t_local_basic,
t_local_unbound,
t_local_fdopen,
t_local_fdopen_listen,
t_local_fdopen_listen_unbound,
t_local_fdopen_connect,
t_local_fdopen_connect_unbound,
t_local_abstract
].
s_misc_cases() ->
[
s_accept_with_explicit_socket_backend
].
init_per_suite(Config0) ->
?P("init_per_suite -> entry with"
"~n Config: ~p"
"~n Nodes: ~p", [Config0, erlang:nodes()]),
case ?LIB:init_per_suite(Config0) of
{skip, _} = SKIP ->
SKIP;
Config1 when is_list(Config1) ->
?P("init_per_suite -> end when "
"~n Config: ~p", [Config1]),
%% We need a monitor on this node also
kernel_test_sys_monitor:start(),
Config1
end.
end_per_suite(Config0) ->
?P("end_per_suite -> entry with"
"~n Config: ~p"
"~n Nodes: ~p", [Config0, erlang:nodes()]),
%% Stop the local monitor
kernel_test_sys_monitor:stop(),
Config1 = ?LIB:end_per_suite(Config0),
?P("end_per_suite -> "
"~n Nodes: ~p", [erlang:nodes()]),
Config1.
init_per_group(inet_backend_default = _GroupName, Config) ->
[{socket_create_opts, []} | Config];
init_per_group(inet_backend_inet = _GroupName, Config) ->
case ?EXPLICIT_INET_BACKEND() of
true ->
%% The environment trumps us,
%% so only the default group should be run!
{skip, "explicit inet backend"};
false ->
[{socket_create_opts, [{inet_backend, inet}]} | Config]
end;
init_per_group(inet_backend_socket = _GroupName, Config) ->
case ?EXPLICIT_INET_BACKEND() of
true ->
%% The environment trumps us,
%% so only the default group should be run!
{skip, "explicit inet backend"};
false ->
[{socket_create_opts, [{inet_backend, socket}]} | Config]
end;
init_per_group(t_local = _GroupName, Config) ->
case ?IS_SOCKET_BACKEND(Config) of
true ->
case ?LIB:has_support_unix_domain_socket() of
true ->
Config;
false ->
{skip, "AF_LOCAL not supported"}
end;
_ ->
try gen_tcp:connect({local,<<"/">>}, 0, []) of
{error, eafnosupport} ->
{skip, "AF_LOCAL not supported"};
{error,_} ->
Config
catch
_C:_E:_S ->
{skip, "AF_LOCAL not supported"}
end
end;
init_per_group(sockaddr = _GroupName, Config) ->
case is_socket_supported() of
ok ->
Config;
{skip, _} = SKIP ->
SKIP
end;
init_per_group(_GroupName, Config) ->
Config.
end_per_group(t_local, _Config) ->
delete_local_filenames();
end_per_group(_, _Config) ->
ok.
init_per_testcase(Func, Config)
when Func =:= undefined -> % Insert your testcase name here
dbg:tracer(),
dbg:p(self(), c),
dbg:tpl(prim_inet, cx),
dbg:tpl(local_tcp, cx),
dbg:tpl(inet, cx),
dbg:tpl(gen_tcp, cx),
Config;
init_per_testcase(_Func, Config) ->
?P("init_per_testcase -> entry with"
"~n Config: ~p"
"~n Nodes: ~p"
"~n Links: ~p"
"~n Monitors: ~p",
[Config, erlang:nodes(), pi(links), pi(monitors)]),
kernel_test_global_sys_monitor:reset_events(),
?P("init_per_testcase -> done when"
"~n Nodes: ~p"
"~n Links: ~p"
"~n Monitors: ~p", [erlang:nodes(), pi(links), pi(monitors)]),
Config.
end_per_testcase(Func, _Config)
when Func =:= undefined -> % Insert your testcase name here
dbg:stop();
end_per_testcase(_Func, Config) ->
?P("end_per_testcase -> entry with"
"~n Config: ~p"
"~n Nodes: ~p"
"~n Links: ~p"
"~n Monitors: ~p",
[Config, erlang:nodes(), pi(links), pi(monitors)]),
?P("system events during test: "
"~n ~p", [kernel_test_global_sys_monitor:events()]),
?P("end_per_testcase -> done with"
"~n Nodes: ~p"
"~n Links: ~p"
"~n Monitors: ~p", [erlang:nodes(), pi(links), pi(monitors)]),
ok.
%%% gen_tcp:accept/1,2
%% Test that gen_tcp:accept/2 (with timeout) works.
t_accept_timeout(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
throw({skip, Reason})
end
end,
TC = fun(Addr) -> do_accept_timeout(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_accept_timeout(Config, Addr) ->
{ok, L} = gen_tcp:listen(0, ?INET_BACKEND_OPTS(Config) ++ [{ip, Addr}]),
timeout({gen_tcp, accept, [L, 200]}, 0.2, 1.0).
%%% gen_tcp:connect/X
%% Test that gen_tcp:connect/4 (with timeout) works.
t_connect_timeout(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
throw({skip, Reason})
end
end,
TC = fun(Addr) -> do_connect_timeout(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_connect_timeout(Config, Addr)->
%%BadAddr = {134,138,177,16},
%%TcpPort = 80,
{ok, BadAddr} = unused_ip(),
TcpPort = 45638,
ok = ?P("Connecting to ~p, port ~p", [BadAddr, TcpPort]),
connect_timeout({gen_tcp, connect,
[BadAddr,TcpPort,
?INET_BACKEND_OPTS(Config) ++ [{ip, Addr}], 200]},
0.2, 5.0).
%% Test that setting only the source port for a connection works.
t_connect_src_port(Config) when is_list(Config) ->
Timeout = 1000,
Loopback = {127,0,0,1},
%% Allocate a port to later use as source port
{ok, Tmp} = gen_tcp:listen(0, [{ip,Loopback}, {linger,{true,0}}]),
{ok, SrcPort} = inet:port(Tmp),
io:format("SrcPort = ~w~n", [SrcPort]),
{ok, L} = gen_tcp:listen(0, [{ip,Loopback}]),
ok = gen_tcp:close(Tmp),
{ok, DstPort} = inet:port(L),
io:format("DstPort = ~w~n", [DstPort]),
ConnectOpts = [{port,SrcPort}, {linger,{true,0}}],
{ok, C} = gen_tcp:connect(Loopback, DstPort, ConnectOpts, Timeout),
{ok, A} = gen_tcp:accept(L, Timeout),
{ok, {_, SrcPort}} = inet:peername(A),
ok = gen_tcp:close(L),
ok = gen_tcp:close(C),
ok = gen_tcp:close(A).
%% Test that gen_tcp:connect/3 handles non-existings hosts, and other
%% invalid things.
t_connect_bad(Config) when is_list(Config) ->
NonExistingPort = 45638, % Not in use, I hope.
t_connect_bad(Config, localhost, NonExistingPort, "port not in use"),
t_connect_bad(Config, "non-existing-host-xxx", 7, "non-existing host"),
element(1, os:type()) =:= unix andalso
begin
t_connect_bad(Config, "", 7, "empty host string"),
t_connect_bad(Config, '', 7, "empty host atom")
end,
t_connect_bad(Config, ".", 7, "root domain string"),
t_connect_bad(Config, '.', 7, "root domain atom").
t_connect_bad(Config, Host, Port, Descr) ->
{error, Reason} =
gen_tcp:connect(Host, Port,?INET_BACKEND_OPTS(Config)),
io:format(
"Error for connection attempt to " ++ Descr ++ ": ~p~n",
[Reason]),
ok.
%%% gen_tcp:recv/X
%% Test that gen_tcp:recv/3 (with timeout works).
t_recv_timeout(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
skip(Reason)
end
end,
TC = fun(Addr) -> do_recv_timeout(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_recv_timeout(Config, Addr) ->
{ok, L} = gen_tcp:listen(0,
?INET_BACKEND_OPTS(Config) ++ [{ip, Addr}]),
{ok, Port} = inet:port(L),
{ok, Client} = gen_tcp:connect(Addr, Port,
?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}, {active, false}]),
{ok, _A} = gen_tcp:accept(L),
timeout({gen_tcp, recv, [Client, 0, 200]}, 0.2, 5.0).
%% Test that end of file on a socket is reported correctly.
t_recv_eof(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
skip(Reason)
end
end,
TC = fun(Addr) -> do_recv_eof(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_recv_eof(Config, Addr) ->
{ok, L} = gen_tcp:listen(0,
?INET_BACKEND_OPTS(Config) ++ [{ip, Addr}]),
{ok, Port} = inet:port(L),
{ok, Client} = gen_tcp:connect(Addr, Port,
?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}, {active, false}]),
{ok, A} = gen_tcp:accept(L),
ok = gen_tcp:close(A),
{error, closed} = gen_tcp:recv(Client, 0),
ok.
%% Test using message delimiter $X.
t_recv_delim(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
skip(Reason)
end
end,
TC = fun(Addr) -> do_recv_delim(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_recv_delim(Config, Addr) ->
?P("create listen socket"),
{ok, L} = gen_tcp:listen(0, ?INET_BACKEND_OPTS(Config) ++ [{ip, Addr}]),
{ok, Port} = inet:port(L),
Opts = ?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}, {active,false}, {packet,line}, {line_delimiter,$X}],
{ok, Client} = gen_tcp:connect(Addr, Port, Opts),
{ok, A} = gen_tcp:accept(L),
?P("send the data"),
ok = gen_tcp:send(A, "abcXefgX"),
%% Why do we need a timeout?
%% Sure, normally there would be no delay,
%% but this testcase has nothing to do with timeouts?
?P("read the first chunk"),
{ok, "abcX"} = gen_tcp:recv(Client, 0), % 200),
?P("read the second chunk"),
{ok, "efgX"} = gen_tcp:recv(Client, 0), % 200),
?P("set active = 2"),
ok = inet:setopts(Client, [{active,2}]),
?P("send the data again"),
ok = gen_tcp:send(A, "abcXefgX"),
?P("await the first chunk"),
receive {tcp, Client, "abcX"} -> ?P("received first chunk") end,
?P("await the second chunk"),
receive {tcp, Client, "efgX"} -> ?P("received second chunk") end,
?P("cleanup"),
ok = gen_tcp:close(Client),
ok = gen_tcp:close(A),
?P("done"),
ok.
%%% gen_tcp:shutdown/2
t_shutdown_write(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
skip(Reason)
end
end,
TC = fun(Addr) -> do_shutdown_write(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_shutdown_write(Config, Addr) ->
?P("create listen socket"),
{ok, L} = gen_tcp:listen(0, ?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}]),
{ok, Port} = inet:port(L),
?P("create connect socket (C)"),
{ok, C} = gen_tcp:connect(Addr, Port,
?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}, {active, false}]),
?P("create accept socket (A)"),
{ok, A} = gen_tcp:accept(L),
?P("send message A -> C"),
ok = gen_tcp:send(A, "Hej Client"),
?P("socket A shutdown(write)"),
ok = gen_tcp:shutdown(A, write),
?P("socket C recv - expect message"),
{ok, "Hej Client"} = gen_tcp:recv(C, 0),
?P("socket C recv - expect closed"),
{error, closed} = gen_tcp:recv(C, 0),
?P("done"),
ok.
t_shutdown_both(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
skip(Reason)
end
end,
TC = fun(Addr) -> do_shutdown_both(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_shutdown_both(Config, Addr) ->
?P("create listen socket"),
{ok, L} = gen_tcp:listen(0, ?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}]),
{ok, Port} = inet:port(L),
?P("create connect socket (C)"),
{ok, C} = gen_tcp:connect(Addr, Port,
?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}, {active, false}]),
?P("create accept socket (A)"),
{ok, A} = gen_tcp:accept(L),
?P("send message A -> C"),
ok = gen_tcp:send(A, "Hej Client"),
?P("socket A shutdown(read_write)"),
ok = gen_tcp:shutdown(A, read_write),
?P("socket C recv - expect message"),
{ok, "Hej Client"} = gen_tcp:recv(C, 0),
?P("socket C recv - expect closed"),
{error, closed} = gen_tcp:recv(C, 0),
?P("done"),
ok.
t_shutdown_error(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
skip(Reason)
end
end,
TC = fun(Addr) -> do_shutdown_error(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_shutdown_error(Config, Addr) ->
?P("create listen socket"),
{ok, L} = gen_tcp:listen(0, ?INET_BACKEND_OPTS(Config) ++ [{ip, Addr}]),
?P("shutdown socket (with How = read_write)"),
case gen_tcp:shutdown(L, read_write) of
{error, enotconn} ->
ok;
ok -> % On some platforms this can happen...Linux...
case os:type() of
{unix, linux} ->
?P("unexpected shutdown success - can happen on linux"),
ok;
_ ->
exit(unexpected_success)
end;
{error, Reason} ->
exit({unexpected_shutdown_error, Reason})
end,
?P("close socket"),
ok = gen_tcp:close(L),
?P("shutdown socket again (with How = read_write)"),
{error, closed} = gen_tcp:shutdown(L, read_write),
?P("done"),
ok.
t_shutdown_async(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
skip(Reason)
end
end,
TC = fun(Addr) -> do_shutdown_async(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_shutdown_async(Config, Addr) ->
?P("create listen socket"),
{ok, L} = gen_tcp:listen(0, ?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}, {sndbuf, 4096}]),
if
is_port(L) ->
do_shutdown_async2(Config, Addr, L);
true ->
(catch gen_tcp:close(L)),
exit({skip, "inet-only testcase"})
end.
do_shutdown_async2(Config, Addr, L) ->
{OS, _} = os:type(),
{ok, Port} = inet:port(L),
?P("connect"),
{ok, Client} = gen_tcp:connect(Addr, Port,
?INET_BACKEND_OPTS(Config) ++
[{ip, Addr},
{recbuf, 4096},
{active, false}]),
?P("accept connection"),
{ok, S} = gen_tcp:accept(L),
?P("create payload"),
PayloadSize = 1024 * 1024,
Payload = lists:duplicate(PayloadSize, $.),
?P("send payload"),
ok = gen_tcp:send(S, Payload),
?P("verify queue size"),
case erlang:port_info(S, queue_size) of
{queue_size, N} when N > 0 -> ok;
{queue_size, 0} when OS =:= win32 -> ok;
{queue_size, 0} = T -> ct:fail({unexpected, T})
end,
?P("shutdown(write) accepted socket"),
ok = gen_tcp:shutdown(S, write),
?P("recv from connected socket"),
{ok, Buf} = gen_tcp:recv(Client, PayloadSize),
?P("recv(0) from connected socket (expect closed)"),
{error, closed} = gen_tcp:recv(Client, 0),
?P("verify recv data"),
case length(Buf) of
PayloadSize -> ?P("done"), ok;
Sz -> ?P("ERROR: "
"~n extected: ~p"
"~n received: ~p", [PayloadSize, Sz]),
ct:fail({payload_size,
{expected, PayloadSize},
{received, Sz}})
end.
%%% gen_tcp:fdopen/2
t_fdopen(Config) when is_list(Config) ->
Cond = fun() -> ok end,
Pre = fun() -> case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
skip(Reason)
end
end,
TC = fun(Addr) -> do_fdopen(Config, Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_fdopen(Config, Addr) ->
Question = "Aaaa... Long time ago in a small town in Germany,",
Question1 = list_to_binary(Question),
Question2 = [<<"Aaaa">>, "... ", $L, <<>>, $o, "ng time ago ",
["in ", [], <<"a small town">>, [" in Germany,", <<>>]]],
Question1 = iolist_to_binary(Question2),
Answer = "there was a shoemaker, Schumacher was his name.",
{ok, L} = gen_tcp:listen(0,
?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}, {active, false}]),
{ok, Port} = inet:port(L),
{ok, Client} = gen_tcp:connect(Addr, Port,
?INET_BACKEND_OPTS(Config) ++
[{ip, Addr}, {active, false}]),
{A, FD} = case gen_tcp:accept(L) of
{ok, ASock} when is_port(ASock) ->
{ok, FileDesc} = prim_inet:getfd(ASock),
{ASock, FileDesc};
{ok, ASock} -> % socket
{ok, [{fd, FileDesc}]} =
gen_tcp_socket:getopts(ASock, [fd]),
{ASock, FileDesc}
end,
?P("fdopen -> accepted: "
"~n A: ~p"
"~n FD: ~p", [A, FD]),
{ok, Server} = try gen_tcp:fdopen(FD, ?INET_BACKEND_OPTS(Config)) of
{ok, _} = OK ->
OK;
{error, notsup = R} ->
%% This is not supported for 'socket on Windows'
skip({fdopen, R})
catch
throw:{error, notsup = R} ->
%% This is not supported for 'socket on Windows'
skip({fdopen, R})
end,
ok = gen_tcp:send(Client, Question),
{ok, Question} = gen_tcp:recv(Server, length(Question), 2000),
ok = gen_tcp:send(Client, Question1),
{ok, Question} = gen_tcp:recv(Server, length(Question), 2000),
ok = gen_tcp:send(Client, Question2),
{ok, Question} = gen_tcp:recv(Server, length(Question), 2000),
ok = gen_tcp:send(Server, Answer),
{ok, Answer} = gen_tcp:recv(Client, length(Answer), 2000),
ok = gen_tcp:close(Client),
{error, closed} = gen_tcp:recv(A, 1, 2000),
ok = gen_tcp:close(Server),
ok = gen_tcp:close(A),
ok = gen_tcp:close(L),
ok.
t_fdconnect(Config) when is_list(Config) ->
Cond = fun() ->
?P("Try verify if IPv4 is supported"),
?HAS_SUPPORT_IPV4()
end,
Pre = fun() ->
{ok, Addr} = ?WHICH_LOCAL_ADDR(inet),
?P("Use (local) address: ~p", [Addr]),
#{local_addr => Addr}
end,
Case = fun(#{local_addr := Addr}) ->
do_t_fdconnect(Addr, Config)
end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, Case, Post).
do_t_fdconnect(Addr, Config) ->
Question = "Aaaa... Long time ago in a small town in Germany,",
Question1 = list_to_binary(Question),
Question2 = [<<"Aaaa">>, "... ", $L, <<>>, $o, "ng time ago ",
["in ", [], <<"a small town">>, [" in Germany,", <<>>]]],
Question1 = iolist_to_binary(Question2),
Answer = "there was a shoemaker, Schumacher was his name.",
Path = proplists:get_value(data_dir, Config),
Lib = "gen_tcp_api_SUITE",
?P("try load util nif lib"),
case erlang:load_nif(filename:join(Path, Lib), []) of
ok ->
ok;
{error, {reload, ReasonStr}} ->
?P("already loaded: "
"~n ~s", [ReasonStr]),
ok;
{error, Reason} ->
?P("UNEXPECTED - failed loading util nif lib: "
"~n ~p", [Reason]),
?SKIPT("failed loading util nif lib")
end,
?P("try create listen socket"),
LOpts = ?INET_BACKEND_OPTS(Config) ++ [{ifaddr, Addr}, {active, false}],
L = try gen_tcp:listen(0, LOpts) of
{ok, LSock} ->
LSock;
{error, eaddrnotavail = LReason} ->
?SKIPT(listen_failed_str(LReason));
{error, LReason} ->
?P("UNEXPECTED ERROR - listen error: "
"~n COpts: ~p"
"~n Reason: ~p", [LReason]),
ct:fail({unexpected_listen_error, LReason, LOpts})
catch
LC : LE : LS ->
?P("UNEXPECTED ERROR - caught listen: "
"~n LOpts: ~p"
"~n C: ~p"
"~n E: ~p"
"~n S: ~p", [LOpts, LC, LE, LS]),
ct:fail({listen_failure, {LC, LE, LS}, LOpts})
end,
{ok, LPort} = inet:port(L),
?P("try create file descriptor"),
FD = gen_tcp_api_SUITE:getsockfd(),
?P("try connect (to port ~w) using file descriptor ~w", [LPort, FD]),
COpts = ?INET_BACKEND_OPTS(Config) ++ [{fd, FD},
{ifaddr, Addr},
{active, false}],
%% The debug is just to "see" that it (debug) "works"...
Client = try gen_tcp:connect(Addr, LPort, COpts ++ [{debug, true}]) of
{ok, CSock} ->
ok = inet:setopts(CSock, [{debug, false}]),
CSock;
{error, eaddrnotavail = CReason} ->
gen_tcp:close(L),
gen_tcp_api_SUITE:closesockfd(FD),
?SKIPT(connect_failed_str(CReason));
{error, CReason} ->
?P("UNEXPECTED ERROR - connect error: "
"~n COpts: ~p"
"~n Reason: ~p", [COpts, CReason]),
ct:fail({unexpected_connect_error, CReason, COpts})
catch
CC : CE : CS ->
?P("UNEXPECTED ERROR - caught connect: "
"~n COpts: ~p"
"~n C: ~p"
"~n E: ~p"
"~n S: ~p", [COpts, CC, CE, CS]),
ct:fail({connect_failure, {CC, CE, CS}, COpts})
end,
?P("try accept connection"),
Server = try gen_tcp:accept(L) of
{ok, ASock} ->
ASock;
{error, eaddrnotavail = AReason} ->
gen_tcp:close(Client),
gen_tcp:close(L),
gen_tcp_api_SUITE:closesockfd(FD),
?SKIPT(accept_failed_str(AReason));
{error, AReason} ->
?P("UNEXPECTED ERROR - accept error: "
"~n Reason: ~p", [AReason]),
ct:fail({unexpected_accept_error, AReason})
catch
AC : AE : AS ->
?P("UNEXPECTED ERROR - caught accept: "
"~n C: ~p"
"~n E: ~p"
"~n S: ~p", [AC, AE, AS]),
ct:fail({accept_failure, {AC, AE, AS}})
end,
?P("begin validation"),
ok = gen_tcp:send(Client, Question),
{ok, Question} = gen_tcp:recv(Server, length(Question), 2000),
ok = gen_tcp:send(Client, Question1),
{ok, Question} = gen_tcp:recv(Server, length(Question), 2000),
ok = gen_tcp:send(Client, Question2),
{ok, Question} = gen_tcp:recv(Server, length(Question), 2000),
ok = gen_tcp:send(Server, Answer),
{ok, Answer} = gen_tcp:recv(Client, length(Answer), 2000),
?P("cleanup"),
ok = gen_tcp:close(Client),
FD = gen_tcp_api_SUITE:closesockfd(FD),
{error, closed} = gen_tcp:recv(Server, 1, 2000),
ok = gen_tcp:close(Server),
ok = gen_tcp:close(L),
?P("done"),
ok.
%%% implicit inet6 option to api functions
t_implicit_inet6(Config) when is_list(Config) ->
?TC_TRY(t_implicit_inet6, fun() -> do_t_implicit_inet6(Config) end).
do_t_implicit_inet6(Config) ->
?P("try get hostname"),
Host = ok(inet:gethostname()),
?P("try get address for host ~p", [Host]),
case inet:getaddr(Host, inet6) of
{ok, Addr} ->
?P("address: ~p", [Addr]),
t_implicit_inet6(Config, Host, Addr);
{error, Reason} ->
{skip,
"Can not look up IPv6 address: "
++atom_to_list(Reason)}
end.
t_implicit_inet6(Config, Host, Addr) ->
Loopback = {0,0,0,0,0,0,0,1},
InetBackendOpts = ?INET_BACKEND_OPTS(Config),
case gen_tcp:listen(0, InetBackendOpts ++ [inet6, {ip,Loopback}]) of
{ok, S1} ->
?P("try ~s ~p", ["::1", Loopback]),
implicit_inet6(Config, S1, Loopback),
ok = gen_tcp:close(S1),
%%
LocalAddr = ok(get_localaddr()),
S2 = case gen_tcp:listen(0, InetBackendOpts ++ [{ip, LocalAddr}]) of
{ok, LSock2} ->
LSock2;
{error, Reason2} ->
?P("Listen failed (ip):"
"~n Reason2: ~p", [Reason2]),
?SKIPT(listen_failed_str(Reason2))
end,
implicit_inet6(Config, S2, LocalAddr),
ok = gen_tcp:close(S2),
%%
?P("try ~s ~p", [Host, Addr]),
S3 = case gen_tcp:listen(0, InetBackendOpts ++ [{ifaddr,Addr}]) of
{ok, LSock3} ->
LSock3;
{error, Reason3} ->
?P("Listen failed (ifaddr):"
"~n Reason3: ~p", [Reason3]),
?SKIPT(listen_failed_str(Reason3))
end,
implicit_inet6(Config, S3, Addr),
ok = gen_tcp:close(S3),
?P("done"),
ok;
{error, Reason1} ->
?SKIPT(listen_failed_str(Reason1))
end.
implicit_inet6(Config, S, Addr) ->
P = ok(inet:port(S)),
S2 = case gen_tcp:connect(Addr, P, ?INET_BACKEND_OPTS(Config)) of
{ok, CSock} ->
CSock;
{error, CReason} ->
?SKIPT(connect_failed_str(CReason))
end,
P2 = ok(inet:port(S2)),
S1 = case gen_tcp:accept(S) of
{ok, ASock} ->
ASock;
{error, AReason} ->
?SKIPT(accept_failed_str(AReason))
end,
P1 = P = ok(inet:port(S1)),
{Addr,P2} = ok(inet:peername(S1)),
{Addr,P1} = ok(inet:peername(S2)),
{Addr,P1} = ok(inet:sockname(S1)),
{Addr,P2} = ok(inet:sockname(S2)),
ok = gen_tcp:close(S2),
ok = gen_tcp:close(S1).
t_local_basic(Config) ->
SFile = local_filename(server),
SAddr = {local, bin_filename(SFile)},
CFile = local_filename(client),
CAddr = {local,bin_filename(CFile)},
_ = file:delete(SFile),
_ = file:delete(CFile),
%%
?P("try create listen socket"),
InetBackendOpts = ?INET_BACKEND_OPTS(Config),
L =
ok(
gen_tcp:listen(0, InetBackendOpts ++
[{ifaddr,{local,SFile}},{active,false}])),
?P("try connect"),
C =
ok(
gen_tcp:connect(
{local,SFile}, 0, InetBackendOpts ++
[{ifaddr,{local,CFile}},{active,false}])),
?P("try accept connection"),
S = ok(gen_tcp:accept(L)),
?P("try get sockname for listen socket"),
%% SAddr = ok(inet:sockname(L)),
case inet:sockname(L) of
{ok, SAddr} ->
ok;
{ok, SAddr2} ->
?P("Invalid sockname: "
"~n Expected: ~p"
"~n Actual: ~p", [SAddr, SAddr2]),
exit({sockename, SAddr, SAddr2});
{error, Reason} ->
exit({sockname, Reason})
end,
?P("try get peername for listen socket"),
{error, enotconn} = inet:peername(L),
?P("try handshake"),
local_handshake(S, SAddr, C, CAddr),
?P("try close listen socket"),
ok = gen_tcp:close(L),
?P("try close accept socket"),
ok = gen_tcp:close(S),
?P("try close connect socket"),
ok = gen_tcp:close(C),
%%
?P("try 'local' files"),
ok = file:delete(SFile),
ok = file:delete(CFile),
?P("done"),
ok.
t_local_unbound(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() -> is_not_windows() end,
fun() -> do_local_unbound(Config) end).
do_local_unbound(Config) ->
?P("create local (server) filename"),
SFile = local_filename(server),
SAddr = {local,bin_filename(SFile)},
_ = file:delete(SFile),
%%
InetBackendOpts = ?INET_BACKEND_OPTS(Config),
?P("create listen socket with ifaddr ~p", [SAddr]),
L = ok(gen_tcp:listen(0, InetBackendOpts ++
[{ifaddr,SAddr},{active,false}])),
?P("listen socket created: ~p"
"~n => try connect", [L]),
C = ok(gen_tcp:connect(SAddr, 0,
InetBackendOpts ++ [{active,false}])),
?P("connected: ~p"
"~n => try accept", [C]),
S = ok(gen_tcp:accept(L)),
?P("accepted: ~p"
"~n => sockname", [S]),
SAddr = ok(inet:sockname(L)),
?P("sockname: ~p"
"~n => peername (expect enotconn)", [SAddr]),
{error, enotconn} = inet:peername(L),
?P("try local handshake"),
local_handshake(S, SAddr, C, {local,<<>>}),
?P("close listen socket"),
ok = gen_tcp:close(L),
?P("close accepted socket"),
ok = gen_tcp:close(S),
?P("close connected socket"),
ok = gen_tcp:close(C),
?P("delete (local) file"),
ok = file:delete(SFile),
?P("done"),
ok.
t_local_fdopen(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() -> is_not_windows() end,
fun() -> do_local_fdopen(Config) end).
do_local_fdopen(Config) ->
?P("create local (server) filename"),
SFile = local_filename(server),
SAddr = {local,bin_filename(SFile)},
_ = file:delete(SFile),
%%
InetBackendOpts = ?INET_BACKEND_OPTS(Config),
ListenOpts = InetBackendOpts ++ [{ifaddr,SAddr},{active,false}],
?P("create listen socket with ListenOpts ~p", [ListenOpts]),
L = ok(gen_tcp:listen(0, ListenOpts)),
ConnectOpts = InetBackendOpts ++ [{active,false}],
?P("listen socket created: ~p"
"~n => try connect ~p", [L, ConnectOpts]),
C0 = ok(gen_tcp:connect(SAddr, 0, ConnectOpts)),
ok = inet:setopts(C0, [{debug, true}]),
?P("connected: ~p"
"~n => get fd", [C0]),
Fd = if
is_port(C0) ->
FD0 = ok(prim_inet:getfd(C0)),
?P("FD: ~p"
"~n => ignore fd", [FD0]),
%% Turn off C0, so it does not generate any events!
ok = prim_inet:ignorefd(C0, true),
FD0;
true ->
[{fd, FD0}] = ok(inet:getopts(C0, [fd])),
?P("FD: ~p", [FD0]),
FD0
end,
?P("ignored fd:"
"~n => try fdopen (local)"),
C = ok(gen_tcp:fdopen(Fd, ?INET_BACKEND_OPTS(Config) ++ [local])),
?P("fd open: ~p"
"~n => try accept", [C]),
S = ok(gen_tcp:accept(L)),
?P("accepted: ~p"
"~n => get sockname", [S]),
SAddr = ok(inet:sockname(L)),
?P("sockname: ~p"
"~n => try get peername (expect enotconn)", [SAddr]),
{error,enotconn} = inet:peername(L),
?P("try local handshake"),
local_handshake(S, SAddr, C, {local,<<>>}),
?P("close listen socket"),
ok = gen_tcp:close(L),
?P("close accepted socket"),
ok = gen_tcp:close(S),
?P("close connected socket (final)"),
ok = gen_tcp:close(C),
?P("close connected socket (pre)"),
ok = gen_tcp:close(C0),
?P("delete (local) file"),
ok = file:delete(SFile),
?P("done"),
ok.
t_local_fdopen_listen(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() -> is_not_windows() end,
fun() -> do_local_fdopen_listen(Config) end).
do_local_fdopen_listen(Config) ->
?P("create local (server) filename"),
SFile = local_filename(server),
SAddr = {local,bin_filename(SFile)},
_ = file:delete(SFile),
InetBackendOpts = ?INET_BACKEND_OPTS(Config),
?P("create dummy listen socket with ifaddr ~p", [SAddr]),
L0 = ok(gen_tcp:listen(0, InetBackendOpts ++
[{ifaddr,SAddr},{active,false}])),
?P("dummy listen socket created: ~p"
"~n => try get FD", [L0]),
Fd = if
is_port(L0) ->
ok(prim_inet:getfd(L0));
true ->
[{fd, FD0}] = ok(inet:getopts(L0, [fd])),
FD0
end,
?P("FD: ~p"
"~n => try create proper listen socket (using fd)", [Fd]),
L = ok(gen_tcp:listen(0, InetBackendOpts ++
[{fd,Fd},local,{active,false}])),
?P("try connect"),
C = ok(gen_tcp:connect(SAddr, 0, InetBackendOpts ++ [{active,false}])),
?P("try accept (connection)"),
S = ok(gen_tcp:accept(L)),
?P("verify (proper) listen socket sockname"),
SAddr = ok(inet:sockname(L)),
?P("verify (proper) listen socket peername (expect enotconn)"),
{error, enotconn} = inet:peername(L),
?P("perform handshake"),
local_handshake(S, SAddr, C, {local,<<>>}),
?P("close (proper) listen socket"),
ok = gen_tcp:close(L),
?P("close (dummy) listen socket"),
ok = gen_tcp:close(L0),
?P("close accepted socket"),
ok = gen_tcp:close(S),
?P("close connected socket"),
ok = gen_tcp:close(C),
?P("delete file (used for socket)"),
ok = file:delete(SFile),
?P("done"),
ok.
t_local_fdopen_listen_unbound(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() -> is_not_windows() end,
fun() -> do_local_fdopen_listen_unbound(Config) end).
do_local_fdopen_listen_unbound(Config) ->
SFile = local_filename(server),
SAddr = {local,bin_filename(SFile)},
_ = file:delete(SFile),
P = ok(prim_inet:open(tcp, local, stream)),
Fd = ok(prim_inet:getfd(P)),
InetBackendOpts = ?INET_BACKEND_OPTS(Config),
L =
ok(gen_tcp:listen(
0, InetBackendOpts ++ [{fd,Fd},{ifaddr,SAddr},{active,false}])),
C = ok(gen_tcp:connect(SAddr, 0, InetBackendOpts ++ [{active,false}])),
S = ok(gen_tcp:accept(L)),
SAddr = ok(inet:sockname(L)),
{error,enotconn} = inet:peername(L),
local_handshake(S, SAddr, C, {local,<<>>}),
ok = gen_tcp:close(L),
ok = gen_tcp:close(P),
ok = gen_tcp:close(S),
ok = gen_tcp:close(C),
ok = file:delete(SFile),
ok.
t_local_fdopen_connect(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() -> is_not_windows() end,
fun() -> do_local_fdopen_connect(Config) end).
do_local_fdopen_connect(Config) ->
SFile = local_filename(server),
SAddr = {local,bin_filename(SFile)},
CFile = local_filename(client),
CAddr = {local,bin_filename(CFile)},
_ = file:delete(SFile),
_ = file:delete(CFile),
InetBackendOpts = ?INET_BACKEND_OPTS(Config),
L = ok(gen_tcp:listen(0, InetBackendOpts ++ [{ifaddr,SAddr},{active,false}])),
P = ok(prim_inet:open(tcp, local, stream)),
Fd = ok(prim_inet:getfd(P)),
C =
ok(gen_tcp:connect(
SAddr, 0, InetBackendOpts ++
[{fd,Fd},{ifaddr,CAddr},{active,false}])),
S = ok(gen_tcp:accept(L)),
SAddr = ok(inet:sockname(L)),
{error,enotconn} = inet:peername(L),
local_handshake(S, SAddr, C, CAddr),
ok = gen_tcp:close(L),
ok = gen_tcp:close(S),
ok = gen_tcp:close(C),
ok = gen_tcp:close(P),
ok = file:delete(SFile),
ok.
t_local_fdopen_connect_unbound(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() -> is_not_windows() end,
fun() -> do_local_fdopen_connect_unbound(Config) end).
do_local_fdopen_connect_unbound(Config) ->
SFile = local_filename(server),
SAddr = {local,bin_filename(SFile)},
_ = file:delete(SFile),
InetBackendOpts = ?INET_BACKEND_OPTS(Config),
L = ok(gen_tcp:listen(0, InetBackendOpts ++ [{ifaddr,SAddr},{active,false}])),
P = ok(prim_inet:open(tcp, local, stream)),
Fd = ok(prim_inet:getfd(P)),
C = ok(gen_tcp:connect(SAddr, 0, InetBackendOpts ++ [{fd,Fd},{active,false}])),
S = ok(gen_tcp:accept(L)),
SAddr = ok(inet:sockname(L)),
{error,enotconn} = inet:peername(L),
local_handshake(S, SAddr, C, {local,<<>>}),
ok = gen_tcp:close(L),
ok = gen_tcp:close(S),
ok = gen_tcp:close(C),
ok = gen_tcp:close(P),
ok = file:delete(SFile),
ok.
t_local_abstract(Config) ->
?TC_TRY(t_local_abstract, fun() -> do_local_abstract(Config) end).
do_local_abstract(Config) ->
?P("only run on linux"),
case os:type() of
{unix, linux} ->
AbstAddr = {local,<<>>},
InetBackendOpts = ?INET_BACKEND_OPTS(Config),
?P("create listen socket"),
L =
ok(gen_tcp:listen(
0, InetBackendOpts ++ [{ifaddr,AbstAddr},{active,false}])),
?P("listen socket created: ~p"
"~n => sockname", [L]),
{local, _} = SAddr = ok(inet:sockname(L)),
?P("(listen socket) sockname verified"
"~n => try connect"),
C =
ok(gen_tcp:connect(
SAddr, 0,
InetBackendOpts ++ [{ifaddr,AbstAddr},{active,false}])),
?P("connected: ~p"
"~n => sockname", [C]),
{local,_} = CAddr = ok(inet:sockname(C)),
?P("(connected socket) sockname verified"
"~n => try accept"),
S = ok(gen_tcp:accept(L)),
?P("accepted: ~p"
"~n => peername (expect enotconn)", [S]),
{error,enotconn} = inet:peername(L),
?P("try local handshake"),
local_handshake(S, SAddr, C, CAddr),
?P("close listen socket"),
ok = gen_tcp:close(L),
?P("close accepted socket"),
ok = gen_tcp:close(S),
?P("close connected socket"),
ok = gen_tcp:close(C),
?P("done"),
ok;
_ ->
?P("skip (unless linux)"),
{skip,"AF_LOCAL Abstract Addresses only supported on Linux"}
end.
local_handshake(S, SAddr, C, CAddr) ->
?P("~w -> entry with"
"~n Server Sock: ~p"
"~n Server Sock Info: ~p"
"~n Server Addr: ~p"
"~n Client Sock: ~p"
"~n Client Sock Info: ~p"
"~n Client Addr: ~p"
"~n", [?FUNCTION_NAME,
S, inet:info(S), SAddr,
C, inet:info(C), CAddr]),
SData = "9876543210",
CData = "0123456789",
?P("~w -> verify server sockname", [?FUNCTION_NAME]),
SAddr = ok(inet:sockname(S)),
?P("~w -> verify client sockname", [?FUNCTION_NAME]),
CAddr = ok(inet:sockname(C)),
?P("~w -> verify server peername", [?FUNCTION_NAME]),
CAddr = ok(inet:peername(S)),
?P("~w -> verify client peername", [?FUNCTION_NAME]),
SAddr = ok(inet:peername(C)),
?P("~w -> send data on client socket", [?FUNCTION_NAME]),
ok = gen_tcp:send(C, CData),
?P("~w -> send data on server socket", [?FUNCTION_NAME]),
ok = gen_tcp:send(S, SData),
?P("~w -> recv data on server socket", [?FUNCTION_NAME]),
CData = ok(gen_tcp:recv(S, length(CData))),
?P("~w -> recv data on client socket", [?FUNCTION_NAME]),
SData = ok(gen_tcp:recv(C, length(SData))),
?P("~w -> done", [?FUNCTION_NAME]),
ok.
t_accept_inet6_tclass(Config) when is_list(Config) ->
?TC_TRY(t_accept_inet6_tclass, fun() -> do_accept_inet6_tclass(Config) end).
do_accept_inet6_tclass(Config) ->
TClassOpt = {tclass, 8#56 bsl 2}, % Expedited forwarding
Loopback = {0,0,0,0,0,0,0,1},
?P("create listen socket with tclass: ~p", [TClassOpt]),
case gen_tcp:listen(0, ?INET_BACKEND_OPTS(Config) ++
[inet6, {ip, Loopback}, TClassOpt]) of
{ok, L} ->
?P("listen socket created: "
"~n ~p", [L]),
LPort = ok(inet:port(L)),
?P("try to connect to port ~p", [LPort]),
Sa = ok(gen_tcp:connect(Loopback, LPort,
?INET_BACKEND_OPTS(Config))),
?P("connected: ~p"
"~n => accept connection", [Sa]),
Sb = ok(gen_tcp:accept(L)),
?P("accepted: ~p"
"~n => getopts (tclass)", [Sb]),
[TClassOpt] = ok(inet:getopts(Sb, [tclass])),
?P("tclass verified => close accepted socket"),
ok = gen_tcp:close(Sb),
?P("close connected socket"),
ok = gen_tcp:close(Sa),
?P("close listen socket"),
ok = gen_tcp:close(L),
?P("done"),
ok;
{error, _Reason} ->
?P("ERROR: Failed create listen socket"
"~n ~p", [_Reason]),
{skip,"IPv6 TCLASS not supported"}
end.
%% Here we use socket:sockaddr_in6() when creating and using the
%% socket(s).
%%
t_simple_local_sockaddr_in6_send_recv(Config) when is_list(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() -> ?LIB:has_support_ipv6() end,
fun() ->
Domain = inet6,
{ok, LocalAddr} = ?LIB:which_local_addr(Domain),
SockAddr = #{family => Domain,
addr => LocalAddr,
port => 0},
do_simple_sockaddr_send_recv(SockAddr, Config)
end).
t_simple_link_local_sockaddr_in6_send_recv(Config) when is_list(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() ->
?LIB:has_support_ipv6(),
is_net_supported()
end,
fun() ->
Domain = inet6,
LinkLocalAddr =
case ?LIB:which_link_local_addr(Domain) of
{ok, LLA} ->
LLA;
{error, _} ->
skip("No link local address")
end,
Filter =
fun(#{addr := #{family := D,
addr := A}}) ->
(D =:= Domain) andalso (A =:= LinkLocalAddr);
(_) ->
false
end,
case net:getifaddrs(Filter) of
{ok, [#{addr := #{scope_id := ScopeID}}|_]} ->
SockAddr = #{family => Domain,
addr => LinkLocalAddr,
port => 0,
scope_id => ScopeID},
do_simple_sockaddr_send_recv(SockAddr, Config);
{ok, _} ->
skip("Scope ID not found");
{error, R} ->
skip({failed_getifaddrs, R})
end
end).
%% Here we use socket:sockaddr_in() when creating and using the
%% socket(s).
%%
t_simple_local_sockaddr_in_send_recv(Config) when is_list(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() -> ok end,
fun() ->
Domain = inet,
{ok, LocalAddr} = ?LIB:which_local_addr(Domain),
SockAddr = #{family => Domain,
addr => LocalAddr,
port => 0},
do_simple_sockaddr_send_recv(SockAddr, Config)
end).
t_simple_link_local_sockaddr_in_send_recv(Config) when is_list(Config) ->
?TC_TRY(?FUNCTION_NAME,
fun() -> ok end,
fun() ->
Domain = inet,
LinkLocalAddr =
case ?LIB:which_link_local_addr(Domain) of
{ok, LLA} ->
LLA;
{error, _} ->
skip("No link local address")
end,
SockAddr = #{family => Domain,
addr => LinkLocalAddr,
port => 0},
do_simple_sockaddr_send_recv(SockAddr, Config)
end).
do_simple_sockaddr_send_recv(SockAddr, _) ->
%% Create the server
Self = self(),
?P("~n SockAddr: ~p", [SockAddr]),
ServerF = fun() ->
?P("try create listen socket"),
LSock =
try gen_tcp:listen(0, [{ifaddr, SockAddr},
{active, true},
binary]) of
{ok, LS} ->
LS;
{error, LReason} ->
?P("listen error: "
"~n Reason: ~p", [LReason]),
exit({listen_error, LReason})
catch
LC:LE:LS ->
?P("listen failure: "
"~n Error Class: ~p"
"~n Error: ~p"
"~n Call Stack: ~p", [LC, LE, LS]),
exit({listen_failure, {LC, LE, LS}})
end,
?P("try get listen port"),
{ok, Port} = inet:port(LSock),
?P("listen port: ~w", [Port]),
Self ! {{port, Port}, self()},
?P("try accept"),
{ok, ASock} = gen_tcp:accept(LSock),
?P("accepted: "
"~n ~p", [ASock]),
receive
{tcp, ASock, <<"hej">>} ->
?P("received expected message - send reply"),
ok = gen_tcp:send(ASock, "hopp")
end,
?P("await termination command"),
receive
{die, Self} ->
?P("terminating"),
(catch gen_tcp:close(ASock)),
(catch gen_tcp:close(LSock)),
exit(normal)
end
end,
?P("try start server"),
Server = spawn_link(ServerF),
?P("server started - await port "),
ServerPort = receive
{{port, Port}, Server} ->
Port;
{'EXIT', Server, Reason} ->
?P("server died unexpectedly: "
"~n ~p", [Reason]),
exit({unexpected_listen_failure, Reason})
end,
?P("server port received: ~p", [ServerPort]),
?P("try connect to server"),
ServerSockAddr = SockAddr#{port => ServerPort},
{ok, CSock} = gen_tcp:connect(ServerSockAddr,
[{ifaddr, SockAddr},
{active, true},
binary]),
?P("connected to server: "
"~n CSock: ~p"
"~n CPort: ~p", [CSock, inet:port(CSock)]),
?P("try send message"),
ok = gen_tcp:send(CSock, "hej"),
?P("await reply message"),
receive
{tcp, CSock, <<"hopp">>} ->
?P("received expected reply message")
end,
?P("terminate server"),
Server ! {die, self()},
?P("await server termination"),
receive
{'EXIT', Server, normal} ->
ok
end,
?P("cleanup"),
(catch gen_tcp:close(CSock)),
?P("done"),
ok.
%% On MacOS (maybe more), accepting a connection resulted in a crash.
%% Note that since 'socket' currently does not work on windows
%% we have to skip on that platform.
s_accept_with_explicit_socket_backend(Config) when is_list(Config) ->
Cond = fun() ->
is_socket_supported()
end,
Pre = fun() ->
case ?WHICH_LOCAL_ADDR(inet) of
{ok, Addr} ->
Addr;
{error, Reason} ->
skip(Reason)
end
end,
TC = fun(Addr) -> do_s_accept_with_explicit_socket_backend(Addr) end,
Post = fun(_) -> ok end,
?TC_TRY(?FUNCTION_NAME,
Cond, Pre, TC, Post).
do_s_accept_with_explicit_socket_backend(Addr) ->
?P("try create listen socket"),
{ok, S} = gen_tcp:listen(0, [{inet_backend, socket}, {ip, Addr}]),
?P("try get port number (via sockname)"),
{ok, {_, Port}} = inet:sockname(S),
ClientF = fun() ->
?P("[client] try connect (tp ~p)", [Port]),
{ok, _} = gen_tcp:connect(Addr, Port, [{ip, Addr}]),
?P("[client] connected - wait for termination command"),
receive
die ->
?P("[client] termination command received"),
exit(normal)
after infinity -> ok
end
end,
?P("create client"),
Client = spawn_link(ClientF),
?P("try accept"),
{ok, _} = gen_tcp:accept(S),
?P("accepted - command client to terminate"),
Client ! die,
?P("done"),
ok.
%%% Utilities
is_socket_supported() ->
try socket:info() of
_ -> ok
catch
error : notsup ->
{skip, "esock not supported"};
error : undef ->
{skip, "esock not configured"}
end.
%% Calls M:F/length(A), which should return a timeout error, and complete
%% within the given time.
timeout({M,F,A}, Lower, Upper) ->
case test_server:timecall(M, F, A) of
{Time, Result} when Time < Lower ->
ct:fail({too_short_time, Time, Result});
{Time, Result} when Time > Upper ->
ct:fail({too_long_time, Time, Result});
{_, {error, timeout}} ->
ok;
{_, Result} ->
ct:fail({unexpected_result, Result})
end.
connect_timeout({M,F,A}, Lower, Upper) ->
case test_server:timecall(M, F, A) of
{Time, Result} when Time < Lower ->
case Result of
{error, econnrefused = E} ->
{skip, "Not tested -- got error " ++ atom_to_list(E)};
{error, enetunreach = E} ->
{skip, "Not tested -- got error " ++ atom_to_list(E)};
{error, ehostunreach = E} ->
{skip, "Not tested -- got error " ++ atom_to_list(E)};
{ok, Socket} -> % What the...
Pinfo = erlang:port_info(Socket),
Db = inet_db:lookup_socket(Socket),
Peer = inet:peername(Socket),
ct:fail({too_short_time, Time,
[Result,Pinfo,Db,Peer]});
_ ->
ct:fail({too_short_time, Time, Result})
end;
{Time, Result} when Time > Upper ->
ct:fail({too_long_time, Time, Result});
{_, {error, timeout}} ->
ok;
{_, Result} ->
ct:fail({unexpected_result, Result})
end.
%% Try to obtain an unused IP address in the local network.
unused_ip() ->
{ok, Host} = inet:gethostname(),
{ok, Hent} = inet:gethostbyname(Host),
#hostent{h_addr_list=[{A, B, C, _D}|_]} = Hent,
%% Note: In our net, addresses below 16 are reserved for routers and
%% other strange creatures.
IP = unused_ip(A, B, C, 16),
if
(IP =:= error) ->
%% This is not supported on all platforms (yet), so...
try net:getifaddrs() of
{ok, IfAddrs} ->
?P("~n we = ~p"
"~n unused_ip = ~p"
"~n ~p", [Hent, IP, IfAddrs]);
{error, _} ->
?P("~n we: ~p"
"~n unused_ip: ~p", [Hent, IP])
catch
_:_:_ ->
?P("~n we: ~p"
"~n unused_ip: ~p", [Hent, IP])
end;
true ->
?P("~n we: ~p"
"~n unused_ip: ~p", [Hent, IP])
end,
IP.
unused_ip(255, 255, 255, 255) -> error;
unused_ip(255, B, C, D) -> unused_ip(1, B + 1, C, D);
unused_ip(A, 255, C, D) -> unused_ip(A, 1, C + 1, D);
unused_ip(A, B, 255, D) -> unused_ip(A, B, 1, D + 1);
unused_ip(A, B, C, D) ->
case inet:gethostbyaddr({A, B, C, D}) of
{ok, _} -> unused_ip(A + 1, B, C, D);
{error, _} -> {ok, {A, B, C, D}}
end.
ok({ok,V}) -> V;
ok(NotOk) ->
try throw(not_ok)
catch
throw:Thrown:Stacktrace ->
erlang:raise(
error, {Thrown, NotOk}, tl(Stacktrace))
end.
get_localaddr() ->
get_localaddr(["localhost", "localhost6", "ip6-localhost"]).
get_localaddr([]) ->
{error, localaddr_not_found};
get_localaddr([Localhost|Ls]) ->
case inet:getaddr(Localhost, inet6) of
{ok, LocalAddr} ->
?P("~s ~p", [Localhost, LocalAddr]),
{ok, LocalAddr};
_ ->
get_localaddr(Ls)
end.
getsockfd() -> undefined.
closesockfd(_FD) -> undefined.
-define(WIN32_LOCAL_PRE, "").
-define(UNIX_LOCAL_PRE, "/tmp/").
local_filename(Tag) ->
{OSF, _} = os:type(),
local_filename(OSF, Tag).
local_filename(win32, Tag) ->
local_filename(?WIN32_LOCAL_PRE, ".sock", Tag);
local_filename(_, Tag) ->
local_filename(?UNIX_LOCAL_PRE, "", Tag).
local_filename(Pre, Post, Tag) ->
?F("~s~s_~s_~w_~w~s",
[Pre,
?MODULE_STRING,os:getpid(),erlang:system_time(millisecond),Tag,
Post]).
%% Pre ++
%% ?MODULE_STRING "_" ++ os:getpid() ++ "_" ++ atom_to_list(Tag) ++
%% Post.
bin_filename(String) ->
unicode:characters_to_binary(String, file:native_name_encoding()).
delete_local_filenames() ->
{OSF, _} = os:type(),
delete_local_filenames(OSF).
delete_local_filenames(win32) ->
do_delete_local_filenames(?WIN32_LOCAL_PRE);
delete_local_filenames(_) ->
do_delete_local_filenames(?UNIX_LOCAL_PRE).
do_delete_local_filenames(Pre) ->
_ =
[file:delete(F) ||
F <-
filelib:wildcard(
Pre ++ ?MODULE_STRING "_" ++ os:getpid() ++ "_*")],
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
is_not_windows() ->
case os:type() of
{win32, nt} ->
skip("This does not work on Windows");
_ ->
ok
end.
is_net_supported() ->
try net:info() of
#{} ->
ok
catch
error : notsup ->
not_supported(net)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
not_supported(What) ->
skip({not_supported, What}).
skip(Reason) ->
throw({skip, Reason}).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pi(Item) ->
{Item, Val} = process_info(self(), Item),
Val.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
connect_failed_str(Reason) ->
?F("Connect failed: ~w", [Reason]).
listen_failed_str(Reason) ->
?F("Listen failed: ~w", [Reason]).
accept_failed_str(Reason) ->
?F("Accept failed: ~w", [Reason]).
|