forked from scylladb/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
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 scylladb#317 from illia-li/il/add/marshal/map_tests
Add `map` marshal tests
- Loading branch information
Showing
6 changed files
with
546 additions
and
1 deletion.
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
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,191 @@ | ||
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 TestMarshalMapV2Corrupt(t *testing.T) { | ||
elem := gocql.NewNativeType(2, gocql.TypeSmallInt, "") | ||
tType := gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeMap, ""), elem, elem) | ||
|
||
//unmarshal data than bigger the normal data, does not return error. | ||
brokenBigData := serialization.GetTypes(mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...)...) | ||
|
||
refInt32 := func(v int32) *int32 { return &v } | ||
refModInt32 := func(v mod.Int32) *mod.Int32 { return &v } | ||
|
||
marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } | ||
unmarshal := func(bytes []byte, i interface{}) error { | ||
return gocql.Unmarshal(tType, bytes, i) | ||
} | ||
|
||
val := int32(math.MaxInt16 + 1) | ||
valc := mod.Int32(val) | ||
serialization.NegativeMarshalSet{ | ||
Values: mod.Values{ | ||
map[int32]int32{val: val}, map[int32]int32{val: 0}, map[int32]int32{0: val}, | ||
map[int32]*int32{val: refInt32(val)}, map[int32]*int32{val: refInt32(0)}, map[int32]*int32{0: refInt32(val)}, | ||
map[mod.Int32]mod.Int32{valc: valc}, map[mod.Int32]mod.Int32{valc: 0}, map[mod.Int32]mod.Int32{0: valc}, | ||
map[mod.Int32]*mod.Int32{valc: refModInt32(valc)}, map[mod.Int32]*mod.Int32{valc: refModInt32(0)}, map[mod.Int32]*mod.Int32{0: refModInt32(valc)}, | ||
}.AddVariants(mod.All...), | ||
}.Run("big_vals", t, marshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01\x00\x02\xff\xff\x00\x02\xff\xff\x01"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenBigData, | ||
}.Run("big_data_elem1+", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01\x00\x00\x00\x00\xff"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenBigData, | ||
}.Run("big_data_zeroElem1+", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x00\x01"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenBigData, | ||
}.Run("big_data_elems0+", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01\x00\x02\xff\xff\x00\x02\xff"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("small_data_val_value-", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01\x00\x02\xff\xff\x00\x02"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("small_data_val_len", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01\x00\x02\xff\xff\x00"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("small_data_val_len-", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01\x00\x02\xff\xff"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("small_data_val-", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01\x00\x02\xff"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("small_data_key_value-", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01\x00\x02"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("small_data_key_len", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01\x00"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("small_data_key_len-", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00\x01"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("small_data_pair-", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\x00"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("small_data_elems-", t, unmarshal) | ||
} | ||
|
||
func TestMarshalMapV2CorruptMax(t *testing.T) { | ||
t.Parallel() | ||
elem := gocql.NewNativeType(2, gocql.TypeSmallInt, "") | ||
tType := gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeMap, ""), elem, elem) | ||
|
||
elems := math.MaxUint16 + 1 | ||
values := []func() interface{}{ | ||
func() interface{} { | ||
out := make(map[int32]int32, elems) | ||
for i := 0; i < elems; i++ { | ||
out[int32(i)] = int32(1) | ||
} | ||
return out | ||
}, | ||
func() interface{} { | ||
out := make(map[int32]*int32, elems) | ||
for i := 0; i < elems; i++ { | ||
tmp := int32(1) | ||
out[int32(i)] = &tmp | ||
} | ||
return out | ||
}, | ||
func() interface{} { | ||
out := make(map[mod.Int32]mod.Int32, elems) | ||
for i := 0; i < elems; i++ { | ||
out[mod.Int32(i)] = mod.Int32(1) | ||
} | ||
return out | ||
}, | ||
func() interface{} { | ||
out := make(map[mod.Int32]*mod.Int32, elems) | ||
for i := 0; i < elems; i++ { | ||
tmp := mod.Int32(1) | ||
out[mod.Int32(i)] = &tmp | ||
} | ||
return out | ||
}, | ||
} | ||
|
||
marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } | ||
|
||
for _, v := range values { | ||
value := v() | ||
name := fmt.Sprintf("%T", value) | ||
|
||
serialization.NegativeMarshalSet{ | ||
Values: mod.Values{value}.AddVariants(mod.All...), | ||
}.Run(name, t, marshal) | ||
} | ||
} |
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,130 @@ | ||
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 TestMarshalMapV2(t *testing.T) { | ||
elem := gocql.NewNativeType(2, gocql.TypeSmallInt, "") | ||
tType := gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeMap, ""), elem, elem) | ||
|
||
refInt16 := func(v int16) *int16 { return &v } | ||
refModInt16 := func(v mod.Int16) *mod.Int16 { return &v } | ||
|
||
marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } | ||
unmarshal := func(bytes []byte, i interface{}) error { | ||
return gocql.Unmarshal(tType, bytes, i) | ||
} | ||
|
||
serialization.PositiveSet{ | ||
Data: nil, | ||
Values: mod.Values{ | ||
(map[int16]int16)(nil), (map[int16]*int16)(nil), | ||
(map[mod.Int16]mod.Int16)(nil), (map[mod.Int16]*mod.Int16)(nil), | ||
(*map[int16]int16)(nil), (*map[int16]*int16)(nil), | ||
(*map[mod.Int16]mod.Int16)(nil), (*map[mod.Int16]*mod.Int16)(nil), | ||
}.AddVariants(mod.CustomType), | ||
}.Run("[nil]nullable", t, marshal, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: []byte("\x00\x00"), | ||
Values: mod.Values{ | ||
make(map[int16]int16), make(map[int16]*int16), | ||
make(map[mod.Int16]mod.Int16), make(map[mod.Int16]*mod.Int16), | ||
}.AddVariants(mod.All...), | ||
}.Run("zero elems", t, marshal, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: []byte("\x00\x01\x00\x00\x00\x00"), | ||
Values: mod.Values{ | ||
map[int16]int16{0: 0}, map[int16]*int16{0: refInt16(0)}, | ||
map[mod.Int16]mod.Int16{0: 0}, map[mod.Int16]*mod.Int16{0: refModInt16(0)}, | ||
}.AddVariants(mod.All...), | ||
}.Run("[]{zero elem}unmarshal", t, nil, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: []byte("\x00\x01\x00\x02\x00\x00\x00\x02\x00\x00"), | ||
Values: mod.Values{ | ||
map[int16]int16{0: 0}, map[int16]*int16{0: refInt16(0)}, | ||
map[mod.Int16]mod.Int16{0: 0}, map[mod.Int16]*mod.Int16{0: refModInt16(0)}, | ||
}.AddVariants(mod.All...), | ||
}.Run("[]{0:0}", t, marshal, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: []byte("\x00\x01\x00\x02\x7f\xff\x00\x02\x7f\xff"), | ||
Values: mod.Values{ | ||
map[int16]int16{32767: 32767}, map[int16]*int16{32767: refInt16(32767)}, | ||
map[mod.Int16]mod.Int16{32767: 32767}, map[mod.Int16]*mod.Int16{32767: refModInt16(32767)}, | ||
}.AddVariants(mod.All...), | ||
}.Run("[]{max:max}", t, marshal, unmarshal) | ||
} | ||
|
||
func TestMarshalMapV2Max(t *testing.T) { | ||
t.Parallel() | ||
elem := gocql.NewNativeType(2, gocql.TypeSmallInt, "") | ||
tType := gocql.NewCollectionType(gocql.NewNativeType(2, gocql.TypeMap, ""), elem, elem) | ||
|
||
elems := math.MaxUint16 | ||
|
||
data := make([]byte, 0, elems*4+2) | ||
data = append(data, 255, 255) | ||
uintData := func(v uint) (byte, byte) { | ||
return byte(v >> 8), byte(v) | ||
} | ||
for v := 0; v < elems; v++ { | ||
b1, b2 := uintData(uint(v)) | ||
data = append(data, 0, 2, b1, b2, 0, 2, 0, 1) | ||
} | ||
|
||
values := []func() interface{}{ | ||
func() interface{} { | ||
out := make(map[int16]int16, elems) | ||
for i := 0; i < elems; i++ { | ||
out[int16(i)] = int16(1) | ||
} | ||
return out | ||
}, | ||
func() interface{} { | ||
out := make(map[int16]*int16, elems) | ||
for i := 0; i < elems; i++ { | ||
tmp := int16(1) | ||
out[int16(i)] = &tmp | ||
} | ||
return out | ||
}, | ||
func() interface{} { | ||
out := make(map[mod.Int16]mod.Int16, elems) | ||
for i := 0; i < elems; i++ { | ||
out[mod.Int16(i)] = mod.Int16(1) | ||
} | ||
return out | ||
}, | ||
func() interface{} { | ||
out := make(map[mod.Int16]*mod.Int16, elems) | ||
for i := 0; i < elems; i++ { | ||
tmp := mod.Int16(1) | ||
out[mod.Int16(i)] = &tmp | ||
} | ||
return out | ||
}, | ||
} | ||
unmarshal := func(bytes []byte, i interface{}) error { | ||
return gocql.Unmarshal(tType, bytes, i) | ||
} | ||
|
||
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, nil, unmarshal) | ||
} | ||
} |
Oops, something went wrong.