xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testenv.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 "apr_env.h"
18 #include "apr_errno.h"
19 #include "testutil.h"
20 
21 #define TEST_ENVVAR_NAME "apr_test_envvar"
22 #define TEST_ENVVAR2_NAME "apr_test_envvar2"
23 #define TEST_ENVVAR_VALUE "Just a value that we'll check"
24 
25 static int have_env_set;
26 static int have_env_get;
27 static int have_env_del;
28 
test_setenv(abts_case * tc,void * data)29 static void test_setenv(abts_case *tc, void *data)
30 {
31     apr_status_t rv;
32 
33     rv = apr_env_set(TEST_ENVVAR_NAME, TEST_ENVVAR_VALUE, p);
34     have_env_set = (rv != APR_ENOTIMPL);
35     if (!have_env_set) {
36         ABTS_NOT_IMPL(tc, "apr_env_set");
37     } else {
38         APR_ASSERT_SUCCESS(tc, "set environment variable", rv);
39     }
40 }
41 
test_getenv(abts_case * tc,void * data)42 static void test_getenv(abts_case *tc, void *data)
43 {
44     char *value;
45     apr_status_t rv;
46 
47     if (!have_env_set) {
48         ABTS_NOT_IMPL(tc, "apr_env_set (skip test for apr_env_get)");
49         return;
50     }
51 
52     rv = apr_env_get(&value, TEST_ENVVAR_NAME, p);
53     have_env_get = (rv != APR_ENOTIMPL);
54     if (!have_env_get) {
55         ABTS_NOT_IMPL(tc, "apr_env_get");
56         return;
57     }
58     APR_ASSERT_SUCCESS(tc, "get environment variable", rv);
59     ABTS_STR_EQUAL(tc, TEST_ENVVAR_VALUE, value);
60 }
61 
test_delenv(abts_case * tc,void * data)62 static void test_delenv(abts_case *tc, void *data)
63 {
64     char *value;
65     apr_status_t rv;
66 
67     if (!have_env_set) {
68         ABTS_NOT_IMPL(tc, "apr_env_set (skip test for apr_env_delete)");
69         return;
70     }
71 
72     rv = apr_env_delete(TEST_ENVVAR_NAME, p);
73     have_env_del = (rv != APR_ENOTIMPL);
74     if (!have_env_del) {
75         ABTS_NOT_IMPL(tc, "apr_env_delete");
76         return;
77     }
78     APR_ASSERT_SUCCESS(tc, "delete environment variable", rv);
79 
80     if (!have_env_get) {
81         ABTS_NOT_IMPL(tc, "apr_env_get (skip sanity check for apr_env_delete)");
82         return;
83     }
84     rv = apr_env_get(&value, TEST_ENVVAR_NAME, p);
85     ABTS_INT_EQUAL(tc, APR_ENOENT, rv);
86 }
87 
88 /** http://issues.apache.org/bugzilla/show_bug.cgi?id=40764 */
test_emptyenv(abts_case * tc,void * data)89 static void test_emptyenv(abts_case *tc, void *data)
90 {
91     char *value;
92     apr_status_t rv;
93 
94     if (!(have_env_set && have_env_get)) {
95         ABTS_NOT_IMPL(tc, "apr_env_set (skip test_emptyenv)");
96         return;
97     }
98     /** Set empty string and test that rv != ENOENT) */
99     rv = apr_env_set(TEST_ENVVAR_NAME, "", p);
100     APR_ASSERT_SUCCESS(tc, "set environment variable", rv);
101     rv = apr_env_get(&value, TEST_ENVVAR_NAME, p);
102     APR_ASSERT_SUCCESS(tc, "get environment variable", rv);
103     ABTS_STR_EQUAL(tc, "", value);
104 
105     if (!have_env_del) {
106         ABTS_NOT_IMPL(tc, "apr_env (skip recycle test_emptyenv)");
107         return;
108     }
109     /** Delete and retest */
110     rv = apr_env_delete(TEST_ENVVAR_NAME, p);
111     APR_ASSERT_SUCCESS(tc, "delete environment variable", rv);
112     rv = apr_env_get(&value, TEST_ENVVAR_NAME, p);
113     ABTS_INT_EQUAL(tc, APR_ENOENT, rv);
114 
115     /** Set second variable + test*/
116     rv = apr_env_set(TEST_ENVVAR2_NAME, TEST_ENVVAR_VALUE, p);
117     APR_ASSERT_SUCCESS(tc, "set second environment variable", rv);
118     rv = apr_env_get(&value, TEST_ENVVAR2_NAME, p);
119     APR_ASSERT_SUCCESS(tc, "get second environment variable", rv);
120     ABTS_STR_EQUAL(tc, TEST_ENVVAR_VALUE, value);
121 
122     /** Finally, test ENOENT (first variable) followed by second != ENOENT) */
123     rv = apr_env_get(&value, TEST_ENVVAR_NAME, p);
124     ABTS_INT_EQUAL(tc, APR_ENOENT, rv);
125     rv = apr_env_get(&value, TEST_ENVVAR2_NAME, p);
126     APR_ASSERT_SUCCESS(tc, "verify second environment variable", rv);
127     ABTS_STR_EQUAL(tc, TEST_ENVVAR_VALUE, value);
128 
129     /** Cleanup */
130     apr_env_delete(TEST_ENVVAR2_NAME, p);
131 }
132 
testenv(abts_suite * suite)133 abts_suite *testenv(abts_suite *suite)
134 {
135     suite = ADD_SUITE(suite)
136 
137     abts_run_test(suite, test_setenv, NULL);
138     abts_run_test(suite, test_getenv, NULL);
139     abts_run_test(suite, test_delenv, NULL);
140     abts_run_test(suite, test_emptyenv, NULL);
141 
142     return suite;
143 }
144 
145