2024-05-03 23:39:06 +00:00
|
|
|
#include "filetransfer.hh"
|
2024-05-11 13:10:45 +00:00
|
|
|
#include "compression.hh"
|
2024-05-03 23:39:06 +00:00
|
|
|
|
2024-04-28 00:04:08 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <exception>
|
2024-05-03 23:39:06 +00:00
|
|
|
#include <future>
|
|
|
|
#include <gtest/gtest.h>
|
2024-04-28 00:04:08 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string_view>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <thread>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2024-05-30 21:38:32 +00:00
|
|
|
// local server tests don't work on darwin without some incantations
|
|
|
|
// the horrors do not want to look up. contributions welcome though!
|
|
|
|
#if __APPLE__
|
|
|
|
#define NOT_ON_DARWIN(n) DISABLED_##n
|
|
|
|
#else
|
|
|
|
#define NOT_ON_DARWIN(n) n
|
|
|
|
#endif
|
|
|
|
|
2024-05-03 23:39:06 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2024-05-29 18:51:37 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Reply {
|
|
|
|
std::string status, headers;
|
|
|
|
std::function<std::string()> content;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:39:06 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2024-04-28 00:04:08 +00:00
|
|
|
static std::tuple<uint16_t, AutoCloseFD>
|
2024-05-29 18:51:37 +00:00
|
|
|
serveHTTP(std::vector<Reply> replies)
|
2024-04-28 00:04:08 +00:00
|
|
|
{
|
|
|
|
AutoCloseFD listener(::socket(AF_INET6, SOCK_STREAM, 0));
|
|
|
|
if (!listener) {
|
|
|
|
throw SysError(errno, "socket() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
Pipe trigger;
|
|
|
|
trigger.create();
|
|
|
|
|
|
|
|
sockaddr_in6 addr = {
|
|
|
|
.sin6_family = AF_INET6,
|
|
|
|
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
|
|
|
|
};
|
|
|
|
socklen_t len = sizeof(addr);
|
|
|
|
if (::bind(listener.get(), reinterpret_cast<const sockaddr *>(&addr), sizeof(addr)) < 0) {
|
|
|
|
throw SysError(errno, "bind() failed");
|
|
|
|
}
|
|
|
|
if (::getsockname(listener.get(), reinterpret_cast<sockaddr *>(&addr), &len) < 0) {
|
|
|
|
throw SysError(errno, "getsockname() failed");
|
|
|
|
}
|
|
|
|
if (::listen(listener.get(), 1) < 0) {
|
|
|
|
throw SysError(errno, "listen() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::thread(
|
2024-05-29 18:51:37 +00:00
|
|
|
[replies, at{0}](AutoCloseFD socket, AutoCloseFD trigger) mutable {
|
2024-04-28 00:04:08 +00:00
|
|
|
while (true) {
|
|
|
|
pollfd pfds[2] = {
|
|
|
|
{
|
|
|
|
.fd = socket.get(),
|
|
|
|
.events = POLLIN,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.fd = trigger.get(),
|
|
|
|
.events = POLLHUP,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (::poll(pfds, 2, -1) <= 0) {
|
|
|
|
throw SysError(errno, "poll() failed");
|
|
|
|
}
|
|
|
|
if (pfds[1].revents & POLLHUP) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(pfds[0].revents & POLLIN)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoCloseFD conn(::accept(socket.get(), nullptr, nullptr));
|
|
|
|
if (!conn) {
|
|
|
|
throw SysError(errno, "accept() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto send = [&](std::string_view bit) {
|
|
|
|
while (!bit.empty()) {
|
|
|
|
auto written = ::write(conn.get(), bit.data(), bit.size());
|
|
|
|
if (written < 0) {
|
|
|
|
throw SysError(errno, "write() failed");
|
|
|
|
}
|
|
|
|
bit.remove_prefix(written);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-29 18:51:37 +00:00
|
|
|
const auto & reply = replies[at++ % replies.size()];
|
|
|
|
|
2024-04-28 00:04:08 +00:00
|
|
|
send("HTTP/1.1 ");
|
2024-05-29 18:51:37 +00:00
|
|
|
send(reply.status);
|
2024-04-28 00:04:08 +00:00
|
|
|
send("\r\n");
|
2024-05-29 18:51:37 +00:00
|
|
|
send(reply.headers);
|
2024-04-28 00:04:08 +00:00
|
|
|
send("\r\n");
|
2024-05-29 18:51:37 +00:00
|
|
|
send(reply.content());
|
2024-04-28 00:04:08 +00:00
|
|
|
::shutdown(conn.get(), SHUT_RDWR);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
std::move(listener),
|
|
|
|
std::move(trigger.readSide)
|
|
|
|
)
|
|
|
|
.detach();
|
|
|
|
|
|
|
|
return {
|
|
|
|
ntohs(addr.sin6_port),
|
|
|
|
std::move(trigger.writeSide),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-29 18:51:37 +00:00
|
|
|
static std::tuple<uint16_t, AutoCloseFD>
|
|
|
|
serveHTTP(std::string status, std::string headers, std::function<std::string()> content)
|
|
|
|
{
|
|
|
|
return serveHTTP({{{status, headers, content}}});
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:39:06 +00:00
|
|
|
TEST(FileTransfer, exceptionAbortsDownload)
|
|
|
|
{
|
|
|
|
struct Done
|
|
|
|
{};
|
|
|
|
|
|
|
|
auto ft = makeFileTransfer();
|
|
|
|
|
|
|
|
LambdaSink broken([](auto block) { throw Done(); });
|
|
|
|
|
|
|
|
ASSERT_THROW(ft->download(FileTransferRequest("file:///dev/zero"), broken), Done);
|
|
|
|
|
|
|
|
// makeFileTransfer returns a ref<>, which cannot be cleared. since we also
|
|
|
|
// can't default-construct it we'll have to overwrite it instead, but we'll
|
|
|
|
// take the raw pointer out first so we can destroy it in a detached thread
|
|
|
|
// (otherwise a failure will stall the process and have it killed by meson)
|
|
|
|
auto reset = std::async(std::launch::async, [&]() { ft = makeFileTransfer(); });
|
|
|
|
EXPECT_EQ(reset.wait_for(10s), std::future_status::ready);
|
|
|
|
// if this did time out we have to leak `reset`.
|
|
|
|
if (reset.wait_for(0s) == std::future_status::timeout) {
|
|
|
|
(void) new auto(std::move(reset));
|
|
|
|
}
|
|
|
|
}
|
2024-04-28 00:04:08 +00:00
|
|
|
|
2024-05-30 21:38:32 +00:00
|
|
|
TEST(FileTransfer, NOT_ON_DARWIN(reportsSetupErrors))
|
2024-04-28 00:04:08 +00:00
|
|
|
{
|
|
|
|
auto [port, srv] = serveHTTP("404 not found", "", [] { return ""; });
|
|
|
|
auto ft = makeFileTransfer();
|
|
|
|
ASSERT_THROW(
|
2024-05-01 16:01:19 +00:00
|
|
|
ft->transfer(FileTransferRequest(fmt("http://[::1]:%d/index", port))),
|
2024-04-28 00:04:08 +00:00
|
|
|
FileTransferError);
|
|
|
|
}
|
|
|
|
|
2024-05-30 21:38:32 +00:00
|
|
|
TEST(FileTransfer, NOT_ON_DARWIN(reportsTransferError))
|
2024-04-28 00:04:08 +00:00
|
|
|
{
|
|
|
|
auto [port, srv] = serveHTTP("200 ok", "content-length: 100\r\n", [] {
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
auto ft = makeFileTransfer();
|
|
|
|
FileTransferRequest req(fmt("http://[::1]:%d/index", port));
|
|
|
|
req.baseRetryTimeMs = 0;
|
2024-05-01 16:01:19 +00:00
|
|
|
ASSERT_THROW(ft->transfer(req), FileTransferError);
|
2024-04-28 00:04:08 +00:00
|
|
|
}
|
2024-05-11 13:10:45 +00:00
|
|
|
|
2024-05-30 21:38:32 +00:00
|
|
|
TEST(FileTransfer, NOT_ON_DARWIN(handlesContentEncoding))
|
2024-05-11 13:10:45 +00:00
|
|
|
{
|
|
|
|
std::string original = "Test data string";
|
|
|
|
std::string compressed = compress("gzip", original);
|
|
|
|
|
|
|
|
auto [port, srv] = serveHTTP("200 ok", "content-encoding: gzip\r\n", [&] { return compressed; });
|
|
|
|
auto ft = makeFileTransfer();
|
|
|
|
|
|
|
|
StringSink sink;
|
|
|
|
ft->download(FileTransferRequest(fmt("http://[::1]:%d/index", port)), sink);
|
|
|
|
EXPECT_EQ(sink.s, original);
|
|
|
|
}
|
2024-05-29 18:51:37 +00:00
|
|
|
|
|
|
|
TEST(FileTransfer, usesIntermediateLinkHeaders)
|
|
|
|
{
|
|
|
|
auto [port, srv] = serveHTTP({
|
|
|
|
{"301 ok",
|
|
|
|
"location: /second\r\n"
|
|
|
|
"content-length: 0\r\n",
|
|
|
|
[] { return ""; }},
|
|
|
|
{"307 ok",
|
|
|
|
"location: /third\r\n"
|
|
|
|
"content-length: 0\r\n",
|
|
|
|
[] { return ""; }},
|
|
|
|
{"307 ok",
|
|
|
|
"location: /fourth\r\n"
|
|
|
|
"link: <http://foo>; rel=\"immutable\"\r\n"
|
|
|
|
"content-length: 0\r\n",
|
|
|
|
[] { return ""; }},
|
|
|
|
{"200 ok", "content-length: 1\r\n", [] { return "a"; }},
|
|
|
|
});
|
|
|
|
auto ft = makeFileTransfer();
|
|
|
|
FileTransferRequest req(fmt("http://[::1]:%d/first", port));
|
|
|
|
req.baseRetryTimeMs = 0;
|
2024-05-01 16:01:19 +00:00
|
|
|
auto result = ft->transfer(req);
|
2024-05-29 18:51:37 +00:00
|
|
|
ASSERT_EQ(result.immutableUrl, "http://foo");
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:39:06 +00:00
|
|
|
}
|