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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_thread_proc.h"
18 #include "apr_errno.h"
19 #include "apr_general.h"
21 #include "apr_strings.h"
24 /* XXX I'm sure there has to be a better way to do this ... */
26 #define EXTENSION ".exe"
28 #define EXTENSION ".nlm"
33 #define TESTSTR "This is a test"
35 static apr_proc_t newproc;
37 static void test_create_proc(CuTest *tc)
41 apr_file_t *testfile = NULL;
46 rv = apr_procattr_create(&attr, p);
47 CuAssertIntEquals(tc, APR_SUCCESS, rv);
49 rv = apr_procattr_io_set(attr, APR_FULL_BLOCK, APR_FULL_BLOCK,
51 CuAssertIntEquals(tc, APR_SUCCESS, rv);
53 rv = apr_procattr_dir_set(attr, "data");
54 CuAssertIntEquals(tc, APR_SUCCESS, rv);
56 rv = apr_procattr_cmdtype_set(attr, APR_PROGRAM);
57 CuAssertIntEquals(tc, APR_SUCCESS, rv);
59 args[0] = "proc_child" EXTENSION;
62 rv = apr_proc_create(&newproc, "../proc_child" EXTENSION, args, NULL,
64 CuAssertIntEquals(tc, APR_SUCCESS, rv);
66 testfile = newproc.in;
68 length = strlen(TESTSTR);
69 rv = apr_file_write(testfile, TESTSTR, &length);
70 CuAssertIntEquals(tc, APR_SUCCESS, rv);
71 CuAssertIntEquals(tc, strlen(TESTSTR), length);
73 testfile = newproc.out;
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);
81 static void test_proc_wait(CuTest *tc)
85 rv = apr_proc_wait(&newproc, NULL, NULL, APR_WAIT);
86 CuAssertIntEquals(tc, APR_CHILD_DONE, rv);
89 static void test_file_redir(CuTest *tc)
91 apr_file_t *testout = NULL;
92 apr_file_t *testerr = NULL;
97 apr_file_t *testfile = NULL;
102 rv = apr_file_open(&testfile, "data/stdin",
103 APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
105 CuAssertIntEquals(tc, APR_SUCCESS, rv);
106 rv = apr_file_open(&testout, "data/stdout",
107 APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
109 CuAssertIntEquals(tc, APR_SUCCESS, rv);
110 rv = apr_file_open(&testerr, "data/stderr",
111 APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
113 CuAssertIntEquals(tc, APR_SUCCESS, rv);
115 length = strlen(TESTSTR);
116 apr_file_write(testfile, TESTSTR, &length);
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);
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);
135 args[0] = "proc_child";
138 rv = apr_proc_create(&newproc, "../proc_child" EXTENSION, args, NULL,
140 CuAssertIntEquals(tc, APR_SUCCESS, rv);
142 rv = apr_proc_wait(&newproc, NULL, NULL, APR_WAIT);
143 CuAssertIntEquals(tc, APR_CHILD_DONE, rv);
146 rv = apr_file_seek(testout, APR_SET, &offset);
147 CuAssertIntEquals(tc, APR_SUCCESS, rv);
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);
156 apr_file_close(testfile);
157 apr_file_close(testout);
158 apr_file_close(testerr);
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);
168 CuSuite *testproc(void)
170 CuSuite *suite = CuSuiteNew("Process control");
172 SUITE_ADD_TEST(suite, test_create_proc);
173 SUITE_ADD_TEST(suite, test_proc_wait);
174 SUITE_ADD_TEST(suite, test_file_redir);