From 66b1855d3b045835cee2d0c282acf7d39e004192 Mon Sep 17 00:00:00 2001 From: illia-li Date: Mon, 21 Oct 2024 09:53:27 -0400 Subject: [PATCH 1/3] add `gocql.NewCollectionType` function --- marshal.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/marshal.go b/marshal.go index c40c32a25..e81232586 100644 --- a/marshal.go +++ b/marshal.go @@ -2314,6 +2314,14 @@ func (s NativeType) String() string { } } +func NewCollectionType(m NativeType, key, elem TypeInfo) CollectionType { + return CollectionType{ + NativeType: m, + Key: key, + Elem: elem, + } +} + type CollectionType struct { NativeType Key TypeInfo // only used for TypeMap From 6c7ecaccbc4086cbcfb582184c668021d075b5eb Mon Sep 17 00:00:00 2001 From: illia-li Date: Mon, 21 Oct 2024 09:55:03 -0400 Subject: [PATCH 2/3] add `set`, `list` tests --- .../marshal_19_list_set_v2_corrupt_test.go | 222 ++++++++++++++++++ .../marshal_19_list_set_v2_test.go | 176 ++++++++++++++ .../marshal_19_list_set_v3_corrupt_test.go | 162 +++++++++++++ .../marshal_19_list_set_v3_test.go | 104 ++++++++ 4 files changed, 664 insertions(+) create mode 100644 tests/serialization/marshal_19_list_set_v2_corrupt_test.go create mode 100644 tests/serialization/marshal_19_list_set_v2_test.go create mode 100644 tests/serialization/marshal_19_list_set_v3_corrupt_test.go create mode 100644 tests/serialization/marshal_19_list_set_v3_test.go diff --git a/tests/serialization/marshal_19_list_set_v2_corrupt_test.go b/tests/serialization/marshal_19_list_set_v2_corrupt_test.go new file mode 100644 index 000000000..145473560 --- /dev/null +++ b/tests/serialization/marshal_19_list_set_v2_corrupt_test.go @@ -0,0 +1,222 @@ +package serialization_test + +import ( + "fmt" + "math" + "testing" + + "github.com/gocql/gocql" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" +) + +func TestMarshalSetListV2Corrupt(t *testing.T) { + elem := gocql.NewNativeType(2, gocql.TypeSmallInt, "") + tTypes := []gocql.TypeInfo{ + gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeList, ""), nil, elem), + gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeSet, ""), nil, elem), + } + + // unmarshal data than bigger the normal data, does not return error. + brokenBigData := serialization.GetTypes(mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...)...) + + brokenBigDataSlices := serialization.GetTypes(mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + }.AddVariants(mod.All...)...) + + refInt32 := func(v int32) *int32 { return &v } + refModInt32 := func(v mod.Int32) *mod.Int32 { return &v } + + for _, tType := range tTypes { + marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } + unmarshal := func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + } + + t.Run(tType.Type().String(), func(t *testing.T) { + + val := int32(math.MaxInt16 + 1) + valc := mod.Int32(val) + serialization.NegativeMarshalSet{ + Values: mod.Values{ + []int32{val}, []*int32{refInt32(val)}, + [1]int32{val}, [1]*int32{refInt32(val)}, + []mod.Int32{valc}, []*mod.Int32{refModInt32(valc)}, + [1]mod.Int32{valc}, [1]*mod.Int32{refModInt32(valc)}, + }.AddVariants(mod.All...), + }.Run("big_vals", t, marshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x01\x00\x02\xff\xff\x01"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + BrokenTypes: brokenBigData, + }.Run("big_data_elem1+", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x01\x00\x00\xff"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + BrokenTypes: brokenBigData, + }.Run("big_data_zeroElem1+", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00\x01"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + BrokenTypes: brokenBigDataSlices, + }.Run("big_data_elem0+", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x01\x00\x02\xff"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elem_value-", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x01\x00\x02"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elem_value--", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x01\x00"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elem_len-", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x01"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elem-", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elems-", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: nil, + Values: mod.Values{ + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.CustomType), + }.Run("nil_data_to_array", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: make([]byte, 0), + Values: mod.Values{ + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.CustomType), + }.Run("zero_data_to_array", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00"), + Values: mod.Values{ + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("zero_elems_to_array", t, unmarshal) + }) + } +} + +func TestMarshalSetListV2CorruptMax(t *testing.T) { + t.Parallel() + elem := gocql.NewNativeType(2, gocql.TypeSmallInt, "") + + tTypes := []gocql.TypeInfo{ + gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeList, ""), nil, elem), + gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeSet, ""), nil, elem), + } + + elems := math.MaxUint16 + 1 + values := []func() interface{}{ + func() interface{} { + out := make([]int16, elems) + for i := range out { + out[i] = int16(1) + } + return out + }, + func() interface{} { + out := make([]*int16, elems) + for i := range out { + tmp := int16(1) + out[i] = &tmp + } + return out + }, + func() interface{} { + out := make([]mod.Int16, elems) + for i := range out { + out[i] = mod.Int16(1) + } + return out + }, + func() interface{} { + out := make([]*mod.Int16, elems) + for i := range out { + tmp := mod.Int16(1) + out[i] = &tmp + } + return out + }, + } + + for _, tType := range tTypes { + marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } + + t.Run(tType.Type().String(), func(t *testing.T) { + for _, v := range values { + value := v() + name := fmt.Sprintf("%T", value) + + serialization.NegativeMarshalSet{ + Values: mod.Values{value}.AddVariants(mod.All...), + }.Run(name, t, marshal) + } + }) + } +} diff --git a/tests/serialization/marshal_19_list_set_v2_test.go b/tests/serialization/marshal_19_list_set_v2_test.go new file mode 100644 index 000000000..da05792e7 --- /dev/null +++ b/tests/serialization/marshal_19_list_set_v2_test.go @@ -0,0 +1,176 @@ +package serialization_test + +import ( + "fmt" + "math" + "testing" + + "github.com/gocql/gocql" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" +) + +func TestMarshalSetListV2(t *testing.T) { + elem := gocql.NewNativeType(2, gocql.TypeSmallInt, "") + + tTypes := []gocql.TypeInfo{ + gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeList, ""), nil, elem), + gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeSet, ""), nil, elem), + } + + // unmarshal `zero` data return an error + brokenZeroDataUnmarshal := serialization.GetTypes(mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + &[]int16{}, &[]*int16{}, + &[]mod.Int16{}, &[]*mod.Int16{}, + (*[1]int16)(nil), (*[1]*int16)(nil), + (*[1]mod.Int16)(nil), (*[1]*mod.Int16)(nil), + }.AddVariants(mod.CustomType)...) + + refInt16 := func(v int16) *int16 { return &v } + refModInt16 := func(v mod.Int16) *mod.Int16 { return &v } + + for _, tType := range tTypes { + marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } + unmarshal := func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + } + + t.Run(tType.Type().String(), func(t *testing.T) { + + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{ + ([]int16)(nil), ([]*int16)(nil), + ([]mod.Int16)(nil), ([]*mod.Int16)(nil), + (*[]int16)(nil), (*[]*int16)(nil), + (*[]mod.Int16)(nil), (*[]*mod.Int16)(nil), + (*[1]int16)(nil), (*[1]*int16)(nil), + (*[1]mod.Int16)(nil), (*[1]*mod.Int16)(nil), + }.AddVariants(mod.CustomType), + }.Run("[nil]nullable", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: make([]byte, 0), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + &[]int16{}, &[]*int16{}, + &[]mod.Int16{}, &[]*mod.Int16{}, + (*[1]int16)(nil), (*[1]*int16)(nil), + (*[1]mod.Int16)(nil), (*[1]*mod.Int16)(nil), + }.AddVariants(mod.CustomType), + BrokenUnmarshalTypes: brokenZeroDataUnmarshal, + }.Run("[]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x00"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("zero elems", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x01\x00\x02\x00\x00"), + Values: mod.Values{ + []int16{0}, []*int16{refInt16(0)}, + []mod.Int16{0}, []*mod.Int16{refModInt16(0)}, + [1]int16{0}, [1]*int16{refInt16(0)}, + [1]mod.Int16{0}, [1]*mod.Int16{refModInt16(0)}, + }.AddVariants(mod.All...), + }.Run("[]{0}", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x01\x00\x00"), + Values: mod.Values{ + []int16{0}, []*int16{refInt16(0)}, + []mod.Int16{0}, []*mod.Int16{refModInt16(0)}, + [1]int16{0}, [1]*int16{refInt16(0)}, + [1]mod.Int16{0}, [1]*mod.Int16{refModInt16(0)}, + }.AddVariants(mod.All...), + }.Run("[]{zero elem}unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x01\x00\x02\x7f\xff"), + Values: mod.Values{ + []int16{32767}, []*int16{refInt16(32767)}, + []mod.Int16{32767}, []*mod.Int16{refModInt16(32767)}, + [1]int16{32767}, [1]*int16{refInt16(32767)}, + [1]mod.Int16{32767}, [1]*mod.Int16{refModInt16(32767)}, + }.AddVariants(mod.All...), + }.Run("[]{max}", t, marshal, unmarshal) + }) + } +} + +func TestMarshalSetListV2Max(t *testing.T) { + t.Parallel() + elem := gocql.NewNativeType(2, gocql.TypeSmallInt, "") + + tTypes := []gocql.TypeInfo{ + gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeList, ""), nil, elem), + gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeSet, ""), nil, elem), + } + + elems := math.MaxUint16 + + data := make([]byte, 0, elems*4+2) + data = append(data, 255, 255) + for v := 0; v < elems; v++ { + data = append(data, 0, 2, 0, 1) + } + + values := []func() interface{}{ + func() interface{} { + out := make([]int16, elems) + for i := range out { + out[i] = int16(1) + } + return out + }, + func() interface{} { + out := make([]*int16, elems) + for i := range out { + tmp := int16(1) + out[i] = &tmp + } + return out + }, + func() interface{} { + out := make([]mod.Int16, elems) + for i := range out { + out[i] = mod.Int16(1) + } + return out + }, + func() interface{} { + out := make([]*mod.Int16, elems) + for i := range out { + tmp := mod.Int16(1) + out[i] = &tmp + } + return out + }, + } + + for _, tType := range tTypes { + marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } + unmarshal := func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + } + + t.Run(tType.Type().String(), func(t *testing.T) { + for _, v := range values { + value := v() + name := fmt.Sprintf("%T", value) + + serialization.PositiveSet{ + Data: data, + Values: mod.Values{value}.AddVariants(mod.All...), + }.Run(name, t, marshal, unmarshal) + } + }) + } +} diff --git a/tests/serialization/marshal_19_list_set_v3_corrupt_test.go b/tests/serialization/marshal_19_list_set_v3_corrupt_test.go new file mode 100644 index 000000000..4c8e3ed74 --- /dev/null +++ b/tests/serialization/marshal_19_list_set_v3_corrupt_test.go @@ -0,0 +1,162 @@ +package serialization_test + +import ( + "math" + "testing" + + "github.com/gocql/gocql" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" +) + +func TestMarshalSetListV3Corrupt(t *testing.T) { + elem := gocql.NewNativeType(3, gocql.TypeSmallInt, "") + tTypes := []gocql.TypeInfo{ + gocql.NewCollectionType(gocql.NewNativeType(3, gocql.TypeList, ""), nil, elem), + gocql.NewCollectionType(gocql.NewNativeType(3, gocql.TypeSet, ""), nil, elem), + } + + // unmarshal data than bigger the normal data, does not return error. + brokenBigData := serialization.GetTypes(mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...)...) + + brokenBigDataSlices := serialization.GetTypes(mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + }.AddVariants(mod.All...)...) + + refInt32 := func(v int32) *int32 { return &v } + refModInt32 := func(v mod.Int32) *mod.Int32 { return &v } + + for _, tType := range tTypes { + marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } + unmarshal := func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + } + + t.Run(tType.Type().String(), func(t *testing.T) { + + val := int32(math.MaxInt16 + 1) + valc := mod.Int32(val) + serialization.NegativeMarshalSet{ + Values: mod.Values{ + []int32{val}, []*int32{refInt32(val)}, + [1]int32{val}, [1]*int32{refInt32(val)}, + []mod.Int32{valc}, []*mod.Int32{refModInt32(valc)}, + [1]mod.Int32{valc}, [1]*mod.Int32{refModInt32(valc)}, + }.AddVariants(mod.All...), + }.Run("big_vals", t, marshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00\x00\x01\x00\x00\x00\x02\xff\xff\x01"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + BrokenTypes: brokenBigData, + }.Run("big_data_elem1+", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00\x00\x01\x00\x00\x00\x00\xff"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + BrokenTypes: brokenBigData, + }.Run("big_data_zeroElem1+", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00\x00\x00\x01"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + BrokenTypes: brokenBigDataSlices, + }.Run("big_data_elem0+", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00\x00\x01\x00\x00\x00\x02\xff"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elem_value-", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00\x00\x01\x00\x00\x00\x02"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elem_value--", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00\x00\x01\x00"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elem_len-", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00\x00\x01"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elem-", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("small_data_elems-", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: nil, + Values: mod.Values{ + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.CustomType), + }.Run("nil_data_to_array", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: make([]byte, 0), + Values: mod.Values{ + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.CustomType), + }.Run("zero_data_to_array", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x00\x00\x00"), + Values: mod.Values{ + [1]int16{}, [1]*int16{}, + [1]mod.Int16{}, [1]*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("zero_elems_to_array", t, unmarshal) + }) + } +} diff --git a/tests/serialization/marshal_19_list_set_v3_test.go b/tests/serialization/marshal_19_list_set_v3_test.go new file mode 100644 index 000000000..1409f84e4 --- /dev/null +++ b/tests/serialization/marshal_19_list_set_v3_test.go @@ -0,0 +1,104 @@ +package serialization_test + +import ( + "testing" + + "github.com/gocql/gocql" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" +) + +func TestMarshalSetListV3(t *testing.T) { + elem := gocql.NewNativeType(3, gocql.TypeSmallInt, "") + + tTypes := []gocql.TypeInfo{ + gocql.NewCollectionType(gocql.NewNativeType(3, gocql.TypeList, ""), nil, elem), + gocql.NewCollectionType(gocql.NewNativeType(3, gocql.TypeSet, ""), nil, elem), + } + + // unmarshal `zero` data return an error + brokenZeroDataUnmarshal := serialization.GetTypes(mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + &[]int16{}, &[]*int16{}, + &[]mod.Int16{}, &[]*mod.Int16{}, + (*[1]int16)(nil), (*[1]*int16)(nil), + (*[1]mod.Int16)(nil), (*[1]*mod.Int16)(nil), + }.AddVariants(mod.CustomType)...) + + refInt16 := func(v int16) *int16 { return &v } + refModInt16 := func(v mod.Int16) *mod.Int16 { return &v } + + for _, tType := range tTypes { + marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } + unmarshal := func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + } + + t.Run(tType.Type().String(), func(t *testing.T) { + + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{ + ([]int16)(nil), ([]*int16)(nil), + ([]mod.Int16)(nil), ([]*mod.Int16)(nil), + (*[]int16)(nil), (*[]*int16)(nil), + (*[]mod.Int16)(nil), (*[]*mod.Int16)(nil), + (*[1]int16)(nil), (*[1]*int16)(nil), + (*[1]mod.Int16)(nil), (*[1]*mod.Int16)(nil), + }.AddVariants(mod.CustomType), + }.Run("[nil]nullable", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: make([]byte, 0), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + &[]int16{}, &[]*int16{}, + &[]mod.Int16{}, &[]*mod.Int16{}, + (*[1]int16)(nil), (*[1]*int16)(nil), + (*[1]mod.Int16)(nil), (*[1]*mod.Int16)(nil), + }.AddVariants(mod.CustomType), + BrokenUnmarshalTypes: brokenZeroDataUnmarshal, + }.Run("[]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x00\x00\x00"), + Values: mod.Values{ + []int16{}, []*int16{}, + []mod.Int16{}, []*mod.Int16{}, + }.AddVariants(mod.All...), + }.Run("zero elems", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00"), + Values: mod.Values{ + []int16{0}, []*int16{refInt16(0)}, + []mod.Int16{0}, []*mod.Int16{refModInt16(0)}, + [1]int16{0}, [1]*int16{refInt16(0)}, + [1]mod.Int16{0}, [1]*mod.Int16{refModInt16(0)}, + }.AddVariants(mod.All...), + }.Run("[]{0}", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x00\x00\x01\x00\x00\x00\x00"), + Values: mod.Values{ + []int16{0}, []*int16{refInt16(0)}, + []mod.Int16{0}, []*mod.Int16{refModInt16(0)}, + [1]int16{0}, [1]*int16{refInt16(0)}, + [1]mod.Int16{0}, [1]*mod.Int16{refModInt16(0)}, + }.AddVariants(mod.All...), + }.Run("[]{zero elem}unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x00\x00\x01\x00\x00\x00\x02\x7f\xff"), + Values: mod.Values{ + []int16{32767}, []*int16{refInt16(32767)}, + []mod.Int16{32767}, []*mod.Int16{refModInt16(32767)}, + [1]int16{32767}, [1]*int16{refInt16(32767)}, + [1]mod.Int16{32767}, [1]*mod.Int16{refModInt16(32767)}, + }.AddVariants(mod.All...), + }.Run("[]{max}", t, marshal, unmarshal) + }) + } +} From 385807fcfd73b92d8d26c05af45b603f4af73864 Mon Sep 17 00:00:00 2001 From: illia-li Date: Mon, 21 Oct 2024 15:15:05 -0400 Subject: [PATCH 3/3] add into `mod` package `custom types` for `[]int32`,`[]*int32` --- internal/tests/serialization/mod/custom.go | 48 +++++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/internal/tests/serialization/mod/custom.go b/internal/tests/serialization/mod/custom.go index ed45696ba..475854688 100644 --- a/internal/tests/serialization/mod/custom.go +++ b/internal/tests/serialization/mod/custom.go @@ -32,13 +32,25 @@ type ( SliceInt16R []*int16 SliceInt16C []Int16 SliceInt16CR []*Int16 - SliceAny []interface{} + + SliceInt32 []int32 + SliceInt32R []*int32 + SliceInt32C []Int32 + SliceInt32CR []*Int32 + + SliceAny []interface{} Arr1Int16 [1]int16 Arr1Int16R [1]*int16 Arr1Int16C [1]Int16 Arr1Int16CR [1]*Int16 - ArrAny [1]interface{} + + Arr1Int32 [1]int32 + Arr1Int32R [1]*int32 + Arr1Int32C [1]Int32 + Arr1Int32CR [1]*Int32 + + ArrAny [1]interface{} MapInt16 map[int16]int16 MapInt16R map[int16]*int16 @@ -114,6 +126,14 @@ func customType(i interface{}) interface{} { return SliceInt16C(v) case []*Int16: return SliceInt16CR(v) + case []int32: + return SliceInt32(v) + case []*int32: + return SliceInt32R(v) + case []Int32: + return SliceInt32C(v) + case []*Int32: + return SliceInt32CR(v) case [1]int16: return Arr1Int16(v) case [1]*int16: @@ -122,6 +142,14 @@ func customType(i interface{}) interface{} { return Arr1Int16C(v) case [1]*Int16: return Arr1Int16CR(v) + case [1]int32: + return Arr1Int32(v) + case [1]*int32: + return Arr1Int32R(v) + case [1]Int32: + return Arr1Int32C(v) + case [1]*Int32: + return Arr1Int32CR(v) case map[int16]int16: return MapInt16(v) case map[int16]*int16: @@ -185,6 +213,14 @@ func intoCustomR(i interface{}) interface{} { return (*SliceInt16C)(v) case *[]*Int16: return (*SliceInt16CR)(v) + case *[]int32: + return (*SliceInt32)(v) + case *[]*int32: + return (*SliceInt32R)(v) + case *[]Int32: + return (*SliceInt32C)(v) + case *[]*Int32: + return (*SliceInt32CR)(v) case *[1]int16: return (*Arr1Int16)(v) case *[1]*int16: @@ -193,6 +229,14 @@ func intoCustomR(i interface{}) interface{} { return (*Arr1Int16C)(v) case *[1]*Int16: return (*Arr1Int16CR)(v) + case *[1]int32: + return (*Arr1Int32)(v) + case *[1]*int32: + return (*Arr1Int32R)(v) + case *[1]Int32: + return (*Arr1Int32C)(v) + case *[1]*Int32: + return (*Arr1Int32CR)(v) case *map[int16]int16: return (*MapInt16)(v) case *map[int16]*int16: