2
0

testmmap.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "testutil.h"
  17. #include "fspr_mmap.h"
  18. #include "fspr_errno.h"
  19. #include "fspr_general.h"
  20. #include "fspr_lib.h"
  21. #include "fspr_file_io.h"
  22. #include "fspr_strings.h"
  23. /* hmmm, what is a truly portable define for the max path
  24. * length on a platform?
  25. */
  26. #define PATH_LEN 255
  27. #define TEST_STRING "This is the MMAP data file."APR_EOL_STR
  28. #if !APR_HAS_MMAP
  29. static void not_implemented(abts_case *tc, void *data)
  30. {
  31. ABTS_NOT_IMPL(tc, "User functions");
  32. }
  33. #else
  34. static fspr_mmap_t *themmap = NULL;
  35. static fspr_file_t *thefile = NULL;
  36. static char *file1;
  37. static fspr_finfo_t finfo;
  38. static int fsize;
  39. static void create_filename(abts_case *tc, void *data)
  40. {
  41. char *oldfileptr;
  42. fspr_filepath_get(&file1, 0, p);
  43. #ifndef NETWARE
  44. #ifdef WIN32
  45. ABTS_TRUE(tc, file1[1] == ':');
  46. #else
  47. ABTS_TRUE(tc, file1[0] == '/');
  48. #endif
  49. #endif
  50. ABTS_TRUE(tc, file1[strlen(file1) - 1] != '/');
  51. oldfileptr = file1;
  52. file1 = fspr_pstrcat(p, file1,"/data/mmap_datafile.txt" ,NULL);
  53. ABTS_TRUE(tc, oldfileptr != file1);
  54. }
  55. static void test_file_close(abts_case *tc, void *data)
  56. {
  57. fspr_status_t rv;
  58. rv = fspr_file_close(thefile);
  59. ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
  60. }
  61. static void test_file_open(abts_case *tc, void *data)
  62. {
  63. fspr_status_t rv;
  64. rv = fspr_file_open(&thefile, file1, APR_READ, APR_UREAD | APR_GREAD, p);
  65. ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
  66. ABTS_PTR_NOTNULL(tc, thefile);
  67. }
  68. static void test_get_filesize(abts_case *tc, void *data)
  69. {
  70. fspr_status_t rv;
  71. rv = fspr_file_info_get(&finfo, APR_FINFO_NORM, thefile);
  72. ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
  73. ABTS_ASSERT(tc, "File size mismatch", fsize == finfo.size);
  74. }
  75. static void test_mmap_create(abts_case *tc, void *data)
  76. {
  77. fspr_status_t rv;
  78. rv = fspr_mmap_create(&themmap, thefile, 0, (fspr_size_t) finfo.size,
  79. APR_MMAP_READ, p);
  80. ABTS_PTR_NOTNULL(tc, themmap);
  81. ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
  82. }
  83. static void test_mmap_contents(abts_case *tc, void *data)
  84. {
  85. ABTS_PTR_NOTNULL(tc, themmap);
  86. ABTS_PTR_NOTNULL(tc, themmap->mm);
  87. ABTS_INT_EQUAL(tc, fsize, themmap->size);
  88. /* Must use nEquals since the string is not guaranteed to be NULL terminated */
  89. ABTS_STR_NEQUAL(tc, themmap->mm, TEST_STRING, fsize);
  90. }
  91. static void test_mmap_delete(abts_case *tc, void *data)
  92. {
  93. fspr_status_t rv;
  94. ABTS_PTR_NOTNULL(tc, themmap);
  95. rv = fspr_mmap_delete(themmap);
  96. ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
  97. }
  98. static void test_mmap_offset(abts_case *tc, void *data)
  99. {
  100. fspr_status_t rv;
  101. void *addr;
  102. ABTS_PTR_NOTNULL(tc, themmap);
  103. rv = fspr_mmap_offset(&addr, themmap, 5);
  104. /* Must use nEquals since the string is not guaranteed to be NULL terminated */
  105. ABTS_STR_NEQUAL(tc, addr, TEST_STRING + 5, fsize-5);
  106. }
  107. #endif
  108. abts_suite *testmmap(abts_suite *suite)
  109. {
  110. suite = ADD_SUITE(suite)
  111. #if APR_HAS_MMAP
  112. fsize = strlen(TEST_STRING);
  113. abts_run_test(suite, create_filename, NULL);
  114. abts_run_test(suite, test_file_open, NULL);
  115. abts_run_test(suite, test_get_filesize, NULL);
  116. abts_run_test(suite, test_mmap_create, NULL);
  117. abts_run_test(suite, test_mmap_contents, NULL);
  118. abts_run_test(suite, test_mmap_offset, NULL);
  119. abts_run_test(suite, test_mmap_delete, NULL);
  120. abts_run_test(suite, test_file_close, NULL);
  121. #else
  122. abts_run_test(suite, not_implemented, NULL);
  123. #endif
  124. return suite;
  125. }