Skip to content

Commit

Permalink
fix ascii tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Nov 7, 2024
1 parent 9fa449e commit 6bc5d3e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 60 deletions.
65 changes: 36 additions & 29 deletions tests/serialization/marshal_12_ascii_corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,48 @@ 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/ascii"
)

func TestMarshalAsciiMustFail(t *testing.T) {
tType := gocql.NewNativeType(4, gocql.TypeAscii, "")

marshal := func(i interface{}) ([]byte, error) {
return gocql.Marshal(tType, i)
type testSuite struct {
name string
marshal func(interface{}) ([]byte, error)
unmarshal func(bytes []byte, i interface{}) error
}
unmarshal := func(bytes []byte, i interface{}) error {
return gocql.Unmarshal(tType, bytes, i)

testSuites := [2]testSuite{
{
name: "serialization.ascii",
marshal: ascii.Marshal,
unmarshal: ascii.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)
},
},
}

// according to the 'cql protocol' - 'ascii' a sequence of bytes in the ASCII range [0, 127].
// marshal and unmarshal functions does not return an error if bytes have values outside of this range.
brokenAllTypes := serialization.GetTypes(mod.Values{[]byte{}, ""}.AddVariants(mod.All...)...)

serialization.NegativeMarshalSet{
Values: mod.Values{
[]byte{255},
[]byte{127, 255, 127},
string([]byte{255}),
string([]byte{127, 255, 127}),
}.AddVariants(mod.All...),
BrokenTypes: brokenAllTypes,
}.Run("corrupt_vals", t, marshal)

serialization.NegativeUnmarshalSet{
Data: []byte{255},
Values: mod.Values{[]byte{}, ""}.AddVariants(mod.All...),
BrokenTypes: brokenAllTypes,
}.Run("corrupt_data1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte{127, 255, 127},
Values: mod.Values{[]byte{}, ""}.AddVariants(mod.All...),
BrokenTypes: brokenAllTypes,
}.Run("corrupt_data2", t, unmarshal)
for _, tSuite := range testSuites {
unmarshal := tSuite.unmarshal

t.Run(tSuite.name, func(t *testing.T) {
serialization.NegativeUnmarshalSet{
Data: []byte{255},
Values: mod.Values{[]byte{}, ""}.AddVariants(mod.All...),
}.Run("corrupt_data1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte{127, 255, 127},
Values: mod.Values{[]byte{}, ""}.AddVariants(mod.All...),
}.Run("corrupt_data2", t, unmarshal)
})
}
}
83 changes: 52 additions & 31 deletions tests/serialization/marshal_12_ascii_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,64 @@ 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/ascii"
)

func TestMarshalAscii(t *testing.T) {
tType := gocql.NewNativeType(4, gocql.TypeAscii, "")

marshal := func(i interface{}) ([]byte, error) {
return gocql.Marshal(tType, i)
type testSuite struct {
name string
marshal func(interface{}) ([]byte, error)
unmarshal func(bytes []byte, i interface{}) error
}
unmarshal := func(bytes []byte, i interface{}) error {
return gocql.Unmarshal(tType, bytes, i)

testSuites := [2]testSuite{
{
name: "serialization.int",
marshal: ascii.Marshal,
unmarshal: ascii.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)
},
},
}

// unmarshal `zero` data into ([]byte)(nil), (*[]byte)(*[nil])
brokenZeroSlices := serialization.GetTypes(make([]byte, 0), (*[]byte)(nil))

serialization.PositiveSet{
Data: nil,
Values: mod.Values{
([]byte)(nil),
(*[]byte)(nil),
(*string)(nil),
}.AddVariants(mod.CustomType),
}.Run("[nil]nullable", t, marshal, unmarshal)

serialization.PositiveSet{
Data: nil,
Values: mod.Values{""}.AddVariants(mod.CustomType),
}.Run("[nil]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: make([]byte, 0),
Values: mod.Values{make([]byte, 0), ""}.AddVariants(mod.All...),
BrokenUnmarshalTypes: brokenZeroSlices,
}.Run("[]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: []byte("test text string"),
Values: mod.Values{[]byte("test text string"), "test text string"}.AddVariants(mod.All...),
}.Run("text", t, nil, unmarshal)
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),
(*string)(nil),
}.AddVariants(mod.CustomType),
}.Run("[nil]nullable", t, marshal, unmarshal)

serialization.PositiveSet{
Data: nil,
Values: mod.Values{""}.AddVariants(mod.CustomType),
}.Run("[nil]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: make([]byte, 0),
Values: mod.Values{make([]byte, 0), ""}.AddVariants(mod.All...),
}.Run("[]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: []byte("test text string"),
Values: mod.Values{[]byte("test text string"), "test text string"}.AddVariants(mod.All...),
}.Run("text", t, nil, unmarshal)
})
}
}

0 comments on commit 6bc5d3e

Please sign in to comment.