Skip to content

Commit

Permalink
fix decimal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Dec 8, 2024
1 parent 5a6a0fc commit f010340
Show file tree
Hide file tree
Showing 3 changed files with 417 additions and 43 deletions.
6 changes: 0 additions & 6 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,12 +598,6 @@ var unmarshalTests = []struct {
map[string]int{"foo": 1},
unmarshalErrorf("unmarshal map: unexpected eof"),
},
{
NativeType{proto: 2, typ: TypeDecimal},
[]byte("\xff\xff\xff"),
inf.NewDec(0, 0), // From the datastax/python-driver test suite
unmarshalErrorf("inf.Dec needs at least 4 bytes, while value has only 3"),
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x89\xa2\xc3\xc2\x9a\xe0F\x91"),
Expand Down
69 changes: 69 additions & 0 deletions tests/serialization/marshal_10_decimal_corrupt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package serialization_test

import (
"gopkg.in/inf.v0"
"testing"

"github.com/gocql/gocql"
"github.com/gocql/gocql/internal/tests/serialization"
"github.com/gocql/gocql/internal/tests/serialization/mod"
"github.com/gocql/gocql/serialization/decimal"
)

func TestMarshalDecimalCorrupt(t *testing.T) {
type testSuite struct {
name string
marshal func(interface{}) ([]byte, error)
unmarshal func(bytes []byte, i interface{}) error
}

tType := gocql.NewNativeType(4, gocql.TypeDecimal, "")

testSuites := [2]testSuite{
{
name: "serialization.decimal",
marshal: decimal.Marshal,
unmarshal: decimal.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{"1s2", "1s", "-1s", ",1", "0,1"}.AddVariants(mod.All...),
}.Run("corrupt_vals", t, marshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\x00\x00\x00\x7f"),
Values: mod.Values{*inf.NewDec(0, 0), ""}.AddVariants(mod.All...),
}.Run("corrupt_data+", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\x00\x00\xff\x80"),
Values: mod.Values{*inf.NewDec(0, 0), ""}.AddVariants(mod.All...),
}.Run("corrupt_data-", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\x00\x00"),
Values: mod.Values{*inf.NewDec(0, 0), ""}.AddVariants(mod.All...),
}.Run("small_data1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00"),
Values: mod.Values{*inf.NewDec(0, 0), ""}.AddVariants(mod.All...),
}.Run("small_data2", t, unmarshal)
})
}
}
Loading

0 comments on commit f010340

Please sign in to comment.