load_file_test.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/core/lib/iomgr/load_file.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/slice.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include "src/core/lib/gpr/string.h"
  26. #include "src/core/lib/gpr/tmpfile.h"
  27. #include "test/core/util/test_config.h"
  28. #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
  29. static const char prefix[] = "file_test";
  30. static void test_load_empty_file(void) {
  31. FILE* tmp = nullptr;
  32. grpc_slice slice;
  33. grpc_slice slice_with_null_term;
  34. grpc_error_handle error;
  35. char* tmp_name;
  36. LOG_TEST_NAME("test_load_empty_file");
  37. tmp = gpr_tmpfile(prefix, &tmp_name);
  38. GPR_ASSERT(tmp_name != nullptr);
  39. GPR_ASSERT(tmp != nullptr);
  40. fclose(tmp);
  41. error = grpc_load_file(tmp_name, 0, &slice);
  42. GPR_ASSERT(error == GRPC_ERROR_NONE);
  43. GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == 0);
  44. error = grpc_load_file(tmp_name, 1, &slice_with_null_term);
  45. GPR_ASSERT(error == GRPC_ERROR_NONE);
  46. GPR_ASSERT(GRPC_SLICE_LENGTH(slice_with_null_term) == 1);
  47. GPR_ASSERT(GRPC_SLICE_START_PTR(slice_with_null_term)[0] == 0);
  48. remove(tmp_name);
  49. gpr_free(tmp_name);
  50. grpc_slice_unref(slice);
  51. grpc_slice_unref(slice_with_null_term);
  52. }
  53. static void test_load_failure(void) {
  54. FILE* tmp = nullptr;
  55. grpc_slice slice;
  56. grpc_error_handle error;
  57. char* tmp_name;
  58. LOG_TEST_NAME("test_load_failure");
  59. tmp = gpr_tmpfile(prefix, &tmp_name);
  60. GPR_ASSERT(tmp_name != nullptr);
  61. GPR_ASSERT(tmp != nullptr);
  62. fclose(tmp);
  63. remove(tmp_name);
  64. error = grpc_load_file(tmp_name, 0, &slice);
  65. GPR_ASSERT(error != GRPC_ERROR_NONE);
  66. GRPC_ERROR_UNREF(error);
  67. GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == 0);
  68. gpr_free(tmp_name);
  69. grpc_slice_unref(slice);
  70. }
  71. static void test_load_small_file(void) {
  72. FILE* tmp = nullptr;
  73. grpc_slice slice;
  74. grpc_slice slice_with_null_term;
  75. grpc_error_handle error;
  76. char* tmp_name;
  77. const char* blah = "blah";
  78. LOG_TEST_NAME("test_load_small_file");
  79. tmp = gpr_tmpfile(prefix, &tmp_name);
  80. GPR_ASSERT(tmp_name != nullptr);
  81. GPR_ASSERT(tmp != nullptr);
  82. GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah));
  83. fclose(tmp);
  84. error = grpc_load_file(tmp_name, 0, &slice);
  85. GPR_ASSERT(error == GRPC_ERROR_NONE);
  86. GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == strlen(blah));
  87. GPR_ASSERT(!memcmp(GRPC_SLICE_START_PTR(slice), blah, strlen(blah)));
  88. error = grpc_load_file(tmp_name, 1, &slice_with_null_term);
  89. GPR_ASSERT(error == GRPC_ERROR_NONE);
  90. GPR_ASSERT(GRPC_SLICE_LENGTH(slice_with_null_term) == (strlen(blah) + 1));
  91. GPR_ASSERT(strcmp((const char*)GRPC_SLICE_START_PTR(slice_with_null_term),
  92. blah) == 0);
  93. remove(tmp_name);
  94. gpr_free(tmp_name);
  95. grpc_slice_unref(slice);
  96. grpc_slice_unref(slice_with_null_term);
  97. }
  98. static void test_load_big_file(void) {
  99. FILE* tmp = nullptr;
  100. grpc_slice slice;
  101. grpc_error_handle error;
  102. char* tmp_name;
  103. static const size_t buffer_size = 124631;
  104. unsigned char* buffer = static_cast<unsigned char*>(gpr_malloc(buffer_size));
  105. unsigned char* current;
  106. size_t i;
  107. LOG_TEST_NAME("test_load_big_file");
  108. memset(buffer, 42, buffer_size);
  109. tmp = gpr_tmpfile(prefix, &tmp_name);
  110. GPR_ASSERT(tmp != nullptr);
  111. GPR_ASSERT(tmp_name != nullptr);
  112. GPR_ASSERT(fwrite(buffer, 1, buffer_size, tmp) == buffer_size);
  113. fclose(tmp);
  114. error = grpc_load_file(tmp_name, 0, &slice);
  115. GPR_ASSERT(error == GRPC_ERROR_NONE);
  116. GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == buffer_size);
  117. current = GRPC_SLICE_START_PTR(slice);
  118. for (i = 0; i < buffer_size; i++) {
  119. GPR_ASSERT(current[i] == 42);
  120. }
  121. remove(tmp_name);
  122. gpr_free(tmp_name);
  123. grpc_slice_unref(slice);
  124. gpr_free(buffer);
  125. }
  126. int main(int argc, char** argv) {
  127. grpc::testing::TestEnvironment env(argc, argv);
  128. grpc_init();
  129. test_load_empty_file();
  130. test_load_failure();
  131. test_load_small_file();
  132. test_load_big_file();
  133. grpc_shutdown();
  134. return 0;
  135. }