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