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 <unistd.h>
22 
23 #include <android-base/file.h>
24 #include <android-base/stringprintf.h>
25 #include <gtest/gtest.h>
26 
27 // If the socket spec is incorrectly specified (i.e w/o a "tcp:" prefix),
28 // check for the contents of the returned error string.
TEST(socket_spec,parse_tcp_socket_spec_failure_error_check)29 TEST(socket_spec, parse_tcp_socket_spec_failure_error_check) {
30     std::string hostname, error, serial;
31     int port;
32 
33     // spec needs to be prefixed with "tcp:"
34     const std::string spec("sneakernet:5037");
35     EXPECT_FALSE(parse_tcp_socket_spec(spec, &hostname, &port, &serial, &error));
36     EXPECT_TRUE(error.find("sneakernet") != std::string::npos);
37     EXPECT_EQ(error, "specification is not tcp: " + spec);
38 }
39 
TEST(socket_spec,parse_tcp_socket_spec_just_port_success)40 TEST(socket_spec, parse_tcp_socket_spec_just_port_success) {
41     std::string hostname, error, serial;
42     int port;
43     EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &serial, &error));
44     EXPECT_EQ("", hostname);
45     EXPECT_EQ(5037, port);
46     EXPECT_EQ("", serial);
47 }
48 
TEST(socket_spec,parse_tcp_socket_spec_bad_ports_failure)49 TEST(socket_spec, parse_tcp_socket_spec_bad_ports_failure) {
50     std::string hostname, error, serial;
51     int port;
52     EXPECT_FALSE(parse_tcp_socket_spec("tcp:", &hostname, &port, &serial, &error));
53     EXPECT_FALSE(parse_tcp_socket_spec("tcp:-1", &hostname, &port, &serial, &error));
54     EXPECT_FALSE(parse_tcp_socket_spec("tcp:65536", &hostname, &port, &serial, &error));
55 }
56 
TEST(socket_spec,parse_tcp_socket_spec_host_and_port_success)57 TEST(socket_spec, parse_tcp_socket_spec_host_and_port_success) {
58     std::string hostname, error, serial;
59     int port;
60     EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &serial, &error));
61     EXPECT_EQ("localhost", hostname);
62     EXPECT_EQ(1234, port);
63     EXPECT_EQ("localhost:1234", serial);
64 }
65 
TEST(socket_spec,parse_tcp_socket_spec_host_no_port_success)66 TEST(socket_spec, parse_tcp_socket_spec_host_no_port_success) {
67     std::string hostname, error, serial;
68     int port;
69     EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost", &hostname, &port, &serial, &error));
70     EXPECT_EQ("localhost", hostname);
71     EXPECT_EQ(5555, port);
72     EXPECT_EQ("localhost:5555", serial);
73 }
74 
TEST(socket_spec,parse_tcp_socket_spec_host_ipv4_no_port_success)75 TEST(socket_spec, parse_tcp_socket_spec_host_ipv4_no_port_success) {
76     std::string hostname, error, serial;
77     int port;
78     EXPECT_TRUE(parse_tcp_socket_spec("tcp:127.0.0.1", &hostname, &port, &serial, &error));
79     EXPECT_EQ("127.0.0.1", hostname);
80     EXPECT_EQ(5555, port);
81     EXPECT_EQ("127.0.0.1:5555", serial);
82 }
83 
TEST(socket_spec,parse_tcp_socket_spec_host_bad_ports_failure)84 TEST(socket_spec, parse_tcp_socket_spec_host_bad_ports_failure) {
85     std::string hostname, error, serial;
86     int port;
87     EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:", &hostname, &port, &serial, &error));
88     EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:-1", &hostname, &port, &serial, &error));
89     EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:65536", &hostname, &port, &serial, &error));
90 }
91 
TEST(socket_spec,parse_tcp_socket_spec_host_ipv4_bad_ports_failure)92 TEST(socket_spec, parse_tcp_socket_spec_host_ipv4_bad_ports_failure) {
93     std::string hostname, error, serial;
94     int port;
95     EXPECT_FALSE(parse_tcp_socket_spec("tcp:127.0.0.1:", &hostname, &port, &serial, &error));
96     EXPECT_FALSE(parse_tcp_socket_spec("tcp:127.0.0.1:-1", &hostname, &port, &serial, &error));
97     EXPECT_FALSE(parse_tcp_socket_spec("tcp:127.0.0.1:65536", &hostname, &port, &serial, &error));
98 }
99 
TEST(socket_spec,parse_tcp_socket_spec_host_ipv6_bad_ports_failure)100 TEST(socket_spec, parse_tcp_socket_spec_host_ipv6_bad_ports_failure) {
101     std::string hostname, error, serial;
102     int port;
103     EXPECT_FALSE(parse_tcp_socket_spec("tcp:2601:644:8e80:620:c63:50c9:8a91:8efa:", &hostname,
104                                        &port, &serial, &error));
105     EXPECT_FALSE(parse_tcp_socket_spec("tcp:2601:644:8e80:620:c63:50c9:8a91:8efa:-1", &hostname,
106                                        &port, &serial, &error));
107     EXPECT_FALSE(parse_tcp_socket_spec("tcp:2601:644:8e80:620:c63:50c9:8a91:8efa:65536", &hostname,
108                                        &port, &serial, &error));
109 }
110 
TEST(socket_spec,parse_tcp_socket_spec_ipv6_and_port_success)111 TEST(socket_spec, parse_tcp_socket_spec_ipv6_and_port_success) {
112     std::string hostname, error, serial;
113     int port;
114     EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &serial, &error));
115     EXPECT_EQ("::1", hostname);
116     EXPECT_EQ(1234, port);
117     EXPECT_EQ("[::1]:1234", serial);
118 
119     // Repeat with different format of ipv6
120     EXPECT_TRUE(parse_tcp_socket_spec("tcp:[2601:644:8e80:620::fbbc]:2345", &hostname, &port,
121                                       &serial, &error));
122     EXPECT_EQ("2601:644:8e80:620::fbbc", hostname);
123     EXPECT_EQ(2345, port);
124     EXPECT_EQ("[2601:644:8e80:620::fbbc]:2345", serial);
125 }
126 
TEST(socket_spec,parse_tcp_socket_spec_ipv6_no_port_success)127 TEST(socket_spec, parse_tcp_socket_spec_ipv6_no_port_success) {
128     std::string hostname, error, serial;
129     int port;
130     EXPECT_TRUE(parse_tcp_socket_spec("tcp:::1", &hostname, &port, &serial, &error));
131     EXPECT_EQ("::1", hostname);
132     EXPECT_EQ(5555, port);
133     EXPECT_EQ("[::1]:5555", serial);
134 
135     // Repeat with other supported formats of ipv6.
136     EXPECT_TRUE(parse_tcp_socket_spec("tcp:2601:644:8e80:620::fbbc", &hostname, &port, &serial,
137                                       &error));
138     EXPECT_EQ("2601:644:8e80:620::fbbc", hostname);
139     EXPECT_EQ(5555, port);
140     EXPECT_EQ("[2601:644:8e80:620::fbbc]:5555", serial);
141 
142     EXPECT_TRUE(parse_tcp_socket_spec("tcp:2601:644:8e80:620:c63:50c9:8a91:8efa", &hostname, &port,
143                                       &serial, &error));
144     EXPECT_EQ("2601:644:8e80:620:c63:50c9:8a91:8efa", hostname);
145     EXPECT_EQ(5555, port);
146     EXPECT_EQ("[2601:644:8e80:620:c63:50c9:8a91:8efa]:5555", serial);
147 
148     EXPECT_TRUE(parse_tcp_socket_spec("tcp:2601:644:8e80:620:2d0e:b944:5288:97df", &hostname, &port,
149                                       &serial, &error));
150     EXPECT_EQ("2601:644:8e80:620:2d0e:b944:5288:97df", hostname);
151     EXPECT_EQ(5555, port);
152     EXPECT_EQ("[2601:644:8e80:620:2d0e:b944:5288:97df]:5555", serial);
153 }
154 
TEST(socket_spec,parse_tcp_socket_spec_ipv6_bad_ports_failure)155 TEST(socket_spec, parse_tcp_socket_spec_ipv6_bad_ports_failure) {
156     std::string hostname, error, serial;
157     int port;
158     EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]", &hostname, &port, &serial, &error));
159     EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:", &hostname, &port, &serial, &error));
160     EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:-1", &hostname, &port, &serial, &error));
161 
162     EXPECT_TRUE(parse_tcp_socket_spec("tcp:2601:644:8e80:620:2d0e:b944:5288:97df", &hostname, &port,
163                                       &serial, &error));
164     EXPECT_FALSE(parse_tcp_socket_spec("tcp:2601:644:8e80:620:2d0e:b944:5288:97df:", &hostname,
165                                        &port, &serial, &error));
166     EXPECT_FALSE(parse_tcp_socket_spec("tcp:2601:644:8e80:620:2d0e:b944:5288:97df:-1", &hostname,
167                                        &port, &serial, &error));
168 }
169 
TEST(socket_spec,get_host_socket_spec_port_success)170 TEST(socket_spec, get_host_socket_spec_port_success) {
171     std::string error;
172     EXPECT_EQ(5555, get_host_socket_spec_port("tcp:5555", &error));
173     EXPECT_EQ(5555, get_host_socket_spec_port("tcp:localhost:5555", &error));
174     EXPECT_EQ(5555, get_host_socket_spec_port("tcp:[::1]:5555", &error));
175 }
176 
TEST(socket_spec,get_host_socket_spec_port_vsock_success)177 TEST(socket_spec, get_host_socket_spec_port_vsock_success) {
178     std::string error;
179 #ifdef __linux__  // vsock is only supported on linux
180     EXPECT_EQ(5555, get_host_socket_spec_port("vsock:5555", &error));
181 #else
182     GTEST_SKIP() << "vsock is only supported on linux";
183 #endif
184 }
185 
TEST(socket_spec,get_host_socket_spec_port_no_port)186 TEST(socket_spec, get_host_socket_spec_port_no_port) {
187     std::string error;
188     EXPECT_EQ(5555, get_host_socket_spec_port("tcp:localhost", &error));
189     EXPECT_EQ(-1, get_host_socket_spec_port("vsock:localhost", &error));
190 }
191 
TEST(socket_spec,get_host_socket_spec_port_bad_ports)192 TEST(socket_spec, get_host_socket_spec_port_bad_ports) {
193     std::string error;
194     EXPECT_EQ(-1, get_host_socket_spec_port("tcp:65536", &error));
195     EXPECT_EQ(-1, get_host_socket_spec_port("tcp:-5", &error));
196 
197     // The following two expectations happen to fail on non-linux anyway(for
198     // different reasons than "vsock is only supported on linux").
199     EXPECT_EQ(-1, get_host_socket_spec_port("vsock:-5", &error));
200     EXPECT_EQ(-1, get_host_socket_spec_port("vsock:5:5555", &error));
201 }
202 
TEST(socket_spec,get_host_socket_spec_port_bad_string)203 TEST(socket_spec, get_host_socket_spec_port_bad_string) {
204     std::string error;
205     EXPECT_EQ(-1, get_host_socket_spec_port("tcpz:5555", &error));
206     EXPECT_EQ(-1, get_host_socket_spec_port("vsockz:5555", &error));
207     EXPECT_EQ(-1, get_host_socket_spec_port("abcd:5555", &error));
208     EXPECT_EQ(-1, get_host_socket_spec_port("abcd", &error));
209 }
210 
TEST(socket_spec,socket_spec_listen_connect_tcp)211 TEST(socket_spec, socket_spec_listen_connect_tcp) {
212     std::string error, serial;
213     int port;
214     unique_fd server_fd, client_fd;
215     EXPECT_FALSE(socket_spec_connect(&client_fd, "tcp:localhost:7777", &port, &serial, &error));
216     server_fd.reset(socket_spec_listen("tcp:7777", &error, &port));
217     EXPECT_NE(server_fd.get(), -1);
218     EXPECT_TRUE(socket_spec_connect(&client_fd, "tcp:localhost:7777", &port, &serial, &error));
219     EXPECT_NE(client_fd.get(), -1);
220 }
221 
TEST(socket_spec,socket_spec_connect_failure)222 TEST(socket_spec, socket_spec_connect_failure) {
223     std::string error, serial;
224     int port;
225     unique_fd client_fd;
226     EXPECT_FALSE(socket_spec_connect(&client_fd, "tcp:", &port, &serial, &error));
227     EXPECT_FALSE(socket_spec_connect(&client_fd, "acceptfd:", &port, &serial, &error));
228     EXPECT_FALSE(socket_spec_connect(&client_fd, "vsock:", &port, &serial, &error));
229     EXPECT_FALSE(socket_spec_connect(&client_fd, "vsock:x", &port, &serial, &error));
230     EXPECT_FALSE(socket_spec_connect(&client_fd, "vsock:5", &port, &serial, &error));
231     EXPECT_FALSE(socket_spec_connect(&client_fd, "vsock:5:x", &port, &serial, &error));
232     EXPECT_FALSE(socket_spec_connect(&client_fd, "sneakernet:", &port, &serial, &error));
233 }
234 
TEST(socket_spec,socket_spec_listen_connect_localfilesystem)235 TEST(socket_spec, socket_spec_listen_connect_localfilesystem) {
236     std::string error, serial;
237     int port;
238     unique_fd server_fd, client_fd;
239     TemporaryDir sock_dir;
240 
241     // Only run this test if the created directory is writable.
242     int result = access(sock_dir.path, W_OK);
243     if (result == 0) {
244         std::string sock_addr =
245                 android::base::StringPrintf("localfilesystem:%s/af_unix_socket", sock_dir.path);
246         EXPECT_FALSE(socket_spec_connect(&client_fd, sock_addr, &port, &serial, &error));
247         server_fd.reset(socket_spec_listen(sock_addr, &error, &port));
248 
249         EXPECT_NE(server_fd.get(), -1);
250         EXPECT_TRUE(socket_spec_connect(&client_fd, sock_addr, &port, &serial, &error));
251         EXPECT_NE(client_fd.get(), -1);
252     }
253 }
254 
TEST(socket_spec,is_socket_spec)255 TEST(socket_spec, is_socket_spec) {
256     EXPECT_TRUE(is_socket_spec("tcp:blah"));
257     EXPECT_TRUE(is_socket_spec("acceptfd:blah"));
258     EXPECT_TRUE(is_socket_spec("local:blah"));
259     EXPECT_TRUE(is_socket_spec("localreserved:blah"));
260     EXPECT_TRUE(is_socket_spec("vsock:123:456"));
261 }
262 
TEST(socket_spec,is_local_socket_spec)263 TEST(socket_spec, is_local_socket_spec) {
264     EXPECT_TRUE(is_local_socket_spec("local:blah"));
265     EXPECT_TRUE(is_local_socket_spec("tcp:localhost"));
266     EXPECT_FALSE(is_local_socket_spec("tcp:www.google.com"));
267 }
268