host_port_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <string.h>
22 
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 
27 
28 static void join_host_port_expect(const char* host, int port,
29  const char* expected) {
31  GPR_ASSERT(actual == expected);
32 }
33 
34 static void test_join_host_port(void) {
35  join_host_port_expect("foo", 101, "foo:101");
36  join_host_port_expect("", 102, ":102");
37  join_host_port_expect("1::2", 103, "[1::2]:103");
38  join_host_port_expect("[::1]", 104, "[::1]:104");
39 }
40 
41 /* Garbage in, garbage out. */
42 static void test_join_host_port_garbage(void) {
43  join_host_port_expect("[foo]", 105, "[foo]:105");
44  join_host_port_expect("[::", 106, "[:::106");
45  join_host_port_expect("::]", 107, "[::]]:107");
46 }
47 
48 static void split_host_port_expect(const char* name, const char* host,
49  const char* port, bool ret) {
50  std::string actual_host;
51  std::string actual_port;
52  const bool actual_ret =
53  grpc_core::SplitHostPort(name, &actual_host, &actual_port);
54  GPR_ASSERT(actual_ret == ret);
55  GPR_ASSERT(actual_host == (host == nullptr ? "" : host));
56  GPR_ASSERT(actual_port == (port == nullptr ? "" : port));
57 }
58 
59 static void test_split_host_port() {
60  split_host_port_expect("", "", nullptr, true);
61  split_host_port_expect("[a:b]", "a:b", nullptr, true);
62  split_host_port_expect("1.2.3.4", "1.2.3.4", nullptr, true);
63  split_host_port_expect("0.0.0.0:", "0.0.0.0", "", true);
64  split_host_port_expect("a:b:c::", "a:b:c::", nullptr, true);
65  split_host_port_expect("[a:b:c::]:", "a:b:c::", "", true);
66  split_host_port_expect("[a:b]:30", "a:b", "30", true);
67  split_host_port_expect("1.2.3.4:30", "1.2.3.4", "30", true);
68  split_host_port_expect(":30", "", "30", true);
69 }
70 
72  split_host_port_expect("[a:b", nullptr, nullptr, false);
73  split_host_port_expect("[a:b]30", nullptr, nullptr, false);
74 }
75 
76 int main(int argc, char** argv) {
78 
83 
84  return 0;
85 }
test_split_host_port
static void test_split_host_port()
Definition: host_port_test.cc:59
log.h
test_join_host_port_garbage
static void test_join_host_port_garbage(void)
Definition: host_port_test.cc:42
generate.env
env
Definition: generate.py:37
test_split_host_port_invalid
static void test_split_host_port_invalid()
Definition: host_port_test.cc:71
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::SplitHostPort
bool SplitHostPort(absl::string_view name, absl::string_view *host, absl::string_view *port)
Definition: host_port.cc:88
setup.name
name
Definition: setup.py:542
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::JoinHostPort
std::string JoinHostPort(absl::string_view host, int port)
Definition: host_port.cc:32
host_port.h
split_host_port_expect
static void split_host_port_expect(const char *name, const char *host, const char *port, bool ret)
Definition: host_port_test.cc:48
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
join_host_port_expect
static void join_host_port_expect(const char *host, int port, const char *expected)
Definition: host_port_test.cc:28
test_join_host_port
static void test_join_host_port(void)
Definition: host_port_test.cc:34
main
int main(int argc, char **argv)
Definition: host_port_test.cc:76


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