2024-11-12 23:07:17 +00:00
|
|
|
#include "lix/libstore/filetransfer.hh"
|
|
|
|
#include "lix/libutil/compression.hh"
|
2024-11-19 02:37:24 +00:00
|
|
|
#include "lix/libutil/thread-name.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>
|
2024-05-09 19:22:48 +00:00
|
|
|
#include <string>
|
2024-04-28 00:04:08 +00:00
|
|
|
#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;
|
2024-10-29 19:33:42 +00:00
|
|
|
std::function<std::optional<std::string>(int)> content;
|
2024-11-09 00:17:28 +00:00
|
|
|
std::list<std::string> expectedHeaders;
|
2024-10-29 19:33:42 +00:00
|
|
|
|
|
|
|
Reply(
|
2024-11-09 00:17:28 +00:00
|
|
|
std::string_view status,
|
|
|
|
std::string_view headers,
|
|
|
|
std::function<std::string()> content,
|
|
|
|
std::list<std::string> expectedHeaders = {}
|
2024-10-29 19:33:42 +00:00
|
|
|
)
|
2024-11-09 00:17:28 +00:00
|
|
|
: Reply(
|
|
|
|
status,
|
|
|
|
headers,
|
|
|
|
[content](int round) { return round == 0 ? std::optional(content()) : std::nullopt; },
|
|
|
|
std::move(expectedHeaders)
|
|
|
|
)
|
2024-10-29 19:33:42 +00:00
|
|
|
{
|
|
|
|
}
|
2024-05-29 18:51:37 +00:00
|
|
|
|
2024-10-29 19:33:42 +00:00
|
|
|
Reply(
|
|
|
|
std::string_view status,
|
|
|
|
std::string_view headers,
|
2024-11-09 00:17:28 +00:00
|
|
|
std::function<std::optional<std::string>(int)> content,
|
|
|
|
std::list<std::string> expectedHeaders = {}
|
2024-10-29 19:33:42 +00:00
|
|
|
)
|
|
|
|
: status(status)
|
|
|
|
, headers(headers)
|
|
|
|
, content(content)
|
2024-11-09 00:17:28 +00:00
|
|
|
, expectedHeaders(std::move(expectedHeaders))
|
2024-10-29 19:33:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2024-05-29 18:51:37 +00:00
|
|
|
}
|
|
|
|
|
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-11-19 02:37:24 +00:00
|
|
|
setCurrentThreadName("test httpd server");
|
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");
|
|
|
|
}
|
|
|
|
|
2024-05-29 18:51:37 +00:00
|
|
|
const auto & reply = replies[at++ % replies.size()];
|
|
|
|
|
2024-10-29 19:33:42 +00:00
|
|
|
std::thread([=, conn{std::move(conn)}] {
|
2024-11-19 02:37:24 +00:00
|
|
|
setCurrentThreadName("test httpd connection");
|
2024-10-29 19:33:42 +00:00
|
|
|
auto send = [&](std::string_view bit) {
|
|
|
|
while (!bit.empty()) {
|
2024-11-09 00:17:28 +00:00
|
|
|
auto written = ::send(conn.get(), bit.data(), bit.size(), MSG_NOSIGNAL);
|
2024-10-29 19:33:42 +00:00
|
|
|
if (written < 0) {
|
2024-11-09 00:17:28 +00:00
|
|
|
debug("send() failed: %s", strerror(errno));
|
|
|
|
return;
|
2024-10-29 19:33:42 +00:00
|
|
|
}
|
|
|
|
bit.remove_prefix(written);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
send("HTTP/1.1 ");
|
|
|
|
send(reply.status);
|
|
|
|
send("\r\n");
|
2024-11-09 00:17:28 +00:00
|
|
|
|
|
|
|
std::string requestWithHeaders;
|
|
|
|
while (true) {
|
|
|
|
char c;
|
|
|
|
if (recv(conn.get(), &c, 1, MSG_NOSIGNAL) != 1) {
|
|
|
|
debug("recv() failed for headers: %s", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
requestWithHeaders += c;
|
|
|
|
if (requestWithHeaders.ends_with("\r\n\r\n")) {
|
|
|
|
requestWithHeaders.resize(requestWithHeaders.size() - 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug("got request:\n%s", requestWithHeaders);
|
|
|
|
for (auto & expected : reply.expectedHeaders) {
|
|
|
|
ASSERT_TRUE(requestWithHeaders.contains(fmt("%s\r\n", expected)));
|
|
|
|
}
|
|
|
|
|
2024-10-29 19:33:42 +00:00
|
|
|
send(reply.headers);
|
|
|
|
send("\r\n");
|
|
|
|
for (int round = 0; ; round++) {
|
|
|
|
if (auto content = reply.content(round); content.has_value()) {
|
|
|
|
send(*content);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
::shutdown(conn.get(), SHUT_WR);
|
|
|
|
for (;;) {
|
|
|
|
char buf[1];
|
2024-11-09 00:17:28 +00:00
|
|
|
switch (recv(conn.get(), buf, 1, MSG_NOSIGNAL)) {
|
2024-10-29 19:33:42 +00:00
|
|
|
case 0:
|
|
|
|
return; // remote closed
|
|
|
|
case 1:
|
|
|
|
continue; // connection still held open by remote
|
|
|
|
default:
|
2024-11-09 00:17:28 +00:00
|
|
|
debug("recv() failed: %s", strerror(errno));
|
|
|
|
return;
|
2024-10-29 19:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).detach();
|
2024-04-28 00:04:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
{
|
2024-08-05 03:19:58 +00:00
|
|
|
struct Done : std::exception
|
2024-05-03 23:39:06 +00:00
|
|
|
{};
|
|
|
|
|
|
|
|
auto ft = makeFileTransfer();
|
|
|
|
|
|
|
|
LambdaSink broken([](auto block) { throw Done(); });
|
|
|
|
|
2024-11-15 15:15:11 +00:00
|
|
|
auto [port, srv] = serveHTTP({{"200 ok", "", [](int) { return "foo"; }}});
|
|
|
|
ASSERT_THROW(ft->download(fmt("http://[::1]:%d/index", port)).second->drainInto(broken), Done);
|
2024-05-03 23:39:06 +00:00
|
|
|
|
|
|
|
// 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-10-07 07:43:30 +00:00
|
|
|
TEST(FileTransfer, exceptionAbortsRead)
|
|
|
|
{
|
|
|
|
auto [port, srv] = serveHTTP("200 ok", "content-length: 0\r\n", [] { return ""; });
|
|
|
|
auto ft = makeFileTransfer();
|
|
|
|
char buf[10] = "";
|
2024-10-28 17:59:43 +00:00
|
|
|
ASSERT_THROW(ft->download(fmt("http://[::1]:%d/index", port)).second->read(buf, 10), EndOfFile);
|
2024-10-07 07:43:30 +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-10-28 17:59:43 +00:00
|
|
|
ft->download(fmt("http://[::1]:%d/index", port)),
|
2024-10-26 13:19:57 +00:00
|
|
|
FileTransferError
|
|
|
|
);
|
2024-04-28 00:04:08 +00:00
|
|
|
}
|
|
|
|
|
2024-05-09 19:22:48 +00:00
|
|
|
TEST(FileTransfer, NOT_ON_DARWIN(defersFailures))
|
2024-04-28 00:04:08 +00:00
|
|
|
{
|
2024-05-09 19:22:48 +00:00
|
|
|
auto [port, srv] = serveHTTP("200 ok", "content-length: 100000000\r\n", [] {
|
2024-04-28 00:04:08 +00:00
|
|
|
std::this_thread::sleep_for(10ms);
|
2024-05-09 19:22:48 +00:00
|
|
|
// just a bunch of data to fill the curl wrapper buffer, otherwise the
|
|
|
|
// initial wait for header data will also wait for the the response to
|
|
|
|
// complete (the source is only woken when curl returns data, and curl
|
|
|
|
// might only do so once its internal buffer has already been filled.)
|
|
|
|
return std::string(1024 * 1024, ' ');
|
2024-04-28 00:04:08 +00:00
|
|
|
});
|
2024-10-26 15:33:14 +00:00
|
|
|
auto ft = makeFileTransfer(0);
|
2024-10-28 17:59:43 +00:00
|
|
|
auto src = ft->download(fmt("http://[::1]:%d/index", port)).second;
|
2024-05-09 19:22:48 +00:00
|
|
|
ASSERT_THROW(src->drain(), 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;
|
2024-10-28 17:59:43 +00:00
|
|
|
ft->download(fmt("http://[::1]:%d/index", port)).second->drainInto(sink);
|
2024-05-11 13:10:45 +00:00
|
|
|
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"; }},
|
|
|
|
});
|
2024-10-26 15:33:14 +00:00
|
|
|
auto ft = makeFileTransfer(0);
|
2024-10-28 17:59:43 +00:00
|
|
|
auto [result, _data] = ft->download(fmt("http://[::1]:%d/first", port));
|
2024-05-29 18:51:37 +00:00
|
|
|
ASSERT_EQ(result.immutableUrl, "http://foo");
|
|
|
|
}
|
|
|
|
|
2024-10-29 19:33:42 +00:00
|
|
|
TEST(FileTransfer, stalledReaderDoesntBlockOthers)
|
|
|
|
{
|
|
|
|
auto [port, srv] = serveHTTP({
|
|
|
|
{"200 ok",
|
|
|
|
"content-length: 100000000\r\n",
|
|
|
|
[](int round) mutable {
|
|
|
|
return round < 100 ? std::optional(std::string(1'000'000, ' ')) : std::nullopt;
|
|
|
|
}},
|
|
|
|
});
|
|
|
|
auto ft = makeFileTransfer(0);
|
|
|
|
auto [_result1, data1] = ft->download(fmt("http://[::1]:%d", port));
|
|
|
|
auto [_result2, data2] = ft->download(fmt("http://[::1]:%d", port));
|
|
|
|
auto drop = [](Source & source, size_t size) {
|
|
|
|
char buf[1000];
|
|
|
|
while (size > 0) {
|
|
|
|
auto round = std::min(size, sizeof(buf));
|
|
|
|
source(buf, round);
|
|
|
|
size -= round;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// read 10M of each of the 100M, then the rest. neither reader should
|
|
|
|
// block the other, nor should it take that long to copy 200MB total.
|
|
|
|
drop(*data1, 10'000'000);
|
|
|
|
drop(*data2, 10'000'000);
|
|
|
|
drop(*data1, 90'000'000);
|
|
|
|
drop(*data2, 90'000'000);
|
|
|
|
|
|
|
|
ASSERT_THROW(drop(*data1, 1), EndOfFile);
|
|
|
|
ASSERT_THROW(drop(*data2, 1), EndOfFile);
|
|
|
|
}
|
|
|
|
|
2024-11-09 00:17:28 +00:00
|
|
|
TEST(FileTransfer, retries)
|
|
|
|
{
|
|
|
|
auto [port, srv] = serveHTTP({
|
|
|
|
// transient setup failure
|
|
|
|
{"429 try again later", "content-length: 0\r\n", [] { return ""; }},
|
|
|
|
// transient transfer failure (simulates a connection break)
|
|
|
|
{"200 ok",
|
|
|
|
"content-length: 2\r\n"
|
|
|
|
"accept-ranges: bytes\r\n",
|
|
|
|
[] { return "a"; }},
|
|
|
|
// wrapper should ask for remaining data now
|
|
|
|
{"200 ok",
|
|
|
|
"content-length: 1\r\n"
|
|
|
|
"content-range: bytes 1-1/2\r\n",
|
|
|
|
[] { return "b"; },
|
|
|
|
{"Range: bytes=1-"}},
|
|
|
|
});
|
|
|
|
auto ft = makeFileTransfer(0);
|
|
|
|
auto [result, data] = ft->download(fmt("http://[::1]:%d", port));
|
|
|
|
ASSERT_EQ(data->drain(), "ab");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileTransfer, doesntRetrySetupForever)
|
|
|
|
{
|
|
|
|
auto [port, srv] = serveHTTP({
|
|
|
|
{"429 try again later", "content-length: 0\r\n", [] { return ""; }},
|
|
|
|
});
|
|
|
|
auto ft = makeFileTransfer(0);
|
|
|
|
ASSERT_THROW(ft->download(fmt("http://[::1]:%d", port)), FileTransferError);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileTransfer, doesntRetryTransferForever)
|
|
|
|
{
|
|
|
|
constexpr size_t LIMIT = 20;
|
|
|
|
ASSERT_LT(fileTransferSettings.tries, LIMIT); // just to keep test runtime low
|
|
|
|
std::vector<Reply> replies;
|
|
|
|
for (size_t i = 0; i < LIMIT; i++) {
|
|
|
|
replies.emplace_back(
|
|
|
|
"200 ok",
|
|
|
|
fmt("content-length: %1%\r\n"
|
|
|
|
"accept-ranges: bytes\r\n"
|
|
|
|
"content-range: bytes %2%-%3%/%3%\r\n",
|
|
|
|
LIMIT - i,
|
|
|
|
i,
|
|
|
|
LIMIT),
|
|
|
|
[] { return "a"; }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
auto [port, srv] = serveHTTP(replies);
|
|
|
|
auto ft = makeFileTransfer(0);
|
|
|
|
ASSERT_THROW(ft->download(fmt("http://[::1]:%d", port)).second->drain(), FileTransferError);
|
|
|
|
}
|
|
|
|
|
2024-11-09 00:17:28 +00:00
|
|
|
TEST(FileTransfer, doesntRetryUploads)
|
|
|
|
{
|
|
|
|
auto ft = makeFileTransfer(0);
|
|
|
|
|
|
|
|
{
|
|
|
|
auto [port, srv] = serveHTTP({
|
|
|
|
{"429 try again later", "", [] { return ""; }},
|
|
|
|
{"200 ok", "", [] { return ""; }},
|
|
|
|
});
|
|
|
|
ASSERT_THROW(ft->upload(fmt("http://[::1]:%d", port), ""), FileTransferError);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto [port, srv] = serveHTTP({
|
|
|
|
{"429 try again later", "", [] { return ""; }},
|
|
|
|
{"200 ok", "", [] { return ""; }},
|
|
|
|
});
|
|
|
|
ASSERT_THROW(ft->upload(fmt("http://[::1]:%d", port), "foo"), FileTransferError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:39:06 +00:00
|
|
|
}
|