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.
20 #include "apr_file_io.h"
21 #include "apr_errno.h"
22 #include "apr_general.h"
24 #include "apr_thread_proc.h"
25 #include "apr_strings.h"
27 static apr_file_t *readp = NULL;
28 static apr_file_t *writep = NULL;
30 static void create_pipe(CuTest *tc)
34 rv = apr_file_pipe_create(&readp, &writep, p);
35 CuAssertIntEquals(tc, APR_SUCCESS, rv);
36 CuAssertPtrNotNull(tc, readp);
37 CuAssertPtrNotNull(tc, writep);
40 static void close_pipe(CuTest *tc)
43 apr_size_t nbytes = 256;
46 rv = apr_file_close(readp);
47 rv = apr_file_close(writep);
48 CuAssertIntEquals(tc, APR_SUCCESS, rv);
50 rv = apr_file_read(readp, buf, &nbytes);
51 CuAssertIntEquals(tc, 1, APR_STATUS_IS_EBADF(rv));
54 static void set_timeout(CuTest *tc)
57 apr_file_t *readp = NULL;
58 apr_file_t *writep = NULL;
59 apr_interval_time_t timeout;
61 rv = apr_file_pipe_create(&readp, &writep, p);
62 CuAssertIntEquals(tc, APR_SUCCESS, rv);
63 CuAssertPtrNotNull(tc, readp);
64 CuAssertPtrNotNull(tc, writep);
66 rv = apr_file_pipe_timeout_get(readp, &timeout);
67 CuAssertIntEquals(tc, APR_SUCCESS, rv);
68 CuAssert(tc, "Timeout mismatch, expected -1", timeout == -1);
70 rv = apr_file_pipe_timeout_set(readp, apr_time_from_sec(1));
71 CuAssertIntEquals(tc, APR_SUCCESS, rv);
73 rv = apr_file_pipe_timeout_get(readp, &timeout);
74 CuAssertIntEquals(tc, APR_SUCCESS, rv);
75 CuAssert(tc, "Timeout mismatch, expected 1 second",
76 timeout == apr_time_from_sec(1));
79 static void read_write(CuTest *tc)
85 nbytes = strlen("this is a test");
86 buf = (char *)apr_palloc(p, nbytes + 1);
88 rv = apr_file_pipe_create(&readp, &writep, p);
89 CuAssertIntEquals(tc, APR_SUCCESS, rv);
90 CuAssertPtrNotNull(tc, readp);
91 CuAssertPtrNotNull(tc, writep);
93 rv = apr_file_pipe_timeout_set(readp, apr_time_from_sec(1));
94 CuAssertIntEquals(tc, APR_SUCCESS, rv);
96 rv = apr_file_read(readp, buf, &nbytes);
98 CuAssertIntEquals(tc, 1, APR_STATUS_IS_TIMEUP(rv));
99 CuAssertIntEquals(tc, 0, nbytes);
103 static void read_write_notimeout(CuTest *tc)
106 char *buf = "this is a test";
110 nbytes = strlen("this is a test");
112 rv = apr_file_pipe_create(&readp, &writep, p);
113 CuAssertIntEquals(tc, APR_SUCCESS, rv);
114 CuAssertPtrNotNull(tc, readp);
115 CuAssertPtrNotNull(tc, writep);
117 rv = apr_file_write(writep, buf, &nbytes);
118 CuAssertIntEquals(tc, strlen("this is a test"), nbytes);
119 CuAssertIntEquals(tc, APR_SUCCESS, rv);
122 input = apr_pcalloc(p, nbytes + 1);
123 rv = apr_file_read(readp, input, &nbytes);
124 CuAssertIntEquals(tc, APR_SUCCESS, rv);
125 CuAssertIntEquals(tc, strlen("this is a test"), nbytes);
126 CuAssertStrEquals(tc, "this is a test", input);
131 #define EXTENSION ".exe"
133 #define EXTENSION ".nlm"
138 static void test_pipe_writefull(CuTest *tc)
140 int iterations = 1000;
142 int bytes_per_iteration = 8000;
143 char *buf = (char *)malloc(bytes_per_iteration);
144 char responsebuf[128];
147 apr_proc_t proc = {0};
148 apr_procattr_t *procattr;
152 rv = apr_procattr_create(&procattr, p);
153 CuAssertIntEquals(tc, APR_SUCCESS, rv);
155 rv = apr_procattr_io_set(procattr, APR_CHILD_BLOCK, APR_CHILD_BLOCK,
157 CuAssertIntEquals(tc, APR_SUCCESS, rv);
159 rv = apr_procattr_error_check_set(procattr, 1);
160 CuAssertIntEquals(tc, APR_SUCCESS, rv);
162 args[0] = "readchild" EXTENSION;
164 rv = apr_proc_create(&proc, "./readchild" EXTENSION, args, NULL, procattr, p);
165 CuAssertIntEquals(tc, APR_SUCCESS, rv);
167 rv = apr_file_pipe_timeout_set(proc.in, apr_time_from_sec(10));
168 CuAssertIntEquals(tc, APR_SUCCESS, rv);
170 rv = apr_file_pipe_timeout_set(proc.out, apr_time_from_sec(10));
171 CuAssertIntEquals(tc, APR_SUCCESS, rv);
175 rv = apr_file_write_full(proc.in, buf, bytes_per_iteration, NULL);
176 CuAssertIntEquals(tc, APR_SUCCESS, rv);
181 rv = apr_file_close(proc.in);
182 CuAssertIntEquals(tc, APR_SUCCESS, rv);
184 nbytes = sizeof(responsebuf);
185 rv = apr_file_read(proc.out, responsebuf, &nbytes);
186 CuAssertIntEquals(tc, APR_SUCCESS, rv);
187 bytes_processed = (int)apr_strtoi64(responsebuf, NULL, 10);
188 CuAssertIntEquals(tc, iterations * bytes_per_iteration, bytes_processed);
191 CuSuite *testpipe(void)
193 CuSuite *suite = CuSuiteNew("Pipes");
195 SUITE_ADD_TEST(suite, create_pipe);
196 SUITE_ADD_TEST(suite, close_pipe);
197 SUITE_ADD_TEST(suite, set_timeout);
198 SUITE_ADD_TEST(suite, read_write);
199 SUITE_ADD_TEST(suite, read_write_notimeout);
200 SUITE_ADD_TEST(suite, test_pipe_writefull);