test(coverage): drive internal/crypto to 95.6%#142
Merged
Conversation
Add coverage_test.go covering the previously-untested surface: - AES Keyring: NewKeyring constructor (valid + 6 error paths), EncryptVersioned + key-rotation fallthrough across v1→v2, legacy unversioned envelopes, unknown-version rejection, nil-keyring guard, ActiveVersion accessor. - AES low-level: Encrypt/Decrypt bad key length, bad base64, too-short ciphertext, ErrEncrypt/ErrDecrypt Error+Unwrap. - splitVersionedEnvelope: short input, non-digit version, missing dot. - JWT: SignJWT auto-JTI + preserve-existing-JTI + auto-IssuedAt, VerifyJWT alg:none rejection, wrong secret, expired (errors.Is sentinel), future-IAT, NotBefore-future, malformed header, RFC8725 alg pinning. - Fingerprint: ParseIP valid/invalid, FingerprintIP plain/AS-prefix/ empty-ASN/garbage-ASN/IPv6/short-prefix cases. - Token: GenerateAPIKey format + 100-iter uniqueness. - Error types: Error()+Unwrap() round-trips for all five wrapper errors. jwt.go simplification: remove dead defensive branches that jwt/v4 already covers — the post-parse IAT check (lib validates iat in RegisteredClaims. Valid()), the non-HMAC keyfunc check (WithValidMethods rejects alg mismatch first), the !Valid/!ok claims-assertion guard (Parse always sets Valid=true on success with the concrete claims type), and the non- ValidationError fallback in VerifyJWT (v4's parser only ever returns *jwt.ValidationError). *jwt.ValidationError already implements errors.Is for sentinels so callers using errors.Is(err, jwt.ErrTokenExpired) keep working without an explicit unwrap. Coverage: 37.5% → 95.6% (138 stmts, 6 missed — all unreachable defensive code: cipher.NewGCM / io.ReadFull rand / SignedString HMAC failures). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
internal/crypto/coverage_test.gocovering Keyring rotation (v1→v2 fallthrough, legacy unversioned envelopes, unknown-version + nil-keyring rejection), AES error paths (bad base64, short ciphertext, bad key length, wrapped error Error/Unwrap), JWT SignJWT/VerifyJWT (auto-JTI, auto-IAT, alg:none + HS384/HS512 + wrong-secret + expired + future-IAT + NotBefore-future + malformed header rejection), ParseIP + FingerprintIP (AS-prefix/empty/garbage/IPv6 cases), GenerateAPIKey format + 100-iter uniqueness, and Error/Unwrap for all 5 error wrapper types.jwt.goby removing 4 dead defensive branches that jwt/v4 already covers: the post-parse IAT check (lib validates iat), the non-HMAC keyfunc check (WithValidMethods rejects alg mismatch first), the !ok/!Valid claims-assertion guard (Parse always sets Valid=true on success with concrete claims type), and the non-ValidationError fallback in VerifyJWT (parser only ever returns *jwt.ValidationError).*jwt.ValidationErroralready implementserrors.Isfor sentinels, so callers usingerrors.Is(err, jwt.ErrTokenExpired)keep working without an explicit unwrap.cipher.NewGCM/io.ReadFull(rand.Reader)/SignedStringHMAC failures,rand.Readfailure).Test plan
go test ./internal/crypto -coverprofile=cov.out -coverpkg=./internal/crypto -count=1→coverage: 95.6% of statementsgo test ./internal/crypto -v -count=1→ all tests PASS (existing + new)go build ./...cleango vet ./...clean*crypto.ErrJWTVerify(verified via rg) — simplification is API-compatible because the existingTestSignVerify_ExpiredTokenReturnsErrTokenExpiredtest usesVerifyOnboardingJWT(still wraps) anderrors.Is(err, jwt.ErrTokenExpired)keeps working on both Verify funcs via*jwt.ValidationError.Is().🤖 Generated with Claude Code