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 "test/core/util/http_client/httpcli_test_util.h"
18
19 #include <grpc/support/alloc.h>
20 #include <grpc/support/port_platform.h>
21 #include <grpc/support/string_util.h>
22 #include <grpc/support/time.h>
23 #include <string.h>
24
25 #include <algorithm>
26 #include <string>
27 #include <vector>
28
29 #include "absl/log/check.h"
30 #include "absl/log/log.h"
31 #include "absl/strings/match.h"
32 #include "absl/strings/str_cat.h"
33 #include "absl/strings/string_view.h"
34 #include "absl/types/optional.h"
35 #include "src/core/config/config_vars.h"
36 #include "src/core/util/subprocess.h"
37 #include "test/core/test_util/port.h"
38
39 namespace grpc_core {
40 namespace testing {
41
StartHttpRequestTestServer(int argc,char ** argv,bool use_ssl)42 HttpRequestTestServer StartHttpRequestTestServer(int argc, char** argv,
43 bool use_ssl) {
44 int server_port = grpc_pick_unused_port_or_die();
45 // Find root path. The logic is different for bazel vs. cmake.
46 std::string root;
47 absl::string_view me(argv[0]);
48 size_t last_slash = me.rfind('/');
49 if (last_slash != me.npos) {
50 absl::string_view dirname = me.substr(0, last_slash);
51 if (absl::EndsWith(dirname, "/http_client")) {
52 // Bazel paths will end in "test/core/util/http_client".
53 root = absl::StrCat(dirname, "/../../../..");
54 } else {
55 // Cmake paths will be "cmake/build".
56 root = absl::StrCat(dirname, "/../..");
57 }
58 } else {
59 root = ".";
60 }
61 // Construct args.
62 std::vector<const char*> args;
63 std::string python_wrapper_path;
64 std::string test_server_path;
65 CHECK_LE(argc, 2);
66 if (argc == 2) {
67 args.push_back(argv[1]);
68 } else {
69 python_wrapper_path =
70 absl::StrCat(root, "/test/core/util/http_client/python_wrapper.sh");
71 test_server_path =
72 absl::StrCat(root, "/test/core/util/http_client/test_server.py");
73 args.push_back(python_wrapper_path.c_str());
74 args.push_back(test_server_path.c_str());
75 }
76 args.push_back("--port");
77 std::string port_number = absl::StrCat(server_port);
78 args.push_back(port_number.c_str());
79 if (use_ssl) {
80 args.push_back("--ssl");
81 // Set the environment variable for the SSL certificate file.
82 ConfigVars::Overrides overrides;
83 overrides.default_ssl_roots_file_path =
84 absl::StrCat(root, "/src/core/tsi/test_creds/ca.pem");
85 ConfigVars::SetOverrides(overrides);
86 }
87 LOG(INFO) << "starting HttpRequest test server subprocess:";
88 for (size_t i = 0; i < args.size(); i++) {
89 LOG(INFO) << " HttpRequest test server subprocess argv[" << i
90 << "]: " << args[i];
91 }
92 // Start the server.
93 gpr_subprocess* server = gpr_subprocess_create(args.size(), args.data());
94 CHECK(server);
95 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
96 gpr_time_from_seconds(5, GPR_TIMESPAN)));
97 return {server, server_port};
98 }
99
100 } // namespace testing
101 } // namespace grpc_core
102