|
| 1 | +#! /usr/bin/python3 |
| 2 | + |
| 3 | +##--------------------------------------------------------------------\ |
| 4 | +# hackrfpy 'tests/test_sigmf.py' |
| 5 | +# SigMF sidecar writer/reader. Pins the core namespace shape and the |
| 6 | +# core:extensions declaration for hackrf:* keys (strict SigMF validators |
| 7 | +# reject a file that uses a non-core namespace without declaring it). No |
| 8 | +# device needed. |
| 9 | +# |
| 10 | +# |
| 11 | +# Author(s): Lauren Linkous |
| 12 | +# Last Update: July 11, 2026 |
| 13 | +##--------------------------------------------------------------------\ |
| 14 | + |
| 15 | +import json |
| 16 | + |
| 17 | +from hackrfpy import write_sigmf_meta, read_sigmf_meta |
| 18 | + |
| 19 | + |
| 20 | +def test_core_fields_written(tmp_path): |
| 21 | + meta_path = write_sigmf_meta(str(tmp_path / "cap.iq"), 433.92e6, 8e6) |
| 22 | + meta = json.load(open(meta_path)) |
| 23 | + g = meta["global"] |
| 24 | + assert g["core:datatype"] == "ci8" |
| 25 | + assert g["core:sample_rate"] == 8e6 |
| 26 | + assert meta["captures"][0]["core:frequency"] == 433.92e6 |
| 27 | + |
| 28 | + |
| 29 | +def test_extensions_declared_when_gains_present(tmp_path): |
| 30 | + # hackrf:* keys require a core:extensions declaration to be spec-valid. |
| 31 | + meta_path = write_sigmf_meta(str(tmp_path / "cap.iq"), 100e6, 8e6, |
| 32 | + lna=24, vga=20, amp=True) |
| 33 | + g = json.load(open(meta_path))["global"] |
| 34 | + assert g["hackrf:lna_gain_db"] == 24 |
| 35 | + exts = g.get("core:extensions") |
| 36 | + assert isinstance(exts, list) and len(exts) == 1 |
| 37 | + assert exts[0]["name"] == "hackrf" |
| 38 | + assert exts[0]["optional"] is True |
| 39 | + assert "version" in exts[0] |
| 40 | + |
| 41 | + |
| 42 | +def test_extensions_absent_when_no_gains(tmp_path): |
| 43 | + # No hackrf:* keys -> no extension declaration (nothing to declare). |
| 44 | + meta_path = write_sigmf_meta(str(tmp_path / "cap.iq"), 100e6, 8e6) |
| 45 | + g = json.load(open(meta_path))["global"] |
| 46 | + assert "core:extensions" not in g |
| 47 | + assert not any(k.startswith("hackrf:") for k in g) |
| 48 | + |
| 49 | + |
| 50 | +def test_roundtrip_via_data_path(tmp_path): |
| 51 | + # read_sigmf_meta accepts the data path and maps it to the sidecar. |
| 52 | + data_path = str(tmp_path / "cap.iq") |
| 53 | + write_sigmf_meta(data_path, 915e6, 10e6, lna=16) |
| 54 | + meta = read_sigmf_meta(data_path) |
| 55 | + assert meta["global"]["hackrf:lna_gain_db"] == 16 |
| 56 | + assert meta["captures"][0]["core:frequency"] == 915e6 |
0 commit comments