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 | |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <android-base/file.h> |
| 24 | #include <android-base/stringprintf.h> |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
| 26 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 27 | TEST(socket_spec, parse_tcp_socket_spec_just_port) { |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 28 | std::string hostname, error, serial; |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 29 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 30 | EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &serial, &error)); |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 31 | EXPECT_EQ("", hostname); |
| 32 | EXPECT_EQ(5037, port); |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 33 | EXPECT_EQ("", serial); |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 34 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 36 | TEST(socket_spec, parse_tcp_socket_spec_bad_ports) { |
| 37 | std::string hostname, error, serial; |
| 38 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 39 | EXPECT_FALSE(parse_tcp_socket_spec("tcp:", &hostname, &port, &serial, &error)); |
| 40 | EXPECT_FALSE(parse_tcp_socket_spec("tcp:-1", &hostname, &port, &serial, &error)); |
| 41 | EXPECT_FALSE(parse_tcp_socket_spec("tcp:65536", &hostname, &port, &serial, &error)); |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 42 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 44 | TEST(socket_spec, parse_tcp_socket_spec_host_and_port) { |
| 45 | std::string hostname, error, serial; |
| 46 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 47 | 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] | 48 | EXPECT_EQ("localhost", hostname); |
| 49 | EXPECT_EQ(1234, port); |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 50 | EXPECT_EQ("localhost:1234", serial); |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 51 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 53 | TEST(socket_spec, parse_tcp_socket_spec_host_no_port) { |
| 54 | std::string hostname, error, serial; |
| 55 | int port; |
| 56 | EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost", &hostname, &port, &serial, &error)); |
| 57 | EXPECT_EQ("localhost", hostname); |
| 58 | EXPECT_EQ(5555, port); |
| 59 | EXPECT_EQ("localhost:5555", serial); |
| 60 | } |
| 61 | |
| 62 | TEST(socket_spec, parse_tcp_socket_spec_host_bad_ports) { |
| 63 | std::string hostname, error, serial; |
| 64 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 65 | EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:", &hostname, &port, &serial, &error)); |
| 66 | EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:-1", &hostname, &port, &serial, &error)); |
| 67 | 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] | 68 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 69 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 70 | TEST(socket_spec, parse_tcp_socket_spec_ipv6_and_port) { |
| 71 | std::string hostname, error, serial; |
| 72 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 73 | 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] | 74 | EXPECT_EQ("::1", hostname); |
| 75 | EXPECT_EQ(1234, port); |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 76 | EXPECT_EQ("[::1]:1234", serial); |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 77 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 78 | |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 79 | TEST(socket_spec, parse_tcp_socket_spec_ipv6_no_port) { |
| 80 | std::string hostname, error, serial; |
| 81 | int port; |
| 82 | EXPECT_TRUE(parse_tcp_socket_spec("tcp:::1", &hostname, &port, &serial, &error)); |
| 83 | EXPECT_EQ("::1", hostname); |
| 84 | EXPECT_EQ(5555, port); |
| 85 | EXPECT_EQ("[::1]:5555", serial); |
| 86 | } |
| 87 | |
| 88 | TEST(socket_spec, parse_tcp_socket_spec_ipv6_bad_ports) { |
| 89 | std::string hostname, error, serial; |
| 90 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 91 | EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]", &hostname, &port, &serial, &error)); |
| 92 | EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:", &hostname, &port, &serial, &error)); |
| 93 | 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] | 94 | } |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 95 | |
| 96 | TEST(socket_spec, get_host_socket_spec_port) { |
| 97 | std::string error; |
| 98 | EXPECT_EQ(5555, get_host_socket_spec_port("tcp:5555", &error)); |
| 99 | EXPECT_EQ(5555, get_host_socket_spec_port("tcp:localhost:5555", &error)); |
| 100 | EXPECT_EQ(5555, get_host_socket_spec_port("tcp:[::1]:5555", &error)); |
| 101 | EXPECT_EQ(5555, get_host_socket_spec_port("vsock:5555", &error)); |
| 102 | } |
| 103 | |
| 104 | TEST(socket_spec, get_host_socket_spec_port_no_port) { |
| 105 | std::string error; |
| 106 | EXPECT_EQ(5555, get_host_socket_spec_port("tcp:localhost", &error)); |
| 107 | EXPECT_EQ(-1, get_host_socket_spec_port("vsock:localhost", &error)); |
| 108 | } |
| 109 | |
| 110 | TEST(socket_spec, get_host_socket_spec_port_bad_ports) { |
| 111 | std::string error; |
| 112 | EXPECT_EQ(-1, get_host_socket_spec_port("tcp:65536", &error)); |
| 113 | EXPECT_EQ(-1, get_host_socket_spec_port("tcp:-5", &error)); |
| 114 | EXPECT_EQ(-1, get_host_socket_spec_port("vsock:-5", &error)); |
| 115 | EXPECT_EQ(-1, get_host_socket_spec_port("vsock:5:5555", &error)); |
| 116 | } |
| 117 | |
| 118 | TEST(socket_spec, get_host_socket_spec_port_bad_string) { |
| 119 | std::string error; |
| 120 | EXPECT_EQ(-1, get_host_socket_spec_port("tcpz:5555", &error)); |
| 121 | EXPECT_EQ(-1, get_host_socket_spec_port("vsockz:5555", &error)); |
| 122 | EXPECT_EQ(-1, get_host_socket_spec_port("abcd:5555", &error)); |
| 123 | EXPECT_EQ(-1, get_host_socket_spec_port("abcd", &error)); |
| 124 | } |
| 125 | |
| 126 | TEST(socket_spec, socket_spec_listen_connect_tcp) { |
| 127 | std::string error, serial; |
| 128 | int port; |
| 129 | unique_fd server_fd, client_fd; |
| 130 | EXPECT_FALSE(socket_spec_connect(&client_fd, "tcp:localhost:7777", &port, &serial, &error)); |
| 131 | server_fd.reset(socket_spec_listen("tcp:7777", &error, &port)); |
| 132 | EXPECT_NE(server_fd.get(), -1); |
| 133 | EXPECT_TRUE(socket_spec_connect(&client_fd, "tcp:localhost:7777", &port, &serial, &error)); |
| 134 | EXPECT_NE(client_fd.get(), -1); |
| 135 | } |
| 136 | |
| 137 | TEST(socket_spec, socket_spec_listen_connect_localfilesystem) { |
| 138 | std::string error, serial; |
| 139 | int port; |
| 140 | unique_fd server_fd, client_fd; |
| 141 | TemporaryDir sock_dir; |
| 142 | |
| 143 | // Only run this test if the created directory is writable. |
| 144 | int result = access(sock_dir.path, W_OK); |
| 145 | if (result == 0) { |
| 146 | std::string sock_addr = |
| 147 | android::base::StringPrintf("localfilesystem:%s/af_unix_socket", sock_dir.path); |
| 148 | EXPECT_FALSE(socket_spec_connect(&client_fd, sock_addr, &port, &serial, &error)); |
| 149 | server_fd.reset(socket_spec_listen(sock_addr, &error, &port)); |
| 150 | EXPECT_NE(server_fd.get(), -1); |
| 151 | EXPECT_TRUE(socket_spec_connect(&client_fd, sock_addr, &port, &serial, &error)); |
| 152 | EXPECT_NE(client_fd.get(), -1); |
| 153 | } |
| 154 | } |