diff --git a/marshal/tests/mod/all.go b/internal/tests/serialization/mod/all.go similarity index 100% rename from marshal/tests/mod/all.go rename to internal/tests/serialization/mod/all.go diff --git a/marshal/tests/mod/custom.go b/internal/tests/serialization/mod/custom.go similarity index 100% rename from marshal/tests/mod/custom.go rename to internal/tests/serialization/mod/custom.go diff --git a/marshal/tests/mod/custom_refs.go b/internal/tests/serialization/mod/custom_refs.go similarity index 100% rename from marshal/tests/mod/custom_refs.go rename to internal/tests/serialization/mod/custom_refs.go diff --git a/marshal/tests/mod/refs.go b/internal/tests/serialization/mod/refs.go similarity index 100% rename from marshal/tests/mod/refs.go rename to internal/tests/serialization/mod/refs.go diff --git a/marshal/tests/serialization/pointers.go b/internal/tests/serialization/pointers.go similarity index 81% rename from marshal/tests/serialization/pointers.go rename to internal/tests/serialization/pointers.go index dc4f69ea4..d6a72ac41 100644 --- a/marshal/tests/serialization/pointers.go +++ b/internal/tests/serialization/pointers.go @@ -5,15 +5,15 @@ import ( "reflect" ) -// ErrFirstPtrChanged this error indicates that a double or single reference was passed to the Unmarshal function +// errFirstPtrChanged this error indicates that a double or single reference was passed to the Unmarshal function // (example (**int)(**0) or (*int)(*0)) and Unmarshal overwritten first reference. -var ErrFirstPtrChanged = errors.New("unmarshal function rewrote first pointer") +var errFirstPtrChanged = errors.New("unmarshal function rewrote first pointer") -// ErrSecondPtrNotChanged this error indicates that a double reference was passed to the Unmarshal function +// errSecondPtrNotChanged this error indicates that a double reference was passed to the Unmarshal function // (example (**int)(**0)) and the function did not overwrite the second reference. // Of course, it's not friendly to the garbage collector, overwriting references to values all the time, // but this is the current implementation `gocql` and changing it can lead to unexpected results in some cases. -var ErrSecondPtrNotChanged = errors.New("unmarshal function did not rewrite second pointer") +var errSecondPtrNotChanged = errors.New("unmarshal function did not rewrite second pointer") func getPointers(i interface{}) *pointer { rv := reflect.ValueOf(i) @@ -45,10 +45,10 @@ func (p *pointer) NotNil() bool { func (p *pointer) Valid(v interface{}) error { p2 := getPointers(v) if p.Fist != p2.Fist { - return ErrFirstPtrChanged + return errFirstPtrChanged } if p.Second != 0 && p2.Second != 0 && p2.Second == p.Second { - return ErrSecondPtrNotChanged + return errSecondPtrNotChanged } return nil } diff --git a/marshal/tests/serialization/pointers_test.go b/internal/tests/serialization/pointers_test.go similarity index 100% rename from marshal/tests/serialization/pointers_test.go rename to internal/tests/serialization/pointers_test.go diff --git a/internal/tests/serialization/set_negative_marshal.go b/internal/tests/serialization/set_negative_marshal.go new file mode 100644 index 000000000..2d11c67fb --- /dev/null +++ b/internal/tests/serialization/set_negative_marshal.go @@ -0,0 +1,59 @@ +package serialization + +import ( + "errors" + "reflect" + "runtime/debug" + "testing" +) + +// NegativeMarshalSet is a tool for marshal funcs testing for cases when the function should an error. +type NegativeMarshalSet struct { + Values []interface{} + BrokenTypes []reflect.Type +} + +func (s NegativeMarshalSet) Run(name string, t *testing.T, marshal func(interface{}) ([]byte, error)) { + if name == "" { + t.Fatal("name should be provided") + } + if marshal == nil { + t.Fatal("marshal function should be provided") + } + t.Run(name, func(t *testing.T) { + for m := range s.Values { + val := s.Values[m] + + t.Run(stringValue(val), func(t *testing.T) { + _, err := func() (d []byte, err error) { + defer func() { + if r := recover(); r != nil { + err = panicErr{err: r.(error), stack: debug.Stack()} + } + }() + return marshal(val) + }() + + testFailed := false + wasPanic := errors.As(err, &panicErr{}) + if err == nil || wasPanic { + testFailed = true + } + + if isTypeOf(val, s.BrokenTypes) { + if testFailed { + t.Skipf("skipped bacause there is unsolved problem") + } + t.Fatalf("expected to panic or no error for (%T), but got an error", val) + } + + if testFailed { + if wasPanic { + t.Fatalf("was panic %s", err) + } + t.Errorf("expected an error for (%T), but got no error", val) + } + }) + } + }) +} diff --git a/internal/tests/serialization/set_negative_unmarshal.go b/internal/tests/serialization/set_negative_unmarshal.go new file mode 100644 index 000000000..4e9aa0a21 --- /dev/null +++ b/internal/tests/serialization/set_negative_unmarshal.go @@ -0,0 +1,77 @@ +package serialization + +import ( + "bytes" + "errors" + "fmt" + "reflect" + "runtime/debug" + "testing" +) + +// NegativeUnmarshalSet is a tool for unmarshal funcs testing for cases when the function should an error. +type NegativeUnmarshalSet struct { + Data []byte + Values []interface{} + BrokenTypes []reflect.Type +} + +func (s NegativeUnmarshalSet) Run(name string, t *testing.T, unmarshal func([]byte, interface{}) error) { + if name == "" { + t.Fatal("name should be provided") + } + if unmarshal == nil { + t.Fatal("unmarshal function should be provided") + } + t.Run(name, func(t *testing.T) { + for m := range s.Values { + val := s.Values[m] + + if rt := reflect.TypeOf(val); rt.Kind() != reflect.Ptr { + unmarshalIn := newRef(val) + s.run(fmt.Sprintf("%T", val), t, unmarshal, val, unmarshalIn) + } else { + // Test unmarshal to (*type)(nil) + unmarshalIn := newRef(val) + s.run(fmt.Sprintf("%T**nil", val), t, unmarshal, val, unmarshalIn) + + // Test unmarshal to &type{} + unmarshalInZero := newRefToZero(val) + s.run(fmt.Sprintf("%T**zero", val), t, unmarshal, val, unmarshalInZero) + } + } + }) +} + +func (s NegativeUnmarshalSet) run(name string, t *testing.T, f func([]byte, interface{}) error, val, unmarshalIn interface{}) { + t.Run(name, func(t *testing.T) { + err := func() (err error) { + defer func() { + if r := recover(); r != nil { + err = panicErr{err: r.(error), stack: debug.Stack()} + } + }() + return f(bytes.Clone(s.Data), unmarshalIn) + }() + + testFailed := false + wasPanic := errors.As(err, &panicErr{}) + if err == nil || wasPanic { + testFailed = true + } + + if isTypeOf(val, s.BrokenTypes) { + if testFailed { + t.Skipf("skipped bacause there is unsolved problem") + } + t.Fatalf("expected to panic or no error for (%T), but got an error", unmarshalIn) + } + + if testFailed { + if wasPanic { + t.Fatalf("was panic %s", err) + } + t.Errorf("expected an error for (%T), but got no error", unmarshalIn) + } + }) +} diff --git a/marshal/tests/serialization/set.go b/internal/tests/serialization/set_positive.go similarity index 57% rename from marshal/tests/serialization/set.go rename to internal/tests/serialization/set_positive.go index 7415d3990..99fd9eea5 100644 --- a/marshal/tests/serialization/set.go +++ b/internal/tests/serialization/set_positive.go @@ -7,17 +7,12 @@ import ( "reflect" "runtime/debug" "testing" - - "github.com/gocql/gocql/internal/tests/utils" ) -type Sets []*Set - -// Set is a tool for generating test cases of marshal and unmarshall funcs. -// For cases when the function should no error, -// marshaled data from Set.Values should be equal with Set.Data, -// unmarshalled value from Set.Data should be equal with Set.Values. -type Set struct { +// PositiveSet is a tool for marshal and unmarshall funcs testing for cases when the function should no error, +// on marshal - marshaled data from PositiveSet.Values should be equal with PositiveSet.Data, +// on unmarshall - unmarshalled value from PositiveSet.Data should be equal with PositiveSet.Values. +type PositiveSet struct { Data []byte Values []interface{} @@ -25,7 +20,7 @@ type Set struct { BrokenUnmarshalTypes []reflect.Type } -func (s Set) Run(name string, t *testing.T, marshal func(interface{}) ([]byte, error), unmarshal func([]byte, interface{}) error) { +func (s PositiveSet) Run(name string, t *testing.T, marshal func(interface{}) ([]byte, error), unmarshal func([]byte, interface{}) error) { if name == "" { t.Fatal("name should be provided") } @@ -41,15 +36,15 @@ func (s Set) Run(name string, t *testing.T, marshal func(interface{}) ([]byte, e if unmarshal != nil { if rt := reflect.TypeOf(val); rt.Kind() != reflect.Ptr { - unmarshalIn := utils.NewRef(val) + unmarshalIn := newRef(val) s.runUnmarshalTest("unmarshal", t, unmarshal, val, unmarshalIn) } else { // Test unmarshal to (*type)(nil) - unmarshalIn := utils.NewRef(val) + unmarshalIn := newRef(val) s.runUnmarshalTest("unmarshal**nil", t, unmarshal, val, unmarshalIn) // Test unmarshal to &type{} - unmarshalInZero := utils.NewRefToZero(val) + unmarshalInZero := newRefToZero(val) s.runUnmarshalTest("unmarshal**zero", t, unmarshal, val, unmarshalInZero) } } @@ -58,13 +53,13 @@ func (s Set) Run(name string, t *testing.T, marshal func(interface{}) ([]byte, e }) } -func (s Set) runMarshalTest(t *testing.T, f func(interface{}) ([]byte, error), val interface{}) { +func (s PositiveSet) runMarshalTest(t *testing.T, f func(interface{}) ([]byte, error), val interface{}) { t.Run("marshal", func(t *testing.T) { result, err := func() (d []byte, err error) { defer func() { if r := recover(); r != nil { - err = utils.PanicErr{Err: r.(error), Stack: debug.Stack()} + err = panicErr{err: r.(error), stack: debug.Stack()} } }() return f(val) @@ -72,11 +67,11 @@ func (s Set) runMarshalTest(t *testing.T, f func(interface{}) ([]byte, error), v expected := bytes.Clone(s.Data) if err != nil { - if !errors.As(err, &utils.PanicErr{}) { - err = errors.Join(MarshalErr, err) + if !errors.As(err, &panicErr{}) { + err = errors.Join(marshalErr, err) } - } else if !utils.EqualData(expected, result) { - err = UnequalError{Expected: utils.StringData(s.Data), Got: utils.StringData(result)} + } else if !equalData(expected, result) { + err = unequalError{Expected: stringData(s.Data), Got: stringData(result)} } if isTypeOf(val, s.BrokenMarshalTypes) { @@ -91,7 +86,7 @@ func (s Set) runMarshalTest(t *testing.T, f func(interface{}) ([]byte, error), v }) } -func (s Set) runUnmarshalTest(name string, t *testing.T, f func([]byte, interface{}) error, expected, result interface{}) { +func (s PositiveSet) runUnmarshalTest(name string, t *testing.T, f func([]byte, interface{}) error, expected, result interface{}) { t.Run(name, func(t *testing.T) { expectedPtr := getPointers(result) @@ -99,18 +94,18 @@ func (s Set) runUnmarshalTest(name string, t *testing.T, f func([]byte, interfac err := func() (err error) { defer func() { if r := recover(); r != nil { - err = utils.PanicErr{Err: fmt.Errorf("%s", r), Stack: debug.Stack()} + err = panicErr{err: fmt.Errorf("%s", r), stack: debug.Stack()} } }() return f(bytes.Clone(s.Data), result) }() if err != nil { - if !errors.As(err, &utils.PanicErr{}) { - err = errors.Join(UnmarshalErr, err) + if !errors.As(err, &panicErr{}) { + err = errors.Join(unmarshalErr, err) } - } else if !utils.EqualVals(expected, utils.DeReference(result)) { - err = UnequalError{Expected: utils.StringValue(expected), Got: utils.StringValue(result)} + } else if !equalVals(expected, deReference(result)) { + err = unequalError{Expected: stringValue(expected), Got: stringValue(result)} } else { err = expectedPtr.Valid(result) } diff --git a/internal/tests/utils/utils.go b/internal/tests/serialization/utils.go similarity index 51% rename from internal/tests/utils/utils.go rename to internal/tests/serialization/utils.go index 6b2239b48..667efe993 100644 --- a/internal/tests/utils/utils.go +++ b/internal/tests/serialization/utils.go @@ -1,19 +1,9 @@ -package utils +package serialization import ( "reflect" ) -func DeReference(in interface{}) interface{} { - return reflect.Indirect(reflect.ValueOf(in)).Interface() -} - -func Reference(val interface{}) interface{} { - out := reflect.New(reflect.TypeOf(val)) - out.Elem().Set(reflect.ValueOf(val)) - return out.Interface() -} - func GetTypes(values ...interface{}) []reflect.Type { types := make([]reflect.Type, len(values)) for i, value := range values { @@ -21,3 +11,17 @@ func GetTypes(values ...interface{}) []reflect.Type { } return types } + +func isTypeOf(value interface{}, types []reflect.Type) bool { + valueType := reflect.TypeOf(value) + for i := range types { + if types[i] == valueType { + return true + } + } + return false +} + +func deReference(in interface{}) interface{} { + return reflect.Indirect(reflect.ValueOf(in)).Interface() +} diff --git a/internal/tests/utils/equal.go b/internal/tests/serialization/utils_equal.go similarity index 91% rename from internal/tests/utils/equal.go rename to internal/tests/serialization/utils_equal.go index 71e48af80..9acf5035a 100644 --- a/internal/tests/utils/equal.go +++ b/internal/tests/serialization/utils_equal.go @@ -1,24 +1,23 @@ -package utils +package serialization import ( "bytes" "fmt" + "github.com/gocql/gocql/internal/tests/serialization/mod" "gopkg.in/inf.v0" "math/big" "reflect" "unsafe" - - "github.com/gocql/gocql/marshal/tests/mod" ) -func EqualData(in1, in2 []byte) bool { +func equalData(in1, in2 []byte) bool { if in1 == nil || in2 == nil { return in1 == nil && in2 == nil } return bytes.Equal(in1, in2) } -func EqualVals(in1, in2 interface{}) bool { +func equalVals(in1, in2 interface{}) bool { rin1 := reflect.ValueOf(in1) rin2 := reflect.ValueOf(in2) if rin1.Kind() != rin2.Kind() { diff --git a/internal/tests/serialization/utils_error.go b/internal/tests/serialization/utils_error.go new file mode 100644 index 000000000..12b4ff3f4 --- /dev/null +++ b/internal/tests/serialization/utils_error.go @@ -0,0 +1,27 @@ +package serialization + +import ( + "errors" + "fmt" +) + +var unmarshalErr = errors.New("unmarshal unexpectedly failed with error") +var marshalErr = errors.New("marshal unexpectedly failed with error") + +type unequalError struct { + Expected string + Got string +} + +func (e unequalError) Error() string { + return fmt.Sprintf("expect %s but got %s", e.Expected, e.Got) +} + +type panicErr struct { + err error + stack []byte +} + +func (e panicErr) Error() string { + return fmt.Sprintf("%v\n%s", e.err, e.stack) +} diff --git a/internal/tests/utils/new.go b/internal/tests/serialization/utils_new.go similarity index 67% rename from internal/tests/utils/new.go rename to internal/tests/serialization/utils_new.go index d2901db01..a821272ed 100644 --- a/internal/tests/utils/new.go +++ b/internal/tests/serialization/utils_new.go @@ -1,15 +1,15 @@ -package utils +package serialization import ( "reflect" ) -func NewRef(in interface{}) interface{} { +func newRef(in interface{}) interface{} { out := reflect.New(reflect.TypeOf(in)).Interface() return out } -func NewRefToZero(in interface{}) interface{} { +func newRefToZero(in interface{}) interface{} { rv := reflect.ValueOf(in) nw := reflect.New(rv.Type().Elem()) out := reflect.New(rv.Type()) diff --git a/internal/tests/utils/string_vals.go b/internal/tests/serialization/utils_str.go similarity index 69% rename from internal/tests/utils/string_vals.go rename to internal/tests/serialization/utils_str.go index 73bbf19ee..3634224ec 100644 --- a/internal/tests/utils/string_vals.go +++ b/internal/tests/serialization/utils_str.go @@ -1,4 +1,4 @@ -package utils +package serialization import ( "fmt" @@ -9,16 +9,25 @@ import ( "time" ) -// StringValue returns (value_type)(value) in the human-readable format. -func StringValue(in interface{}) string { - valStr := stringValue(in) +const printLimit = 100 + +// stringValue returns (value_type)(value) in the human-readable format. +func stringValue(in interface{}) string { + valStr := stringVal(in) if len(valStr) > printLimit { valStr = valStr[:printLimit] } return fmt.Sprintf("(%T)(%s)", in, valStr) } -func stringValue(in interface{}) string { +func stringData(p []byte) string { + if len(p) > printLimit { + p = p[:printLimit] + } + return fmt.Sprintf("[%x]", p) +} + +func stringVal(in interface{}) string { switch i := in.(type) { case string: return i @@ -40,7 +49,7 @@ func stringValue(in interface{}) string { if rv.IsNil() { return "*nil" } - return fmt.Sprintf("*%s", stringValue(rv.Elem().Interface())) + return fmt.Sprintf("*%s", stringVal(rv.Elem().Interface())) case reflect.Slice: if rv.IsNil() { return "[nil]" diff --git a/internal/tests/utils/panic_err.go b/internal/tests/utils/panic_err.go deleted file mode 100644 index 45d27cb9c..000000000 --- a/internal/tests/utils/panic_err.go +++ /dev/null @@ -1,14 +0,0 @@ -package utils - -import ( - "fmt" -) - -type PanicErr struct { - Err error - Stack []byte -} - -func (e PanicErr) Error() string { - return fmt.Sprintf("%v\n%s", e.Err, e.Stack) -} diff --git a/internal/tests/utils/string.go b/internal/tests/utils/string.go deleted file mode 100644 index 8d7712518..000000000 --- a/internal/tests/utils/string.go +++ /dev/null @@ -1,14 +0,0 @@ -package utils - -import ( - "fmt" -) - -const printLimit = 100 - -func StringData(p []byte) string { - if len(p) > printLimit { - p = p[:printLimit] - } - return fmt.Sprintf("[%x]", p) -} diff --git a/marshal/tests/serialization/utils.go b/marshal/tests/serialization/utils.go deleted file mode 100644 index 63010dd56..000000000 --- a/marshal/tests/serialization/utils.go +++ /dev/null @@ -1,29 +0,0 @@ -package serialization - -import ( - "errors" - "fmt" - "reflect" -) - -var UnmarshalErr = errors.New("unmarshal unexpectedly failed with error") -var MarshalErr = errors.New("marshal unexpectedly failed with error") - -type UnequalError struct { - Expected string - Got string -} - -func (e UnequalError) Error() string { - return fmt.Sprintf("expect %s but got %s", e.Expected, e.Got) -} - -func isTypeOf(value interface{}, types []reflect.Type) bool { - valueType := reflect.TypeOf(value) - for i := range types { - if types[i] == valueType { - return true - } - } - return false -} diff --git a/marshal_10_decimal_test.go b/marshal_10_decimal_test.go index b4a5ed75e..e2f063506 100644 --- a/marshal_10_decimal_test.go +++ b/marshal_10_decimal_test.go @@ -6,9 +6,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/internal/tests/utils" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalDecimal(t *testing.T) { @@ -20,36 +19,36 @@ func TestMarshalDecimal(t *testing.T) { } // Unmarshal does not support deserialization of `decimal` with `nil` and `zero` `value len` 'into `inf.Dec`. - brokenUnmarshalTypes := utils.GetTypes(inf.Dec{}, (*inf.Dec)(nil)) + brokenUnmarshalTypes := serialization.GetTypes(inf.Dec{}, (*inf.Dec)(nil)) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{(*inf.Dec)(nil)}, }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{inf.Dec{}}, BrokenUnmarshalTypes: brokenUnmarshalTypes, }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{*inf.NewDec(0, 0)}.AddVariants(mod.Reference), BrokenUnmarshalTypes: brokenUnmarshalTypes, }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00"), Values: mod.Values{*inf.NewDec(0, 0)}.AddVariants(mod.Reference), }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xff\xff\xff\x7f\xff\xff\xff\xff\xff\xff\xff"), Values: mod.Values{*inf.NewDec(int64(math.MaxInt64), inf.Scale(int32(math.MaxInt32)))}.AddVariants(mod.Reference), }.Run("max_ints", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00"), Values: mod.Values{*inf.NewDec(int64(math.MinInt64), inf.Scale(int32(math.MinInt32)))}.AddVariants(mod.Reference), }.Run("min_ints", t, marshal, unmarshal) diff --git a/marshal_1_boolean_test.go b/marshal_1_boolean_test.go index f5fb3e78a..dbfa470f8 100644 --- a/marshal_1_boolean_test.go +++ b/marshal_1_boolean_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalBoolean(t *testing.T) { @@ -16,32 +16,32 @@ func TestMarshalBoolean(t *testing.T) { return gocql.Unmarshal(tType, bytes, i) } - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{(*bool)(nil)}.AddVariants(mod.CustomType), }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{false}.AddVariants(mod.CustomType), }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{false}.AddVariants(mod.All...), }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00"), Values: mod.Values{false}.AddVariants(mod.All...), }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x01"), Values: mod.Values{true}.AddVariants(mod.All...), }.Run("[ff]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff"), Values: mod.Values{true}.AddVariants(mod.All...), }.Run("[01]", t, nil, unmarshal) diff --git a/marshal_2_tinyint_test.go b/marshal_2_tinyint_test.go index 64eab3543..80ae976c1 100644 --- a/marshal_2_tinyint_test.go +++ b/marshal_2_tinyint_test.go @@ -5,9 +5,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/internal/tests/utils" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalTinyint(t *testing.T) { @@ -19,17 +18,17 @@ func TestMarshalTinyint(t *testing.T) { } // unmarshal `custom string` unsupported - brokenCustomStrings := utils.GetTypes(mod.String(""), (*mod.String)(nil)) + brokenCustomStrings := serialization.GetTypes(mod.String(""), (*mod.String)(nil)) // marshal "" (empty string) unsupported // unmarshal nil value into (string)("0") - brokenEmptyStrings := utils.GetTypes(string(""), mod.String("")) + brokenEmptyStrings := serialization.GetTypes(string(""), mod.String("")) // marshal `custom string` unsupported // marshal `big.Int` unsupported - brokenMarshalTypes := append(brokenCustomStrings, utils.GetTypes(big.Int{}, &big.Int{})...) + brokenMarshalTypes := append(brokenCustomStrings, serialization.GetTypes(big.Int{}, &big.Int{})...) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), @@ -40,7 +39,7 @@ func TestMarshalTinyint(t *testing.T) { BrokenUnmarshalTypes: brokenEmptyStrings, }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -50,7 +49,7 @@ func TestMarshalTinyint(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -60,7 +59,7 @@ func TestMarshalTinyint(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00"), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -71,21 +70,21 @@ func TestMarshalTinyint(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f"), Values: mod.Values{int8(127), int16(127), int32(127), int64(127), int(127), "127", *big.NewInt(127)}.AddVariants(mod.All...), BrokenMarshalTypes: brokenMarshalTypes, BrokenUnmarshalTypes: brokenCustomStrings, }.Run("127", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80"), Values: mod.Values{int8(-128), int16(-128), int32(-128), int64(-128), int(-128), "-128", *big.NewInt(-128)}.AddVariants(mod.All...), BrokenMarshalTypes: brokenMarshalTypes, BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-128", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff"), Values: mod.Values{uint8(255), uint16(255), uint32(255), uint64(255), uint(255)}.AddVariants(mod.All...), }.Run("255", t, marshal, unmarshal) diff --git a/marshal_3_smallint_corrupt_test.go b/marshal_3_smallint_corrupt_test.go new file mode 100644 index 000000000..262188cf1 --- /dev/null +++ b/marshal_3_smallint_corrupt_test.go @@ -0,0 +1,92 @@ +package gocql_test + +import ( + "math/big" + "testing" + + "github.com/gocql/gocql" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" +) + +func TestMarshalSmallintCorrupt(t *testing.T) { + tType := gocql.NewNativeType(4, gocql.TypeSmallInt, "") + + 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 function does not return an error in cases where the length of the data is different from 0 or 2 + brokenUnmarshalTypes := serialization.GetTypes( + 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...)...) + brokenUnmarshalTypes = append(brokenUnmarshalTypes, serialization.GetTypes("", (*string)(nil))...) + + serialization.NegativeMarshalSet{ + Values: mod.Values{ + int32(32768), int64(32768), int(32768), + "32768", *big.NewInt(32768), + int32(-32769), int64(-32769), int(-32769), + "-32769", *big.NewInt(-32769), + uint32(65536), uint64(65536), uint(65536), + }.AddVariants(mod.All...), + }.Run("big_vals", t, marshal) + + 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("\x80\x00\x00"), + 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...), + BrokenTypes: brokenUnmarshalTypes, + }.Run("big_data", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\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...), + BrokenTypes: brokenUnmarshalTypes, + }.Run("small_data", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x80"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_int8_128", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x7f\xff"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_int8_32767", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xff\x7f"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_int8_-129", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x7f\xff"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_int8_-32768", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x01\x00"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_uint_256", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xff\xff"), + Values: mod.Values{uint8(0)}.AddVariants(mod.All...), + }.Run("small_type_uint_65535", t, unmarshal) +} diff --git a/marshal_3_smallint_test.go b/marshal_3_smallint_test.go index 85997bfd3..2a86e06da 100644 --- a/marshal_3_smallint_test.go +++ b/marshal_3_smallint_test.go @@ -5,9 +5,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/internal/tests/utils" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalSmallint(t *testing.T) { @@ -19,17 +18,17 @@ func TestMarshalSmallint(t *testing.T) { } // unmarshal `custom string` unsupported - brokenCustomStrings := utils.GetTypes(mod.String(""), (*mod.String)(nil)) + brokenCustomStrings := serialization.GetTypes(mod.String(""), (*mod.String)(nil)) // marshal "" (empty string) unsupported // unmarshal nil value into (string)("0") - brokenEmptyStrings := utils.GetTypes(string(""), mod.String("")) + brokenEmptyStrings := serialization.GetTypes(string(""), mod.String("")) // marshal `custom string` unsupported // marshal `big.Int` unsupported - brokenMarshalTypes := append(brokenCustomStrings, utils.GetTypes(big.Int{}, &big.Int{})...) + brokenMarshalTypes := append(brokenCustomStrings, serialization.GetTypes(big.Int{}, &big.Int{})...) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), @@ -40,7 +39,7 @@ func TestMarshalSmallint(t *testing.T) { BrokenUnmarshalTypes: brokenEmptyStrings, }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -50,7 +49,7 @@ func TestMarshalSmallint(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -60,7 +59,7 @@ func TestMarshalSmallint(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00"), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -71,40 +70,40 @@ func TestMarshalSmallint(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x7f"), Values: mod.Values{int8(127), int16(127), int32(127), int64(127), int(127), "127", *big.NewInt(127)}.AddVariants(mod.All...), BrokenMarshalTypes: brokenMarshalTypes, BrokenUnmarshalTypes: brokenCustomStrings, }.Run("127", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\x80"), Values: mod.Values{int8(-128), int16(-128), int32(-128), int64(-128), int(-128), "-128", *big.NewInt(-128)}.AddVariants(mod.All...), BrokenMarshalTypes: brokenMarshalTypes, BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-128", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xff"), Values: mod.Values{int16(32767), int32(32767), int64(32767), int(32767), "32767", *big.NewInt(32767)}.AddVariants(mod.All...), BrokenMarshalTypes: brokenMarshalTypes, BrokenUnmarshalTypes: brokenCustomStrings, }.Run("32767", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80\x00"), Values: mod.Values{int16(-32768), int32(-32768), int64(-32768), int(-32768), "-32768", *big.NewInt(-32768)}.AddVariants(mod.All...), BrokenMarshalTypes: brokenMarshalTypes, BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-32768", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\xff"), Values: mod.Values{uint8(255), uint16(255), uint32(255), uint64(255), uint(255)}.AddVariants(mod.All...), }.Run("255", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff"), Values: mod.Values{uint16(65535), uint32(65535), uint64(65535), uint(65535)}.AddVariants(mod.All...), }.Run("65535", t, marshal, unmarshal) diff --git a/marshal_4_int_test.go b/marshal_4_int_test.go index 29bcc6ebc..605e93a68 100644 --- a/marshal_4_int_test.go +++ b/marshal_4_int_test.go @@ -5,9 +5,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/internal/tests/utils" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalInt(t *testing.T) { @@ -19,20 +18,20 @@ func TestMarshalInt(t *testing.T) { } // unmarshal `custom string` unsupported - brokenCustomStrings := utils.GetTypes(mod.String(""), (*mod.String)(nil)) + brokenCustomStrings := serialization.GetTypes(mod.String(""), (*mod.String)(nil)) // marshal "" (empty string) unsupported // unmarshal nil value into (string)("0") - brokenEmptyStrings := utils.GetTypes(string(""), mod.String("")) + brokenEmptyStrings := serialization.GetTypes(string(""), mod.String("")) // marshal `custom string` unsupported // marshal `big.Int` unsupported - brokenMarshalTypes := append(brokenCustomStrings, utils.GetTypes(big.Int{}, &big.Int{})...) + brokenMarshalTypes := append(brokenCustomStrings, serialization.GetTypes(big.Int{}, &big.Int{})...) // marshal data, which equal math.MaxUint32, into uint32, uit64, uint leads to an error - brokenUints := utils.GetTypes(mod.Uint32(0), mod.Uint64(0), mod.Uint(0), (*mod.Uint32)(nil), (*mod.Uint64)(nil), (*mod.Uint)(nil)) + brokenUints := serialization.GetTypes(mod.Uint32(0), mod.Uint64(0), mod.Uint(0), (*mod.Uint32)(nil), (*mod.Uint64)(nil), (*mod.Uint)(nil)) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), @@ -43,7 +42,7 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenEmptyStrings, }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -53,7 +52,7 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -63,7 +62,7 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00"), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -74,7 +73,7 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xff\xff\xff"), Values: mod.Values{ int32(2147483647), int64(2147483647), int(2147483647), @@ -84,7 +83,7 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("2147483647", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80\x00\x00\x00"), Values: mod.Values{ int32(-2147483648), int64(-2147483648), int(-2147483648), @@ -94,7 +93,7 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-2147483648", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x7f\xff"), Values: mod.Values{ int16(32767), int32(32767), int64(32767), int(32767), @@ -104,7 +103,7 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("32767", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\x80\x00"), Values: mod.Values{ int16(-32768), int32(-32768), int64(-32768), int(-32768), @@ -114,7 +113,7 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-32768", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x7f"), Values: mod.Values{ int8(127), int16(127), int32(127), int64(127), int(127), @@ -124,7 +123,7 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("127", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\x80"), Values: mod.Values{ int8(-128), int16(-128), int32(-128), int64(-128), int(-128), @@ -134,21 +133,21 @@ func TestMarshalInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-128", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\xff"), Values: mod.Values{ uint8(255), uint16(255), uint32(255), uint64(255), uint(255), }.AddVariants(mod.All...), }.Run("255", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\xff\xff"), Values: mod.Values{ uint16(65535), uint32(65535), uint64(65535), uint(65535), }.AddVariants(mod.All...), }.Run("65535", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\xff"), Values: mod.Values{ uint32(4294967295), uint64(4294967295), uint(4294967295), diff --git a/marshal_5_bigint_test.go b/marshal_5_bigint_test.go index 45f350300..c9a39a1f1 100644 --- a/marshal_5_bigint_test.go +++ b/marshal_5_bigint_test.go @@ -5,9 +5,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/internal/tests/utils" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalBigInt(t *testing.T) { @@ -19,20 +18,20 @@ func TestMarshalBigInt(t *testing.T) { } // unmarshal `custom string` unsupported - brokenCustomStrings := utils.GetTypes(mod.String(""), (*mod.String)(nil)) + brokenCustomStrings := serialization.GetTypes(mod.String(""), (*mod.String)(nil)) // marshal "" (empty string) unsupported // unmarshal nil value into (string)("0") - brokenEmptyStrings := utils.GetTypes(string(""), mod.String("")) + brokenEmptyStrings := serialization.GetTypes(string(""), mod.String("")) // marshal `custom string` unsupported // marshal `big.Int` unsupported - brokenMarshalTypes := append(brokenCustomStrings, utils.GetTypes(big.Int{}, &big.Int{})...) + brokenMarshalTypes := append(brokenCustomStrings, serialization.GetTypes(big.Int{}, &big.Int{})...) // marshal data, which equal math.MaxUint64, into uint and uit64 leads to an error - brokenUints := utils.GetTypes(uint(0), mod.Uint64(0), mod.Uint(0), (*uint)(nil), (*mod.Uint64)(nil), (*mod.Uint)(nil)) + brokenUints := serialization.GetTypes(uint(0), mod.Uint64(0), mod.Uint(0), (*uint)(nil), (*mod.Uint64)(nil), (*mod.Uint)(nil)) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), @@ -43,7 +42,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenEmptyStrings, }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -53,7 +52,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -63,7 +62,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -74,7 +73,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"), Values: mod.Values{ int64(9223372036854775807), int(9223372036854775807), @@ -84,7 +83,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("max", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), Values: mod.Values{ int64(-9223372036854775808), int(-9223372036854775808), @@ -94,7 +93,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("min", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x7f\xff\xff\xff"), Values: mod.Values{ int32(2147483647), int64(2147483647), int(2147483647), @@ -104,7 +103,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("2147483647", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\xff\x80\x00\x00\x00"), Values: mod.Values{ int32(-2147483648), int64(-2147483648), int(-2147483648), @@ -114,7 +113,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-2147483648", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x7f\xff"), Values: mod.Values{ int16(32767), int32(32767), int64(32767), int(32767), @@ -124,7 +123,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("32767", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\xff\xff\xff\x80\x00"), Values: mod.Values{ int16(-32768), int32(-32768), int64(-32768), int(-32768), @@ -134,7 +133,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-32768", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x7f"), Values: mod.Values{ int8(127), int16(127), int32(127), int64(127), int(127), @@ -144,7 +143,7 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("127", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\xff\xff\xff\xff\x80"), Values: mod.Values{ int8(-128), int16(-128), int32(-128), int64(-128), int(-128), @@ -154,28 +153,28 @@ func TestMarshalBigInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-128", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x00\xff"), Values: mod.Values{ uint8(255), uint16(255), uint32(255), uint64(255), uint(255), }.AddVariants(mod.All...), }.Run("255", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\xff\xff"), Values: mod.Values{ uint16(65535), uint32(65535), uint64(65535), uint(65535), }.AddVariants(mod.All...), }.Run("65535", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\xff\xff\xff\xff"), Values: mod.Values{ uint32(4294967295), uint64(4294967295), uint(4294967295), }.AddVariants(mod.All...), }.Run("4294967295", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Values: mod.Values{ uint64(18446744073709551615), uint(18446744073709551615), diff --git a/marshal_6_counter_test.go b/marshal_6_counter_test.go index 1e9a5ad96..b05ad7cbc 100644 --- a/marshal_6_counter_test.go +++ b/marshal_6_counter_test.go @@ -5,9 +5,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/internal/tests/utils" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalCounter(t *testing.T) { @@ -19,20 +18,20 @@ func TestMarshalCounter(t *testing.T) { } // unmarshal `custom string` unsupported - brokenCustomStrings := utils.GetTypes(mod.String(""), (*mod.String)(nil)) + brokenCustomStrings := serialization.GetTypes(mod.String(""), (*mod.String)(nil)) // marshal "" (empty string) unsupported // unmarshal nil value into (string)("0") - brokenEmptyStrings := utils.GetTypes(string(""), mod.String("")) + brokenEmptyStrings := serialization.GetTypes(string(""), mod.String("")) // marshal `custom string` unsupported // marshal `big.Int` unsupported - brokenMarshalTypes := append(brokenCustomStrings, utils.GetTypes(big.Int{}, &big.Int{})...) + brokenMarshalTypes := append(brokenCustomStrings, serialization.GetTypes(big.Int{}, &big.Int{})...) // marshal data, which equal math.MaxUint64, into uint and uit64 leads to an error - brokenUints := utils.GetTypes(uint(0), mod.Uint64(0), mod.Uint(0), (*uint)(nil), (*mod.Uint64)(nil), (*mod.Uint)(nil)) + brokenUints := serialization.GetTypes(uint(0), mod.Uint64(0), mod.Uint(0), (*uint)(nil), (*mod.Uint64)(nil), (*mod.Uint)(nil)) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), @@ -43,7 +42,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenEmptyStrings, }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -53,7 +52,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -63,7 +62,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -74,7 +73,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"), Values: mod.Values{ int64(9223372036854775807), int(9223372036854775807), @@ -84,7 +83,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("max", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), Values: mod.Values{ int64(-9223372036854775808), int(-9223372036854775808), @@ -94,7 +93,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("min", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x7f\xff\xff\xff"), Values: mod.Values{ int32(2147483647), int64(2147483647), int(2147483647), @@ -104,7 +103,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("2147483647", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\xff\x80\x00\x00\x00"), Values: mod.Values{ int32(-2147483648), int64(-2147483648), int(-2147483648), @@ -114,7 +113,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-2147483648", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x7f\xff"), Values: mod.Values{ int16(32767), int32(32767), int64(32767), int(32767), @@ -124,7 +123,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("32767", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\xff\xff\xff\x80\x00"), Values: mod.Values{ int16(-32768), int32(-32768), int64(-32768), int(-32768), @@ -134,7 +133,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-32768", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x7f"), Values: mod.Values{ int8(127), int16(127), int32(127), int64(127), int(127), @@ -144,7 +143,7 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("127", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\xff\xff\xff\xff\x80"), Values: mod.Values{ int8(-128), int16(-128), int32(-128), int64(-128), int(-128), @@ -154,28 +153,28 @@ func TestMarshalCounter(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-128", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x00\xff"), Values: mod.Values{ uint8(255), uint16(255), uint32(255), uint64(255), uint(255), }.AddVariants(mod.All...), }.Run("255", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\xff\xff"), Values: mod.Values{ uint16(65535), uint32(65535), uint64(65535), uint(65535), }.AddVariants(mod.All...), }.Run("65535", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\xff\xff\xff\xff"), Values: mod.Values{ uint32(4294967295), uint64(4294967295), uint(4294967295), }.AddVariants(mod.All...), }.Run("4294967295", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Values: mod.Values{ uint64(18446744073709551615), uint(18446744073709551615), diff --git a/marshal_7_varint_test.go b/marshal_7_varint_test.go index e99ae0d79..06093dce6 100644 --- a/marshal_7_varint_test.go +++ b/marshal_7_varint_test.go @@ -5,9 +5,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/internal/tests/utils" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalVarInt(t *testing.T) { @@ -19,19 +18,19 @@ func TestMarshalVarInt(t *testing.T) { } // unmarshal `custom string` unsupported - brokenCustomStrings := utils.GetTypes(mod.String(""), (*mod.String)(nil)) + brokenCustomStrings := serialization.GetTypes(mod.String(""), (*mod.String)(nil)) // marshal "" (empty string) unsupported // unmarshal nil value into (string)("0") - brokenEmptyStrings := utils.GetTypes(string(""), mod.String("")) + brokenEmptyStrings := serialization.GetTypes(string(""), mod.String("")) // marshal data, which equal math.MaxUint64, into uint and uit64 leads to an error - brokenUints := utils.GetTypes(uint(0), mod.Uint64(0), mod.Uint(0), (*uint)(nil), (*mod.Uint64)(nil), (*mod.Uint)(nil)) + brokenUints := serialization.GetTypes(uint(0), mod.Uint64(0), mod.Uint(0), (*uint)(nil), (*mod.Uint64)(nil), (*mod.Uint)(nil)) // marshal and unmarshal all strings with data or value which out of range of int64 unsupported - brokenBigStrings := utils.GetTypes(string(""), (*string)(nil), mod.String(""), (*mod.String)(nil)) + brokenBigStrings := serialization.GetTypes(string(""), (*string)(nil), mod.String(""), (*mod.String)(nil)) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), @@ -42,7 +41,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenEmptyStrings, }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -52,7 +51,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -62,7 +61,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00"), Values: mod.Values{ int8(0), int16(0), int32(0), int64(0), int(0), @@ -73,7 +72,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"), Values: mod.Values{ int64(9223372036854775807), int(9223372036854775807), @@ -83,7 +82,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("maxInt64", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), Values: mod.Values{ int64(-9223372036854775808), int(-9223372036854775808), @@ -93,7 +92,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("minInt64", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xff\xff\xff"), Values: mod.Values{ int32(2147483647), int64(2147483647), int(2147483647), @@ -103,7 +102,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("2147483647", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80\x00\x00\x00"), Values: mod.Values{ int32(-2147483648), int64(-2147483648), int(-2147483648), @@ -113,7 +112,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-2147483648", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xff"), Values: mod.Values{ int16(32767), int32(32767), int64(32767), int(32767), @@ -123,7 +122,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("32767", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80\x00"), Values: mod.Values{ int16(-32768), int32(-32768), int64(-32768), int(-32768), @@ -133,7 +132,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-32768", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f"), Values: mod.Values{ int8(127), int16(127), int32(127), int64(127), int(127), @@ -143,7 +142,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("127", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x80"), Values: mod.Values{ int8(-128), int16(-128), int32(-128), int64(-128), int(-128), @@ -153,28 +152,28 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenCustomStrings, }.Run("-128", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\xff"), Values: mod.Values{ uint8(255), uint16(255), uint32(255), uint64(255), uint(255), }.AddVariants(mod.All...), }.Run("255", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\xff\xff"), Values: mod.Values{ uint16(65535), uint32(65535), uint64(65535), uint(65535), }.AddVariants(mod.All...), }.Run("65535", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\xff\xff\xff\xff"), Values: mod.Values{ uint32(4294967295), uint64(4294967295), uint(4294967295), }.AddVariants(mod.All...), }.Run("4294967295", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\xff\xff\xff\xff\xff\xff\xff\xff"), Values: mod.Values{ uint64(18446744073709551615), uint(18446744073709551615), @@ -183,7 +182,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenUints, }.Run("max_uint", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"), Values: mod.Values{ "9223372036854775808", *big.NewInt(0).Add(big.NewInt(1), big.NewInt(9223372036854775807)), @@ -192,7 +191,7 @@ func TestMarshalVarInt(t *testing.T) { BrokenUnmarshalTypes: brokenBigStrings, }.Run("maxInt64+1", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\x7f\xff\xff\xff\xff\xff\xff\xff"), Values: mod.Values{ "-9223372036854775809", *big.NewInt(0).Add(big.NewInt(-1), big.NewInt(-9223372036854775808)), diff --git a/marshal_8_float_test.go b/marshal_8_float_test.go index b3cc6403b..6398cbe97 100644 --- a/marshal_8_float_test.go +++ b/marshal_8_float_test.go @@ -5,8 +5,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalFloat(t *testing.T) { @@ -17,47 +17,47 @@ func TestMarshalFloat(t *testing.T) { return gocql.Unmarshal(tType, bytes, i) } - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{(*float32)(nil)}.AddVariants(mod.CustomType), }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{float32(0)}.AddVariants(mod.CustomType), }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{float32(0)}.AddVariants(mod.All...), }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00"), Values: mod.Values{float32(0)}.AddVariants(mod.All...), }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\x7f\xff\xff"), Values: mod.Values{float32(math.MaxFloat32)}.AddVariants(mod.All...), }.Run("max", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x01"), Values: mod.Values{float32(math.SmallestNonzeroFloat32)}.AddVariants(mod.All...), }.Run("smallest", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\x80\x00\x00"), Values: mod.Values{float32(math.Inf(1))}.AddVariants(mod.All...), }.Run("inf+", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\x80\x00\x00"), Values: mod.Values{float32(math.Inf(-1))}.AddVariants(mod.All...), }.Run("inf-", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xc0\x00\x00"), Values: mod.Values{float32(math.NaN())}.AddVariants(mod.All...), }.Run("nan", t, marshal, unmarshal) diff --git a/marshal_9_duble_test.go b/marshal_9_duble_test.go index 8395ab5d2..0b00cdebb 100644 --- a/marshal_9_duble_test.go +++ b/marshal_9_duble_test.go @@ -5,8 +5,8 @@ import ( "testing" "github.com/gocql/gocql" - "github.com/gocql/gocql/marshal/tests/mod" - "github.com/gocql/gocql/marshal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization" + "github.com/gocql/gocql/internal/tests/serialization/mod" ) func TestMarshalDouble(t *testing.T) { @@ -17,47 +17,47 @@ func TestMarshalDouble(t *testing.T) { return gocql.Unmarshal(tType, bytes, i) } - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{(*float64)(nil)}.AddVariants(mod.CustomType), }.Run("[nil]nullable", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: nil, Values: mod.Values{float64(0)}.AddVariants(mod.CustomType), }.Run("[nil]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: make([]byte, 0), Values: mod.Values{float64(0)}.AddVariants(mod.All...), }.Run("[]unmarshal", t, nil, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"), Values: mod.Values{float64(0)}.AddVariants(mod.All...), }.Run("zeros", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xef\xff\xff\xff\xff\xff\xff"), Values: mod.Values{float64(math.MaxFloat64)}.AddVariants(mod.All...), }.Run("max", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x01"), Values: mod.Values{float64(math.SmallestNonzeroFloat64)}.AddVariants(mod.All...), }.Run("smallest", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xf0\x00\x00\x00\x00\x00\x00"), Values: mod.Values{float64(math.Inf(1))}.AddVariants(mod.All...), }.Run("inf+", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\xff\xf0\x00\x00\x00\x00\x00\x00"), Values: mod.Values{float64(math.Inf(-1))}.AddVariants(mod.All...), }.Run("inf-", t, marshal, unmarshal) - serialization.Set{ + serialization.PositiveSet{ Data: []byte("\x7f\xf8\x00\x00\x00\x00\x00\x01"), Values: mod.Values{float64(math.NaN())}.AddVariants(mod.All...), }.Run("nan", t, marshal, unmarshal)