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: ** michael@0: ** Name: prpoll_norm.c michael@0: ** michael@0: ** Description: This program tests PR_Poll with sockets. michael@0: ** Normal operation are tested michael@0: ** michael@0: ** Modification History: michael@0: ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. michael@0: ** The debug mode will print all of the printfs associated with this test. michael@0: ** The regress mode will be the default mode. Since the regress tool limits michael@0: ** the output to a one line status:PASS or FAIL,all of the printf statements michael@0: ** have been handled with an if (debug_mode) statement. michael@0: ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to michael@0: ** recognize the return code from tha main program. michael@0: ***********************************************************************/ michael@0: michael@0: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: /* Used to get the command line option */ michael@0: #include "plgetopt.h" michael@0: michael@0: #include "prinit.h" michael@0: #include "prio.h" michael@0: #include "prlog.h" michael@0: #include "prprf.h" michael@0: #include "prnetdb.h" michael@0: #include "obsolete/probslet.h" michael@0: michael@0: #include "private/pprio.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: PRIntn failed_already=0; michael@0: PRIntn debug_mode; michael@0: michael@0: #define NUM_ITERATIONS 5 michael@0: michael@0: static void PR_CALLBACK michael@0: clientThreadFunc(void *arg) michael@0: { michael@0: PRUintn port = (PRUintn) arg; michael@0: PRFileDesc *sock; michael@0: PRNetAddr addr; michael@0: char buf[128]; michael@0: int i; michael@0: PRStatus sts; michael@0: PRInt32 n; michael@0: michael@0: addr.inet.family = PR_AF_INET; michael@0: addr.inet.port = PR_htons((PRUint16)port); michael@0: addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); michael@0: memset(buf, 0, sizeof(buf)); michael@0: PR_snprintf(buf, sizeof(buf), "%hu", port); michael@0: michael@0: for (i = 0; i < NUM_ITERATIONS; i++) { michael@0: sock = PR_NewTCPSocket(); michael@0: PR_ASSERT(sock != NULL); michael@0: michael@0: sts = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT); michael@0: PR_ASSERT(sts == PR_SUCCESS); michael@0: michael@0: n = PR_Write(sock, buf, sizeof(buf)); michael@0: PR_ASSERT(n >= 0); michael@0: michael@0: sts = PR_Close(sock); michael@0: PR_ASSERT(sts == PR_SUCCESS); michael@0: } michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL; michael@0: PRUint16 listenPort1, listenPort2; michael@0: PRNetAddr addr; michael@0: char buf[128]; michael@0: PRThread *clientThread; michael@0: PRPollDesc pds0[20], pds1[20], *pds, *other_pds; michael@0: PRIntn npds; michael@0: PRInt32 retVal; michael@0: PRIntn i, j; michael@0: PRSocketOptionData optval; michael@0: michael@0: /* The command line argument: -d is used to determine if the test is being run michael@0: in debug mode. The regress tool requires only one line output:PASS or FAIL. michael@0: All of the printfs associated with this test has been handled with a if (debug_mode) michael@0: test. michael@0: Usage: test_name -d 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 'd': /* debug mode */ michael@0: debug_mode = 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: /* main test */ michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: PR_STDIO_INIT(); michael@0: michael@0: if (debug_mode) { michael@0: printf("This program tests PR_Poll with sockets.\n"); michael@0: printf("Normal operation are tested.\n\n"); michael@0: } michael@0: michael@0: /* Create two listening sockets */ michael@0: if ((listenSock1 = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr, "Can't create a new TCP socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: memset(&addr, 0, sizeof(addr)); michael@0: addr.inet.family = PR_AF_INET; michael@0: addr.inet.ip = PR_htonl(PR_INADDR_ANY); michael@0: addr.inet.port = PR_htons(0); michael@0: if (PR_Bind(listenSock1, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "Can't bind socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetSockName failed\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: listenPort1 = PR_ntohs(addr.inet.port); michael@0: optval.option = PR_SockOpt_Nonblocking; michael@0: optval.value.non_blocking = PR_TRUE; michael@0: PR_SetSocketOption(listenSock1, &optval); michael@0: if (PR_Listen(listenSock1, 5) == PR_FAILURE) { michael@0: fprintf(stderr, "Can't listen on a socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: michael@0: if ((listenSock2 = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr, "Can't create a new TCP socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: addr.inet.family = PR_AF_INET; michael@0: addr.inet.ip = PR_htonl(PR_INADDR_ANY); michael@0: addr.inet.port = PR_htons(0); michael@0: if (PR_Bind(listenSock2, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "Can't bind socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetSockName failed\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: listenPort2 = PR_ntohs(addr.inet.port); michael@0: PR_SetSocketOption(listenSock2, &optval); michael@0: if (PR_Listen(listenSock2, 5) == PR_FAILURE) { michael@0: fprintf(stderr, "Can't listen on a socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: PR_snprintf(buf, sizeof(buf), michael@0: "The server thread is listening on ports %hu and %hu\n\n", michael@0: listenPort1, listenPort2); michael@0: if (debug_mode) printf("%s", buf); michael@0: michael@0: /* Set up the poll descriptor array */ michael@0: pds = pds0; michael@0: other_pds = pds1; michael@0: memset(pds, 0, sizeof(pds)); michael@0: pds[0].fd = listenSock1; michael@0: pds[0].in_flags = PR_POLL_READ; michael@0: pds[1].fd = listenSock2; michael@0: pds[1].in_flags = PR_POLL_READ; michael@0: /* Add some unused entries to test if they are ignored by PR_Poll() */ michael@0: memset(&pds[2], 0, sizeof(pds[2])); michael@0: memset(&pds[3], 0, sizeof(pds[3])); michael@0: memset(&pds[4], 0, sizeof(pds[4])); michael@0: npds = 5; michael@0: michael@0: clientThread = PR_CreateThread(PR_USER_THREAD, michael@0: clientThreadFunc, (void *) listenPort1, michael@0: PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, michael@0: PR_UNJOINABLE_THREAD, 0); michael@0: if (clientThread == NULL) { michael@0: fprintf(stderr, "can't create thread\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: michael@0: clientThread = PR_CreateThread(PR_USER_THREAD, michael@0: clientThreadFunc, (void *) listenPort2, michael@0: PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, michael@0: PR_UNJOINABLE_THREAD, 0); michael@0: if (clientThread == NULL) { michael@0: fprintf(stderr, "can't create thread\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: michael@0: if (debug_mode) { michael@0: printf("Two client threads are created. Each of them will\n"); michael@0: printf("send data to one of the two ports the server is listening on.\n"); michael@0: printf("The data they send is the port number. Each of them send\n"); michael@0: printf("the data five times, so you should see ten lines below,\n"); michael@0: printf("interleaved in an arbitrary order.\n"); michael@0: } michael@0: michael@0: /* two clients, three events per iteration: accept, read, close */ michael@0: i = 0; michael@0: while (i < 2 * 3 * NUM_ITERATIONS) { michael@0: PRPollDesc *tmp; michael@0: int nextIndex; michael@0: int nEvents = 0; michael@0: michael@0: retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT); michael@0: PR_ASSERT(retVal != 0); /* no timeout */ michael@0: if (retVal == -1) { michael@0: fprintf(stderr, "PR_Poll failed\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: michael@0: nextIndex = 2; michael@0: /* the two listening sockets */ michael@0: for (j = 0; j < 2; j++) { michael@0: other_pds[j] = pds[j]; michael@0: PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0 michael@0: && (pds[j].out_flags & PR_POLL_EXCEPT) == 0); michael@0: if (pds[j].out_flags & PR_POLL_READ) { michael@0: PRFileDesc *sock; michael@0: michael@0: nEvents++; michael@0: sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT); michael@0: if (sock == NULL) { michael@0: fprintf(stderr, "PR_Accept() failed\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: other_pds[nextIndex].fd = sock; michael@0: other_pds[nextIndex].in_flags = PR_POLL_READ; michael@0: nextIndex++; michael@0: } else if (pds[j].out_flags & PR_POLL_ERR) { michael@0: fprintf(stderr, "PR_Poll() indicates that an fd has error\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } else if (pds[j].out_flags & PR_POLL_NVAL) { michael@0: fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n", michael@0: PR_FileDesc2NativeHandle(pds[j].fd)); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: } michael@0: michael@0: for (j = 2; j < npds; j++) { michael@0: if (NULL == pds[j].fd) { michael@0: /* michael@0: * Keep the unused entries in the poll descriptor array michael@0: * for testing purposes. michael@0: */ michael@0: other_pds[nextIndex] = pds[j]; michael@0: nextIndex++; michael@0: continue; michael@0: } michael@0: michael@0: PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0 michael@0: && (pds[j].out_flags & PR_POLL_EXCEPT) == 0); michael@0: if (pds[j].out_flags & PR_POLL_READ) { michael@0: PRInt32 nAvail; michael@0: PRInt32 nRead; michael@0: michael@0: nEvents++; michael@0: nAvail = PR_Available(pds[j].fd); michael@0: nRead = PR_Read(pds[j].fd, buf, sizeof(buf)); michael@0: PR_ASSERT(nAvail == nRead); michael@0: if (nRead == -1) { michael@0: fprintf(stderr, "PR_Read() failed\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } else if (nRead == 0) { michael@0: PR_Close(pds[j].fd); michael@0: continue; michael@0: } else { michael@0: /* Just to be safe */ michael@0: buf[127] = '\0'; michael@0: if (debug_mode) printf("The server received \"%s\" from a client\n", buf); michael@0: } michael@0: } else if (pds[j].out_flags & PR_POLL_ERR) { michael@0: fprintf(stderr, "PR_Poll() indicates that an fd has error\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } else if (pds[j].out_flags & PR_POLL_NVAL) { michael@0: fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: other_pds[nextIndex] = pds[j]; michael@0: nextIndex++; michael@0: } michael@0: michael@0: PR_ASSERT(retVal == nEvents); michael@0: /* swap */ michael@0: tmp = pds; michael@0: pds = other_pds; michael@0: other_pds = tmp; michael@0: npds = nextIndex; michael@0: i += nEvents; michael@0: } michael@0: michael@0: if (debug_mode) printf("Tests passed\n"); michael@0: michael@0: exit_now: michael@0: michael@0: if (listenSock1) { michael@0: PR_Close(listenSock1); michael@0: } michael@0: if (listenSock2) { michael@0: PR_Close(listenSock2); michael@0: } michael@0: michael@0: PR_Cleanup(); michael@0: michael@0: if(failed_already) michael@0: return 1; michael@0: else michael@0: return 0; michael@0: michael@0: }