secure_endpoint_test.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/security/transport/secure_endpoint.h"
  19. #include <fcntl.h>
  20. #include <sys/types.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include "src/core/lib/gpr/useful.h"
  25. #include "src/core/lib/iomgr/endpoint_pair.h"
  26. #include "src/core/lib/iomgr/iomgr.h"
  27. #include "src/core/lib/slice/slice_internal.h"
  28. #include "src/core/tsi/fake_transport_security.h"
  29. #include "test/core/iomgr/endpoint_tests.h"
  30. #include "test/core/util/test_config.h"
  31. static gpr_mu* g_mu;
  32. static grpc_pollset* g_pollset;
  33. static grpc_endpoint_test_fixture secure_endpoint_create_fixture_tcp_socketpair(
  34. size_t slice_size, grpc_slice* leftover_slices, size_t leftover_nslices,
  35. bool use_zero_copy_protector) {
  36. grpc_core::ExecCtx exec_ctx;
  37. tsi_frame_protector* fake_read_protector =
  38. tsi_create_fake_frame_protector(nullptr);
  39. tsi_frame_protector* fake_write_protector =
  40. tsi_create_fake_frame_protector(nullptr);
  41. tsi_zero_copy_grpc_protector* fake_read_zero_copy_protector =
  42. use_zero_copy_protector
  43. ? tsi_create_fake_zero_copy_grpc_protector(nullptr)
  44. : nullptr;
  45. tsi_zero_copy_grpc_protector* fake_write_zero_copy_protector =
  46. use_zero_copy_protector
  47. ? tsi_create_fake_zero_copy_grpc_protector(nullptr)
  48. : nullptr;
  49. grpc_endpoint_test_fixture f;
  50. grpc_endpoint_pair tcp;
  51. grpc_arg a[1];
  52. a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
  53. a[0].type = GRPC_ARG_INTEGER;
  54. a[0].value.integer = static_cast<int>(slice_size);
  55. grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
  56. tcp = grpc_iomgr_create_endpoint_pair("fixture", &args);
  57. grpc_endpoint_add_to_pollset(tcp.client, g_pollset);
  58. grpc_endpoint_add_to_pollset(tcp.server, g_pollset);
  59. if (leftover_nslices == 0) {
  60. f.client_ep = grpc_secure_endpoint_create(fake_read_protector,
  61. fake_read_zero_copy_protector,
  62. tcp.client, nullptr, 0);
  63. } else {
  64. unsigned i;
  65. tsi_result result;
  66. size_t still_pending_size;
  67. size_t total_buffer_size = 8192;
  68. size_t buffer_size = total_buffer_size;
  69. uint8_t* encrypted_buffer = static_cast<uint8_t*>(gpr_malloc(buffer_size));
  70. uint8_t* cur = encrypted_buffer;
  71. grpc_slice encrypted_leftover;
  72. for (i = 0; i < leftover_nslices; i++) {
  73. grpc_slice plain = leftover_slices[i];
  74. uint8_t* message_bytes = GRPC_SLICE_START_PTR(plain);
  75. size_t message_size = GRPC_SLICE_LENGTH(plain);
  76. while (message_size > 0) {
  77. size_t protected_buffer_size_to_send = buffer_size;
  78. size_t processed_message_size = message_size;
  79. result = tsi_frame_protector_protect(
  80. fake_write_protector, message_bytes, &processed_message_size, cur,
  81. &protected_buffer_size_to_send);
  82. GPR_ASSERT(result == TSI_OK);
  83. message_bytes += processed_message_size;
  84. message_size -= processed_message_size;
  85. cur += protected_buffer_size_to_send;
  86. GPR_ASSERT(buffer_size >= protected_buffer_size_to_send);
  87. buffer_size -= protected_buffer_size_to_send;
  88. }
  89. grpc_slice_unref(plain);
  90. }
  91. do {
  92. size_t protected_buffer_size_to_send = buffer_size;
  93. result = tsi_frame_protector_protect_flush(fake_write_protector, cur,
  94. &protected_buffer_size_to_send,
  95. &still_pending_size);
  96. GPR_ASSERT(result == TSI_OK);
  97. cur += protected_buffer_size_to_send;
  98. GPR_ASSERT(buffer_size >= protected_buffer_size_to_send);
  99. buffer_size -= protected_buffer_size_to_send;
  100. } while (still_pending_size > 0);
  101. encrypted_leftover = grpc_slice_from_copied_buffer(
  102. reinterpret_cast<const char*>(encrypted_buffer),
  103. total_buffer_size - buffer_size);
  104. f.client_ep = grpc_secure_endpoint_create(
  105. fake_read_protector, fake_read_zero_copy_protector, tcp.client,
  106. &encrypted_leftover, 1);
  107. grpc_slice_unref(encrypted_leftover);
  108. gpr_free(encrypted_buffer);
  109. }
  110. f.server_ep = grpc_secure_endpoint_create(fake_write_protector,
  111. fake_write_zero_copy_protector,
  112. tcp.server, nullptr, 0);
  113. return f;
  114. }
  115. static grpc_endpoint_test_fixture
  116. secure_endpoint_create_fixture_tcp_socketpair_noleftover(size_t slice_size) {
  117. return secure_endpoint_create_fixture_tcp_socketpair(slice_size, nullptr, 0,
  118. false);
  119. }
  120. static grpc_endpoint_test_fixture
  121. secure_endpoint_create_fixture_tcp_socketpair_noleftover_zero_copy(
  122. size_t slice_size) {
  123. return secure_endpoint_create_fixture_tcp_socketpair(slice_size, nullptr, 0,
  124. true);
  125. }
  126. static grpc_endpoint_test_fixture
  127. secure_endpoint_create_fixture_tcp_socketpair_leftover(size_t slice_size) {
  128. grpc_slice s =
  129. grpc_slice_from_copied_string("hello world 12345678900987654321");
  130. return secure_endpoint_create_fixture_tcp_socketpair(slice_size, &s, 1,
  131. false);
  132. }
  133. static grpc_endpoint_test_fixture
  134. secure_endpoint_create_fixture_tcp_socketpair_leftover_zero_copy(
  135. size_t slice_size) {
  136. grpc_slice s =
  137. grpc_slice_from_copied_string("hello world 12345678900987654321");
  138. return secure_endpoint_create_fixture_tcp_socketpair(slice_size, &s, 1, true);
  139. }
  140. static void clean_up(void) {}
  141. static grpc_endpoint_test_config configs[] = {
  142. {"secure_ep/tcp_socketpair",
  143. secure_endpoint_create_fixture_tcp_socketpair_noleftover, clean_up},
  144. {"secure_ep/tcp_socketpair_zero_copy",
  145. secure_endpoint_create_fixture_tcp_socketpair_noleftover_zero_copy,
  146. clean_up},
  147. {"secure_ep/tcp_socketpair_leftover",
  148. secure_endpoint_create_fixture_tcp_socketpair_leftover, clean_up},
  149. {"secure_ep/tcp_socketpair_leftover_zero_copy",
  150. secure_endpoint_create_fixture_tcp_socketpair_leftover_zero_copy,
  151. clean_up},
  152. };
  153. static void inc_call_ctr(void* arg, grpc_error_handle /*error*/) {
  154. ++*static_cast<int*>(arg);
  155. }
  156. static void test_leftover(grpc_endpoint_test_config config, size_t slice_size) {
  157. grpc_endpoint_test_fixture f = config.create_fixture(slice_size);
  158. grpc_slice_buffer incoming;
  159. grpc_slice s =
  160. grpc_slice_from_copied_string("hello world 12345678900987654321");
  161. grpc_core::ExecCtx exec_ctx;
  162. int n = 0;
  163. grpc_closure done_closure;
  164. gpr_log(GPR_INFO, "Start test left over");
  165. grpc_slice_buffer_init(&incoming);
  166. GRPC_CLOSURE_INIT(&done_closure, inc_call_ctr, &n, grpc_schedule_on_exec_ctx);
  167. grpc_endpoint_read(f.client_ep, &incoming, &done_closure, /*urgent=*/false);
  168. grpc_core::ExecCtx::Get()->Flush();
  169. GPR_ASSERT(n == 1);
  170. GPR_ASSERT(incoming.count == 1);
  171. GPR_ASSERT(grpc_slice_eq(s, incoming.slices[0]));
  172. grpc_endpoint_shutdown(
  173. f.client_ep, GRPC_ERROR_CREATE_FROM_STATIC_STRING("test_leftover end"));
  174. grpc_endpoint_shutdown(
  175. f.server_ep, GRPC_ERROR_CREATE_FROM_STATIC_STRING("test_leftover end"));
  176. grpc_endpoint_destroy(f.client_ep);
  177. grpc_endpoint_destroy(f.server_ep);
  178. grpc_slice_unref_internal(s);
  179. grpc_slice_buffer_destroy_internal(&incoming);
  180. clean_up();
  181. }
  182. static void destroy_pollset(void* p, grpc_error_handle /*error*/) {
  183. grpc_pollset_destroy(static_cast<grpc_pollset*>(p));
  184. }
  185. int main(int argc, char** argv) {
  186. grpc_closure destroyed;
  187. grpc::testing::TestEnvironment env(argc, argv);
  188. grpc_init();
  189. {
  190. grpc_core::ExecCtx exec_ctx;
  191. g_pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
  192. grpc_pollset_init(g_pollset, &g_mu);
  193. grpc_endpoint_tests(configs[0], g_pollset, g_mu);
  194. grpc_endpoint_tests(configs[1], g_pollset, g_mu);
  195. test_leftover(configs[2], 1);
  196. test_leftover(configs[3], 1);
  197. GRPC_CLOSURE_INIT(&destroyed, destroy_pollset, g_pollset,
  198. grpc_schedule_on_exec_ctx);
  199. grpc_pollset_shutdown(g_pollset, &destroyed);
  200. }
  201. grpc_shutdown();
  202. gpr_free(g_pollset);
  203. return 0;
  204. }