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