~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/selct_er.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* 
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are 
 
17
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
22
 * Alternatively, the contents of this file may be used under the
 
23
 * terms of the GNU General Public License Version 2 or later (the
 
24
 * "GPL"), in which case the provisions of the GPL are applicable 
 
25
 * instead of those above.  If you wish to allow use of your 
 
26
 * version of this file only under the terms of the GPL and not to
 
27
 * allow others to use your version of this file under the MPL,
 
28
 * indicate your decision by deleting the provisions above and
 
29
 * replace them with the notice and other provisions required by
 
30
 * the GPL.  If you do not delete the provisions above, a recipient
 
31
 * may use your version of this file under either the MPL or the
 
32
 * GPL.
 
33
 */
 
34
 
 
35
/***********************************************************************
 
36
**  1997 - Netscape Communications Corporation
 
37
**
 
38
** Name: prselect_err.c
 
39
**
 
40
** Description: tests PR_Select with sockets Error condition functions.
 
41
**
 
42
** Modification History:
 
43
** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
 
44
**               The debug mode will print all of the printfs associated with this test.
 
45
**                       The regress mode will be the default mode. Since the regress tool limits
 
46
**           the output to a one line status:PASS or FAIL,all of the printf statements
 
47
**                       have been handled with an if (debug_mode) statement.
 
48
** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
 
49
**                      recognize the return code from tha main program.
 
50
***********************************************************************/
 
51
 
 
52
#ifdef XP_BEOS
 
53
#include <stdio.h>
 
54
int main()
 
55
{
 
56
    printf( "This test is not ported to the BeOS\n" );
 
57
    return 0;
 
58
}
 
59
#else
 
60
 
 
61
/***********************************************************************
 
62
** Includes
 
63
***********************************************************************/
 
64
/* Used to get the command line option */
 
65
#include "plgetopt.h"
 
66
 
 
67
#include "primpl.h"
 
68
#include "pprio.h"
 
69
#include "prnetdb.h"
 
70
 
 
71
#include <stdio.h>
 
72
#include <string.h>
 
73
#include <stdlib.h>
 
74
 
 
75
 
 
76
PRIntn failed_already=0;
 
77
PRIntn debug_mode;
 
78
 
 
79
int main(int argc, char **argv)
 
80
{
 
81
    PRFileDesc *listenSock1, *listenSock2;
 
82
    PRFileDesc *badFD;
 
83
    PRUint16 listenPort1, listenPort2;
 
84
    PRNetAddr addr;
 
85
    PR_fd_set readFdSet;
 
86
    char buf[128];
 
87
    PRInt32 retVal;
 
88
 
 
89
        /* The command line argument: -d is used to determine if the test is being run
 
90
        in debug mode. The regress tool requires only one line output:PASS or FAIL.
 
91
        All of the printfs associated with this test has been handled with a if (debug_mode)
 
92
        test.
 
93
        Usage: test_name -d
 
94
        */
 
95
        PLOptStatus os;
 
96
        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
 
97
        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
 
98
    {
 
99
                if (PL_OPT_BAD == os) continue;
 
100
        switch (opt->option)
 
101
        {
 
102
        case 'd':  /* debug mode */
 
103
                        debug_mode = 1;
 
104
            break;
 
105
         default:
 
106
            break;
 
107
        }
 
108
    }
 
109
        PL_DestroyOptState(opt);
 
110
 
 
111
 /* main test */
 
112
        
 
113
    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
 
114
    PR_STDIO_INIT();
 
115
 
 
116
    if (debug_mode) {
 
117
                printf("This program tests PR_Select with sockets.  Error\n");
 
118
                printf("reporting operations are tested.\n\n");
 
119
        }
 
120
 
 
121
    /* Create two listening sockets */
 
122
    if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
 
123
        fprintf(stderr, "Can't create a new TCP socket\n");
 
124
        failed_already=1;
 
125
        goto exit_now;
 
126
    }
 
127
    addr.inet.family = AF_INET;
 
128
    addr.inet.ip = PR_htonl(INADDR_ANY);
 
129
    addr.inet.port = PR_htons(0);
 
130
    if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
 
131
        fprintf(stderr, "Can't bind socket\n");
 
132
        failed_already=1;
 
133
        goto exit_now;
 
134
    }
 
135
    if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
 
136
        fprintf(stderr, "PR_GetSockName failed\n");
 
137
        failed_already=1;
 
138
        goto exit_now;
 
139
    }
 
140
    listenPort1 = PR_ntohs(addr.inet.port);
 
141
    if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
 
142
        fprintf(stderr, "Can't listen on a socket\n");
 
143
        failed_already=1;
 
144
        goto exit_now;
 
145
    }
 
146
 
 
147
    if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
 
148
        fprintf(stderr, "Can't create a new TCP socket\n");
 
149
        failed_already=1;
 
150
        goto exit_now;
 
151
    }
 
152
    addr.inet.family = AF_INET;
 
153
    addr.inet.ip = PR_htonl(INADDR_ANY);
 
154
    addr.inet.port = PR_htons(0);
 
155
    if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
 
156
        fprintf(stderr, "Can't bind socket\n");
 
157
        failed_already=1;
 
158
        goto exit_now;
 
159
    }
 
160
    if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
 
161
        fprintf(stderr, "PR_GetSockName failed\n");
 
162
        failed_already=1;
 
163
        goto exit_now;
 
164
    }
 
165
    listenPort2 = PR_ntohs(addr.inet.port);
 
166
    if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
 
167
        fprintf(stderr, "Can't listen on a socket\n");
 
168
        failed_already=1;
 
169
        goto exit_now;
 
170
    }
 
171
    PR_snprintf(buf, sizeof(buf),
 
172
            "The server thread is listening on ports %hu and %hu\n\n",
 
173
            listenPort1, listenPort2);
 
174
    if (debug_mode) printf("%s", buf);
 
175
 
 
176
    /* Set up the fd set */
 
177
    PR_FD_ZERO(&readFdSet);
 
178
    PR_FD_SET(listenSock1, &readFdSet);
 
179
    PR_FD_SET(listenSock2, &readFdSet);
 
180
 
 
181
 
 
182
    /* Testing bad fd */
 
183
    if (debug_mode) printf("PR_Select should detect a bad file descriptor\n");
 
184
    if ((badFD = PR_NewTCPSocket()) == NULL) {
 
185
        fprintf(stderr, "Can't create a TCP socket\n");
 
186
        failed_already=1;
 
187
        goto exit_now;
 
188
    }
 
189
 
 
190
    PR_FD_SET(badFD, &readFdSet);
 
191
    /*
 
192
     * Make the fd invalid
 
193
     */
 
194
#if defined(XP_UNIX)
 
195
    close(PR_FileDesc2NativeHandle(badFD));
 
196
#elif defined(XP_OS2)
 
197
    soclose(PR_FileDesc2NativeHandle(badFD));
 
198
#elif defined(WIN32) || defined(WIN16)
 
199
    closesocket(PR_FileDesc2NativeHandle(badFD));
 
200
#elif defined(XP_MAC)
 
201
    _PR_MD_CLOSE_SOCKET(PR_FileDesc2NativeHandle(badFD));
 
202
#else
 
203
#error "Unknown architecture"
 
204
#endif
 
205
 
 
206
    retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
 
207
            PR_INTERVAL_NO_TIMEOUT);
 
208
    if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
 
209
        fprintf(stderr, "Failed to detect the bad fd: "
 
210
                "PR_Select returns %d\n", retVal);
 
211
        if (retVal == -1) {
 
212
            fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
 
213
                    PR_GetOSError());
 
214
                failed_already=1;
 
215
        }
 
216
        goto exit_now;
 
217
    }
 
218
    if (debug_mode) printf("PR_Select detected a bad fd.  Test passed.\n\n");
 
219
        PR_FD_CLR(badFD, &readFdSet);
 
220
 
 
221
        PR_Cleanup();
 
222
        goto exit_now;
 
223
exit_now:
 
224
        if(failed_already)      
 
225
                return 1;
 
226
        else
 
227
                return 0;
 
228
 
 
229
}
 
230
 
 
231
#endif /* XP_BEOS */