diff --git a/ca/ca_test.go b/ca/ca_test.go index 1b3094106..65f774074 100644 --- a/ca/ca_test.go +++ b/ca/ca_test.go @@ -359,7 +359,7 @@ func TestIssuePrecertificate(t *testing.T) { test.AssertNotError(t, err, "Certificate failed to parse") poisonExtension := findExtension(cert.Extensions, OIDExtensionCTPoison) - test.AssertEquals(t, true, poisonExtension != nil) + test.AssertNotNil(t, poisonExtension, "Precert doesn't contain poison extension") if poisonExtension != nil { test.AssertEquals(t, poisonExtension.Critical, true) test.AssertDeepEquals(t, poisonExtension.Value, []byte{0x05, 0x00}) // ASN.1 DER NULL diff --git a/grpc/errors_test.go b/grpc/errors_test.go index 41ee7fa36..0ffa57cf9 100644 --- a/grpc/errors_test.go +++ b/grpc/errors_test.go @@ -51,8 +51,8 @@ func TestErrorWrapping(t *testing.T) { test.Assert(t, err != nil, fmt.Sprintf("nil error returned, expected: %s", err)) test.AssertDeepEquals(t, err, es.err) - test.AssertEquals(t, wrapError(context.Background(), nil), nil) - test.AssertEquals(t, unwrapError(nil, nil), nil) + test.AssertNil(t, wrapError(context.Background(), nil), "Wrapping nil should still be nil") + test.AssertNil(t, unwrapError(nil, nil), "Unwrapping nil should still be nil") } // TestSubErrorWrapping tests that a boulder error with suberrors can be diff --git a/privatekey/privatekey_test.go b/privatekey/privatekey_test.go index f3fe653f8..bcc2ecf38 100644 --- a/privatekey/privatekey_test.go +++ b/privatekey/privatekey_test.go @@ -57,6 +57,6 @@ func TestLoad(t *testing.T) { signer, public, err = Load("../test/hierarchy/ee-e1.cert.pem") test.AssertError(t, err, "Should have failed, file is a certificate") - test.AssertEquals(t, signer, nil) - test.AssertEquals(t, public, nil) + test.AssertNil(t, signer, "Signer should be nil") + test.AssertNil(t, public, "Public should be nil") } diff --git a/sa/sa_test.go b/sa/sa_test.go index 07a504a2d..41c067b46 100644 --- a/sa/sa_test.go +++ b/sa/sa_test.go @@ -1483,7 +1483,7 @@ func TestGetOrderForNames(t *testing.T) { // It should not error since a ready order can be reused. test.AssertNotError(t, err, "sa.GetOrderForNames returned an unexpected error for ready order reuse") // The order returned should have the same ID as the order we created above - test.AssertEquals(t, result != nil, true) + test.AssertNotNil(t, result, "sa.GetOrderForNames returned nil result") test.AssertEquals(t, result.Id, order.Id) // Set the order processing so it can be finalized diff --git a/test/asserts.go b/test/asserts.go index f56af7c37..1b9514f27 100644 --- a/test/asserts.go +++ b/test/asserts.go @@ -22,12 +22,31 @@ func Assert(t *testing.T, result bool, message string) { } } -// AssertNotNil checks an object to be non-nil +// AssertNil checks that an object is nil. Being a "boxed nil" (a nil value +// wrapped in a non-nil interface type) is not good enough. +func AssertNil(t *testing.T, obj interface{}, message string) { + t.Helper() + if obj != nil { + t.Fatal(message) + } +} + +// AssertNotNil checks an object to be non-nil. Being a "boxed nil" (a nil value +// wrapped in a non-nil interface type) is not good enough. +// Note that there is a gap between AssertNil and AssertNotNil. Both fail when +// called with a boxed nil. This is intentional: we want to avoid boxed nils. func AssertNotNil(t *testing.T, obj interface{}, message string) { t.Helper() if obj == nil { t.Fatal(message) } + switch reflect.TypeOf(obj).Kind() { + // .IsNil() only works on chan, func, interface, map, pointer, and slice. + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice: + if reflect.ValueOf(obj).IsNil() { + t.Fatal(message) + } + } } // AssertNotError checks that err is nil