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

« back to all changes in this revision

Viewing changes to srclib/apr/test/testpools.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 <string.h>
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
#if APR_HAVE_UNISTD_H
 
26
#include <unistd.h>
 
27
#endif
 
28
#include "testutil.h"
 
29
 
 
30
#define ALLOC_BYTES 1024
 
31
 
 
32
static apr_pool_t *pmain = NULL;
 
33
static apr_pool_t *pchild = NULL;
 
34
 
 
35
static void alloc_bytes(abts_case *tc, void *data)
 
36
{
 
37
    int i;
 
38
    char *alloc;
 
39
    
 
40
    alloc = apr_palloc(pmain, ALLOC_BYTES);
 
41
    ABTS_PTR_NOTNULL(tc, alloc);
 
42
 
 
43
    for (i=0;i<ALLOC_BYTES;i++) {
 
44
        char *ptr = alloc + i;
 
45
        *ptr = 0xa;
 
46
    }
 
47
    /* This is just added to get the positive.  If this test fails, the
 
48
     * suite will seg fault.
 
49
     */
 
50
    ABTS_TRUE(tc, 1);
 
51
}
 
52
 
 
53
static void calloc_bytes(abts_case *tc, void *data)
 
54
{
 
55
    int i;
 
56
    char *alloc;
 
57
    
 
58
    alloc = apr_pcalloc(pmain, ALLOC_BYTES);
 
59
    ABTS_PTR_NOTNULL(tc, alloc);
 
60
 
 
61
    for (i=0;i<ALLOC_BYTES;i++) {
 
62
        char *ptr = alloc + i;
 
63
        ABTS_TRUE(tc, *ptr == '\0');
 
64
    }
 
65
}
 
66
 
 
67
static void parent_pool(abts_case *tc, void *data)
 
68
{
 
69
    apr_status_t rv;
 
70
 
 
71
    rv = apr_pool_create(&pmain, NULL);
 
72
    ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
 
73
    ABTS_PTR_NOTNULL(tc, pmain);
 
74
}
 
75
 
 
76
static void child_pool(abts_case *tc, void *data)
 
77
{
 
78
    apr_status_t rv;
 
79
 
 
80
    rv = apr_pool_create(&pchild, pmain);
 
81
    ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
 
82
    ABTS_PTR_NOTNULL(tc, pchild);
 
83
}
 
84
 
 
85
static void test_ancestor(abts_case *tc, void *data)
 
86
{
 
87
    ABTS_INT_EQUAL(tc, 1, apr_pool_is_ancestor(pmain, pchild));
 
88
}
 
89
 
 
90
static void test_notancestor(abts_case *tc, void *data)
 
91
{
 
92
    ABTS_INT_EQUAL(tc, 0, apr_pool_is_ancestor(pchild, pmain));
 
93
}
 
94
 
 
95
static apr_status_t success_cleanup(void *data)
 
96
{
 
97
    return APR_SUCCESS;
 
98
}
 
99
 
 
100
static char *checker_data = "Hello, world.";
 
101
 
 
102
static apr_status_t checker_cleanup(void *data)
 
103
{
 
104
    return data == checker_data ? APR_SUCCESS : APR_EGENERAL;
 
105
}
 
106
 
 
107
static void test_cleanups(abts_case *tc, void *data)
 
108
{
 
109
    apr_status_t rv;
 
110
    int n;
 
111
 
 
112
    /* do this several times to test the cleanup freelist handling. */
 
113
    for (n = 0; n < 5; n++) {
 
114
        apr_pool_cleanup_register(pchild, NULL, success_cleanup,
 
115
                                  success_cleanup);
 
116
        apr_pool_cleanup_register(pchild, checker_data, checker_cleanup,
 
117
                                  success_cleanup);
 
118
        apr_pool_cleanup_register(pchild, NULL, checker_cleanup, 
 
119
                                  success_cleanup);
 
120
 
 
121
        rv = apr_pool_cleanup_run(p, NULL, success_cleanup);
 
122
        ABTS_ASSERT(tc, "nullop cleanup run OK", rv == APR_SUCCESS);
 
123
        rv = apr_pool_cleanup_run(p, checker_data, checker_cleanup);
 
124
        ABTS_ASSERT(tc, "cleanup passed correct data", rv == APR_SUCCESS);
 
125
        rv = apr_pool_cleanup_run(p, NULL, checker_cleanup);
 
126
        ABTS_ASSERT(tc, "cleanup passed correct data", rv == APR_EGENERAL);
 
127
 
 
128
        if (n == 2) {
 
129
            /* clear the pool to check that works */
 
130
            apr_pool_clear(pchild);
 
131
        }
 
132
 
 
133
        if (n % 2 == 0) {
 
134
            /* throw another random cleanup into the mix */
 
135
            apr_pool_cleanup_register(pchild, NULL,
 
136
                                      apr_pool_cleanup_null,
 
137
                                      apr_pool_cleanup_null);
 
138
        }
 
139
    }
 
140
}
 
141
 
 
142
abts_suite *testpool(abts_suite *suite)
 
143
{
 
144
    suite = ADD_SUITE(suite)
 
145
 
 
146
    abts_run_test(suite, parent_pool, NULL);
 
147
    abts_run_test(suite, child_pool, NULL);
 
148
    abts_run_test(suite, test_ancestor, NULL);
 
149
    abts_run_test(suite, test_notancestor, NULL);
 
150
    abts_run_test(suite, alloc_bytes, NULL);
 
151
    abts_run_test(suite, calloc_bytes, NULL);
 
152
    abts_run_test(suite, test_cleanups, NULL);
 
153
 
 
154
    return suite;
 
155
}
 
156