blob: e9d5270ca5f1ba0d8671cd3dcd28647b0e119d0b [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 Hughes14d673e2019-07-30 13:51:03 -070027TEST(socket_spec, parse_tcp_socket_spec_just_port) {
Cody Schuffelen331a9082019-01-02 14:17:29 -080028 std::string hostname, error, serial;
Josh Gao3fb517c2016-09-19 17:31:55 -070029 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080030 EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070031 EXPECT_EQ("", hostname);
32 EXPECT_EQ(5037, port);
Cody Schuffelen331a9082019-01-02 14:17:29 -080033 EXPECT_EQ("", serial);
Elliott Hughes14d673e2019-07-30 13:51:03 -070034}
Josh Gao3fb517c2016-09-19 17:31:55 -070035
Elliott Hughes14d673e2019-07-30 13:51:03 -070036TEST(socket_spec, parse_tcp_socket_spec_bad_ports) {
37 std::string hostname, error, serial;
38 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080039 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 Hughes14d673e2019-07-30 13:51:03 -070042}
Josh Gao3fb517c2016-09-19 17:31:55 -070043
Elliott Hughes14d673e2019-07-30 13:51:03 -070044TEST(socket_spec, parse_tcp_socket_spec_host_and_port) {
45 std::string hostname, error, serial;
46 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080047 EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070048 EXPECT_EQ("localhost", hostname);
49 EXPECT_EQ(1234, port);
Cody Schuffelen331a9082019-01-02 14:17:29 -080050 EXPECT_EQ("localhost:1234", serial);
Elliott Hughes14d673e2019-07-30 13:51:03 -070051}
Josh Gao3fb517c2016-09-19 17:31:55 -070052
Elliott Hughes14d673e2019-07-30 13:51:03 -070053TEST(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
62TEST(socket_spec, parse_tcp_socket_spec_host_bad_ports) {
63 std::string hostname, error, serial;
64 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080065 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 Hughes14d673e2019-07-30 13:51:03 -070068}
Josh Gao3fb517c2016-09-19 17:31:55 -070069
Elliott Hughes14d673e2019-07-30 13:51:03 -070070TEST(socket_spec, parse_tcp_socket_spec_ipv6_and_port) {
71 std::string hostname, error, serial;
72 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080073 EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &serial, &error));
Josh Gao3fb517c2016-09-19 17:31:55 -070074 EXPECT_EQ("::1", hostname);
75 EXPECT_EQ(1234, port);
Cody Schuffelen331a9082019-01-02 14:17:29 -080076 EXPECT_EQ("[::1]:1234", serial);
Elliott Hughes14d673e2019-07-30 13:51:03 -070077}
Josh Gao3fb517c2016-09-19 17:31:55 -070078
Elliott Hughes14d673e2019-07-30 13:51:03 -070079TEST(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
88TEST(socket_spec, parse_tcp_socket_spec_ipv6_bad_ports) {
89 std::string hostname, error, serial;
90 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -080091 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 Gao3fb517c2016-09-19 17:31:55 -070094}
Jason Jeremy Iman84613872019-07-19 12:44:39 +090095
96TEST(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
104TEST(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
110TEST(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
118TEST(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
126TEST(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
137TEST(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}