blob: 93fc191bcafc49b8eb570559632060c965427b50 [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
Jason Jeremy Iman84613872019-07-19 12:44:39 +090021#include <unistd.h>
22
23#include <android-base/file.h>
24#include <android-base/stringprintf.h>
Josh Gao3fb517c2016-09-19 17:31:55 -070025#include <gtest/gtest.h>
26
Elliott Hughes35be7f92020-05-12 16:36:04 -070027TEST(socket_spec, parse_tcp_socket_spec_failure) {
28 std::string hostname, error, serial;
29 int port;
30 EXPECT_FALSE(parse_tcp_socket_spec("sneakernet:5037", &hostname, &port, &serial, &error));
31 EXPECT_TRUE(error.find("sneakernet") != std::string::npos);
32}
33
Elliott Hughes14d673e2019-07-30 13:51:03 -070034TEST(socket_spec, parse_tcp_socket_spec_just_port) {
Cody Schuffelen331a9082019-01-02 14:17:29 -080035 std::string hostname, error, serial;
Josh Gao3fb517c2016-09-19 17:31:55 -070036 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080037 EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070038 EXPECT_EQ("", hostname);
39 EXPECT_EQ(5037, port);
Cody Schuffelen331a9082019-01-02 14:17:29 -080040 EXPECT_EQ("", serial);
Elliott Hughes14d673e2019-07-30 13:51:03 -070041}
Josh Gao3fb517c2016-09-19 17:31:55 -070042
Elliott Hughes14d673e2019-07-30 13:51:03 -070043TEST(socket_spec, parse_tcp_socket_spec_bad_ports) {
44 std::string hostname, error, serial;
45 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080046 EXPECT_FALSE(parse_tcp_socket_spec("tcp:", &hostname, &port, &serial, &error));
47 EXPECT_FALSE(parse_tcp_socket_spec("tcp:-1", &hostname, &port, &serial, &error));
48 EXPECT_FALSE(parse_tcp_socket_spec("tcp:65536", &hostname, &port, &serial, &error));
Elliott Hughes14d673e2019-07-30 13:51:03 -070049}
Josh Gao3fb517c2016-09-19 17:31:55 -070050
Elliott Hughes14d673e2019-07-30 13:51:03 -070051TEST(socket_spec, parse_tcp_socket_spec_host_and_port) {
52 std::string hostname, error, serial;
53 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080054 EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070055 EXPECT_EQ("localhost", hostname);
56 EXPECT_EQ(1234, port);
Cody Schuffelen331a9082019-01-02 14:17:29 -080057 EXPECT_EQ("localhost:1234", serial);
Elliott Hughes14d673e2019-07-30 13:51:03 -070058}
Josh Gao3fb517c2016-09-19 17:31:55 -070059
Elliott Hughes14d673e2019-07-30 13:51:03 -070060TEST(socket_spec, parse_tcp_socket_spec_host_no_port) {
61 std::string hostname, error, serial;
62 int port;
63 EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost", &hostname, &port, &serial, &error));
64 EXPECT_EQ("localhost", hostname);
65 EXPECT_EQ(5555, port);
66 EXPECT_EQ("localhost:5555", serial);
67}
68
69TEST(socket_spec, parse_tcp_socket_spec_host_bad_ports) {
70 std::string hostname, error, serial;
71 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080072 EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:", &hostname, &port, &serial, &error));
73 EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:-1", &hostname, &port, &serial, &error));
74 EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:65536", &hostname, &port, &serial, &error));
Elliott Hughes14d673e2019-07-30 13:51:03 -070075}
Josh Gao3fb517c2016-09-19 17:31:55 -070076
Elliott Hughes14d673e2019-07-30 13:51:03 -070077TEST(socket_spec, parse_tcp_socket_spec_ipv6_and_port) {
78 std::string hostname, error, serial;
79 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080080 EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070081 EXPECT_EQ("::1", hostname);
82 EXPECT_EQ(1234, port);
Cody Schuffelen331a9082019-01-02 14:17:29 -080083 EXPECT_EQ("[::1]:1234", serial);
Elliott Hughes14d673e2019-07-30 13:51:03 -070084}
Josh Gao3fb517c2016-09-19 17:31:55 -070085
Elliott Hughes14d673e2019-07-30 13:51:03 -070086TEST(socket_spec, parse_tcp_socket_spec_ipv6_no_port) {
87 std::string hostname, error, serial;
88 int port;
89 EXPECT_TRUE(parse_tcp_socket_spec("tcp:::1", &hostname, &port, &serial, &error));
90 EXPECT_EQ("::1", hostname);
91 EXPECT_EQ(5555, port);
92 EXPECT_EQ("[::1]:5555", serial);
93}
94
95TEST(socket_spec, parse_tcp_socket_spec_ipv6_bad_ports) {
96 std::string hostname, error, serial;
97 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080098 EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]", &hostname, &port, &serial, &error));
99 EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:", &hostname, &port, &serial, &error));
100 EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:-1", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -0700101}
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900102
103TEST(socket_spec, get_host_socket_spec_port) {
104 std::string error;
105 EXPECT_EQ(5555, get_host_socket_spec_port("tcp:5555", &error));
106 EXPECT_EQ(5555, get_host_socket_spec_port("tcp:localhost:5555", &error));
107 EXPECT_EQ(5555, get_host_socket_spec_port("tcp:[::1]:5555", &error));
108 EXPECT_EQ(5555, get_host_socket_spec_port("vsock:5555", &error));
109}
110
111TEST(socket_spec, get_host_socket_spec_port_no_port) {
112 std::string error;
113 EXPECT_EQ(5555, get_host_socket_spec_port("tcp:localhost", &error));
114 EXPECT_EQ(-1, get_host_socket_spec_port("vsock:localhost", &error));
115}
116
117TEST(socket_spec, get_host_socket_spec_port_bad_ports) {
118 std::string error;
119 EXPECT_EQ(-1, get_host_socket_spec_port("tcp:65536", &error));
120 EXPECT_EQ(-1, get_host_socket_spec_port("tcp:-5", &error));
121 EXPECT_EQ(-1, get_host_socket_spec_port("vsock:-5", &error));
122 EXPECT_EQ(-1, get_host_socket_spec_port("vsock:5:5555", &error));
123}
124
125TEST(socket_spec, get_host_socket_spec_port_bad_string) {
126 std::string error;
127 EXPECT_EQ(-1, get_host_socket_spec_port("tcpz:5555", &error));
128 EXPECT_EQ(-1, get_host_socket_spec_port("vsockz:5555", &error));
129 EXPECT_EQ(-1, get_host_socket_spec_port("abcd:5555", &error));
130 EXPECT_EQ(-1, get_host_socket_spec_port("abcd", &error));
131}
132
133TEST(socket_spec, socket_spec_listen_connect_tcp) {
134 std::string error, serial;
135 int port;
136 unique_fd server_fd, client_fd;
137 EXPECT_FALSE(socket_spec_connect(&client_fd, "tcp:localhost:7777", &port, &serial, &error));
138 server_fd.reset(socket_spec_listen("tcp:7777", &error, &port));
139 EXPECT_NE(server_fd.get(), -1);
140 EXPECT_TRUE(socket_spec_connect(&client_fd, "tcp:localhost:7777", &port, &serial, &error));
141 EXPECT_NE(client_fd.get(), -1);
142}
143
Elliott Hughes35be7f92020-05-12 16:36:04 -0700144TEST(socket_spec, socket_spec_connect_failure) {
145 std::string error, serial;
146 int port;
147 unique_fd client_fd;
148 EXPECT_FALSE(socket_spec_connect(&client_fd, "tcp:", &port, &serial, &error));
149 EXPECT_FALSE(socket_spec_connect(&client_fd, "acceptfd:", &port, &serial, &error));
150 EXPECT_FALSE(socket_spec_connect(&client_fd, "vsock:", &port, &serial, &error));
151 EXPECT_FALSE(socket_spec_connect(&client_fd, "vsock:x", &port, &serial, &error));
152 EXPECT_FALSE(socket_spec_connect(&client_fd, "vsock:5", &port, &serial, &error));
153 EXPECT_FALSE(socket_spec_connect(&client_fd, "vsock:5:x", &port, &serial, &error));
154 EXPECT_FALSE(socket_spec_connect(&client_fd, "sneakernet:", &port, &serial, &error));
155}
156
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900157TEST(socket_spec, socket_spec_listen_connect_localfilesystem) {
158 std::string error, serial;
159 int port;
160 unique_fd server_fd, client_fd;
161 TemporaryDir sock_dir;
162
163 // Only run this test if the created directory is writable.
164 int result = access(sock_dir.path, W_OK);
165 if (result == 0) {
166 std::string sock_addr =
167 android::base::StringPrintf("localfilesystem:%s/af_unix_socket", sock_dir.path);
168 EXPECT_FALSE(socket_spec_connect(&client_fd, sock_addr, &port, &serial, &error));
169 server_fd.reset(socket_spec_listen(sock_addr, &error, &port));
170 EXPECT_NE(server_fd.get(), -1);
171 EXPECT_TRUE(socket_spec_connect(&client_fd, sock_addr, &port, &serial, &error));
172 EXPECT_NE(client_fd.get(), -1);
173 }
174}
Elliott Hughes35be7f92020-05-12 16:36:04 -0700175
176TEST(socket_spec, is_socket_spec) {
177 EXPECT_TRUE(is_socket_spec("tcp:blah"));
178 EXPECT_TRUE(is_socket_spec("acceptfd:blah"));
179 EXPECT_TRUE(is_socket_spec("local:blah"));
180 EXPECT_TRUE(is_socket_spec("localreserved:blah"));
Andrew Walbranebf09dd2021-03-03 18:06:12 +0000181 EXPECT_TRUE(is_socket_spec("vsock:123:456"));
Elliott Hughes35be7f92020-05-12 16:36:04 -0700182}
183
184TEST(socket_spec, is_local_socket_spec) {
185 EXPECT_TRUE(is_local_socket_spec("local:blah"));
186 EXPECT_TRUE(is_local_socket_spec("tcp:localhost"));
187 EXPECT_FALSE(is_local_socket_spec("tcp:www.google.com"));
188}