Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Oct 15, 2024
1 parent 826074e commit 6b215f1
Show file tree
Hide file tree
Showing 4 changed files with 625 additions and 526 deletions.
4 changes: 2 additions & 2 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1900,8 +1900,8 @@ func TestVarint(t *testing.T) {
}

err := session.Query("SELECT test FROM varint_test").Scan(&result64)
if err == nil || strings.Index(err.Error(), "out of range") == -1 {
t.Errorf("expected out of range error since value is too big for int64")
if err == nil || strings.Index(err.Error(), "the data value should be in the int64 range") == -1 {
t.Errorf("expected out of range error since value is too big for int64, result:%d", result64)
}

// value not set in cassandra, leave bind variable empty
Expand Down
192 changes: 0 additions & 192 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,55 +362,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\x00"),
0,
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\x37\xE2\x3C\xEC"),
int32(937573612),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\x37\xE2\x3C\xEC"),
big.NewInt(937573612),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\xC9v\x8D:\x86"),
big.NewInt(-234234234234), // From the iconara/cql-rb test suite
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte(nil),
nil,
nil,
unmarshalErrorf("can not unmarshal into non-pointer <nil>"),
},
{
NativeType{proto: 2, typ: TypeInet},
[]byte("\x7F\x00\x00\x01"),
Expand Down Expand Up @@ -937,149 +888,6 @@ func TestMarshal_Decode(t *testing.T) {
}
}

func TestMarshalVarint(t *testing.T) {
varintTests := []struct {
Value interface{}
Marshaled []byte
Unmarshaled *big.Int
}{
{
Value: int8(0),
Marshaled: []byte("\x00"),
Unmarshaled: big.NewInt(0),
},
{
Value: uint8(255),
Marshaled: []byte("\x00\xFF"),
Unmarshaled: big.NewInt(255),
},
{
Value: int8(-1),
Marshaled: []byte("\xFF"),
Unmarshaled: big.NewInt(-1),
},
{
Value: big.NewInt(math.MaxInt32),
Marshaled: []byte("\x7F\xFF\xFF\xFF"),
Unmarshaled: big.NewInt(math.MaxInt32),
},
{
Value: big.NewInt(int64(math.MaxInt32) + 1),
Marshaled: []byte("\x00\x80\x00\x00\x00"),
Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
},
{
Value: big.NewInt(math.MinInt32),
Marshaled: []byte("\x80\x00\x00\x00"),
Unmarshaled: big.NewInt(math.MinInt32),
},
{
Value: big.NewInt(int64(math.MinInt32) - 1),
Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
},
{
Value: math.MinInt64,
Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
Unmarshaled: big.NewInt(math.MinInt64),
},
{
Value: uint64(math.MaxInt64) + 1,
Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
Unmarshaled: bigintize("9223372036854775808"),
},
{
Value: bigintize("2361183241434822606848"), // 2**71
Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
Unmarshaled: bigintize("2361183241434822606848"),
},
{
Value: bigintize("-9223372036854775809"), // -2**63 - 1
Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
Unmarshaled: bigintize("-9223372036854775809"),
},
}

for i, test := range varintTests {
data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
if err != nil {
t.Errorf("error marshaling varint: %v (test #%d)", err, i)
}

if !bytes.Equal(test.Marshaled, data) {
t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
}

binder := new(big.Int)
err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder)
if err != nil {
t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
}

if test.Unmarshaled.Cmp(binder) != 0 {
t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
}
}

varintUint64Tests := []struct {
Value interface{}
Marshaled []byte
Unmarshaled uint64
}{
{
Value: int8(0),
Marshaled: []byte("\x00"),
Unmarshaled: 0,
},
{
Value: uint8(255),
Marshaled: []byte("\x00\xFF"),
Unmarshaled: 255,
},
{
Value: big.NewInt(math.MaxInt32),
Marshaled: []byte("\x7F\xFF\xFF\xFF"),
Unmarshaled: uint64(math.MaxInt32),
},
{
Value: big.NewInt(int64(math.MaxInt32) + 1),
Marshaled: []byte("\x00\x80\x00\x00\x00"),
Unmarshaled: uint64(int64(math.MaxInt32) + 1),
},
{
Value: uint64(math.MaxInt64) + 1,
Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
Unmarshaled: 9223372036854775808,
},
{
Value: uint64(math.MaxUint64),
Marshaled: []byte("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
Unmarshaled: uint64(math.MaxUint64),
},
}

for i, test := range varintUint64Tests {
data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
if err != nil {
t.Errorf("error marshaling varint: %v (test #%d)", err, i)
}

if !bytes.Equal(test.Marshaled, data) {
t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
}

var binder uint64
err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, &binder)
if err != nil {
t.Errorf("error unmarshaling varint to uint64: %v (test #%d)", err, i)
}

if test.Unmarshaled != binder {
t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
}
}
}

func equalStringPointerSlice(leftList, rightList []*string) bool {
if len(leftList) != len(rightList) {
return false
Expand Down
127 changes: 127 additions & 0 deletions tests/serialization/marshal_7_varint_corrupt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package serialization_test

import (
"math/big"
"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/varint"
)

func TestMarshalVarIntCorrupt(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.TypeVarint, "")

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

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x7f"),
Values: mod.Values{
int8(0), int16(0), int32(0), int64(0), int(0),
uint8(0), uint16(0), uint32(0), uint64(0), uint(0),
"", *big.NewInt(0),
}.AddVariants(mod.All...),
}.Run("corrupt_data+", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\xff\x80"),
Values: mod.Values{
int8(0), int16(0), int32(0), int64(0), int(0),
uint8(0), uint16(0), uint32(0), uint64(0), uint(0),
"", *big.NewInt(0),
}.AddVariants(mod.All...),
}.Run("corrupt_data-", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x80"),
Values: mod.Values{int8(0)}.AddVariants(mod.All...),
}.Run("small_type_maxInt8+1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\xff\x7f"),
Values: mod.Values{int8(0)}.AddVariants(mod.All...),
}.Run("small_type_minInt8-1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x80\x00"),
Values: mod.Values{int8(0), int16(0)}.AddVariants(mod.All...),
}.Run("small_type_maxInt16+1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\xff\x7f\xff"),
Values: mod.Values{int8(0)}.AddVariants(mod.All...),
}.Run("small_type_minInt16-1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x80\x00\x00\x00"),
Values: mod.Values{int8(0), int16(0), int32(0)}.AddVariants(mod.All...),
}.Run("small_type_maxInt32+1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\xff\x7f\xff\xff\xff\xff"),
Values: mod.Values{int8(0), int16(0), int32(0)}.AddVariants(mod.All...),
}.Run("small_type_minInt32-1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
Values: mod.Values{int8(0), int16(0), int32(0), int64(0), int(0)}.AddVariants(mod.All...),
}.Run("small_type_maxInt64+1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\xff\x7f\xff\xff\xff\xff\xff\xff\xff\xff"),
Values: mod.Values{int8(0), int16(0), int32(0), int64(0), int(0)}.AddVariants(mod.All...),
}.Run("small_type_minInt64-1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x01\x00"),
Values: mod.Values{uint8(0)}.AddVariants(mod.All...),
}.Run("small_type_maxUint8+1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x01\x00\x00"),
Values: mod.Values{uint8(0), uint16(0)}.AddVariants(mod.All...),
}.Run("small_type_maxUint16+1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x01\x00\x00\x00\x00"),
Values: mod.Values{uint8(0), uint16(0), uint32(0)}.AddVariants(mod.All...),
}.Run("small_type_maxUint32+1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x01\x00\x00\x00\x00\x00\x00\x00\x00"),
Values: mod.Values{uint8(0), uint16(0), uint32(0), uint64(0), uint(0)}.AddVariants(mod.All...),
}.Run("small_type_maxUint64+1", t, unmarshal)
})
}
}
Loading

0 comments on commit 6b215f1

Please sign in to comment.