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 TEST(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 
34 TEST(socket_spec, parse_tcp_socket_spec_just_port) {
35     std::string hostname, error, serial;
36     int port;
37     EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &serial, &error));
38     EXPECT_EQ("", hostname);
39     EXPECT_EQ(5037, port);
40     EXPECT_EQ("", serial);
41 }
42 
43 TEST(socket_spec, parse_tcp_socket_spec_bad_ports) {
44     std::string hostname, error, serial;
45     int port;
46     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));
49 }
50 
51 TEST(socket_spec, parse_tcp_socket_spec_host_and_port) {
52     std::string hostname, error, serial;
53     int port;
54     EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &serial, &error));
55     EXPECT_EQ("localhost", hostname);
56     EXPECT_EQ(1234, port);
57     EXPECT_EQ("localhost:1234", serial);
58 }
59 
60 TEST(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 
69 TEST(socket_spec, parse_tcp_socket_spec_host_bad_ports) {
70     std::string hostname, error, serial;
71     int port;
72     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));
75 }
76 
77 TEST(socket_spec, parse_tcp_socket_spec_ipv6_and_port) {
78     std::string hostname, error, serial;
79     int port;
80     EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &serial, &error));
81     EXPECT_EQ("::1", hostname);
82     EXPECT_EQ(1234, port);
83     EXPECT_EQ("[::1]:1234", serial);
84 }
85 
86 TEST(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 
95 TEST(socket_spec, parse_tcp_socket_spec_ipv6_bad_ports) {
96     std::string hostname, error, serial;
97     int port;
98     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));
101 }
102 
103 TEST(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 
111 TEST(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 
117 TEST(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 
125 TEST(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 
133 TEST(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 
144 TEST(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 
157 TEST(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 }
175 
176 TEST(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"));
181     EXPECT_TRUE(is_socket_spec("vsock:123:456"));
182 }
183 
184 TEST(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 }
189