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 <string.h>
20
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23
24 #include "src/core/lib/gpr/host_port.h"
25 #include "test/core/util/test_config.h"
26
join_host_port_expect(const char * host,int port,const char * expected)27 static void join_host_port_expect(const char* host, int port,
28 const char* expected) {
29 char* buf;
30 int len;
31 len = gpr_join_host_port(&buf, host, port);
32 GPR_ASSERT(len >= 0);
33 GPR_ASSERT(strlen(expected) == (size_t)len);
34 GPR_ASSERT(strcmp(expected, buf) == 0);
35 gpr_free(buf);
36 }
37
test_join_host_port(void)38 static void test_join_host_port(void) {
39 join_host_port_expect("foo", 101, "foo:101");
40 join_host_port_expect("", 102, ":102");
41 join_host_port_expect("1::2", 103, "[1::2]:103");
42 join_host_port_expect("[::1]", 104, "[::1]:104");
43 }
44
45 /* Garbage in, garbage out. */
test_join_host_port_garbage(void)46 static void test_join_host_port_garbage(void) {
47 join_host_port_expect("[foo]", 105, "[foo]:105");
48 join_host_port_expect("[::", 106, "[:::106");
49 join_host_port_expect("::]", 107, "[::]]:107");
50 }
51
main(int argc,char ** argv)52 int main(int argc, char** argv) {
53 grpc_test_init(argc, argv);
54
55 test_join_host_port();
56 test_join_host_port_garbage();
57
58 return 0;
59 }
60