- interface gtkmm-4 : véhicules, ajout par VIN, mises à jour, dossier, téléchargement avec reprise et arrêt, choix de la clé, confirmation, écriture avec progression, fin avec guide d'installation ; travail dans un thread, résultats renvoyés via Glib::Dispatcher - préférences : dossier de téléchargement, langue (système par défaut), recherche automatique de nouvelle version d'OpenPU - recherche de nouvelle version (releases GitHub, format tags Gitea accepté) avec ouverture de la page dans le navigateur ; openpu-cli app-update - versions : project(VERSION) + git describe régénéré à chaque build, openpu-cli --version, À propos, User-Agent, CHANGELOG, tools/release.sh (étiquette annotée, push vers tous les remotes, release GitHub) - traductions gettext : textes anglais, fr.po complet (96 chaînes), extracteur de secours sans xgettext - core : module job (téléchargement licence/fw/carte), liens des guides PDF, ordre des véhicules conservé (ordered_json) - licence MIT (LICENSE, en-têtes SPDX) ; dépôt principal sur GitHub - validé dans WSLg : parcours complet jusqu'à l'écriture d'une clé loop (FAT32 propre, fichiers téléchargés supprimés) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
202 lines
6.3 KiB
C++
202 lines
6.3 KiB
C++
// SPDX-License-Identifier: MIT
|
|
#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);
|
|
}
|