From 34f3f98b81680866a3a47da1893721aad7ac5ba1 Mon Sep 17 00:00:00 2001 From: Alexis Date: Mon, 23 Sep 2024 16:57:48 +0200 Subject: [PATCH] Start some tests --- TESTING.md | 6 ++++++ rust/Cargo.lock | 7 +++++++ rust/Cargo.toml | 3 ++- rust/src/lib.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 TESTING.md diff --git a/TESTING.md b/TESTING.md new file mode 100644 index 0000000..3dc123b --- /dev/null +++ b/TESTING.md @@ -0,0 +1,6 @@ +# Creating a Request + +```shell +openssl ts -query -data README.md -no_nonce -sha512 -cert -out file.tsq +``` + diff --git a/rust/Cargo.lock b/rust/Cargo.lock index a596d46..1fe4aa3 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -40,6 +40,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "indoc" version = "2.0.5" @@ -165,6 +171,7 @@ name = "sigstore-tsp" version = "0.1.0" dependencies = [ "asn1", + "hex", "pyo3", "self_cell", ] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 5c20a6c..3304772 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -11,4 +11,5 @@ crate-type = ["cdylib"] [dependencies] pyo3 = "0.22.0" asn1 = "0.17" -self_cell = "1" \ No newline at end of file +self_cell = "1" +hex = "0.4" \ No newline at end of file diff --git a/rust/src/lib.rs b/rust/src/lib.rs index f73a21e..605e7bb 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -67,7 +67,6 @@ pub struct TsaPolicyId { // extensions [0] IMPLICIT Extensions OPTIONAL } #[derive(asn1::Asn1Read, asn1::Asn1Write)] pub struct RawTimeStampReq<'a> { - #[default(1)] pub version: u8, // TODO(dm) Do we want to change this to an ENUM? pub message_imprint: MessageImprint<'a>, @@ -76,7 +75,7 @@ pub struct RawTimeStampReq<'a> { pub nonce: Option>, - #[default(false)] + // #[default(false)] pub cert_req: bool, // pub extensions: Option>, } @@ -282,3 +281,50 @@ fn sigstore_tsp(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(parse_timestamp_request, m)?)?; Ok(()) } + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn request_test() { + // See TESTING.md + // openssl ts -query -data README.md -no_nonce -sha512 -cert -out file.tsq + let enc_request = hex::decode("30590201013051300d060960864801650304020305000440c05812f7df5c643047235d400de272f03bd3d319f71f0c75c4b57948ccf2cdf85f8feb422dedcfa9ec26c102b08f778332ad9be18f759aebe93d64dcc65f49c30101ff") + .expect("Decoding failed"); + let request = asn1::parse_single::(&enc_request).unwrap(); + + assert_eq!(request.version, 1); + } + + #[test] + fn simple_test() { + // 300d06096086480165030402030500 + let algo_identifier = asn1::write_single(&AlgorithmIdentifier { + oid: asn1::DefinedByMarker::marker(), + params: AlgorithmParameters::Sha512(Some(())) + }); + match algo_identifier { + Ok(vec) => { println!("AlgorithmIdentifier {}", hex::encode(&vec)) }, + Err(_) => { todo!() } + } + + let enc_test = hex::decode("300d06096086480165030402030500").expect("Decoding failed"); + let algo_identifier_round = asn1::parse_single::(&enc_test).unwrap(); + + + let message_imprint = asn1::write_single(&MessageImprint { + hash_algorithm: AlgorithmIdentifier { + oid: asn1::DefinedByMarker::marker(), + params: AlgorithmParameters::Sha512(Some(())), + }, + hashed_message: asn1::BitString::new(&hex::decode("C05812F7DF5C643047235D400DE272F03BD3D319F71F0C75C4B57948CCF2CDF85F8FEB422DEDCFA9EC26C102B08F778332AD9BE18F759AEBE93D64DCC65F49C3").expect(""), 0).unwrap(), + }); + match message_imprint { + Ok(vec) => { println!("MessageImprint {}", hex::encode(&vec)) }, + Err(_) => { todo!() } + } + + } +} \ No newline at end of file