Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove assert #1200

Merged
merged 13 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ go_test(
"//jwk",
"//jwk/ecdsa",
"//jws",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
],
)
Expand Down
1 change: 0 additions & 1 deletion cert/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ go_test(
deps = [
":cert",
"//internal/jwxtest",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
],
)
Expand Down
14 changes: 4 additions & 10 deletions cert/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/lestrrat-go/jwx/v3/cert"
"github.com/lestrrat-go/jwx/v3/internal/jwxtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func parseCIDR(s string) *net.IPNet {
Expand All @@ -34,9 +34,7 @@ func parseURI(s string) *url.URL {

func TestCert(t *testing.T) {
privkey, err := jwxtest.GenerateRsaKey()
if !assert.NoError(t, err, `jwxtest.GenerateRsaKey`) {
return
}
require.NoError(t, err, `jwxtest.GenerateRsaKey`)

testExtKeyUsage := []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}
testUnknownExtKeyUsage := []asn1.ObjectIdentifier{[]int{1, 2, 3}, []int{2, 59, 1}}
Expand Down Expand Up @@ -111,12 +109,8 @@ func TestCert(t *testing.T) {
}

b64, err := cert.Create(rand.Reader, &template, &template, &privkey.PublicKey, privkey)
if !assert.NoError(t, err, `cert.Certificate should succeed`) {
return
}
require.NoError(t, err, `cert.Certificate should succeed`)

_, err = cert.Parse(b64)
if !assert.NoError(t, err, `cert.Parse should succeed`) {
return
}
require.NoError(t, err, `cert.Parse should succeed`)
}
5 changes: 1 addition & 4 deletions cert/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/lestrrat-go/jwx/v3/cert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -25,9 +24,7 @@ KpDY+K+bsqw=`)

func TestChain(t *testing.T) {
goldenCert, err := cert.Parse(certBytes)
if !assert.NoError(t, err, `x509.ParseCertificate should succeed`) {
return
}
require.NoError(t, err, `x509.ParseCertificate should succeed`)

testcases := []struct {
Name string
Expand Down
2 changes: 1 addition & 1 deletion internal/base64/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ go_test(
name = "base64_test",
srcs = ["base64_test.go"],
embed = [":base64"],
deps = ["@com_github_stretchr_testify//assert"],
deps = ["@com_github_stretchr_testify//require"],
)

alias(
Expand Down
10 changes: 3 additions & 7 deletions internal/base64/base64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/base64"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDecode(t *testing.T) {
Expand Down Expand Up @@ -37,12 +37,8 @@ func TestDecode(t *testing.T) {
tc.Encoding.Encode(dst, payload)

decoded, err := Decode(dst)
if !assert.NoError(t, err, `Decode should succeed`) {
return
}
if !assert.Equal(t, payload, decoded, `decoded content should match`) {
return
}
require.NoError(t, err, `Decode should succeed`)
require.Equal(t, payload, decoded, `decoded content should match`)
})
}
}
2 changes: 1 addition & 1 deletion internal/jwxtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ go_library(
"//jwk",
"//jwk/ecdsa",
"//jws",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
],
)

Expand Down
14 changes: 4 additions & 10 deletions internal/jwxtest/jwxtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/lestrrat-go/jwx/v3/jwk"
ourecdsa "github.com/lestrrat-go/jwx/v3/jwk/ecdsa"
"github.com/lestrrat-go/jwx/v3/jws"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func GenerateRsaKey() (*rsa.PrivateKey, error) {
Expand Down Expand Up @@ -165,9 +165,7 @@ func WriteJSONFile(template string, v interface{}) (string, func(), error) {

func DumpFile(t *testing.T, file string) {
buf, err := os.ReadFile(file)
if !assert.NoError(t, err, `failed to read file %s for debugging`, file) {
return
}
require.NoError(t, err, `failed to read file %s for debugging`, file)

if isHash, isArray := bytes.ContainsRune(buf, '{'), bytes.ContainsRune(buf, '['); isHash || isArray {
// Looks like a JSON-like thing. Dump that in a formatted manner, and
Expand All @@ -180,9 +178,7 @@ func DumpFile(t *testing.T, file string) {
v = []interface{}{}
}

if !assert.NoError(t, json.Unmarshal(buf, &v), `failed to parse contents as JSON`) {
return
}
require.NoError(t, json.Unmarshal(buf, &v), `failed to parse contents as JSON`)

buf, _ = json.MarshalIndent(v, "", " ")
t.Logf("=== BEGIN %s (formatted JSON) ===", file)
Expand All @@ -200,9 +196,7 @@ func DumpFile(t *testing.T, file string) {
if strings.HasSuffix(file, ".jwe") {
// cross our fingers our jwe implementation works
m, err := jwe.Parse(buf)
if !assert.NoError(t, err, `failed to parse JWE encrypted message`) {
return
}
require.NoError(t, err, `failed to parse JWE encrypted message`)

buf, _ = json.MarshalIndent(m, "", " ")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/keyconv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ go_test(
"//internal/jwxtest",
"//jwa",
"//jwk",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
],
)

Expand Down
123 changes: 43 additions & 80 deletions internal/keyconv/keyconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import (
"github.com/lestrrat-go/jwx/v3/internal/keyconv"
"github.com/lestrrat-go/jwx/v3/jwa"
"github.com/lestrrat-go/jwx/v3/jwk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestKeyconv(t *testing.T) {
t.Run("RSA", func(t *testing.T) {
key, err := jwxtest.GenerateRsaKey()
if !assert.NoError(t, err, `rsa.GenerateKey should succeed`) {
return
}
require.NoError(t, err, `rsa.GenerateKey should succeed`)
t.Run("PrivateKey", func(t *testing.T) {
jwkKey, _ := jwk.Import(key)
testcases := []struct {
Expand All @@ -33,45 +31,36 @@ func TestKeyconv(t *testing.T) {
for _, tc := range testcases {
t.Run("Assign to rsa.PrivateKey", func(t *testing.T) {
var dst rsa.PrivateKey
var checker func(assert.TestingT, error, ...interface{}) bool
var checker func(require.TestingT, error, ...interface{})
if tc.Error {
checker = assert.Error
checker = require.Error
} else {
checker = assert.NoError
}

if !checker(t, keyconv.RSAPrivateKey(&dst, tc.Src), `keyconv.RSAPrivateKey should succeed`) {
return
checker = require.NoError
}

checker(t, keyconv.RSAPrivateKey(&dst, tc.Src), `keyconv.RSAPrivateKey should succeed`)
if !tc.Error {
// From Go 1.20 on, for purposes of our test, we need the
// precomputed values as well
dst.Precompute()
if !assert.Equal(t, key, &dst, `keyconv.RSAPrivateKey should produce same value`) {
return
}
require.Equal(t, key, &dst, `keyconv.RSAPrivateKey should produce same value`)
}
})
t.Run("Assign to *rsa.PrivateKey", func(t *testing.T) {
dst := &rsa.PrivateKey{}
var checker func(assert.TestingT, error, ...interface{}) bool
var checker func(require.TestingT, error, ...interface{})
if tc.Error {
checker = assert.Error
checker = require.Error
} else {
checker = assert.NoError
checker = require.NoError
}

if !checker(t, keyconv.RSAPrivateKey(dst, tc.Src), `keyconv.RSAPrivateKey should succeed`) {
return
}
checker(t, keyconv.RSAPrivateKey(dst, tc.Src), `keyconv.RSAPrivateKey should succeed`)
if !tc.Error {
// From Go 1.20 on, for purposes of our test, we need the
// precomputed values as well
dst.Precompute()
if !assert.Equal(t, key, dst, `keyconv.RSAPrivateKey should produce same value`) {
return
}
require.Equal(t, key, dst, `keyconv.RSAPrivateKey should produce same value`)
}
})
}
Expand All @@ -92,48 +81,38 @@ func TestKeyconv(t *testing.T) {
for _, tc := range testcases {
t.Run("Assign to rsa.PublicKey", func(t *testing.T) {
var dst rsa.PublicKey
var checker func(assert.TestingT, error, ...interface{}) bool
var checker func(require.TestingT, error, ...interface{})
if tc.Error {
checker = assert.Error
checker = require.Error
} else {
checker = assert.NoError
checker = require.NoError
}

if !checker(t, keyconv.RSAPublicKey(&dst, tc.Src), `keyconv.RSAPublicKey should succeed`) {
return
}
checker(t, keyconv.RSAPublicKey(&dst, tc.Src), `keyconv.RSAPublicKey should succeed`)
if !tc.Error {
if !assert.Equal(t, pubkey, &dst, `keyconv.RSAPublicKey should produce same value`) {
return
}
require.Equal(t, pubkey, &dst, `keyconv.RSAPublicKey should produce same value`)
}
})
t.Run("Assign to *rsa.PublicKey", func(t *testing.T) {
dst := &rsa.PublicKey{}
var checker func(assert.TestingT, error, ...interface{}) bool
var checker func(require.TestingT, error, ...interface{})
if tc.Error {
checker = assert.Error
checker = require.Error
} else {
checker = assert.NoError
checker = require.NoError
}

if !checker(t, keyconv.RSAPublicKey(dst, tc.Src), `keyconv.RSAPublicKey should succeed`) {
return
}
checker(t, keyconv.RSAPublicKey(dst, tc.Src), `keyconv.RSAPublicKey should succeed`)
if !tc.Error {
if !assert.Equal(t, pubkey, dst, `keyconv.RSAPublicKey should produce same value`) {
return
}
require.Equal(t, pubkey, dst, `keyconv.RSAPublicKey should produce same value`)
}
})
}
})
})
t.Run("ECDSA", func(t *testing.T) {
key, err := jwxtest.GenerateEcdsaKey(jwa.P521)
if !assert.NoError(t, err, `ecdsa.GenerateKey should succeed`) {
return
}
require.NoError(t, err, `ecdsa.GenerateKey should succeed`)

t.Run("PrivateKey", func(t *testing.T) {
jwkKey, _ := jwk.Import(key)
Expand All @@ -150,38 +129,30 @@ func TestKeyconv(t *testing.T) {
for _, tc := range testcases {
t.Run("Assign to ecdsa.PrivateKey", func(t *testing.T) {
var dst ecdsa.PrivateKey
var checker func(assert.TestingT, error, ...interface{}) bool
var checker func(require.TestingT, error, ...interface{})
if tc.Error {
checker = assert.Error
checker = require.Error
} else {
checker = assert.NoError
checker = require.NoError
}

if !checker(t, keyconv.ECDSAPrivateKey(&dst, tc.Src), `keyconv.ECDSAPrivateKey should succeed`) {
return
}
checker(t, keyconv.ECDSAPrivateKey(&dst, tc.Src), `keyconv.ECDSAPrivateKey should succeed`)
if !tc.Error {
if !assert.Equal(t, key, &dst, `keyconv.ECDSAPrivateKey should produce same value`) {
return
}
require.Equal(t, key, &dst, `keyconv.ECDSAPrivateKey should produce same value`)
}
})
t.Run("Assign to *ecdsa.PrivateKey", func(t *testing.T) {
dst := &ecdsa.PrivateKey{}
var checker func(assert.TestingT, error, ...interface{}) bool
var checker func(require.TestingT, error, ...interface{})
if tc.Error {
checker = assert.Error
checker = require.Error
} else {
checker = assert.NoError
checker = require.NoError
}

if !checker(t, keyconv.ECDSAPrivateKey(dst, tc.Src), `keyconv.ECDSAPrivateKey should succeed`) {
return
}
checker(t, keyconv.ECDSAPrivateKey(dst, tc.Src), `keyconv.ECDSAPrivateKey should succeed`)
if !tc.Error {
if !assert.Equal(t, key, dst, `keyconv.ECDSAPrivateKey should produce same value`) {
return
}
require.Equal(t, key, dst, `keyconv.ECDSAPrivateKey should produce same value`)
}
})
}
Expand All @@ -202,38 +173,30 @@ func TestKeyconv(t *testing.T) {
for _, tc := range testcases {
t.Run("Assign to ecdsa.PublicKey", func(t *testing.T) {
var dst ecdsa.PublicKey
var checker func(assert.TestingT, error, ...interface{}) bool
var checker func(require.TestingT, error, ...interface{})
if tc.Error {
checker = assert.Error
checker = require.Error
} else {
checker = assert.NoError
checker = require.NoError
}

if !checker(t, keyconv.ECDSAPublicKey(&dst, tc.Src), `keyconv.ECDSAPublicKey should succeed`) {
return
}
checker(t, keyconv.ECDSAPublicKey(&dst, tc.Src), `keyconv.ECDSAPublicKey should succeed`)
if !tc.Error {
if !assert.Equal(t, pubkey, &dst, `keyconv.ECDSAPublicKey should produce same value`) {
return
}
require.Equal(t, pubkey, &dst, `keyconv.ECDSAPublicKey should produce same value`)
}
})
t.Run("Assign to *ecdsa.PublicKey", func(t *testing.T) {
dst := &ecdsa.PublicKey{}
var checker func(assert.TestingT, error, ...interface{}) bool
var checker func(require.TestingT, error, ...interface{})
if tc.Error {
checker = assert.Error
checker = require.Error
} else {
checker = assert.NoError
checker = require.NoError
}

if !checker(t, keyconv.ECDSAPublicKey(dst, tc.Src), `keyconv.ECDSAPublicKey should succeed`) {
return
}
checker(t, keyconv.ECDSAPublicKey(dst, tc.Src), `keyconv.ECDSAPublicKey should succeed`)
if !tc.Error {
if !assert.Equal(t, pubkey, dst, `keyconv.ECDSAPublicKey should produce same value`) {
return
}
require.Equal(t, pubkey, dst, `keyconv.ECDSAPublicKey should produce same value`)
}
})
}
Expand Down
Loading
Loading