Files
opu/tests/test_storage.cpp
T
yann64andClaude Opus 5.5 543347a1e6 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
2026-11-28 19:00:21 +01:00

107 lines
3.4 KiB
C++

#include "openpu/storage.hpp"
#include "test_util.hpp"
#include <doctest/doctest.h>
using namespace openpu;
TEST_CASE("official vehicles.json (with BOM) is understood")
{
const std::string official = "\xEF\xBB\xBF"
R"({"VF3AAAAAAAAAAAAA1":{"name":"5008","type":"nac","wave":"path_guide_wave_4"},)"
R"("VR3AAAAAAAAAAAAA2":{"name":"e208","type":"rcc","wave":"path_guide_rcc",)"
R"("undeletedFiles":{"fw":"C:\\dl\\PSA_rcc.tar","license":"C:\\dl\\license_X.key"}}})";
const auto v = VehicleStore::parse(official);
REQUIRE(v.size() == 2);
CHECK(v[0].vin == "VF3AAAAAAAAAAAAA1");
CHECK(v[0].type == HeadUnitType::Nac);
CHECK(v[0].guideKey == "path_guide_wave_4");
CHECK(v[1].type == HeadUnitType::Rcc);
CHECK(v[1].pendingFirmware == "C:\\dl\\PSA_rcc.tar");
CHECK(v[1].pendingLicense == "C:\\dl\\license_X.key");
CHECK(v[1].pendingMap.empty());
}
TEST_CASE("vehicle store round trip")
{
TempDir tmp;
VehicleStore store(tmp.path() / "sub" / "vehicles.json");
store.load(); // missing file
CHECK(store.all().empty());
Vehicle a;
a.vin = "VF3AAAAAAAAAAAAA1";
a.name = "Ma 208";
a.type = HeadUnitType::Nac;
a.guideKey = "path_guide_wave_2";
store.upsert(a);
Vehicle b = a;
b.vin = "VR3AAAAAAAAAAAAA2";
b.type = HeadUnitType::CronyNonav;
b.pendingMap = "/dl/map.tar";
store.upsert(b);
a.name = "renamed";
store.upsert(a);
store.save();
VehicleStore again(store.file());
again.load();
REQUIRE(again.all().size() == 2);
REQUIRE(again.find("VF3AAAAAAAAAAAAA1") != nullptr);
CHECK(again.find("VF3AAAAAAAAAAAAA1")->name == "renamed");
CHECK(again.find("VR3AAAAAAAAAAAAA2")->type == HeadUnitType::CronyNonav);
CHECK(again.find("VR3AAAAAAAAAAAAA2")->pendingMap == "/dl/map.tar");
CHECK(again.remove("VF3AAAAAAAAAAAAA1"));
CHECK_FALSE(again.remove("VF3AAAAAAAAAAAAA1"));
CHECK(again.all().size() == 1);
}
TEST_CASE("corrupt vehicles file is an error, not silently emptied")
{
TempDir tmp;
writeAll(tmp.path() / "vehicles.json", "{broken");
VehicleStore store(tmp.path() / "vehicles.json");
CHECK_THROWS_AS(store.load(), StorageError);
}
TEST_CASE("settings round trip")
{
TempDir tmp;
const auto file = tmp.path() / "config.json";
Settings s = Settings::load(file);
CHECK(s.language.empty());
CHECK_FALSE(s.pending.has_value());
CHECK_FALSE(s.effectiveDownloadDir().empty());
s.language = "fr";
s.downloadDir = "/data/dl";
PendingDownload p;
p.vin = "VR3AAAAAAAAAAAAA2";
p.firmware = true;
p.firmwareFile = "PSA_rcc.tar";
p.licenseFile = "license_X.key";
s.pending = p;
s.save(file);
const Settings r = Settings::load(file);
CHECK(r.language == "fr");
CHECK(r.effectiveDownloadDir() == "/data/dl");
REQUIRE(r.pending.has_value());
CHECK(r.pending->firmware);
CHECK_FALSE(r.pending->map);
CHECK(r.pending->licenseFile == "license_X.key");
}
TEST_CASE("XDG paths")
{
// Only checks the shape: paths end with our application directory.
#ifdef __HAIKU__
CHECK(paths::configDir().filename() == "OpenPU");
CHECK(paths::defaultDownloadDir().filename() == "OpenPU downloads");
#else
CHECK(paths::configDir().filename() == "openpu");
CHECK(paths::defaultDownloadDir().filename() == "OpenPU");
#endif
}