1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1999-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * File: fdcach.c
|
---|
40 | * Description:
|
---|
41 | * This test verifies that the fd cache and stack are working
|
---|
42 | * correctly.
|
---|
43 | */
|
---|
44 |
|
---|
45 | #include "nspr.h"
|
---|
46 |
|
---|
47 | #include <stdio.h>
|
---|
48 | #include <stdlib.h>
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Define ORDER_PRESERVED if the implementation of PR_SetFDCacheSize
|
---|
52 | * preserves the ordering of the fd's when moving them between the
|
---|
53 | * cache and the stack.
|
---|
54 | */
|
---|
55 | #define ORDER_PRESERVED 1
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * NUM_FDS must be <= FD_CACHE_SIZE.
|
---|
59 | */
|
---|
60 | #define FD_CACHE_SIZE 1024
|
---|
61 | #define NUM_FDS 20
|
---|
62 |
|
---|
63 | int main(int argc, char **argv)
|
---|
64 | {
|
---|
65 | int i;
|
---|
66 | PRFileDesc *fds[NUM_FDS];
|
---|
67 | PRFileDesc *savefds[NUM_FDS];
|
---|
68 | int numfds = sizeof(fds)/sizeof(fds[0]);
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * Switch between cache and stack when they are empty.
|
---|
72 | * Then start with the fd cache.
|
---|
73 | */
|
---|
74 | PR_SetFDCacheSize(0, FD_CACHE_SIZE);
|
---|
75 | PR_SetFDCacheSize(0, 0);
|
---|
76 | PR_SetFDCacheSize(0, FD_CACHE_SIZE);
|
---|
77 |
|
---|
78 | /* Add some fd's to the fd cache. */
|
---|
79 | for (i = 0; i < numfds; i++) {
|
---|
80 | savefds[i] = PR_NewTCPSocket();
|
---|
81 | if (NULL == savefds[i]) {
|
---|
82 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
83 | exit(1);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | for (i = 0; i < numfds; i++) {
|
---|
87 | if (PR_Close(savefds[i]) == PR_FAILURE) {
|
---|
88 | fprintf(stderr, "PR_Close failed\n");
|
---|
89 | exit(1);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * Create some fd's. These fd's should come from
|
---|
95 | * the fd cache. Verify the FIFO ordering of the fd
|
---|
96 | * cache.
|
---|
97 | */
|
---|
98 | for (i = 0; i < numfds; i++) {
|
---|
99 | fds[i] = PR_NewTCPSocket();
|
---|
100 | if (NULL == fds[i]) {
|
---|
101 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
102 | exit(1);
|
---|
103 | }
|
---|
104 | if (fds[i] != savefds[i]) {
|
---|
105 | fprintf(stderr, "fd cache malfunctioned\n");
|
---|
106 | exit(1);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | /* Put the fd's back to the fd cache. */
|
---|
110 | for (i = 0; i < numfds; i++) {
|
---|
111 | if (PR_Close(savefds[i]) == PR_FAILURE) {
|
---|
112 | fprintf(stderr, "PR_Close failed\n");
|
---|
113 | exit(1);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* Switch to the fd stack. */
|
---|
118 | PR_SetFDCacheSize(0, 0);
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Create some fd's. These fd's should come from
|
---|
122 | * the fd stack.
|
---|
123 | */
|
---|
124 | for (i = 0; i < numfds; i++) {
|
---|
125 | fds[i] = PR_NewTCPSocket();
|
---|
126 | if (NULL == fds[i]) {
|
---|
127 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
128 | exit(1);
|
---|
129 | }
|
---|
130 | #ifdef ORDER_PRESERVED
|
---|
131 | if (fds[i] != savefds[numfds-1-i]) {
|
---|
132 | fprintf(stderr, "fd stack malfunctioned\n");
|
---|
133 | exit(1);
|
---|
134 | }
|
---|
135 | #else
|
---|
136 | savefds[numfds-1-i] = fds[i];
|
---|
137 | #endif
|
---|
138 | }
|
---|
139 | /* Put the fd's back to the fd stack. */
|
---|
140 | for (i = 0; i < numfds; i++) {
|
---|
141 | if (PR_Close(savefds[i]) == PR_FAILURE) {
|
---|
142 | fprintf(stderr, "PR_Close failed\n");
|
---|
143 | exit(1);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * Now create some fd's and verify the LIFO ordering of
|
---|
149 | * the fd stack.
|
---|
150 | */
|
---|
151 | for (i = 0; i < numfds; i++) {
|
---|
152 | fds[i] = PR_NewTCPSocket();
|
---|
153 | if (NULL == fds[i]) {
|
---|
154 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
155 | exit(1);
|
---|
156 | }
|
---|
157 | if (fds[i] != savefds[numfds-1-i]) {
|
---|
158 | fprintf(stderr, "fd stack malfunctioned\n");
|
---|
159 | exit(1);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | /* Put the fd's back to the fd stack. */
|
---|
163 | for (i = 0; i < numfds; i++) {
|
---|
164 | if (PR_Close(savefds[i]) == PR_FAILURE) {
|
---|
165 | fprintf(stderr, "PR_Close failed\n");
|
---|
166 | exit(1);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | /* Switch to the fd cache. */
|
---|
171 | PR_SetFDCacheSize(0, FD_CACHE_SIZE);
|
---|
172 |
|
---|
173 | for (i = 0; i < numfds; i++) {
|
---|
174 | fds[i] = PR_NewTCPSocket();
|
---|
175 | if (NULL == fds[i]) {
|
---|
176 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
177 | exit(1);
|
---|
178 | }
|
---|
179 | #ifdef ORDER_PRESERVED
|
---|
180 | if (fds[i] != savefds[i]) {
|
---|
181 | fprintf(stderr, "fd cache malfunctioned\n");
|
---|
182 | exit(1);
|
---|
183 | }
|
---|
184 | #else
|
---|
185 | savefds[i] = fds[i];
|
---|
186 | #endif
|
---|
187 | }
|
---|
188 | for (i = 0; i < numfds; i++) {
|
---|
189 | if (PR_Close(savefds[i]) == PR_FAILURE) {
|
---|
190 | fprintf(stderr, "PR_Close failed\n");
|
---|
191 | exit(1);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | for (i = 0; i < numfds; i++) {
|
---|
196 | fds[i] = PR_NewTCPSocket();
|
---|
197 | if (NULL == fds[i]) {
|
---|
198 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
199 | exit(1);
|
---|
200 | }
|
---|
201 | if (fds[i] != savefds[i]) {
|
---|
202 | fprintf(stderr, "fd cache malfunctioned\n");
|
---|
203 | exit(1);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | for (i = 0; i < numfds; i++) {
|
---|
207 | if (PR_Close(savefds[i]) == PR_FAILURE) {
|
---|
208 | fprintf(stderr, "PR_Close failed\n");
|
---|
209 | exit(1);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* Switch to the fd stack. */
|
---|
214 | PR_SetFDCacheSize(0, 0);
|
---|
215 |
|
---|
216 | for (i = 0; i < numfds; i++) {
|
---|
217 | fds[i] = PR_NewTCPSocket();
|
---|
218 | if (NULL == fds[i]) {
|
---|
219 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
220 | exit(1);
|
---|
221 | }
|
---|
222 | #ifdef ORDER_PRESERVED
|
---|
223 | if (fds[i] != savefds[numfds-1-i]) {
|
---|
224 | fprintf(stderr, "fd stack malfunctioned\n");
|
---|
225 | exit(1);
|
---|
226 | }
|
---|
227 | #else
|
---|
228 | savefds[numfds-1-i];
|
---|
229 | #endif
|
---|
230 | }
|
---|
231 | for (i = 0; i < numfds; i++) {
|
---|
232 | if (PR_Close(savefds[i]) == PR_FAILURE) {
|
---|
233 | fprintf(stderr, "PR_Close failed\n");
|
---|
234 | exit(1);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | for (i = 0; i < numfds; i++) {
|
---|
239 | fds[i] = PR_NewTCPSocket();
|
---|
240 | if (NULL == fds[i]) {
|
---|
241 | fprintf(stderr, "PR_NewTCPSocket failed\n");
|
---|
242 | exit(1);
|
---|
243 | }
|
---|
244 | if (fds[i] != savefds[numfds-1-i]) {
|
---|
245 | fprintf(stderr, "fd stack malfunctioned\n");
|
---|
246 | exit(1);
|
---|
247 | }
|
---|
248 | }
|
---|
249 | for (i = 0; i < numfds; i++) {
|
---|
250 | if (PR_Close(savefds[i]) == PR_FAILURE) {
|
---|
251 | fprintf(stderr, "PR_Close failed\n");
|
---|
252 | exit(1);
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | PR_Cleanup();
|
---|
257 | printf("PASS\n");
|
---|
258 | return 0;
|
---|
259 | }
|
---|