xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testpools.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 
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 
alloc_bytes(abts_case * tc,void * data)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 
calloc_bytes(abts_case * tc,void * data)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 
parent_pool(abts_case * tc,void * data)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, APR_SUCCESS, rv);
73     ABTS_PTR_NOTNULL(tc, pmain);
74 }
75 
child_pool(abts_case * tc,void * data)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, APR_SUCCESS, rv);
82     ABTS_PTR_NOTNULL(tc, pchild);
83 }
84 
test_ancestor(abts_case * tc,void * data)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 
test_notancestor(abts_case * tc,void * data)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 
success_cleanup(void * data)95 static apr_status_t success_cleanup(void *data)
96 {
97     return APR_SUCCESS;
98 }
99 
100 static char *checker_data = "Hello, world.";
101 
checker_cleanup(void * data)102 static apr_status_t checker_cleanup(void *data)
103 {
104     return data == checker_data ? APR_SUCCESS : APR_EGENERAL;
105 }
106 
test_cleanups(abts_case * tc,void * data)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 
testpool(abts_suite * suite)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 
157