#include "openpu/archive.hpp" #include "openpu/usb.hpp" #include "test_util.hpp" #include #include #include using namespace openpu; namespace fs = std::filesystem; namespace { void makeTar(const fs::path& file, const std::vector>& 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(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 calls; std::string label; std::vector 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", ""}, {"rcc_update/a.bin", "AAAA"}}); const fs::path map = tmp.path() / "dl" / "map.tar"; makeTar(map, {{"map/data.bin", "MM"}, {"UpdateInfo.xml", ""}}); UsbWritePlan plan; plan.license = lic; plan.archives = {fw, map}; CHECK(plan.payloadSize() == 3 + 4 + 2); const fs::path usb = tmp.path() / "usb"; std::vector 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::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 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{"format", "eject"}); CHECK(usb.label == "MYSTICK"); CHECK(readAll(tmp.path() / "stick" / "rcc_update" / "a.bin") == "AAAA"); CHECK(steps == std::vector{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)"); }