github.com/ipld/go-ipld-prime@v0.21.0/codec/dagcbor/unmarshal_test.go (about) 1 package dagcbor 2 3 import ( 4 "runtime" 5 "strings" 6 "testing" 7 8 "github.com/ipld/go-ipld-prime/datamodel" 9 10 qt "github.com/frankban/quicktest" 11 12 "github.com/ipld/go-ipld-prime/node/basicnode" 13 ) 14 15 func TestFunBlocks(t *testing.T) { 16 t.Run("zero length link", func(t *testing.T) { 17 // This fixture has a zero length link -- not even the multibase byte (which dag-cbor insists must be zero) is there. 18 buf := strings.NewReader("\x8d\x8d\x97\xd8*@") 19 nb := basicnode.Prototype.Any.NewBuilder() 20 err := Decode(nb, buf) 21 qt.Assert(t, err, qt.Equals, ErrInvalidMultibase) 22 }) 23 t.Run("fuzz001", func(t *testing.T) { 24 // This fixture might cause an overly large allocation if you aren't careful to have resource budgets. 25 buf := strings.NewReader("\x9a\xff000") 26 nb := basicnode.Prototype.Any.NewBuilder() 27 err := Decode(nb, buf) 28 if runtime.GOARCH == "386" { 29 // TODO: fix refmt to properly handle 64-bit ints on 32-bit runtime 30 qt.Assert(t, err.Error(), qt.Equals, "cbor: positive integer is out of length") 31 } else { 32 qt.Assert(t, err, qt.Equals, ErrAllocationBudgetExceeded) 33 } 34 }) 35 t.Run("fuzz002", func(t *testing.T) { 36 // This fixture might cause an overly large allocation if you aren't careful to have resource budgets. 37 buf := strings.NewReader("\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9f\x9a\xff000") 38 nb := basicnode.Prototype.Any.NewBuilder() 39 err := Decode(nb, buf) 40 if runtime.GOARCH == "386" { 41 // TODO: fix refmt to properly handle 64-bit ints on 32-bit 42 qt.Assert(t, err.Error(), qt.Equals, "cbor: positive integer is out of length") 43 } else { 44 qt.Assert(t, err, qt.Equals, ErrAllocationBudgetExceeded) 45 } 46 }) 47 t.Run("fuzz003", func(t *testing.T) { 48 // This fixture might cause an overly large allocation if you aren't careful to have resource budgets. 49 buf := strings.NewReader("\x9f\x9f\x9f\x9f\x9f\x9f\x9f\xbb00000000") 50 nb := basicnode.Prototype.Any.NewBuilder() 51 err := Decode(nb, buf) 52 if runtime.GOARCH == "386" { 53 // TODO: fix refmt to properly handle 64-bit ints on 32-bit 54 qt.Assert(t, err.Error(), qt.Equals, "cbor: positive integer is out of length") 55 } else { 56 qt.Assert(t, err, qt.Equals, ErrAllocationBudgetExceeded) 57 } 58 }) 59 t.Run("undef", func(t *testing.T) { 60 // This fixture tests that we tolerate cbor's "undefined" token (even though it's noncanonical and you shouldn't use it), 61 // and that it becomes a null in the data model level. 62 buf := strings.NewReader("\xf7") 63 nb := basicnode.Prototype.Any.NewBuilder() 64 err := Decode(nb, buf) 65 qt.Assert(t, err, qt.IsNil) 66 qt.Assert(t, nb.Build().Kind(), qt.Equals, datamodel.Kind_Null) 67 }) 68 t.Run("extra bytes", func(t *testing.T) { 69 buf := strings.NewReader("\xa0\x00") 70 nb := basicnode.Prototype.Any.NewBuilder() 71 err := Decode(nb, buf) 72 qt.Assert(t, err, qt.Equals, ErrTrailingBytes) 73 }) 74 }