httpcli_test_util.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // 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 <grpc/support/port_platform.h>
  17. #include "test/core/http/httpcli_test_util.h"
  18. #include <string.h>
  19. #include <tuple>
  20. #include <vector>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include <grpc/support/string_util.h>
  25. #include <grpc/support/sync.h>
  26. #include "src/core/lib/security/security_connector/ssl_utils_config.h"
  27. #include "test/core/util/port.h"
  28. #include "test/core/util/subprocess.h"
  29. #include "test/core/util/test_config.h"
  30. namespace grpc_core {
  31. namespace testing {
  32. HttpRequestTestServer StartHttpRequestTestServer(int argc, char** argv,
  33. bool use_ssl) {
  34. char* me = argv[0];
  35. char* lslash = strrchr(me, '/');
  36. std::vector<char*> args;
  37. int server_port = grpc_pick_unused_port_or_die();
  38. // figure out where we are
  39. char* root;
  40. if (lslash != nullptr) {
  41. // Hack for bazel target
  42. if (static_cast<unsigned>(lslash - me) >= (sizeof("http") - 1) &&
  43. strncmp(me + (lslash - me) - sizeof("http") + 1, "http",
  44. sizeof("http") - 1) == 0) {
  45. lslash = me + (lslash - me) - sizeof("http");
  46. }
  47. root = static_cast<char*>(
  48. gpr_malloc(static_cast<size_t>(lslash - me + sizeof("/../.."))));
  49. memcpy(root, me, static_cast<size_t>(lslash - me));
  50. memcpy(root + (lslash - me), "/../..", sizeof("/../.."));
  51. } else {
  52. root = gpr_strdup(".");
  53. }
  54. GPR_ASSERT(argc <= 2);
  55. if (argc == 2) {
  56. args.push_back(gpr_strdup(argv[1]));
  57. } else {
  58. char* python_wrapper_arg;
  59. char* test_server_arg;
  60. gpr_asprintf(&python_wrapper_arg, "%s/test/core/http/python_wrapper.sh",
  61. root);
  62. gpr_asprintf(&test_server_arg, "%s/test/core/http/test_server.py", root);
  63. args.push_back(python_wrapper_arg);
  64. args.push_back(test_server_arg);
  65. }
  66. // start the server
  67. args.push_back(gpr_strdup("--port"));
  68. char* server_port_str;
  69. gpr_asprintf(&server_port_str, "%d", server_port);
  70. args.push_back(server_port_str);
  71. if (use_ssl) {
  72. args.push_back(gpr_strdup("--ssl"));
  73. // Set the environment variable for the SSL certificate file
  74. char* pem_file;
  75. gpr_asprintf(&pem_file, "%s/src/core/tsi/test_creds/ca.pem", root);
  76. GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, pem_file);
  77. gpr_free(pem_file);
  78. }
  79. gpr_log(GPR_INFO, "starting HttpRequest test server subprocess:");
  80. for (size_t i = 0; i < args.size(); i++) {
  81. gpr_log(GPR_INFO, " HttpRequest test server subprocess argv[%ld]: %s", i,
  82. args[i]);
  83. }
  84. gpr_subprocess* server =
  85. gpr_subprocess_create(args.size(), const_cast<const char**>(args.data()));
  86. GPR_ASSERT(server);
  87. for (size_t i = 0; i < args.size(); i++) {
  88. gpr_free(args[i]);
  89. }
  90. gpr_free(root);
  91. gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  92. gpr_time_from_seconds(5, GPR_TIMESPAN)));
  93. return {server, server_port};
  94. }
  95. } // namespace testing
  96. } // namespace grpc_core