|
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 /* |
|
7 * File: sockping.c |
|
8 * |
|
9 * Description: |
|
10 * This test runs in conjunction with the sockpong test. |
|
11 * This test creates a socket pair and passes one socket |
|
12 * to the sockpong test. Then this test writes "ping" to |
|
13 * to the sockpong test and the sockpong test writes "pong" |
|
14 * back. To run this pair of tests, just invoke sockping. |
|
15 * |
|
16 * Tested areas: process creation, socket pairs, file |
|
17 * descriptor inheritance. |
|
18 */ |
|
19 |
|
20 #include "prerror.h" |
|
21 #include "prio.h" |
|
22 #include "prproces.h" |
|
23 |
|
24 #include <stdio.h> |
|
25 #include <string.h> |
|
26 #include <stdlib.h> |
|
27 |
|
28 #define NUM_ITERATIONS 10 |
|
29 |
|
30 static char *child_argv[] = { "sockpong", NULL }; |
|
31 |
|
32 int main(int argc, char **argv) |
|
33 { |
|
34 PRFileDesc *sock[2]; |
|
35 PRStatus status; |
|
36 PRProcess *process; |
|
37 PRProcessAttr *attr; |
|
38 char buf[1024]; |
|
39 PRInt32 nBytes; |
|
40 PRInt32 exitCode; |
|
41 int idx; |
|
42 |
|
43 status = PR_NewTCPSocketPair(sock); |
|
44 if (status == PR_FAILURE) { |
|
45 fprintf(stderr, "PR_NewTCPSocketPair failed\n"); |
|
46 exit(1); |
|
47 } |
|
48 |
|
49 status = PR_SetFDInheritable(sock[0], PR_FALSE); |
|
50 if (status == PR_FAILURE) { |
|
51 fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n", |
|
52 PR_GetError(), PR_GetOSError()); |
|
53 exit(1); |
|
54 } |
|
55 status = PR_SetFDInheritable(sock[1], PR_TRUE); |
|
56 if (status == PR_FAILURE) { |
|
57 fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n", |
|
58 PR_GetError(), PR_GetOSError()); |
|
59 exit(1); |
|
60 } |
|
61 |
|
62 attr = PR_NewProcessAttr(); |
|
63 if (attr == NULL) { |
|
64 fprintf(stderr, "PR_NewProcessAttr failed\n"); |
|
65 exit(1); |
|
66 } |
|
67 |
|
68 status = PR_ProcessAttrSetInheritableFD(attr, sock[1], "SOCKET"); |
|
69 if (status == PR_FAILURE) { |
|
70 fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n"); |
|
71 exit(1); |
|
72 } |
|
73 |
|
74 process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr); |
|
75 if (process == NULL) { |
|
76 fprintf(stderr, "PR_CreateProcess failed\n"); |
|
77 exit(1); |
|
78 } |
|
79 PR_DestroyProcessAttr(attr); |
|
80 status = PR_Close(sock[1]); |
|
81 if (status == PR_FAILURE) { |
|
82 fprintf(stderr, "PR_Close failed\n"); |
|
83 exit(1); |
|
84 } |
|
85 |
|
86 for (idx = 0; idx < NUM_ITERATIONS; idx++) { |
|
87 strcpy(buf, "ping"); |
|
88 printf("ping process: sending \"%s\"\n", buf); |
|
89 nBytes = PR_Write(sock[0], buf, 5); |
|
90 if (nBytes == -1) { |
|
91 fprintf(stderr, "PR_Write failed: (%d, %d)\n", |
|
92 PR_GetError(), PR_GetOSError()); |
|
93 exit(1); |
|
94 } |
|
95 memset(buf, 0, sizeof(buf)); |
|
96 nBytes = PR_Read(sock[0], buf, sizeof(buf)); |
|
97 if (nBytes == -1) { |
|
98 fprintf(stderr, "PR_Read failed: (%d, %d)\n", |
|
99 PR_GetError(), PR_GetOSError()); |
|
100 exit(1); |
|
101 } |
|
102 printf("ping process: received \"%s\"\n", buf); |
|
103 if (nBytes != 5) { |
|
104 fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n", |
|
105 nBytes); |
|
106 exit(1); |
|
107 } |
|
108 if (strcmp(buf, "pong") != 0) { |
|
109 fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n", |
|
110 buf); |
|
111 exit(1); |
|
112 } |
|
113 } |
|
114 |
|
115 status = PR_Close(sock[0]); |
|
116 if (status == PR_FAILURE) { |
|
117 fprintf(stderr, "PR_Close failed\n"); |
|
118 exit(1); |
|
119 } |
|
120 status = PR_WaitProcess(process, &exitCode); |
|
121 if (status == PR_FAILURE) { |
|
122 fprintf(stderr, "PR_WaitProcess failed\n"); |
|
123 exit(1); |
|
124 } |
|
125 if (exitCode == 0) { |
|
126 printf("PASS\n"); |
|
127 return 0; |
|
128 } else { |
|
129 printf("FAIL\n"); |
|
130 return 1; |
|
131 } |
|
132 } |