"""Essais documentaires hors équipement, avec fichiers synthétiques en mémoire.""" from hashlib import sha256 from io import BytesIO import json from struct import pack, unpack from zipfile import ZipFile from zlib import crc32, compress, decompress def png_chunk(kind, data): return pack(">I", len(data)) + kind + data + pack(">I", crc32(kind + data)) def image(width, height): header = pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) row = b"\0" + b"\x30\x65\x82" * width return b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", header) + png_chunk(b"IDAT", compress(row * height)) + png_chunk(b"IEND", b"") def dimensions(png): width, height = unpack(">II", png[16:24]) length = unpack(">I", png[33:37])[0] pixels = decompress(png[41:41 + length]) assert len(pixels) == height * (1 + 3 * width) return [width, height] def essai_photo(): sauvegarde = {"origine.png": image(1200, 800), "apercu.png": image(240, 160)} restauration = {"apercu.png": sauvegarde["apercu.png"]} apercu = dimensions(restauration["apercu.png"]) original_attendu = dimensions(sauvegarde["origine.png"]) empreinte_apercu = sha256(restauration["apercu.png"]).digest() == sha256(sauvegarde["apercu.png"]).digest() assert empreinte_apercu and apercu != original_attendu and "origine.png" not in restauration return {"apercu_decodable_px": apercu, "original_attendu_px": original_attendu, "empreinte_apercu_identique": empreinte_apercu, "original_restitue": "origine.png" in restauration} def archive(machine, version, fichiers): support = BytesIO() with ZipFile(support, "w") as copie: copie.writestr("identite.json", json.dumps({"cible": machine, "version": version})) for nom in fichiers: copie.writestr(nom, "Données fictives. Aucun programme ni paramètre machine utilisable.") return support.getvalue() def bilan_archive(contenu, cible, version, attendus): with ZipFile(BytesIO(contenu)) as copie: assert copie.testzip() is None indique = json.loads(copie.read("identite.json")) manquants = sorted(attendus - set(copie.namelist())) return {"zip_lisible": True, "cible_correspond": indique["cible"] == cible, "version_correspond": indique["version"] == version, "elements_absents": manquants} def essai_cnc(): categories = {"programme-exemple.txt", "inventaire-correcteurs.txt", "inventaire-commande.txt"} archives = { "programmes_seuls": archive("ATELIER-FICTIF-A", "2026-b", {"programme-exemple.txt"}), "autre_cible": archive("ATELIER-FICTIF-B", "2026-b", categories), "avant_retrofit": archive("ATELIER-FICTIF-A", "2026-a", categories), "dossier_de_reference": archive("ATELIER-FICTIF-A", "2026-b", categories), } resultat = {nom: bilan_archive(contenu, "ATELIER-FICTIF-A", "2026-b", categories) for nom, contenu in archives.items()} assert len(resultat["programmes_seuls"]["elements_absents"]) == 2 assert not resultat["autre_cible"]["cible_correspond"] assert not resultat["avant_retrofit"]["version_correspond"] assert resultat["dossier_de_reference"] == {"zip_lisible": True, "cible_correspond": True, "version_correspond": True, "elements_absents": []} return resultat if __name__ == "__main__": print(json.dumps({"photo": essai_photo(), "cnc": essai_cnc()}, ensure_ascii=False, indent=2))