usb: backend UDisks2 et écriture de la mise à jour sur clé
- API UsbBackend (liste, formatage FAT32, éjection) + implémentation UDisks2 via GDBus : disques USB amovibles uniquement (jamais HintSystem), table MBR + partition 0x0c à 1 Mio formatée vfat, montage, PowerOff ; autorisation déléguée à polkit (pas de root) - writeToUsb : vérification des archives AVANT effacement, formatage, licence dans license/, extraction fw puis carte, éjection - règles de l'app officielle : label assaini (11 car., majuscules, NEW), taille de clé minimale 2/4/8… Go avec marge de 10 % - CLI : usb-list, write-usb (confirmation YES), write-dir - tests : faux backend (ordre des étapes, archive corrompue => clé intacte), labels, tailles - tools/e2e-usb.sh : image loop via UDisks2 + contrôle sfdisk/fsck/mtools ; tools/dev-polkit.sh pour WSL/CI - validé de bout en bout avec le vrai firmware RCC 8,39 Go : MBR, FAT32, label, arborescence, fsck propre, en environ 20 s 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
543347a1e6
commit
6a7a2f1ee9
11 files changed
+1047
-8
No files matched your search
@@ -0,0 +1,152 @@
|
||||
#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)");
|
||||
}
|
||||
Reference in new issue
Block a user