1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
/*
* test_socket.c
*
* Test Socket Type
*
* Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
#include "testutil.h"
#include "testutil_nss.h"
#include "pkix_pl_common.h"
#define LDAP_PORT 389
static void *plContext = NULL;
typedef enum {
SERVER_LISTENING,
SERVER_RECV1,
SERVER_POLL1,
SERVER_SEND2,
SERVER_POLL2,
SERVER_RECV3,
SERVER_POLL3,
SERVER_SEND4,
SERVER_POLL4,
SERVER_DONE,
SERVER_FAILED
} SERVER_STATE;
typedef enum {
CLIENT_WAITFORCONNECT,
CLIENT_SEND1,
CLIENT_POLL1,
CLIENT_RECV2,
CLIENT_POLL2,
CLIENT_SEND3,
CLIENT_POLL3,
CLIENT_RECV4,
CLIENT_POLL4,
CLIENT_DONE,
CLIENT_FAILED
} CLIENT_STATE;
SERVER_STATE serverState;
CLIENT_STATE clientState;
PKIX_PL_Socket *sSock = NULL;
PKIX_PL_Socket *cSock = NULL;
PKIX_PL_Socket *rendezvousSock = NULL;
PKIX_PL_Socket_Callback *sCallbackList;
PKIX_PL_Socket_Callback *cCallbackList;
PKIX_PL_Socket_Callback *rvCallbackList;
PRNetAddr serverNetAddr;
PRNetAddr clientNetAddr;
PRIntn backlog = 0;
PRIntervalTime timeout = 0;
char *sendBuf1 = "Hello, world!";
char *sendBuf2 = "Ack";
char *sendBuf3 = "What do you mean, \"Ack\"?";
char *sendBuf4 = "What do you mean, \"What do you mean, \'Ack\'?\"?";
char rcvBuf1[100];
char rcvBuf2[100];
static void
printUsage(char *testname)
{
char *fmt = "USAGE: %s [-arenas] server:port\n";
printf(fmt, testname);
}
/* Functional tests for Socket public functions */
static void
do_other_work(void)
{ /* while waiting for nonblocking I/O to complete */
(void)PR_Sleep(2 * 60);
}
static PKIX_Boolean
server()
{
PKIX_Int32 bytesRead = 0;
PKIX_Int32 bytesWritten = 0;
PKIX_Boolean keepGoing = PKIX_FALSE;
PKIX_TEST_STD_VARS();
switch (serverState) {
case SERVER_LISTENING:
subTest("SERVER_LISTENING");
PKIX_TEST_EXPECT_NO_ERROR(sCallbackList->acceptCallback(sSock, &rendezvousSock, plContext));
if (rendezvousSock) {
PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_Socket_GetCallbackList(rendezvousSock, &rvCallbackList, plContext));
serverState = SERVER_RECV1;
}
break;
case SERVER_RECV1:
subTest("SERVER_RECV1");
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->recvCallback(rendezvousSock,
rcvBuf1,
sizeof(rcvBuf1),
&bytesRead,
plContext));
if (bytesRead > 0) {
/* confirm that rcvBuf1 = sendBuf1 */
if ((bytesRead != (PRInt32)PL_strlen(sendBuf1) + 1) ||
(strncmp(sendBuf1, rcvBuf1, bytesRead) != 0)) {
testError("Receive buffer mismatch\n");
}
serverState = SERVER_SEND2;
keepGoing = PKIX_TRUE;
} else {
serverState = SERVER_POLL1;
}
break;
case SERVER_POLL1:
subTest("SERVER_POLL1");
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->pollCallback(rendezvousSock, NULL, &bytesRead, plContext));
if (bytesRead > 0) {
/* confirm that rcvBuf1 = sendBuf1 */
if ((bytesRead != (PRInt32)PL_strlen(sendBuf1) + 1) ||
(strncmp(sendBuf1, rcvBuf1, bytesRead) != 0)) {
testError("Receive buffer mismatch\n");
}
serverState = SERVER_SEND2;
keepGoing = PKIX_TRUE;
}
break;
case SERVER_SEND2:
subTest("SERVER_SEND2");
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->sendCallback(rendezvousSock,
sendBuf2,
strlen(sendBuf2) +
1,
&bytesWritten,
plContext));
if (bytesWritten > 0) {
serverState = SERVER_RECV3;
} else {
serverState = SERVER_POLL2;
}
break;
case SERVER_POLL2:
subTest("SERVER_POLL2");
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->pollCallback(rendezvousSock, &bytesWritten, NULL, plContext));
if (bytesWritten > 0) {
serverState = SERVER_RECV3;
}
break;
case SERVER_RECV3:
subTest("SERVER_RECV3");
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->recvCallback(rendezvousSock,
rcvBuf1,
sizeof(rcvBuf1),
&bytesRead,
plContext));
if (bytesRead > 0) {
serverState = SERVER_SEND4;
keepGoing = PKIX_TRUE;
} else {
serverState = SERVER_POLL3;
}
break;
case SERVER_POLL3:
subTest("SERVER_POLL3");
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->pollCallback(rendezvousSock, NULL, &bytesRead, plContext));
if (bytesRead > 0) {
serverState = SERVER_SEND4;
keepGoing = PKIX_TRUE;
}
break;
case SERVER_SEND4:
subTest("SERVER_SEND4");
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->sendCallback(rendezvousSock,
sendBuf4,
strlen(sendBuf4) +
1,
&bytesWritten,
plContext));
if (bytesWritten > 0) {
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->shutdownCallback(rendezvousSock, plContext));
PKIX_TEST_DECREF_BC(sSock);
PKIX_TEST_DECREF_BC(rendezvousSock);
serverState = SERVER_DONE;
} else {
serverState = SERVER_POLL4;
}
break;
case SERVER_POLL4:
subTest("SERVER_POLL4");
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->pollCallback(rendezvousSock, &bytesWritten, NULL, plContext));
if (bytesWritten > 0) {
PKIX_TEST_EXPECT_NO_ERROR(rvCallbackList->shutdownCallback(rendezvousSock, plContext));
PKIX_TEST_DECREF_BC(sSock);
PKIX_TEST_DECREF_BC(rendezvousSock);
serverState = SERVER_DONE;
}
break;
case SERVER_DONE:
default:
subTest("SERVER_DONE");
break;
}
cleanup:
PKIX_TEST_RETURN();
return (keepGoing);
}
static PKIX_Boolean
client()
{
PKIX_Boolean keepGoing = PKIX_FALSE;
PKIX_Int32 bytesRead = 0;
PKIX_Int32 bytesWritten = 0;
PRErrorCode cStat = 0;
/* At 2 seconds each cycle, this should suffice! */
PKIX_UInt32 giveUpCount = 10;
PKIX_TEST_STD_VARS();
switch (clientState) {
case CLIENT_WAITFORCONNECT:
subTest("CLIENT_WAITFORCONNECT");
clientState = CLIENT_FAILED;
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->connectcontinueCallback(cSock, &cStat, plContext));
if (cStat == 0) {
clientState = CLIENT_SEND1;
keepGoing = PKIX_TRUE;
} else {
clientState = CLIENT_WAITFORCONNECT;
if (--giveUpCount == 0) {
testError("Client unable to connect");
}
}
break;
case CLIENT_SEND1:
subTest("CLIENT_SEND1");
clientState = CLIENT_FAILED;
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->sendCallback(cSock,
sendBuf1,
strlen(sendBuf1) +
1,
&bytesWritten,
plContext));
if (bytesWritten > 0) {
clientState = CLIENT_RECV2;
} else {
clientState = CLIENT_POLL1;
}
break;
case CLIENT_POLL1:
subTest("CLIENT_POLL1");
clientState = CLIENT_FAILED;
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->pollCallback(cSock, &bytesWritten, NULL, plContext));
if (bytesWritten > 0) {
clientState = CLIENT_RECV2;
} else {
clientState = CLIENT_POLL1;
}
break;
case CLIENT_RECV2:
subTest("CLIENT_RECV2");
clientState = CLIENT_FAILED;
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->recvCallback(cSock,
rcvBuf2,
sizeof(rcvBuf2),
&bytesRead,
plContext));
if (bytesRead > 0) {
/* confirm that rcvBuf2 = sendBuf2 */
if ((bytesRead != (PRInt32)PL_strlen(sendBuf2) + 1) ||
(strncmp(sendBuf2, rcvBuf2, bytesRead) != 0)) {
testError("Receive buffer mismatch\n");
}
clientState = CLIENT_SEND3;
keepGoing = PKIX_TRUE;
} else {
clientState = CLIENT_POLL2;
}
break;
case CLIENT_POLL2:
subTest("CLIENT_POLL2");
clientState = CLIENT_FAILED;
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->pollCallback(cSock, NULL, &bytesRead, plContext));
if (bytesRead > 0) {
/* confirm that rcvBuf2 = sendBuf2 */
if ((bytesRead != (PRInt32)PL_strlen(sendBuf2) + 1) ||
(strncmp(sendBuf2, rcvBuf2, bytesRead) != 0)) {
testError("Receive buffer mismatch\n");
}
clientState = CLIENT_SEND3;
} else {
clientState = CLIENT_POLL2;
}
break;
case CLIENT_SEND3:
subTest("CLIENT_SEND3");
clientState = CLIENT_FAILED;
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->sendCallback(cSock,
sendBuf3,
strlen(sendBuf3) +
1,
&bytesWritten,
plContext));
if (bytesWritten > 0) {
clientState = CLIENT_RECV4;
} else {
clientState = CLIENT_POLL3;
}
break;
case CLIENT_POLL3:
subTest("CLIENT_POLL3");
clientState = CLIENT_FAILED;
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->pollCallback(cSock, &bytesWritten, NULL, plContext));
if (bytesWritten > 0) {
clientState = CLIENT_RECV4;
} else {
clientState = CLIENT_POLL3;
}
break;
case CLIENT_RECV4:
subTest("CLIENT_RECV4");
clientState = CLIENT_FAILED;
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->recvCallback(cSock,
rcvBuf2,
sizeof(rcvBuf2),
&bytesRead,
plContext));
if (bytesRead > 0) {
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->shutdownCallback(cSock, plContext));
PKIX_TEST_DECREF_BC(cSock);
clientState = CLIENT_DONE;
} else {
clientState = CLIENT_POLL4;
}
break;
case CLIENT_POLL4:
subTest("CLIENT_POLL4");
clientState = CLIENT_FAILED;
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->pollCallback(cSock, NULL, &bytesRead, plContext));
if (bytesRead > 0) {
PKIX_TEST_EXPECT_NO_ERROR(cCallbackList->shutdownCallback(cSock, plContext));
PKIX_TEST_DECREF_BC(cSock);
clientState = CLIENT_DONE;
} else {
clientState = CLIENT_POLL4;
}
break;
case CLIENT_DONE:
default:
subTest("CLIENT_DONE");
break;
}
cleanup:
PKIX_TEST_RETURN();
return (keepGoing);
}
static void
dispatcher()
{
PKIX_Boolean keepGoing = PKIX_FALSE;
PKIX_TEST_STD_VARS();
do {
if (serverState < SERVER_DONE) {
do {
keepGoing = server();
} while (keepGoing == PKIX_TRUE);
}
if (clientState < CLIENT_DONE) {
do {
keepGoing = client();
} while (keepGoing == PKIX_TRUE);
}
do_other_work();
} while ((serverState < SERVER_DONE) || (clientState < CLIENT_DONE));
PKIX_TEST_RETURN();
}
int
test_socket(int argc, char *argv[])
{
int j = 0;
PKIX_UInt32 actualMinorVersion;
char buf[PR_NETDB_BUF_SIZE];
char *serverName = NULL;
char *sepPtr = NULL;
PRHostEnt hostent;
PRUint16 portNum = 0;
PRStatus prstatus = PR_FAILURE;
PRErrorCode cStat = 0;
void *ipaddr = NULL;
PKIX_Error *bindError = NULL;
PRIntn hostenum;
PKIX_TEST_STD_VARS();
startTests("Socket");
PKIX_TEST_EXPECT_NO_ERROR(
PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
if (argc != (j + 2)) {
printUsage(argv[0]);
pkixTestErrorMsg = "Missing command line argument.";
goto cleanup;
}
serverName = argv[j + 1];
subTest("Using pkix_pl_Socket_CreateByName");
PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_Socket_CreateByName(PKIX_TRUE, timeout, serverName, &cStat, &sSock, plContext));
PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_Socket_GetCallbackList(sSock, &sCallbackList, plContext));
PKIX_TEST_EXPECT_NO_ERROR(sCallbackList->listenCallback(sSock, backlog, plContext));
serverState = SERVER_LISTENING;
PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_Socket_CreateByName(PKIX_FALSE, timeout, serverName, &cStat, &cSock, plContext));
PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_Socket_GetCallbackList(cSock, &cCallbackList, plContext));
if ((timeout == 0) && (cStat == PR_IN_PROGRESS_ERROR)) {
clientState = CLIENT_WAITFORCONNECT;
} else {
clientState = CLIENT_SEND1;
}
dispatcher();
subTest("Using pkix_pl_Socket_Create");
sepPtr = strchr(serverName, ':');
/* First strip off the portnum, if present, from the end of the name */
if (sepPtr) {
*sepPtr++ = '\0';
portNum = (PRUint16)atoi(sepPtr);
} else {
portNum = (PRUint16)LDAP_PORT;
}
/*
* The hostname may be a fully-qualified name. Just
* use the leftmost component in our lookup.
*/
sepPtr = strchr(serverName, '.');
if (sepPtr) {
*sepPtr++ = '\0';
}
prstatus = PR_GetHostByName(serverName, buf, sizeof(buf), &hostent);
if ((prstatus != PR_SUCCESS) || (hostent.h_length != 4)) {
printUsage(argv[0]);
pkixTestErrorMsg =
"PR_GetHostByName rejects command line argument.";
goto cleanup;
}
serverNetAddr.inet.family = PR_AF_INET;
serverNetAddr.inet.port = PR_htons(portNum);
serverNetAddr.inet.ip = PR_INADDR_ANY;
hostenum = PR_EnumerateHostEnt(0, &hostent, portNum, &clientNetAddr);
if (hostenum == -1) {
pkixTestErrorMsg =
"PR_EnumerateHostEnt failed.";
goto cleanup;
}
backlog = 5;
/* timeout = PR_INTERVAL_NO_TIMEOUT; */
/* timeout = 0; nonblocking */
timeout = 0;
bindError = pkix_pl_Socket_Create(PKIX_TRUE, timeout, &serverNetAddr, &cStat, &sSock, plContext);
/* If PR_Bind can't handle INADDR_ANY, try it with the real name */
if (bindError) {
PKIX_TEST_DECREF_BC(bindError);
serverNetAddr.inet.ip = PR_htonl(*(PRUint32 *)ipaddr);
PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_Socket_Create(PKIX_TRUE,
timeout,
&serverNetAddr,
&cStat,
&sSock,
plContext));
}
PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_Socket_GetCallbackList(sSock, &sCallbackList, plContext));
PKIX_TEST_EXPECT_NO_ERROR(sCallbackList->listenCallback(sSock, backlog, plContext));
serverState = SERVER_LISTENING;
PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_Socket_Create(PKIX_FALSE, timeout, &clientNetAddr, &cStat, &cSock, plContext));
PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_Socket_GetCallbackList(cSock, &cCallbackList, plContext));
if ((timeout == 0) && (cStat == PR_IN_PROGRESS_ERROR)) {
clientState = CLIENT_WAITFORCONNECT;
} else {
clientState = CLIENT_SEND1;
}
dispatcher();
cleanup:
PKIX_TEST_DECREF_AC(sSock);
PKIX_TEST_DECREF_AC(cSock);
PKIX_TEST_DECREF_AC(rendezvousSock);
PKIX_TEST_RETURN();
endTests("Socket");
return (0);
}
|