Skip to content

test: add unit tests for crypto utility (#599) - #746

Merged
DeFiVC merged 1 commit into
ChainLearnOfficial:mainfrom
Martha-code-dev:test/unit-tests-cert-a11y-crypto-sanitize
Oct 21, 2026
Merged

DeFiVC merged 1 commit into
ChainLearnOfficial:mainfrom
Martha-code-dev:test/unit-tests-cert-a11y-crypto-sanitize

Conversation

@Martha-code-dev

@Martha-code-dev Martha-code-dev commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR was scoped to batch unit test additions for #591, #592, #599, and #600. On investigation, #591 (certificate utilities), #592 (accessibility checker), and #600 (sanitize utility) already have thorough, passing unit test coverage on main (tests/unit/credentials/certificate-urls.test.ts, tests/unit/credentials/completion-certificate.test.ts, tests/unit/courses/accessibility.test.ts, tests/unit/utils/sanitize.test.ts) — each covers the acceptance criteria listed in its issue. Only #599 (crypto utility) had no test file, so this PR adds that coverage and fixes a latent correctness bug it uncovered.

closes #599
closes #591
closes #592
closes #600

Changes

  • Add unit tests for crypto utility #599 — Add unit tests for crypto utility: Added tests/unit/utils/crypto.test.ts covering sha256Hash() (hex format, length, determinism, distinct inputs, a known test vector, unicode input) and safeEqual() (equal strings, differing strings, differing lengths, prefix strings, empty strings, case sensitivity). While writing the different-length cases, found that safeEqual() zero-pads both buffers before the constant-time comparison, which made strings differing only by trailing NUL bytes (e.g. "abc" vs "abc\0") compare as equal. Fixed src/utils/crypto.ts so safeEqual() always returns false when the original (unpadded) lengths differ, while still running a constant-time comparison on the padded buffers so timing doesn't leak the length mismatch.

Not included

Test plan

  • npx vitest run tests/unit/utils/crypto.test.ts — 13/13 passing, including the NUL-byte regression case.
  • npx vitest run tests/unit/utils/crypto.test.ts tests/unit/utils/sanitize.test.ts tests/unit/credentials/certificate-urls.test.ts tests/unit/credentials/completion-certificate.test.ts tests/unit/courses/accessibility.test.ts — 40/40 passing (confirms the safeEqual fix doesn't regress anything these other suites touch).
  • Full vitest run on this branch shows 71 pre-existing failing test files unrelated to this change (a broken logger.debug mock affecting many service tests on main already); none are in the files touched by this PR.

Happy to split the "Not included" findings into separate comments on #591/#592/#600 if that's preferred over noting them here.

Add unit tests for sha256Hash() and safeEqual() covering output format,
determinism, equal/different strings, and different-length inputs.

Also fix a latent bug in safeEqual(): zero-padding the shorter buffer
before the constant-time comparison made strings that only differ by
trailing NUL bytes (e.g. "abc" vs "abc\0") compare as equal. safeEqual
now explicitly returns false whenever the original lengths differ.
@drips-wave

drips-wave Bot commented Oct 21, 2026

Copy link
Copy Markdown

@Martha-code-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@DeFiVC
DeFiVC merged commit 8877fb5 into ChainLearnOfficial:main Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants