Exemple #1
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;
}
int main(int argc, char *argv[])
{
	int clnt_sk[MAX_CLIENTS], acpt_sk[MAX_CLIENTS], sk;
	int lstn_sk;
	struct sockaddr_in lstn_addr, acpt_addr;
	socklen_t addrlen;
	int error, i;
	char *message = "hello, world!\n";
	char msgbuf[100];
	int pf_class;

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

	/* Initialize the server and client addresses. */
	pf_class = PF_INET;

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

	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

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

	test_listen(lstn_sk, MAX_CLIENTS);

	for (i = 0; i < MAX_CLIENTS; i++) {
		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
		test_connect(clnt_sk[i], (struct sockaddr *)&lstn_addr,
			     sizeof(lstn_addr));
	}

	for (i = 0; i < MAX_CLIENTS; i++) {
		addrlen = sizeof(acpt_addr);
		acpt_sk[i] = test_accept(lstn_sk, (struct sockaddr *)&acpt_addr,
					 &addrlen);
	}

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

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

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

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

	errno = 0;
	/*Do a send first before doing shutdown */
	test_send(acpt_sk[0], message, strlen(message), 0);

	/*shutdown() TEST3: shutdown with SHUT_WR flag to disable new send */
	error = shutdown(clnt_sk[0], SHUT_WR);
	if (error < 0)
		tst_brkm(TBROK, NULL, "shutdown with SHUT_WR flag "
			 "error:%d, errno:%d", error, errno);

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

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

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

	tst_resm(TPASS, "shutdown() with SHUT_WR flag - SUCCESS");

	errno = 0;

	/*shutdown() TEST4: shutdown with SHUT_RD flag to disable new receive */
	test_shutdown(clnt_sk[1], SHUT_RD);

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

	/* Sending a message on SHUT_RD socket. */
	error = test_send(clnt_sk[1], message, strlen(message), 0);
	if (error < 0)
		tst_brkm(TBROK, NULL, "send on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

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

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

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

	tst_resm(TPASS, "shutdown() with SHUT_RD flag - SUCCESS");

	/*shutdown() TEST5: shutdown with SHUT_RDWR flag to disable new
	   receive/send */
	test_shutdown(clnt_sk[2], SHUT_RDWR);

	error = recv(acpt_sk[2], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUTDOWN received socket "
			 "error:%d, errno:%d", error, errno);

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

	tst_resm(TPASS, "shutdown() with SHUT_RDWR flag - SUCCESS");

	/*shutdown() TEST6: Unconnected socket, ENOTCONN Expected error */
	error = shutdown(sk, SHUT_RD);
	if ((error != -1) || (errno != ENOTCONN))
		tst_brkm(TBROK, NULL, "shutdown on an unconnected socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() on an unconnected socket - SUCCESS");

	for (i = 0; i < MAX_CLIENTS; i++)
		close(clnt_sk[i]);
	for (i = 0; i < MAX_CLIENTS; i++)
		close(acpt_sk[i]);

	close(lstn_sk);
	close(sk);

	tst_exit();
}
Exemple #3
0
int
main(int argc, char ** argv)
{
    int argerr = 0;
    int flag;
    int option_index = 0;

    enum test_modes mode = test_standard;

    crm_set_options(NULL, "mode [options]", long_options,
            "Provides a summary of cluster's current state."
            "\n\nOutputs varying levels of detail in a number of different formats.\n");

    while (1) {
        flag = crm_get_option(argc, argv, &option_index);
        if (flag == -1) {
            break;
        }

        switch(flag) {
        case 'V':
            verbose = 1;
            break;
        case '$':
        case '?':
            crm_help(flag, EX_OK);
            break;
        case 'p':
            mode = test_passive;
            break;
        case 't':
            mode = test_api_sanity;
            break;
        case 'm':
            mode = test_api_mainloop;
            break;
        default:
            ++argerr;
            break;
        }
    }

    crm_log_init("stonith-test", LOG_INFO, TRUE, verbose ? TRUE : FALSE, argc, argv, FALSE);

    if (optind > argc) {
        ++argerr;
    }

    if (argerr) {
        crm_help('?', EX_USAGE);
    }

    crm_debug("Create");
    st = stonith_api_new();

    switch (mode) {
    case test_standard:
        standard_dev_test();
        break;
    case test_passive:
        passive_test();
        break;
    case test_api_sanity:
        sanity_tests();
        break;
    case test_api_mainloop:
        mainloop_tests();
        break;
    }

    test_shutdown(0);
    return 0;
}
int
main(int argc, char *argv[])
{
        int msg_count;
	socklen_t len;
	int sk,pf_class,lstn_sk,acpt_sk, flag;
        char *message = "hello, world!\n";
	char *message_rcv;
        int count;

        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

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

	message_rcv = malloc(512);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = 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);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

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

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
	
	msg_count = (strlen(message) + 1);

	flag = MSG_NOSIGNAL;
	/*Sending the message*/
	count = test_send(sk, message, msg_count, flag);

	/*recvfrom () TEST1: Bad socket descriptor, EBADF Expected error*/
	count = recvfrom(-1, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "recvfrom with a bad socket "
			 "descriptor count:%d, errno:%d", count, errno);

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

	/*recvfrom () TEST2: Invalid socket , ENOTSOCK Expected error*/
	count = recvfrom(0, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "recvfrom with invalid socket "
			 "count:%d, errno:%d", count, errno);

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

	/*recvfrom () TEST3: Invalid message pointer EFAULT, Expected error*/
	count = recvfrom(acpt_sk, (char *)-1, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "recvfrom with invalid message "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() with invalid message ptr - EFAULT");

	/*TEST4: recvfrom on listening socket,ENOTCONN Expected error*/
	count = recvfrom(lstn_sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, tst_exit, "recvfrom on listening socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() on listening socket - ENOTCONN");

	count = test_send(acpt_sk, message, msg_count, flag);

	test_shutdown(sk, SHUT_WR);

	/*recvfrom () TEST5:reading on a socket that received SHUTDOWN*/
	count = recvfrom(acpt_sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count < 0)
		tst_brkm(TBROK, tst_exit, "recvfrom on a socket that has "
			 "received shutdown count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() on a socket that has received shutdown - "
		 "EOF");

	/*recvfrom () TEST6:reading the pending message on socket that sent 
	SHUTDOWN*/
	count = recvfrom(sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count < 0)
		tst_brkm(TBROK, tst_exit, "recvfrom on a socket with pending "
			 "message that has sent shutdown count:%d, errno:%d",
			 count, errno);

	tst_resm(TPASS, "recvfrom() on a socket with pending message that has "
		 "sent shutdown - SUCCESS");

	/*recvfrom () TEST7: No more message and association is shutdown,
	ENOTCONN Expected error*/
	count = recvfrom(sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, tst_exit, "recvfrom on a socket with no "
			 "pending messages and has sent shutdown count:%d, "
			 "errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() on a socket with no pending messages and "
		 " has sent shutdown - ENOTCONN");

	close(sk);
	close(lstn_sk);
	close(acpt_sk);
	return 0;
	
}
int
main(int argc, char *argv[])
{
        socklen_t len;
	int ret;
	int sk,pf_class,lstn_sk,acpt_sk;
	int flag = 0;
	struct msghdr inmessage;
        char *message = "hello, world!\n";
	struct iovec iov_rcv;
        int count;
	char * buffer_rcv;
        char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char *message1 = "hello, world!\n";

        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* 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);

        lstn_sk = 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);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);

	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);

	memset(&inmessage, 0, sizeof(inmessage));
        buffer_rcv = malloc(REALLY_BIG);

        iov_rcv.iov_base = buffer_rcv;
        iov_rcv.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov_rcv;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;
        inmessage.msg_controllen = sizeof(incmsg);

	/*recvmsg () TEST1: Bad socket descriptor, EBADF Expected error*/
	count = recvmsg(-1, &inmessage, flag);
	if (count != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "recvmsg with a bad socket "
			 "descriptor count:%d, errno:%d", count, errno);

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

	/*recvmsg () TEST2: Invalid socket , ENOTSOCK Expected error*/
	count = recvmsg(0, &inmessage, flag);
	if (count != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "recvmsg with invalid socket "
			 "count:%d, errno:%d", count, errno);

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

	/*recvmsg () TEST3: Invalid iovec pointer EFAULT, Expected error*/
	inmessage.msg_iov = (struct iovec *)-1;
	count = recvmsg(acpt_sk, &inmessage, flag);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "recvmsg with invalid iovec "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with invalid iovec ptr - EFAULT");

	inmessage.msg_iov = &iov_rcv;

	/*recvmsg () TEST4: Invalid msghdr pointer EFAULT, Expected error*/
	count = recvmsg(acpt_sk, (struct msghdr *)-1, flag);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "recvmsg with invalid msghdr "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with invalid msghdr ptr - EFAULT");

	/*recvmsg () TEST5:recvmsg on listening socket,ENOTCONN Expected error*/
	count = recvmsg(lstn_sk, &inmessage, flag);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, NULL, "recvmsg on listening socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() on listening socket - ENOTCONN");

	count = test_send(acpt_sk, message1, strlen(message), 0);

	ret = test_shutdown(sk, SHUT_WR);

	flag = MSG_NOSIGNAL;
	/*recvmsg () TEST6:reading on a socket that received SHUTDOWN*/
	count = recvmsg(acpt_sk, &inmessage, flag);
	if (count < 0)
		tst_brkm(TBROK, NULL, "recvmsg on a socket that has "
			 "received shutdown count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() on a socket that has received shutdown - "
		 "EOF");

	/*recvmsg () TEST7:reading the pending message socket that sent
	SHUTDOWN*/
	count = recvmsg(sk, &inmessage, flag);
	if (count < 0)
		tst_brkm(TBROK, NULL, "recvmsg on a socket with pending "
			 "message that has sent shutdown count:%d, errno:%d",
			 count, errno);

	tst_resm(TPASS, "recvmsg() on a socket with pending message that has "
		 "sent shutdown - SUCCESS");

	/*recvmsg () TEST8: No more message and association is shutdown,
	ENOTCONN Expected error*/
	count = recvmsg(sk, &inmessage, flag);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, NULL, "recvmsg on a socket with no "
			 "pending messages and has sent shutdown count:%d, "
			 "errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() on a socket with no pending messages and "
		 " has sent shutdown - ENOTCONN");

	close(sk);
	close(lstn_sk);
	close(acpt_sk);
	tst_exit();
}