107 lines
4.6 KiB
Plaintext
107 lines
4.6 KiB
Plaintext
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Build from the project root with:
|
|
// clang++ -std=c++17 -fobjc-arc -Isrc platforms/macos/Bridge/DriveWireTCP.mm \
|
|
// tests/macos/drivewire-tcp-probe.mm -framework Network -framework Foundation -o /tmp/vcc-dw-tcp-probe
|
|
// The listener below is a test fixture bound exclusively to IPv4 loopback.
|
|
#include "devices/DriveWireTransport.h"
|
|
#include <arpa/inet.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
using namespace std::chrono_literals;
|
|
using namespace VCC::Devices;
|
|
|
|
static void require(bool condition, const char* description) {
|
|
if (!condition) {
|
|
std::fprintf(stderr, "Failure: %s\n", description);
|
|
std::exit(1);
|
|
}
|
|
}
|
|
|
|
template<class Check>
|
|
static void waitFor(Check check, const char* description) {
|
|
auto deadline = std::chrono::steady_clock::now() + 8s;
|
|
while (!check()) {
|
|
require(std::chrono::steady_clock::now() < deadline, description);
|
|
std::this_thread::sleep_for(2ms);
|
|
}
|
|
}
|
|
|
|
static void sendAll(int client, const uint8_t* data, size_t size) {
|
|
while (size) {
|
|
auto written = send(client, data, size, 0);
|
|
require(written > 0, "fixture could not send data");
|
|
data += written;
|
|
size -= written;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
int listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
require(listener >= 0, "create loopback socket");
|
|
sockaddr_in address{};
|
|
address.sin_family = AF_INET;
|
|
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
require(bind(listener, reinterpret_cast<sockaddr*>(&address), sizeof(address)) == 0, "bind loopback");
|
|
require(listen(listener, 2) == 0, "listen on loopback");
|
|
socklen_t length = sizeof(address);
|
|
require(getsockname(listener, reinterpret_cast<sockaddr*>(&address), &length) == 0, "get dynamic port");
|
|
uint16_t port = ntohs(address.sin_port);
|
|
std::vector<uint8_t> burst(160 * 1024);
|
|
for (size_t i = 0; i < burst.size(); ++i) burst[i] = static_cast<uint8_t>(i * 37);
|
|
const std::array<uint8_t, 4> message{0, 0xff, 'A', '\r'};
|
|
|
|
std::thread fixture([&] {
|
|
for (int pass = 0; pass < 2; ++pass) {
|
|
int client = accept(listener, nullptr, nullptr);
|
|
require(client >= 0, "accept loopback connection");
|
|
std::array<uint8_t, 4> received{};
|
|
require(recv(client, received.data(), received.size(), MSG_WAITALL) == sizeof(received), "receive message");
|
|
require(received == message, "order and binary transparency on write");
|
|
sendAll(client, received.data(), received.size());
|
|
if (pass == 1) sendAll(client, burst.data(), burst.size());
|
|
close(client);
|
|
}
|
|
});
|
|
|
|
using State = DriveWireTransport::State;
|
|
for (int pass = 0; pass < 2; ++pass) {
|
|
auto stream = makeDriveWireTCPConnection("127.0.0.1", port);
|
|
require(stream != nullptr, "construct stream");
|
|
waitFor([&] { return stream->state() == State::connected; }, "connection becomes ready");
|
|
require(!stream->write(burst.data(), burst.size()), "reject oversized write atomically");
|
|
for (auto byte : message) require(stream->write(&byte, 1), "write guest byte");
|
|
|
|
if (!pass) waitFor([&] { return stream->state() == State::closed; }, "observe peer EOF before reading");
|
|
std::vector<uint8_t> actual;
|
|
std::array<uint8_t, 513> chunk{};
|
|
std::vector<uint8_t> expected(message.begin(), message.end());
|
|
if (pass) expected.insert(expected.end(), burst.begin(), burst.end());
|
|
waitFor([&] {
|
|
size_t count;
|
|
while ((count = stream->read(chunk.data(), chunk.size()))) {
|
|
actual.insert(actual.end(), chunk.begin(), chunk.begin() + count);
|
|
}
|
|
return stream->state() == State::closed && actual.size() == expected.size();
|
|
}, "drain all buffered bytes and receive EOF");
|
|
require(actual == expected, "echo and large response survive buffering and EOF");
|
|
require(!stream->write(message.data(), message.size()), "writes fail after closure");
|
|
}
|
|
fixture.join();
|
|
close(listener);
|
|
auto refused = makeDriveWireTCPConnection("127.0.0.1", port);
|
|
require(refused != nullptr, "construct refused connection");
|
|
waitFor([&] { return refused->state() == State::failed; }, "asynchronous connection refusal");
|
|
auto canceled = makeDriveWireTCPConnection("127.0.0.1", port);
|
|
canceled.reset();
|
|
std::puts("DriveWire outbound TCP: echo, backpressure, EOF, reconnect, refusal and cancellation passed");
|
|
}
|