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