Skip to content

Commit

Permalink
Merge pull request #288 from illia-li/il/add/marshal_tests_inet
Browse files Browse the repository at this point in the history
Add marshal tests for `inet`
  • Loading branch information
dkropachev authored Oct 3, 2024
2 parents fe56500 + a6aa680 commit 9a26180
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/tests/serialization/mod/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ type (
String string

Bytes []byte
Bytes3 [3]byte
Bytes4 [4]byte
Bytes5 [5]byte
Bytes15 [15]byte
Bytes16 [16]byte
Bytes17 [17]byte

SliceInt16 []int16
SliceInt16R []*int16
Expand Down Expand Up @@ -90,10 +94,18 @@ func customType(i interface{}) interface{} {
return String(v)
case []byte:
return Bytes(v)
case [3]byte:
return Bytes3(v)
case [4]byte:
return Bytes4(v)
case [5]byte:
return Bytes5(v)
case [15]byte:
return Bytes15(v)
case [16]byte:
return Bytes16(v)
case [17]byte:
return Bytes17(v)
case []int16:
return SliceInt16(v)
case []*int16:
Expand Down
117 changes: 117 additions & 0 deletions marshal_14_inet_corrupt_test.go
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)
}
116 changes: 116 additions & 0 deletions marshal_14_inet_test.go
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)
}

0 comments on commit 9a26180

Please sign in to comment.