Skip to content

Commit

Permalink
fix timestamp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Dec 9, 2024
1 parent f0daafd commit 8fb31b4
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 169 deletions.
2 changes: 1 addition & 1 deletion cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,7 @@ func TestEmptyTimestamp(t *testing.T) {
t.Fatalf("failed to create table with error '%v'", err)
}

if err := session.Query("INSERT INTO test_empty_timestamp (id, num) VALUES (?,?)", 1, 561).Exec(); err != nil {
if err := session.Query("INSERT INTO test_empty_timestamp (id, num) VALUES (?,?)", 1, 0).Exec(); err != nil {
t.Fatalf("failed to insert with err: %v", err)
}

Expand Down
97 changes: 0 additions & 97 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
int64(1376387523000),
nil,
nil,
},
{
NativeType{proto: 5, typ: TypeDuration},
[]byte("\x89\xa2\xc3\xc2\x9a\xe0F\x91\x06"),
Expand Down Expand Up @@ -317,23 +303,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
func() *time.Time {
t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
return &t
}(),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimestamp},
[]byte(nil),
(*time.Time)(nil),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBoolean},
[]byte("\x00"),
Expand Down Expand Up @@ -863,72 +832,6 @@ func TestMarshalPointer(t *testing.T) {
}
}

func TestMarshalTimestamp(t *testing.T) {
var marshalTimestampTests = []struct {
Info TypeInfo
Data []byte
Value interface{}
}{
{
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
},
{
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
int64(1376387523000),
},
{
// 9223372036854 is the maximum time representable in ms since the epoch
// with int64 if using UnixNano to convert
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
time.Date(2262, time.April, 11, 23, 47, 16, 854775807, time.UTC),
},
{
// One nanosecond after causes overflow when using UnixNano
// Instead it should resolve to the same time in ms
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\x00\x00\x08\x63\x7b\xd0\x5a\xf6"),
time.Date(2262, time.April, 11, 23, 47, 16, 854775808, time.UTC),
},
{
// -9223372036855 is the minimum time representable in ms since the epoch
// with int64 if using UnixNano to convert
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
time.Date(1677, time.September, 21, 00, 12, 43, 145224192, time.UTC),
},
{
// One nanosecond earlier causes overflow when using UnixNano
// it should resolve to the same time in ms
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\xff\xff\xf7\x9c\x84\x2f\xa5\x09"),
time.Date(1677, time.September, 21, 00, 12, 43, 145224191, time.UTC),
},
{
// Store the zero time as a blank slice
NativeType{proto: 2, typ: TypeTimestamp},
[]byte{},
time.Time{},
},
}

for i, test := range marshalTimestampTests {
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, decBigInt(test.Data), data, decBigInt(data), test.Value)
}
}
}

func TestMarshalTuple(t *testing.T) {
info := TupleTypeInfo{
NativeType: NativeType{proto: 3, typ: TypeTuple},
Expand Down
77 changes: 49 additions & 28 deletions tests/serialization/marshal_16_timestamp_corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,60 @@ 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/timestamp"
)

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

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
}

// unmarshal of all supported `go types` does not return an error on all type of corruption.
brokenTypes := serialization.GetTypes(int64(0), (*int64)(nil), mod.Int64(0), (*mod.Int64)(nil), time.Time{}, (*time.Time)(nil))

serialization.NegativeUnmarshalSet{
Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff\xff"),
Values: mod.Values{
int64(0), time.Time{},
}.AddVariants(mod.All...),
BrokenTypes: brokenTypes,
}.Run("big_data", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\xff\xff\xff\xff\xff\xff\xff"),
Values: mod.Values{
int64(0), time.Time{},
}.AddVariants(mod.All...),
BrokenTypes: brokenTypes,
}.Run("small_data1", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00"),
Values: mod.Values{
int64(0), time.Time{},
}.AddVariants(mod.All...),
BrokenTypes: brokenTypes,
}.Run("small_data2", t, unmarshal)
testSuites := [2]testSuite{
{
name: "serialization.timestamp",
marshal: timestamp.Marshal,
unmarshal: timestamp.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 {
unmarshal := tSuite.unmarshal

t.Run(tSuite.name, func(t *testing.T) {

serialization.NegativeUnmarshalSet{
Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff\xff"),
Values: mod.Values{
int64(0), time.Time{},
}.AddVariants(mod.All...),
}.Run("big_data", t, unmarshal)

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

serialization.NegativeUnmarshalSet{
Data: []byte("\x00"),
Values: mod.Values{
int64(0), time.Time{},
}.AddVariants(mod.All...),
}.Run("small_data2", t, unmarshal)
})
}
}
107 changes: 64 additions & 43 deletions tests/serialization/marshal_16_timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,83 @@ 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/timestamp"
)

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

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
}

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

zeroTime := time.UnixMilli(0).UTC()

// unmarshall `nil` and `zero` data returns a negative value of the `time.Time{}`
brokenTime := serialization.GetTypes(time.Time{}, &time.Time{})
for _, tSuite := range testSuites {
marshal := tSuite.marshal
unmarshal := tSuite.unmarshal

serialization.PositiveSet{
Data: nil,
Values: mod.Values{
(*int64)(nil), (*time.Time)(nil),
}.AddVariants(mod.CustomType),
}.Run("[nil]nullable", t, marshal, unmarshal)
t.Run(tSuite.name, func(t *testing.T) {
serialization.PositiveSet{
Data: nil,
Values: mod.Values{
(*int64)(nil), (*time.Time)(nil),
}.AddVariants(mod.CustomType),
}.Run("[nil]nullable", t, marshal, unmarshal)

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

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

serialization.PositiveSet{
Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
Values: mod.Values{
int64(0), zeroTime,
}.AddVariants(mod.All...),
}.Run("zeros", t, marshal, unmarshal)
serialization.PositiveSet{
Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
Values: mod.Values{
int64(0), zeroTime,
}.AddVariants(mod.All...),
}.Run("zeros", t, marshal, unmarshal)

serialization.PositiveSet{
Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
Values: mod.Values{
int64(math.MaxInt64), time.UnixMilli(math.MaxInt64).UTC(),
}.AddVariants(mod.All...),
}.Run("max", t, marshal, unmarshal)
serialization.PositiveSet{
Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
Values: mod.Values{
int64(math.MaxInt64), time.UnixMilli(math.MaxInt64).UTC(),
}.AddVariants(mod.All...),
}.Run("max", t, marshal, unmarshal)

serialization.PositiveSet{
Data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
Values: mod.Values{
int64(math.MinInt64), time.UnixMilli(math.MinInt64).UTC(),
}.AddVariants(mod.All...),
}.Run("min", t, marshal, unmarshal)
serialization.PositiveSet{
Data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
Values: mod.Values{
int64(math.MinInt64), time.UnixMilli(math.MinInt64).UTC(),
}.AddVariants(mod.All...),
}.Run("min", t, marshal, unmarshal)
})
}
}

0 comments on commit 8fb31b4

Please sign in to comment.