core: téléchargement avec reprise, archives tar et persistance
- Downloader : HEAD + Range avec reprise, re-probe à chaque essai (URL CDN signées), retries bornés, fichier partiel conservé, repli sans Range, annulation, progression et ETA - licence : fichier temporaire renommé à la fin ; le placeholder vide « licenses_<id>_<ts>.tar » renvoyé par Majestic pendant la génération de la licence est jeté et la requête relancée (cause du bug ENOENT de l'app officielle) - archives (libarchive) : vérification complète, extraction qui ignore UpdateInfo.xml, refus des chemins absolus/.., liens ignorés (FAT32) - persistance : vehicles.json compatible avec l'app officielle (BOM inclus), réglages + téléchargement en attente, chemins XDG / Haiku, écriture atomique - CLI : download, verify, extract ; Ctrl-C annule proprement - tests : faux CDN (coupures, sans Range, 4xx, annulation), tar ustar/gnu/pax/v7, corruption, traversal (42 cas, 322 assertions) - validé en réel : firmware RCC 8,39 Go téléchargé, interrompu, repris et vérifié (ustar, 16 entrées) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
This commit is contained in:
1 parent
95704f9882
commit
543347a1e6
17 files changed
+2168
-40
No files matched your search
@@ -0,0 +1,200 @@
|
||||
#include "openpu/archive.hpp"
|
||||
|
||||
#include "test_util.hpp"
|
||||
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
#include <cstdio>
|
||||
#include <doctest/doctest.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using namespace openpu;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Entry {
|
||||
std::string path;
|
||||
std::string content;
|
||||
bool directory = false;
|
||||
std::string symlinkTarget;
|
||||
};
|
||||
|
||||
/// Writes a tar with libarchive. `format` is an ARCHIVE_FORMAT_TAR_* value.
|
||||
void makeTar(const fs::path& file, const std::vector<Entry>& entries, int format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED)
|
||||
{
|
||||
archive* a = archive_write_new();
|
||||
archive_write_set_format(a, format);
|
||||
REQUIRE(archive_write_open_filename(a, file.c_str()) == ARCHIVE_OK);
|
||||
for (const auto& e : entries) {
|
||||
archive_entry* entry = archive_entry_new();
|
||||
archive_entry_set_pathname(entry, e.path.c_str());
|
||||
if (e.directory) {
|
||||
archive_entry_set_filetype(entry, AE_IFDIR);
|
||||
archive_entry_set_perm(entry, 0755);
|
||||
} else if (!e.symlinkTarget.empty()) {
|
||||
archive_entry_set_filetype(entry, AE_IFLNK);
|
||||
archive_entry_set_symlink(entry, e.symlinkTarget.c_str());
|
||||
} else {
|
||||
archive_entry_set_filetype(entry, AE_IFREG);
|
||||
archive_entry_set_perm(entry, 0600);
|
||||
archive_entry_set_size(entry, static_cast<la_int64_t>(e.content.size()));
|
||||
}
|
||||
REQUIRE(archive_write_header(a, entry) == ARCHIVE_OK);
|
||||
if (!e.content.empty())
|
||||
archive_write_data(a, e.content.data(), e.content.size());
|
||||
archive_entry_free(entry);
|
||||
}
|
||||
archive_write_close(a);
|
||||
archive_write_free(a);
|
||||
}
|
||||
|
||||
std::vector<Entry> rccLike()
|
||||
{
|
||||
return {
|
||||
{"rcc_update/", "", true, ""},
|
||||
{"rcc_update/UpdateInfo.xml", "<info/>", false, ""},
|
||||
{"rcc_update/pkg/a.bin", std::string(300000, 'x'), false, ""},
|
||||
{"rcc_update/pkg/b.txt", "hello", false, ""},
|
||||
{"./UpdateInfo.xml", "<root/>", false, ""},
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("extraction skips UpdateInfo.xml and keeps the tree")
|
||||
{
|
||||
TempDir tmp;
|
||||
const fs::path tar = tmp.path() / "fw.tar";
|
||||
makeTar(tar, rccLike());
|
||||
const fs::path usb = tmp.path() / "usb";
|
||||
|
||||
ArchiveProgress last;
|
||||
extractTar(tar, usb, [&](const ArchiveProgress& p) { last = p; });
|
||||
CHECK(readAll(usb / "rcc_update/pkg/b.txt") == "hello");
|
||||
CHECK(fs::file_size(usb / "rcc_update/pkg/a.bin") == 300000);
|
||||
CHECK_FALSE(fs::exists(usb / "rcc_update/UpdateInfo.xml"));
|
||||
CHECK_FALSE(fs::exists(usb / "UpdateInfo.xml"));
|
||||
CHECK(last.entries == 5);
|
||||
CHECK(last.processed == last.total);
|
||||
}
|
||||
|
||||
TEST_CASE("payload size excludes skipped entries")
|
||||
{
|
||||
TempDir tmp;
|
||||
const fs::path tar = tmp.path() / "fw.tar";
|
||||
makeTar(tar, rccLike());
|
||||
CHECK(tarPayloadSize(tar) == 300000 + 5);
|
||||
}
|
||||
|
||||
TEST_CASE("verify accepts ustar, gnu, pax and v7 (no magic) archives")
|
||||
{
|
||||
TempDir tmp;
|
||||
for (int format : {ARCHIVE_FORMAT_TAR_USTAR, ARCHIVE_FORMAT_TAR_GNUTAR, ARCHIVE_FORMAT_TAR_PAX_RESTRICTED}) {
|
||||
const fs::path tar = tmp.path() / ("f" + std::to_string(format) + ".tar");
|
||||
makeTar(tar, rccLike(), format);
|
||||
CHECK_NOTHROW(verifyTar(tar));
|
||||
}
|
||||
// V7: ustar header with the magic field blanked out (seen in some PSA archives).
|
||||
const fs::path v7 = tmp.path() / "v7.tar";
|
||||
makeTar(v7, {{"file.bin", "data", false, ""}}, ARCHIVE_FORMAT_TAR_USTAR);
|
||||
std::string raw = readAll(v7);
|
||||
raw.replace(257, 8, std::string(8, '\0')); // magic + version
|
||||
unsigned sum = 0;
|
||||
raw.replace(148, 8, std::string(8, ' '));
|
||||
for (std::size_t i = 0; i < 512; ++i)
|
||||
sum += static_cast<unsigned char>(raw[i]);
|
||||
char chk[8];
|
||||
std::snprintf(chk, sizeof chk, "%06o", sum);
|
||||
raw.replace(148, 7, std::string(chk, 6) + '\0');
|
||||
writeAll(v7, raw);
|
||||
CHECK_NOTHROW(verifyTar(v7));
|
||||
extractTar(v7, tmp.path() / "out");
|
||||
CHECK(readAll(tmp.path() / "out/file.bin") == "data");
|
||||
}
|
||||
|
||||
TEST_CASE("corrupt archives are reported as Corrupt")
|
||||
{
|
||||
TempDir tmp;
|
||||
const fs::path tar = tmp.path() / "fw.tar";
|
||||
makeTar(tar, rccLike());
|
||||
|
||||
SUBCASE("truncated")
|
||||
{
|
||||
std::string raw = readAll(tar);
|
||||
writeAll(tar, raw.substr(0, 100000));
|
||||
try {
|
||||
verifyTar(tar);
|
||||
FAIL("expected ArchiveError");
|
||||
} catch (const ArchiveError& e) {
|
||||
CHECK(e.kind() == ArchiveError::Kind::Corrupt);
|
||||
}
|
||||
}
|
||||
SUBCASE("garbage")
|
||||
{
|
||||
writeAll(tar, std::string(4096, '\x5a'));
|
||||
try {
|
||||
verifyTar(tar);
|
||||
FAIL("expected ArchiveError");
|
||||
} catch (const ArchiveError& e) {
|
||||
CHECK(e.kind() == ArchiveError::Kind::Corrupt);
|
||||
}
|
||||
}
|
||||
SUBCASE("empty file")
|
||||
{
|
||||
writeAll(tar, "");
|
||||
CHECK_THROWS_AS(verifyTar(tar), ArchiveError);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("missing archive is an I/O error")
|
||||
{
|
||||
TempDir tmp;
|
||||
try {
|
||||
verifyTar(tmp.path() / "nope.tar");
|
||||
FAIL("expected ArchiveError");
|
||||
} catch (const ArchiveError& e) {
|
||||
CHECK(e.kind() == ArchiveError::Kind::Io);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("path traversal and links are refused")
|
||||
{
|
||||
TempDir tmp;
|
||||
const fs::path usb = tmp.path() / "usb";
|
||||
|
||||
SUBCASE("dot-dot")
|
||||
{
|
||||
const fs::path tar = tmp.path() / "evil.tar";
|
||||
makeTar(tar, {{"ok.txt", "1", false, ""}, {"../escape.txt", "x", false, ""}});
|
||||
CHECK_THROWS_AS(extractTar(tar, usb), ArchiveError);
|
||||
CHECK_FALSE(fs::exists(tmp.path() / "escape.txt"));
|
||||
}
|
||||
SUBCASE("absolute")
|
||||
{
|
||||
const fs::path tar = tmp.path() / "abs.tar";
|
||||
makeTar(tar, {{(tmp.path() / "abs.txt").string(), "x", false, ""}});
|
||||
CHECK_THROWS_AS(extractTar(tar, usb), ArchiveError);
|
||||
CHECK_FALSE(fs::exists(tmp.path() / "abs.txt"));
|
||||
}
|
||||
SUBCASE("symlinks are skipped")
|
||||
{
|
||||
const fs::path tar = tmp.path() / "link.tar";
|
||||
makeTar(tar, {{"link", "", false, "/etc/passwd"}, {"link2", "", false, "../.."}, {"f.txt", "ok", false, ""}});
|
||||
extractTar(tar, usb);
|
||||
CHECK_FALSE(fs::exists(fs::symlink_status(usb / "link")));
|
||||
CHECK(readAll(usb / "f.txt") == "ok");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("cancellation")
|
||||
{
|
||||
TempDir tmp;
|
||||
const fs::path tar = tmp.path() / "fw.tar";
|
||||
makeTar(tar, rccLike());
|
||||
CancelToken cancel;
|
||||
cancel.cancel();
|
||||
CHECK_THROWS_AS(extractTar(tar, tmp.path() / "usb", {}, &cancel), Cancelled);
|
||||
CHECK_THROWS_AS(verifyTar(tar, {}, &cancel), Cancelled);
|
||||
}
|
||||
Reference in new issue
Block a user