-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix (saml): address possible panic if clock.Clock is nil
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
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,15 @@ | ||
package saml | ||
|
||
import "reflect" | ||
|
||
// isNil reports if a is nil | ||
func isNil(a any) bool { | ||
if a == nil { | ||
return true | ||
} | ||
switch reflect.TypeOf(a).Kind() { | ||
case reflect.Ptr, reflect.Map, reflect.Chan, reflect.Slice, reflect.Func: | ||
return reflect.ValueOf(a).IsNil() | ||
} | ||
return false | ||
} |
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,56 @@ | ||
package saml | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_isNil(t *testing.T) { | ||
t.Parallel() | ||
|
||
var testErrNilPtr *testError | ||
var testMapNilPtr map[string]struct{} | ||
var testArrayNilPtr *[1]string | ||
var testChanNilPtr *chan string | ||
var testSliceNilPtr *[]string | ||
var testFuncNil func() | ||
|
||
var testChanString chan string | ||
|
||
tc := []struct { | ||
i any | ||
want bool | ||
}{ | ||
{i: &testError{}, want: false}, | ||
{i: testError{}, want: false}, | ||
{i: &map[string]struct{}{}, want: false}, | ||
{i: map[string]struct{}{}, want: false}, | ||
{i: [1]string{}, want: false}, | ||
{i: &[1]string{}, want: false}, | ||
{i: &testChanString, want: false}, | ||
{i: "string", want: false}, | ||
{i: []string{}, want: false}, | ||
{i: func() {}, want: false}, | ||
{i: nil, want: true}, | ||
{i: testErrNilPtr, want: true}, | ||
{i: testMapNilPtr, want: true}, | ||
{i: testArrayNilPtr, want: true}, | ||
{i: testChanNilPtr, want: true}, | ||
{i: testChanString, want: true}, | ||
{i: testSliceNilPtr, want: true}, | ||
{i: testFuncNil, want: true}, | ||
} | ||
|
||
for i, tc := range tc { | ||
t.Run(fmt.Sprintf("test #%d", i+1), func(t *testing.T) { | ||
assert := assert.New(t) | ||
assert.Equal(tc.want, isNil(tc.i)) | ||
}) | ||
} | ||
} | ||
|
||
type testError struct{} | ||
|
||
func (*testError) Error() string { return "error" } |
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