diff --git a/tests/unit/plugins/module_utils/test_ota_vectors.py b/tests/unit/plugins/module_utils/test_ota_vectors.py new file mode 100644 index 0000000..949abc8 --- /dev/null +++ b/tests/unit/plugins/module_utils/test_ota_vectors.py @@ -0,0 +1,17 @@ +"""tests/vectors/ota_vectors.json, which other implementations (meshcore-open) +test against, must match the Python code -- regenerate it when this fails.""" + +import importlib.util +import json +import os + +VECTORS = os.path.join(os.path.dirname(__file__), "..", "..", "..", "vectors") + + +def test_vectors_match_the_code(): + spec = importlib.util.spec_from_file_location("ota_vectors", os.path.join(VECTORS, "ota_vectors.py")) + gen = importlib.util.module_from_spec(spec) + spec.loader.exec_module(gen) + with open(os.path.join(VECTORS, "ota_vectors.json")) as f: + stored = json.load(f) + assert stored == json.loads(json.dumps(gen.build())), "regenerate: python3 tests/vectors/ota_vectors.py" diff --git a/tests/vectors/ota_vectors.json b/tests/vectors/ota_vectors.json new file mode 100644 index 0000000..a27da96 --- /dev/null +++ b/tests/vectors/ota_vectors.json @@ -0,0 +1,296 @@ +{ + "airtime": [ + { + "bw": 62.5, + "cr": 1, + "n": 48, + "secs": 0.381952, + "sf": 8 + }, + { + "bw": 62.5, + "cr": 1, + "n": 172, + "secs": 0.569856, + "sf": 7 + }, + { + "bw": 62.5, + "cr": 4, + "n": 150, + "secs": 0.778752, + "sf": 7 + }, + { + "bw": 125.0, + "cr": 1, + "n": 255, + "secs": 9.281536, + "sf": 12 + } + ], + "crc16": [ + { + "crc": 65535, + "hex": "" + }, + { + "crc": 57840, + "hex": "00" + }, + { + "crc": 10673, + "hex": "313233343536373839" + }, + { + "crc": 57070, + "hex": "030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c" + } + ], + "frame_len": { + "req": [ + { + "len": 22, + "payload": 2 + }, + { + "len": 150, + "payload": 139 + }, + { + "len": 182, + "payload": 160 + } + ], + "text": [ + { + "len": 22, + "text": "fw status" + }, + { + "len": 54, + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + } + ] + }, + "packages": [ + { + "base_sha": null, + "format": 1, + "hex": "4d434657010200000000000000000000490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000076312e31382e302d747468636330353938620000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f", + "image_sha": "490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb", + "name": "x-v3.mcfw", + "version": "v1.18.0-tthcc0598b" + }, + { + "base_sha": "fe7484101f1cad46e055d09488b5aa0a29d00e3172e4394df31fa8d9cc7744a3", + "format": 2, + "hex": "4d434657020200000000000000000000490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000076312e31382e302d747468636330353938620000000000000000000000000000fe7484101f1cad46e055d09488b5aa0a29d00e3172e4394df31fa8d9cc7744a300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f", + "image_sha": "490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb", + "name": "x-v3.from-v1.mcfw", + "version": "v1.18.0-tthcc0598b" + }, + { + "base_sha": "43a84e0150b8649544af685a5ee19c8254428cccc2b525ed6591a77efacd158a", + "format": 2, + "hex": "4d434657020200000000000000000000490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000076312e31382e302d74746863633035393862000000000000000000000000000043a84e0150b8649544af685a5ee19c8254428cccc2b525ed6591a77efacd158a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f", + "image_sha": "490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb", + "name": "x-v3.from-v2.mcfw", + "version": "v1.18.0-tthcc0598b" + } + ], + "parse_base": [ + { + "sha8": "490418bc", + "text": "> 490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb" + }, + { + "sha8": "490418bc", + "text": "> 490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb fresh 490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb" + }, + { + "sha8": null, + "text": "ERR: fw status|begin" + } + ], + "parse_missing": [ + { + "hex": "0100000000", + "state": 1, + "units": [] + }, + { + "hex": "01000003000500090010000300", + "state": 1, + "units": [ + 5, + 9, + 16 + ] + }, + { + "hex": "040000", + "state": null, + "units": null + }, + { + "hex": "03000000000000", + "state": 3, + "units": [] + } + ], + "parse_radio": [ + { + "preset": [ + 869.432, + 62.5, + 7, + 5 + ], + "text": "> 869.432,62.5,7,5" + }, + { + "preset": [ + 866.5, + 62.5, + 8, + 5 + ], + "text": "> 866.5,62.5,8,5" + }, + { + "preset": null, + "text": "Unknown command" + } + ], + "parse_status": [ + { + "base": [ + 456492, + "fe748410" + ], + "text": "> idle, batt 4137mV (min 3800), free 352256 B, base 456492 fe748410, delta" + }, + { + "base": null, + "text": "> failed: not newer than this image (pkg 1794864486, node 1794864486) [b2 d0 x0 t0]" + }, + { + "base": null, + "text": "> rx 12/86 units, batt 4100mV [b12 d0 x0 t0]" + }, + { + "base": null, + "text": "Unknown command" + } + ], + "pick": [ + { + "base": "fe748410", + "full_ok": false, + "kind": "delta", + "package": "x-v3.from-v1.mcfw" + }, + { + "base": "43a84e01", + "full_ok": false, + "kind": "delta", + "package": "x-v3.from-v2.mcfw" + }, + { + "base": "490418bc", + "full_ok": false, + "kind": "current", + "package": "x-v3.mcfw" + }, + { + "base": "00000000", + "full_ok": false, + "kind": "none", + "package": null + }, + { + "base": "00000000", + "full_ok": true, + "kind": "full", + "package": "x-v3.mcfw" + } + ], + "preset": [ + { + "fmt": "869.525,62.5,7,5", + "preset": [ + 869.525, + 62.5, + 7, + 5 + ], + "text": "869.525,62.5,7,5" + }, + { + "fmt": "866,62.5,7,5", + "preset": [ + 866.0, + 62.5, + 7, + 5 + ], + "text": "866,62.5,7,5" + }, + { + "fmt": "869.432,62.5,8,8", + "preset": [ + 869.432, + 62.5, + 8, + 8 + ], + "text": "869.432,62.5,8,8" + } + ], + "preset_bad": [ + "869.525,62.5,7", + "869.525,62.5,13,5", + "869.525,62.5,7,4", + "x,62.5,7,5", + "869.525,600,7,5" + ], + "text_missing": [ + { + "offsets": [], + "text": "> none" + }, + { + "offsets": [ + 0, + 96, + 2688 + ], + "text": "> 0 96 2688" + }, + { + "offsets": null, + "text": "ERR: not receiving" + } + ], + "w_payload": [ + { + "chunk": "030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c", + "hex": "f077010000000000806028030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c", + "off": 0, + "sid": 1 + }, + { + "chunk": "030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b7279", + "hex": "f077efbe000b00005bc363030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b7279", + "off": 2816, + "sid": 48879 + }, + { + "chunk": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "hex": "f07768c78000000080e1ee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "off": 128, + "sid": 51048 + } + ] +} diff --git a/tests/vectors/ota_vectors.py b/tests/vectors/ota_vectors.py new file mode 100755 index 0000000..3353252 --- /dev/null +++ b/tests/vectors/ota_vectors.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python3 +"""Test vectors of the OTA protocol, for implementations in other languages. + + python3 tests/vectors/ota_vectors.py > tests/vectors/ota_vectors.json + +meshcore-open (Dart) implements the same transfer as module_utils/meshcore_ota +and meshcore_ota_engine; it checks itself against this JSON (a copy in its +test/ directory). tests/unit/.../test_ota_vectors.py fails when the JSON no +longer matches the Python code -- regenerate it and copy it over. +""" + +import json +import os +import sys + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.abspath(os.path.join(HERE, "..", "..", "..", "..", ".."))) + +from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_ota import ( + Package, + crc16, + fmt_preset, + lora_airtime, + parse_base, + parse_missing, + parse_preset, + parse_radio, + parse_status, + pick, + req_frame_len, + text_frame_len, + text_missing, + w_payload, +) + +IMG_OLD = "fe7484101f1cad46e055d09488b5aa0a29d00e3172e4394df31fa8d9cc7744a3" +IMG_MID = "43a84e0150b8649544af685a5ee19c8254428cccc2b525ed6591a77efacd158a" +IMG_NEW = "490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb" + + +def mcfw(fmt, image, base=None, version="v1.18.0-tthcc0598b"): + head = bytearray(256) + head[0:4] = b"MCFW" + head[4] = fmt + head[5] = 2 + head[16:48] = bytes.fromhex(image) + head[128 : 128 + len(version)] = version.encode() + if base: + head[160:192] = bytes.fromhex(base) + return bytes(head) + bytes(range(64)) + + +def build(): + release = [ + ("x-v3.mcfw", mcfw(1, IMG_NEW)), + ("x-v3.from-v1.mcfw", mcfw(2, IMG_NEW, IMG_OLD)), + ("x-v3.from-v2.mcfw", mcfw(2, IMG_NEW, IMG_MID)), + ] + pkgs = [Package(n, d) for n, d in release] + + def picked(base, full_ok): + kind, p = pick(pkgs, base, full_ok) + return {"base": base, "full_ok": full_ok, "kind": kind, "package": p.name if p else None} + + chunk = bytes((i * 7 + 3) & 0xFF for i in range(128)) + return { + "packages": [ + { + "name": n, + "hex": d.hex(), + "format": p.format, + "image_sha": p.image_sha, + "base_sha": p.base_sha, + "version": p.version, + } + for (n, d), p in zip(release, pkgs) + ], + "pick": [ + picked(IMG_OLD[:8], False), + picked(IMG_MID[:8], False), + picked(IMG_NEW[:8], False), + picked("00000000", False), + picked("00000000", True), + ], + "crc16": [{"hex": h, "crc": crc16(bytes.fromhex(h))} for h in ("", "00", "313233343536373839", chunk.hex())], + "w_payload": [ + {"sid": sid, "off": off, "chunk": c.hex(), "hex": w_payload(sid, off, c).hex()} + for sid, off, c in ((1, 0, chunk), (0xBEEF, 2816, chunk[:91]), (51048, 128, b"\x00" * 128)) + ], + "parse_status": [ + {"text": t, "base": list(parse_status(t)) if parse_status(t) else None} + for t in ( + "> idle, batt 4137mV (min 3800), free 352256 B, base 456492 fe748410, delta", + "> failed: not newer than this image (pkg 1794864486, node 1794864486) [b2 d0 x0 t0]", + "> rx 12/86 units, batt 4100mV [b12 d0 x0 t0]", + "Unknown command", + ) + ], + "parse_base": [ + {"text": t, "sha8": (parse_base(t) or (None, None))[1]} + for t in ("> " + IMG_NEW, "> " + IMG_NEW + " fresh " + IMG_NEW, "ERR: fw status|begin") + ], + "parse_missing": [ + { + "hex": h, + "state": (parse_missing(bytes.fromhex(h)) or (None, None))[0], + "units": (parse_missing(bytes.fromhex(h)) or (None, None))[1], + } + for h in ("0100000000", "01000003000500090010000300", "040000", "03000000000000") + ], + "text_missing": [ + {"text": t, "offsets": text_missing(t)} for t in ("> none", "> 0 96 2688", "ERR: not receiving") + ], + "preset": [ + {"text": t, "preset": list(parse_preset(t)), "fmt": fmt_preset(parse_preset(t))} + for t in ("869.525,62.5,7,5", "866,62.5,7,5", "869.432,62.5,8,8") + ], + "preset_bad": ["869.525,62.5,7", "869.525,62.5,13,5", "869.525,62.5,7,4", "x,62.5,7,5", "869.525,600,7,5"], + "parse_radio": [ + {"text": t, "preset": list(parse_radio(t)) if parse_radio(t) else None} + for t in ("> 869.432,62.5,7,5", "> 866.5,62.5,8,5", "Unknown command") + ], + "airtime": [ + {"n": n, "sf": sf, "bw": bw, "cr": cr, "secs": round(lora_airtime(n, sf, bw, cr), 9)} + for n, sf, bw, cr in ((48, 8, 62.5, 1), (172, 7, 62.5, 1), (150, 7, 62.5, 4), (255, 12, 125.0, 1)) + ], + "frame_len": { + "req": [{"payload": n, "len": req_frame_len(n)} for n in (2, 139, 160)], + "text": [{"text": t, "len": text_frame_len(t)} for t in ("fw status", "x" * 40)], + }, + } + + +if __name__ == "__main__": + print(json.dumps(build(), indent=1, sort_keys=True))