• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 
17 #include "socket_spec.h"
18 
19 #include <string>
20 
21 #include <gtest/gtest.h>
22 
TEST(socket_spec,parse_tcp_socket_spec)23 TEST(socket_spec, parse_tcp_socket_spec) {
24     std::string hostname, error;
25     int port;
26     EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &error));
27     EXPECT_EQ("", hostname);
28     EXPECT_EQ(5037, port);
29 
30     // Bad ports:
31     EXPECT_FALSE(parse_tcp_socket_spec("tcp:", &hostname, &port, &error));
32     EXPECT_FALSE(parse_tcp_socket_spec("tcp:-1", &hostname, &port, &error));
33     EXPECT_FALSE(parse_tcp_socket_spec("tcp:65536", &hostname, &port, &error));
34 
35     EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &error));
36     EXPECT_EQ("localhost", hostname);
37     EXPECT_EQ(1234, port);
38 
39     EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost", &hostname, &port, &error));
40     EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:", &hostname, &port, &error));
41     EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:-1", &hostname, &port, &error));
42     EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:65536", &hostname, &port, &error));
43 
44     // IPv6:
45     EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &error));
46     EXPECT_EQ("::1", hostname);
47     EXPECT_EQ(1234, port);
48 
49     EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]", &hostname, &port, &error));
50     EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:", &hostname, &port, &error));
51     EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:-1", &hostname, &port, &error));
52     EXPECT_FALSE(parse_tcp_socket_spec("tcp:::1", &hostname, &port, &error));
53     EXPECT_FALSE(parse_tcp_socket_spec("tcp:::1:1234", &hostname, &port, &error));
54 }
55