Skip to content

Commit

Permalink
fix duration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Dec 24, 2024
1 parent d8502e4 commit 97fd1f7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 223 deletions.
100 changes: 1 addition & 99 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ package gocql
import (
"bytes"
"encoding/binary"
"gopkg.in/inf.v0"
"math"
"math/big"
"net"
"reflect"
"strings"
"testing"
"time"

"gopkg.in/inf.v0"
)

type AliasInt int
Expand Down Expand Up @@ -122,27 +120,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x89\xa2\xc3\xc2\x9a\xe0F\x91\x06"),
Duration{Months: 1233, Days: 123213, Nanoseconds: 2312323},
nil,
nil,
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x89\xa1\xc3\xc2\x99\xe0F\x91\x05"),
Duration{Months: -1233, Days: -123213, Nanoseconds: -2312323},
nil,
nil,
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x02\x04\x80\xe6"),
Duration{Months: 1, Days: 2, Nanoseconds: 115},
nil,
nil,
},
{
CollectionType{
NativeType: NativeType{proto: 2, typ: TypeList},
Expand Down Expand Up @@ -546,36 +523,6 @@ var unmarshalTests = []struct {
map[string]int{"foo": 1},
unmarshalErrorf("unmarshal map: unexpected eof"),
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x89\xa2\xc3\xc2\x9a\xe0F\x91"),
Duration{},
unmarshalErrorf("failed to unmarshal duration into *gocql.Duration: failed to extract nanoseconds: data expect to have 9 bytes, but it has only 8"),
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x89\xa2\xc3\xc2\x9a"),
Duration{},
unmarshalErrorf("failed to unmarshal duration into *gocql.Duration: failed to extract nanoseconds: unexpected eof"),
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x89\xa2\xc3\xc2"),
Duration{},
unmarshalErrorf("failed to unmarshal duration into *gocql.Duration: failed to extract days: data expect to have 5 bytes, but it has only 4"),
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x89\xa2"),
Duration{},
unmarshalErrorf("failed to unmarshal duration into *gocql.Duration: failed to extract days: unexpected eof"),
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x89"),
Duration{},
unmarshalErrorf("failed to unmarshal duration into *gocql.Duration: failed to extract month: data expect to have 2 bytes, but it has only 1"),
},
}

func decimalize(s string) *inf.Dec {
Expand Down Expand Up @@ -1234,51 +1181,6 @@ func BenchmarkUnmarshalVarchar(b *testing.B) {
}
}

func TestMarshalDuration(t *testing.T) {
durationS := "1h10m10s"
duration, _ := time.ParseDuration(durationS)
expectedData := append([]byte{0, 0}, encVint(duration.Nanoseconds())...)
var marshalDurationTests = []struct {
Info TypeInfo
Data []byte
Value interface{}
}{
{
NativeType{proto: 5, typ: TypeDuration},
expectedData,
duration.Nanoseconds(),
},
{
NativeType{proto: 5, typ: TypeDuration},
expectedData,
duration,
},
{
NativeType{proto: 5, typ: TypeDuration},
expectedData,
durationS,
},
{
NativeType{proto: 5, typ: TypeDuration},
expectedData,
&duration,
},
}

for i, test := range marshalDurationTests {
t.Log(i, test)
data, err := Marshal(test.Info, test.Value)
if err != nil {
t.Errorf("marshalTest[%d]: %v", i, err)
continue
}
if !bytes.Equal(data, test.Data) {
t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
test.Data, decInt(test.Data), data, decInt(data), test.Value)
}
}
}

func TestReadCollectionSize(t *testing.T) {
listV2 := CollectionType{
NativeType: NativeType{proto: 2, typ: TypeList},
Expand Down
62 changes: 46 additions & 16 deletions tests/serialization/marshal_18_duration_corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ func TestMarshalDurationCorrupt(t *testing.T) {
return gocql.Unmarshal(tType, bytes, i)
}

// unmarshal `gocql.Duration` with data which more cql values (int32,int32,int64) size does not return an error
brokenDuration := serialization.GetTypes(gocql.Duration{}, &gocql.Duration{})

serialization.NegativeMarshalSet{
Values: mod.Values{
"23123113", "sda",
Expand All @@ -34,71 +31,76 @@ func TestMarshalDurationCorrupt(t *testing.T) {
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
BrokenTypes: brokenDuration,
}.Run("big_data_month1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\xf1\x00\x00\x00\x01\x00\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
BrokenTypes: brokenDuration,
}.Run("big_data_month2", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\xf1\x00\x00\x00\x00\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
BrokenTypes: brokenDuration,
}.Run("big_data_day1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\xf1\x00\x00\x00\x01\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
BrokenTypes: brokenDuration,
}.Run("big_data_day2", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe"),
Data: []byte("\x00\x01\xff\xff\xff\xff\xff\xff\xff\xff\xff"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
int64(0), time.Duration(0), "",
}.AddVariants(mod.All...),
BrokenTypes: brokenDuration,
}.Run("big_data_nano1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff"),
Data: []byte("\x01\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
int64(0), time.Duration(0), "",
}.AddVariants(mod.All...),
BrokenTypes: brokenDuration,
}.Run("big_data_nano2", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x01\x00\x41\xfd\xfc\x9b\xc5\xc4\x9e\x00\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "",
}.AddVariants(mod.All...),
}.Run("big_data_nano3", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\xc3\x41\xfd\xfc\x9b\xc5\xc4\x9e\x00\x01"),
Values: mod.Values{
int64(0), time.Duration(0), "",
}.AddVariants(mod.All...),
}.Run("big_data_nano4", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\x00\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
BrokenTypes: brokenDuration,
}.Run("big_data_len1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
BrokenTypes: brokenDuration,
}.Run("big_data_len2", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfd\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
BrokenTypes: brokenDuration,
}.Run("big_data_len3", t, unmarshal)

serialization.NegativeUnmarshalSet{
Expand All @@ -108,6 +110,13 @@ func TestMarshalDurationCorrupt(t *testing.T) {
}.AddVariants(mod.All...),
}.Run("small_data_len1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
}.Run("small_data_len2", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\xf0\xff\xff\xff\xfe\x00"),
Values: mod.Values{
Expand All @@ -121,4 +130,25 @@ func TestMarshalDurationCorrupt(t *testing.T) {
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
}.Run("small_data_len3", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
}.Run("small_data_len_nanos", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\xf0\xff\xff\xff\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
}.Run("small_data_len_days", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\xf0\xff\xff\xff\x00\x00"),
Values: mod.Values{
int64(0), time.Duration(0), "", gocql.Duration{},
}.AddVariants(mod.All...),
}.Run("small_data_len_months", t, unmarshal)
}
Loading

0 comments on commit 97fd1f7

Please sign in to comment.