forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from illia-li/il/add/marshal/list_set_tests
Add `list`, `set` marshal tests
- Loading branch information
Showing
6 changed files
with
718 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 222 additions & 0 deletions
222
tests/serialization/marshal_19_list_set_v2_corrupt_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.