httpcli_test_util.cc
Go to the documentation of this file.
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 
18 
20 
21 #include <string.h>
22 
23 #include <tuple>
24 #include <vector>
25 
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
30 #include <grpc/support/sync.h>
31 
33 #include "test/core/util/port.h"
36 
37 namespace grpc_core {
38 namespace testing {
39 
41  bool use_ssl) {
42  char* me = argv[0];
43  char* lslash = strrchr(me, '/');
44  std::vector<char*> args;
46  // figure out where we are
47  char* root;
48  if (lslash != nullptr) {
49  // Hack for bazel target
50  if (static_cast<unsigned>(lslash - me) >= (sizeof("http") - 1) &&
51  strncmp(me + (lslash - me) - sizeof("http") + 1, "http",
52  sizeof("http") - 1) == 0) {
53  lslash = me + (lslash - me) - sizeof("http");
54  }
55  root = static_cast<char*>(
56  gpr_malloc(static_cast<size_t>(lslash - me + sizeof("/../.."))));
57  memcpy(root, me, static_cast<size_t>(lslash - me));
58  memcpy(root + (lslash - me), "/../..", sizeof("/../.."));
59  } else {
60  root = gpr_strdup(".");
61  }
62  GPR_ASSERT(argc <= 2);
63  if (argc == 2) {
64  args.push_back(gpr_strdup(argv[1]));
65  } else {
66  char* python_wrapper_arg;
67  char* test_server_arg;
68  gpr_asprintf(&python_wrapper_arg, "%s/test/core/http/python_wrapper.sh",
69  root);
70  gpr_asprintf(&test_server_arg, "%s/test/core/http/test_server.py", root);
71  args.push_back(python_wrapper_arg);
72  args.push_back(test_server_arg);
73  }
74  // start the server
75  args.push_back(gpr_strdup("--port"));
76  char* server_port_str;
77  gpr_asprintf(&server_port_str, "%d", server_port);
78  args.push_back(server_port_str);
79  if (use_ssl) {
80  args.push_back(gpr_strdup("--ssl"));
81  // Set the environment variable for the SSL certificate file
82  char* pem_file;
83  gpr_asprintf(&pem_file, "%s/src/core/tsi/test_creds/ca.pem", root);
84  GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, pem_file);
85  gpr_free(pem_file);
86  }
87  gpr_log(GPR_INFO, "starting HttpRequest test server subprocess:");
88  for (size_t i = 0; i < args.size(); i++) {
89  gpr_log(GPR_INFO, " HttpRequest test server subprocess argv[%ld]: %s", i,
90  args[i]);
91  }
93  gpr_subprocess_create(args.size(), const_cast<const char**>(args.data()));
95  for (size_t i = 0; i < args.size(); i++) {
96  gpr_free(args[i]);
97  }
98  gpr_free(root);
101  return {server, server_port};
102 }
103 
104 } // namespace testing
105 } // namespace grpc_core
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
log.h
port.h
grpc_core
Definition: call_metric_recorder.h:31
string.h
server_port
static int server_port
Definition: bad_server_response_test.cc:86
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
string_util.h
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
httpcli_test_util.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
root
RefCountedPtr< grpc_tls_certificate_provider > root
Definition: xds_server_config_fetcher.cc:223
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
grpc.h
gpr_asprintf
GPRAPI int gpr_asprintf(char **strp, const char *format,...) GPR_PRINT_FORMAT_CHECK(2
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
ssl_utils_config.h
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
GPR_GLOBAL_CONFIG_SET
#define GPR_GLOBAL_CONFIG_SET(name, value)
Definition: global_config_generic.h:26
gpr_subprocess
struct gpr_subprocess gpr_subprocess
Definition: test/core/util/subprocess.h:24
subprocess.h
test_config.h
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
server
Definition: examples/python/async_streaming/server.py:1
grpc_core::testing::HttpRequestTestServer
Definition: httpcli_test_util.h:27
gpr_subprocess_create
gpr_subprocess * gpr_subprocess_create(int argc, const char **argv)
alloc.h
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
grpc_core::testing::StartHttpRequestTestServer
HttpRequestTestServer StartHttpRequestTestServer(int argc, char **argv, bool use_ssl)
Definition: httpcli_test_util.cc:40
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
sync.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
gpr_time_from_seconds
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:123
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:02