Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 23 | TEST(socket_spec, parse_tcp_socket_spec_just_port) { |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 24 | std::string hostname, error, serial; |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 25 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 26 | EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &serial, &error)); |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 27 | EXPECT_EQ("", hostname); |
| 28 | EXPECT_EQ(5037, port); |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 29 | EXPECT_EQ("", serial); |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 30 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 32 | TEST(socket_spec, parse_tcp_socket_spec_bad_ports) { |
| 33 | std::string hostname, error, serial; |
| 34 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 35 | 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 Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 38 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 40 | TEST(socket_spec, parse_tcp_socket_spec_host_and_port) { |
| 41 | std::string hostname, error, serial; |
| 42 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 43 | EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &serial, &error)); |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 44 | EXPECT_EQ("localhost", hostname); |
| 45 | EXPECT_EQ(1234, port); |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 46 | EXPECT_EQ("localhost:1234", serial); |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 47 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 49 | TEST(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 | |
| 58 | TEST(socket_spec, parse_tcp_socket_spec_host_bad_ports) { |
| 59 | std::string hostname, error, serial; |
| 60 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 61 | 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 Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 64 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 66 | TEST(socket_spec, parse_tcp_socket_spec_ipv6_and_port) { |
| 67 | std::string hostname, error, serial; |
| 68 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 69 | EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &serial, &error)); |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 70 | EXPECT_EQ("::1", hostname); |
| 71 | EXPECT_EQ(1234, port); |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 72 | EXPECT_EQ("[::1]:1234", serial); |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 73 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 74 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame^] | 75 | TEST(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 | |
| 84 | TEST(socket_spec, parse_tcp_socket_spec_ipv6_bad_ports) { |
| 85 | std::string hostname, error, serial; |
| 86 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 87 | 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 Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 90 | } |