@@ -67,9 +67,6 @@ func (test rawListTest[T]) run(t *testing.T) {
6767 // check iterator
6868 it := rl .ContentIterator ()
6969 i := 0
70- if count := it .Count (); count != test .length {
71- t .Fatalf ("iterator has wrong Count %d, want %d" , count , test .length )
72- }
7370 for it .Next () {
7471 var item T
7572 if err := DecodeBytes (it .Value (), & item ); err != nil {
@@ -153,9 +150,6 @@ func TestRawListEmpty(t *testing.T) {
153150 if ! bytes .Equal (b , unhex ("C0" )) {
154151 t .Fatalf ("empty RawList has wrong encoding %x" , b )
155152 }
156- if ! rl .Empty () {
157- t .Fatal ("list should be Empty" )
158- }
159153 if rl .Len () != 0 {
160154 t .Fatalf ("empty list has Len %d" , rl .Len ())
161155 }
@@ -225,6 +219,58 @@ func TestRawListAppend(t *testing.T) {
225219 }
226220}
227221
222+ func TestRawListAppendRaw (t * testing.T ) {
223+ var rl RawList [uint64 ]
224+
225+ if err := rl .AppendRaw (unhex ("01" )); err != nil {
226+ t .Fatal ("AppendRaw(01) failed:" , err )
227+ }
228+ if err := rl .AppendRaw (unhex ("820102" )); err != nil {
229+ t .Fatal ("AppendRaw(820102) failed:" , err )
230+ }
231+ if rl .Len () != 2 {
232+ t .Fatalf ("wrong Len %d after valid appends" , rl .Len ())
233+ }
234+
235+ if err := rl .AppendRaw (nil ); err == nil {
236+ t .Fatal ("AppendRaw(nil) should fail" )
237+ }
238+ if err := rl .AppendRaw (unhex ("0102" )); err == nil {
239+ t .Fatal ("AppendRaw(0102) should fail due to trailing bytes" )
240+ }
241+ if err := rl .AppendRaw (unhex ("8201" )); err == nil {
242+ t .Fatal ("AppendRaw(8201) should fail due to truncated value" )
243+ }
244+ if rl .Len () != 2 {
245+ t .Fatalf ("wrong Len %d after invalid appends, want 2" , rl .Len ())
246+ }
247+ }
248+
249+ func TestRawListDecodeInvalid (t * testing.T ) {
250+ tests := []struct {
251+ input string
252+ err error
253+ }{
254+ // Single item with non-canonical size (0x81 wrapping byte <= 0x7F).
255+ {input : "C28142" , err : ErrCanonSize },
256+ // Single item claiming more bytes than available in the list.
257+ {input : "C484020202" , err : ErrElemTooLarge },
258+ // Two items, second has non-canonical size.
259+ {input : "C3018142" , err : ErrCanonSize },
260+ // Two items, second claims more bytes than remain in the list.
261+ {input : "C401830202" , err : ErrElemTooLarge },
262+ // Item is a sub-list whose declared size exceeds available bytes.
263+ {input : "C3C40102" , err : ErrElemTooLarge },
264+ }
265+ for _ , test := range tests {
266+ var rl RawList [RawValue ]
267+ err := DecodeBytes (unhex (test .input ), & rl )
268+ if ! errors .Is (err , test .err ) {
269+ t .Errorf ("input %s: error mismatch: got %v, want %v" , test .input , err , test .err )
270+ }
271+ }
272+ }
273+
228274func TestCountValues (t * testing.T ) {
229275 tests := []struct {
230276 input string // note: spaces in input are stripped by unhex
0 commit comments