Skip to content

Commit

Permalink
Merge pull request #316 from illia-li/il/add/marshal/list_set_tests
Browse files Browse the repository at this point in the history
Add `list`, `set` marshal tests
  • Loading branch information
dkropachev authored Oct 22, 2024
2 parents 2b89814 + 385807f commit 32250de
Show file tree
Hide file tree
Showing 6 changed files with 718 additions and 2 deletions.
48 changes: 46 additions & 2 deletions internal/tests/serialization/mod/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
222 changes: 222 additions & 0 deletions tests/serialization/marshal_19_list_set_v2_corrupt_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading

0 comments on commit 32250de

Please sign in to comment.