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

« back to all changes in this revision

Viewing changes to srclib/apr/test/testlfs.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 2004-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 "apr_file_io.h"
 
18
#include "apr_file_info.h"
 
19
#include "apr_errno.h"
 
20
#include "apr_general.h"
 
21
#include "apr_poll.h"
 
22
#include "apr_strings.h"
 
23
#include "apr_lib.h"
 
24
#include "apr_mmap.h"
 
25
#include "testutil.h"
 
26
 
 
27
/* Only enable these tests by default on platforms which support sparse
 
28
 * files... just Unixes? */
 
29
#if defined(WIN32) || defined(OS2) || defined(NETWARE)
 
30
static void test_nolfs(abts_case *tc, void *data)
 
31
{
 
32
    ABTS_NOT_IMPL(tc, "Large Files tests require Sparse file support");
 
33
}
 
34
#elif APR_HAS_LARGE_FILES
 
35
#define USE_LFS_TESTS
 
36
 
 
37
/* Tests which create an 8Gb sparse file and then check it can be used
 
38
 * as normal. */
 
39
 
 
40
static apr_off_t eightGb = APR_INT64_C(2) << 32;
 
41
 
 
42
static int madefile = 0;
 
43
 
 
44
#define PRECOND if (!madefile) { ABTS_NOT_IMPL(tc, "Large file tests not enabled"); return; }
 
45
 
 
46
#define TESTDIR "lfstests"
 
47
#define TESTFILE "large.bin"
 
48
#define TESTFN "lfstests/large.bin"
 
49
 
 
50
static void test_open(abts_case *tc, void *data)
 
51
{
 
52
    apr_file_t *f;
 
53
    apr_status_t rv;
 
54
 
 
55
    rv = apr_dir_make(TESTDIR, APR_OS_DEFAULT, p);
 
56
    if (rv && !APR_STATUS_IS_EEXIST(rv)) {
 
57
        APR_ASSERT_SUCCESS(tc, "make test directory", rv);
 
58
    }
 
59
 
 
60
    APR_ASSERT_SUCCESS(tc, "open file",
 
61
                       apr_file_open(&f, TESTFN, 
 
62
                                     APR_CREATE | APR_WRITE | APR_TRUNCATE,
 
63
                                     APR_OS_DEFAULT, p));
 
64
 
 
65
    rv = apr_file_trunc(f, eightGb);
 
66
 
 
67
    APR_ASSERT_SUCCESS(tc, "close large file", apr_file_close(f));
 
68
 
 
69
    /* 8Gb may pass rlimits or filesystem limits */
 
70
 
 
71
    if (APR_STATUS_IS_EINVAL(rv)
 
72
#ifdef EFBIG
 
73
        || rv == EFBIG
 
74
#endif
 
75
        ) {
 
76
        ABTS_NOT_IMPL(tc, "Creation of large file (limited by rlimit or fs?)");
 
77
    } 
 
78
    else {
 
79
        APR_ASSERT_SUCCESS(tc, "truncate file to 8gb", rv);
 
80
    }
 
81
 
 
82
    madefile = rv == APR_SUCCESS;
 
83
}
 
84
 
 
85
static void test_reopen(abts_case *tc, void *data)
 
86
{
 
87
    apr_file_t *fh;
 
88
    apr_finfo_t finfo;
 
89
 
 
90
    PRECOND;
 
91
    
 
92
    APR_ASSERT_SUCCESS(tc, "re-open 8Gb file",
 
93
                       apr_file_open(&fh, TESTFN, APR_READ, APR_OS_DEFAULT, p));
 
94
 
 
95
    APR_ASSERT_SUCCESS(tc, "file_info_get failed",
 
96
                       apr_file_info_get(&finfo, APR_FINFO_NORM, fh));
 
97
    
 
98
    ABTS_ASSERT(tc, "file_info_get gave incorrect size",
 
99
             finfo.size == eightGb);
 
100
 
 
101
    APR_ASSERT_SUCCESS(tc, "re-close large file", apr_file_close(fh));
 
102
}
 
103
 
 
104
static void test_stat(abts_case *tc, void *data)
 
105
{
 
106
    apr_finfo_t finfo;
 
107
 
 
108
    PRECOND;
 
109
 
 
110
    APR_ASSERT_SUCCESS(tc, "stat large file", 
 
111
                       apr_stat(&finfo, TESTFN, APR_FINFO_NORM, p));
 
112
    
 
113
    ABTS_ASSERT(tc, "stat gave incorrect size", finfo.size == eightGb);
 
114
}
 
115
 
 
116
static void test_readdir(abts_case *tc, void *data)
 
117
{
 
118
    apr_dir_t *dh;
 
119
    apr_status_t rv;
 
120
 
 
121
    PRECOND;
 
122
 
 
123
    APR_ASSERT_SUCCESS(tc, "open test directory", 
 
124
                       apr_dir_open(&dh, TESTDIR, p));
 
125
 
 
126
    do {
 
127
        apr_finfo_t finfo;
 
128
        
 
129
        rv = apr_dir_read(&finfo, APR_FINFO_NORM, dh);
 
130
        
 
131
        if (rv == APR_SUCCESS && strcmp(finfo.name, TESTFILE) == 0) {
 
132
            ABTS_ASSERT(tc, "apr_dir_read gave incorrect size for large file", 
 
133
                     finfo.size == eightGb);
 
134
        }
 
135
 
 
136
    } while (rv == APR_SUCCESS);
 
137
        
 
138
    if (!APR_STATUS_IS_ENOENT(rv)) {
 
139
        APR_ASSERT_SUCCESS(tc, "apr_dir_read failed", rv);
 
140
    }
 
141
    
 
142
    APR_ASSERT_SUCCESS(tc, "close test directory",
 
143
                       apr_dir_close(dh));
 
144
}
 
145
 
 
146
#define TESTSTR "Hello, world."
 
147
 
 
148
static void test_append(abts_case *tc, void *data)
 
149
{
 
150
    apr_file_t *fh;
 
151
    apr_finfo_t finfo;
 
152
    
 
153
    PRECOND;
 
154
 
 
155
    APR_ASSERT_SUCCESS(tc, "open 8Gb file for append",
 
156
                       apr_file_open(&fh, TESTFN, APR_WRITE | APR_APPEND, 
 
157
                                     APR_OS_DEFAULT, p));
 
158
 
 
159
    APR_ASSERT_SUCCESS(tc, "append to 8Gb file",
 
160
                       apr_file_write_full(fh, TESTSTR, strlen(TESTSTR), NULL));
 
161
 
 
162
    APR_ASSERT_SUCCESS(tc, "file_info_get failed",
 
163
                       apr_file_info_get(&finfo, APR_FINFO_NORM, fh));
 
164
    
 
165
    ABTS_ASSERT(tc, "file_info_get gave incorrect size",
 
166
             finfo.size == eightGb + strlen(TESTSTR));
 
167
 
 
168
    APR_ASSERT_SUCCESS(tc, "close 8Gb file", apr_file_close(fh));
 
169
}
 
170
 
 
171
static void test_seek(abts_case *tc, void *data)
 
172
{
 
173
    apr_file_t *fh;
 
174
    apr_off_t pos;
 
175
 
 
176
    PRECOND;
 
177
    
 
178
    APR_ASSERT_SUCCESS(tc, "open 8Gb file for writing",
 
179
                       apr_file_open(&fh, TESTFN, APR_WRITE, 
 
180
                                     APR_OS_DEFAULT, p));
 
181
 
 
182
    pos = 0;
 
183
    APR_ASSERT_SUCCESS(tc, "relative seek to end", 
 
184
                       apr_file_seek(fh, APR_END, &pos));
 
185
    ABTS_ASSERT(tc, "seek to END gave 8Gb", pos == eightGb);
 
186
    
 
187
    pos = eightGb;
 
188
    APR_ASSERT_SUCCESS(tc, "seek to 8Gb", apr_file_seek(fh, APR_SET, &pos));
 
189
    ABTS_ASSERT(tc, "seek gave 8Gb offset", pos == eightGb);
 
190
 
 
191
    pos = 0;
 
192
    APR_ASSERT_SUCCESS(tc, "relative seek to 0", apr_file_seek(fh, APR_CUR, &pos));
 
193
    ABTS_ASSERT(tc, "relative seek gave 8Gb offset", pos == eightGb);
 
194
 
 
195
    apr_file_close(fh);
 
196
}
 
197
 
 
198
static void test_write(abts_case *tc, void *data)
 
199
{
 
200
    apr_file_t *fh;
 
201
    apr_off_t pos = eightGb - 4;
 
202
 
 
203
    PRECOND;
 
204
 
 
205
    APR_ASSERT_SUCCESS(tc, "re-open 8Gb file",
 
206
                       apr_file_open(&fh, TESTFN, APR_WRITE, APR_OS_DEFAULT, p));
 
207
 
 
208
    APR_ASSERT_SUCCESS(tc, "seek to 8Gb - 4", 
 
209
                       apr_file_seek(fh, APR_SET, &pos));
 
210
    ABTS_ASSERT(tc, "seek gave 8Gb-4 offset", pos == eightGb - 4);
 
211
 
 
212
    APR_ASSERT_SUCCESS(tc, "write magic string to 8Gb-4",
 
213
                       apr_file_write_full(fh, "FISH", 4, NULL));
 
214
 
 
215
    APR_ASSERT_SUCCESS(tc, "close 8Gb file", apr_file_close(fh));
 
216
}
 
217
 
 
218
 
 
219
#if APR_HAS_MMAP
 
220
static void test_mmap(abts_case *tc, void *data)
 
221
{
 
222
    apr_mmap_t *map;
 
223
    apr_file_t *fh;
 
224
    apr_size_t len = 16384; /* hopefully a multiple of the page size */
 
225
    apr_off_t off = eightGb - len; 
 
226
    void *ptr;
 
227
 
 
228
    PRECOND;
 
229
 
 
230
    APR_ASSERT_SUCCESS(tc, "open 8gb file for mmap",
 
231
                       apr_file_open(&fh, TESTFN, APR_READ, APR_OS_DEFAULT, p));
 
232
    
 
233
    APR_ASSERT_SUCCESS(tc, "mmap 8Gb file",
 
234
                       apr_mmap_create(&map, fh, off, len, APR_MMAP_READ, p));
 
235
 
 
236
    APR_ASSERT_SUCCESS(tc, "close file", apr_file_close(fh));
 
237
 
 
238
    ABTS_ASSERT(tc, "mapped a 16K block", map->size == len);
 
239
    
 
240
    APR_ASSERT_SUCCESS(tc, "get pointer into mmaped region",
 
241
                       apr_mmap_offset(&ptr, map, len - 4));
 
242
    ABTS_ASSERT(tc, "pointer was not NULL", ptr != NULL);
 
243
 
 
244
    ABTS_ASSERT(tc, "found the magic string", memcmp(ptr, "FISH", 4) == 0);
 
245
 
 
246
    APR_ASSERT_SUCCESS(tc, "delete mmap handle", apr_mmap_delete(map));
 
247
}
 
248
#endif /* APR_HAS_MMAP */
 
249
 
 
250
static void test_format(abts_case *tc, void *data)
 
251
{
 
252
    apr_off_t off;
 
253
 
 
254
    PRECOND;
 
255
 
 
256
    off = apr_atoi64(apr_off_t_toa(p, eightGb));
 
257
 
 
258
    ABTS_ASSERT(tc, "apr_atoi64 parsed apr_off_t_toa result incorrectly",
 
259
             off == eightGb);
 
260
}
 
261
 
 
262
#else
 
263
static void test_nolfs(abts_case *tc, void *data)
 
264
{
 
265
    ABTS_NOT_IMPL(tc, "Large Files not supported");
 
266
}
 
267
#endif
 
268
 
 
269
abts_suite *testlfs(abts_suite *suite)
 
270
{
 
271
    suite = ADD_SUITE(suite)
 
272
 
 
273
#ifdef USE_LFS_TESTS
 
274
    abts_run_test(suite, test_open, NULL);
 
275
    abts_run_test(suite, test_reopen, NULL);
 
276
    abts_run_test(suite, test_stat, NULL);
 
277
    abts_run_test(suite, test_readdir, NULL);
 
278
    abts_run_test(suite, test_seek, NULL);
 
279
    abts_run_test(suite, test_append, NULL);
 
280
    abts_run_test(suite, test_write, NULL);
 
281
#if APR_HAS_MMAP
 
282
    abts_run_test(suite, test_mmap, NULL);
 
283
#endif
 
284
    abts_run_test(suite, test_format, NULL);
 
285
#else
 
286
    abts_run_test(suite, test_nolfs, NULL);
 
287
#endif
 
288
 
 
289
    return suite;
 
290
}
 
291