bad_ssl_test.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <stdio.h>
  19. #include <string.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/grpc_security.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include <grpc/support/string_util.h>
  25. #include "src/core/lib/gpr/string.h"
  26. #include "src/core/lib/gprpp/host_port.h"
  27. #include "src/core/lib/gprpp/memory.h"
  28. #include "src/core/lib/security/security_connector/ssl_utils_config.h"
  29. #include "test/core/end2end/cq_verifier.h"
  30. #include "test/core/util/port.h"
  31. #include "test/core/util/subprocess.h"
  32. #include "test/core/util/test_config.h"
  33. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  34. static void run_test(const char* target, size_t nops) {
  35. grpc_channel_credentials* ssl_creds =
  36. grpc_ssl_credentials_create(nullptr, nullptr, nullptr, nullptr);
  37. grpc_channel* channel;
  38. grpc_call* c;
  39. grpc_metadata_array initial_metadata_recv;
  40. grpc_metadata_array trailing_metadata_recv;
  41. grpc_slice details;
  42. grpc_status_code status;
  43. grpc_call_error error;
  44. gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
  45. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  46. cq_verifier* cqv = cq_verifier_create(cq);
  47. grpc_op ops[6];
  48. grpc_op* op;
  49. grpc_arg ssl_name_override = {
  50. GRPC_ARG_STRING,
  51. const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
  52. {const_cast<char*>("foo.test.google.fr")}};
  53. grpc_channel_args args;
  54. args.num_args = 1;
  55. args.args = &ssl_name_override;
  56. grpc_metadata_array_init(&initial_metadata_recv);
  57. grpc_metadata_array_init(&trailing_metadata_recv);
  58. channel = grpc_channel_create(target, ssl_creds, &args);
  59. grpc_slice host = grpc_slice_from_static_string("foo.test.google.fr:1234");
  60. c = grpc_channel_create_call(channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
  61. grpc_slice_from_static_string("/foo"), &host,
  62. deadline, nullptr);
  63. memset(ops, 0, sizeof(ops));
  64. op = ops;
  65. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  66. op->data.send_initial_metadata.count = 0;
  67. op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY;
  68. op->reserved = nullptr;
  69. op++;
  70. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  71. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  72. op->data.recv_status_on_client.status = &status;
  73. op->data.recv_status_on_client.status_details = &details;
  74. op->flags = 0;
  75. op->reserved = nullptr;
  76. op++;
  77. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  78. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  79. op->flags = 0;
  80. op->reserved = nullptr;
  81. op++;
  82. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  83. op->flags = 0;
  84. op->reserved = nullptr;
  85. op++;
  86. error = grpc_call_start_batch(c, ops, nops, tag(1), nullptr);
  87. GPR_ASSERT(GRPC_CALL_OK == error);
  88. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  89. cq_verify(cqv);
  90. GPR_ASSERT(status != GRPC_STATUS_OK);
  91. grpc_call_unref(c);
  92. grpc_slice_unref(details);
  93. grpc_metadata_array_destroy(&initial_metadata_recv);
  94. grpc_metadata_array_destroy(&trailing_metadata_recv);
  95. grpc_channel_destroy(channel);
  96. grpc_completion_queue_destroy(cq);
  97. cq_verifier_destroy(cqv);
  98. grpc_channel_credentials_release(ssl_creds);
  99. }
  100. int main(int argc, char** argv) {
  101. char* me = argv[0];
  102. char* lslash = strrchr(me, '/');
  103. char* lunder = strrchr(me, '_');
  104. char* tmp;
  105. char root[1024];
  106. char test[64];
  107. int port = grpc_pick_unused_port_or_die();
  108. char* args[10];
  109. int status;
  110. size_t i;
  111. gpr_subprocess* svr;
  112. /* figure out where we are */
  113. if (lslash) {
  114. memcpy(root, me, static_cast<size_t>(lslash - me));
  115. root[lslash - me] = 0;
  116. } else {
  117. strcpy(root, ".");
  118. }
  119. if (argc == 2) {
  120. GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, argv[1]);
  121. }
  122. /* figure out our test name */
  123. tmp = lunder - 1;
  124. while (*tmp != '_') tmp--;
  125. tmp++;
  126. memcpy(test, tmp, static_cast<size_t>(lunder - tmp));
  127. /* start the server */
  128. gpr_asprintf(&args[0], "%s/bad_ssl_%s_server%s", root, test,
  129. gpr_subprocess_binary_extension());
  130. args[1] = const_cast<char*>("--bind");
  131. std::string joined = grpc_core::JoinHostPort("::", port);
  132. args[2] = const_cast<char*>(joined.c_str());
  133. svr = gpr_subprocess_create(4, const_cast<const char**>(args));
  134. gpr_free(args[0]);
  135. for (i = 3; i <= 4; i++) {
  136. grpc_init();
  137. run_test(args[2], i);
  138. grpc_shutdown();
  139. }
  140. gpr_subprocess_interrupt(svr);
  141. status = gpr_subprocess_join(svr);
  142. gpr_subprocess_destroy(svr);
  143. return status;
  144. }