Compare decoded RSA keys with Equal to fix a flaky test - #764
Conversation
Test_WriteKeypair and Test_Create compared a generated rsa.PrivateKey with one parsed back out of a PKCS#12 bundle using assert.Equal, which uses reflect.DeepEqual. Since Go 1.24 an rsa.PrivateKey holds an unexported FIPS key whose precomputed dP, dQ and qInv values are stored fixed-width when the key is generated but minimal-width when the key is parsed. Whenever one of those values has a leading zero byte, roughly one key in a hundred, the two keys are not deeply equal even though they are the same key, and the test fails. Use the key's own Equal method instead, which compares only the mathematical key material. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Richard Wall <richard@the-moon.net>
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The flaky test comparisons are corrected without unresolved review issues.
Review effort: Lite
Findings: None
What changed in this PR
Fixes flaky RSA private-key comparisons in PKCS#12-related tests by using semantic key equality instead of reflective deep equality.
Changes:
- Replaced deep-equality assertions with RSA key
Equalcomparisons. - Added comments explaining representation differences in newer Go versions.
| File | Description |
|---|---|
pkg/keystore/pkcs12/pkcs12_test.go |
Uses key-specific equality for decoded keys. |
pkg/filestore/writer_test.go |
Avoids flaky reflective comparisons for decoded RSA keys. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
erikgb
left a comment
There was a problem hiding this comment.
/lgtm
/approve
Thanks, Richard!
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: erikgb The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Fixes a flaky unit test that failed on #762 and blocked its merge until a retest.
Test_WriteKeypairandTest_Createcompare a generatedrsa.PrivateKeywith one decoded from a PKCS#12 bundle usingassert.Equal, which usesreflect.DeepEqual. The two keys are the same key but are not deeply equal about one time in a hundred. This PR compares them with the key's ownEqualmethod instead.Why does it flake?
Since Go 1.24 an
rsa.PrivateKeyholds an unexported FIPS key. Its precomputeddP,dQandqInvvalues are byte slices, and they are encoded differently depending on how the key was made:big.Int.Bytes(): crypto/rsa/rsa.go#L583-L585Whenever one of those three values has a leading zero byte, the slices differ in length and
reflect.DeepEqualreports the keys as different.rsa.PrivateKey.Equalcompares onlyN,E,Dand the primes: crypto/rsa/rsa.go#L128-L144.Failure from #762 and a standalone repro
The failed job on #762 shows the length mismatch:
This test reproduces it without cert-manager code:
With this PR both packages pass
go test -count=20, andgolangci-lintreports no issues.[Claude Fable 5.1]