Files
sfile/tests/make_fixtures.py
cnmcdee a2106045a6 json: --json output and libsfile linkable library
- sfile --json: one JSON object per file (JSON Lines) carrying format,
  offsets, decoded fields and geotags when present; default CLI
  output byte-identical (verified against the pre-refactor binary).
- libsfile: src/sfile.h + src/sfile_lib.c expose the decoder
  (sfile_decode_file/mem, sfile_result_to_json, format table
  introspection); make lib builds libsfile.a and libsfile.so.1.1.0,
  make install installs them plus the header.
- tests/test_json.py: JSON parses and matches the text table on the
  same files (make test-json); tests/test_library.c + test_lib.sh:
  static and shared link test (make test-lib).
2026-12-02 09:28:39 +00:00

77 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""make_fixtures.py — build the small synthetic sample files the
--json and library tests decode. Shared by tests/test_json.py and
tests/test_lib.sh so both test the same bytes.
Every fixture is built from the format table in src/formats.h, so the
expected values below are the table's own ground truth.
"""
import os
import struct
import sys
def build_fixtures(outdir):
os.makedirs(outdir, exist_ok=True)
paths = {}
# PNG (task 564, big-endian): 800x600, 8-bit, colour type 2.
png = (bytes.fromhex("89504e470d0a1a0a") + struct.pack(">I", 13) + b"IHDR"
+ struct.pack(">II", 800, 600) + bytes([8, 2, 0, 0, 0]))
paths["png"] = _write(outdir, "sample.png", png)
# SPS track file (task 678, big-endian, extension-only: no magic).
# latitude 37.7749 / longitude -122.4194 at offsets 27 / 35.
sps = bytearray(161)
sps[0:11] = b"v1.0" + b"\x00" * 7
struct.pack_into(">d", sps, 11, 0.0) # start
struct.pack_into(">d", sps, 19, 3600.0) # end
struct.pack_into(">d", sps, 27, 37.7749) # latitude
struct.pack_into(">d", sps, 35, -122.4194) # longitude
struct.pack_into(">d", sps, 43, 100.0) # chart_max
struct.pack_into(">d", sps, 51, -10.0) # chart_min
struct.pack_into(">h", sps, 59, -8) # timezone
sps[61:72] = b"GPS" + b"\x00" * 8 # source
sps[72:93] = b"sfile-test" + b"\x00" * 11 # author
sps[93:114] = b"geo-fixture" + b"\x00" * 10 # name
sps[114:155] = b"San Francisco" + b"\x00" * 28 # location
struct.pack_into(">h", sps, 155, 2) # channels
struct.pack_into(">i", sps, 157, 0) # note_length
paths["sps"] = _write(outdir, "track.sps", bytes(sps))
# JSON document (task 334, text fallback: first key "name").
paths["json"] = _write(outdir, "doc.json",
b'{"name": "sfile", "version": 1}\n')
# XML document (task 839, text fallback: root <catalog version="2.0">).
paths["xml"] = _write(outdir, "doc.xml",
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<catalog version="2.0"><item/></catalog>\n')
# Plain text (task 742).
paths["txt"] = _write(outdir, "note.txt",
b"Hello, sfile.\nSecond line.\n")
# Unknown: no magic match, unknown extension.
paths["unknown"] = _write(outdir, "mystery.zzz", bytes(range(1, 65)))
# Empty PNG: identified by extension, every field beyond the window.
paths["empty_png"] = _write(outdir, "empty.png", b"")
# WEBM (task 805): magic known, FMT_UNKNOWN, no header layout.
paths["webm"] = _write(outdir, "clip.webm",
bytes.fromhex("1a45dfa3") + bytes(60))
# WAB (task 802): magic known, FMT_BINARY, no header layout.
paths["wab"] = _write(outdir, "addr.wab",
bytes.fromhex("9ccbcb8d1375d211915800c04f7956a4")
+ bytes(48))
return paths
def _write(outdir, name, data):
path = os.path.join(outdir, name)
with open(path, "wb") as fh:
fh.write(data)
return path
if __name__ == "__main__":
outdir = sys.argv[1] if len(sys.argv) > 1 else "."
for key, path in build_fixtures(outdir).items():
print(f"{key}: {path}")