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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "apr_errno.h"
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"
25 static int have_env_set;
26 static int have_env_get;
27 static int have_env_del;
29 static void test_setenv(CuTest *tc)
33 rv = apr_env_set(TEST_ENVVAR_NAME, TEST_ENVVAR_VALUE, p);
34 have_env_set = (rv != APR_ENOTIMPL);
36 CuNotImpl(tc, "apr_env_set");
39 apr_assert_success(tc, "set environment variable", rv);
42 static void test_getenv(CuTest *tc)
48 CuNotImpl(tc, "apr_env_set (skip test for apr_env_get)");
52 rv = apr_env_get(&value, TEST_ENVVAR_NAME, p);
53 have_env_get = (rv != APR_ENOTIMPL);
55 CuNotImpl(tc, "apr_env_get");
58 apr_assert_success(tc, "get environment variable", rv);
59 CuAssertStrEquals(tc, TEST_ENVVAR_VALUE, value);
62 static void test_delenv(CuTest *tc)
68 CuNotImpl(tc, "apr_env_set (skip test for apr_env_delete)");
72 rv = apr_env_delete(TEST_ENVVAR_NAME, p);
73 have_env_del = (rv != APR_ENOTIMPL);
75 CuNotImpl(tc, "apr_env_delete");
78 apr_assert_success(tc, "delete environment variable", rv);
81 CuNotImpl(tc, "apr_env_get (skip sanity check for apr_env_delete)");
84 rv = apr_env_get(&value, TEST_ENVVAR_NAME, p);
85 CuAssertIntEquals(tc, APR_ENOENT, rv);
88 /** http://issues.apache.org/bugzilla/show_bug.cgi?id=40764 */
89 static void test_emptyenv(CuTest *tc)
94 if (!(have_env_set && have_env_get)) {
95 CuNotImpl(tc, "apr_env_set (skip test_emptyenv)");
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 CuAssertStrEquals(tc, "", value);
106 CuNotImpl(tc, "apr_env_del (skip recycle test_emptyenv)");
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 CuAssertIntEquals(tc, APR_ENOENT, rv);
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 CuAssertStrEquals(tc, TEST_ENVVAR_VALUE, value);
122 /** Finally, test ENOENT (first variable) followed by second != ENOENT) */
123 rv = apr_env_get(&value, TEST_ENVVAR_NAME, p);
124 CuAssertIntEquals(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 CuAssertStrEquals(tc, TEST_ENVVAR_VALUE, value);
130 apr_env_delete(TEST_ENVVAR2_NAME, p);
133 CuSuite *testenv(void)
135 CuSuite *suite = CuSuiteNew("Environment");
137 SUITE_ADD_TEST(suite, test_setenv);
138 SUITE_ADD_TEST(suite, test_getenv);
139 SUITE_ADD_TEST(suite, test_delenv);
140 SUITE_ADD_TEST(suite, test_emptyenv);