Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / tests / pipepong2.c
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) 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: pipepong2.c
40  *
41  * Description:
42  * This test runs in conjunction with the pipeping2 test.
43  * The pipeping2 test creates two pipes and passes two
44  * pipe fd's to this test.  Then the pipeping2 test writes
45  * "ping" to this test and this test writes "pong" back.
46  * To run this pair of tests, just invoke pipeping2.
47  *
48  * Tested areas: process creation, pipes, file descriptor
49  * inheritance.
50  */
51
52 #include "prerror.h"
53 #include "prio.h"
54
55 #include <stdio.h>
56 #include <string.h>
57 #include <stdlib.h>
58
59 #define NUM_ITERATIONS 10
60
61 int main(int argc, char **argv)
62 {
63     PRFileDesc *pipe_read, *pipe_write;
64     PRStatus status;
65     char buf[1024];
66     PRInt32 nBytes;
67     int idx;
68
69     pipe_read = PR_GetInheritedFD("PIPE_READ");
70     if (pipe_read == NULL) {
71         fprintf(stderr, "PR_GetInheritedFD failed\n");
72         exit(1);
73     }
74     status = PR_SetFDInheritable(pipe_read, PR_FALSE);
75     if (status == PR_FAILURE) {
76         fprintf(stderr, "PR_SetFDInheritable failed\n");
77         exit(1);
78     }
79     pipe_write = PR_GetInheritedFD("PIPE_WRITE");
80     if (pipe_write == NULL) {
81         fprintf(stderr, "PR_GetInheritedFD failed\n");
82         exit(1);
83     }
84     status = PR_SetFDInheritable(pipe_write, PR_FALSE);
85     if (status == PR_FAILURE) {
86         fprintf(stderr, "PR_SetFDInheritable failed\n");
87         exit(1);
88     }
89
90     for (idx = 0; idx < NUM_ITERATIONS; idx++) {
91         memset(buf, 0, sizeof(buf));
92         nBytes = PR_Read(pipe_read, buf, sizeof(buf));
93         if (nBytes == -1) {
94             fprintf(stderr, "PR_Read failed: (%d, %d)\n",
95                     PR_GetError(), PR_GetOSError());
96             exit(1);
97         }
98         printf("pong process: received \"%s\"\n", buf);
99         if (nBytes != 5) {
100             fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
101                     nBytes);
102             exit(1);
103         }
104         if (strcmp(buf, "ping") != 0) {
105             fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n",
106                     buf);
107             exit(1);
108         }
109
110         strcpy(buf, "pong");
111         printf("pong process: sending \"%s\"\n", buf);
112         nBytes = PR_Write(pipe_write, buf, 5);
113         if (nBytes == -1) {
114             fprintf(stderr, "PR_Write failed\n");
115             exit(1);
116         }
117     }
118
119     status = PR_Close(pipe_read);
120     if (status == PR_FAILURE) {
121         fprintf(stderr, "PR_Close failed\n");
122         exit(1);
123     }
124     status = PR_Close(pipe_write);
125     if (status == PR_FAILURE) {
126         fprintf(stderr, "PR_Close failed\n");
127         exit(1);
128     }
129     return 0;
130 }