#!/usr/bin/env python3 """Ota: staging, chunk bitmap, hash checks, commit and sessions. Runs in a temp directory.""" import hashlib import json import os import struct import tempfile import espnow_ota as o import file_hashes M = b"\x01\x02\x03\x04\x05\x06" def begin(sess, path, body): p = path.encode() return struct.pack("> 3)] & (1 << (i & 7)): r(chunk(11, body, i)) assert r(cmd(o.OP_END, 11))[1] == o.OK r(cmd(o.OP_COMMIT, 11)) assert open("lib/x.py", "rb").read() == body os.chdir(old) @in_tmp def test_commit_repeated_after_reset(): r = Rig() r.send_file(14, "a.py", BODY) assert r(cmd(o.OP_COMMIT, 14))[1] == o.OK fresh = Rig() # the driver reset before its reply arrived op, code, arg = fresh(cmd(o.OP_COMMIT, 14)) assert code == o.OK and fresh.resets == 0 assert fresh(cmd(o.OP_COMMIT, 15))[1] == o.NOSESS @in_tmp def test_empty_file(): r = Rig() assert r.send_file(13, "lib/pkg/__init__.py", b"") == o.OK r(cmd(o.OP_COMMIT, 13)) assert open("lib/pkg/__init__.py", "rb").read() == b"" @in_tmp def test_too_big_rejected(): r = Rig() pkt = bytearray(begin(12, "a.py", b"x")) struct.pack_into("