[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- RNBSocketTest.cpp -------------------------------------------------===//
|
2017-04-13 05:56:29 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-04-13 05:56:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
2020-04-23 22:58:49 +08:00
|
|
|
#include "llvm/Testing/Support/Error.h"
|
2017-04-13 05:56:29 +08:00
|
|
|
|
|
|
|
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) {
|
|
|
|
char addr_buffer[256];
|
|
|
|
sprintf(addr_buffer, "%s:%d", baton, port);
|
2020-04-23 22:58:49 +08:00
|
|
|
llvm::Expected<std::unique_ptr<Socket>> socket_or_err =
|
|
|
|
Socket::TcpConnect(addr_buffer, false);
|
|
|
|
ASSERT_THAT_EXPECTED(socket_or_err, llvm::Succeeded());
|
|
|
|
Socket *client_socket = socket_or_err->get();
|
|
|
|
|
2017-04-13 05:56:29 +08:00
|
|
|
char buffer[32];
|
|
|
|
size_t read_size = 32;
|
2020-04-23 22:58:49 +08:00
|
|
|
Status err = client_socket->Read((void *)&buffer[0], read_size);
|
2017-04-13 05:56:29 +08:00
|
|
|
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) {
|
2017-04-27 07:17:20 +08:00
|
|
|
// 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);
|
|
|
|
|
2017-04-13 05:56:29 +08:00
|
|
|
RNBSocket server_socket;
|
|
|
|
auto result =
|
2017-04-27 07:17:20 +08:00
|
|
|
server_socket.Listen(addr, 0, ServerCallbackv4, (const void *)addr_wrap);
|
2017-04-13 05:56:29 +08:00
|
|
|
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"); }
|
|
|
|
|
2017-04-27 07:17:20 +08:00
|
|
|
TEST(RNBSocket, LoopBackListenIPv6) { TestSocketListen("::1"); }
|
|
|
|
|
2017-07-14 04:58:13 +08:00
|
|
|
TEST(RNBSocket, AnyListen) { TestSocketListen("*"); }
|
|
|
|
|
2017-04-13 05:56:29 +08:00
|
|
|
void TestSocketConnect(const char *addr) {
|
2017-04-27 07:17:20 +08:00
|
|
|
// 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;
|
|
|
|
|
2017-04-13 05:56:29 +08:00
|
|
|
char addr_wrap[256];
|
2017-04-27 07:17:20 +08:00
|
|
|
if (addresses.front().GetFamily() == AF_INET6)
|
|
|
|
sprintf(addr_wrap, "[%s]:0", addr);
|
|
|
|
else
|
|
|
|
sprintf(addr_wrap, "%s:0", addr);
|
2017-04-13 05:56:29 +08:00
|
|
|
|
|
|
|
Socket *server_socket;
|
|
|
|
Predicate<uint16_t> port_predicate;
|
|
|
|
port_predicate.SetValue(0, eBroadcastNever);
|
2020-04-23 22:58:49 +08:00
|
|
|
llvm::Expected<std::unique_ptr<Socket>> socket_or_err =
|
|
|
|
Socket::TcpListen(addr_wrap, false, &port_predicate);
|
|
|
|
ASSERT_THAT_EXPECTED(socket_or_err, llvm::Succeeded());
|
|
|
|
server_socket = socket_or_err->get();
|
2017-04-13 05:56:29 +08:00
|
|
|
|
|
|
|
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;
|
2020-04-23 22:58:49 +08:00
|
|
|
Status err = server_socket->Accept(connected_socket);
|
2017-04-13 05:56:29 +08:00
|
|
|
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"); }
|
2017-04-27 07:17:20 +08:00
|
|
|
|
|
|
|
TEST(RNBSocket, LoopBackConnectIPv6) { TestSocketConnect("::1"); }
|