xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testud.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 #include <stdio.h>
18 #include <stdlib.h>
19 #include "apr_file_io.h"
20 #include "apr_errno.h"
21 #include "apr_general.h"
22 #include "apr_lib.h"
23 #include "apr_strings.h"
24 #include "testutil.h"
25 
26 static apr_pool_t *pool;
27 static char *testdata;
28 static int cleanup_called = 0;
29 
string_cleanup(void * data)30 static apr_status_t string_cleanup(void *data)
31 {
32     cleanup_called = 1;
33     return APR_SUCCESS;
34 }
35 
set_userdata(abts_case * tc,void * data)36 static void set_userdata(abts_case *tc, void *data)
37 {
38     apr_status_t rv;
39 
40     rv = apr_pool_userdata_set(testdata, "TEST", string_cleanup, pool);
41     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
42 }
43 
get_userdata(abts_case * tc,void * data)44 static void get_userdata(abts_case *tc, void *data)
45 {
46     apr_status_t rv;
47     void *retdata;
48 
49     rv = apr_pool_userdata_get(&retdata, "TEST", pool);
50     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
51     ABTS_STR_EQUAL(tc, testdata, retdata);
52 }
53 
get_nonexistkey(abts_case * tc,void * data)54 static void get_nonexistkey(abts_case *tc, void *data)
55 {
56     apr_status_t rv;
57     void *retdata;
58 
59     rv = apr_pool_userdata_get(&retdata, "DOESNTEXIST", pool);
60     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
61     ABTS_PTR_EQUAL(tc, NULL, retdata);
62 }
63 
post_pool_clear(abts_case * tc,void * data)64 static void post_pool_clear(abts_case *tc, void *data)
65 {
66     apr_status_t rv;
67     void *retdata;
68 
69     rv = apr_pool_userdata_get(&retdata, "DOESNTEXIST", pool);
70     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
71     ABTS_PTR_EQUAL(tc, NULL, retdata);
72 }
73 
testud(abts_suite * suite)74 abts_suite *testud(abts_suite *suite)
75 {
76     suite = ADD_SUITE(suite)
77 
78     apr_pool_create(&pool, p);
79     testdata = apr_pstrdup(pool, "This is a test\n");
80 
81     abts_run_test(suite, set_userdata, NULL);
82     abts_run_test(suite, get_userdata, NULL);
83     abts_run_test(suite, get_nonexistkey, NULL);
84 
85     apr_pool_clear(pool);
86 
87     abts_run_test(suite, post_pool_clear, NULL);
88 
89     return suite;
90 }
91 
92