xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testpipe.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <stdlib.h>
18 
19 #include "testutil.h"
20 #include "apr_file_io.h"
21 #include "apr_errno.h"
22 #include "apr_general.h"
23 #include "apr_lib.h"
24 #include "apr_thread_proc.h"
25 #include "apr_strings.h"
26 
27 static apr_file_t *readp = NULL;
28 static apr_file_t *writep = NULL;
29 
create_pipe(abts_case * tc,void * data)30 static void create_pipe(abts_case *tc, void *data)
31 {
32     apr_status_t rv;
33 
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);
38 }
39 
close_pipe(abts_case * tc,void * data)40 static void close_pipe(abts_case *tc, void *data)
41 {
42     apr_status_t rv;
43     apr_size_t nbytes = 256;
44     char buf[256];
45 
46     rv = apr_file_close(readp);
47     rv = apr_file_close(writep);
48     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
49 
50     rv = apr_file_read(readp, buf, &nbytes);
51     ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EBADF(rv));
52 }
53 
set_timeout(abts_case * tc,void * data)54 static void set_timeout(abts_case *tc, void *data)
55 {
56     apr_status_t rv;
57     apr_interval_time_t timeout;
58 
59     rv = apr_file_pipe_create_ex(&readp, &writep, APR_WRITE_BLOCK, p);
60     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
61     ABTS_PTR_NOTNULL(tc, readp);
62     ABTS_PTR_NOTNULL(tc, writep);
63 
64     rv = apr_file_pipe_timeout_get(writep, &timeout);
65     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
66     ABTS_ASSERT(tc, "Timeout mismatch, expected -1", timeout == -1);
67 
68     rv = apr_file_pipe_timeout_set(readp, apr_time_from_sec(1));
69     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
70 
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));
75 }
76 
read_write(abts_case * tc,void * data)77 static void read_write(abts_case *tc, void *data)
78 {
79     apr_status_t rv;
80     char *buf;
81     apr_size_t nbytes;
82 
83     nbytes = strlen("this is a test");
84     buf = (char *)apr_palloc(p, nbytes + 1);
85 
86     rv = apr_file_pipe_create_ex(&readp, &writep, APR_WRITE_BLOCK, p);
87     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
88     ABTS_PTR_NOTNULL(tc, readp);
89     ABTS_PTR_NOTNULL(tc, writep);
90 
91     rv = apr_file_pipe_timeout_set(readp, apr_time_from_sec(1));
92     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
93 
94     if (!rv) {
95         rv = apr_file_read(readp, buf, &nbytes);
96         ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_TIMEUP(rv));
97         ABTS_SIZE_EQUAL(tc, 0, nbytes);
98     }
99 }
100 
read_write_notimeout(abts_case * tc,void * data)101 static void read_write_notimeout(abts_case *tc, void *data)
102 {
103     apr_status_t rv;
104     char *buf = "this is a test";
105     char *input;
106     apr_size_t nbytes;
107 
108     nbytes = strlen("this is a test");
109 
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);
114 
115     rv = apr_file_write(writep, buf, &nbytes);
116     ABTS_SIZE_EQUAL(tc, strlen("this is a test"), nbytes);
117     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
118 
119     nbytes = 256;
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_SIZE_EQUAL(tc, strlen("this is a test"), nbytes);
124     ABTS_STR_EQUAL(tc, "this is a test", input);
125 }
126 
test_pipe_writefull(abts_case * tc,void * data)127 static void test_pipe_writefull(abts_case *tc, void *data)
128 {
129     int iterations = 1000;
130     int i;
131     int bytes_per_iteration = 8000;
132     char *buf = (char *)malloc(bytes_per_iteration);
133     char responsebuf[128];
134     apr_size_t nbytes;
135     int bytes_processed;
136     apr_proc_t proc = {0};
137     apr_procattr_t *procattr;
138     const char *args[2];
139     apr_status_t rv;
140     apr_exit_why_e why;
141 
142     rv = apr_procattr_create(&procattr, p);
143     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
144 
145     rv = apr_procattr_io_set(procattr, APR_CHILD_BLOCK, APR_CHILD_BLOCK,
146                              APR_CHILD_BLOCK);
147     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
148 
149     rv = apr_procattr_cmdtype_set(procattr, APR_PROGRAM_ENV);
150     APR_ASSERT_SUCCESS(tc, "Couldn't set copy environment", rv);
151 
152     rv = apr_procattr_error_check_set(procattr, 1);
153     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
154 
155     args[0] = "readchild" EXTENSION;
156     args[1] = NULL;
157     rv = apr_proc_create(&proc, TESTBINPATH "readchild" EXTENSION, args, NULL, procattr, p);
158     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
159 
160     rv = apr_file_pipe_timeout_set(proc.in, apr_time_from_sec(10));
161     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
162 
163     rv = apr_file_pipe_timeout_set(proc.out, apr_time_from_sec(10));
164     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
165 
166     i = iterations;
167     do {
168         rv = apr_file_write_full(proc.in, buf, bytes_per_iteration, NULL);
169         ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
170     } while (--i);
171 
172     free(buf);
173 
174     rv = apr_file_close(proc.in);
175     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
176 
177     nbytes = sizeof(responsebuf);
178     rv = apr_file_read(proc.out, responsebuf, &nbytes);
179     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
180     bytes_processed = (int)apr_strtoi64(responsebuf, NULL, 10);
181     ABTS_INT_EQUAL(tc, iterations * bytes_per_iteration, bytes_processed);
182 
183     ABTS_ASSERT(tc, "wait for child process",
184              apr_proc_wait(&proc, NULL, &why, APR_WAIT) == APR_CHILD_DONE);
185 
186     ABTS_ASSERT(tc, "child terminated normally", why == APR_PROC_EXIT);
187 }
188 
testpipe(abts_suite * suite)189 abts_suite *testpipe(abts_suite *suite)
190 {
191     suite = ADD_SUITE(suite)
192 
193     abts_run_test(suite, create_pipe, NULL);
194     abts_run_test(suite, close_pipe, NULL);
195     abts_run_test(suite, set_timeout, NULL);
196     abts_run_test(suite, close_pipe, NULL);
197     abts_run_test(suite, read_write, NULL);
198     abts_run_test(suite, close_pipe, NULL);
199     abts_run_test(suite, read_write_notimeout, NULL);
200     abts_run_test(suite, test_pipe_writefull, NULL);
201     abts_run_test(suite, close_pipe, NULL);
202 
203     return suite;
204 }
205 
206