Ejemplo n.º 1
0
int main ()
{
    int rc;
    int push;
    int pull1;
    int pull2;
    int sndprio;

    pull1 = test_socket (AF_SP, NN_PULL);
    test_bind (pull1, SOCKET_ADDRESS_A);
    pull2 = test_socket (AF_SP, NN_PULL);
    test_bind (pull2, SOCKET_ADDRESS_B);
    push = test_socket (AF_SP, NN_PUSH);
    sndprio = 1;
    rc = nn_setsockopt (push, NN_SOL_SOCKET, NN_SNDPRIO,
        &sndprio, sizeof (sndprio));
    errno_assert (rc == 0);
    test_connect (push, SOCKET_ADDRESS_A);
    sndprio = 2;
    rc = nn_setsockopt (push, NN_SOL_SOCKET, NN_SNDPRIO,
        &sndprio, sizeof (sndprio));
    errno_assert (rc == 0);
    test_connect (push, SOCKET_ADDRESS_B);

    test_send (push, "ABC");
    test_send (push, "DEF");
    test_recv (pull1, "ABC");
    test_recv (pull1, "DEF");

    test_close (pull1);
    test_close (push);
    test_close (pull2);

    /*  Test removing a pipe from the list. */

    push = test_socket (AF_SP, NN_PUSH);
    test_bind (push, SOCKET_ADDRESS_A);
    pull1 = test_socket (AF_SP, NN_PULL);
    test_connect (pull1, SOCKET_ADDRESS_A);

    test_send (push, "ABC");
    test_recv (pull1, "ABC");
    test_close (pull1);

    rc = nn_send (push, "ABC", 3, NN_DONTWAIT);
    nn_assert (rc == -1 && nn_errno() == EAGAIN);

    pull1 = test_socket (AF_SP, NN_PULL);
    test_connect (pull1, SOCKET_ADDRESS_A);

    test_send (push, "ABC");
    test_recv (pull1, "ABC");
    test_close (pull1);
    test_close (push);

    return 0;
}
Ejemplo n.º 2
0
int main ()
{
    int rc;
    int pub1;
    int pub2;
    int sub1;
    int sub2;
    char buf [3];

    pub1 = test_socket (AF_SP, NN_PUB);
    test_bind (pub1, SOCKET_ADDRESS);
    sub1 = test_socket (AF_SP, NN_SUB);
    rc = nn_setsockopt (sub1, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
    errno_assert (rc == 0);
    test_connect (sub1, SOCKET_ADDRESS);
    sub2 = test_socket (AF_SP, NN_SUB);
    rc = nn_setsockopt (sub2, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
    errno_assert (rc == 0);
    test_connect (sub2, SOCKET_ADDRESS);

    /*  Wait till connections are established to prevent message loss. */
    nn_sleep (10);

    test_send (pub1, "0123456789012345678901234567890123456789");
    test_recv (sub1, "0123456789012345678901234567890123456789");
    test_recv (sub2, "0123456789012345678901234567890123456789");

    test_close (pub1);
    test_close (sub1);
    test_close (sub2);

    /*  Check receiving messages from two publishers. */

    sub1 = test_socket (AF_SP, NN_SUB);
    rc = nn_setsockopt (sub1, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
    errno_assert (rc == 0);
    test_bind (sub1, SOCKET_ADDRESS);
    pub1 = test_socket (AF_SP, NN_PUB);
    test_connect (pub1, SOCKET_ADDRESS);
    pub2 = test_socket (AF_SP, NN_PUB);
    test_connect (pub2, SOCKET_ADDRESS);
    nn_sleep (100);

    test_send (pub1, "0123456789012345678901234567890123456789");
    test_send (pub2, "0123456789012345678901234567890123456789");
    test_recv (sub1, "0123456789012345678901234567890123456789");
    test_recv (sub1, "0123456789012345678901234567890123456789");

    test_close (pub2);
    test_close (pub1);
    test_close (sub1);

    return 0;
}
Ejemplo n.º 3
0
int main ()
{
    int push1;
    int push2;
    int pull1;
    int pull2;

    /*  Test fan-out. */

    push1 = test_socket (AF_SP, NN_PUSH);
    test_bind (push1, SOCKET_ADDRESS);
    pull1 = test_socket (AF_SP, NN_PULL);
    test_connect (pull1, SOCKET_ADDRESS);
    pull2 = test_socket (AF_SP, NN_PULL);
    test_connect (pull2, SOCKET_ADDRESS);

    /*  Wait till both connections are established to get messages spread
        evenly between the two pull sockets. */
    nn_sleep (10);

    test_send (push1, "ABC");
    test_send (push1, "DEF");

    test_recv (pull1, "ABC");
    test_recv (pull2, "DEF");

    test_close (push1);
    test_close (pull1);
    test_close (pull2);

    /*  Test fan-in. */

    pull1 = test_socket (AF_SP, NN_PULL);
    test_bind (pull1, SOCKET_ADDRESS);
    push1 = test_socket (AF_SP, NN_PUSH);
    test_connect (push1, SOCKET_ADDRESS);
    push2 = test_socket (AF_SP, NN_PUSH);
    test_connect (push2, SOCKET_ADDRESS);

    test_send (push1, "ABC");
    test_send (push2, "DEF");

    test_recv (pull1, "ABC");
    test_recv (pull1, "DEF");

    test_close (pull1);
    test_close (push1);
    test_close (push2);

    return 0;
}
Ejemplo n.º 4
0
void device1(NN_UNUSED void *arg)
{
    int rc;

    /*  Intialise the device sockets. */
    deva = test_socket(AF_SP_RAW, NN_PAIR);
    test_bind(deva, SOCKET_ADDRESS_A);
    devb = test_socket(AF_SP_RAW, NN_PAIR);
    test_bind(devb, SOCKET_ADDRESS_B);

    /*  Run the device. */
    rc = nn_device(deva, devb);
    nn_assert(rc < 0 && (nn_errno() == EBADF));
}
Ejemplo n.º 5
0
void device2(NN_UNUSED void *arg)
{
    int rc;

    /*  Intialise the device sockets. */
    devc = test_socket(AF_SP_RAW, NN_PULL);
    test_bind(devc, SOCKET_ADDRESS_C);
    devd = test_socket(AF_SP_RAW, NN_PUSH);
    test_bind(devd, SOCKET_ADDRESS_D);

    /*  Run the device. */
    rc = nn_device(devc, devd);
    nn_assert(rc < 0 && nn_errno() == EBADF);
}
Ejemplo n.º 6
0
int main ()
{
#if !defined NN_HAVE_WINDOWS
    int sb;
    int i;
    int j;
    struct nn_thread threads [THREAD_COUNT];

    /*  Stress the shutdown algorithm. */

    sb = test_socket (AF_SP, NN_PUB);
    test_bind (sb, SOCKET_ADDRESS);

    for (j = 0; j != 10; ++j) {
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_init (&threads [i], routine, NULL);
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_term (&threads [i]);
    }

    test_close (sb);

#endif

    return 0;
}
Ejemplo n.º 7
0
int MessagingInterface::uniquePort(unsigned int start, unsigned int end) {
    int res = 0;
    char address_buf[40];
    std::set<unsigned int> checked_ports; // used to avoid duplicate checks and infinite loops
    while (true) {
        try{
            zmq::socket_t test_bind(*MessagingInterface::getContext(), ZMQ_PULL);
            res = random() % (end-start+1) + start;
            if (checked_ports.count(res) && checked_ports.size() < end-start+1) continue;
            checked_ports.insert(res);
            snprintf(address_buf, 40, "tcp://0.0.0.0:%d", res);
            test_bind.bind(address_buf);
            int linger = 0; // do not wait at socket close time
            test_bind.setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
            test_bind.disconnect(address_buf);
            DBG_CHANNELS << "found available port " << res << "\n";
            break;
        }
        catch (zmq::error_t err) {
            if (zmq_errno() != EADDRINUSE) {
                res = 0;
                break;
            }
            if (checked_ports.size() == end-start+1) {
                res = 0;
                break;
            }
        }
    }
    return res;
}
Ejemplo n.º 8
0
int testipc_shutdown()
{
    int sb;
    int i;
    int j;
    struct nn_thread threads [THREAD_COUNT];
    printf("test ipc shutdown\n");

    /*  Stress the shutdown algorithm. */

#if defined(SIGPIPE) && defined(SIG_IGN)
	signal (SIGPIPE, SIG_IGN);
#endif

    sb = test_socket (AF_SP, NN_PUB);
    test_bind (sb, SOCKET_ADDRESS);

    for (j = 0; j != TEST_LOOPS; ++j) {
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_init (&threads [i], routine, NULL);
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_term (&threads [i]);
    }

    test_close (sb);

    /*  Test race condition of sending message while socket shutting down  */

    sb = test_socket (AF_SP, NN_PUSH);
    test_bind (sb, SOCKET_ADDRESS);

    for (j = 0; j != TEST_LOOPS; ++j) {
        for (i = 0; i != TEST2_THREAD_COUNT; ++i)
            nn_thread_init (&threads [i], routine2, NULL);
        active = TEST2_THREAD_COUNT;
        while ( active )
        {
            (void) nn_send (sb, "hello", 5, NN_DONTWAIT);
        }
        for (i = 0; i != TEST2_THREAD_COUNT; ++i)
            nn_thread_term(&threads [i]);
    }

    test_close (sb);

    return 0;
}
Ejemplo n.º 9
0
int main ()
{
    int sb;
    int i;
    int j;
    struct nn_thread threads [THREAD_COUNT];

    /*  Stress the shutdown algorithm. */

    sb = test_socket (AF_SP, NN_PUB);
    test_bind (sb, SOCKET_ADDRESS);

    for (j = 0; j != TEST_LOOPS; ++j) {
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_init (&threads [i], routine, NULL);
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_term (&threads [i]);
    }

    test_close (sb);

    /*  Test race condition of sending message while socket shutting down  */

    sb = test_socket (AF_SP, NN_PUSH);
    test_bind (sb, SOCKET_ADDRESS);

    for (j = 0; j != TEST_LOOPS; ++j) {
        for (i = 0; i != TEST2_THREAD_COUNT; ++i)
            nn_thread_init (&threads [i], routine2, NULL);
        nn_atomic_init(&active, TEST2_THREAD_COUNT);

        while (active.n) {
            (void) nn_send (sb, "hello", 5, NN_DONTWAIT);
        }

        for (i = 0; i != TEST2_THREAD_COUNT; ++i)
            nn_thread_term (&threads [i]);
        nn_atomic_term(&active);
    }

    test_close (sb);

    return 0;
}
Ejemplo n.º 10
0
int
main(int argc, char *argv[])
{

	test_socket();
	test_socketpair();
	test_listen_unbound();
	test_bind();
	test_listen_bound();
}
Ejemplo n.º 11
0
void test_bind_nonexisting(bool use_ipv6) {
    warnx("If this test fails check if host %s accidentally exists\n", host1);

    if (g_test_subprocess()) {
        test_bind(host1, 6, use_ipv6);
    }

    g_test_trap_subprocess(NULL, 0, 0);
    g_test_trap_assert_failed();
}
Ejemplo n.º 12
0
void test_bind_local_root(bool use_ipv6) {
    warnx("If this test fails check that you are not root\n");

    if (g_test_subprocess()) {
        test_bind("localhost", 6, use_ipv6);
    }

    g_test_trap_subprocess(NULL, 0, 0);
    g_test_trap_assert_failed();
}
Ejemplo n.º 13
0
void device5 (NN_UNUSED void *arg)
{
    int rc;
    int dev0;
    int dev1;

    /*  Intialise the device sockets. */
    dev0 = test_socket (AF_SP_RAW, NN_REP);
    test_bind (dev0, socket_address_h);
    dev1 = test_socket (AF_SP_RAW, NN_REQ);
    test_bind (dev1, socket_address_i);

    /*  Run the device. */
    rc = nn_device (dev0, dev1);
    nn_assert (rc < 0 && nn_errno () == ETERM);

    /*  Clean up. */
    test_close (dev0);
    test_close (dev1);
}
Ejemplo n.º 14
0
Archivo: device.c Proyecto: 0xe/nanomsg
void device1 (NN_UNUSED void *arg)
{
    int rc;
    int deva;
    int devb;

    /*  Intialise the device sockets. */
    deva = test_socket (AF_SP_RAW, NN_PAIR);
    test_bind (deva, SOCKET_ADDRESS_A);
    devb = test_socket (AF_SP_RAW, NN_PAIR);
    test_bind (devb, SOCKET_ADDRESS_B);

    /*  Run the device. */
    rc = nn_device (deva, devb);
    nn_assert (rc < 0 && nn_errno () == ETERM);

    /*  Clean up. */
    test_close (devb);
    test_close (deva);
}
Ejemplo n.º 15
0
void device3(NN_UNUSED void *arg)
{
    int rc;

    /*  Intialise the device socket. */
    deve = test_socket(AF_SP_RAW, NN_BUS);
    test_bind(deve, SOCKET_ADDRESS_E);

    /*  Run the device. */
    rc = nn_device(deve, -1);
    nn_assert(rc < 0 && nn_errno() == EBADF);
}
Ejemplo n.º 16
0
void test_bind_local(bool use_ipv6) {
    uint16_t const test_port = 29407;
    warnx("If this test fails check that TCP port %d is unoccupied\n",
            test_port);

    if (g_test_subprocess()) {
        test_bind("localhost", test_port, use_ipv6);
        exit(0);
    }

    g_test_trap_subprocess(NULL, 0, 0);
    g_test_trap_assert_passed();
}
Ejemplo n.º 17
0
int main(int argc, char *argv[])
{
	printf("Userlevel Firewall Test Application!\n");

	printf("bind to port 1234: ");
	if (test_bind(1234) < 0) {
		printf("failed!\n");
		perror("cause");
		return 1;
	}
	printf("succeeded\n");

	printf("bind to port 4321 (expect failure): ");
	if (test_bind(4321) == 0) {
		printf("succeeded!\n");
		return 1;
	}
	printf("failed\n");

	printf("connect to www.scs.stanford.edu(171.66.3.9):80: ");
	if (test_connect("171.66.3.9", 80) < 0) {
		printf("failed!\n");
		perror("cause");
		return 1;
	}
	printf("succeeded\n");

	printf("connect to market.scs.stanford.edu(171.66.3.10):22: ");
	if (test_connect("171.66.3.10", 22) == 0) {
		printf("succeeded!\n");
		return 1;
	}
	printf("failed\n");

	printf("all tests passed!\n");

	return 0;
}
Ejemplo n.º 18
0
void device6 (NN_UNUSED void *arg)
{
    int rc;
    int dev2;
    int dev3;

    dev2 = test_socket (AF_SP_RAW, NN_REP);
    test_connect (dev2, socket_address_i);
    dev3 = test_socket (AF_SP_RAW, NN_REQ);
    test_bind (dev3, socket_address_j);

    /*  Run the device. */
    rc = nn_device (dev2, dev3);
    nn_assert (rc < 0 && nn_errno () == ETERM);

    /*  Clean up. */
    test_close (dev2);
    test_close (dev3);
}
Ejemplo n.º 19
0
int main ()
{
    int rc;
    int sb;
    int sc;
    struct nn_iovec iov [2];
    struct nn_msghdr hdr;
    char buf [6];

    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, SOCKET_ADDRESS);
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, SOCKET_ADDRESS);

    iov [0].iov_base = "AB";
    iov [0].iov_len = 2;
    iov [1].iov_base = "CDEF";
    iov [1].iov_len = 4;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = iov;
    hdr.msg_iovlen = 2;
    rc = nn_sendmsg (sc, &hdr, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 6);

    iov [0].iov_base = buf;
    iov [0].iov_len = 4;
    iov [1].iov_base = buf + 4;
    iov [1].iov_len = 2;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = iov;
    hdr.msg_iovlen = 2;
    rc = nn_recvmsg (sb, &hdr, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 6);
    nn_assert (memcmp (buf, "ABCDEF", 6) == 0);

    test_close (sc);
    test_close (sb);

    return 0;
}
Ejemplo n.º 20
0
int main (NN_UNUSED int argc, const NN_UNUSED char *argv[])
{
    int sb;
    int sc1;
    int sc2;

    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, "inproc://pair");
    sc1 = test_socket (AF_SP, NN_PAIR);
    test_connect (sc1, "inproc://pair");
    sc2 = test_socket (AF_SP, NN_PAIR);
    test_connect (sc2, "inproc://pair");

    test_send (sb, "HELLO");
    test_recv (sc1, "HELLO");

    test_send (sc1, "THERE");
    test_recv (sb, "THERE");
    return 0;
}
Ejemplo n.º 21
0
int main ()
{
    struct nn_thread thread;

    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, SOCKET_ADDRESS);
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, SOCKET_ADDRESS);

    nn_thread_init (&thread, worker, NULL);

    test_recv (sb, "ABC");
    test_recv (sb, "ABC");

    nn_thread_term (&thread);

    test_close (sc);
    test_close (sb);

    return 0;
}
Ejemplo n.º 22
0
int main ()
{
    int sb;
    int i;
    int j;
    struct grid_thread threads [THREAD_COUNT];

    /*  Stress the shutdown algorithm. */

    sb = test_socket (AF_SP, GRID_PUB);
    test_bind (sb, SOCKET_ADDRESS);

    for (j = 0; j != 10; ++j) {
        for (i = 0; i != THREAD_COUNT; ++i)
            grid_thread_init (&threads [i], routine, NULL);
        for (i = 0; i != THREAD_COUNT; ++i)
            grid_thread_term (&threads [i]);
    }

    test_close (sb);

    return 0;
}
Ejemplo n.º 23
0
/*  test_text() verifies that we drop messages properly when sending invalid
    UTF-8, but not when we send valid data. */
void test_text ()
{
    int sb;
    int sc;
    int opt;
    uint8_t bad[20];

    /*  Negative testing... bad UTF-8 data for text. */
    sb = test_socket (AF_SP, NN_PAIR);
    sc = test_socket (AF_SP, NN_PAIR);

    opt = NN_WS_MSG_TYPE_TEXT;
    test_setsockopt (sb, NN_WS, NN_WS_MSG_TYPE, &opt, sizeof (opt));
    opt = NN_WS_MSG_TYPE_TEXT;
    test_setsockopt (sc, NN_WS, NN_WS_MSG_TYPE, &opt, sizeof (opt));
    opt = 500;
    test_setsockopt (sb, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sizeof (opt));

    test_bind (sb, socket_address);
    test_connect (sc, socket_address);

    test_send (sc, "GOOD");
    test_recv (sb, "GOOD");

    /*  and the bad ... */
    strcpy ((char *)bad, "BAD.");
    bad[2] = (char)0xDD;
    test_send (sc, (char *)bad);

    /*  Make sure we dropped the frame. */
    test_drop (sb, ETIMEDOUT);

    test_close (sb);
    test_close (sc);

    return;
}
Ejemplo n.º 24
0
int
main(int argc, char *argv[])
{
	int clt_sk[MAX_CLIENTS], accept_sk[MAX_CLIENTS];
	int listen_sk, clt2_sk, accept2_sk;
	sockaddr_storage_t clt_loop[MAX_CLIENTS];
	sockaddr_storage_t svr_loop, accept_loop, clt2_loop;
	socklen_t addrlen;
	int error, i;
        char *message = "hello, world!\n";
	char msgbuf[100];
	int pf_class;
	struct pollfd poll_fd;
	fd_set set;
	struct msghdr outmessage;
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct iovec out_iov;
	struct cmsghdr *cmsg;
	struct sctp_sndrcvinfo *sinfo;
	struct msghdr inmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char *big_buffer;
	struct iovec iov;

        /* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered.  
	 */ 
	setvbuf(stdout, NULL, _IONBF, 0); 

	/* Initialize the server and client addresses. */ 
#if TEST_V6
	pf_class = PF_INET6;
        svr_loop.v6.sin6_family = AF_INET6;
        svr_loop.v6.sin6_addr = in6addr_loopback;
        svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
	for (i = 0; i < MAX_CLIENTS; i++) {
        	clt_loop[i].v6.sin6_family = AF_INET6;
        	clt_loop[i].v6.sin6_addr = in6addr_loopback;
        	clt_loop[i].v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
	}
        clt2_loop.v6.sin6_family = AF_INET6;
        clt2_loop.v6.sin6_addr = in6addr_loopback;
        clt2_loop.v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
#else
	pf_class = PF_INET;
	svr_loop.v4.sin_family = AF_INET;
	svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
	for (i = 0; i < MAX_CLIENTS; i++) {
		clt_loop[i].v4.sin_family = AF_INET;
		clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
		clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
	}
	clt2_loop.v4.sin_family = AF_INET;
	clt2_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	clt2_loop.v4.sin_port = htons(SCTP_TESTPORT_2 + i);
#endif

	/* Create and bind the listening server socket.  */
        listen_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(listen_sk, &svr_loop.sa, sizeof(svr_loop));

	/* Mark listen_sk as being able to accept new associations.  */
	test_listen(listen_sk, MAX_CLIENTS-1);

	/* Create and bind the client sockets.  */
	for (i = 0; i < MAX_CLIENTS; i++) {
		clt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
		test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
	}
	clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop));

	addrlen = sizeof(accept_loop);
	/* Try to do accept on a non-listening socket. It should fail. */
	error = accept(clt_sk[0], &accept_loop.sa, &addrlen);
	if ((-1 != error) && (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "accept on non-listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept on non-listening socket");

	/* Try to do a connect from a listening socket. It should fail. */
	error = connect(listen_sk, (struct sockaddr *)&clt_loop[0],
			sizeof(clt_loop[0]));
	if ((-1 != error) && (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connect to non-listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "connect to non-listening socket");

	/* Do a blocking connect from clt_sk's to listen_sk */      
	for (i = 0; i < MAX_CLIENTS; i++)
		test_connect(clt_sk[i], &svr_loop.sa, sizeof(svr_loop));

	tst_resm(TPASS, "connect to listening socket");

	/* Verify that no more connect's can be done after the acceptq
	 * backlog has reached the max value.
	 */
	error = connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop));
	if ((-1 != error) && (ECONNREFUSED != errno))
		tst_brkm(TBROK, tst_exit, "connect after max backlog "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "connect after max backlog");

	/* Extract the associations on the listening socket as new sockets. */
	for (i = 0; i < MAX_CLIENTS; i++) {
		poll_fd.fd = listen_sk;
		poll_fd.events = POLLIN;
		poll_fd.revents = 0;
		error = poll(&poll_fd, 1, -1);
		if ((1 != error) && (1 != poll_fd.revents))
			tst_brkm(TBROK, tst_exit, "Unexpected return value "
				 "with poll, error:%d errno:%d, revents:%d",
				 error, errno, poll_fd.revents);

		addrlen = sizeof(accept_loop);
		accept_sk[i] = test_accept(listen_sk, &accept_loop.sa,
					   &addrlen); 
	}

	tst_resm(TPASS, "accept from listening socket");

	/* Try to do a connect on an established socket. It should fail. */
	error = connect(accept_sk[0], &clt_loop[0].sa, sizeof(clt_loop[0]));
	if ((-1 != error) || (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connect on an established socket "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "connect on an established socket");

	/* Try to do accept on an established socket. It should fail. */
	error = accept(accept_sk[0], &accept_loop.sa, &addrlen);
	if ((-1 != error) && (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "accept on an established socket "
			 "error:%d errno:%d", error, errno);

	error = accept(clt_sk[0], &accept_loop.sa, &addrlen);
	if ((-1 != error) && (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "accept on an established socket "
			 "failure: error:%d errno:%d", error, errno);

	tst_resm(TPASS, "accept on an established socket");

	/* Send and receive a message from the client sockets to the accepted
	 * sockets.
	 */
	for (i = 0; i < MAX_CLIENTS; i++) {
		test_send(clt_sk[i], message, strlen(message), 0);
		test_recv(accept_sk[i], msgbuf, 100, 0);
	}

	tst_resm(TPASS, "client sockets -> accepted sockets");

	/* Send and receive a message from the accepted sockets to the client
	 * sockets.
	 */
	for (i = 0; i < MAX_CLIENTS; i++) {
		test_send(accept_sk[i], message, strlen(message), 0);
		test_recv(clt_sk[i], msgbuf, 100, 0);
	}

	tst_resm(TPASS, "accepted sockets -> client sockets");

	/* Sending a message on a listening socket should fail. */
	error = send(listen_sk, message, strlen(message), MSG_NOSIGNAL);
	if ((-1 != error) || (EPIPE != errno))
		tst_brkm(TBROK, tst_exit, "send on a listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "send on a listening socket");

	/* Trying to receive a message on a listening socket should fail. */
	error = recv(listen_sk, msgbuf, 100, 0);
	if ((-1 != error) || (ENOTCONN != errno))
		tst_brkm(TBROK, tst_exit, "recv on a listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a listening socket");

	/* TESTCASES for shutdown() */
	errno = 0;
	test_send(accept_sk[0], message, strlen(message), 0);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(clt_sk[0]);

	/* Do a SHUT_WR on clt_sk[0] to disable any new sends. */
	test_shutdown(clt_sk[0], SHUT_WR);

	/* Reading on a socket that has received SHUTDOWN should return 0 
	 * indicating EOF.
	 */
	error = recv(accept_sk[0], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN received socket "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUTDOWN received socket");

	/* Read the pending message on clt_sk[0] that was received before
	 * SHUTDOWN call.
	 */  
	test_recv(clt_sk[0], msgbuf, 100, 0);

	/* Initialize inmessage for all receives. */
	big_buffer = test_malloc(REALLY_BIG);
	memset(&inmessage, 0, sizeof(inmessage));	
	iov.iov_base = big_buffer;
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);

	/* Receive the SHUTDOWN_COMP notification as they are enabled. */
	error = test_recvmsg(clt_sk[0], &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);

	tst_resm(TPASS, "recv SHUTDOWN_COMP notification on a SHUT_WR socket");

	/* No more messages and the association is SHUTDOWN, should fail. */
	error = recv(clt_sk[0], msgbuf, 100, 0);
	if ((-1 != error) || (ENOTCONN != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN sent socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUTDOWN sent socket");

	errno = 0;

	/* Do a SHUT_RD on clt_sk[1] to disable any new receives. */
	test_shutdown(clt_sk[1], SHUT_RD);

	error = recv(clt_sk[1], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	/* Sending a message on SHUT_RD socket. */
	test_send(clt_sk[1], message, strlen(message), 0);

	/* Receive the message sent on SHUT_RD socket. */
	test_recv(accept_sk[1], msgbuf, 100, 0);

	/* Send a message to the SHUT_RD socket. */
	test_send(accept_sk[1], message, strlen(message), 0);

	/* We should not receive the message as the socket is SHUT_RD */ 
	error = recv(clt_sk[1], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUT_RD socket");

	/* Do a SHUT_RDWR on clt_sk[2] to disable any new sends/receives. */
	test_shutdown(clt_sk[2], SHUT_RDWR);

	error = recv(accept_sk[2], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket "
			 "error:%d, errno:%d", error, errno);

	error = recv(clt_sk[2], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUT_RDWR socket");

	error = 0;

	for (i = 0; i < MAX_CLIENTS; i++)
		close(clt_sk[i]);
	for (i = 0; i < MAX_CLIENTS; i++)
		close(accept_sk[i]);

	/* Test case to verify accept of a CLOSED association. */
	/* Do a connect, send and a close to ESTABLISH and CLOSE an
	 * association on the listening socket.
	 */
	test_connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop));

	test_send(clt2_sk, message, strlen(message), 0);

	close(clt2_sk);

	FD_ZERO(&set);
	FD_SET(listen_sk, &set);

	error = select(listen_sk + 1, &set, NULL, NULL, NULL);
	if (1 != error)
		tst_brkm(TBROK, tst_exit, "select error:%d, "
			 "errno: %d", error, errno);

	/* Now accept the CLOSED association waiting on the listening 
	 * socket.
	 */  
	accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen); 

	/* Receive the message sent before doing a close. */
	test_recv(accept2_sk, msgbuf, 100, 0);

	/* Receive EOF indication as there are no more messages and the
	 * socket is SHUTDOWN.
	 */
	error = recv(accept2_sk, msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "Unexpected error return on "
			 "recv(error:%d, errno:%d)", error, errno);

	tst_resm(TPASS, "accept of a CLOSED association");

	/* Trying to send a message over the CLOSED association should
	 * generate EPIPE.
	 */
	error = send(accept2_sk, message, strlen(message), MSG_NOSIGNAL);
	if ((-1 != error) || (EPIPE != errno))
		tst_brkm(TBROK, tst_exit, "send to a CLOSED association "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "send to a CLOSED association");

	error = 0;
	close(accept2_sk);

	/* Verify that auto-connect can be done on a TCP-style socket using
	 * sendto/sendmsg.
	 */
	clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop));

	/* Do a sendto() without a connect() */
	test_sendto(clt2_sk, message, strlen(message), 0, &svr_loop.sa,
		    sizeof(svr_loop));

	accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen); 

	test_recv(accept2_sk, msgbuf, 100, 0);

	tst_resm(TPASS, "auto-connect using sendto");

	outmessage.msg_name = &svr_loop;
	outmessage.msg_namelen = sizeof(svr_loop);
	outmessage.msg_iov = NULL;
	outmessage.msg_iovlen = 0;
	outmessage.msg_control = outcmsg;
	outmessage.msg_controllen = sizeof(outcmsg);
	outmessage.msg_flags = 0;

	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));

	/* Verify that SCTP_EOF cannot be used to shutdown an association
	 * on a TCP-style socket.
	 */
	sinfo->sinfo_flags |= SCTP_EOF;
	error = sendmsg(clt2_sk, &outmessage, 0);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_EOF flag "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sendmsg with SCTP_EOF flag");

	/* Verify that SCTP_ABORT cannot be used to abort an association
	 * on a TCP-style socket.
	 */
	sinfo->sinfo_flags |= SCTP_ABORT;
	error = sendmsg(clt2_sk, &outmessage, 0);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_ABORT flag "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sendmsg with SCTP_ABORT flag");

	/* Verify that a normal message can be sent using sendmsg. */
	outmessage.msg_iov = &out_iov;
	outmessage.msg_iovlen = 1;
	out_iov.iov_base = message;
	out_iov.iov_len = strlen(message) + 1;
	sinfo->sinfo_flags = 0;
	test_sendmsg(clt2_sk, &outmessage, 0, strlen(message)+1);

	test_recv(accept2_sk, msgbuf, 100, 0);
	
	tst_resm(TPASS, "sendmsg with no flags");

	close(clt2_sk);
	close(accept2_sk);
	close(listen_sk);

        /* Indicate successful completion.  */
	return 0;
}
Ejemplo n.º 25
0
int main (int argc, const char *argv[])
{
    int rc;
    int sb;
    int i;
    int opt;
    size_t sz;
    int s1, s2;
    void * dummy_buf;
    char addr[128];
    char socket_address[128];

    int port = get_test_port(argc, argv);

    test_addr_from(socket_address, "tcp", "127.0.0.1", port);

    /*  Try closing bound but unconnected socket. */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);
    test_close (sb);

    /*  Try closing a TCP socket while it not connected. At the same time
        test specifying the local address for the connection. */
    sc = test_socket (AF_SP, NN_PAIR);
    test_addr_from(addr, "tcp", "127.0.0.1;127.0.0.1", port);
    test_connect (sc, addr);
    test_close (sc);

    /*  Open the socket anew. */
    sc = test_socket (AF_SP, NN_PAIR);

    /*  Check NODELAY socket option. */
    sz = sizeof (opt);
    rc = nn_getsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, &sz);
    errno_assert (rc == 0);
    nn_assert (sz == sizeof (opt));
    nn_assert (opt == 0);
    opt = 2;
    rc = nn_setsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, sizeof (opt));
    nn_assert (rc < 0 && nn_errno () == EINVAL);
    opt = 1;
    rc = nn_setsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, sizeof (opt));
    errno_assert (rc == 0);
    sz = sizeof (opt);
    rc = nn_getsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, &sz);
    errno_assert (rc == 0);
    nn_assert (sz == sizeof (opt));
    nn_assert (opt == 1);

    /*  Try using invalid address strings. */
    rc = nn_connect (sc, "tcp://*:");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://*:1000000");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://*:some_port");
    nn_assert (rc < 0);
    rc = nn_connect (sc, "tcp://eth10000;127.0.0.1:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == ENODEV);
    rc = nn_connect (sc, "tcp://127.0.0.1");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_bind (sc, "tcp://127.0.0.1:");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_bind (sc, "tcp://127.0.0.1:1000000");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_bind (sc, "tcp://eth10000:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == ENODEV);
    rc = nn_connect (sc, "tcp://:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://-hostname:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://abc.123.---.#:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://[::1]:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://abc.123.:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://abc...123:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://.123:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);

    /*  Connect correctly. Do so before binding the peer socket. */
    test_connect (sc, socket_address);

    /*  Leave enough time for at least on re-connect attempt. */
    nn_sleep (200);

    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);

    /*  Ping-pong test. */
    for (i = 0; i != 100; ++i) {

        test_send (sc, "ABC");
        test_recv (sb, "ABC");

        test_send (sb, "DEF");
        test_recv (sc, "DEF");
    }

    /*  Batch transfer test. */
    for (i = 0; i != 100; ++i) {
        test_send (sc, "0123456789012345678901234567890123456789");
    }
    for (i = 0; i != 100; ++i) {
        test_recv (sb, "0123456789012345678901234567890123456789");
    }

    test_close (sc);
    test_close (sb);

    /*  Test whether connection rejection is handled decently. */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);
    s1 = test_socket (AF_SP, NN_PAIR);
    test_connect (s1, socket_address);
    s2 = test_socket (AF_SP, NN_PAIR);
    test_connect (s2, socket_address);
    nn_sleep (100);
    test_close (s2);
    test_close (s1);
    test_close (sb);

    /*  Test two sockets binding to the same address. */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);
    s1 = test_socket (AF_SP, NN_PAIR);

    rc = nn_bind (s1, socket_address);
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EADDRINUSE);

    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, socket_address);
    nn_sleep (100);
    test_send (sb, "ABC");
    test_recv (sc, "ABC");
    test_close (sb);
    test_close (sc);
    test_close (s1);

    /*  Test NN_RCVMAXSIZE limit */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);
    s1 = test_socket (AF_SP, NN_PAIR);
    test_connect (s1, socket_address);
    opt = 4;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt));
    nn_assert (rc == 0);
    nn_sleep (100);
    test_send (s1, "ABC");
    test_recv (sb, "ABC");
    test_send (s1, "0123456789012345678901234567890123456789");
    rc = nn_recv (sb, &dummy_buf, NN_MSG, NN_DONTWAIT);
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EAGAIN);
    test_close (sb);
    test_close (s1);

    /*  Test that NN_RCVMAXSIZE can be -1, but not lower */
    sb = test_socket (AF_SP, NN_PAIR);
    opt = -1;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt));
    nn_assert (rc >= 0);
    opt = -2;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt));
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    test_close (sb);

    /*  Test closing a socket that is waiting to connect. */
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, socket_address);
    nn_sleep (100);
    test_close (sc);

    return 0;
}
Ejemplo n.º 26
0
int 
main (int argc, char **argv)
{
	int sk1, sk2, sk3, pf_class;
	socklen_t len;
	struct sockaddr_in lstn_addr, acpt_addr;
	struct sockaddr_in conn_addr;
	char * buffer_rcv;
	struct sctp_initmsg sinmsg;
	char *message = "Hello World!\n";

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	/* Opening the socket*/
	
	pf_class = PF_INET;

	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	sk3 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

        lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	test_bind(sk3, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

	len = sizeof(struct sctp_initmsg);
	sinmsg.sinit_num_ostreams = 65535;
	sinmsg.sinit_max_instreams = 10;
	sinmsg.sinit_max_attempts = 1;
	sinmsg.sinit_max_init_timeo = 0;
	test_setsockopt(sk1, SCTP_INITMSG, &sinmsg, len);
	sinmsg.sinit_num_ostreams = 10;
	sinmsg.sinit_max_instreams = 65535;
	test_setsockopt(sk3, SCTP_INITMSG, &sinmsg, len);

	test_listen(sk3, 1);

	len = sizeof(struct sockaddr_in);
	test_connect(sk1, (struct sockaddr *) &conn_addr, len);

	sk2 = test_accept(sk3, (struct sockaddr *) &acpt_addr, &len);

	test_sctp_sendmsg(sk1, message, strlen(message) + 1,
			  (struct sockaddr *)&conn_addr, len,
			  0, 0, 65534, 0, 0);

	buffer_rcv = malloc(100);
	test_recv(sk2, buffer_rcv, (strlen(message) + 1), MSG_NOSIGNAL);

	tst_resm(TPASS, "connect() with init timeout set to 0 - SUCCESS");

	close (sk1);
	close (sk2);
	close (sk3);
	
        return 0;
}
Ejemplo n.º 27
0
int main(void)
{
	int error;
	socklen_t len;
	int sk, sk1, sk2, acpt_sk, pf_class;
	struct sctp_rtoinfo grtinfo;
	struct sockaddr_in lstn_addr, conn_addr;
	struct sctp_initmsg ginmsg;	/*get the value for SCTP_INITMSG */
	struct sctp_initmsg sinmsg;	/*set the value for SCTP_INITMSG */
	struct linger slinger;	/*SO_LINGER structure */
	struct linger glinger;	/*SO_LINGER structure */
	struct sockaddr_in addr;
	struct sockaddr_in *gaddr;
	struct sctp_status gstatus;	/*SCTP_STATUS option */
	int rcvbuf_val_get, rcvbuf_val_set;	/*get and set var for SO_RCVBUF */
	int sndbuf_val_get, sndbuf_val_set;	/*get and set var for SO_SNDBUF */
	struct sctp_prim gprimaddr;	/*SCTP_PRIMARY_ADDR get */
	struct sctp_prim sprimaddr;	/*SCTP_PRIMARY_ADDR set */
	struct sctp_assocparams sassocparams;	/* SCTP_ASSOCPARAMS set */
	struct sctp_assocparams gassocparams;	/* SCTP_ASSOCPARAMS get */

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	pf_class = PF_INET;

	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*setsockopt() TEST1: Bad socket descriptor EBADF, Expected error */
	error = setsockopt(-1, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "setsockopt with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with a bad socket descriptor - EBADF");

	/*setsockopt() TEST2: Invalid socket ENOTSOCK, Expected error */
	error = setsockopt(0, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "setsockopt with an invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with an invalid socket - ENOTSOCK");

	/*setsockopt() TEST3: Invalid level ENOPROTOOPT, Expected error */
	error = setsockopt(sk, -1, SCTP_RTOINFO, 0, 0);
	if (error != -1 || errno != ENOPROTOOPT)
		tst_brkm(TBROK, NULL, "setsockopt with invalid level "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with an invalid level - ENOPROTOOPT");

	/*setsockopt() TEST4: Invalid option buffer EFAULT, Expected error */
	error = setsockopt(sk, IPPROTO_SCTP, SCTP_RTOINFO,
			   (const struct sctp_rtoinfo *)-1,
			   sizeof(struct sctp_rtoinfo));
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "setsockopt with invalid option "
			 "buffer error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with invalid option buffer - EFAULT");

	/*setsockopt() TEST5: Invalid option Name EOPNOTSUPP, Expected error */
	error = setsockopt(sk, IPPROTO_SCTP, SCTP_AUTOCLOSE, 0, 0);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "setsockopt with invalid option "
			 "name error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with invalid option name - EOPNOTSUPP");

	/*getsockopt() TEST6: Bad socket descriptor EBADF, Expected error */
	error = getsockopt(-1, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "getsockopt with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with a bad socket descriptor - EBADF");

	/*getsockopt() TEST7: Invalid socket ENOTSOCK, Expected error */
	error = getsockopt(0, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "getsockopt with an invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with an invalid socket - ENOTSOCK");
#if 0
	/*getsockopt() TEST3: Invalid level ENOPROTOOPT, Expected error */
	/*I have commented this test case because it is returning EOPNOTSUPP.
	   When I checked the code there also it is returning EOPNOTSUPP. As this
	   is not specific to TCP style, I do not want to do the code change */

	error = getsockopt(sk, -1, SCTP_RTOINFO, 0, 0);
	if (error != -1 || errno != ENOPROTOOPT)
		tst_brkm(TBROK, NULL, "getsockopt with invalid level "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with an invalid level - ENOPROTOOPT");
#endif
	len = sizeof(struct sctp_rtoinfo);

	/*getsockopt() TEST8: Invalid option buffer EFAULT, Expected error */
	error = getsockopt(sk, IPPROTO_SCTP, SCTP_RTOINFO,
			   (struct sctp_rtoinfo *)-1, &len);
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "getsockopt with invalid option "
			 "buffer error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with invalid option buffer - EFAULT");

	/*getsockopt() TEST9: Invalid option Name EOPNOTSUPP, Expected error */
	error = getsockopt(sk, IPPROTO_SCTP, SCTP_AUTOCLOSE, &grtinfo, &len);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "getsockopt with invalid option "
			 "name error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with invalid option name - EOPNOTSUPP");

	close(sk);

	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	sk2 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	lstn_addr.sin_family = AF_INET;
	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	conn_addr.sin_family = AF_INET;
	conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	len = sizeof(struct sctp_initmsg);

	/* TEST10: Test cases for getsockopt SCTP_INITMSG */
	test_getsockopt(sk1, SCTP_INITMSG, &ginmsg, &len);

	tst_resm(TPASS, "getsockopt() SCTP_INITMSG - SUCCESS");

	sinmsg.sinit_num_ostreams = 5;
	sinmsg.sinit_max_instreams = 5;
	sinmsg.sinit_max_attempts = 3;
	sinmsg.sinit_max_init_timeo = 30;
	/* TEST11: Test case for setsockopt SCTP_INITMSG */
	test_setsockopt(sk1, SCTP_INITMSG, &sinmsg, sizeof(sinmsg));

	test_getsockopt(sk1, SCTP_INITMSG, &ginmsg, &len);

	if (sinmsg.sinit_num_ostreams != ginmsg.sinit_num_ostreams &&
	    sinmsg.sinit_max_instreams != ginmsg.sinit_max_instreams &&
	    sinmsg.sinit_max_attempts != ginmsg.sinit_max_attempts &&
	    sinmsg.sinit_max_init_timeo != ginmsg.sinit_max_init_timeo)
		tst_brkm(TBROK, NULL, "setsockopt/getsockopt SCTP_INITMSG "
			 "compare failed");

	tst_resm(TPASS, "setsockopt() SCTP_INITMSG - SUCCESS");

	/*Now get the values on different endpoint */
	test_getsockopt(sk2, SCTP_INITMSG, &ginmsg, &len);

	/*Comparison should not succeed here */
	if (sinmsg.sinit_num_ostreams == ginmsg.sinit_num_ostreams &&
	    sinmsg.sinit_max_instreams == ginmsg.sinit_max_instreams &&
	    sinmsg.sinit_max_attempts == ginmsg.sinit_max_attempts &&
	    sinmsg.sinit_max_init_timeo == ginmsg.sinit_max_init_timeo)
		tst_brkm(TBROK, NULL, "setsockopt/getsockopt SCTP_INITMSG "
			 "unexpected compare success");

	/* SO_LINGER Test with l_onff = 0 and l_linger = 0 */
	slinger.l_onoff = 0;
	slinger.l_linger = 0;
	test_bind(sk1, (struct sockaddr *)&lstn_addr, sizeof(lstn_addr));
	test_listen(sk1, 10);
	len = sizeof(struct sockaddr_in);
	test_connect(sk2, (struct sockaddr *)&conn_addr, len);

	acpt_sk = test_accept(sk1, (struct sockaddr *)&addr, &len);

	len = sizeof(struct linger);
	/* TEST12: Test case for setsockopt SO_LINGER */
	error = setsockopt(sk2, SOL_SOCKET, SO_LINGER, &slinger, len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SO_LINGER "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SO_LINGER - SUCCESS");

	/* TEST13: Test case for getsockopt SO_LINGER */
	error = getsockopt(sk2, SOL_SOCKET, SO_LINGER, &glinger, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_LINGER "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SO_LINGER - SUCCESS");

	if (slinger.l_onoff != glinger.l_onoff ||
	    slinger.l_linger != glinger.l_linger)
		tst_brkm(TBROK, NULL, "setsockopt/getsockopt SO_LINGER "
			 "compare failed");

	/*First gets the default SO_RCVBUF value and comapres with the
	   value obtained from SCTP_STATUS */
	len = sizeof(int);
	/* TEST14: Test case for getsockopt SO_RCVBUF */
	error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val_get, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_RCVBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SO_RCVBUF - SUCCESS");

	len = sizeof(struct sctp_status);
	/* TEST15: Test case for getsockopt SCTP_STATUS */
	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_STATUS, &gstatus, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_STATUS "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SCTP_STATUS - SUCCESS");

	/* Reducing the SO_RCVBUF value using setsockopt() */
	/*Minimum value is 128 and hence I am using it */
	len = sizeof(int);
	rcvbuf_val_set = 128;
	/* TEST16: Test case for setsockopt SO_RCVBUF */
	error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val_set, len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SO_RCVBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SO_RCVBUF - SUCCESS");

	error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val_get, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_RCVBUF "
			 "error:%d, errno:%d", error, errno);

	if ((2 * rcvbuf_val_set) != rcvbuf_val_get)
		tst_brkm(TBROK, NULL, "Comparison failed:Set value and "
			 "got value differs Set Value=%d Get Value=%d",
			 (2 * rcvbuf_val_set), rcvbuf_val_get);

	sndbuf_val_set = 1024;
	/* TEST17: Test case for setsockopt SO_SNDBUF */
	error = setsockopt(sk2, SOL_SOCKET, SO_SNDBUF, &sndbuf_val_set, len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SO_SNDBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SO_SNDBUF - SUCCESS");

	/* TEST18: Test case for getsockopt SO_SNDBUF */
	error = getsockopt(sk2, SOL_SOCKET, SO_SNDBUF, &sndbuf_val_get, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_SNDBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SO_SNDBUF - SUCCESS");

	if ((2 * sndbuf_val_set) != sndbuf_val_get)
		tst_brkm(TBROK, NULL, "Comparison failed:Set value and "
			 "got value differs Set Value=%d Get Value=%d\n",
			 (2 * sndbuf_val_set), sndbuf_val_get);

	/* Getting the primary address using SCTP_PRIMARY_ADDR */
	len = sizeof(struct sctp_prim);
	/* TEST19: Test case for getsockopt SCTP_PRIMARY_ADDR */
	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &gprimaddr,
			   &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_PRIMARY_ADDR "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SCTP_PRIMARY_ADDR - SUCCESS");

	gaddr = (struct sockaddr_in *)&gprimaddr.ssp_addr;
	if (htons(gaddr->sin_port) != lstn_addr.sin_port &&
	    gaddr->sin_family != lstn_addr.sin_family &&
	    gaddr->sin_addr.s_addr != lstn_addr.sin_addr.s_addr)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_PRIMARY_ADDR value "
			 "mismatch");

	memcpy(&sprimaddr, &gprimaddr, sizeof(struct sctp_prim));

	/* TEST20: Test case for setsockopt SCTP_PRIMARY_ADDR */
	error = setsockopt(sk2, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &sprimaddr,
			   len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SCTP_PRIMARY_ADDR "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SCTP_PRIMARY_ADDR - SUCCESS");

	/* TEST21: Test case for getsockopt SCTP_PRIMARY_ADDR */
	/* Getting the association info using SCTP_ASSOCINFO */
	len = sizeof(struct sctp_assocparams);
	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_ASSOCINFO, &gassocparams,
			   &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_ASSOCINFO "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SCTP_ASSOCINFO - SUCCESS");

	/* TEST21: Test case for setsockopt SCTP_ASSOCINFO */
	memcpy(&sassocparams, &gassocparams, sizeof(struct sctp_assocparams));
	sassocparams.sasoc_asocmaxrxt += 5;
	sassocparams.sasoc_cookie_life += 10;

	error = setsockopt(sk2, IPPROTO_SCTP, SCTP_ASSOCINFO, &sassocparams,
			   len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SCTP_ASSOCINFO "
			 "error:%d, errno:%d", error, errno);

	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_ASSOCINFO, &gassocparams,
			   &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_ASSOCINFO "
			 "error:%d, errno:%d", error, errno);

	if (sassocparams.sasoc_asocmaxrxt != gassocparams.sasoc_asocmaxrxt ||
	    sassocparams.sasoc_cookie_life != gassocparams.sasoc_cookie_life)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_ASSOCINFO value "
			 "mismatch");
	tst_resm(TPASS, "setsockopt() SCTP_ASSOCINFO - SUCCESS");

	close(sk2);
	close(sk1);
	close(acpt_sk);

	tst_exit();
}
Ejemplo n.º 28
0
int testipc()
{
    int sb;
    int sc;
    int i;
    int s1, s2;

	size_t size;
	char * buf;
    printf("test ipc\n");
    if ( 1 )
    {
        /*  Try closing a IPC socket while it not connected. */
        sc = test_socket (AF_SP, NN_PAIR);
        test_connect (sc, SOCKET_ADDRESS);
        test_close (sc);
        
        /*  Open the socket anew. */
        sc = test_socket (AF_SP, NN_PAIR);
        test_connect (sc, SOCKET_ADDRESS);
        
        /*  Leave enough time for at least one re-connect attempt. */
        nn_sleep (200);
        
        sb = test_socket (AF_SP, NN_PAIR);
        test_bind (sb, SOCKET_ADDRESS);
        
        /*  Ping-pong test. */
        for (i = 0; i != 1; ++i) {
            test_send (sc, "0123456789012345678901234567890123456789");
            test_recv (sb, "0123456789012345678901234567890123456789");
            test_send (sb, "0123456789012345678901234567890123456789");
            test_recv (sc, "0123456789012345678901234567890123456789");
        }
        
        /*  Batch transfer test. */
        for (i = 0; i != 100; ++i) {
            test_send (sc, "XYZ");
        }
        for (i = 0; i != 100; ++i) {
            test_recv (sb, "XYZ");
        }
        
        /*  Send something large enough to trigger overlapped I/O on Windows. */
        size = 10000;
        buf = malloc( size );
        for (i =0; i != size - 1; ++i) {
            buf[i] = 48 + i % 10;
        }
        buf[size-1] = '\0';
        test_send (sc, buf);
        test_recv (sb, buf);
        free( buf );
        
        test_close (sc);
        test_close (sb);
    }
    if ( 1 )
    {
        /*  Test whether connection rejection is handled decently. */
        sb = test_socket (AF_SP, NN_PAIR);
        test_bind (sb, SOCKET_ADDRESS);
        s1 = test_socket (AF_SP, NN_PAIR);
        test_connect (s1, SOCKET_ADDRESS);
        s2 = test_socket (AF_SP, NN_PAIR);
        test_connect (s2, SOCKET_ADDRESS);
        nn_sleep (100);
        test_close (s2);
        test_close (s1);
        test_close (sb);
    }
    if ( 1 )
    {
        /*  Test two sockets binding to the same address. */
        sb = test_socket (AF_SP, NN_PAIR);
        test_bind (sb, SOCKET_ADDRESS);
        s1 = test_socket (AF_SP, NN_PAIR);
        test_bind (s1, SOCKET_ADDRESS);
        sc = test_socket (AF_SP, NN_PAIR);
        test_connect (sc, SOCKET_ADDRESS);
        //printf("sb.%d s1.%d sc.%d\n",sb,s1,sc);
        nn_sleep (100);
        //printf("send.(ABC) to sb\n");
        test_send (sb, "ABC");
        //printf("check recv.(ABC) via sc\n");
        test_recv (sc, "ABC");
        //printf("close sb\n");
        test_close (sb);
        //printf("send.(DEF) to s1 getchar()\n"), getchar();
        test_send (s1, "DEF");
        //printf("check recv.(DEF) via sc, getchar()\n"); getchar();
        //nn_sleep(1000);
        test_recv (sc, "DEF");
        //printf("close sc getchar()\n"); getchar();
        test_close (sc);
        //printf("close s1\n");
        test_close (s1);
    }
    //printf("finished ipc test\n");

    return 0;
}
Ejemplo n.º 29
0
Archivo: msg.c Proyecto: gridmq/gridmq
int main ()
{
    int rc;
    int sb;
    int sc;
    unsigned char *buf1, *buf2;
    int i;
    struct grid_iovec iov;
    struct grid_msghdr hdr;

    sb = test_socket (AF_SP, GRID_PAIR);
    test_bind (sb, SOCKET_ADDRESS);
    sc = test_socket (AF_SP, GRID_PAIR);
    test_connect (sc, SOCKET_ADDRESS);

    buf1 = grid_allocmsg (256, 0);
    alloc_assert (buf1);
    for (i = 0; i != 256; ++i)
        buf1 [i] = (unsigned char) i;
    rc = grid_send (sc, &buf1, GRID_MSG, 0);
    errno_assert (rc >= 0);
    grid_assert (rc == 256);

    buf2 = NULL;
    rc = grid_recv (sb, &buf2, GRID_MSG, 0);
    errno_assert (rc >= 0);
    grid_assert (rc == 256);
    grid_assert (buf2);
    for (i = 0; i != 256; ++i)
        grid_assert (buf2 [i] == (unsigned char) i);
    rc = grid_freemsg (buf2);
    errno_assert (rc == 0);

    buf1 = grid_allocmsg (256, 0);
    alloc_assert (buf1);
    for (i = 0; i != 256; ++i)
        buf1 [i] = (unsigned char) i;
    iov.iov_base = &buf1;
    iov.iov_len = GRID_MSG;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    rc = grid_sendmsg (sc, &hdr, 0);
    errno_assert (rc >= 0);
    grid_assert (rc == 256);

    buf2 = NULL;
    iov.iov_base = &buf2;
    iov.iov_len = GRID_MSG;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    rc = grid_recvmsg (sb, &hdr, 0);
    errno_assert (rc >= 0);
    grid_assert (rc == 256);
    grid_assert (buf2);
    for (i = 0; i != 256; ++i)
        grid_assert (buf2 [i] == (unsigned char) i);
    rc = grid_freemsg (buf2);
    errno_assert (rc == 0);

    test_close (sc);
    test_close (sb);

    /*  Test receiving of large message  */

    sb = test_socket (AF_SP, GRID_PAIR);
    test_bind (sb, SOCKET_ADDRESS_TCP);
    sc = test_socket (AF_SP, GRID_PAIR);
    test_connect (sc, SOCKET_ADDRESS_TCP);

    for (i = 0; i < (int) sizeof (longdata); ++i)
        longdata[i] = '0' + (i % 10);
    longdata [sizeof (longdata) - 1] = 0;
    test_send (sb, longdata);

    rc = grid_recv (sc, &buf2, GRID_MSG, 0);
    errno_assert (rc >= 0);
    grid_assert (rc == sizeof (longdata) - 1);
    grid_assert (buf2);
    for (i = 0; i < (int) sizeof (longdata) - 1; ++i)
        grid_assert (buf2 [i] == longdata [i]);
    rc = grid_freemsg (buf2);
    errno_assert (rc == 0);

    test_close (sc);
    test_close (sb);

    return 0;
}
Ejemplo n.º 30
0
int testreqrep()
{
    int rc;
    int rep1;
    int rep2;
    int req1;
    int req2;
    int resend_ivl;
    char buf [7];
    int timeo;
    printf("test reqrep\n");

    /*  Test req/rep with full socket types. */
    rep1 = test_socket (AF_SP, NN_REP);
    test_bind (rep1, SOCKET_ADDRESS);
    req1 = test_socket (AF_SP, NN_REQ);
    test_connect (req1, SOCKET_ADDRESS);
    req2 = test_socket (AF_SP, NN_REQ);
    test_connect (req2, SOCKET_ADDRESS);

    /*  Check invalid sequence of sends and recvs. */
    rc = nn_send (rep1, "ABC", 3, 0);
    nn_assert (rc == -1 && nn_errno () == EFSM);
    rc = nn_recv (req1, buf, sizeof (buf), 0);
    nn_assert (rc == -1 && nn_errno () == EFSM);

    /*  Check fair queueing the requests. */
    test_send (req2, "ABC");
    test_recv (rep1, "ABC");
    test_send (rep1, "ABC");
    test_recv (req2, "ABC");

    test_send (req1, "ABC");
    test_recv (rep1, "ABC");
    test_send (rep1, "ABC");
    test_recv (req1, "ABC");

    test_close (rep1);
    test_close (req1);
    test_close (req2);

    /*  Check load-balancing of requests. */
    req1 = test_socket (AF_SP, NN_REQ);
    test_bind (req1, SOCKET_ADDRESS);
    rep1 = test_socket (AF_SP, NN_REP);
    test_connect (rep1, SOCKET_ADDRESS);
    rep2 = test_socket (AF_SP, NN_REP);
    test_connect (rep2, SOCKET_ADDRESS);

    test_send (req1, "ABC");
    test_recv (rep1, "ABC");
    test_send (rep1, "ABC");
    test_recv (req1, "ABC");

    test_send (req1, "ABC");
    test_recv (rep2, "ABC");
    test_send (rep2, "ABC");
    test_recv (req1, "ABC");

    test_close (rep2);
    test_close (rep1);
    test_close (req1);

    /*  Test re-sending of the request. */
    rep1 = test_socket (AF_SP, NN_REP);
    test_bind (rep1, SOCKET_ADDRESS);
    req1 = test_socket (AF_SP, NN_REQ);
    test_connect (req1, SOCKET_ADDRESS);
    resend_ivl = 100;
    rc = nn_setsockopt (req1, NN_REQ, NN_REQ_RESEND_IVL,
        &resend_ivl, sizeof (resend_ivl));
    errno_assert (rc == 0);

    test_send (req1, "ABC");
    test_recv (rep1, "ABC");
    /*  The following waits for request to be resent  */
    test_recv (rep1, "ABC");

    test_close (req1);
    test_close (rep1);

    /*  Check sending a request when the peer is not available. (It should
        be sent immediatelly when the peer comes online rather than relying
        on the resend algorithm. */
    req1 = test_socket (AF_SP, NN_REQ);
    test_connect (req1, SOCKET_ADDRESS);
    test_send (req1, "ABC");

    rep1 = test_socket (AF_SP, NN_REP);
    test_bind (rep1, SOCKET_ADDRESS);
    timeo = 200;
    rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
       &timeo, sizeof (timeo));
    errno_assert (rc == 0);
    test_recv (rep1, "ABC");

    test_close (req1);
    test_close (rep1);

    /*  Check removing socket request sent to (It should
        be sent immediatelly to other peer rather than relying
        on the resend algorithm). */
    req1 = test_socket (AF_SP, NN_REQ);
    test_bind (req1, SOCKET_ADDRESS);
    rep1 = test_socket (AF_SP, NN_REP);
    test_connect (rep1, SOCKET_ADDRESS);
    rep2 = test_socket (AF_SP, NN_REP);
    test_connect (rep2, SOCKET_ADDRESS);

    timeo = 200;
    rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
       &timeo, sizeof (timeo));
    errno_assert (rc == 0);
    rc = nn_setsockopt (rep2, NN_SOL_SOCKET, NN_RCVTIMEO,
       &timeo, sizeof (timeo));
    errno_assert (rc == 0);

    test_send (req1, "ABC");
    /*  We got request through rep1  */
    test_recv (rep1, "ABC");
    /*  But instead replying we simulate crash  */
    test_close (rep1);
    /*  The rep2 should get request immediately  */
    test_recv (rep2, "ABC");
    /*  Let's check it's delivered well  */
    test_send (rep2, "REPLY");
    test_recv (req1, "REPLY");


    test_close (req1);
    test_close (rep2);

    /*  Test cancelling delayed request  */

    req1 = test_socket (AF_SP, NN_REQ);
    test_connect (req1, SOCKET_ADDRESS);
    test_send (req1, "ABC");
    test_send (req1, "DEF");

    rep1 = test_socket (AF_SP, NN_REP);
    test_bind (rep1, SOCKET_ADDRESS);
    timeo = 100;
//    rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
//       &timeo, sizeof (timeo));
//    errno_assert (rc == 0);
    test_recv (rep1, "DEF");

    test_close (req1);
    test_close (rep1);

    return 0;
}