From c9a252dc5c47e5d72ff67eae858f11bacfc3474e Mon Sep 17 00:00:00 2001 From: Alexis Date: Thu, 31 Oct 2024 15:04:17 +0100 Subject: [PATCH 1/4] Add an `__eq__` method Signed-off-by: Alexis --- CHANGELOG.md | 4 ++++ rust/src/lib.rs | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6e4835..c64460e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 487fd8d..e86ad3e 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -103,6 +103,12 @@ impl TimeStampReq { hasher.finish() } + fn __eq__(&self, py: pyo3::Python<'_>, other: pyo3::PyRef<'_, Self>) -> pyo3::PyResult { + 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 { let version = self.version()?; let nonce_repr = match self.nonce(py)? { @@ -293,6 +299,12 @@ impl TimeStampResp { hasher.finish() } + fn __eq__(&self, other: pyo3::PyRef<'_, Self>) -> pyo3::PyResult { + 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 { let status = self.status()?; Ok(format!("")) From 636ab8e0a0b774b805e1845c12eac0bd2d3235ed Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 5 Nov 2024 12:27:16 +0100 Subject: [PATCH 2/4] Add missing tests Signed-off-by: Alexis --- test/fixtures/other_request.der | Bin 0 -> 102 bytes test/fixtures/other_response.tsr | Bin 0 -> 1232 bytes test/test_tsp.py | 55 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 test/fixtures/other_request.der create mode 100644 test/fixtures/other_response.tsr create mode 100644 test/test_tsp.py diff --git a/test/fixtures/other_request.der b/test/fixtures/other_request.der new file mode 100644 index 0000000000000000000000000000000000000000..fae125bacae2c976961a637eabe405c0090389a6 GIT binary patch literal 102 zcmV-s0GaNC9O71OfvE00cmrandBcV)J-imegv~>OFHYoBP6v z4654V-}faMXFR7PBg5I7rNJG1ddzMVxW(EGMq@`@E?hd^^lx*#-oQfw2>`KlaMGLp I5+(rw|D$UvrvLx| literal 0 HcmV?d00001 diff --git a/test/fixtures/other_response.tsr b/test/fixtures/other_response.tsr new file mode 100644 index 0000000000000000000000000000000000000000..e66f8533a100f341b43ee037775ae656f5f67eba GIT binary patch literal 1232 zcmXqLVmV{L%*4oG(8O|>jZ>@5qwPB{BRkWACYCKgp$$xo%!a%M+-#f)Z61uN%q&cd z28~bIxS{Ge1sGWtG+t$CykyXLo{5o>jZ>SAk%h5&zX6j$paCyJ7c(mZi^J@~ODcPl zK3Bv}yOMSJm2I)%?B9nvc~;$ebpN}mWV+3AW#z*+XD>Y{TT^u=Ph`i@TRd*b?lF2X zR`1~p(T(@oQcTJ zHi$G3W@87th=~#EG-gJ2W+w)gXvShAha#Sxv%{0au1{%g(l9=??QHUui3h*cDu22@ zDPgr$-}}g4f0Y-SOLf??UX-69B)2g_zV3a*ljeD^Hq2^ToMn(1F@oxn=XN%d1!w-&ixzj*Ur?;Z4=!q(es@R@~j_U}eRvVZEbL*;0CAphN)Y zDS>ds1W8a=6%yhPbp6hse&{`?GGk?4S3z Date: Tue, 5 Nov 2024 12:27:28 +0100 Subject: [PATCH 3/4] Fix type hint Signed-off-by: Alexis --- src/rfc3161_client/tsp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rfc3161_client/tsp.py b/src/rfc3161_client/tsp.py index 70a7f26..8e9de89 100644 --- a/src/rfc3161_client/tsp.py +++ b/src/rfc3161_client/tsp.py @@ -166,7 +166,7 @@ def nonce(self) -> bytes: @property @abc.abstractmethod - def name(self) -> cryptography.x509.Name: + def name(self) -> cryptography.x509.GeneralName: """Returns the name.""" From 1e4ce3c0ec7faef5f1fa125cff3de69cf700ef9f Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 5 Nov 2024 12:29:10 +0100 Subject: [PATCH 4/4] Fix name Signed-off-by: Alexis --- test/test_tsp.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_tsp.py b/test/test_tsp.py index c03fd31..e71dbcf 100644 --- a/test/test_tsp.py +++ b/test/test_tsp.py @@ -49,7 +49,7 @@ def test_parsing_good(self): assert timestamp_request.message_imprint.message.hex().startswith("9b71d224bd62f3785d96d") def test_equality(self): - timestamp_response = parse_timestamp_request((_FIXTURE / "request.der").read_bytes()) - other_response = parse_timestamp_request((_FIXTURE / "other_request.der").read_bytes()) + timestamp_request = parse_timestamp_request((_FIXTURE / "request.der").read_bytes()) + other_request = parse_timestamp_request((_FIXTURE / "other_request.der").read_bytes()) - assert timestamp_response != other_response + assert timestamp_request != other_request