1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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(abts_case *tc, void *data)
34
rv = apr_file_pipe_create(&readp, &writep, p);
35
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
36
ABTS_PTR_NOTNULL(tc, readp);
37
ABTS_PTR_NOTNULL(tc, writep);
40
static void close_pipe(abts_case *tc, void *data)
43
apr_size_t nbytes = 256;
46
rv = apr_file_close(readp);
47
rv = apr_file_close(writep);
48
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
50
rv = apr_file_read(readp, buf, &nbytes);
51
ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EBADF(rv));
54
static void set_timeout(abts_case *tc, void *data)
57
apr_interval_time_t timeout;
59
rv = apr_file_pipe_create(&readp, &writep, p);
60
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
61
ABTS_PTR_NOTNULL(tc, readp);
62
ABTS_PTR_NOTNULL(tc, writep);
64
rv = apr_file_pipe_timeout_get(readp, &timeout);
65
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
66
ABTS_ASSERT(tc, "Timeout mismatch, expected -1", timeout == -1);
68
rv = apr_file_pipe_timeout_set(readp, apr_time_from_sec(1));
69
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
71
rv = apr_file_pipe_timeout_get(readp, &timeout);
72
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
73
ABTS_ASSERT(tc, "Timeout mismatch, expected 1 second",
74
timeout == apr_time_from_sec(1));
77
static void read_write(abts_case *tc, void *data)
83
nbytes = strlen("this is a test");
84
buf = (char *)apr_palloc(p, nbytes + 1);
86
rv = apr_file_pipe_create(&readp, &writep, p);
87
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
88
ABTS_PTR_NOTNULL(tc, readp);
89
ABTS_PTR_NOTNULL(tc, writep);
91
rv = apr_file_pipe_timeout_set(readp, apr_time_from_sec(1));
92
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
95
rv = apr_file_read(readp, buf, &nbytes);
96
ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_TIMEUP(rv));
97
ABTS_INT_EQUAL(tc, 0, nbytes);
101
static void read_write_notimeout(abts_case *tc, void *data)
104
char *buf = "this is a test";
108
nbytes = strlen("this is a test");
110
rv = apr_file_pipe_create(&readp, &writep, p);
111
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
112
ABTS_PTR_NOTNULL(tc, readp);
113
ABTS_PTR_NOTNULL(tc, writep);
115
rv = apr_file_write(writep, buf, &nbytes);
116
ABTS_INT_EQUAL(tc, strlen("this is a test"), nbytes);
117
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
120
input = apr_pcalloc(p, nbytes + 1);
121
rv = apr_file_read(readp, input, &nbytes);
122
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
123
ABTS_INT_EQUAL(tc, strlen("this is a test"), nbytes);
124
ABTS_STR_EQUAL(tc, "this is a test", input);
127
static void test_pipe_writefull(abts_case *tc, void *data)
129
int iterations = 1000;
131
int bytes_per_iteration = 8000;
132
char *buf = (char *)malloc(bytes_per_iteration);
133
char responsebuf[128];
136
apr_proc_t proc = {0};
137
apr_procattr_t *procattr;
142
rv = apr_procattr_create(&procattr, p);
143
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
145
rv = apr_procattr_io_set(procattr, APR_CHILD_BLOCK, APR_CHILD_BLOCK,
147
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
149
rv = apr_procattr_error_check_set(procattr, 1);
150
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
152
args[0] = "readchild" EXTENSION;
154
rv = apr_proc_create(&proc, "./readchild" EXTENSION, args, NULL, procattr, p);
155
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
157
rv = apr_file_pipe_timeout_set(proc.in, apr_time_from_sec(10));
158
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
160
rv = apr_file_pipe_timeout_set(proc.out, apr_time_from_sec(10));
161
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
165
rv = apr_file_write_full(proc.in, buf, bytes_per_iteration, NULL);
166
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
171
rv = apr_file_close(proc.in);
172
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
174
nbytes = sizeof(responsebuf);
175
rv = apr_file_read(proc.out, responsebuf, &nbytes);
176
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
177
bytes_processed = (int)apr_strtoi64(responsebuf, NULL, 10);
178
ABTS_INT_EQUAL(tc, iterations * bytes_per_iteration, bytes_processed);
180
ABTS_ASSERT(tc, "wait for child process",
181
apr_proc_wait(&proc, NULL, &why, APR_WAIT) == APR_CHILD_DONE);
183
ABTS_ASSERT(tc, "child terminated normally", why == APR_PROC_EXIT);
186
abts_suite *testpipe(abts_suite *suite)
188
suite = ADD_SUITE(suite)
190
abts_run_test(suite, create_pipe, NULL);
191
abts_run_test(suite, close_pipe, NULL);
192
abts_run_test(suite, set_timeout, NULL);
193
abts_run_test(suite, close_pipe, NULL);
194
abts_run_test(suite, read_write, NULL);
195
abts_run_test(suite, close_pipe, NULL);
196
abts_run_test(suite, read_write_notimeout, NULL);
197
abts_run_test(suite, test_pipe_writefull, NULL);
198
abts_run_test(suite, close_pipe, NULL);