• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
TEST(socket_spec,parse_tcp_socket_spec_just_port)27 TEST(socket_spec, parse_tcp_socket_spec_just_port) {
28     std::string hostname, error, serial;
29     int port;
30     EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &serial, &error));
31     EXPECT_EQ("", hostname);
32     EXPECT_EQ(5037, port);
33     EXPECT_EQ("", serial);
34 }
35 
TEST(socket_spec,parse_tcp_socket_spec_bad_ports)36 TEST(socket_spec, parse_tcp_socket_spec_bad_ports) {
37     std::string hostname, error, serial;
38     int port;
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));
42 }
43 
TEST(socket_spec,parse_tcp_socket_spec_host_and_port)44 TEST(socket_spec, parse_tcp_socket_spec_host_and_port) {
45     std::string hostname, error, serial;
46     int port;
47     EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &serial, &error));
48     EXPECT_EQ("localhost", hostname);
49     EXPECT_EQ(1234, port);
50     EXPECT_EQ("localhost:1234", serial);
51 }
52 
TEST(socket_spec,parse_tcp_socket_spec_host_no_port)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 
TEST(socket_spec,parse_tcp_socket_spec_host_bad_ports)62 TEST(socket_spec, parse_tcp_socket_spec_host_bad_ports) {
63     std::string hostname, error, serial;
64     int port;
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));
68 }
69 
TEST(socket_spec,parse_tcp_socket_spec_ipv6_and_port)70 TEST(socket_spec, parse_tcp_socket_spec_ipv6_and_port) {
71     std::string hostname, error, serial;
72     int port;
73     EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &serial, &error));
74     EXPECT_EQ("::1", hostname);
75     EXPECT_EQ(1234, port);
76     EXPECT_EQ("[::1]:1234", serial);
77 }
78 
TEST(socket_spec,parse_tcp_socket_spec_ipv6_no_port)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 
TEST(socket_spec,parse_tcp_socket_spec_ipv6_bad_ports)88 TEST(socket_spec, parse_tcp_socket_spec_ipv6_bad_ports) {
89     std::string hostname, error, serial;
90     int port;
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));
94 }
95 
TEST(socket_spec,get_host_socket_spec_port)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 
TEST(socket_spec,get_host_socket_spec_port_no_port)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 
TEST(socket_spec,get_host_socket_spec_port_bad_ports)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 
TEST(socket_spec,get_host_socket_spec_port_bad_string)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 
TEST(socket_spec,socket_spec_listen_connect_tcp)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 
TEST(socket_spec,socket_spec_listen_connect_localfilesystem)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 }
155