diff --git a/cassandra_test.go b/cassandra_test.go index df0c31ce5..088993c50 100644 --- a/cassandra_test.go +++ b/cassandra_test.go @@ -1900,8 +1900,8 @@ func TestVarint(t *testing.T) { } err := session.Query("SELECT test FROM varint_test").Scan(&result64) - if err == nil || strings.Index(err.Error(), "out of range") == -1 { - t.Errorf("expected out of range error since value is too big for int64") + if err == nil || strings.Index(err.Error(), "the data value should be in the int64 range") == -1 { + t.Errorf("expected out of range error since value is too big for int64, result:%d", result64) } // value not set in cassandra, leave bind variable empty diff --git a/marshal_test.go b/marshal_test.go index 86f629510..b6ea1fbca 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -362,55 +362,6 @@ var marshalTests = []struct { nil, nil, }, - { - NativeType{proto: 2, typ: TypeVarint}, - []byte("\x00"), - 0, - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeVarint}, - []byte("\x37\xE2\x3C\xEC"), - int32(937573612), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeVarint}, - []byte("\x37\xE2\x3C\xEC"), - big.NewInt(937573612), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeVarint}, - []byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["), - bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeVarint}, - []byte("\xC9v\x8D:\x86"), - big.NewInt(-234234234234), // From the iconara/cql-rb test suite - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeVarint}, - []byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"), - bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeVarint}, - []byte(nil), - nil, - nil, - unmarshalErrorf("can not unmarshal into non-pointer "), - }, { NativeType{proto: 2, typ: TypeInet}, []byte("\x7F\x00\x00\x01"), @@ -937,149 +888,6 @@ func TestMarshal_Decode(t *testing.T) { } } -func TestMarshalVarint(t *testing.T) { - varintTests := []struct { - Value interface{} - Marshaled []byte - Unmarshaled *big.Int - }{ - { - Value: int8(0), - Marshaled: []byte("\x00"), - Unmarshaled: big.NewInt(0), - }, - { - Value: uint8(255), - Marshaled: []byte("\x00\xFF"), - Unmarshaled: big.NewInt(255), - }, - { - Value: int8(-1), - Marshaled: []byte("\xFF"), - Unmarshaled: big.NewInt(-1), - }, - { - Value: big.NewInt(math.MaxInt32), - Marshaled: []byte("\x7F\xFF\xFF\xFF"), - Unmarshaled: big.NewInt(math.MaxInt32), - }, - { - Value: big.NewInt(int64(math.MaxInt32) + 1), - Marshaled: []byte("\x00\x80\x00\x00\x00"), - Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1), - }, - { - Value: big.NewInt(math.MinInt32), - Marshaled: []byte("\x80\x00\x00\x00"), - Unmarshaled: big.NewInt(math.MinInt32), - }, - { - Value: big.NewInt(int64(math.MinInt32) - 1), - Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"), - Unmarshaled: big.NewInt(int64(math.MinInt32) - 1), - }, - { - Value: math.MinInt64, - Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), - Unmarshaled: big.NewInt(math.MinInt64), - }, - { - Value: uint64(math.MaxInt64) + 1, - Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"), - Unmarshaled: bigintize("9223372036854775808"), - }, - { - Value: bigintize("2361183241434822606848"), // 2**71 - Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"), - Unmarshaled: bigintize("2361183241434822606848"), - }, - { - Value: bigintize("-9223372036854775809"), // -2**63 - 1 - Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), - Unmarshaled: bigintize("-9223372036854775809"), - }, - } - - for i, test := range varintTests { - data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value) - if err != nil { - t.Errorf("error marshaling varint: %v (test #%d)", err, i) - } - - if !bytes.Equal(test.Marshaled, data) { - t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i) - } - - binder := new(big.Int) - err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder) - if err != nil { - t.Errorf("error unmarshaling varint: %v (test #%d)", err, i) - } - - if test.Unmarshaled.Cmp(binder) != 0 { - t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i) - } - } - - varintUint64Tests := []struct { - Value interface{} - Marshaled []byte - Unmarshaled uint64 - }{ - { - Value: int8(0), - Marshaled: []byte("\x00"), - Unmarshaled: 0, - }, - { - Value: uint8(255), - Marshaled: []byte("\x00\xFF"), - Unmarshaled: 255, - }, - { - Value: big.NewInt(math.MaxInt32), - Marshaled: []byte("\x7F\xFF\xFF\xFF"), - Unmarshaled: uint64(math.MaxInt32), - }, - { - Value: big.NewInt(int64(math.MaxInt32) + 1), - Marshaled: []byte("\x00\x80\x00\x00\x00"), - Unmarshaled: uint64(int64(math.MaxInt32) + 1), - }, - { - Value: uint64(math.MaxInt64) + 1, - Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"), - Unmarshaled: 9223372036854775808, - }, - { - Value: uint64(math.MaxUint64), - Marshaled: []byte("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), - Unmarshaled: uint64(math.MaxUint64), - }, - } - - for i, test := range varintUint64Tests { - data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value) - if err != nil { - t.Errorf("error marshaling varint: %v (test #%d)", err, i) - } - - if !bytes.Equal(test.Marshaled, data) { - t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i) - } - - var binder uint64 - err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, &binder) - if err != nil { - t.Errorf("error unmarshaling varint to uint64: %v (test #%d)", err, i) - } - - if test.Unmarshaled != binder { - t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i) - } - } -} - func equalStringPointerSlice(leftList, rightList []*string) bool { if len(leftList) != len(rightList) { return false diff --git a/tests/serialization/marshal_7_varint_corrupt_test.go b/tests/serialization/marshal_7_varint_corrupt_test.go new file mode 100644 index 000000000..126b8c3d6 --- /dev/null +++ b/tests/serialization/marshal_7_varint_corrupt_test.go @@ -0,0 +1,127 @@ +package serialization_test + +import ( + "math/big" + "testing" + + "github.com/gocql/gocql" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" + "github.com/gocql/gocql/serialization/varint" +) + +func TestMarshalVarIntCorrupt(t *testing.T) { + type testSuite struct { + name string + marshal func(interface{}) ([]byte, error) + unmarshal func(bytes []byte, i interface{}) error + } + + tType := gocql.NewNativeType(4, gocql.TypeVarint, "") + + testSuites := [2]testSuite{ + { + name: "serialization.varint", + marshal: varint.Marshal, + unmarshal: varint.Unmarshal, + }, + { + name: "glob", + marshal: func(i interface{}) ([]byte, error) { + return gocql.Marshal(tType, i) + }, + unmarshal: func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + }, + }, + } + + for _, tSuite := range testSuites { + marshal := tSuite.marshal + unmarshal := tSuite.unmarshal + + t.Run(tSuite.name, func(t *testing.T) { + serialization.NegativeMarshalSet{ + Values: mod.Values{"1s2", "1s", "-1s", ".1", ",1", "0.1", "0,1"}.AddVariants(mod.All...), + }.Run("corrupt_vals", t, marshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x7f"), + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "", *big.NewInt(0), + }.AddVariants(mod.All...), + }.Run("corrupt_data+", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xff\x80"), + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "", *big.NewInt(0), + }.AddVariants(mod.All...), + }.Run("corrupt_data-", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x80"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_maxInt8+1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xff\x7f"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_minInt8-1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x80\x00"), + Values: mod.Values{int8(0), int16(0)}.AddVariants(mod.All...), + }.Run("small_type_maxInt16+1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xff\x7f\xff"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_minInt16-1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x80\x00\x00\x00"), + Values: mod.Values{int8(0), int16(0), int32(0)}.AddVariants(mod.All...), + }.Run("small_type_maxInt32+1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xff\x7f\xff\xff\xff\xff"), + Values: mod.Values{int8(0), int16(0), int32(0)}.AddVariants(mod.All...), + }.Run("small_type_minInt32-1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"), + Values: mod.Values{int8(0), int16(0), int32(0), int64(0), int(0)}.AddVariants(mod.All...), + }.Run("small_type_maxInt64+1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xff\x7f\xff\xff\xff\xff\xff\xff\xff\xff"), + Values: mod.Values{int8(0), int16(0), int32(0), int64(0), int(0)}.AddVariants(mod.All...), + }.Run("small_type_minInt64-1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x01\x00"), + Values: mod.Values{uint8(0)}.AddVariants(mod.All...), + }.Run("small_type_maxUint8+1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x01\x00\x00"), + Values: mod.Values{uint8(0), uint16(0)}.AddVariants(mod.All...), + }.Run("small_type_maxUint16+1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x01\x00\x00\x00\x00"), + Values: mod.Values{uint8(0), uint16(0), uint32(0)}.AddVariants(mod.All...), + }.Run("small_type_maxUint32+1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x01\x00\x00\x00\x00\x00\x00\x00\x00"), + Values: mod.Values{uint8(0), uint16(0), uint32(0), uint64(0), uint(0)}.AddVariants(mod.All...), + }.Run("small_type_maxUint64+1", t, unmarshal) + }) + } +} diff --git a/tests/serialization/marshal_7_varint_test.go b/tests/serialization/marshal_7_varint_test.go index caf9b3838..144ebfb65 100644 --- a/tests/serialization/marshal_7_varint_test.go +++ b/tests/serialization/marshal_7_varint_test.go @@ -7,342 +7,506 @@ import ( "github.com/gocql/gocql" "github.com/gocql/gocql/internal/tests/serialization" "github.com/gocql/gocql/internal/tests/serialization/mod" + "github.com/gocql/gocql/serialization/varint" ) -func TestMarshalVarInt(t *testing.T) { +func TestMarshalVarIntNew(t *testing.T) { + type testSuite struct { + name string + marshal func(interface{}) ([]byte, error) + unmarshal func(bytes []byte, i interface{}) error + } + tType := gocql.NewNativeType(4, gocql.TypeVarint, "") - marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } - unmarshal := func(bytes []byte, i interface{}) error { - return gocql.Unmarshal(tType, bytes, i) + testSuites := [2]testSuite{ + { + name: "serialization.varint", + marshal: varint.Marshal, + unmarshal: varint.Unmarshal, + }, + { + name: "glob", + marshal: func(i interface{}) ([]byte, error) { + return gocql.Marshal(tType, i) + }, + unmarshal: func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + }, + }, } - // unmarshal `custom string` unsupported - brokenCustomStrings := serialization.GetTypes(mod.String(""), (*mod.String)(nil)) - - // marshal "" (empty string) unsupported - // unmarshal nil value into (string)("0") - brokenEmptyStrings := serialization.GetTypes(string(""), mod.String("")) - - // marshal data, which equal math.MaxUint64, into uint and uit64 leads to an error - brokenUints := serialization.GetTypes(uint(0), mod.Uint64(0), mod.Uint(0), (*uint)(nil), (*mod.Uint64)(nil), (*mod.Uint)(nil)) - - // marshal and unmarshal all strings with data or value which out of range of int64 unsupported - brokenBigStrings := serialization.GetTypes(string(""), (*string)(nil), mod.String(""), (*mod.String)(nil)) - - serialization.PositiveSet{ - Data: nil, - Values: mod.Values{ - (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), - (*uint8)(nil), (*uint16)(nil), (*uint32)(nil), (*uint64)(nil), (*uint)(nil), - (*string)(nil), (*big.Int)(nil), "", - }.AddVariants(mod.CustomType), - BrokenMarshalTypes: brokenEmptyStrings, - BrokenUnmarshalTypes: brokenEmptyStrings, - }.Run("[nil]nullable", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: nil, - Values: mod.Values{ - int8(0), int16(0), int32(0), int64(0), int(0), - uint8(0), uint16(0), uint32(0), uint64(0), uint(0), - "0", big.Int{}, - }.AddVariants(mod.CustomType), - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("[nil]unmarshal", t, nil, unmarshal) - - serialization.PositiveSet{ - Data: make([]byte, 0), - Values: mod.Values{ - int8(0), int16(0), int32(0), int64(0), int(0), - uint8(0), uint16(0), uint32(0), uint64(0), uint(0), - "0", *big.NewInt(0), - }.AddVariants(mod.All...), - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("[]unmarshal", t, nil, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00"), - Values: mod.Values{ - int8(0), int16(0), int32(0), int64(0), int(0), - uint8(0), uint16(0), uint32(0), uint64(0), uint(0), - "0", *big.NewInt(0), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("zeros", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x01"), - Values: mod.Values{ - int8(1), int16(1), int32(1), int64(1), int(1), - uint8(1), uint16(1), uint32(1), uint64(1), uint(1), - "1", *big.NewInt(1), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("+1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\xff"), - Values: mod.Values{ - int8(-1), int16(-1), int32(-1), int64(-1), int(-1), - "-1", *big.NewInt(-1), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("-1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x7f"), - Values: mod.Values{ - int8(127), int16(127), int32(127), int64(127), int(127), - uint8(127), uint16(127), uint32(127), uint64(127), uint(127), - "127", *big.NewInt(127), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxInt8", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x80"), - Values: mod.Values{ - int8(-128), int16(-128), int32(-128), int64(-128), int(-128), - "-128", *big.NewInt(-128), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("minInt8", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\x80"), - Values: mod.Values{ - int16(128), int32(128), int64(128), int(128), - uint8(128), uint16(128), uint32(128), uint64(128), uint(128), - "128", *big.NewInt(128), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxInt8+1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\xff\x7f"), - Values: mod.Values{ - int16(-129), int32(-129), int64(-129), int(-129), - "-129", *big.NewInt(-129), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("minInt8-1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x7f\xff"), - Values: mod.Values{ - int16(32767), int32(32767), int64(32767), int(32767), - uint16(32767), uint32(32767), uint64(32767), uint(32767), - "32767", *big.NewInt(32767), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxInt16", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x80\x00"), - Values: mod.Values{ - int16(-32768), int32(-32768), int64(-32768), int(-32768), - "-32768", *big.NewInt(-32768), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("minInt16", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\x80\x00"), - Values: mod.Values{ - int32(32768), int64(32768), int(32768), - uint16(32768), uint32(32768), uint64(32768), uint(32768), - "32768", *big.NewInt(32768), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxInt16+1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\xff\x7f\xff"), - Values: mod.Values{ - int32(-32769), int64(-32769), int(-32769), - "-32769", *big.NewInt(-32769), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("minInt16-1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x7f\xff\xff\xff"), - Values: mod.Values{ - int32(2147483647), int64(2147483647), int(2147483647), - uint32(2147483647), uint64(2147483647), uint(2147483647), - "2147483647", *big.NewInt(2147483647), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxInt32", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x80\x00\x00\x00"), - Values: mod.Values{ - int32(-2147483648), int64(-2147483648), int(-2147483648), - "-2147483648", *big.NewInt(-2147483648), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("minInt32", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\x80\x00\x00\x00"), - Values: mod.Values{ - int64(2147483648), int(2147483648), - uint32(2147483648), uint64(2147483648), uint(2147483648), - "2147483648", *big.NewInt(2147483648), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxInt32+1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\xff\x7f\xff\xff\xff"), - Values: mod.Values{ - int64(-2147483649), int(-2147483649), - "-2147483649", *big.NewInt(-2147483649), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("minInt32-1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"), - Values: mod.Values{ - int64(9223372036854775807), int(9223372036854775807), - uint64(9223372036854775807), uint(9223372036854775807), - "9223372036854775807", *big.NewInt(9223372036854775807), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxInt64", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), - Values: mod.Values{ - int64(-9223372036854775808), int(-9223372036854775808), - "-9223372036854775808", *big.NewInt(-9223372036854775808), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("minInt64", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"), - Values: mod.Values{ - "9223372036854775808", *big.NewInt(0).Add(big.NewInt(1), big.NewInt(9223372036854775807)), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenBigStrings, - BrokenUnmarshalTypes: brokenBigStrings, - }.Run("maxInt64+1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\xff\x7f\xff\xff\xff\xff\xff\xff\xff"), - Values: mod.Values{ - "-9223372036854775809", *big.NewInt(0).Add(big.NewInt(-1), big.NewInt(-9223372036854775808)), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenBigStrings, - BrokenUnmarshalTypes: brokenBigStrings, - }.Run("minInt64-1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\xff"), - Values: mod.Values{ - uint8(255), uint16(255), uint32(255), uint64(255), uint(255), - int16(255), int32(255), int64(255), int(255), - "255", *big.NewInt(255), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxUint8", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x01\x00"), - Values: mod.Values{ - uint16(256), uint32(256), uint64(256), uint(256), - int16(256), int32(256), int64(256), int(256), - "256", *big.NewInt(256), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxUint8+1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\xff\xff"), - Values: mod.Values{ - uint16(65535), uint32(65535), uint64(65535), uint(65535), - int32(65535), int64(65535), int(65535), - "65535", *big.NewInt(65535), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxUint16", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x01\x00\x00"), - Values: mod.Values{ - uint32(65536), uint64(65536), uint(65536), - int32(65536), int64(65536), int(65536), - "65536", *big.NewInt(65536), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxUint16+1", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\xff\xff\xff\xff"), - Values: mod.Values{ - uint32(4294967295), uint64(4294967295), uint(4294967295), - int64(4294967295), int(4294967295), - "4294967295", *big.NewInt(4294967295), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxUint32", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x01\x00\x00\x00\x00"), - Values: mod.Values{ - uint64(4294967296), uint(4294967296), - int64(4294967296), int(4294967296), - "4294967296", *big.NewInt(4294967296), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenCustomStrings, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("maxUint32+1", t, marshal, unmarshal) - - bigMaxUint64 := new(big.Int) - bigMaxUint64.SetString("18446744073709551615", 10) - - serialization.PositiveSet{ - Data: []byte("\x00\xff\xff\xff\xff\xff\xff\xff\xff"), - Values: mod.Values{ - uint64(18446744073709551615), uint(18446744073709551615), - "18446744073709551615", *bigMaxUint64, - }.AddVariants(mod.All...), - BrokenMarshalTypes: append(brokenUints, brokenBigStrings...), - BrokenUnmarshalTypes: append(brokenUints, brokenBigStrings...), - }.Run("maxUint64", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x01\x00\x00\x00\x00\x00\x00\x00\x00"), - Values: mod.Values{ - "18446744073709551616", *big.NewInt(0).Add(bigMaxUint64, big.NewInt(1)), - }.AddVariants(mod.All...), - BrokenMarshalTypes: append(brokenUints, brokenBigStrings...), - BrokenUnmarshalTypes: append(brokenUints, brokenBigStrings...), - }.Run("maxUint64+1", t, marshal, unmarshal) + for _, tSuite := range testSuites { + marshal := tSuite.marshal + unmarshal := tSuite.unmarshal + + t.Run(tSuite.name, func(t *testing.T) { + + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{ + (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), + (*uint8)(nil), (*uint16)(nil), (*uint32)(nil), (*uint64)(nil), (*uint)(nil), + (*string)(nil), (*big.Int)(nil), "", + }.AddVariants(mod.CustomType), + }.Run("[nil]nullable", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "", big.Int{}, + }.AddVariants(mod.CustomType), + }.Run("[nil]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: make([]byte, 0), + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "0", *big.NewInt(0), + }.AddVariants(mod.All...), + }.Run("[]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00"), + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "0", *big.NewInt(0), + }.AddVariants(mod.All...), + }.Run("zeros", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x01"), + Values: mod.Values{ + int8(1), int16(1), int32(1), int64(1), int(1), + uint8(1), uint16(1), uint32(1), uint64(1), uint(1), + "1", *big.NewInt(1), + }.AddVariants(mod.All...), + }.Run("+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff"), + Values: mod.Values{ + int8(-1), int16(-1), int32(-1), int64(-1), int(-1), + "-1", *big.NewInt(-1), + }.AddVariants(mod.All...), + }.Run("-1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x7f"), + Values: mod.Values{ + int8(127), int16(127), int32(127), int64(127), int(127), + uint8(127), uint16(127), uint32(127), uint64(127), uint(127), + "127", *big.NewInt(127), + }.AddVariants(mod.All...), + }.Run("maxInt8", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80"), + Values: mod.Values{ + int8(-128), int16(-128), int32(-128), int64(-128), int(-128), + "-128", *big.NewInt(-128), + }.AddVariants(mod.All...), + }.Run("minInt8", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x80"), + Values: mod.Values{ + int16(128), int32(128), int64(128), int(128), + uint8(128), uint16(128), uint32(128), uint64(128), uint(128), + "128", *big.NewInt(128), + }.AddVariants(mod.All...), + }.Run("maxInt8+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\x7f"), + Values: mod.Values{ + int16(-129), int32(-129), int64(-129), int(-129), + "-129", *big.NewInt(-129), + }.AddVariants(mod.All...), + }.Run("minInt8-1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x7f\xff"), + Values: mod.Values{ + int16(32767), int32(32767), int64(32767), int(32767), + uint16(32767), uint32(32767), uint64(32767), uint(32767), + "32767", *big.NewInt(32767), + }.AddVariants(mod.All...), + }.Run("maxInt16", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80\x00"), + Values: mod.Values{ + int16(-32768), int32(-32768), int64(-32768), int(-32768), + "-32768", *big.NewInt(-32768), + }.AddVariants(mod.All...), + }.Run("minInt16", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x80\x00"), + Values: mod.Values{ + int32(32768), int64(32768), int(32768), + uint16(32768), uint32(32768), uint64(32768), uint(32768), + "32768", *big.NewInt(32768), + }.AddVariants(mod.All...), + }.Run("maxInt16+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\x7f\xff"), + Values: mod.Values{ + int32(-32769), int64(-32769), int(-32769), + "-32769", *big.NewInt(-32769), + }.AddVariants(mod.All...), + }.Run("minInt16-1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x7f\xff\xff"), + Values: mod.Values{ + int32(8388607), int64(8388607), int(8388607), + uint32(8388607), uint64(8388607), uint(8388607), + "8388607", *big.NewInt(8388607), + }.AddVariants(mod.All...), + }.Run("maxInt24", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80\x00\x00"), + Values: mod.Values{ + int32(-8388608), int64(-8388608), int(-8388608), + "-8388608", *big.NewInt(-8388608), + }.AddVariants(mod.All...), + }.Run("minInt24", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x80\x00\x00"), + Values: mod.Values{ + int64(8388608), int(8388608), + uint32(8388608), uint64(8388608), uint(8388608), + "8388608", *big.NewInt(8388608), + }.AddVariants(mod.All...), + }.Run("maxInt24+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\x7f\xff\xff"), + Values: mod.Values{ + int64(-8388609), int(-8388609), + "-8388609", *big.NewInt(-8388609), + }.AddVariants(mod.All...), + }.Run("minInt24-1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x7f\xff\xff\xff"), + Values: mod.Values{ + int32(2147483647), int64(2147483647), int(2147483647), + uint32(2147483647), uint64(2147483647), uint(2147483647), + "2147483647", *big.NewInt(2147483647), + }.AddVariants(mod.All...), + }.Run("maxInt32", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80\x00\x00\x00"), + Values: mod.Values{ + int32(-2147483648), int64(-2147483648), int(-2147483648), + "-2147483648", *big.NewInt(-2147483648), + }.AddVariants(mod.All...), + }.Run("minInt32", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x80\x00\x00\x00"), + Values: mod.Values{ + int64(2147483648), int(2147483648), + uint32(2147483648), uint64(2147483648), uint(2147483648), + "2147483648", *big.NewInt(2147483648), + }.AddVariants(mod.All...), + }.Run("maxInt32+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\x7f\xff\xff\xff"), + Values: mod.Values{ + int64(-2147483649), int(-2147483649), + "-2147483649", *big.NewInt(-2147483649), + }.AddVariants(mod.All...), + }.Run("minInt32-1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x7f\xff\xff\xff\xff"), + Values: mod.Values{ + int64(549755813887), int(549755813887), + uint64(549755813887), uint(549755813887), + "549755813887", *big.NewInt(549755813887), + }.AddVariants(mod.All...), + }.Run("maxInt40", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80\x00\x00\x00\x00"), + Values: mod.Values{ + int64(-549755813888), int(-549755813888), + "-549755813888", *big.NewInt(-549755813888), + }.AddVariants(mod.All...), + }.Run("minInt40", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x80\x00\x00\x00\x00"), + Values: mod.Values{ + int64(549755813888), int(549755813888), + uint64(549755813888), uint(549755813888), + "549755813888", *big.NewInt(549755813888), + }.AddVariants(mod.All...), + }.Run("maxInt40+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\x7f\xff\xff\xff\xff"), + Values: mod.Values{ + int64(-549755813889), int(-549755813889), + "-549755813889", *big.NewInt(-549755813889), + }.AddVariants(mod.All...), + }.Run("minInt40-1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x7f\xff\xff\xff\xff\xff"), + Values: mod.Values{ + int64(140737488355327), int(140737488355327), + uint64(140737488355327), uint(140737488355327), + "140737488355327", *big.NewInt(140737488355327), + }.AddVariants(mod.All...), + }.Run("maxInt48", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80\x00\x00\x00\x00\x00"), + Values: mod.Values{ + int64(-140737488355328), int(-140737488355328), + "-140737488355328", *big.NewInt(-140737488355328), + }.AddVariants(mod.All...), + }.Run("minInt48", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x80\x00\x00\x00\x00\x00"), + Values: mod.Values{ + int64(140737488355328), int(140737488355328), + uint64(140737488355328), uint(140737488355328), + "140737488355328", *big.NewInt(140737488355328), + }.AddVariants(mod.All...), + }.Run("maxInt48+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\x7f\xff\xff\xff\xff\xff"), + Values: mod.Values{ + int64(-140737488355329), int(-140737488355329), + "-140737488355329", *big.NewInt(-140737488355329), + }.AddVariants(mod.All...), + }.Run("minInt48-1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x7f\xff\xff\xff\xff\xff\xff"), + Values: mod.Values{ + int64(36028797018963967), int(36028797018963967), + uint64(36028797018963967), uint(36028797018963967), + "36028797018963967", *big.NewInt(36028797018963967), + }.AddVariants(mod.All...), + }.Run("maxInt56", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80\x00\x00\x00\x00\x00\x00"), + Values: mod.Values{ + int64(-36028797018963968), int(-36028797018963968), + "-36028797018963968", *big.NewInt(-36028797018963968), + }.AddVariants(mod.All...), + }.Run("minInt56", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x80\x00\x00\x00\x00\x00\x00"), + Values: mod.Values{ + int64(36028797018963968), int(36028797018963968), + uint64(36028797018963968), uint(36028797018963968), + "36028797018963968", *big.NewInt(36028797018963968), + }.AddVariants(mod.All...), + }.Run("maxInt56+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\x7f\xff\xff\xff\xff\xff\xff"), + Values: mod.Values{ + int64(-36028797018963969), int(-36028797018963969), + "-36028797018963969", *big.NewInt(-36028797018963969), + }.AddVariants(mod.All...), + }.Run("minInt56-1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"), + Values: mod.Values{ + int64(9223372036854775807), int(9223372036854775807), + uint64(9223372036854775807), uint(9223372036854775807), + "9223372036854775807", *big.NewInt(9223372036854775807), + }.AddVariants(mod.All...), + }.Run("maxInt64", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), + Values: mod.Values{ + int64(-9223372036854775808), int(-9223372036854775808), + "-9223372036854775808", *big.NewInt(-9223372036854775808), + }.AddVariants(mod.All...), + }.Run("minInt64", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"), + Values: mod.Values{ + "9223372036854775808", *big.NewInt(0).Add(big.NewInt(1), big.NewInt(9223372036854775807)), + }.AddVariants(mod.All...), + }.Run("maxInt64+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\x7f\xff\xff\xff\xff\xff\xff\xff"), + Values: mod.Values{ + "-9223372036854775809", *big.NewInt(0).Add(big.NewInt(-1), big.NewInt(-9223372036854775808)), + }.AddVariants(mod.All...), + }.Run("minInt64-1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\xff"), + Values: mod.Values{ + uint8(255), uint16(255), uint32(255), uint64(255), uint(255), + int16(255), int32(255), int64(255), int(255), + "255", *big.NewInt(255), + }.AddVariants(mod.All...), + }.Run("maxUint8", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x01\x00"), + Values: mod.Values{ + uint16(256), uint32(256), uint64(256), uint(256), + int16(256), int32(256), int64(256), int(256), + "256", *big.NewInt(256), + }.AddVariants(mod.All...), + }.Run("maxUint8+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\xff\xff"), + Values: mod.Values{ + uint16(65535), uint32(65535), uint64(65535), uint(65535), + int32(65535), int64(65535), int(65535), + "65535", *big.NewInt(65535), + }.AddVariants(mod.All...), + }.Run("maxUint16", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x01\x00\x00"), + Values: mod.Values{ + uint32(65536), uint64(65536), uint(65536), + int32(65536), int64(65536), int(65536), + "65536", *big.NewInt(65536), + }.AddVariants(mod.All...), + }.Run("maxUint16+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\xff\xff\xff"), + Values: mod.Values{ + uint32(16777215), uint64(16777215), uint(16777215), + int32(16777215), int64(16777215), int(16777215), + "16777215", *big.NewInt(16777215), + }.AddVariants(mod.All...), + }.Run("maxUint24", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x01\x00\x00\x00"), + Values: mod.Values{ + uint32(16777216), uint64(16777216), uint(16777216), + int32(16777216), int64(16777216), int(16777216), + "16777216", *big.NewInt(16777216), + }.AddVariants(mod.All...), + }.Run("maxUint24+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\xff\xff\xff\xff"), + Values: mod.Values{ + uint32(4294967295), uint64(4294967295), uint(4294967295), + int64(4294967295), int(4294967295), + "4294967295", *big.NewInt(4294967295), + }.AddVariants(mod.All...), + }.Run("maxUint32", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x01\x00\x00\x00\x00"), + Values: mod.Values{ + uint64(4294967296), uint(4294967296), + int64(4294967296), int(4294967296), + "4294967296", *big.NewInt(4294967296), + }.AddVariants(mod.All...), + }.Run("maxUint32+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\xff\xff\xff\xff\xff"), + Values: mod.Values{ + uint64(1099511627775), uint(1099511627775), + int64(1099511627775), int(1099511627775), + "1099511627775", *big.NewInt(1099511627775), + }.AddVariants(mod.All...), + }.Run("maxUint40", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x01\x00\x00\x00\x00\x00"), + Values: mod.Values{ + uint64(1099511627776), uint(1099511627776), + int64(1099511627776), int(1099511627776), + "1099511627776", *big.NewInt(1099511627776), + }.AddVariants(mod.All...), + }.Run("maxUint40+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\xff\xff\xff\xff\xff\xff"), + Values: mod.Values{ + uint64(281474976710655), uint(281474976710655), + int64(281474976710655), int(281474976710655), + "281474976710655", *big.NewInt(281474976710655), + }.AddVariants(mod.All...), + }.Run("maxUint48", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x01\x00\x00\x00\x00\x00\x00"), + Values: mod.Values{ + uint64(281474976710656), uint(281474976710656), + int64(281474976710656), int(281474976710656), + "281474976710656", *big.NewInt(281474976710656), + }.AddVariants(mod.All...), + }.Run("maxUint48+1", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\xff\xff\xff\xff\xff\xff\xff"), + Values: mod.Values{ + uint64(72057594037927935), uint(72057594037927935), + int64(72057594037927935), int(72057594037927935), + "72057594037927935", *big.NewInt(72057594037927935), + }.AddVariants(mod.All...), + }.Run("maxUint56", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x01\x00\x00\x00\x00\x00\x00\x00"), + Values: mod.Values{ + uint64(72057594037927936), uint(72057594037927936), + int64(72057594037927936), int(72057594037927936), + "72057594037927936", *big.NewInt(72057594037927936), + }.AddVariants(mod.All...), + }.Run("maxUint56+1", t, marshal, unmarshal) + + bigMaxUint64 := new(big.Int) + bigMaxUint64.SetString("18446744073709551615", 10) + + serialization.PositiveSet{ + Data: []byte("\x00\xff\xff\xff\xff\xff\xff\xff\xff"), + Values: mod.Values{ + uint64(18446744073709551615), uint(18446744073709551615), + "18446744073709551615", *bigMaxUint64, + }.AddVariants(mod.All...), + }.Run("maxUint64", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x01\x00\x00\x00\x00\x00\x00\x00\x00"), + Values: mod.Values{ + "18446744073709551616", *big.NewInt(0).Add(bigMaxUint64, big.NewInt(1)), + }.AddVariants(mod.All...), + }.Run("maxUint64+1", t, marshal, unmarshal) + }) + } }