Skip to content

Commit

Permalink
fix date tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Dec 13, 2024
1 parent 65ebeb5 commit 8a691c0
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 256 deletions.
120 changes: 0 additions & 120 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1221,126 +1221,6 @@ func TestUnmarshalInetCopyBytes(t *testing.T) {
}
}

func TestUnmarshalDate(t *testing.T) {
data := []uint8{0x80, 0x0, 0x43, 0x31}
var date time.Time
if err := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &date); err != nil {
t.Fatal(err)
}

expectedDate := "2017-02-04"
formattedDate := date.Format("2006-01-02")
if expectedDate != formattedDate {
t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
return
}
var stringDate string
if err2 := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &stringDate); err2 != nil {
t.Fatal(err2)
}
if expectedDate != stringDate {
t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
return
}
}

func TestMarshalDate(t *testing.T) {
now := time.Now().UTC()
timestamp := now.UnixNano() / int64(time.Millisecond)
expectedData := encInt(int32(timestamp/86400000 + int64(1<<31)))

var marshalDateTests = []struct {
Info TypeInfo
Data []byte
Value interface{}
}{
{
NativeType{proto: 4, typ: TypeDate},
expectedData,
timestamp,
},
{
NativeType{proto: 4, typ: TypeDate},
expectedData,
now,
},
{
NativeType{proto: 4, typ: TypeDate},
expectedData,
&now,
},
{
NativeType{proto: 4, typ: TypeDate},
expectedData,
now.Format("2006-01-02"),
},
}

for i, test := range marshalDateTests {
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 TestLargeDate(t *testing.T) {
farFuture := time.Date(999999, time.December, 31, 0, 0, 0, 0, time.UTC)
expectedFutureData := encInt(int32(farFuture.UnixMilli()/86400000 + int64(1<<31)))

farPast := time.Date(-999999, time.January, 1, 0, 0, 0, 0, time.UTC)
expectedPastData := encInt(int32(farPast.UnixMilli()/86400000 + int64(1<<31)))

var marshalDateTests = []struct {
Data []byte
Value interface{}
ExpectedDate string
}{
{
expectedFutureData,
farFuture,
"999999-12-31",
},
{
expectedPastData,
farPast,
"-999999-01-01",
},
}

nativeType := NativeType{proto: 4, typ: TypeDate}

for i, test := range marshalDateTests {
t.Log(i, test)

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

var date time.Time
if err := Unmarshal(nativeType, data, &date); err != nil {
t.Fatal(err)
}

formattedDate := date.Format("2006-01-02")
if test.ExpectedDate != formattedDate {
t.Fatalf("largeDateTest: expected %v, got %v", test.ExpectedDate, formattedDate)
}
}
}

func BenchmarkUnmarshalVarchar(b *testing.B) {
b.ReportAllocs()
src := make([]byte, 1024)
Expand Down
151 changes: 82 additions & 69 deletions tests/serialization/marshal_17_date_corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,101 @@ 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/date"
)

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

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
}

// marshal the `int64`, `time.Time` values which out of the `cql type` range, does not return an error.
brokenMarshalTypes := serialization.GetTypes(int64(0), (*int64)(nil), time.Time{}, &time.Time{})
testSuites := [2]testSuite{
{
name: "serialization.date",
marshal: date.Marshal,
unmarshal: date.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 of `string`, `time.Time` does not return an error on all type of data corruption.
brokenUnmarshalTypes := serialization.GetTypes(string(""), (*string)(nil), time.Time{}, &time.Time{})
for _, tSuite := range testSuites {
marshal := tSuite.marshal
unmarshal := tSuite.unmarshal

serialization.NegativeMarshalSet{
Values: mod.Values{
time.Date(5881580, 7, 12, 0, 0, 0, 0, time.UTC).UTC().UnixMilli(),
time.Date(5881580, 8, 11, 0, 0, 0, 0, time.UTC).UTC().UnixMilli(),
time.Date(5881581, 7, 11, 0, 0, 0, 0, time.UTC).UTC().UnixMilli(),
time.Date(5883581, 12, 20, 0, 0, 0, 0, time.UTC).UTC().UnixMilli(),
"5881580-07-12", "5881580-08-11", "5881581-07-11", "9223372036854775807-07-12",
time.Date(5881580, 7, 12, 0, 0, 0, 0, time.UTC).UTC(),
time.Date(5881580, 8, 11, 0, 0, 0, 0, time.UTC).UTC(),
time.Date(5881581, 7, 11, 0, 0, 0, 0, time.UTC).UTC(),
time.Date(5883581, 12, 20, 0, 0, 0, 0, time.UTC).UTC(),
}.AddVariants(mod.All...),
BrokenTypes: brokenMarshalTypes,
}.Run("big_vals", t, marshal)
t.Run(tSuite.name, func(t *testing.T) {
serialization.NegativeMarshalSet{
Values: mod.Values{
time.Date(5881580, 7, 12, 0, 0, 0, 0, time.UTC).UnixMilli(),
time.Date(5881580, 8, 11, 0, 0, 0, 0, time.UTC).UnixMilli(),
time.Date(5881581, 7, 11, 0, 0, 0, 0, time.UTC).UnixMilli(),
time.Date(5883581, 12, 20, 0, 0, 0, 0, time.UTC).UnixMilli(),
"5881580-07-12", "5881580-08-11", "5881581-07-11", "9223372036854775807-07-12",
time.Date(5881580, 7, 12, 0, 0, 0, 0, time.UTC).UTC(),
time.Date(5881580, 8, 11, 0, 0, 0, 0, time.UTC).UTC(),
time.Date(5881581, 7, 11, 0, 0, 0, 0, time.UTC).UTC(),
time.Date(5883581, 12, 20, 0, 0, 0, 0, time.UTC).UTC(),
}.AddVariants(mod.All...),
}.Run("big_vals", t, marshal)

serialization.NegativeMarshalSet{
Values: mod.Values{
time.Date(-5877641, 06, 24, 0, 0, 0, 0, time.UTC).UTC().UnixMilli(),
time.Date(-5877641, 07, 23, 0, 0, 0, 0, time.UTC).UTC().UnixMilli(),
time.Date(-5877642, 06, 23, 0, 0, 0, 0, time.UTC).UTC().UnixMilli(),
time.Date(-5887641, 06, 23, 0, 0, 0, 0, time.UTC).UTC().UnixMilli(),
"5881580-07-12", "5881580-08-11", "5881581-07-11", "9223372036854775807-07-12",
"-5877641-06-24", "-5877641-07-23", "-5877642-06-23", "-9223372036854775807-07-12",
time.Date(-5877641, 06, 24, 0, 0, 0, 0, time.UTC).UTC(),
time.Date(-5877641, 07, 23, 0, 0, 0, 0, time.UTC).UTC(),
time.Date(-5877642, 06, 23, 0, 0, 0, 0, time.UTC).UTC(),
time.Date(-5887641, 06, 23, 0, 0, 0, 0, time.UTC).UTC(),
}.AddVariants(mod.All...),
BrokenTypes: brokenMarshalTypes,
}.Run("small_vals", t, marshal)
serialization.NegativeMarshalSet{
Values: mod.Values{
time.Date(-5877641, 06, 22, 0, 0, 0, 0, time.UTC).UnixMilli(),
time.Date(-5877641, 05, 23, 0, 0, 0, 0, time.UTC).UnixMilli(),
time.Date(-5877642, 06, 23, 0, 0, 0, 0, time.UTC).UnixMilli(),
time.Date(-5887641, 06, 23, 0, 0, 0, 0, time.UTC).UnixMilli(),
"-5877641-06-22", "-5877641-05-23", "-5877642-06-23", "-9223372036854775807-07-12",
time.Date(-5877641, 06, 22, 0, 0, 0, 0, time.UTC),
time.Date(-5877641, 05, 23, 0, 0, 0, 0, time.UTC),
time.Date(-5877642, 06, 23, 0, 0, 0, 0, time.UTC),
time.Date(-5887641, 06, 23, 0, 0, 0, 0, time.UTC),
}.AddVariants(mod.All...),
}.Run("small_vals", t, marshal)

serialization.NegativeMarshalSet{
Values: mod.Values{
"a1580-07-11", "1970-0d-11", "02-11", "1970-11",
}.AddVariants(mod.All...),
}.Run("corrupt_vals", t, marshal)
serialization.NegativeMarshalSet{
Values: mod.Values{
"a1580-07-11", "1970-0d-11", "02-11", "1970-11",
}.AddVariants(mod.All...),
}.Run("corrupt_vals", t, marshal)

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

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\x4e\x94\x91\x4e\xff\xff\xff"),
Values: mod.Values{
int64(0), time.Time{}, "",
}.AddVariants(mod.All...),
BrokenTypes: brokenUnmarshalTypes,
}.Run("big_data2", t, unmarshal)
serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\x4e\x94\x91\x4e\xff\xff\xff"),
Values: mod.Values{
int64(0), time.Time{}, "",
}.AddVariants(mod.All...),
}.Run("big_data2", t, unmarshal)

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\x00"),
Values: mod.Values{
int64(0), time.Time{}, "",
}.AddVariants(mod.All...),
BrokenTypes: brokenUnmarshalTypes,
}.Run("small_data1", t, unmarshal)
serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00\x00"),
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...),
BrokenTypes: brokenUnmarshalTypes,
}.Run("small_data2", t, unmarshal)
serialization.NegativeUnmarshalSet{
Data: []byte("\x00"),
Values: mod.Values{
int64(0), time.Time{}, "",
}.AddVariants(mod.All...),
}.Run("small_data2", t, unmarshal)
})
}
}
Loading

0 comments on commit 8a691c0

Please sign in to comment.