2
0

testpools.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_general.h"
  17. #include "fspr_pools.h"
  18. #include "fspr_errno.h"
  19. #include "fspr_file_io.h"
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #if APR_HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include "testutil.h"
  27. #define ALLOC_BYTES 1024
  28. static fspr_pool_t *pmain = NULL;
  29. static fspr_pool_t *pchild = NULL;
  30. static void alloc_bytes(abts_case *tc, void *data)
  31. {
  32. int i;
  33. char *alloc;
  34. alloc = fspr_palloc(pmain, ALLOC_BYTES);
  35. ABTS_PTR_NOTNULL(tc, alloc);
  36. for (i=0;i<ALLOC_BYTES;i++) {
  37. char *ptr = alloc + i;
  38. *ptr = 0xa;
  39. }
  40. /* This is just added to get the positive. If this test fails, the
  41. * suite will seg fault.
  42. */
  43. ABTS_TRUE(tc, 1);
  44. }
  45. static void calloc_bytes(abts_case *tc, void *data)
  46. {
  47. int i;
  48. char *alloc;
  49. alloc = fspr_pcalloc(pmain, ALLOC_BYTES);
  50. ABTS_PTR_NOTNULL(tc, alloc);
  51. for (i=0;i<ALLOC_BYTES;i++) {
  52. char *ptr = alloc + i;
  53. ABTS_TRUE(tc, *ptr == '\0');
  54. }
  55. }
  56. static void parent_pool(abts_case *tc, void *data)
  57. {
  58. fspr_status_t rv;
  59. rv = fspr_pool_create(&pmain, NULL);
  60. ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
  61. ABTS_PTR_NOTNULL(tc, pmain);
  62. }
  63. static void child_pool(abts_case *tc, void *data)
  64. {
  65. fspr_status_t rv;
  66. rv = fspr_pool_create(&pchild, pmain);
  67. ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
  68. ABTS_PTR_NOTNULL(tc, pchild);
  69. }
  70. static void test_ancestor(abts_case *tc, void *data)
  71. {
  72. ABTS_INT_EQUAL(tc, 1, fspr_pool_is_ancestor(pmain, pchild));
  73. }
  74. static void test_notancestor(abts_case *tc, void *data)
  75. {
  76. ABTS_INT_EQUAL(tc, 0, fspr_pool_is_ancestor(pchild, pmain));
  77. }
  78. static fspr_status_t success_cleanup(void *data)
  79. {
  80. return APR_SUCCESS;
  81. }
  82. static char *checker_data = "Hello, world.";
  83. static fspr_status_t checker_cleanup(void *data)
  84. {
  85. return data == checker_data ? APR_SUCCESS : APR_EGENERAL;
  86. }
  87. static void test_cleanups(abts_case *tc, void *data)
  88. {
  89. fspr_status_t rv;
  90. int n;
  91. /* do this several times to test the cleanup freelist handling. */
  92. for (n = 0; n < 5; n++) {
  93. fspr_pool_cleanup_register(pchild, NULL, success_cleanup,
  94. success_cleanup);
  95. fspr_pool_cleanup_register(pchild, checker_data, checker_cleanup,
  96. success_cleanup);
  97. fspr_pool_cleanup_register(pchild, NULL, checker_cleanup,
  98. success_cleanup);
  99. rv = fspr_pool_cleanup_run(p, NULL, success_cleanup);
  100. ABTS_ASSERT(tc, "nullop cleanup run OK", rv == APR_SUCCESS);
  101. rv = fspr_pool_cleanup_run(p, checker_data, checker_cleanup);
  102. ABTS_ASSERT(tc, "cleanup passed correct data", rv == APR_SUCCESS);
  103. rv = fspr_pool_cleanup_run(p, NULL, checker_cleanup);
  104. ABTS_ASSERT(tc, "cleanup passed correct data", rv == APR_EGENERAL);
  105. if (n == 2) {
  106. /* clear the pool to check that works */
  107. fspr_pool_clear(pchild);
  108. }
  109. if (n % 2 == 0) {
  110. /* throw another random cleanup into the mix */
  111. fspr_pool_cleanup_register(pchild, NULL,
  112. fspr_pool_cleanup_null,
  113. fspr_pool_cleanup_null);
  114. }
  115. }
  116. }
  117. abts_suite *testpool(abts_suite *suite)
  118. {
  119. suite = ADD_SUITE(suite)
  120. abts_run_test(suite, parent_pool, NULL);
  121. abts_run_test(suite, child_pool, NULL);
  122. abts_run_test(suite, test_ancestor, NULL);
  123. abts_run_test(suite, test_notancestor, NULL);
  124. abts_run_test(suite, alloc_bytes, NULL);
  125. abts_run_test(suite, calloc_bytes, NULL);
  126. abts_run_test(suite, test_cleanups, NULL);
  127. return suite;
  128. }