michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * A test for nonblocking connect. Functions tested include PR_Connect, michael@0: * PR_Poll, and PR_GetConnectStatus. michael@0: * michael@0: * The test should be invoked with a host name, for example: michael@0: * nbconn www.netscape.com michael@0: * It will do a nonblocking connect to port 80 (HTTP) on that host, michael@0: * and when connected, issue the "GET /" HTTP command. michael@0: * michael@0: * You should run this test in three ways: michael@0: * 1. To a known web site, such as www.netscape.com. The HTML of the michael@0: * top-level page at the web site should be printed. michael@0: * 2. To a machine not running a web server at port 80. This test should michael@0: * fail. Ideally the error code should be PR_CONNECT_REFUSED_ERROR. michael@0: * But it is possible to return PR_UNKNOWN_ERROR on certain platforms. michael@0: * 3. To an unreachable machine, for example, a machine that is off line. michael@0: * The test should fail after the connect times out. Ideally the michael@0: * error code should be PR_IO_TIMEOUT_ERROR, but it is possible to michael@0: * return PR_UNKNOWN_ERROR on certain platforms. michael@0: */ michael@0: michael@0: #include "nspr.h" michael@0: #include "plgetopt.h" michael@0: #include michael@0: #include michael@0: michael@0: #define SERVER_MAX_BIND_COUNT 100 michael@0: #define DATA_BUF_SIZE 256 michael@0: #define TCP_SERVER_PORT 10000 michael@0: #define TCP_UNUSED_PORT 211 michael@0: michael@0: typedef struct Server_Param { michael@0: PRFileDesc *sp_fd; /* server port */ michael@0: } Server_Param; michael@0: static void PR_CALLBACK TCP_Server(void *arg); michael@0: michael@0: int _debug_on; michael@0: #define DPRINTF(arg) if (_debug_on) printf arg michael@0: michael@0: static PRIntn connection_success_test(); michael@0: static PRIntn connection_failure_test(); michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRHostEnt he; michael@0: char buf[1024]; michael@0: PRNetAddr addr; michael@0: PRPollDesc pd; michael@0: PRStatus rv; michael@0: PRSocketOptionData optData; michael@0: const char *hostname = NULL; michael@0: PRIntn default_case, n, bytes_read, bytes_sent; michael@0: PRInt32 failed_already = 0; michael@0: michael@0: /* michael@0: * -d debug mode michael@0: */ michael@0: michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "d"); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 0: /* debug mode */ michael@0: hostname = opt->value; michael@0: break; michael@0: case 'd': /* debug mode */ michael@0: _debug_on = 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: PR_STDIO_INIT(); michael@0: if (hostname) michael@0: default_case = 0; michael@0: else michael@0: default_case = 1; michael@0: michael@0: if (default_case) { michael@0: michael@0: /* michael@0: * In the default case the following tests are executed: michael@0: * 1. successful connection: a server thread accepts a connection michael@0: * from the main thread michael@0: * 2. unsuccessful connection: the main thread tries to connect to a michael@0: * nonexistent port and expects to get an error michael@0: */ michael@0: rv = connection_success_test(); michael@0: if (rv == 0) michael@0: rv = connection_failure_test(); michael@0: return rv; michael@0: } else { michael@0: PRFileDesc *sock; michael@0: michael@0: if (PR_GetHostByName(argv[1], buf, sizeof(buf), &he) == PR_FAILURE) { michael@0: printf( "Unknown host: %s\n", argv[1]); michael@0: exit(1); michael@0: } else { michael@0: printf( "host: %s\n", buf); michael@0: } michael@0: PR_EnumerateHostEnt(0, &he, 80, &addr); michael@0: michael@0: sock = PR_NewTCPSocket(); michael@0: optData.option = PR_SockOpt_Nonblocking; michael@0: optData.value.non_blocking = PR_TRUE; michael@0: PR_SetSocketOption(sock, &optData); michael@0: rv = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT); michael@0: if (rv == PR_FAILURE && PR_GetError() == PR_IN_PROGRESS_ERROR) { michael@0: printf( "Connect in progress\n"); michael@0: } michael@0: michael@0: pd.fd = sock; michael@0: pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT; michael@0: n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: if (n == -1) { michael@0: printf( "PR_Poll failed\n"); michael@0: exit(1); michael@0: } michael@0: printf( "PR_Poll returns %d\n", n); michael@0: if (pd.out_flags & PR_POLL_READ) { michael@0: printf( "PR_POLL_READ\n"); michael@0: } michael@0: if (pd.out_flags & PR_POLL_WRITE) { michael@0: printf( "PR_POLL_WRITE\n"); michael@0: } michael@0: if (pd.out_flags & PR_POLL_EXCEPT) { michael@0: printf( "PR_POLL_EXCEPT\n"); michael@0: } michael@0: if (pd.out_flags & PR_POLL_ERR) { michael@0: printf( "PR_POLL_ERR\n"); michael@0: } michael@0: if (pd.out_flags & PR_POLL_NVAL) { michael@0: printf( "PR_POLL_NVAL\n"); michael@0: } michael@0: michael@0: if (PR_GetConnectStatus(&pd) == PR_SUCCESS) { michael@0: printf("PR_GetConnectStatus: connect succeeded\n"); michael@0: PR_Write(sock, "GET /\r\n\r\n", 9); michael@0: PR_Shutdown(sock, PR_SHUTDOWN_SEND); michael@0: pd.in_flags = PR_POLL_READ; michael@0: while (1) { michael@0: n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: printf( "poll returns %d\n", n); michael@0: n = PR_Read(sock, buf, sizeof(buf)); michael@0: printf( "read returns %d\n", n); michael@0: if (n <= 0) { michael@0: break; michael@0: } michael@0: PR_Write(PR_STDOUT, buf, n); michael@0: } michael@0: } else { michael@0: if (PR_GetError() == PR_IN_PROGRESS_ERROR) { michael@0: printf( "PR_GetConnectStatus: connect still in progress\n"); michael@0: exit(1); michael@0: } michael@0: printf( "PR_GetConnectStatus: connect failed: (%ld, %ld)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: } michael@0: PR_Close(sock); michael@0: printf( "PASS\n"); michael@0: return 0; michael@0: michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * TCP Server michael@0: * Server Thread michael@0: * Accept a connection from the client and write some data michael@0: */ michael@0: static void PR_CALLBACK michael@0: TCP_Server(void *arg) michael@0: { michael@0: Server_Param *sp = (Server_Param *) arg; michael@0: PRFileDesc *sockfd, *newsockfd; michael@0: char data_buf[DATA_BUF_SIZE]; michael@0: PRIntn rv, bytes_read; michael@0: michael@0: sockfd = sp->sp_fd; michael@0: if ((newsockfd = PR_Accept(sockfd, NULL, michael@0: PR_INTERVAL_NO_TIMEOUT)) == NULL) { michael@0: fprintf(stderr,"ERROR - PR_Accept failed: (%d,%d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: return; michael@0: } michael@0: bytes_read = 0; michael@0: while (bytes_read != DATA_BUF_SIZE) { michael@0: rv = PR_Read(newsockfd, data_buf + bytes_read , michael@0: DATA_BUF_SIZE - bytes_read); michael@0: if (rv < 0) { michael@0: fprintf(stderr,"Error - PR_Read failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: PR_Close(newsockfd); michael@0: return; michael@0: } michael@0: PR_ASSERT(rv != 0); michael@0: bytes_read += rv; michael@0: } michael@0: DPRINTF(("Bytes read from client - %d\n",bytes_read)); michael@0: rv = PR_Write(newsockfd, data_buf,DATA_BUF_SIZE); michael@0: if (rv < 0) { michael@0: fprintf(stderr,"Error - PR_Write failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: PR_Close(newsockfd); michael@0: return; michael@0: } michael@0: PR_ASSERT(rv == DATA_BUF_SIZE); michael@0: DPRINTF(("Bytes written to client - %d\n",rv)); michael@0: PR_Close(newsockfd); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * test for successful connection using a non-blocking socket michael@0: */ michael@0: static PRIntn michael@0: connection_success_test() michael@0: { michael@0: PRFileDesc *sockfd = NULL, *conn_fd = NULL; michael@0: PRNetAddr netaddr; michael@0: PRInt32 i, rv; michael@0: PRPollDesc pd; michael@0: PRSocketOptionData optData; michael@0: PRThread *thr = NULL; michael@0: Server_Param sp; michael@0: char send_buf[DATA_BUF_SIZE], recv_buf[DATA_BUF_SIZE]; michael@0: PRIntn default_case, n, bytes_read, bytes_sent; michael@0: PRIntn failed_already = 0; michael@0: michael@0: /* michael@0: * Create a tcp socket michael@0: */ michael@0: if ((sockfd = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr,"Error - PR_NewTCPSocket failed\n"); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: memset(&netaddr, 0 , sizeof(netaddr)); michael@0: netaddr.inet.family = PR_AF_INET; michael@0: netaddr.inet.port = PR_htons(TCP_SERVER_PORT); michael@0: netaddr.inet.ip = PR_htonl(PR_INADDR_ANY); michael@0: /* michael@0: * try a few times to bind server's address, if addresses are in michael@0: * use michael@0: */ michael@0: i = 0; michael@0: while (PR_Bind(sockfd, &netaddr) < 0) { michael@0: if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) { michael@0: netaddr.inet.port += 2; michael@0: if (i++ < SERVER_MAX_BIND_COUNT) michael@0: continue; michael@0: } michael@0: fprintf(stderr,"ERROR - PR_Bind failed: (%d,%d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: michael@0: if (PR_Listen(sockfd, 32) < 0) { michael@0: fprintf(stderr,"ERROR - PR_Listen failed: (%d,%d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: michael@0: if (PR_GetSockName(sockfd, &netaddr) < 0) { michael@0: fprintf(stderr,"ERROR - PR_GetSockName failed: (%d,%d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: if ((conn_fd = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr,"Error - PR_NewTCPSocket failed\n"); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: optData.option = PR_SockOpt_Nonblocking; michael@0: optData.value.non_blocking = PR_TRUE; michael@0: PR_SetSocketOption(conn_fd, &optData); michael@0: rv = PR_Connect(conn_fd, &netaddr, PR_INTERVAL_NO_TIMEOUT); michael@0: if (rv == PR_FAILURE) { michael@0: if (PR_GetError() == PR_IN_PROGRESS_ERROR) { michael@0: DPRINTF(("Connect in progress\n")); michael@0: } else { michael@0: fprintf(stderr,"Error - PR_Connect failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: } michael@0: /* michael@0: * Now create a thread to accept a connection michael@0: */ michael@0: sp.sp_fd = sockfd; michael@0: thr = PR_CreateThread(PR_USER_THREAD, TCP_Server, (void *)&sp, michael@0: PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (thr == NULL) { michael@0: fprintf(stderr,"Error - PR_CreateThread failed: (%d,%d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: DPRINTF(("Created TCP_Server thread [0x%x]\n",thr)); michael@0: pd.fd = conn_fd; michael@0: pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT; michael@0: n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: if (n == -1) { michael@0: fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: if (PR_GetConnectStatus(&pd) == PR_SUCCESS) { michael@0: PRInt32 rv; michael@0: michael@0: DPRINTF(("Connection successful\n")); michael@0: michael@0: /* michael@0: * Write some data, read it back and check data integrity to michael@0: * make sure the connection is good michael@0: */ michael@0: pd.in_flags = PR_POLL_WRITE; michael@0: bytes_sent = 0; michael@0: memset(send_buf, 'a', DATA_BUF_SIZE); michael@0: while (bytes_sent != DATA_BUF_SIZE) { michael@0: rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: if (rv < 0) { michael@0: fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: PR_ASSERT((rv == 1) && (pd.out_flags == PR_POLL_WRITE)); michael@0: rv = PR_Write(conn_fd, send_buf + bytes_sent, michael@0: DATA_BUF_SIZE - bytes_sent); michael@0: if (rv < 0) { michael@0: fprintf(stderr,"Error - PR_Write failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: PR_ASSERT(rv > 0); michael@0: bytes_sent += rv; michael@0: } michael@0: DPRINTF(("Bytes written to server - %d\n",bytes_sent)); michael@0: PR_Shutdown(conn_fd, PR_SHUTDOWN_SEND); michael@0: pd.in_flags = PR_POLL_READ; michael@0: bytes_read = 0; michael@0: memset(recv_buf, 0, DATA_BUF_SIZE); michael@0: while (bytes_read != DATA_BUF_SIZE) { michael@0: rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: if (rv < 0) { michael@0: fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: PR_ASSERT((rv == 1) && (pd.out_flags == PR_POLL_READ)); michael@0: rv = PR_Read(conn_fd, recv_buf + bytes_read , michael@0: DATA_BUF_SIZE - bytes_read); michael@0: if (rv < 0) { michael@0: fprintf(stderr,"Error - PR_Read failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: PR_ASSERT(rv != 0); michael@0: bytes_read += rv; michael@0: } michael@0: DPRINTF(("Bytes read from server - %d\n",bytes_read)); michael@0: /* michael@0: * verify the data read michael@0: */ michael@0: if (memcmp(send_buf, recv_buf, DATA_BUF_SIZE) != 0) { michael@0: fprintf(stderr,"ERROR - data corruption\n"); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: DPRINTF(("Data integrity verified\n")); michael@0: } else { michael@0: fprintf(stderr,"PR_GetConnectStatus: connect failed: (%ld, %ld)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already = 1; michael@0: goto def_exit; michael@0: } michael@0: def_exit: michael@0: if (thr) { michael@0: PR_JoinThread(thr); michael@0: thr = NULL; michael@0: } michael@0: if (sockfd) { michael@0: PR_Close(sockfd); michael@0: sockfd = NULL; michael@0: } michael@0: if (conn_fd) { michael@0: PR_Close(conn_fd); michael@0: conn_fd = NULL; michael@0: } michael@0: if (failed_already) michael@0: return 1; michael@0: else michael@0: return 0; michael@0: michael@0: } michael@0: michael@0: /* michael@0: * test for connection to a nonexistent port using a non-blocking socket michael@0: */ michael@0: static PRIntn michael@0: connection_failure_test() michael@0: { michael@0: PRFileDesc *sockfd = NULL, *conn_fd = NULL; michael@0: PRNetAddr netaddr; michael@0: PRInt32 i, rv; michael@0: PRPollDesc pd; michael@0: PRSocketOptionData optData; michael@0: PRIntn n, failed_already = 0; michael@0: michael@0: /* michael@0: * Create a tcp socket michael@0: */ michael@0: if ((sockfd = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr,"Error - PR_NewTCPSocket failed\n"); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: memset(&netaddr, 0 , sizeof(netaddr)); michael@0: netaddr.inet.family = PR_AF_INET; michael@0: netaddr.inet.port = PR_htons(TCP_SERVER_PORT); michael@0: netaddr.inet.ip = PR_htonl(PR_INADDR_ANY); michael@0: /* michael@0: * try a few times to bind server's address, if addresses are in michael@0: * use michael@0: */ michael@0: i = 0; michael@0: while (PR_Bind(sockfd, &netaddr) < 0) { michael@0: if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) { michael@0: netaddr.inet.port += 2; michael@0: if (i++ < SERVER_MAX_BIND_COUNT) michael@0: continue; michael@0: } michael@0: fprintf(stderr,"ERROR - PR_Bind failed: (%d,%d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: michael@0: if (PR_GetSockName(sockfd, &netaddr) < 0) { michael@0: fprintf(stderr,"ERROR - PR_GetSockName failed: (%d,%d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: #ifdef AIX michael@0: /* michael@0: * On AIX, set to unused/reserved port michael@0: */ michael@0: netaddr.inet.port = PR_htons(TCP_UNUSED_PORT); michael@0: #endif michael@0: if ((conn_fd = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr,"Error - PR_NewTCPSocket failed\n"); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: optData.option = PR_SockOpt_Nonblocking; michael@0: optData.value.non_blocking = PR_TRUE; michael@0: PR_SetSocketOption(conn_fd, &optData); michael@0: rv = PR_Connect(conn_fd, &netaddr, PR_INTERVAL_NO_TIMEOUT); michael@0: if (rv == PR_FAILURE) { michael@0: DPRINTF(("PR_Connect to a non-listen port failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError())); michael@0: } else { michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: fprintf(stderr,"Error - PR_Connect succeeded, expected to fail\n"); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: pd.fd = conn_fd; michael@0: pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT; michael@0: n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: if (n == -1) { michael@0: fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: goto def_exit; michael@0: } michael@0: if (PR_GetConnectStatus(&pd) == PR_SUCCESS) { michael@0: PRInt32 rv; michael@0: fprintf(stderr,"PR_GetConnectStatus succeeded, expected to fail\n"); michael@0: failed_already = 1; michael@0: goto def_exit; michael@0: } michael@0: rv = PR_GetError(); michael@0: DPRINTF(("Connection failed, successfully with PR_Error %d\n",rv)); michael@0: def_exit: michael@0: if (sockfd) { michael@0: PR_Close(sockfd); michael@0: sockfd = NULL; michael@0: } michael@0: if (conn_fd) { michael@0: PR_Close(conn_fd); michael@0: conn_fd = NULL; michael@0: } michael@0: if (failed_already) michael@0: return 1; michael@0: else michael@0: return 0; michael@0: michael@0: }