~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/test/testpipe.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
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
 
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
 
 
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
 
 
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
 
 
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(&readp, &writep, 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(readp, &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
 
 
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(&readp, &writep, 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_INT_EQUAL(tc, 0, nbytes);
 
98
    }
 
99
}
 
100
 
 
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_INT_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_INT_EQUAL(tc, strlen("this is a test"), nbytes);
 
124
    ABTS_STR_EQUAL(tc, "this is a test", input);
 
125
}
 
126
 
 
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_error_check_set(procattr, 1);
 
150
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
151
 
 
152
    args[0] = "readchild" EXTENSION;
 
153
    args[1] = NULL;
 
154
    rv = apr_proc_create(&proc, "./readchild" EXTENSION, args, NULL, procattr, p);
 
155
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
156
 
 
157
    rv = apr_file_pipe_timeout_set(proc.in, apr_time_from_sec(10));
 
158
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
159
 
 
160
    rv = apr_file_pipe_timeout_set(proc.out, apr_time_from_sec(10));
 
161
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
162
 
 
163
    i = iterations;
 
164
    do {
 
165
        rv = apr_file_write_full(proc.in, buf, bytes_per_iteration, NULL);
 
166
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
167
    } while (--i);
 
168
 
 
169
    free(buf);
 
170
 
 
171
    rv = apr_file_close(proc.in);
 
172
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
173
    
 
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);
 
179
 
 
180
    ABTS_ASSERT(tc, "wait for child process",
 
181
             apr_proc_wait(&proc, NULL, &why, APR_WAIT) == APR_CHILD_DONE);
 
182
    
 
183
    ABTS_ASSERT(tc, "child terminated normally", why == APR_PROC_EXIT);
 
184
}
 
185
 
 
186
abts_suite *testpipe(abts_suite *suite)
 
187
{
 
188
    suite = ADD_SUITE(suite)
 
189
 
 
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);
 
199
 
 
200
    return suite;
 
201
}
 
202