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

« back to all changes in this revision

Viewing changes to srclib/apr/test/testdup.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
 
 
18
#include "apr_general.h"
 
19
#include "apr_pools.h"
 
20
#include "apr_errno.h"
 
21
#include "apr_file_io.h"
 
22
#include "testutil.h"
 
23
 
 
24
#define TEST "Testing\n"
 
25
#define TEST2 "Testing again\n"
 
26
#define FILEPATH "data/"
 
27
 
 
28
static void test_file_dup(abts_case *tc, void *data)
 
29
{
 
30
    apr_file_t *file1 = NULL;
 
31
    apr_file_t *file3 = NULL;
 
32
    apr_status_t rv;
 
33
    apr_finfo_t finfo;
 
34
 
 
35
    /* First, create a new file, empty... */
 
36
    rv = apr_file_open(&file1, FILEPATH "testdup.file", 
 
37
                       APR_READ | APR_WRITE | APR_CREATE |
 
38
                       APR_DELONCLOSE, APR_OS_DEFAULT, p);
 
39
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
40
    ABTS_PTR_NOTNULL(tc, file1);
 
41
 
 
42
    rv = apr_file_dup(&file3, file1, p);
 
43
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
44
    ABTS_PTR_NOTNULL(tc, file3);
 
45
 
 
46
    rv = apr_file_close(file1);
 
47
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
48
 
 
49
    /* cleanup after ourselves */
 
50
    rv = apr_file_close(file3);
 
51
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
52
    rv = apr_stat(&finfo, FILEPATH "testdup.file", APR_FINFO_NORM, p);
 
53
    ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
 
54
}  
 
55
 
 
56
static void test_file_readwrite(abts_case *tc, void *data)
 
57
{
 
58
    apr_file_t *file1 = NULL;
 
59
    apr_file_t *file3 = NULL;
 
60
    apr_status_t rv;
 
61
    apr_finfo_t finfo;
 
62
    apr_size_t txtlen = sizeof(TEST);
 
63
    char buff[50];
 
64
    apr_off_t fpos;
 
65
 
 
66
    /* First, create a new file, empty... */
 
67
    rv = apr_file_open(&file1, FILEPATH "testdup.readwrite.file", 
 
68
                       APR_READ | APR_WRITE | APR_CREATE |
 
69
                       APR_DELONCLOSE, APR_OS_DEFAULT, p);
 
70
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
71
    ABTS_PTR_NOTNULL(tc, file1);
 
72
 
 
73
    rv = apr_file_dup(&file3, file1, p);
 
74
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
75
    ABTS_PTR_NOTNULL(tc, file3);
 
76
 
 
77
    rv = apr_file_write(file3, TEST, &txtlen);
 
78
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
79
    ABTS_INT_EQUAL(tc, sizeof(TEST), txtlen);
 
80
 
 
81
    fpos = 0;
 
82
    rv = apr_file_seek(file1, APR_SET, &fpos);
 
83
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
84
    ABTS_ASSERT(tc, "File position mismatch, expected 0", fpos == 0);
 
85
 
 
86
    txtlen = 50;
 
87
    rv = apr_file_read(file1, buff, &txtlen);
 
88
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
89
    ABTS_STR_EQUAL(tc, TEST, buff);
 
90
 
 
91
    /* cleanup after ourselves */
 
92
    rv = apr_file_close(file1);
 
93
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
94
 
 
95
    rv = apr_file_close(file3);
 
96
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
97
    rv = apr_stat(&finfo, FILEPATH "testdup.readwrite.file", APR_FINFO_NORM, p);
 
98
    ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
 
99
}  
 
100
 
 
101
static void test_dup2(abts_case *tc, void *data)
 
102
{
 
103
    apr_file_t *testfile = NULL;
 
104
    apr_file_t *errfile = NULL;
 
105
    apr_file_t *saveerr = NULL;
 
106
    apr_status_t rv;
 
107
 
 
108
    rv = apr_file_open(&testfile, FILEPATH "testdup2.file", 
 
109
                       APR_READ | APR_WRITE | APR_CREATE |
 
110
                       APR_DELONCLOSE, APR_OS_DEFAULT, p);
 
111
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
112
    ABTS_PTR_NOTNULL(tc, testfile);
 
113
 
 
114
    rv = apr_file_open_stderr(&errfile, p);
 
115
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
116
 
 
117
    /* Set aside the real errfile */
 
118
    rv = apr_file_dup(&saveerr, errfile, p);
 
119
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
120
    ABTS_PTR_NOTNULL(tc, saveerr);
 
121
 
 
122
    rv = apr_file_dup2(errfile, testfile, p);
 
123
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
124
    ABTS_PTR_NOTNULL(tc, errfile);
 
125
 
 
126
    apr_file_close(testfile);
 
127
 
 
128
    rv = apr_file_dup2(errfile, saveerr, p);
 
129
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
130
    ABTS_PTR_NOTNULL(tc, errfile);
 
131
}
 
132
 
 
133
static void test_dup2_readwrite(abts_case *tc, void *data)
 
134
{
 
135
    apr_file_t *errfile = NULL;
 
136
    apr_file_t *testfile = NULL;
 
137
    apr_file_t *saveerr = NULL;
 
138
    apr_status_t rv;
 
139
    apr_size_t txtlen = sizeof(TEST);
 
140
    char buff[50];
 
141
    apr_off_t fpos;
 
142
 
 
143
    rv = apr_file_open(&testfile, FILEPATH "testdup2.readwrite.file", 
 
144
                       APR_READ | APR_WRITE | APR_CREATE |
 
145
                       APR_DELONCLOSE, APR_OS_DEFAULT, p);
 
146
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
147
    ABTS_PTR_NOTNULL(tc, testfile);
 
148
 
 
149
    rv = apr_file_open_stderr(&errfile, p);
 
150
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
151
 
 
152
    /* Set aside the real errfile */
 
153
    rv = apr_file_dup(&saveerr, errfile, p);
 
154
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
155
    ABTS_PTR_NOTNULL(tc, saveerr);
 
156
 
 
157
    rv = apr_file_dup2(errfile, testfile, p);
 
158
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
159
    ABTS_PTR_NOTNULL(tc, errfile);
 
160
 
 
161
    txtlen = sizeof(TEST2);
 
162
    rv = apr_file_write(errfile, TEST2, &txtlen);
 
163
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
164
    ABTS_INT_EQUAL(tc, sizeof(TEST2), txtlen);
 
165
 
 
166
    fpos = 0;
 
167
    rv = apr_file_seek(testfile, APR_SET, &fpos);
 
168
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
169
    ABTS_ASSERT(tc, "File position mismatch, expected 0", fpos == 0);
 
170
 
 
171
    txtlen = 50;
 
172
    rv = apr_file_read(testfile, buff, &txtlen);
 
173
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
174
    ABTS_STR_EQUAL(tc, TEST2, buff);
 
175
      
 
176
    apr_file_close(testfile);
 
177
 
 
178
    rv = apr_file_dup2(errfile, saveerr, p);
 
179
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
180
    ABTS_PTR_NOTNULL(tc, errfile);
 
181
}
 
182
 
 
183
abts_suite *testdup(abts_suite *suite)
 
184
{
 
185
    suite = ADD_SUITE(suite)
 
186
 
 
187
    abts_run_test(suite, test_file_dup, NULL);
 
188
    abts_run_test(suite, test_file_readwrite, NULL);
 
189
    abts_run_test(suite, test_dup2, NULL);
 
190
    abts_run_test(suite, test_dup2_readwrite, NULL);
 
191
 
 
192
    return suite;
 
193
}
 
194