From 89a03acc80ea479dc20072c34c6ed04b171076ef Mon Sep 17 00:00:00 2001 From: illia-li Date: Tue, 1 Oct 2024 08:35:55 -0400 Subject: [PATCH] add tests --- marshal_2_tinyint_corrupt_test.go | 69 +++++++++++++++ marshal_2_tinyint_test.go | 139 ++++++++++++++++-------------- 2 files changed, 141 insertions(+), 67 deletions(-) create mode 100644 marshal_2_tinyint_corrupt_test.go diff --git a/marshal_2_tinyint_corrupt_test.go b/marshal_2_tinyint_corrupt_test.go new file mode 100644 index 00000000..680f44a6 --- /dev/null +++ b/marshal_2_tinyint_corrupt_test.go @@ -0,0 +1,69 @@ +package gocql_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/marshal/tinyint" +) + +func TestMarshalTinyintCorrupt(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.TypeTinyInt, "") + + testSuites := [2]testSuite{ + { + name: "serialization.tinyint", + marshal: tinyint.Marshal, + unmarshal: tinyint.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{ + int16(128), int32(128), int64(128), int(128), + "128", *big.NewInt(128), + int16(-129), int32(-129), int64(-129), int(-129), + "-129", *big.NewInt(-129), + uint16(256), uint32(256), uint64(256), uint(256), + }.AddVariants(mod.All...), + }.Run("big_vals", t, marshal) + + 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("\x80\x00"), + 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("big_data", t, unmarshal) + }) + } +} diff --git a/marshal_2_tinyint_test.go b/marshal_2_tinyint_test.go index 80ae976c..0ce760d2 100644 --- a/marshal_2_tinyint_test.go +++ b/marshal_2_tinyint_test.go @@ -7,85 +7,90 @@ import ( "github.com/gocql/gocql" "github.com/gocql/gocql/internal/tests/serialization" "github.com/gocql/gocql/internal/tests/serialization/mod" + "github.com/gocql/gocql/marshal/tinyint" ) func TestMarshalTinyint(t *testing.T) { - tType := gocql.NewNativeType(4, gocql.TypeTinyInt, "") - - marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } - unmarshal := func(bytes []byte, i interface{}) error { - return gocql.Unmarshal(tType, bytes, i) + type testSuite struct { + name string + marshal func(interface{}) ([]byte, error) + unmarshal func(bytes []byte, i interface{}) error } - // unmarshal `custom string` unsupported - brokenCustomStrings := serialization.GetTypes(mod.String(""), (*mod.String)(nil)) + tType := gocql.NewNativeType(4, gocql.TypeTinyInt, "") - // marshal "" (empty string) unsupported - // unmarshal nil value into (string)("0") - brokenEmptyStrings := serialization.GetTypes(string(""), mod.String("")) + testSuites := [2]testSuite{ + { + name: "serialization.tinyint", + marshal: tinyint.Marshal, + unmarshal: tinyint.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) + }, + }, + } - // marshal `custom string` unsupported - // marshal `big.Int` unsupported - brokenMarshalTypes := append(brokenCustomStrings, serialization.GetTypes(big.Int{}, &big.Int{})...) + for _, tSuite := range testSuites { + marshal := tSuite.marshal + unmarshal := tSuite.unmarshal - 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), string(""), - }.AddVariants(mod.CustomType), - BrokenMarshalTypes: brokenEmptyStrings, - BrokenUnmarshalTypes: brokenEmptyStrings, - }.Run("[nil]nullable", t, marshal, 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), string(""), + }.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), - "0", big.Int{}, - }.AddVariants(mod.CustomType), - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("[nil]unmarshal", t, nil, 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...), - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("[]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...), - BrokenMarshalTypes: brokenMarshalTypes, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("zeros", t, marshal, 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("\x7f"), - Values: mod.Values{int8(127), int16(127), int32(127), int64(127), int(127), "127", *big.NewInt(127)}.AddVariants(mod.All...), - BrokenMarshalTypes: brokenMarshalTypes, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("127", t, marshal, unmarshal) + serialization.PositiveSet{ + Data: []byte("\x7f"), + Values: mod.Values{int8(127), int16(127), int32(127), int64(127), int(127), "127", *big.NewInt(127)}.AddVariants(mod.All...), + }.Run("127", 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: brokenMarshalTypes, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("-128", 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("-128", t, marshal, unmarshal) - serialization.PositiveSet{ - Data: []byte("\xff"), - Values: mod.Values{uint8(255), uint16(255), uint32(255), uint64(255), uint(255)}.AddVariants(mod.All...), - }.Run("255", t, marshal, unmarshal) + serialization.PositiveSet{ + Data: []byte("\xff"), + Values: mod.Values{uint8(255), uint16(255), uint32(255), uint64(255), uint(255)}.AddVariants(mod.All...), + }.Run("255", t, marshal, unmarshal) + }) + } }