-
Notifications
You must be signed in to change notification settings - Fork 0
Add more explicit auto id tests #11
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
Open
dastansam
wants to merge
1
commit into
main
Choose a base branch
from
add-auto-id-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||
| from auto_identity import CertificateManager, key_management | ||||||||||
| from cryptography import x509 | ||||||||||
| from cryptography.x509.oid import NameOID | ||||||||||
| from auto_identity import blake2b_256 | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def test_create_csr(): | ||||||||||
|
|
@@ -124,3 +125,26 @@ def test_certificate_to_pem_and_back(): | |||||||||
| certificate_from_pem = self_issuer.pem_to_certificate(pem_certificate) | ||||||||||
|
|
||||||||||
| assert certificate == certificate_from_pem | ||||||||||
|
|
||||||||||
| def test_auto_id_deterministic(): | ||||||||||
| # Create a private key for testing | ||||||||||
| private_key, _ = key_management.generate_ed25519_key_pair() | ||||||||||
| subject_name = "Test" | ||||||||||
|
|
||||||||||
| self_issuer = CertificateManager(private_key=private_key) | ||||||||||
| certificate = self_issuer.self_issue_certificate(subject_name) | ||||||||||
| pem_certificate = self_issuer.certificate_to_pem(certificate) | ||||||||||
|
|
||||||||||
| # Ensure the PEM is bytes | ||||||||||
| assert isinstance(pem_certificate, bytes) | ||||||||||
|
|
||||||||||
| issuer_auto_id = CertificateManager.get_certificate_auto_id(certificate) | ||||||||||
| assert issuer_auto_id == blake2b_256(self_issuer.get_subject_common_name(certificate.subject).encode()).hex() | ||||||||||
| assert issuer_auto_id == "8d2143d76615c515b5cc88fa7806aef268edeea87571c8f8b21a19f77b9993ba" | ||||||||||
|
|
||||||||||
| # for child certificate, auto_id of the issuer is included in the data | ||||||||||
| child_certificate = self_issuer.issue_certificate(self_issuer.create_and_sign_csr("child")) | ||||||||||
|
|
||||||||||
| assert CertificateManager.get_certificate_auto_id(child_certificate) == blake2b_256(CertificateManager.get_certificate_auto_id( | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These assertions are a bit long, consider a slight refactor
Suggested change
|
||||||||||
| certificate).encode() + self_issuer.get_subject_common_name(child_certificate.subject).encode()).hex() | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| assert CertificateManager.get_certificate_auto_id(child_certificate) == "d1575259a7e413f51e24ece5e353f5015fb0e39e6738b2044a3d7a8c3bc11114" | ||||||||||
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't familiar where the hardcoded hash was coming from, until I've done a quick Googling. While this is not a huge problem now, may be confusing in a long run.
I would suggest to introduce two constants "8d2143d76615c515b5cc88fa7806aef268edeea87571c8f8b21a19f77b9993ba" and "d1575259a7e413f51e24ece5e353f5015fb0e39e6738b2044a3d7a8c3bc11114" closer to beginning of the file or function, and highlight that they're coming from the blake2b_256 library.
Maybe something like EXPECTED_SELF_ISSUED_CERT_BLAKE2B_HASH and EXPECTED_CHILD_CERT_BLAKE2B_HASH could work good, wdyt?