Skip to content
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

Add an __eq__ method #48

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Magic method (`__eq__` and `__repr__`) has been added for TimestampResponse
and TimestampRequest ([#48](https://github.com/trailofbits/rfc3161-client/pull/48))

## [0.0.2] - 2024-10-30

Expand Down
12 changes: 12 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ impl TimeStampReq {
hasher.finish()
}

fn __eq__(&self, py: pyo3::Python<'_>, other: pyo3::PyRef<'_, Self>) -> pyo3::PyResult<bool> {
let other_bytes = other.as_bytes(py)?;
let self_bytes = self.as_bytes(py)?;
Ok(other_bytes.eq(&self_bytes.as_bytes()))
}

fn __repr__(&self, py: pyo3::Python<'_>) -> pyo3::PyResult<String> {
let version = self.version()?;
let nonce_repr = match self.nonce(py)? {
Expand Down Expand Up @@ -293,6 +299,12 @@ impl TimeStampResp {
hasher.finish()
}

fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> pyo3::PyResult<bool> {
let other_bytes = asn1::write_single(&other.raw.borrow_dependent()).unwrap();
let self_bytes = asn1::write_single(&self.raw.borrow_dependent()).unwrap();
Ok(other_bytes.eq(&self_bytes))
}

fn __repr__(&self) -> pyo3::PyResult<String> {
let status = self.status()?;
Ok(format!("<TimestampResponse(status={status}, ...)>"))
Expand Down
2 changes: 1 addition & 1 deletion src/rfc3161_client/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def nonce(self) -> bytes:

@property
@abc.abstractmethod
def name(self) -> cryptography.x509.Name:
def name(self) -> cryptography.x509.GeneralName:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FLAG: The annotation was wrong but I didn't think we had to create a whole new PR just for it :)

"""Returns the name."""


Expand Down
Binary file added test/fixtures/other_request.der
Binary file not shown.
Binary file added test/fixtures/other_response.tsr
Binary file not shown.
55 changes: 55 additions & 0 deletions test/test_tsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from datetime import datetime, timezone
from pathlib import Path

from rfc3161_client import TimeStampRequest, decode_timestamp_response
from rfc3161_client._rust import parse_timestamp_request

_HERE = Path(__file__).parent.resolve()
_FIXTURE = _HERE / "fixtures"


class TestTimestampResponse:
def test_parsing_good(self):
timestamp_response = decode_timestamp_response((_FIXTURE / "response.tsr").read_bytes())
assert timestamp_response.status == 0

tst_info = timestamp_response.tst_info
assert tst_info.name.value.rfc4514_string() == "CN=Test TSA Timestamping,O=local"
assert tst_info.nonce == 3937359519792308179
assert not tst_info.ordering
assert tst_info.policy.dotted_string == "1.3.6.1.4.1.57264.2"
assert tst_info.serial_number == 693290210947147715387173185458430793885588677084
assert tst_info.version == 1
assert datetime(2024, 10, 8, 15, 40, 32, tzinfo=timezone.utc) == tst_info.gen_time
assert tst_info.message_imprint.message.hex().startswith("9b71d224bd62f3785d96d")

assert tst_info.accuracy.seconds == 1
assert tst_info.accuracy.millis is None
assert tst_info.accuracy.micros is None

def test_equality(self):
timestamp_response = decode_timestamp_response((_FIXTURE / "response.tsr").read_bytes())
other_response = decode_timestamp_response((_FIXTURE / "other_response.tsr").read_bytes())

assert timestamp_response != other_response
assert decode_timestamp_response(
(_FIXTURE / "response.tsr").read_bytes()
) == decode_timestamp_response((_FIXTURE / "response.tsr").read_bytes())


class TestTimestampRequest:
def test_parsing_good(self):
timestamp_request: TimeStampRequest = parse_timestamp_request(
(_FIXTURE / "request.der").read_bytes()
)
assert timestamp_request.version == 1
assert timestamp_request.cert_req
assert timestamp_request.nonce == 3937359519792308179
assert timestamp_request.policy is None
assert timestamp_request.message_imprint.message.hex().startswith("9b71d224bd62f3785d96d")

def test_equality(self):
timestamp_request = parse_timestamp_request((_FIXTURE / "request.der").read_bytes())
other_request = parse_timestamp_request((_FIXTURE / "other_request.der").read_bytes())

assert timestamp_request != other_request