import hashlib import os import struct import tempfile import unittest from pathlib import Path from unittest.mock import patch from lightnest.binary import (BinaryFormatError, BinaryReader, Limits, SLOT_MAGIC, find_slots, index_elf, index_lnw1, inspect_binary, read_small_program) def blob(shape=(2,), data=None): name = b'weight' header = b'LNW1' + struct.pack('<10I', 1, 8, 1, 2, 32, 32, 16, 16, 1, 1) prefix = header + struct.pack('HHIIIIIHHHHHH', 2, 243, 1, 0, 0, 52, 0, 52, 32, 0, 40, 1, 0) section = struct.pack('>IIIIIIIIII', 0, 8, 3, 4096, 92, 1 << 30, 0, 0, 4, 0) self.write(header + section) with BinaryReader(self.path) as r: info = index_elf(r) self.assertEqual(info['bits'], 32) self.assertEqual(info['byteorder'], 'big') self.assertEqual(info['architecture'], 'riscv') self.assertFalse(info['sections'][0]['file_backed']) self.assertEqual(info['sections'][0]['nbytes'], 1 << 30) def test_unknown_100gb_default_reads_only_magic(self): with self.path.open('wb') as f: f.truncate(100 * 1024**3) real = os.pread reads = [] def guarded(fd, n, offset): reads.append((offset, n)) self.assertLessEqual(offset+n, 4) return real(fd, n, offset) with patch('os.pread', guarded): info = inspect_binary(self.path) self.assertEqual(reads, [(0, 4)]) self.assertEqual(info['weight_search']['scope'], 'not searched') self.assertFalse(info['slot_search']['whole_file']) def test_elf_huge_embedded_payload_prefix_budget(self): prefix, _ = blob((1,), b'') prefix = prefix[:-4] + struct.pack('