testenv.c 4.5 KB

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