main.c 46.6 KB
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
/*
 * Copyright (C) 2016 Freie Universitรคt Berlin
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup     tests
 * @{
 *
 * @file
 * @brief       Test for UDP socks
 *
 * @author      Martine Lenders <m.lenders@fu-berlin.de>
 * @}
 */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/uio.h>

#include "net/ipv6/addr.h"
#include "net/sock/tcp.h"
#include "sched.h"
#include "thread.h"
#include "xtimer.h"

#include "constants.h"
#include "stack.h"

#define _TEST_BUFFER_SIZE   (128)
#define _QUEUE_SIZE         (1)

#define _MSG_QUEUE_SIZE     (4)
#define _CLIENT_BUF_SIZE    (128)
#define _SERVER_BUF_SIZE    (128)
#define _SERVER_QUEUE_SIZE  (1)
#define _CLIENT_MSG_START   (0xe307)
#define _CLIENT_MSG_READ    (0xe308)
#define _CLIENT_MSG_WRITE   (0xe309)
#define _CLIENT_MSG_STOP    (0xe30a)
#define _SERVER_MSG_START   (0xe30b)
#define _SERVER_MSG_ACCEPT  (0xe30c)
#define _SERVER_MSG_READ    (0xe30d)
#define _SERVER_MSG_WRITE   (0xe30e)
#define _SERVER_MSG_CLOSE   (0xe30f)
#define _SERVER_MSG_STOP    (0xe310)

static uint8_t _test_buffer[_TEST_BUFFER_SIZE];

static char _client_stack[THREAD_STACKSIZE_DEFAULT];
static char _server_stack[THREAD_STACKSIZE_DEFAULT];
static uint8_t _client_buf[_CLIENT_BUF_SIZE];
static uint8_t _server_buf[_SERVER_BUF_SIZE];
static msg_t _client_msg_queue[_MSG_QUEUE_SIZE];
static msg_t _server_msg_queue[_MSG_QUEUE_SIZE];
static sock_tcp_t _sock, _client_sock;
static sock_tcp_t _queue_array[_QUEUE_SIZE];
static sock_tcp_t _server_queue_array[_SERVER_QUEUE_SIZE];
static sock_tcp_queue_t _queue, _server_queue;
static sock_tcp_ep_t _server_addr;
static kernel_pid_t _server, _client;

#define CALL(fn)            puts("Calling " # fn); fn; tear_down()

static void *_server_func(void *arg);
static void *_client_func(void *arg);

static void tear_down(void)
{
    msg_t msg = { .type = _CLIENT_MSG_STOP };
    msg_send(&msg, _client);
    msg.type = _SERVER_MSG_STOP;
    msg_send(&msg, _server);
    sock_tcp_disconnect(&_sock);
    sock_tcp_stop_listen(&_queue);
    memset(&_sock, 0, sizeof(_sock));
    memset(&_queue, 0, sizeof(_queue));
    memset(&_server_addr, 0, sizeof(_server_addr));
}

#ifdef MODULE_LWIP_IPV4
#ifdef SO_REUSE
static void test_tcp_connect4__EADDRINUSE(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const uint16_t local_port = _TEST_PORT_REMOTE;

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);    /* start server on _TEST_PORT_REMOTE */

    assert(-EADDRINUSE == sock_tcp_connect(&_sock, &remote, local_port, 0));
}
#endif

static void test_tcp_connect4__EAFNOSUPPORT(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };

    assert(-EAFNOSUPPORT == sock_tcp_connect(&_sock, &remote, 0,
                                             SOCK_FLAGS_REUSE_EP));
}

/* ECONNREFUSED does not apply for lwIP; netconn_connect does not wait for
 * connection build-up */

static void test_tcp_connect4__EINVAL_addr(void)
{
    static const sock_tcp_ep_t remote = { .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };

    assert(-EINVAL == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
}

static void test_tcp_connect4__EINVAL_netif(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = (_TEST_NETIF + 1) };

    assert(-EINVAL == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
}

/* ENETUNREACH not testable in given loopback setup */
/* ETIMEDOUT doesn't work because lwIP's connect doesn't timeout */

static void test_tcp_connect4__success_without_port(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = _TEST_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    sock_tcp_ep_t ep;

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);    /* start server on _TEST_PORT_REMOTE */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    assert(0 == sock_tcp_get_remote(&_sock, &ep));
    assert(AF_INET == ep.family);
    assert(htonl(_TEST_ADDR4_REMOTE) == ep.addr.ipv4_u32);
    assert(SOCK_ADDR_ANY_NETIF == ep.netif);
    assert(_TEST_PORT_REMOTE == ep.port);
}
static void test_tcp_connect4__success_local_port(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const uint16_t local_port = _TEST_PORT_LOCAL;
    sock_tcp_ep_t ep;

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);    /* start server on _TEST_PORT_REMOTE */

    assert(0 == sock_tcp_connect(&_sock, &remote, local_port, SOCK_FLAGS_REUSE_EP));
    assert(0 == sock_tcp_get_local(&_sock, &ep));
    assert(AF_INET == ep.family);
    assert(_TEST_PORT_LOCAL == ep.port);
    assert(0 == sock_tcp_get_remote(&_sock, &ep));
    assert(AF_INET == ep.family);
    assert(htonl(_TEST_ADDR4_REMOTE) == ep.addr.ipv4_u32);
    assert(SOCK_ADDR_ANY_NETIF == ep.netif);
    assert(_TEST_PORT_REMOTE == ep.port);
}

#ifdef SO_REUSE
static void test_tcp_listen4__EADDRINUSE(void)
{
    const sock_tcp_ep_t local = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_LOCAL) },
                                         .family = AF_INET,
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_LOCAL;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);    /* start server on _TEST_PORT_LOCAL */

    assert(-EADDRINUSE == sock_tcp_listen(&_queue, &local, _queue_array,
                                          _QUEUE_SIZE, 0));
}
#endif

static void test_tcp_listen4__EAFNOSUPPORT(void)
{
    const sock_tcp_ep_t local = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_LOCAL) },
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = SOCK_ADDR_ANY_NETIF };

    assert(-EAFNOSUPPORT == sock_tcp_listen(&_queue, &local, _queue_array,
                                            _QUEUE_SIZE, 0));
}

static void test_tcp_listen4__EINVAL(void)
{
    const sock_tcp_ep_t local = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_LOCAL) },
                                         .family = AF_INET,
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = (_TEST_NETIF + 1) };

    assert(-EINVAL == sock_tcp_listen(&_queue, &local, _queue_array,
                                      _QUEUE_SIZE, 0));
}

static void test_tcp_listen4__success_any_netif(void)
{
    const sock_tcp_ep_t local = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_LOCAL) },
                                         .family = AF_INET,
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = SOCK_ADDR_ANY_NETIF };
    sock_tcp_ep_t ep;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, 0));
    assert(0 == sock_tcp_queue_get_local(&_queue, &ep));
    assert(AF_INET == ep.family);
    assert(htonl(_TEST_ADDR4_LOCAL) == ep.addr.ipv4_u32);
    assert(SOCK_ADDR_ANY_NETIF == ep.netif);
    assert(_TEST_PORT_LOCAL == ep.port);
}

static void test_tcp_listen4__success_spec_netif(void)
{
    static const sock_tcp_ep_t local = { .family = AF_INET,
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = _TEST_NETIF };
    sock_tcp_ep_t ep;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, 0));
    assert(0 == sock_tcp_queue_get_local(&_queue, &ep));
    assert(AF_INET == ep.family);
    assert(_TEST_NETIF == ep.netif);
    assert(_TEST_PORT_LOCAL == ep.port);
}

/* ECONNABORTED can't be tested in this setup */

static void test_tcp_accept4__EAGAIN(void)
{
    static const sock_tcp_ep_t local = { .family = AF_INET,
                                         .port = _TEST_PORT_LOCAL };
    sock_tcp_t *sock;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, SOCK_FLAGS_REUSE_EP));
    assert(-EAGAIN == sock_tcp_accept(&_queue, &sock, 0));
}

static void test_tcp_accept4__EINVAL(void)
{
    sock_tcp_t *sock;

    assert(-EINVAL == sock_tcp_accept(&_queue, &sock, SOCK_NO_TIMEOUT));
}

static void test_tcp_accept4__ETIMEDOUT(void)
{
    static const sock_tcp_ep_t local = { .family = AF_INET,
                                         .port = _TEST_PORT_LOCAL };
    sock_tcp_t *sock;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, SOCK_FLAGS_REUSE_EP));
    puts(" * Calling sock_tcp_accept()");
    assert(-ETIMEDOUT == sock_tcp_accept(&_queue, &sock, _TEST_TIMEOUT));
    printf(" * (timed out with timeout %u)\n", _TEST_TIMEOUT);
}

static void test_tcp_accept4__success(void)
{
    static const sock_tcp_ep_t local = { .family = AF_INET,
                                         .port = _TEST_PORT_LOCAL };
    msg_t msg = { .type = _CLIENT_MSG_START,
                  .content = { .value = _TEST_PORT_REMOTE } };
    sock_tcp_ep_t ep;
    sock_tcp_t *sock;

    _server_addr.addr.ipv4_u32 = htonl(_TEST_ADDR4_REMOTE);  /* loopback */
    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_LOCAL;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, 0));
    msg_send(&msg, _client);    /* start client on _TEST_PORT_REMOTE, connecting
                                 * to _TEST_PORT_LOCAL */
    assert(0 == sock_tcp_accept(&_queue, &sock, SOCK_NO_TIMEOUT));
    assert(0 == sock_tcp_get_local(sock, &ep));
    assert(AF_INET == ep.family);
    assert(_TEST_PORT_LOCAL == ep.port);
    assert(0 == sock_tcp_get_remote(sock, &ep));
    assert(AF_INET == ep.family);
    assert(htonl(_TEST_ADDR4_REMOTE) == ep.addr.ipv4_u32);
    assert(SOCK_ADDR_ANY_NETIF == ep.netif);
    assert(_TEST_PORT_REMOTE == ep.port);
}

/* ECONNABORTED can't be tested in this setup */

static void test_tcp_read4__EAGAIN(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    assert(-EAGAIN == sock_tcp_read(&_sock, _test_buffer, sizeof(_test_buffer), 0));
}

static void test_tcp_read4__ECONNRESET(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_CLOSE;
    msg_send(&msg, _server);        /* close connection at server side */
    assert(-ECONNRESET == sock_tcp_read(&_sock, _test_buffer,
                                        sizeof(_test_buffer),
                                        SOCK_NO_TIMEOUT));
}

static void test_tcp_read4__ENOTCONN(void)
{
    assert(-ENOTCONN == sock_tcp_read(&_sock, _test_buffer,
                                      sizeof(_test_buffer), SOCK_NO_TIMEOUT));
}

static void test_tcp_read4__ETIMEDOUT(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    puts(" * Calling sock_tcp_read()");
    assert(-ETIMEDOUT == sock_tcp_read(&_sock, _test_buffer, sizeof(_test_buffer),
                                       _TEST_TIMEOUT));
    printf(" * (timed out with timeout %u)\n", _TEST_TIMEOUT);
}

static void test_tcp_read4__success(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const struct iovec exp_data = { .iov_base = "Hello!",
                                           .iov_len = sizeof("Hello!") };

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_WRITE;
    msg.content.ptr = (void *)&exp_data;
    msg_send(&msg, _server);        /* write expected data at server */
    assert(((ssize_t)exp_data.iov_len) == sock_tcp_read(&_sock, _test_buffer,
                                                        sizeof(_test_buffer),
                                                        SOCK_NO_TIMEOUT));
    assert(memcmp(exp_data.iov_base, _test_buffer, exp_data.iov_len) == 0);
}

static void test_tcp_read4__success_with_timeout(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const struct iovec exp_data = { .iov_base = "Hello!",
                                           .iov_len = sizeof("Hello!") };

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_WRITE;
    msg.content.ptr = (void *)&exp_data;
    msg_send(&msg, _server);        /* write expected data at server */
    assert(((ssize_t)exp_data.iov_len) == sock_tcp_read(&_sock, _test_buffer,
                                                        sizeof(_test_buffer),
                                                        _TEST_TIMEOUT));
    assert(memcmp(exp_data.iov_base, _test_buffer, exp_data.iov_len) == 0);
}

static void test_tcp_read4__success_non_blocking(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const struct iovec exp_data = { .iov_base = "Hello!",
                                           .iov_len = sizeof("Hello!") };

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_WRITE;
    msg.content.ptr = (void *)&exp_data;
    msg_send(&msg, _server);        /* write expected data at server */
    assert(((ssize_t)exp_data.iov_len) == sock_tcp_read(&_sock, _test_buffer,
                                                        sizeof(_test_buffer),
                                                        0));
    assert(memcmp(exp_data.iov_base, _test_buffer, exp_data.iov_len) == 0);
}

/* ENOTCONN not applicable since lwIP always tries to send */

static void test_tcp_write4__ENOTCONN(void)
{
    assert(-ENOTCONN == sock_tcp_write(&_sock, "Hello!", sizeof("Hello!")));
}

static void test_tcp_write4__success(void)
{
    const sock_tcp_ep_t remote = { .addr = { .ipv4_u32 = htonl(_TEST_ADDR4_REMOTE) },
                                          .family = AF_INET,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const struct iovec exp_data = { .iov_base = "Hello!",
                                           .iov_len = sizeof("Hello!") };

    _server_addr.family = AF_INET;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_READ;
    msg.content.ptr = (void *)&exp_data;
    msg_send(&msg, _server);        /* write expected data at server */
    assert(((ssize_t)exp_data.iov_len) == sock_tcp_write(&_sock, "Hello!",
                                                        sizeof("Hello!")));
    assert(memcmp(exp_data.iov_base, _test_buffer, exp_data.iov_len) == 0);
    xtimer_usleep(5000);            /* wait for server */
}
#endif /* MODULE_LWIP_IPV4 */

#ifdef MODULE_LWIP_IPV6
#ifdef SO_REUSE
static void test_tcp_connect6__EADDRINUSE(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const uint16_t local_port = _TEST_PORT_REMOTE;

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);    /* start server on _TEST_PORT_REMOTE */

    assert(-EADDRINUSE == sock_tcp_connect(&_sock, &remote, local_port, 0));
}
#endif

static void test_tcp_connect6__EAFNOSUPPORT(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };

    assert(-EAFNOSUPPORT == sock_tcp_connect(&_sock, &remote, 0,
                                             SOCK_FLAGS_REUSE_EP));
}

/* ECONNREFUSED does not apply for lwIP; netconn_connect does not wait for
 * connection build-up */

static void test_tcp_connect6__EINVAL_addr(void)
{
    static const sock_tcp_ep_t remote = { .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };

    assert(-EINVAL == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
}

static void test_tcp_connect6__EINVAL_netif(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = (_TEST_NETIF + 1) };

    assert(-EINVAL == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
}

/* ENETUNREACH not testable in given loopback setup */
/* ETIMEDOUT doesn't work because lwIP's connect doesn't timeout */

static void test_tcp_connect6__success_without_port(void)
{
    static const ipv6_addr_t remote_addr = { .u8 = _TEST_ADDR6_REMOTE };
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = _TEST_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    sock_tcp_ep_t ep;

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);    /* start server on _TEST_PORT_REMOTE */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    assert(0 == sock_tcp_get_remote(&_sock, &ep));
    assert(AF_INET6 == ep.family);
    assert(memcmp(&remote_addr, &ep.addr.ipv6, sizeof(ipv6_addr_t)) == 0);
    assert(SOCK_ADDR_ANY_NETIF == ep.netif);
    assert(_TEST_PORT_REMOTE == ep.port);
}
static void test_tcp_connect6__success_local_port(void)
{
    static const ipv6_addr_t remote_addr = { .u8 = _TEST_ADDR6_REMOTE };
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const uint16_t local_port = _TEST_PORT_LOCAL;
    sock_tcp_ep_t ep;

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);    /* start server on _TEST_PORT_REMOTE */

    assert(0 == sock_tcp_connect(&_sock, &remote, local_port, SOCK_FLAGS_REUSE_EP));
    assert(0 == sock_tcp_get_local(&_sock, &ep));
    assert(AF_INET6 == ep.family);
    assert(_TEST_PORT_LOCAL == ep.port);
    assert(0 == sock_tcp_get_remote(&_sock, &ep));
    assert(AF_INET6 == ep.family);
    assert(memcmp(&remote_addr, &ep.addr.ipv6, sizeof(ipv6_addr_t)) == 0);
    assert(SOCK_ADDR_ANY_NETIF == ep.netif);
    assert(_TEST_PORT_REMOTE == ep.port);
}

#ifdef SO_REUSE
static void test_tcp_listen6__EADDRINUSE(void)
{
    static const sock_tcp_ep_t local = { .addr = { .ipv6 = _TEST_ADDR6_LOCAL },
                                         .family = AF_INET6,
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_LOCAL;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);    /* start server on _TEST_PORT_LOCAL */

    assert(-EADDRINUSE == sock_tcp_listen(&_queue, &local, _queue_array,
                                          _QUEUE_SIZE, 0));
}
#endif

static void test_tcp_listen6__EAFNOSUPPORT(void)
{
    static const sock_tcp_ep_t local = { .addr = { .ipv6 = _TEST_ADDR6_LOCAL },
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = SOCK_ADDR_ANY_NETIF };

    assert(-EAFNOSUPPORT == sock_tcp_listen(&_queue, &local, _queue_array,
                                            _QUEUE_SIZE, 0));
}

static void test_tcp_listen6__EINVAL(void)
{
    static const sock_tcp_ep_t local = { .addr = { .ipv6 = _TEST_ADDR6_LOCAL },
                                         .family = AF_INET6,
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = (_TEST_NETIF + 1) };

    assert(-EINVAL == sock_tcp_listen(&_queue, &local, _queue_array,
                                      _QUEUE_SIZE, 0));
}

static void test_tcp_listen6__success_any_netif(void)
{
    static const ipv6_addr_t local_addr = { .u8 = _TEST_ADDR6_LOCAL };
    static const sock_tcp_ep_t local = { .addr = { .ipv6 = _TEST_ADDR6_LOCAL },
                                         .family = AF_INET6,
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = SOCK_ADDR_ANY_NETIF };
    sock_tcp_ep_t ep;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, 0));
    assert(0 == sock_tcp_queue_get_local(&_queue, &ep));
    assert(AF_INET6 == ep.family);
    assert(memcmp(&local_addr, &ep.addr.ipv6, sizeof(ipv6_addr_t)) == 0);
    assert(SOCK_ADDR_ANY_NETIF == ep.netif);
    assert(_TEST_PORT_LOCAL == ep.port);
}

static void test_tcp_listen6__success_spec_netif(void)
{
    static const sock_tcp_ep_t local = { .family = AF_INET6,
                                         .port = _TEST_PORT_LOCAL,
                                         .netif = _TEST_NETIF };
    sock_tcp_ep_t ep;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, 0));
    assert(0 == sock_tcp_queue_get_local(&_queue, &ep));
    assert(AF_INET6 == ep.family);
    assert(_TEST_NETIF == ep.netif);
    assert(_TEST_PORT_LOCAL == ep.port);
}

/* ECONNABORTED can't be tested in this setup */

static void test_tcp_accept6__EAGAIN(void)
{
    static const sock_tcp_ep_t local = { .family = AF_INET6,
                                         .port = _TEST_PORT_LOCAL };
    sock_tcp_t *sock;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, 0));
    assert(-EAGAIN == sock_tcp_accept(&_queue, &sock, 0));
}

static void test_tcp_accept6__EINVAL(void)
{
    sock_tcp_t *sock;

    assert(-EINVAL == sock_tcp_accept(&_queue, &sock, SOCK_NO_TIMEOUT));
}

static void test_tcp_accept6__ETIMEDOUT(void)
{
    static const sock_tcp_ep_t local = { .family = AF_INET6,
                                         .port = _TEST_PORT_LOCAL };
    sock_tcp_t *sock;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, 0));
    puts(" * Calling sock_tcp_accept()");
    assert(-ETIMEDOUT == sock_tcp_accept(&_queue, &sock, _TEST_TIMEOUT));
    printf(" * (timed out with timeout %u)\n", _TEST_TIMEOUT);
}

static void test_tcp_accept6__success(void)
{
    static const ipv6_addr_t remote_addr = { .u8 = _TEST_ADDR6_REMOTE };
    static const sock_tcp_ep_t local = { .family = AF_INET6,
                                         .port = _TEST_PORT_LOCAL };
    msg_t msg = { .type = _CLIENT_MSG_START,
                  .content = { .value = _TEST_PORT_REMOTE } };
    sock_tcp_ep_t ep;
    sock_tcp_t *sock;

    _server_addr.addr.ipv6[15] = 1; /* make unspecified address to loopback */
    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_LOCAL;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    assert(0 == sock_tcp_listen(&_queue, &local, _queue_array,
                                _QUEUE_SIZE, 0));
    msg_send(&msg, _client);    /* start client on _TEST_PORT_REMOTE, connecting
                                 * to _TEST_PORT_LOCAL */
    assert(0 == sock_tcp_accept(&_queue, &sock, SOCK_NO_TIMEOUT));
    assert(0 == sock_tcp_get_local(sock, &ep));
    assert(AF_INET6 == ep.family);
    assert(_TEST_PORT_LOCAL == ep.port);
    assert(0 == sock_tcp_get_remote(sock, &ep));
    assert(AF_INET6 == ep.family);
    assert(memcmp(&remote_addr, &ep.addr.ipv6, sizeof(ipv6_addr_t)) == 0);
    assert(SOCK_ADDR_ANY_NETIF == ep.netif);
    assert(_TEST_PORT_REMOTE == ep.port);
}

/* ECONNABORTED can't be tested in this setup */

static void test_tcp_read6__EAGAIN(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    assert(-EAGAIN == sock_tcp_read(&_sock, _test_buffer, sizeof(_test_buffer), 0));
}

static void test_tcp_read6__ECONNRESET(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_CLOSE;
    msg_send(&msg, _server);        /* close connection at server side */
    assert(-ECONNRESET == sock_tcp_read(&_sock, _test_buffer,
                                        sizeof(_test_buffer), SOCK_NO_TIMEOUT));
}

static void test_tcp_read6__ENOTCONN(void)
{
    assert(-ENOTCONN == sock_tcp_read(&_sock, _test_buffer,
                                      sizeof(_test_buffer), SOCK_NO_TIMEOUT));
}

static void test_tcp_read6__ETIMEDOUT(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    puts(" * Calling sock_tcp_read()");
    assert(-ETIMEDOUT == sock_tcp_read(&_sock, _test_buffer,
                                       sizeof(_test_buffer), _TEST_TIMEOUT));
    printf(" * (timed out with timeout %u)\n", _TEST_TIMEOUT);
}
static void test_tcp_read6__success(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const struct iovec exp_data = { .iov_base = "Hello!",
                                           .iov_len = sizeof("Hello!") };

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_WRITE;
    msg.content.ptr = (void *)&exp_data;
    msg_send(&msg, _server);        /* write expected data at server */
    assert(((ssize_t)exp_data.iov_len) == sock_tcp_read(&_sock, _test_buffer,
                                                        sizeof(_test_buffer),
                                                        SOCK_NO_TIMEOUT));
    assert(memcmp(exp_data.iov_base, _test_buffer, exp_data.iov_len) == 0);
}

static void test_tcp_read6__success_with_timeout(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const struct iovec exp_data = { .iov_base = "Hello!",
                                           .iov_len = sizeof("Hello!") };

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_WRITE;
    msg.content.ptr = (void *)&exp_data;
    msg_send(&msg, _server);        /* write expected data at server */
    assert(((ssize_t)exp_data.iov_len) == sock_tcp_read(&_sock, _test_buffer,
                                                        sizeof(_test_buffer),
                                                        _TEST_TIMEOUT));
    assert(memcmp(exp_data.iov_base, _test_buffer, exp_data.iov_len) == 0);
}

static void test_tcp_read6__success_non_blocking(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const struct iovec exp_data = { .iov_base = "Hello!",
                                           .iov_len = sizeof("Hello!") };

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_WRITE;
    msg.content.ptr = (void *)&exp_data;
    msg_send(&msg, _server);        /* write expected data at server */
    assert(((ssize_t)exp_data.iov_len) == sock_tcp_read(&_sock, _test_buffer,
                                                        sizeof(_test_buffer),
                                                        0));
    assert(memcmp(exp_data.iov_base, _test_buffer, exp_data.iov_len) == 0);
}

/* ENOTCONN not applicable since lwIP always tries to send */

static void test_tcp_write6__ENOTCONN(void)
{
    assert(-ENOTCONN == sock_tcp_write(&_sock, "Hello!", sizeof("Hello!")));
}

static void test_tcp_write6__success(void)
{
    static const sock_tcp_ep_t remote = { .addr = { .ipv6 = _TEST_ADDR6_REMOTE },
                                          .family = AF_INET6,
                                          .port = _TEST_PORT_REMOTE,
                                          .netif = SOCK_ADDR_ANY_NETIF };
    msg_t msg = { .type = _SERVER_MSG_START };
    static const struct iovec exp_data = { .iov_base = "Hello!",
                                           .iov_len = sizeof("Hello!") };

    _server_addr.family = AF_INET6;
    _server_addr.port = _TEST_PORT_REMOTE;
    _server_addr.netif = SOCK_ADDR_ANY_NETIF;

    msg_send(&msg, _server);        /* start server on _TEST_PORT_LOCAL */
    msg.type = _SERVER_MSG_ACCEPT;
    msg_send(&msg, _server);        /* let server accept */

    assert(0 == sock_tcp_connect(&_sock, &remote, 0, SOCK_FLAGS_REUSE_EP));
    msg.type = _SERVER_MSG_READ;
    msg.content.ptr = (void *)&exp_data;
    msg_send(&msg, _server);        /* write expected data at server */
    assert(((ssize_t)exp_data.iov_len) == sock_tcp_write(&_sock, "Hello!",
                                                        sizeof("Hello!")));
    assert(memcmp(exp_data.iov_base, _test_buffer, exp_data.iov_len) == 0);
    xtimer_usleep(5000);            /* wait for server */
}
#endif /* MODULE_LWIP_IPV6 */

int main(void)
{
    uint8_t code = 0;

#ifdef SO_REUSE
    code |= 1;
#endif
#ifdef MODULE_LWIP_IPV4
    code |= (1 << 4);
#endif
#ifdef MODULE_LWIP_IPV6
    code |= (1 << 6);
#endif
    printf("code 0x%02x\n", code);
    xtimer_init();
    _net_init();
    assert(0 < thread_create(_client_stack, sizeof(_client_stack),
                             THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST,
                             _client_func, NULL, "tcp_client"));
    assert(0 < thread_create(_server_stack, sizeof(_server_stack),
                             THREAD_PRIORITY_MAIN - 2, THREAD_CREATE_STACKTEST,
                             _server_func, NULL, "tcp_server"));
    tear_down();
#ifdef MODULE_LWIP_IPV4
#ifdef SO_REUSE
    CALL(test_tcp_connect4__EADDRINUSE());
#endif
    CALL(test_tcp_connect4__EAFNOSUPPORT());
    /* ECONNREFUSED does not apply for lwIP; netconn_connect does not wait for
     * connection build-up */
    CALL(test_tcp_connect4__EINVAL_addr());
    CALL(test_tcp_connect4__EINVAL_netif());
    /* ENETUNREACH not testable in given loopback setup */
    /* ETIMEDOUT doesn't work because lwIP's connect doesn't timeout */
    CALL(test_tcp_connect4__success_without_port());
    CALL(test_tcp_connect4__success_local_port());
#ifdef SO_REUSE
    CALL(test_tcp_listen4__EADDRINUSE());
#endif
    CALL(test_tcp_listen4__EAFNOSUPPORT());
    CALL(test_tcp_listen4__EINVAL());
    CALL(test_tcp_listen4__success_any_netif());
    CALL(test_tcp_listen4__success_spec_netif());
    /* sock_tcp_disconnect() is tested in tear_down() */
    /* sock_tcp_stop_listen() is tested in tear_down() */
    /* sock_tcp_get_local() is tested in sock_tcp_connect() tests */
    /* sock_tcp_get_remote() is tested in sock_tcp_connect() tests */
    /* sock_tcp_queue_get_local() is tested in sock_tcp_listen() tests */
    /* ECONNABORTED can't be tested in this setup */
    CALL(test_tcp_accept4__EAGAIN());
    CALL(test_tcp_accept4__EINVAL());
    CALL(test_tcp_accept4__ETIMEDOUT());
    CALL(test_tcp_accept4__success());
    /* ECONNABORTED can't be tested in this setup */
    CALL(test_tcp_read4__EAGAIN());
    CALL(test_tcp_read4__ECONNRESET());
    CALL(test_tcp_read4__ENOTCONN());
    CALL(test_tcp_read4__ETIMEDOUT());
    CALL(test_tcp_read4__success());
    CALL(test_tcp_read4__success_with_timeout());
    CALL(test_tcp_read4__success_non_blocking());
    /* ECONNABORTED can't be tested in this setup */
    /* ENOTCONN not applicable since lwIP always tries to send */
    CALL(test_tcp_write4__ENOTCONN());
    CALL(test_tcp_write4__success());
#endif /* MODULE_LWIP_IPV4 */
#ifdef MODULE_LWIP_IPV6
#ifdef SO_REUSE
    CALL(test_tcp_connect6__EADDRINUSE());
#endif
    CALL(test_tcp_connect6__EAFNOSUPPORT());
    /* ECONNREFUSED does not apply for lwIP; netconn_connect does not wait for
     * connection build-up */
    CALL(test_tcp_connect6__EINVAL_addr());
    CALL(test_tcp_connect6__EINVAL_netif());
    /* ENETUNREACH not testable in given loopback setup */
    /* ETIMEDOUT doesn't work because lwIP's connect doesn't timeout */
    CALL(test_tcp_connect6__success_without_port());
    CALL(test_tcp_connect6__success_local_port());
#ifdef SO_REUSE
    CALL(test_tcp_listen6__EADDRINUSE());
#endif
    CALL(test_tcp_listen6__EAFNOSUPPORT());
    CALL(test_tcp_listen6__EINVAL());
    CALL(test_tcp_listen6__success_any_netif());
    CALL(test_tcp_listen6__success_spec_netif());
    /* sock_tcp_disconnect() is tested in tear_down() */
    /* sock_tcp_stop_listen() is tested in tear_down() */
    /* sock_tcp_get_local() is tested in sock_tcp_connect() tests */
    /* sock_tcp_get_remote() is tested in sock_tcp_connect() tests */
    /* sock_tcp_queue_get_local() is tested in sock_tcp_listen() tests */
    /* ECONNABORTED can't be tested in this setup */
    CALL(test_tcp_accept6__EAGAIN());
    CALL(test_tcp_accept6__EINVAL());
    CALL(test_tcp_accept6__ETIMEDOUT());
    CALL(test_tcp_accept6__success());
    /* ECONNABORTED can't be tested in this setup */
    CALL(test_tcp_read6__EAGAIN());
    CALL(test_tcp_read6__ECONNRESET());
    CALL(test_tcp_read6__ENOTCONN());
    CALL(test_tcp_read6__ETIMEDOUT());
    CALL(test_tcp_read6__success());
    CALL(test_tcp_read6__success_with_timeout());
    CALL(test_tcp_read6__success_non_blocking());
    /* ECONNABORTED can't be tested in this setup */
    /* ENOTCONN not applicable since lwIP always tries to send */
    CALL(test_tcp_write6__ENOTCONN());
    CALL(test_tcp_write6__success());
#endif /* MODULE_LWIP_IPV6 */

    puts("ALL TESTS SUCCESSFUL");

    return 0;
}

static void *_server_func(void *arg)
{
    bool server_started = false;
    sock_tcp_t *sock = NULL;

    (void)arg;
    msg_init_queue(_server_msg_queue, _MSG_QUEUE_SIZE);
    _server = sched_active_pid;
    while (1) {
        msg_t msg;

        msg_receive(&msg);
        switch (msg.type) {
            case _SERVER_MSG_START:
                if (!server_started) {
                    assert(0 == sock_tcp_listen(&_server_queue, &_server_addr,
                                                _server_queue_array,
                                                _SERVER_QUEUE_SIZE,
                                                SOCK_FLAGS_REUSE_EP));
                    server_started = true;
                }
                break;
            case _SERVER_MSG_ACCEPT:
                if (server_started) {
                    assert(0 == sock_tcp_accept(&_server_queue, &sock,
                                                SOCK_NO_TIMEOUT));
                }
                break;
            case _SERVER_MSG_READ:
                if (sock != NULL) {
                    const struct iovec *exp = msg.content.ptr;

                    assert(((ssize_t)exp->iov_len) ==
                           sock_tcp_read(sock, _server_buf, sizeof(_server_buf),
                                         SOCK_NO_TIMEOUT));
                    assert(memcmp(exp->iov_base, _server_buf, exp->iov_len) == 0);
                }
                break;
            case _SERVER_MSG_WRITE:
                if (sock != NULL) {
                    const struct iovec *data = msg.content.ptr;

                    assert(((ssize_t)data->iov_len) ==
                           sock_tcp_write(sock, data->iov_base, data->iov_len));
                }
                break;
            case _SERVER_MSG_CLOSE:
                if (sock != NULL) {
                    sock_tcp_disconnect(sock);
                    sock = NULL;
                }
                break;
            case _SERVER_MSG_STOP:
                if (server_started) {
                    sock_tcp_stop_listen(&_server_queue);
                    server_started = false;
                    /* sock_tcp_stop_listen is also supposed to close sock */
                    sock = NULL;
                }
                break;
            default:
                break;
        }
    }
    return NULL;
}

static void *_client_func(void *arg)
{
    bool client_started = false;

    (void)arg;
    msg_init_queue(_client_msg_queue, _MSG_QUEUE_SIZE);
    _client = sched_active_pid;
    while (1) {
        msg_t msg;

        msg_receive(&msg);
        switch (msg.type) {
            case _CLIENT_MSG_START:
                if (!client_started) {
                    const uint16_t local_port = (uint16_t)msg.content.value;
                    assert(0 == sock_tcp_connect(&_client_sock, &_server_addr,
                                                 local_port, SOCK_FLAGS_REUSE_EP));
                    client_started = true;
                }
                break;
            case _CLIENT_MSG_READ:
                if (client_started) {
                    const struct iovec *exp = msg.content.ptr;

                    assert(((ssize_t)exp->iov_len) ==
                           sock_tcp_read(&_client_sock, _client_buf,
                                         sizeof(_client_buf), SOCK_NO_TIMEOUT));
                    assert(memcmp(exp->iov_base, _client_buf, exp->iov_len) == 0);
                }
                break;
            case _CLIENT_MSG_WRITE:
                if (client_started) {
                    const struct iovec *data = msg.content.ptr;

                    assert(((ssize_t)data->iov_len) ==
                           sock_tcp_write(&_client_sock, data->iov_base,
                                          data->iov_len));
                }
                break;
            case _CLIENT_MSG_STOP:
                if (client_started) {
                    sock_tcp_disconnect(&_client_sock);
                    memset(&_client_sock, 0, sizeof(sock_tcp_t));
                    client_started = false;
                }
                break;
            default:
                break;
        }
    }
    return NULL;
}