forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 58
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 #288 from illia-li/il/add/marshal_tests_inet
Add marshal tests for `inet`
- Loading branch information
Showing
3 changed files
with
245 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package gocql_test | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/gocql/gocql" | ||
"github.com/gocql/gocql/internal/tests/serialization" | ||
"github.com/gocql/gocql/internal/tests/serialization/mod" | ||
) | ||
|
||
func TestMarshalsInetMustFail(t *testing.T) { | ||
tType := gocql.NewNativeType(4, gocql.TypeInet, "") | ||
|
||
marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } | ||
unmarshal := func(bytes []byte, i interface{}) error { | ||
return gocql.Unmarshal(tType, bytes, i) | ||
} | ||
|
||
// marshal big and small `net.IP` values does not return error | ||
brokenNetIP := serialization.GetTypes(net.IP{}, &net.IP{}) | ||
|
||
// unmarshal big and small data into `string` and `*string` does not return error | ||
brokenString := serialization.GetTypes("", (*string)(nil)) | ||
|
||
serialization.NegativeMarshalSet{ | ||
Values: mod.Values{ | ||
"192.168.0.1.1", | ||
net.IP{192, 168, 0, 1, 1}, | ||
[]byte{192, 168, 0, 1, 1}, | ||
[5]byte{192, 168, 0, 1, 1}, | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenNetIP, | ||
}.Run("big_valsV4", t, marshal) | ||
|
||
serialization.NegativeMarshalSet{ | ||
Values: mod.Values{ | ||
"fe80:cd00:0:cde:1257:0:211e:729cc", | ||
net.IP("\xb6\xb7\x7c\x23\xc7\x76\x40\xff\x82\x8d\xa3\x85\xf3\xe8\xa2\xaf\xaf"), | ||
[]byte("\xb6\xb7\x7c\x23\xc7\x76\x40\xff\x82\x8d\xa3\x85\xf3\xe8\xa2\xaf\xaf"), | ||
[17]byte{254, 128, 205, 0, 0, 0, 12, 222, 18, 87, 0, 0, 33, 30, 114, 156, 156}, | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenNetIP, | ||
}.Run("big_valsV6", t, marshal) | ||
|
||
serialization.NegativeMarshalSet{ | ||
Values: mod.Values{ | ||
"192.168.0", | ||
net.IP{192, 168, 0}, | ||
[]byte{192, 168, 0}, | ||
[3]byte{192, 168, 0}, | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenNetIP, | ||
}.Run("small_valsV4", t, marshal) | ||
|
||
serialization.NegativeMarshalSet{ | ||
Values: mod.Values{ | ||
"fe80:cd00:0:cde:1257:0:211e", | ||
net.IP("\xb6\xb7\x7c\x23\xc7\x76\x40\xff\x82\x8d\xa3\x85\xf3\xe8\xa2"), | ||
[]byte("\xb6\xb7\x7c\x23\xc7\x76\x40\xff\x82\x8d\xa3\x85\xf3\xe8\xa2"), | ||
[15]byte{254, 128, 205, 0, 0, 0, 12, 222, 18, 87, 0, 0, 33, 30, 114}, | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenNetIP, | ||
}.Run("small_valsV6", t, marshal) | ||
|
||
serialization.NegativeMarshalSet{ | ||
Values: mod.Values{ | ||
"b6b77c@3-c776-40ff-828d-a385f3e8a2a", | ||
"00000000-0000-0000-0000-0#0000000000", | ||
"192.168.a.1", | ||
}.AddVariants(mod.All...), | ||
}.Run("corrupt_vals", t, marshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte{192, 168, 0, 1, 1}, | ||
Values: mod.Values{ | ||
"", | ||
net.IP{}, | ||
[]byte{}, | ||
[4]byte{}, | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenString, | ||
}.Run("big_dataV4", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\xb6\xb7\x7c\x23\xc7\x76\x40\xff\x82\x8d\xa3\x85\xf3\xe8\xa2\xaf\xaf"), | ||
Values: mod.Values{ | ||
"", | ||
net.IP{}, | ||
[]byte{}, | ||
[16]byte{}, | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenString, | ||
}.Run("big_dataV6", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte{192, 168, 0}, | ||
Values: mod.Values{ | ||
"", | ||
net.IP{}, | ||
[]byte{}, | ||
[4]byte{}, | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenString, | ||
}.Run("small_dataV4", t, unmarshal) | ||
|
||
serialization.NegativeUnmarshalSet{ | ||
Data: []byte("\xb6\xb7\x7c\x23\xc7\x76\x40\xff\x82\x8d\xa3\x85\xf3\xe8\xa2"), | ||
Values: mod.Values{ | ||
"", | ||
net.IP{}, | ||
[]byte{}, | ||
[16]byte{}, | ||
}.AddVariants(mod.All...), | ||
BrokenTypes: brokenString, | ||
}.Run("small_dataV6", t, unmarshal) | ||
} |
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,116 @@ | ||
package gocql_test | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/gocql/gocql" | ||
"github.com/gocql/gocql/internal/tests/serialization" | ||
"github.com/gocql/gocql/internal/tests/serialization/mod" | ||
) | ||
|
||
func TestMarshalsInet(t *testing.T) { | ||
tType := gocql.NewNativeType(4, gocql.TypeInet, "") | ||
|
||
marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } | ||
unmarshal := func(bytes []byte, i interface{}) error { | ||
return gocql.Unmarshal(tType, bytes, i) | ||
} | ||
|
||
// marshal and unmarshal []byte{}, [4]byte{}, [16]byte{} and `custom types` of these types unsupported | ||
// marshal and unmarshal `custom string` unsupported | ||
brokenTypes := serialization.GetTypes(mod.Values{ | ||
[]byte{}, | ||
[4]byte{}, | ||
[16]byte{}, | ||
mod.String(""), | ||
}.AddVariants(mod.All...)...) | ||
|
||
// unmarshal zero and nil data into `net.IP` and `string` unsupported | ||
brokenZeroUnmarshal := serialization.GetTypes(mod.Values{net.IP{}, ""}.AddVariants(mod.Reference)...) | ||
|
||
serialization.PositiveSet{ | ||
Data: nil, | ||
Values: mod.Values{ | ||
([]byte)(nil), | ||
(*[]byte)(nil), | ||
(*[4]byte)(nil), | ||
(*[16]byte)(nil), | ||
(net.IP)(nil), | ||
(*net.IP)(nil), | ||
"", | ||
(*string)(nil), | ||
}.AddVariants(mod.CustomType), | ||
BrokenMarshalTypes: serialization.GetTypes([]byte{}, mod.Bytes{}, "", mod.String("")), | ||
BrokenUnmarshalTypes: serialization.GetTypes([]byte{}, mod.Bytes{}, net.IP{}, mod.String("")), | ||
}.Run("[nil]nullable", t, marshal, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: nil, | ||
Values: mod.Values{ | ||
[4]byte{}, | ||
[16]byte{}, | ||
}.AddVariants(mod.CustomType), | ||
BrokenUnmarshalTypes: brokenTypes, | ||
}.Run("[nil]unmarshal", t, nil, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: make([]byte, 0), | ||
Values: mod.Values{ | ||
make([]byte, 0), | ||
[4]byte{}, | ||
[16]byte{}, | ||
make(net.IP, 0), | ||
"0.0.0.0", | ||
}.AddVariants(mod.All...), | ||
BrokenUnmarshalTypes: append(brokenTypes, brokenZeroUnmarshal...), | ||
}.Run("[]unmarshal", t, nil, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: []byte{0, 0, 0, 0}, | ||
Values: mod.Values{ | ||
"0.0.0.0", | ||
[]byte{0, 0, 0, 0}, | ||
net.IP{0, 0, 0, 0}, | ||
[4]byte{0, 0, 0, 0}, | ||
}.AddVariants(mod.All...), | ||
BrokenMarshalTypes: brokenTypes, | ||
BrokenUnmarshalTypes: brokenTypes, | ||
}.Run("zerosV4", t, marshal, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
Values: mod.Values{ | ||
"::", | ||
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
[16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
}.AddVariants(mod.All...), | ||
BrokenMarshalTypes: brokenTypes, | ||
BrokenUnmarshalTypes: brokenTypes, | ||
}.Run("zerosV6", t, marshal, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: []byte{192, 168, 0, 1}, | ||
Values: mod.Values{ | ||
"192.168.0.1", | ||
[]byte{192, 168, 0, 1}, | ||
net.IP{192, 168, 0, 1}, | ||
[4]byte{192, 168, 0, 1}, | ||
}.AddVariants(mod.All...), | ||
BrokenMarshalTypes: brokenTypes, | ||
BrokenUnmarshalTypes: brokenTypes, | ||
}.Run("ipV4", t, marshal, unmarshal) | ||
|
||
serialization.PositiveSet{ | ||
Data: []byte("\xfe\x80\xcd\x00\x00\x00\x0c\xde\x12\x57\x00\x00\x21\x1e\x72\x9c"), | ||
Values: mod.Values{ | ||
"fe80:cd00:0:cde:1257:0:211e:729c", | ||
[]byte("\xfe\x80\xcd\x00\x00\x00\x0c\xde\x12\x57\x00\x00\x21\x1e\x72\x9c"), | ||
net.IP("\xfe\x80\xcd\x00\x00\x00\x0c\xde\x12\x57\x00\x00\x21\x1e\x72\x9c"), | ||
[16]byte{254, 128, 205, 0, 0, 0, 12, 222, 18, 87, 0, 0, 33, 30, 114, 156}, | ||
}.AddVariants(mod.All...), | ||
BrokenMarshalTypes: brokenTypes, | ||
BrokenUnmarshalTypes: brokenTypes, | ||
}.Run("ipV6", t, marshal, unmarshal) | ||
} |