Skip to content

test(coverage/crypto): drive crypto to ≥95% (was 89.2%)#30

Merged
mastermanas805 merged 1 commit into
masterfrom
coverage/crypto-95
May 21, 2026
Merged

test(coverage/crypto): drive crypto to ≥95% (was 89.2%)#30
mastermanas805 merged 1 commit into
masterfrom
coverage/crypto-95

Conversation

@mastermanas805
Copy link
Copy Markdown
Member

Summary

Drives common/crypto line coverage from 89.2% → 95.5% by adding 8 internal tests targeting previously-uncovered error branches in aes.go, jwt.go, and token.go.

Test additions (crypto/coverage_test.go, internal package)

  • Encrypt nonce-read error path — randReader swap with errReader
  • GenerateAPIKey rand-read error + short-read paths — tokenRandReader swap
  • SignJWT + SignOnboardingJWT SignedString error paths — jwtSigningMethod swap with failingSigningMethod
  • VerifyOnboardingJWT alg-confusion guard (RS256 header rejected)
  • VerifyJWT + VerifyOnboardingJWT defensive iat-future check — uses jwt.TimeFunc override so the library's RegisteredClaims.Valid passes, leaving our defensive check to fire (guards against jwt/v4 upstream dropping the iat check)

Source-side change

Three package-level vars replace direct references:

  • var randReader io.Reader = rand.Reader (aes.go)
  • var tokenRandReader io.Reader = rand.Reader (token.go)
  • var jwtSigningMethod jwt.SigningMethod = jwt.SigningMethodHS256 (jwt.go)

Production behaviour unchanged; only tests override them.

Remaining uncovered (5 stmts — genuinely unreachable)

  • aes.go:61-63 / 87-89 cipher.NewGCM error — AES blocks never fail GCM construction
  • jwt.go:83 non-ValidationError unwrap — jwt/v4 always wraps parse errors in *ValidationError
  • jwt.go:86-88 / 130-132 !ok || !parsed.Valid defensive — ParseWithClaims with a typed receiver either errors or returns ok && Valid

Test plan

  • go test ./crypto -count=1 -race passes
  • go vet ./crypto/ clean
  • go test ./... -count=1 -short (whole common module) passes
  • Coverage: go test ./crypto -coverprofile=cov.out → 95.5%

🤖 Generated with Claude Code

Adds 8 internal tests targeting previously-uncovered branches:

- Encrypt nonce-read error path (lines 66-68): swappable
  randReader package var lets tests inject an erroring io.Reader.
- GenerateAPIKey rand-read error + short-read paths: swappable
  tokenRandReader package var.
- SignJWT + SignOnboardingJWT SignedString error paths:
  swappable jwtSigningMethod package var lets a failingSigningMethod
  drive the error branch.
- VerifyOnboardingJWT alg-confusion guard (RS256 header rejected).
- VerifyJWT + VerifyOnboardingJWT defensive iat-future check:
  uses jwt.TimeFunc override so the library's RegisteredClaims.Valid
  passes, leaving the second-line-of-defense check in our code to
  fire — guards against jwt/v4 upstream dropping the iat check.

Source-side change: 3 package-level vars (randReader,
tokenRandReader, jwtSigningMethod) replace direct references to
crypto/rand.Reader and jwt.SigningMethodHS256. Production behaviour
unchanged; only tests override them.

Coverage: 89.2% -> 95.5%. Remaining 5 uncovered statements are
genuinely unreachable defensive code:
  - aes.go:61-63 / 87-89 cipher.NewGCM error (AES blocks never
    fail GCM construction)
  - jwt.go:83 non-ValidationError unwrap (jwt/v4 always wraps
    parse errors in ValidationError)
  - jwt.go:86-88 / 130-132 !ok || !parsed.Valid defensive
    (ParseWithClaims with a typed receiver always either errors
    or returns ok && Valid)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mastermanas805 mastermanas805 merged commit 4473edd into master May 21, 2026
9 checks passed
mastermanas805 added a commit that referenced this pull request May 21, 2026
Caps the cross-repo 95% coverage push for `common`. After PRs #25-#30
landed for crypto/nats/plans/readiness/r2/logctx, the two remaining
storage backends sat at 63.6% (dospaces) and 72.2% (s3).

dospaces -> 100.0% via accessor + customerEndpointURL fallback coverage:
all simple getters (MasterAccessKey, MasterSecretKey, Endpoint, PublicURL,
Bucket, Region, UseTLS) plus the scheme-prefix and already-schemed
endpoint branches inside customerEndpointURL().

s3 -> 97.2% via accessor + IssueTenantCredentials default-fallback +
safeSessionName branch coverage: digits/dashes/underscores retained,
RoleSessionName truncation at 56, customerEndpointURL passthrough for
already-schemed endpoints, RevokeTenantCredentials no-op confirmation,
and AssumeRole error propagation through IssueTenantCredentials.

Total: 94.5% -> 98.0%. Every package now >=95%.

Coverage block (rule 17):

  Symptom:        common total 94.5% < 95% target; per-pkg dospaces/s3 below floor
  Enumeration:    go tool cover -func | sort | grep "<100"
  Sites found:    2 (storageprovider/dospaces, storageprovider/s3)
  Sites touched:  both
  Coverage test:  TestRegistry_AllProvidersSatisfyContract + per-backend
                  registry-walk tests already in place; new coverage files
                  add accessor + edge-branch assertions.
  Live verified:  go test ./... -short -coverprofile cov.out; total = 98.0%

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant