testfilecopy.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_file_io.h"
  18. #include "fspr_file_info.h"
  19. #include "fspr_errno.h"
  20. #include "fspr_pools.h"
  21. static void copy_helper(abts_case *tc, const char *from, const char * to,
  22. fspr_fileperms_t perms, int append, fspr_pool_t *p)
  23. {
  24. fspr_status_t rv;
  25. fspr_status_t dest_rv;
  26. fspr_finfo_t copy;
  27. fspr_finfo_t orig;
  28. fspr_finfo_t dest;
  29. dest_rv = fspr_stat(&dest, to, APR_FINFO_SIZE, p);
  30. if (!append) {
  31. rv = fspr_file_copy(from, to, perms, p);
  32. }
  33. else {
  34. rv = fspr_file_append(from, to, perms, p);
  35. }
  36. APR_ASSERT_SUCCESS(tc, "Error copying file", rv);
  37. rv = fspr_stat(&orig, from, APR_FINFO_SIZE, p);
  38. APR_ASSERT_SUCCESS(tc, "Couldn't stat original file", rv);
  39. rv = fspr_stat(&copy, to, APR_FINFO_SIZE, p);
  40. APR_ASSERT_SUCCESS(tc, "Couldn't stat copy file", rv);
  41. if (!append) {
  42. ABTS_ASSERT(tc, "File size differs", orig.size == copy.size);
  43. }
  44. else {
  45. ABTS_ASSERT(tc, "File size differs",
  46. ((dest_rv == APR_SUCCESS)
  47. ? dest.size : 0) + orig.size == copy.size);
  48. }
  49. }
  50. static void copy_short_file(abts_case *tc, void *data)
  51. {
  52. fspr_status_t rv;
  53. /* make absolutely sure that the dest file doesn't exist. */
  54. fspr_file_remove("data/file_copy.txt", p);
  55. copy_helper(tc, "data/file_datafile.txt", "data/file_copy.txt",
  56. APR_FILE_SOURCE_PERMS, 0, p);
  57. rv = fspr_file_remove("data/file_copy.txt", p);
  58. APR_ASSERT_SUCCESS(tc, "Couldn't remove copy file", rv);
  59. }
  60. static void copy_over_existing(abts_case *tc, void *data)
  61. {
  62. fspr_status_t rv;
  63. /* make absolutely sure that the dest file doesn't exist. */
  64. fspr_file_remove("data/file_copy.txt", p);
  65. /* This is a cheat. I don't want to create a new file, so I just copy
  66. * one file, then I copy another. If the second copy succeeds, then
  67. * this works.
  68. */
  69. copy_helper(tc, "data/file_datafile.txt", "data/file_copy.txt",
  70. APR_FILE_SOURCE_PERMS, 0, p);
  71. copy_helper(tc, "data/mmap_datafile.txt", "data/file_copy.txt",
  72. APR_FILE_SOURCE_PERMS, 0, p);
  73. rv = fspr_file_remove("data/file_copy.txt", p);
  74. APR_ASSERT_SUCCESS(tc, "Couldn't remove copy file", rv);
  75. }
  76. static void append_nonexist(abts_case *tc, void *data)
  77. {
  78. fspr_status_t rv;
  79. /* make absolutely sure that the dest file doesn't exist. */
  80. fspr_file_remove("data/file_copy.txt", p);
  81. copy_helper(tc, "data/file_datafile.txt", "data/file_copy.txt",
  82. APR_FILE_SOURCE_PERMS, 0, p);
  83. rv = fspr_file_remove("data/file_copy.txt", p);
  84. APR_ASSERT_SUCCESS(tc, "Couldn't remove copy file", rv);
  85. }
  86. static void append_exist(abts_case *tc, void *data)
  87. {
  88. fspr_status_t rv;
  89. /* make absolutely sure that the dest file doesn't exist. */
  90. fspr_file_remove("data/file_copy.txt", p);
  91. /* This is a cheat. I don't want to create a new file, so I just copy
  92. * one file, then I copy another. If the second copy succeeds, then
  93. * this works.
  94. */
  95. copy_helper(tc, "data/file_datafile.txt", "data/file_copy.txt",
  96. APR_FILE_SOURCE_PERMS, 0, p);
  97. copy_helper(tc, "data/mmap_datafile.txt", "data/file_copy.txt",
  98. APR_FILE_SOURCE_PERMS, 1, p);
  99. rv = fspr_file_remove("data/file_copy.txt", p);
  100. APR_ASSERT_SUCCESS(tc, "Couldn't remove copy file", rv);
  101. }
  102. abts_suite *testfilecopy(abts_suite *suite)
  103. {
  104. suite = ADD_SUITE(suite)
  105. abts_run_test(suite, copy_short_file, NULL);
  106. abts_run_test(suite, copy_over_existing, NULL);
  107. abts_run_test(suite, append_nonexist, NULL);
  108. abts_run_test(suite, append_exist, NULL);
  109. return suite;
  110. }