#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later from __future__ import annotations import argparse,struct,sys,unittest from pathlib import Path P=argparse.ArgumentParser();P.add_argument("--root",type=Path,required=True);R=P.parse_args().root;sys.path.insert(0,str(R/"tools")) from phase10dq_inventory_protocol import ProtocolError,parse_stream # noqa:E402 F=struct.Struct("<8sIIiiIIQ24s");E=struct.Struct("<256sIIIIQQq24s") def frame(kind,count=0,transferred=0,status=0):return F.pack(b"CHI10DQ1",1,kind,status,0,count,0,transferred,bytes(24)) def entry(name="eboot.bin"): raw=name.encode();return E.pack(raw+bytes(256-len(raw)),len(raw),8,0o100555,0,123,9,10,bytes(24)) class Tests(unittest.TestCase): def test_success(self): value=parse_stream(frame(1)+entry()+frame(2,1,320));self.assertEqual(value[0].name,"eboot.bin");self.assertEqual(value[0].size,123) def test_truncated(self): with self.assertRaises(ProtocolError):parse_stream(frame(1)+entry()) def test_target_error(self): with self.assertRaisesRegex(ProtocolError,"target error"):parse_stream(frame(3,status=1)+frame(2)) def test_traversal_rejected(self): with self.assertRaises(ProtocolError):parse_stream(frame(1)+entry("../x")+frame(2,1,320)) def test_terminal_count_rejected(self): with self.assertRaises(ProtocolError):parse_stream(frame(1)+entry()+frame(2,2,320)) if __name__=="__main__":unittest.main(argv=[__file__])