blob: 3a2f60c3b7264a26bd876133966bb57f119aca97 [file] [log] [blame]
Josh Gao3fb517c2016-09-19 17:31:55 -07001/*
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
Elliott Hughes14d673e2019-07-30 13:51:03 -070023TEST(socket_spec, parse_tcp_socket_spec_just_port) {
Cody Schuffelen331a9082019-01-02 14:17:29 -080024 std::string hostname, error, serial;
Josh Gao3fb517c2016-09-19 17:31:55 -070025 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080026 EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070027 EXPECT_EQ("", hostname);
28 EXPECT_EQ(5037, port);
Cody Schuffelen331a9082019-01-02 14:17:29 -080029 EXPECT_EQ("", serial);
Elliott Hughes14d673e2019-07-30 13:51:03 -070030}
Josh Gao3fb517c2016-09-19 17:31:55 -070031
Elliott Hughes14d673e2019-07-30 13:51:03 -070032TEST(socket_spec, parse_tcp_socket_spec_bad_ports) {
33 std::string hostname, error, serial;
34 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080035 EXPECT_FALSE(parse_tcp_socket_spec("tcp:", &hostname, &port, &serial, &error));
36 EXPECT_FALSE(parse_tcp_socket_spec("tcp:-1", &hostname, &port, &serial, &error));
37 EXPECT_FALSE(parse_tcp_socket_spec("tcp:65536", &hostname, &port, &serial, &error));
Elliott Hughes14d673e2019-07-30 13:51:03 -070038}
Josh Gao3fb517c2016-09-19 17:31:55 -070039
Elliott Hughes14d673e2019-07-30 13:51:03 -070040TEST(socket_spec, parse_tcp_socket_spec_host_and_port) {
41 std::string hostname, error, serial;
42 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080043 EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070044 EXPECT_EQ("localhost", hostname);
45 EXPECT_EQ(1234, port);
Cody Schuffelen331a9082019-01-02 14:17:29 -080046 EXPECT_EQ("localhost:1234", serial);
Elliott Hughes14d673e2019-07-30 13:51:03 -070047}
Josh Gao3fb517c2016-09-19 17:31:55 -070048
Elliott Hughes14d673e2019-07-30 13:51:03 -070049TEST(socket_spec, parse_tcp_socket_spec_host_no_port) {
50 std::string hostname, error, serial;
51 int port;
52 EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost", &hostname, &port, &serial, &error));
53 EXPECT_EQ("localhost", hostname);
54 EXPECT_EQ(5555, port);
55 EXPECT_EQ("localhost:5555", serial);
56}
57
58TEST(socket_spec, parse_tcp_socket_spec_host_bad_ports) {
59 std::string hostname, error, serial;
60 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080061 EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:", &hostname, &port, &serial, &error));
62 EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:-1", &hostname, &port, &serial, &error));
63 EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:65536", &hostname, &port, &serial, &error));
Elliott Hughes14d673e2019-07-30 13:51:03 -070064}
Josh Gao3fb517c2016-09-19 17:31:55 -070065
Elliott Hughes14d673e2019-07-30 13:51:03 -070066TEST(socket_spec, parse_tcp_socket_spec_ipv6_and_port) {
67 std::string hostname, error, serial;
68 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080069 EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070070 EXPECT_EQ("::1", hostname);
71 EXPECT_EQ(1234, port);
Cody Schuffelen331a9082019-01-02 14:17:29 -080072 EXPECT_EQ("[::1]:1234", serial);
Elliott Hughes14d673e2019-07-30 13:51:03 -070073}
Josh Gao3fb517c2016-09-19 17:31:55 -070074
Elliott Hughes14d673e2019-07-30 13:51:03 -070075TEST(socket_spec, parse_tcp_socket_spec_ipv6_no_port) {
76 std::string hostname, error, serial;
77 int port;
78 EXPECT_TRUE(parse_tcp_socket_spec("tcp:::1", &hostname, &port, &serial, &error));
79 EXPECT_EQ("::1", hostname);
80 EXPECT_EQ(5555, port);
81 EXPECT_EQ("[::1]:5555", serial);
82}
83
84TEST(socket_spec, parse_tcp_socket_spec_ipv6_bad_ports) {
85 std::string hostname, error, serial;
86 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080087 EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]", &hostname, &port, &serial, &error));
88 EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:", &hostname, &port, &serial, &error));
89 EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:-1", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070090}