diff --git a/marshal_test.go b/marshal_test.go index d81042053..ed077ec1d 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -1374,7 +1374,7 @@ func TestMarshalNil(t *testing.T) { func TestUnmarshalInetCopyBytes(t *testing.T) { data := []byte{127, 0, 0, 1} var ip net.IP - if err := unmarshalInet(NativeType{proto: 2, typ: TypeInet}, data, &ip); err != nil { + if err := unmarshalInet(data, &ip); err != nil { t.Fatal(err) } diff --git a/tests/serialization/marshal_14_inet_corrupt_test.go b/tests/serialization/marshal_14_inet_corrupt_test.go index 885f18f93..9888dcb74 100644 --- a/tests/serialization/marshal_14_inet_corrupt_test.go +++ b/tests/serialization/marshal_14_inet_corrupt_test.go @@ -10,111 +10,131 @@ import ( "github.com/gocql/gocql" "github.com/gocql/gocql/internal/tests/serialization" "github.com/gocql/gocql/internal/tests/serialization/mod" + "github.com/gocql/gocql/serialization/inet" ) 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) + type testSuite struct { + name string + marshal func(interface{}) ([]byte, error) + unmarshal func(bytes []byte, i interface{}) error } - // 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) + testSuites := [2]testSuite{ + { + name: "serialization.inet", + marshal: inet.Marshal, + unmarshal: inet.Unmarshal, + }, + { + name: "glob", + marshal: func(i interface{}) ([]byte, error) { + return gocql.Marshal(tType, i) + }, + unmarshal: func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + }, + }, + } + + for _, tSuite := range testSuites { + marshal := tSuite.marshal + unmarshal := tSuite.unmarshal + + t.Run(tSuite.name, func(t *testing.T) { + + 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...), + }.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...), + }.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...), + }.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...), + }.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...), + }.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...), + }.Run("big_dataV6", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xb6\xb7\x7c\x23\xc7\x76\x40\xff\x82\x8d\xa3\x85\xf3\xe8\xa2\xaf"), + Values: mod.Values{ + [4]byte{}, + }.AddVariants(mod.All...), + }.Run("big_dataV6Array4", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte{192, 168, 0}, + Values: mod.Values{ + "", + net.IP{}, + []byte{}, + [4]byte{}, + }.AddVariants(mod.All...), + }.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...), + }.Run("small_dataV6", t, unmarshal) + }) + } } diff --git a/tests/serialization/marshal_14_inet_test.go b/tests/serialization/marshal_14_inet_test.go index 05a9d9bd5..e2da74d00 100644 --- a/tests/serialization/marshal_14_inet_test.go +++ b/tests/serialization/marshal_14_inet_test.go @@ -10,110 +10,150 @@ import ( "github.com/gocql/gocql" "github.com/gocql/gocql/internal/tests/serialization" "github.com/gocql/gocql/internal/tests/serialization/mod" + "github.com/gocql/gocql/serialization/inet" ) 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) + type testSuite struct { + name string + marshal func(interface{}) ([]byte, error) + unmarshal func(bytes []byte, i interface{}) error } - // 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) + testSuites := [2]testSuite{ + { + name: "serialization.inet", + marshal: inet.Marshal, + unmarshal: inet.Unmarshal, + }, + { + name: "glob", + marshal: func(i interface{}) ([]byte, error) { + return gocql.Marshal(tType, i) + }, + unmarshal: func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + }, + }, + } + + for _, tSuite := range testSuites { + marshal := tSuite.marshal + unmarshal := tSuite.unmarshal + + t.Run(tSuite.name, func(t *testing.T) { + 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), + }.Run("[nil]nullable", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{ + [4]byte{}, + [16]byte{}, + }.AddVariants(mod.CustomType), + }.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...), + }.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{}, + }.AddVariants(mod.All...), + }.Run("zerosV4", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte{0, 0, 0, 0}, + Values: mod.Values{ + [16]byte{}, + }.AddVariants(mod.All...), + }.Run("zerosV4unmarshal", t, nil, 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...), + }.Run("zerosV6", 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{ + [4]byte{0, 0, 0, 0}, + }.AddVariants(mod.All...), + }.Run("zerosV6unmarshal", t, nil, 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...), + }.Run("ipV4", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte{192, 168, 0, 1}, + Values: mod.Values{ + [16]byte{192, 168, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + }.AddVariants(mod.All...), + }.Run("ipV4unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: []byte{255, 255, 255, 255}, + Values: mod.Values{ + "255.255.255.255", + []byte{255, 255, 255, 255}, + net.IP{255, 255, 255, 255}, + [4]byte{255, 255, 255, 255}, + }.AddVariants(mod.All...), + }.Run("ipV4max", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte{255, 255, 255, 255}, + Values: mod.Values{ + [16]byte{255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + }.AddVariants(mod.All...), + }.Run("ipV4maxUnmarshal", t, nil, 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...), + }.Run("ipV6", t, marshal, unmarshal) + }) + } }