Files
opu/tests/test_usb.cpp
T
yann64andClaude Opus 5.5 384aaf78c6 gtk: interface GTK4, préférences, versions, traductions, licence MIT
- 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
2026-11-28 19:54:04 +01:00

182 lines
6.2 KiB
C++

// SPDX-License-Identifier: MIT
#include "openpu/archive.hpp"
#include "openpu/usb.hpp"
#include "test_util.hpp"
#include <archive.h>
#include <archive_entry.h>
#include <doctest/doctest.h>
using namespace openpu;
namespace fs = std::filesystem;
namespace {
void makeTar(const fs::path& file, const std::vector<std::pair<std::string, std::string>>& files)
{
archive* a = archive_write_new();
archive_write_set_format_ustar(a);
REQUIRE(archive_write_open_filename(a, file.c_str()) == ARCHIVE_OK);
for (const auto& [name, content] : files) {
archive_entry* e = archive_entry_new();
archive_entry_set_pathname(e, name.c_str());
archive_entry_set_filetype(e, AE_IFREG);
archive_entry_set_perm(e, 0644);
archive_entry_set_size(e, static_cast<la_int64_t>(content.size()));
archive_write_header(a, e);
archive_write_data(a, content.data(), content.size());
archive_entry_free(e);
}
archive_write_close(a);
archive_write_free(a);
}
/// Backend that "formats" by wiping a directory, and records the calls.
class FakeUsb final : public UsbBackend {
public:
explicit FakeUsb(fs::path root) : root_(std::move(root)) {}
std::vector<std::string> calls;
std::string label;
std::vector<UsbDrive> list() override
{
UsbDrive d;
d.id = "fake";
d.device = "/dev/fake";
d.size = 8000000000ULL;
return {d};
}
fs::path formatFat32(const UsbDrive&, const std::string& l) override
{
calls.emplace_back("format");
label = l;
fs::remove_all(root_);
fs::create_directories(root_);
return root_;
}
void eject(const UsbDrive&) override { calls.emplace_back("eject"); }
private:
fs::path root_;
};
} // namespace
TEST_CASE("volume label sanitising matches the official app")
{
CHECK(sanitizeLabel("") == "NEW");
CHECK(sanitizeLabel(" ") == "NEW");
CHECK(sanitizeLabel("Clé USB 3.0") == "CLUSB30");
CHECK(sanitizeLabel("my_stick_name_long") == "MY_STICK_NA");
CHECK(sanitizeLabel("new") == "NEW");
}
TEST_CASE("minimum stick size is 2 GB times a power of two")
{
CHECK(minimumStickSize(0) == 2000000000ULL);
CHECK(minimumStickSize(1000000000ULL) == 2000000000ULL);
CHECK(minimumStickSize(1900000000ULL) == 4000000000ULL); // +10 % > 2 GB
CHECK(minimumStickSize(8391587840ULL + 10626) == 16000000000ULL); // real RCC firmware + license
CHECK(minimumStickSize(2728908800ULL + 10475161600ULL) == 16000000000ULL); // NAC fw + map
CHECK(withMargin(1000) == 1100);
}
TEST_CASE("payload is written: license folder then archives at the root")
{
TempDir tmp;
const fs::path lic = tmp.path() / "dl" / "license_ABC.key";
writeAll(lic, "KEY");
const fs::path fw = tmp.path() / "dl" / "fw.tar";
makeTar(fw, {{"UpdateInfo.xml", "<x/>"}, {"rcc_update/a.bin", "AAAA"}});
const fs::path map = tmp.path() / "dl" / "map.tar";
makeTar(map, {{"map/data.bin", "MM"}, {"UpdateInfo.xml", "<y/>"}});
UsbWritePlan plan;
plan.license = lic;
plan.archives = {fw, map};
CHECK(plan.payloadSize() == 3 + 4 + 2);
const fs::path usb = tmp.path() / "usb";
std::vector<UsbStep> steps;
writePayload(usb, plan, [&](const UsbProgress& p) {
if (steps.empty() || steps.back() != p.step)
steps.push_back(p.step);
});
CHECK(readAll(usb / "license" / "license_ABC.key") == "KEY");
CHECK(readAll(usb / "rcc_update" / "a.bin") == "AAAA");
CHECK(readAll(usb / "map" / "data.bin") == "MM");
CHECK_FALSE(fs::exists(usb / "UpdateInfo.xml"));
CHECK(steps == std::vector<UsbStep>{UsbStep::CopyingLicense, UsbStep::Extracting});
}
TEST_CASE("writeToUsb verifies before formatting, then writes and ejects")
{
TempDir tmp;
const fs::path fw = tmp.path() / "fw.tar";
makeTar(fw, {{"rcc_update/a.bin", "AAAA"}});
FakeUsb usb(tmp.path() / "stick");
const UsbDrive drive = usb.list().front();
UsbWritePlan plan;
plan.archives = {fw};
std::vector<UsbStep> steps;
writeToUsb(usb, drive, "my stick", plan, [&](const UsbProgress& p) {
if (steps.empty() || steps.back() != p.step)
steps.push_back(p.step);
});
CHECK(usb.calls == std::vector<std::string>{"format", "eject"});
CHECK(usb.label == "MYSTICK");
CHECK(readAll(tmp.path() / "stick" / "rcc_update" / "a.bin") == "AAAA");
CHECK(steps ==
std::vector<UsbStep>{UsbStep::Verifying, UsbStep::Formatting, UsbStep::Extracting, UsbStep::Ejecting, UsbStep::Done});
SUBCASE("a corrupt archive aborts before the stick is erased")
{
writeAll(tmp.path() / "stick" / "keep.txt", "precious");
writeAll(fw, std::string(2048, 'Z'));
usb.calls.clear();
CHECK_THROWS_AS(writeToUsb(usb, drive, "x", plan), ArchiveError);
CHECK(usb.calls.empty());
CHECK(readAll(tmp.path() / "stick" / "keep.txt") == "precious");
}
}
TEST_CASE("drive description")
{
UsbDrive d;
d.vendor = "SanDisk";
d.model = "Ultra";
d.device = "/dev/sdb";
d.size = 32000000000ULL;
CHECK(d.description() == "SanDisk Ultra (/dev/sdb, 32.0 GB)");
}
#include "openpu/job.hpp"
TEST_CASE("installation guide links")
{
CHECK(guideUrl("path_guide_rcc", "fr").find("map-software-rcc-fr") != std::string::npos);
CHECK(guideUrl("path_guide_wave_4", "fr").find("map-software-nac-fr") != std::string::npos);
CHECK(guideUrl("path_guide_wave_default", "de").find("map-software-nac-en") != std::string::npos);
CHECK(guideUrl("path_guide_aio", "en").find("map-software-iao") != std::string::npos);
CHECK(guideUrl("path_guide_crony_2", "fr") == "https://www.stellantis.com/");
CHECK(guideUrl("", "fr").empty());
}
TEST_CASE("downloaded files give the write plan in the official order")
{
DownloadedFiles f;
f.license = fs::path("/dl/license_X.key");
f.firmware = fs::path("/dl/fw.tar");
f.map = fs::path("/dl/map.tar");
auto plan = f.writePlan();
REQUIRE(plan.license.has_value());
CHECK(plan.archives == std::vector<fs::path>{"/dl/fw.tar", "/dl/map.tar"});
f.firmware.reset(); // map only: the license is not copied
plan = f.writePlan();
CHECK_FALSE(plan.license.has_value());
CHECK(plan.archives == std::vector<fs::path>{"/dl/map.tar"});
}