From 8a691c064e2cdfd0a1cad81a9adda7fb4efc988a Mon Sep 17 00:00:00 2001 From: illia-li Date: Wed, 11 Dec 2024 16:55:27 -0400 Subject: [PATCH] fix `date` tests --- marshal_test.go | 120 -------------- .../marshal_17_date_corrupt_test.go | 151 ++++++++++-------- tests/serialization/marshal_17_date_test.go | 138 ++++++++-------- 3 files changed, 153 insertions(+), 256 deletions(-) diff --git a/marshal_test.go b/marshal_test.go index fbb7b9146..59a5211dc 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -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) diff --git a/tests/serialization/marshal_17_date_corrupt_test.go b/tests/serialization/marshal_17_date_corrupt_test.go index 2f1f6197e..21b22764d 100644 --- a/tests/serialization/marshal_17_date_corrupt_test.go +++ b/tests/serialization/marshal_17_date_corrupt_test.go @@ -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) + }) + } } diff --git a/tests/serialization/marshal_17_date_test.go b/tests/serialization/marshal_17_date_test.go index 37fc4df55..30035eaab 100644 --- a/tests/serialization/marshal_17_date_test.go +++ b/tests/serialization/marshal_17_date_test.go @@ -4,88 +4,92 @@ package serialization_test import ( + "math" "testing" "time" "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 TestMarshalsDate(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 + } + + 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) + }, + }, } zeroDate := time.Date(-5877641, 06, 23, 0, 0, 0, 0, time.UTC).UTC() middleDate := time.UnixMilli(0).UTC() maxDate := time.Date(5881580, 07, 11, 0, 0, 0, 0, time.UTC).UTC() - // marshal strings with big year like "-5877641-06-23" returns an error - brokenBigString := serialization.GetTypes(string(""), (*string)(nil)) - - // marshal `custom string` and `custom int64` unsupported - brokenMarshal := serialization.GetTypes(mod.String(""), (*mod.String)(nil), mod.Int64(0), (*mod.Int64)(nil)) - - // unmarshal `zero` data into not zero string and time.Time - brokenZero := serialization.GetTypes(time.Time{}, &time.Time{}, string(""), (*string)(nil)) - - // unmarshal `nil` data into not zero time.Time - brokenNil := serialization.GetTypes(time.Time{}) - - // unmarshal into `custom string`, `int64` and `custom int64` unsupported - brokenUnmarshal := serialization.GetTypes(mod.String(""), (*mod.String)(nil), mod.Int64(0), (*mod.Int64)(nil), int64(0), (*int64)(nil)) - - serialization.PositiveSet{ - Data: nil, - Values: mod.Values{ - (*int64)(nil), (*time.Time)(nil), (*string)(nil), - }.AddVariants(mod.CustomType), - }.Run("[nil]nullable", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: nil, - Values: mod.Values{ - int64(0), zeroDate, "", - }.AddVariants(mod.CustomType), - BrokenUnmarshalTypes: append(brokenUnmarshal, brokenNil...), - }.Run("[nil]unmarshal", t, nil, unmarshal) - - serialization.PositiveSet{ - Data: make([]byte, 0), - Values: mod.Values{ - int64(0), zeroDate, "-5877641-06-23", - }.AddVariants(mod.All...), - BrokenUnmarshalTypes: append(brokenUnmarshal, brokenZero...), - }.Run("[]unmarshal", t, nil, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\x00\x00\x00"), - Values: mod.Values{ - zeroDate.UnixMilli(), zeroDate, "-5877641-06-23", - }.AddVariants(mod.All...), - BrokenMarshalTypes: append(brokenMarshal, brokenBigString...), - BrokenUnmarshalTypes: brokenUnmarshal, - }.Run("zeros", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x80\x00\x00\x00"), - Values: mod.Values{ - middleDate.UnixMilli(), middleDate, "1970-01-01", - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenMarshal, - BrokenUnmarshalTypes: brokenUnmarshal, - }.Run("middle", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\xff\xff\xff\xff"), - Values: mod.Values{ - maxDate.UnixMilli(), maxDate, "5881580-07-11", - }.AddVariants(mod.All...), - BrokenMarshalTypes: append(brokenMarshal, brokenBigString...), - BrokenUnmarshalTypes: brokenUnmarshal, - }.Run("max", t, marshal, unmarshal) + for _, tSuite := range testSuites { + marshal := tSuite.marshal + unmarshal := tSuite.unmarshal + + t.Run(tSuite.name, func(t *testing.T) { + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{ + (*uint32)(nil), (*int32)(nil), (*int64)(nil), (*string)(nil), (*time.Time)(nil), + }.AddVariants(mod.CustomType), + }.Run("[nil]nullable", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{ + uint32(0), int32(0), zeroDate.UnixMilli(), "", zeroDate, + }.AddVariants(mod.CustomType), + }.Run("[nil]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: make([]byte, 0), + Values: mod.Values{ + uint32(0), int32(0), zeroDate.UnixMilli(), zeroDate, "-5877641-06-23", + }.AddVariants(mod.All...), + }.Run("[]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x00\x00\x00"), + Values: mod.Values{ + uint32(0), int32(0), zeroDate.UnixMilli(), zeroDate, "-5877641-06-23", + }.AddVariants(mod.All...), + }.Run("zeros", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80\x00\x00\x00"), + Values: mod.Values{ + uint32(1 << 31), int32(math.MinInt32), middleDate.UnixMilli(), middleDate, "1970-01-01", + }.AddVariants(mod.All...), + }.Run("middle", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\xff\xff\xff"), + Values: mod.Values{ + uint32(math.MaxUint32), int32(-1), maxDate.UnixMilli(), maxDate, "5881580-07-11", + }.AddVariants(mod.All...), + }.Run("max", t, marshal, unmarshal) + }) + } }