diff --git a/tests/serialization/marshal_12_ascii_corrupt_test.go b/tests/serialization/marshal_12_ascii_corrupt_test.go index 1b627c2f3..004908a5e 100644 --- a/tests/serialization/marshal_12_ascii_corrupt_test.go +++ b/tests/serialization/marshal_12_ascii_corrupt_test.go @@ -6,41 +6,48 @@ 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/ascii" ) func TestMarshalAsciiMustFail(t *testing.T) { tType := gocql.NewNativeType(4, gocql.TypeAscii, "") - marshal := func(i interface{}) ([]byte, error) { - return gocql.Marshal(tType, i) + type testSuite struct { + name string + marshal func(interface{}) ([]byte, error) + unmarshal func(bytes []byte, i interface{}) error } - unmarshal := func(bytes []byte, i interface{}) error { - return gocql.Unmarshal(tType, bytes, i) + + testSuites := [2]testSuite{ + { + name: "serialization.ascii", + marshal: ascii.Marshal, + unmarshal: ascii.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) + }, + }, } - // according to the 'cql protocol' - 'ascii' a sequence of bytes in the ASCII range [0, 127]. - // marshal and unmarshal functions does not return an error if bytes have values outside of this range. - brokenAllTypes := serialization.GetTypes(mod.Values{[]byte{}, ""}.AddVariants(mod.All...)...) - - serialization.NegativeMarshalSet{ - Values: mod.Values{ - []byte{255}, - []byte{127, 255, 127}, - string([]byte{255}), - string([]byte{127, 255, 127}), - }.AddVariants(mod.All...), - BrokenTypes: brokenAllTypes, - }.Run("corrupt_vals", t, marshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte{255}, - Values: mod.Values{[]byte{}, ""}.AddVariants(mod.All...), - BrokenTypes: brokenAllTypes, - }.Run("corrupt_data1", t, unmarshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte{127, 255, 127}, - Values: mod.Values{[]byte{}, ""}.AddVariants(mod.All...), - BrokenTypes: brokenAllTypes, - }.Run("corrupt_data2", t, unmarshal) + for _, tSuite := range testSuites { + unmarshal := tSuite.unmarshal + + t.Run(tSuite.name, func(t *testing.T) { + serialization.NegativeUnmarshalSet{ + Data: []byte{255}, + Values: mod.Values{[]byte{}, ""}.AddVariants(mod.All...), + }.Run("corrupt_data1", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte{127, 255, 127}, + Values: mod.Values{[]byte{}, ""}.AddVariants(mod.All...), + }.Run("corrupt_data2", t, unmarshal) + }) + } } diff --git a/tests/serialization/marshal_12_ascii_test.go b/tests/serialization/marshal_12_ascii_test.go index 5056e1448..4f2da68aa 100644 --- a/tests/serialization/marshal_12_ascii_test.go +++ b/tests/serialization/marshal_12_ascii_test.go @@ -6,43 +6,64 @@ 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/ascii" ) func TestMarshalAscii(t *testing.T) { tType := gocql.NewNativeType(4, gocql.TypeAscii, "") - marshal := func(i interface{}) ([]byte, error) { - return gocql.Marshal(tType, i) + type testSuite struct { + name string + marshal func(interface{}) ([]byte, error) + unmarshal func(bytes []byte, i interface{}) error } - unmarshal := func(bytes []byte, i interface{}) error { - return gocql.Unmarshal(tType, bytes, i) + + testSuites := [2]testSuite{ + { + name: "serialization.int", + marshal: ascii.Marshal, + unmarshal: ascii.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 `zero` data into ([]byte)(nil), (*[]byte)(*[nil]) - brokenZeroSlices := serialization.GetTypes(make([]byte, 0), (*[]byte)(nil)) - - serialization.PositiveSet{ - Data: nil, - Values: mod.Values{ - ([]byte)(nil), - (*[]byte)(nil), - (*string)(nil), - }.AddVariants(mod.CustomType), - }.Run("[nil]nullable", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: nil, - Values: mod.Values{""}.AddVariants(mod.CustomType), - }.Run("[nil]unmarshal", t, nil, unmarshal) - - serialization.PositiveSet{ - Data: make([]byte, 0), - Values: mod.Values{make([]byte, 0), ""}.AddVariants(mod.All...), - BrokenUnmarshalTypes: brokenZeroSlices, - }.Run("[]unmarshal", t, nil, unmarshal) - - serialization.PositiveSet{ - Data: []byte("test text string"), - Values: mod.Values{[]byte("test text string"), "test text string"}.AddVariants(mod.All...), - }.Run("text", t, nil, 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{ + ([]byte)(nil), + (*[]byte)(nil), + (*string)(nil), + }.AddVariants(mod.CustomType), + }.Run("[nil]nullable", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{""}.AddVariants(mod.CustomType), + }.Run("[nil]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: make([]byte, 0), + Values: mod.Values{make([]byte, 0), ""}.AddVariants(mod.All...), + }.Run("[]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: []byte("test text string"), + Values: mod.Values{[]byte("test text string"), "test text string"}.AddVariants(mod.All...), + }.Run("text", t, nil, unmarshal) + }) + } }