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
19 #include "src/core/lib/gprpp/host_port.h"
20
21 #include "gtest/gtest.h"
22
23 #include "test/core/util/test_config.h"
24
join_host_port_expect(const char * host,int port,const char * expected)25 static void join_host_port_expect(const char* host, int port,
26 const char* expected) {
27 std::string actual = grpc_core::JoinHostPort(host, port);
28 ASSERT_EQ(actual, expected);
29 }
30
TEST(HostPortTest,JoinHostPort)31 TEST(HostPortTest, JoinHostPort) {
32 join_host_port_expect("foo", 101, "foo:101");
33 join_host_port_expect("", 102, ":102");
34 join_host_port_expect("1::2", 103, "[1::2]:103");
35 join_host_port_expect("[::1]", 104, "[::1]:104");
36 }
37
38 // Garbage in, garbage out.
TEST(HostPortTest,JoinHostPortGarbage)39 TEST(HostPortTest, JoinHostPortGarbage) {
40 join_host_port_expect("[foo]", 105, "[foo]:105");
41 join_host_port_expect("[::", 106, "[:::106");
42 join_host_port_expect("::]", 107, "[::]]:107");
43 }
44
split_host_port_expect(const char * name,const char * host,const char * port,bool ret)45 static void split_host_port_expect(const char* name, const char* host,
46 const char* port, bool ret) {
47 std::string actual_host;
48 std::string actual_port;
49 const bool actual_ret =
50 grpc_core::SplitHostPort(name, &actual_host, &actual_port);
51 ASSERT_EQ(actual_ret, ret);
52 ASSERT_EQ(actual_host, (host == nullptr ? "" : host));
53 ASSERT_EQ(actual_port, (port == nullptr ? "" : port));
54 }
55
TEST(HostPortTest,SplitHostPort)56 TEST(HostPortTest, SplitHostPort) {
57 split_host_port_expect("", "", nullptr, true);
58 split_host_port_expect("[a:b]", "a:b", nullptr, true);
59 split_host_port_expect("1.2.3.4", "1.2.3.4", nullptr, true);
60 split_host_port_expect("0.0.0.0:", "0.0.0.0", "", true);
61 split_host_port_expect("a:b:c::", "a:b:c::", nullptr, true);
62 split_host_port_expect("[a:b:c::]:", "a:b:c::", "", true);
63 split_host_port_expect("[a:b]:30", "a:b", "30", true);
64 split_host_port_expect("1.2.3.4:30", "1.2.3.4", "30", true);
65 split_host_port_expect(":30", "", "30", true);
66 }
67
TEST(HostPortTest,SplitHostPortInvalid)68 TEST(HostPortTest, SplitHostPortInvalid) {
69 split_host_port_expect("[a:b", nullptr, nullptr, false);
70 split_host_port_expect("[a:b]30", nullptr, nullptr, false);
71 }
72
main(int argc,char ** argv)73 int main(int argc, char** argv) {
74 grpc::testing::TestEnvironment env(&argc, argv);
75 ::testing::InitGoogleTest(&argc, argv);
76 return RUN_ALL_TESTS();
77 }
78