RNBSocketTest.cpp
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//===-- RNBSocketTest.cpp ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include <arpa/inet.h>
#include <sys/sysctl.h>
#include <unistd.h>
#include "RNBDefs.h"
#include "RNBSocket.h"
#include "lldb/Host/Socket.h"
#include "lldb/Host/StringConvert.h"
#include "lldb/Host/common/TCPSocket.h"
using namespace lldb_private;
std::string hello = "Hello, world!";
std::string goodbye = "Goodbye!";
static void ServerCallbackv4(const void *baton, in_port_t port) {
auto child_pid = fork();
if (child_pid == 0) {
Socket *client_socket;
char addr_buffer[256];
sprintf(addr_buffer, "%s:%d", baton, port);
Status err = Socket::TcpConnect(addr_buffer, false, client_socket);
if (err.Fail())
abort();
char buffer[32];
size_t read_size = 32;
err = client_socket->Read((void *)&buffer[0], read_size);
if (err.Fail())
abort();
std::string Recv(&buffer[0], read_size);
if (Recv != hello)
abort();
size_t write_size = goodbye.length();
err = client_socket->Write(goodbye.c_str(), write_size);
if (err.Fail())
abort();
if (write_size != goodbye.length())
abort();
delete client_socket;
exit(0);
}
}
void TestSocketListen(const char *addr) {
// Skip IPv6 tests if there isn't a valid interafce
auto addresses = lldb_private::SocketAddress::GetAddressInfo(
addr, NULL, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
if (addresses.size() == 0)
return;
char addr_wrap[256];
if (addresses.front().GetFamily() == AF_INET6)
sprintf(addr_wrap, "[%s]", addr);
else
sprintf(addr_wrap, "%s", addr);
RNBSocket server_socket;
auto result =
server_socket.Listen(addr, 0, ServerCallbackv4, (const void *)addr_wrap);
ASSERT_TRUE(result == rnb_success);
result = server_socket.Write(hello.c_str(), hello.length());
ASSERT_TRUE(result == rnb_success);
std::string bye;
result = server_socket.Read(bye);
ASSERT_TRUE(result == rnb_success);
ASSERT_EQ(bye, goodbye);
int exit_status;
wait(&exit_status);
ASSERT_EQ(exit_status, 0);
}
TEST(RNBSocket, LoopBackListenIPv4) { TestSocketListen("127.0.0.1"); }
TEST(RNBSocket, LoopBackListenIPv6) { TestSocketListen("::1"); }
TEST(RNBSocket, AnyListen) { TestSocketListen("*"); }
void TestSocketConnect(const char *addr) {
// Skip IPv6 tests if there isn't a valid interafce
auto addresses = lldb_private::SocketAddress::GetAddressInfo(
addr, NULL, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
if (addresses.size() == 0)
return;
char addr_wrap[256];
if (addresses.front().GetFamily() == AF_INET6)
sprintf(addr_wrap, "[%s]:0", addr);
else
sprintf(addr_wrap, "%s:0", addr);
Socket *server_socket;
Predicate<uint16_t> port_predicate;
port_predicate.SetValue(0, eBroadcastNever);
Status err =
Socket::TcpListen(addr_wrap, false, server_socket, &port_predicate);
ASSERT_FALSE(err.Fail());
auto port = ((TCPSocket *)server_socket)->GetLocalPortNumber();
auto child_pid = fork();
if (child_pid != 0) {
RNBSocket client_socket;
auto result = client_socket.Connect(addr, port);
ASSERT_TRUE(result == rnb_success);
result = client_socket.Write(hello.c_str(), hello.length());
ASSERT_TRUE(result == rnb_success);
std::string bye;
result = client_socket.Read(bye);
ASSERT_TRUE(result == rnb_success);
ASSERT_EQ(bye, goodbye);
} else {
Socket *connected_socket;
err = server_socket->Accept(connected_socket);
if (err.Fail()) {
llvm::errs() << err.AsCString();
abort();
}
char buffer[32];
size_t read_size = 32;
err = connected_socket->Read((void *)&buffer[0], read_size);
if (err.Fail()) {
llvm::errs() << err.AsCString();
abort();
}
std::string Recv(&buffer[0], read_size);
if (Recv != hello) {
llvm::errs() << err.AsCString();
abort();
}
size_t write_size = goodbye.length();
err = connected_socket->Write(goodbye.c_str(), write_size);
if (err.Fail()) {
llvm::errs() << err.AsCString();
abort();
}
if (write_size != goodbye.length()) {
llvm::errs() << err.AsCString();
abort();
}
exit(0);
}
int exit_status;
wait(&exit_status);
ASSERT_EQ(exit_status, 0);
}
TEST(RNBSocket, LoopBackConnectIPv4) { TestSocketConnect("127.0.0.1"); }
TEST(RNBSocket, LoopBackConnectIPv6) { TestSocketConnect("::1"); }