accept.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /***********************************************************************
  6. ** 1996 - Netscape Communications Corporation
  7. **
  8. ** Name: accept.c
  9. **
  10. ** Description: Run accept() sucessful connection tests.
  11. **
  12. ** Modification History:
  13. ** 04-Jun-97 AGarcia - Reconvert test file to return a 0 for PASS and a 1 for FAIL
  14. ** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode
  15. ** The debug mode will print all of the printfs associated with this test.
  16. ** The regress mode will be the default mode. Since the regress tool limits
  17. ** the output to a one line status:PASS or FAIL,all of the printf statements
  18. ** have been handled with an if (debug_mode) statement.
  19. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  20. ** recognize the return code from tha main program.
  21. ** 12-June-97 Revert to return code 0 and 1.
  22. ***********************************************************************/
  23. /***********************************************************************
  24. ** Includes
  25. ***********************************************************************/
  26. #include "nspr.h"
  27. #include "prpriv.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "plgetopt.h"
  31. #include "plerror.h"
  32. #define BASE_PORT 10000
  33. #define CLIENT_DATA 128
  34. #define ACCEPT_NORMAL 0x1
  35. #define ACCEPT_FAST 0x2
  36. #define ACCEPT_READ 0x3
  37. #define ACCEPT_READ_FAST 0x4
  38. #define ACCEPT_READ_FAST_CB 0x5
  39. #define CLIENT_NORMAL 0x1
  40. #define CLIENT_TIMEOUT_ACCEPT 0x2
  41. #define CLIENT_TIMEOUT_SEND 0x3
  42. #define SERVER_MAX_BIND_COUNT 100
  43. #if defined(XP_OS2)
  44. #define TIMEOUTSECS 10
  45. #else
  46. #define TIMEOUTSECS 2
  47. #endif
  48. PRIntervalTime timeoutTime;
  49. static PRInt32 count = 1;
  50. static PRFileDesc *output;
  51. static PRNetAddr serverAddr;
  52. static PRThreadScope thread_scope = PR_LOCAL_THREAD;
  53. static PRInt32 clientCommand;
  54. static PRInt32 iterations;
  55. static PRStatus rv;
  56. static PRFileDesc *listenSock;
  57. static PRFileDesc *clientSock = NULL;
  58. static PRNetAddr listenAddr;
  59. static PRNetAddr clientAddr;
  60. static PRThread *clientThread;
  61. static PRNetAddr *raddr;
  62. static char buf[4096 + 2*sizeof(PRNetAddr) + 32];
  63. static PRInt32 status;
  64. static PRInt32 bytesRead;
  65. PRIntn failed_already=0;
  66. PRIntn debug_mode;
  67. void Test_Assert(const char *msg, const char *file, PRIntn line)
  68. {
  69. failed_already=1;
  70. if (debug_mode) {
  71. PR_fprintf(output, "@%s:%d ", file, line);
  72. PR_fprintf(output, msg);
  73. }
  74. } /* Test_Assert */
  75. #define TEST_ASSERT(expr) \
  76. if (!(expr)) Test_Assert(#expr, __FILE__, __LINE__)
  77. #ifdef WINNT
  78. #define CALLBACK_MAGIC 0x12345678
  79. void timeout_callback(void *magic)
  80. {
  81. TEST_ASSERT(magic == (void *)CALLBACK_MAGIC);
  82. if (debug_mode) {
  83. PR_fprintf(output, "timeout callback called okay\n");
  84. }
  85. }
  86. #endif
  87. static void PR_CALLBACK
  88. ClientThread(void *_action)
  89. {
  90. PRInt32 action = * (PRInt32 *) _action;
  91. PRInt32 iterations = count;
  92. PRFileDesc *sock = NULL;
  93. serverAddr.inet.family = PR_AF_INET;
  94. serverAddr.inet.port = listenAddr.inet.port;
  95. serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
  96. for (; iterations--;) {
  97. PRInt32 rv;
  98. char buf[CLIENT_DATA];
  99. memset(buf, 0xaf, sizeof(buf)); /* initialize with arbitrary data */
  100. sock = PR_NewTCPSocket();
  101. if (!sock) {
  102. if (!debug_mode) {
  103. failed_already=1;
  104. }
  105. else {
  106. PR_fprintf(output, "client: unable to create socket\n");
  107. }
  108. return;
  109. }
  110. if (action != CLIENT_TIMEOUT_ACCEPT) {
  111. if ((rv = PR_Connect(sock, &serverAddr,
  112. timeoutTime)) < 0) {
  113. if (!debug_mode) {
  114. failed_already=1;
  115. }
  116. else
  117. PR_fprintf(output,
  118. "client: unable to connect to server (%ld, %ld, %ld, %ld)\n",
  119. iterations, rv, PR_GetError(), PR_GetOSError());
  120. goto ErrorExit;
  121. }
  122. if (action != CLIENT_TIMEOUT_SEND) {
  123. if ((rv = PR_Send(sock, buf, CLIENT_DATA,
  124. 0, timeoutTime))< 0) {
  125. if (!debug_mode) {
  126. failed_already=1;
  127. } else {
  128. PR_fprintf(output,
  129. "client: unable to send to server (%d, %ld, %ld)\n",
  130. CLIENT_DATA, rv, PR_GetError());
  131. }
  132. goto ErrorExit;
  133. }
  134. } else {
  135. PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1));
  136. }
  137. } else {
  138. PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1));
  139. }
  140. if (debug_mode) {
  141. PR_fprintf(output, ".");
  142. }
  143. PR_Close(sock);
  144. sock = NULL;
  145. }
  146. if (debug_mode) {
  147. PR_fprintf(output, "\n");
  148. }
  149. ErrorExit:
  150. if (sock != NULL) {
  151. PR_Close(sock);
  152. }
  153. }
  154. static void
  155. RunTest(PRInt32 acceptType, PRInt32 clientAction)
  156. {
  157. int i;
  158. /* First bind to the socket */
  159. listenSock = PR_NewTCPSocket();
  160. if (!listenSock) {
  161. failed_already=1;
  162. if (debug_mode) {
  163. PR_fprintf(output, "unable to create listen socket\n");
  164. }
  165. return;
  166. }
  167. memset(&listenAddr, 0, sizeof(listenAddr));
  168. listenAddr.inet.family = PR_AF_INET;
  169. listenAddr.inet.port = PR_htons(BASE_PORT);
  170. listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
  171. /*
  172. * try a few times to bind server's address, if addresses are in
  173. * use
  174. */
  175. i = 0;
  176. while (PR_Bind(listenSock, &listenAddr) == PR_FAILURE) {
  177. if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) {
  178. listenAddr.inet.port += 2;
  179. if (i++ < SERVER_MAX_BIND_COUNT) {
  180. continue;
  181. }
  182. }
  183. failed_already=1;
  184. if (debug_mode) {
  185. PR_fprintf(output,"accept: ERROR - PR_Bind failed\n");
  186. }
  187. return;
  188. }
  189. rv = PR_Listen(listenSock, 100);
  190. if (rv == PR_FAILURE) {
  191. failed_already=1;
  192. if (debug_mode) {
  193. PR_fprintf(output, "unable to listen\n");
  194. }
  195. return;
  196. }
  197. clientCommand = clientAction;
  198. clientThread = PR_CreateThread(PR_USER_THREAD, ClientThread,
  199. (void *)&clientCommand, PR_PRIORITY_NORMAL, thread_scope,
  200. PR_JOINABLE_THREAD, 0);
  201. if (!clientThread) {
  202. failed_already=1;
  203. if (debug_mode) {
  204. PR_fprintf(output, "error creating client thread\n");
  205. }
  206. return;
  207. }
  208. iterations = count;
  209. for (; iterations--;) {
  210. switch (acceptType) {
  211. case ACCEPT_NORMAL:
  212. clientSock = PR_Accept(listenSock, &clientAddr,
  213. timeoutTime);
  214. switch(clientAction) {
  215. case CLIENT_TIMEOUT_ACCEPT:
  216. TEST_ASSERT(clientSock == 0);
  217. TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
  218. break;
  219. case CLIENT_NORMAL:
  220. TEST_ASSERT(clientSock);
  221. bytesRead = PR_Recv(clientSock,
  222. buf, CLIENT_DATA, 0, timeoutTime);
  223. TEST_ASSERT(bytesRead == CLIENT_DATA);
  224. break;
  225. case CLIENT_TIMEOUT_SEND:
  226. TEST_ASSERT(clientSock);
  227. bytesRead = PR_Recv(clientSock,
  228. buf, CLIENT_DATA, 0, timeoutTime);
  229. TEST_ASSERT(bytesRead == -1);
  230. TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
  231. break;
  232. }
  233. break;
  234. case ACCEPT_READ:
  235. status = PR_AcceptRead(listenSock, &clientSock,
  236. &raddr, buf, CLIENT_DATA, timeoutTime);
  237. switch(clientAction) {
  238. case CLIENT_TIMEOUT_ACCEPT:
  239. /* Invalid test case */
  240. TEST_ASSERT(0);
  241. break;
  242. case CLIENT_NORMAL:
  243. TEST_ASSERT(clientSock);
  244. TEST_ASSERT(status == CLIENT_DATA);
  245. break;
  246. case CLIENT_TIMEOUT_SEND:
  247. TEST_ASSERT(status == -1);
  248. TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
  249. break;
  250. }
  251. break;
  252. #ifdef WINNT
  253. case ACCEPT_FAST:
  254. clientSock = PR_NTFast_Accept(listenSock,
  255. &clientAddr, timeoutTime);
  256. switch(clientAction) {
  257. case CLIENT_TIMEOUT_ACCEPT:
  258. TEST_ASSERT(clientSock == 0);
  259. if (debug_mode) {
  260. PR_fprintf(output, "PR_GetError is %ld\n", PR_GetError());
  261. }
  262. TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
  263. break;
  264. case CLIENT_NORMAL:
  265. TEST_ASSERT(clientSock);
  266. bytesRead = PR_Recv(clientSock,
  267. buf, CLIENT_DATA, 0, timeoutTime);
  268. TEST_ASSERT(bytesRead == CLIENT_DATA);
  269. break;
  270. case CLIENT_TIMEOUT_SEND:
  271. TEST_ASSERT(clientSock);
  272. bytesRead = PR_Recv(clientSock,
  273. buf, CLIENT_DATA, 0, timeoutTime);
  274. TEST_ASSERT(bytesRead == -1);
  275. TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
  276. break;
  277. }
  278. break;
  279. break;
  280. case ACCEPT_READ_FAST:
  281. status = PR_NTFast_AcceptRead(listenSock,
  282. &clientSock, &raddr, buf, 4096, timeoutTime);
  283. switch(clientAction) {
  284. case CLIENT_TIMEOUT_ACCEPT:
  285. /* Invalid test case */
  286. TEST_ASSERT(0);
  287. break;
  288. case CLIENT_NORMAL:
  289. TEST_ASSERT(clientSock);
  290. TEST_ASSERT(status == CLIENT_DATA);
  291. break;
  292. case CLIENT_TIMEOUT_SEND:
  293. TEST_ASSERT(clientSock == NULL);
  294. TEST_ASSERT(status == -1);
  295. TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
  296. break;
  297. }
  298. break;
  299. case ACCEPT_READ_FAST_CB:
  300. status = PR_NTFast_AcceptRead_WithTimeoutCallback(
  301. listenSock, &clientSock, &raddr, buf, 4096,
  302. timeoutTime, timeout_callback, (void *)CALLBACK_MAGIC);
  303. switch(clientAction) {
  304. case CLIENT_TIMEOUT_ACCEPT:
  305. /* Invalid test case */
  306. TEST_ASSERT(0);
  307. break;
  308. case CLIENT_NORMAL:
  309. TEST_ASSERT(clientSock);
  310. TEST_ASSERT(status == CLIENT_DATA);
  311. break;
  312. case CLIENT_TIMEOUT_SEND:
  313. if (debug_mode) {
  314. PR_fprintf(output, "clientSock = 0x%8.8lx\n", clientSock);
  315. }
  316. TEST_ASSERT(clientSock == NULL);
  317. TEST_ASSERT(status == -1);
  318. TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
  319. break;
  320. }
  321. break;
  322. #endif
  323. }
  324. if (clientSock != NULL) {
  325. PR_Close(clientSock);
  326. clientSock = NULL;
  327. }
  328. }
  329. PR_Close(listenSock);
  330. PR_JoinThread(clientThread);
  331. }
  332. void AcceptUpdatedTest(void)
  333. {
  334. RunTest(ACCEPT_NORMAL, CLIENT_NORMAL);
  335. }
  336. void AcceptNotUpdatedTest(void)
  337. {
  338. RunTest(ACCEPT_FAST, CLIENT_NORMAL);
  339. }
  340. void AcceptReadTest(void)
  341. {
  342. RunTest(ACCEPT_READ, CLIENT_NORMAL);
  343. }
  344. void AcceptReadNotUpdatedTest(void)
  345. {
  346. RunTest(ACCEPT_READ_FAST, CLIENT_NORMAL);
  347. }
  348. void AcceptReadCallbackTest(void)
  349. {
  350. RunTest(ACCEPT_READ_FAST_CB, CLIENT_NORMAL);
  351. }
  352. void TimeoutAcceptUpdatedTest(void)
  353. {
  354. RunTest(ACCEPT_NORMAL, CLIENT_TIMEOUT_ACCEPT);
  355. }
  356. void TimeoutAcceptNotUpdatedTest(void)
  357. {
  358. RunTest(ACCEPT_FAST, CLIENT_TIMEOUT_ACCEPT);
  359. }
  360. void TimeoutAcceptReadCallbackTest(void)
  361. {
  362. RunTest(ACCEPT_READ_FAST_CB, CLIENT_TIMEOUT_ACCEPT);
  363. }
  364. void TimeoutReadUpdatedTest(void)
  365. {
  366. RunTest(ACCEPT_NORMAL, CLIENT_TIMEOUT_SEND);
  367. }
  368. void TimeoutReadNotUpdatedTest(void)
  369. {
  370. RunTest(ACCEPT_FAST, CLIENT_TIMEOUT_SEND);
  371. }
  372. void TimeoutReadReadTest(void)
  373. {
  374. RunTest(ACCEPT_READ, CLIENT_TIMEOUT_SEND);
  375. }
  376. void TimeoutReadReadNotUpdatedTest(void)
  377. {
  378. RunTest(ACCEPT_READ_FAST, CLIENT_TIMEOUT_SEND);
  379. }
  380. void TimeoutReadReadCallbackTest(void)
  381. {
  382. RunTest(ACCEPT_READ_FAST_CB, CLIENT_TIMEOUT_SEND);
  383. }
  384. /************************************************************************/
  385. static void Measure(void (*func)(void), const char *msg)
  386. {
  387. PRIntervalTime start, stop;
  388. double d;
  389. start = PR_IntervalNow();
  390. (*func)();
  391. stop = PR_IntervalNow();
  392. d = (double)PR_IntervalToMicroseconds(stop - start);
  393. if (debug_mode) {
  394. PR_fprintf(output, "%40s: %6.2f usec\n", msg, d / count);
  395. }
  396. }
  397. int main(int argc, char **argv)
  398. {
  399. /* The command line argument: -d is used to determine if the test is being run
  400. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  401. All of the printfs associated with this test has been handled with a if (debug_mode)
  402. test.
  403. Usage: test_name [-d] [-c n]
  404. */
  405. PLOptStatus os;
  406. PLOptState *opt = PL_CreateOptState(argc, argv, "Gdc:");
  407. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  408. {
  409. if (PL_OPT_BAD == os) {
  410. continue;
  411. }
  412. switch (opt->option)
  413. {
  414. case 'G': /* global threads */
  415. thread_scope = PR_GLOBAL_THREAD;
  416. break;
  417. case 'd': /* debug mode */
  418. debug_mode = 1;
  419. break;
  420. case 'c': /* loop counter */
  421. count = atoi(opt->value);
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. PL_DestroyOptState(opt);
  428. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  429. output = PR_STDERR;
  430. PR_STDIO_INIT();
  431. timeoutTime = PR_SecondsToInterval(TIMEOUTSECS);
  432. if (debug_mode) {
  433. PR_fprintf(output, "\nRun accept() sucessful connection tests\n");
  434. }
  435. Measure(AcceptUpdatedTest, "PR_Accept()");
  436. Measure(AcceptReadTest, "PR_AcceptRead()");
  437. #ifdef WINNT
  438. Measure(AcceptNotUpdatedTest, "PR_NTFast_Accept()");
  439. Measure(AcceptReadNotUpdatedTest, "PR_NTFast_AcceptRead()");
  440. Measure(AcceptReadCallbackTest, "PR_NTFast_AcceptRead_WithTimeoutCallback()");
  441. #endif
  442. if (debug_mode) {
  443. PR_fprintf(output, "\nRun accept() timeout in the accept tests\n");
  444. }
  445. #ifdef WINNT
  446. Measure(TimeoutReadReadCallbackTest, "PR_NTFast_AcceptRead_WithTimeoutCallback()");
  447. #endif
  448. Measure(TimeoutReadUpdatedTest, "PR_Accept()");
  449. if (debug_mode) {
  450. PR_fprintf(output, "\nRun accept() timeout in the read tests\n");
  451. }
  452. Measure(TimeoutReadReadTest, "PR_AcceptRead()");
  453. #ifdef WINNT
  454. Measure(TimeoutReadNotUpdatedTest, "PR_NTFast_Accept()");
  455. Measure(TimeoutReadReadNotUpdatedTest, "PR_NTFast_AcceptRead()");
  456. Measure(TimeoutReadReadCallbackTest, "PR_NTFast_AcceptRead_WithTimeoutCallback()");
  457. #endif
  458. PR_fprintf(output, "%s\n", (failed_already) ? "FAIL" : "PASS");
  459. return failed_already;
  460. }