Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * File: fdcach.c |
michael@0 | 8 | * Description: |
michael@0 | 9 | * This test verifies that the fd cache and stack are working |
michael@0 | 10 | * correctly. |
michael@0 | 11 | */ |
michael@0 | 12 | |
michael@0 | 13 | #include "nspr.h" |
michael@0 | 14 | |
michael@0 | 15 | #include <stdio.h> |
michael@0 | 16 | #include <stdlib.h> |
michael@0 | 17 | |
michael@0 | 18 | /* |
michael@0 | 19 | * Define ORDER_PRESERVED if the implementation of PR_SetFDCacheSize |
michael@0 | 20 | * preserves the ordering of the fd's when moving them between the |
michael@0 | 21 | * cache and the stack. |
michael@0 | 22 | */ |
michael@0 | 23 | #define ORDER_PRESERVED 1 |
michael@0 | 24 | |
michael@0 | 25 | /* |
michael@0 | 26 | * NUM_FDS must be <= FD_CACHE_SIZE. |
michael@0 | 27 | */ |
michael@0 | 28 | #define FD_CACHE_SIZE 1024 |
michael@0 | 29 | #define NUM_FDS 20 |
michael@0 | 30 | |
michael@0 | 31 | int main(int argc, char **argv) |
michael@0 | 32 | { |
michael@0 | 33 | int i; |
michael@0 | 34 | PRFileDesc *fds[NUM_FDS]; |
michael@0 | 35 | PRFileDesc *savefds[NUM_FDS]; |
michael@0 | 36 | int numfds = sizeof(fds)/sizeof(fds[0]); |
michael@0 | 37 | |
michael@0 | 38 | /* |
michael@0 | 39 | * Switch between cache and stack when they are empty. |
michael@0 | 40 | * Then start with the fd cache. |
michael@0 | 41 | */ |
michael@0 | 42 | PR_SetFDCacheSize(0, FD_CACHE_SIZE); |
michael@0 | 43 | PR_SetFDCacheSize(0, 0); |
michael@0 | 44 | PR_SetFDCacheSize(0, FD_CACHE_SIZE); |
michael@0 | 45 | |
michael@0 | 46 | /* Add some fd's to the fd cache. */ |
michael@0 | 47 | for (i = 0; i < numfds; i++) { |
michael@0 | 48 | savefds[i] = PR_NewTCPSocket(); |
michael@0 | 49 | if (NULL == savefds[i]) { |
michael@0 | 50 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 51 | exit(1); |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | for (i = 0; i < numfds; i++) { |
michael@0 | 55 | if (PR_Close(savefds[i]) == PR_FAILURE) { |
michael@0 | 56 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 57 | exit(1); |
michael@0 | 58 | } |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | /* |
michael@0 | 62 | * Create some fd's. These fd's should come from |
michael@0 | 63 | * the fd cache. Verify the FIFO ordering of the fd |
michael@0 | 64 | * cache. |
michael@0 | 65 | */ |
michael@0 | 66 | for (i = 0; i < numfds; i++) { |
michael@0 | 67 | fds[i] = PR_NewTCPSocket(); |
michael@0 | 68 | if (NULL == fds[i]) { |
michael@0 | 69 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 70 | exit(1); |
michael@0 | 71 | } |
michael@0 | 72 | if (fds[i] != savefds[i]) { |
michael@0 | 73 | fprintf(stderr, "fd cache malfunctioned\n"); |
michael@0 | 74 | exit(1); |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | /* Put the fd's back to the fd cache. */ |
michael@0 | 78 | for (i = 0; i < numfds; i++) { |
michael@0 | 79 | if (PR_Close(savefds[i]) == PR_FAILURE) { |
michael@0 | 80 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 81 | exit(1); |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /* Switch to the fd stack. */ |
michael@0 | 86 | PR_SetFDCacheSize(0, 0); |
michael@0 | 87 | |
michael@0 | 88 | /* |
michael@0 | 89 | * Create some fd's. These fd's should come from |
michael@0 | 90 | * the fd stack. |
michael@0 | 91 | */ |
michael@0 | 92 | for (i = 0; i < numfds; i++) { |
michael@0 | 93 | fds[i] = PR_NewTCPSocket(); |
michael@0 | 94 | if (NULL == fds[i]) { |
michael@0 | 95 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 96 | exit(1); |
michael@0 | 97 | } |
michael@0 | 98 | #ifdef ORDER_PRESERVED |
michael@0 | 99 | if (fds[i] != savefds[numfds-1-i]) { |
michael@0 | 100 | fprintf(stderr, "fd stack malfunctioned\n"); |
michael@0 | 101 | exit(1); |
michael@0 | 102 | } |
michael@0 | 103 | #else |
michael@0 | 104 | savefds[numfds-1-i] = fds[i]; |
michael@0 | 105 | #endif |
michael@0 | 106 | } |
michael@0 | 107 | /* Put the fd's back to the fd stack. */ |
michael@0 | 108 | for (i = 0; i < numfds; i++) { |
michael@0 | 109 | if (PR_Close(savefds[i]) == PR_FAILURE) { |
michael@0 | 110 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 111 | exit(1); |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | /* |
michael@0 | 116 | * Now create some fd's and verify the LIFO ordering of |
michael@0 | 117 | * the fd stack. |
michael@0 | 118 | */ |
michael@0 | 119 | for (i = 0; i < numfds; i++) { |
michael@0 | 120 | fds[i] = PR_NewTCPSocket(); |
michael@0 | 121 | if (NULL == fds[i]) { |
michael@0 | 122 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 123 | exit(1); |
michael@0 | 124 | } |
michael@0 | 125 | if (fds[i] != savefds[numfds-1-i]) { |
michael@0 | 126 | fprintf(stderr, "fd stack malfunctioned\n"); |
michael@0 | 127 | exit(1); |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | /* Put the fd's back to the fd stack. */ |
michael@0 | 131 | for (i = 0; i < numfds; i++) { |
michael@0 | 132 | if (PR_Close(savefds[i]) == PR_FAILURE) { |
michael@0 | 133 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 134 | exit(1); |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | /* Switch to the fd cache. */ |
michael@0 | 139 | PR_SetFDCacheSize(0, FD_CACHE_SIZE); |
michael@0 | 140 | |
michael@0 | 141 | for (i = 0; i < numfds; i++) { |
michael@0 | 142 | fds[i] = PR_NewTCPSocket(); |
michael@0 | 143 | if (NULL == fds[i]) { |
michael@0 | 144 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 145 | exit(1); |
michael@0 | 146 | } |
michael@0 | 147 | #ifdef ORDER_PRESERVED |
michael@0 | 148 | if (fds[i] != savefds[i]) { |
michael@0 | 149 | fprintf(stderr, "fd cache malfunctioned\n"); |
michael@0 | 150 | exit(1); |
michael@0 | 151 | } |
michael@0 | 152 | #else |
michael@0 | 153 | savefds[i] = fds[i]; |
michael@0 | 154 | #endif |
michael@0 | 155 | } |
michael@0 | 156 | for (i = 0; i < numfds; i++) { |
michael@0 | 157 | if (PR_Close(savefds[i]) == PR_FAILURE) { |
michael@0 | 158 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 159 | exit(1); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | for (i = 0; i < numfds; i++) { |
michael@0 | 164 | fds[i] = PR_NewTCPSocket(); |
michael@0 | 165 | if (NULL == fds[i]) { |
michael@0 | 166 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 167 | exit(1); |
michael@0 | 168 | } |
michael@0 | 169 | if (fds[i] != savefds[i]) { |
michael@0 | 170 | fprintf(stderr, "fd cache malfunctioned\n"); |
michael@0 | 171 | exit(1); |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | for (i = 0; i < numfds; i++) { |
michael@0 | 175 | if (PR_Close(savefds[i]) == PR_FAILURE) { |
michael@0 | 176 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 177 | exit(1); |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | /* Switch to the fd stack. */ |
michael@0 | 182 | PR_SetFDCacheSize(0, 0); |
michael@0 | 183 | |
michael@0 | 184 | for (i = 0; i < numfds; i++) { |
michael@0 | 185 | fds[i] = PR_NewTCPSocket(); |
michael@0 | 186 | if (NULL == fds[i]) { |
michael@0 | 187 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 188 | exit(1); |
michael@0 | 189 | } |
michael@0 | 190 | #ifdef ORDER_PRESERVED |
michael@0 | 191 | if (fds[i] != savefds[numfds-1-i]) { |
michael@0 | 192 | fprintf(stderr, "fd stack malfunctioned\n"); |
michael@0 | 193 | exit(1); |
michael@0 | 194 | } |
michael@0 | 195 | #else |
michael@0 | 196 | savefds[numfds-1-i]; |
michael@0 | 197 | #endif |
michael@0 | 198 | } |
michael@0 | 199 | for (i = 0; i < numfds; i++) { |
michael@0 | 200 | if (PR_Close(savefds[i]) == PR_FAILURE) { |
michael@0 | 201 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 202 | exit(1); |
michael@0 | 203 | } |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | for (i = 0; i < numfds; i++) { |
michael@0 | 207 | fds[i] = PR_NewTCPSocket(); |
michael@0 | 208 | if (NULL == fds[i]) { |
michael@0 | 209 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 210 | exit(1); |
michael@0 | 211 | } |
michael@0 | 212 | if (fds[i] != savefds[numfds-1-i]) { |
michael@0 | 213 | fprintf(stderr, "fd stack malfunctioned\n"); |
michael@0 | 214 | exit(1); |
michael@0 | 215 | } |
michael@0 | 216 | } |
michael@0 | 217 | for (i = 0; i < numfds; i++) { |
michael@0 | 218 | if (PR_Close(savefds[i]) == PR_FAILURE) { |
michael@0 | 219 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 220 | exit(1); |
michael@0 | 221 | } |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | PR_Cleanup(); |
michael@0 | 225 | printf("PASS\n"); |
michael@0 | 226 | return 0; |
michael@0 | 227 | } |