#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later 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 phase10ds_metadata_protocol import ProtocolError,parse_stream # noqa:E402 F=struct.Struct("<8sIIiiIIQ24s");DATA=[b"a",b"{}",b"",b"crc"] def frame(k,index=0,length=0,total=0,status=0):return F.pack(b"CHM10DS1",1,k,status,0,index,length,total,bytes(24)) def stream(): raw=frame(1,4);total=0 for i,data in enumerate(DATA):raw+=frame(2,i,len(data),total)+data;total+=len(data) return raw+frame(3,4,0,total) class Tests(unittest.TestCase): def test_success(self):self.assertEqual(parse_stream(stream())["app.json"],b"{}") def test_truncated(self): with self.assertRaises(ProtocolError):parse_stream(stream()[:-1]) def test_wrong_index(self): raw=bytearray(stream());raw[64+24:64+28]=(3).to_bytes(4,"little") with self.assertRaises(ProtocolError):parse_stream(bytes(raw)) def test_target_error(self): with self.assertRaisesRegex(ProtocolError,"target error"):parse_stream(frame(4,status=1)+bytes(64)) if __name__=="__main__":unittest.main(argv=[__file__])