upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testproc.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_thread_proc.h"
18 #include "apr_errno.h"
19 #include "apr_general.h"
20 #include "apr_lib.h"
21 #include "apr_strings.h"
22 #include "test_apr.h"
23
24 /* XXX I'm sure there has to be a better way to do this ... */
25 #ifdef WIN32
26 #define EXTENSION ".exe"
27 #elif NETWARE
28 #define EXTENSION ".nlm"
29 #else
30 #define EXTENSION
31 #endif
32
33 #define TESTSTR "This is a test"
34
35 static apr_proc_t newproc;
36
37 static void test_create_proc(CuTest *tc)
38 {
39     const char *args[2];
40     apr_procattr_t *attr;
41     apr_file_t *testfile = NULL;
42     apr_status_t rv;
43     apr_size_t length;
44     char *buf;
45
46     rv = apr_procattr_create(&attr, p);
47     CuAssertIntEquals(tc, APR_SUCCESS, rv);
48
49     rv = apr_procattr_io_set(attr, APR_FULL_BLOCK, APR_FULL_BLOCK, 
50                              APR_NO_PIPE);
51     CuAssertIntEquals(tc, APR_SUCCESS, rv);
52
53     rv = apr_procattr_dir_set(attr, "data");
54     CuAssertIntEquals(tc, APR_SUCCESS, rv);
55
56     rv = apr_procattr_cmdtype_set(attr, APR_PROGRAM);
57     CuAssertIntEquals(tc, APR_SUCCESS, rv);
58
59     args[0] = "proc_child" EXTENSION;
60     args[1] = NULL;
61     
62     rv = apr_proc_create(&newproc, "../proc_child" EXTENSION, args, NULL, 
63                          attr, p);
64     CuAssertIntEquals(tc, APR_SUCCESS, rv);
65
66     testfile = newproc.in;
67
68     length = strlen(TESTSTR);
69     rv = apr_file_write(testfile, TESTSTR, &length);
70     CuAssertIntEquals(tc, APR_SUCCESS, rv);
71     CuAssertIntEquals(tc, strlen(TESTSTR), length);
72
73     testfile = newproc.out;
74     length = 256;
75     buf = apr_pcalloc(p, length);
76     rv = apr_file_read(testfile, buf, &length);
77     CuAssertIntEquals(tc, APR_SUCCESS, rv);
78     CuAssertStrEquals(tc, TESTSTR, buf);
79 }
80
81 static void test_proc_wait(CuTest *tc)
82 {
83     apr_status_t rv;
84
85     rv = apr_proc_wait(&newproc, NULL, NULL, APR_WAIT);
86     CuAssertIntEquals(tc, APR_CHILD_DONE, rv);
87 }
88
89 static void test_file_redir(CuTest *tc)
90 {
91     apr_file_t *testout = NULL;
92     apr_file_t *testerr = NULL;
93     apr_off_t offset;
94     apr_status_t rv;
95     const char *args[2];
96     apr_procattr_t *attr;
97     apr_file_t *testfile = NULL;
98     apr_size_t length;
99     char *buf;
100
101     testfile = NULL;
102     rv = apr_file_open(&testfile, "data/stdin",
103                        APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
104                        APR_OS_DEFAULT, p);
105     CuAssertIntEquals(tc, APR_SUCCESS, rv);
106     rv = apr_file_open(&testout, "data/stdout",
107                        APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
108                        APR_OS_DEFAULT, p);
109     CuAssertIntEquals(tc, APR_SUCCESS, rv);
110     rv = apr_file_open(&testerr, "data/stderr",
111                        APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
112                        APR_OS_DEFAULT, p);
113     CuAssertIntEquals(tc, APR_SUCCESS, rv);
114
115     length = strlen(TESTSTR);
116     apr_file_write(testfile, TESTSTR, &length);
117     offset = 0;
118     rv = apr_file_seek(testfile, APR_SET, &offset);
119     CuAssertIntEquals(tc, APR_SUCCESS, rv);
120     CuAssert(tc, "File position mismatch, expected 0", offset == 0);
121
122     rv = apr_procattr_create(&attr, p);
123     CuAssertIntEquals(tc, APR_SUCCESS, rv);
124     rv = apr_procattr_child_in_set(attr, testfile, NULL);
125     CuAssertIntEquals(tc, APR_SUCCESS, rv);
126     rv = apr_procattr_child_out_set(attr, testout, NULL);
127     CuAssertIntEquals(tc, APR_SUCCESS, rv);
128     rv = apr_procattr_child_err_set(attr, testerr, NULL);
129     CuAssertIntEquals(tc, APR_SUCCESS, rv);
130     rv = apr_procattr_dir_set(attr, "data");
131     CuAssertIntEquals(tc, APR_SUCCESS, rv);
132     rv = apr_procattr_cmdtype_set(attr, APR_PROGRAM);
133     CuAssertIntEquals(tc, APR_SUCCESS, rv);
134
135     args[0] = "proc_child";
136     args[1] = NULL;
137
138     rv = apr_proc_create(&newproc, "../proc_child" EXTENSION, args, NULL, 
139                          attr, p);
140     CuAssertIntEquals(tc, APR_SUCCESS, rv);
141
142     rv = apr_proc_wait(&newproc, NULL, NULL, APR_WAIT);
143     CuAssertIntEquals(tc, APR_CHILD_DONE, rv);
144
145     offset = 0;
146     rv = apr_file_seek(testout, APR_SET, &offset);
147     CuAssertIntEquals(tc, APR_SUCCESS, rv);
148
149     length = 256;
150     buf = apr_pcalloc(p, length);
151     rv = apr_file_read(testout, buf, &length);
152     CuAssertIntEquals(tc, APR_SUCCESS, rv);
153     CuAssertStrEquals(tc, TESTSTR, buf);
154
155
156     apr_file_close(testfile);
157     apr_file_close(testout);
158     apr_file_close(testerr);
159
160     rv = apr_file_remove("data/stdin", p);;
161     CuAssertIntEquals(tc, APR_SUCCESS, rv);
162     rv = apr_file_remove("data/stdout", p);;
163     CuAssertIntEquals(tc, APR_SUCCESS, rv);
164     rv = apr_file_remove("data/stderr", p);;
165     CuAssertIntEquals(tc, APR_SUCCESS, rv);
166 }
167
168 CuSuite *testproc(void)
169 {
170     CuSuite *suite = CuSuiteNew("Process control");
171
172     SUITE_ADD_TEST(suite, test_create_proc);
173     SUITE_ADD_TEST(suite, test_proc_wait);
174     SUITE_ADD_TEST(suite, test_file_redir);
175
176     return suite;
177 }
178