Technician tool v0.6

Imported from the Google Drive folder 004-technician_tool/v0.6.
This commit is contained in:
kkallas committed 2026-09-28 17:31:03 +02:00
1 parent f5b32e1554
commit 22bd853ecf
30 files changed
+1521 -568

No files matched your search

+103
View File
@@ -0,0 +1,103 @@
"""FAT32/MBR layout: field-level checks against the FAT spec on an in-memory
disk, plus an independent FAT implementation (pyfatfs) mounting the result."""
import struct
import pytest
from kota_tech.sd import fat32
MiB, GiB = 1024**2, 1024**3
def make_disk(size: int, label="KOTA") -> tuple[bytearray, fat32.Fat32Layout]:
lay = fat32.plan(size)
disk = bytearray(b"\xAA" * size) # old garbage everywhere
zeros, writes = fat32.build(lay, label, 0x12345678, 0xCAFEBABE, size)
for off, n in zeros:
disk[off:off + n] = bytes(n)
for off, data in writes:
disk[off:off + len(data)] = data
return disk, lay
@pytest.fixture(scope="module")
def small():
return make_disk(96 * MiB)
def test_mbr_has_one_fat32_lba_partition(small):
disk, lay = small
assert disk[510:512] == b"\x55\xAA"
status, ptype, lba, count = struct.unpack_from("<B3xB3xII", disk, 0x1BE)
assert (status, ptype, lba) == (0, 0x0C, fat32.PART_START // 512)
assert count == lay.part_sectors
assert disk[0x1CE:0x1FE] == bytes(48) # other entries empty
assert struct.unpack_from("<I", disk, 0x1B8)[0] == 0xCAFEBABE
def test_boot_sector_fields(small):
disk, lay = small
b = disk[fat32.PART_START:fat32.PART_START + 512]
bps, spc, rsvd, nfats, root_ent, tot16, media, fat16 = struct.unpack_from("<HBHBHHBH", b, 11)
assert (bps, spc, rsvd, nfats, root_ent, tot16, media, fat16) == (512, lay.sectors_per_cluster, 32, 2, 0, 0, 0xF8, 0)
hidden, tot32, fatsz, _, _, root_clus, fsinfo, bkboot = struct.unpack_from("<IIIHHIHH", b, 28)
assert hidden == fat32.PART_START // 512 and tot32 == lay.part_sectors and fatsz == lay.fat_sectors
assert (root_clus, fsinfo, bkboot) == (2, 1, 6)
assert b[66] == 0x29 and b[71:82] == b"KOTA " and b[82:90] == b"FAT32 " and b[510:] == b"\x55\xAA"
assert disk[fat32.PART_START + 6 * 512:fat32.PART_START + 7 * 512] == b # backup boot sector
def test_fat_is_large_enough_and_clusters_valid(small):
_, lay = small
assert fat32.MIN_CLUSTERS <= lay.clusters
assert lay.fat_sectors * 512 // 4 >= lay.clusters + 2
assert lay.data_start + lay.clusters * lay.sectors_per_cluster <= lay.part_sectors
def test_fsinfo_and_fat_entries(small):
disk, lay = small
info = disk[fat32.PART_START + 512:fat32.PART_START + 1024]
assert struct.unpack_from("<I", info, 0)[0] == 0x41615252
assert struct.unpack_from("<III", info, 484) == (0x61417272, lay.clusters - 1, 3)
assert struct.unpack_from("<I", info, 508)[0] == 0xAA550000
for fat in range(2):
off = fat32.PART_START + (32 + fat * lay.fat_sectors) * 512
assert struct.unpack_from("<III", disk, off) == (0x0FFFFFF8, 0x0FFFFFFF, 0x0FFFFFFF)
assert disk[off + 12:off + lay.fat_sectors * 512] == bytes(lay.fat_sectors * 512 - 12)
def test_root_holds_only_the_label(small):
disk, lay = small
root = fat32.PART_START + lay.data_start * 512
assert disk[root:root + 11] == b"KOTA " and disk[root + 11] == 0x08
assert disk[root + 32:root + lay.sectors_per_cluster * 512] == bytes(lay.sectors_per_cluster * 512 - 32)
@pytest.mark.parametrize("size,spc", [(4 * GiB, 8), (15 * GiB, 16), (31_457_280_000, 32), (64 * GiB, 32)])
def test_real_card_sizes(size, spc):
lay = fat32.plan(size)
assert lay.sectors_per_cluster == spc
assert lay.part_sectors * 512 <= min(size - fat32.PART_START, 32 * GiB)
assert fat32.MIN_CLUSTERS <= lay.clusters < fat32.MAX_CLUSTERS
def test_too_small_card_refused():
with pytest.raises(ValueError):
fat32.plan(20 * MiB)
def test_independent_fat_implementation_mounts_it(small, tmp_path):
pyfat = pytest.importorskip("pyfatfs.PyFat")
disk, lay = small
img = tmp_path / "part.img"
img.write_bytes(bytes(disk[fat32.PART_START:fat32.PART_START + lay.part_sectors * 512]))
fs = pyfat.PyFat()
fs.open(str(img))
assert fs.fat_type == pyfat.PyFat.FAT_TYPE_FAT32
assert fs.bpb_header["BPB_TotSec32"] == lay.part_sectors
fs.close()
from pyfatfs.PyFatFS import PyFatFS
with PyFatFS(str(img)) as ffs: # write + read back a file like ENV.TXT
ffs.writebytes("/ENV.TXT", b"INFLUX_URL=x\n")
assert ffs.readbytes("/ENV.TXT") == b"INFLUX_URL=x\n"
+28
View File
@@ -244,3 +244,31 @@ def test_non_firmware_file_refused(tmp_path, image):
p.write_bytes(b"hello firmware team")
with pytest.raises(ImageError, match="not an ESP32 firmware"):
FirmwareLibrary(tmp_path / "lib").import_file(p, reference=image)
# ------------------------------------------- bundled developer build (v0.6)
from kota_tech.flash.image import bundled_images # noqa: E402
def test_bundled_developer_build_is_labelled_and_not_default():
imgs = bundled_images()
assert [i.origin for i in imgs] == ["factory", "developer"] # factory first
dev = imgs[1]
assert dev.built_by == "Krzysztof Kallas" and "built by Krzysztof Kallas" in dev.label()
assert dev.kind == "merged" and dev.app.elf_sha.startswith("c94ba0e9b")
assert dev.partition_table == imgs[0].partition_table # App-only safe on v1.0 boxes
assert [i.origin for i in factory_images_only()] == ["factory"]
def factory_images_only():
from kota_tech.flash.image import factory_images
return factory_images()
def test_import_records_who_built_it(image, tmp_path):
lib = FirmwareLibrary(tmp_path / "lib")
img = lib.import_file(_app_only_file(image, tmp_path), reference=image, built_by=" Anna Nowak ")
assert img.built_by == "Anna Nowak" and "built by Anna Nowak" in img.label()
again, _ = lib.images(image)
assert again[0].built_by == "Anna Nowak"
+42
View File
@@ -0,0 +1,42 @@
"""Matter onboarding payload, checked against the real sticker of the bench box
(QR decoded from matter_commissioning_qr.png: MT:Y.K9042C00KA0648G00)."""
from pathlib import Path
import segno
from kota_tech.core.logfile import read_log_file
from kota_tech.matter.payload import SetupPayload, payload_from_log, verhoeff_check
STICKER_QR = "MT:Y.K9042C00KA0648G00"
STICKER_MANUAL = "34970112332" # printed as 3497-011-2332
def test_default_payload_matches_sticker():
p = SetupPayload()
assert p.qr_text() == STICKER_QR
assert p.manual_code() == STICKER_MANUAL
assert p.manual_code_pretty() == "3497-011-2332"
def test_payload_from_real_boot_log():
lines = [ln.text for ln in read_log_file(Path(__file__).parent / "fixtures" / "serial_log_20260917_162548.txt")]
p = payload_from_log(lines)
assert p.from_log and (p.vendor_id, p.product_id, p.discriminator) == (0xFFF1, 0x8000, 3840)
assert p.qr_text() == STICKER_QR
def test_other_discriminator_changes_both_codes():
p = SetupPayload(discriminator=1234)
assert p.qr_text() != STICKER_QR and p.manual_code() != STICKER_MANUAL
assert p.qr_text().startswith("MT:") and len(p.qr_text()) == 3 + 19
def test_verhoeff():
assert verhoeff_check("3497011233") == 2
assert verhoeff_check("236") == 3 # textbook example
def test_qr_renders():
qr = segno.make(STICKER_QR, error="m")
assert qr.symbol_size(border=0)[0] >= 21
+23 -10
View File
@@ -5,9 +5,12 @@ import plistlib
import pytest
from kota_tech.sd.disks import MAX_CARD_BYTES, Disk, parse_macos, parse_windows
import sys
from pathlib import Path
from kota_tech.sd.disks import MAX_CARD_BYTES, Disk, Volume, parse_macos, parse_windows
from kota_tech.sd.envfile import EnvConfig, parse_env
from kota_tech.sd.provision import check_card, windows_format_script, write_env
from kota_tech.sd.provision import check_card, win_format_command, write_env
URL = "https://influx.kota-app.com/api/v2/write?bucket=sensorbox&org=kota"
TOKEN = "T" * 86 + "==" # shape of an InfluxDB token, not a real one
@@ -108,15 +111,25 @@ def test_parse_macos_excludes_system_disk():
# -------------------------------------------------------- format script
def test_windows_script_rechecks_disk_before_erasing(tmp_path):
def test_elevated_helper_command(tmp_path):
disk = Disk("5", "SDXC Card", 63_864_569_856, "SD")
ps = windows_format_script(disk, tmp_path / "r.json")
erase = ps.index("Clear-Disk")
for guard in ("IsSystem -or $d.IsBoot", "'MMC','SD','USB'", "-ne 63864569856"):
assert guard in ps and ps.index(guard) < erase
assert "-PartitionStyle MBR" in ps and "-FileSystem FAT32" in ps
assert str(32 * 1024**3) in ps # FAT32 partition capped at 32 GB
assert "NewFileSystemLabel 'KOTA'" in ps
prog, params, cwd = win_format_command(disk, tmp_path / "r.json")
assert params.startswith("-m kota_tech.sd.win_format --disk 5 --size 63864569856 --label KOTA")
assert (Path(cwd) / "kota_tech").is_dir() # helper is importable from there
@pytest.mark.skipif(sys.platform != "win32", reason="Windows formatter")
@pytest.mark.parametrize("disks,msg", [
([], "not a USB/SD/MMC"), # disk vanished / internal
([Disk("5", "SD", 1_000, "SD")], "changed since it was selected"), # swapped card
([Disk("5", "SD", 31_457_280_000, "SD", [Volume("C:\\", "NTFS", "")])], "Windows drive"),
])
def test_formatter_guard_refuses_before_writing(monkeypatch, disks, msg):
from kota_tech.sd import win_format
monkeypatch.setattr(win_format.win_disks, "list_disks", lambda buses: disks)
monkeypatch.setattr(win_format, "RawDisk", lambda *a: pytest.fail("must not open the disk for writing"))
with pytest.raises(win_format.FormatError, match=msg):
win_format.format_card(5, 31_457_280_000, "KOTA", {})
def test_large_disks_are_not_cards():