forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #278 from illia-li/il/add/marshal_corrupt_test_suite
add marshal `corrupt test suite`
- Loading branch information
Showing
28 changed files
with
471 additions
and
273 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 15 additions & 11 deletions
26
internal/tests/utils/utils.go → internal/tests/serialization/utils.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
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 { | ||
types[i] = reflect.TypeOf(value) | ||
} | ||
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() | ||
} |
9 changes: 4 additions & 5 deletions
9
internal/tests/utils/equal.go → internal/tests/serialization/utils_equal.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
Oops, something went wrong.