From 08c6168e1cfa6d9cdf37d059e57a390b9f39116f Mon Sep 17 00:00:00 2001 From: Xynnn007 Date: Wed, 17 Jul 2024 10:02:13 +0800 Subject: [PATCH] dep/verifier: fix eventlog with extra blank chars Due to definition of AAEL, a ' ' is allowed as part of content field. Thus we now parse the original eventlog string using ' ' as separator to distinguish domain, operation and content only twice, and the things left are treated as the content of the eventlog, including extra ' 's. Signed-off-by: Xynnn007 --- deps/verifier/src/eventlog/mod.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/deps/verifier/src/eventlog/mod.rs b/deps/verifier/src/eventlog/mod.rs index 7580f2f0f..c6a2d91db 100644 --- a/deps/verifier/src/eventlog/mod.rs +++ b/deps/verifier/src/eventlog/mod.rs @@ -12,7 +12,7 @@ use hash::HashAlgorithm; use serde_json::{Map, Value}; use sha2::{digest::FixedOutput, Digest, Sha256, Sha384, Sha512}; -#[derive(Clone)] +#[derive(Clone, PartialEq, Debug)] pub struct AAEvent { pub domain: String, pub operation: String, @@ -24,10 +24,7 @@ impl FromStr for AAEvent { fn from_str(input: &str) -> Result { let input_trimed = input.trim_end(); - let sections: Vec<&str> = input_trimed.split(' ').collect(); - if sections.len() != 3 { - bail!("Illegal AA event entry format. Should be ` `"); - } + let sections: Vec<&str> = input_trimed.splitn(3, ' ').collect(); Ok(Self { domain: sections[0].into(), operation: sections[1].into(), @@ -168,10 +165,12 @@ impl AAEventlog { #[cfg(test)] mod tests { - use std::fs; + use std::{fs, str::FromStr}; use rstest::rstest; + use super::AAEvent; + #[rstest] #[case("./test_data/aael/AAEL_data_1", b"71563a23b430b8637970b866169052815ef9434056516dc9f78c1b3bfb745cee18a2ca92aa53c8122be5cbe59a100764")] #[case("./test_data/aael/AAEL_data_2", b"31fa17881137923029b1da5b368e92d8b22b14bbb4deaa360da61fce7aa530bd2f4c59ac7bd27021ef64104ff4dd04f9")] @@ -186,4 +185,15 @@ mod tests { let sum = hex::decode(sum).unwrap(); aael.integrity_check(&sum).unwrap(); } + + #[rstest] + #[case("domain operation con tent", AAEvent { domain: "domain".into(), operation: "operation".into(), content: "con tent".into() })] + #[case("domain operation content", AAEvent { domain: "domain".into(), operation: "operation".into(), content: "content".into() })] + #[case("dom ain operation content", AAEvent { domain: "dom".into(), operation: "ain".into(), content: "operation content".into() })] + #[case(r#"github.com/confidential-containers EventWithJSONParams { "key1":"value1\tmore values and 'quotes'\n", "key2": [ "value2", 2, true, null ] }"#, AAEvent { domain: "github.com/confidential-containers".into(), operation: "EventWithJSONParams".into(), content: r#"{ "key1":"value1\tmore values and 'quotes'\n", "key2": [ "value2", 2, true, null ] }"#.into() })] + + fn test_parse_log_entry(#[case] entry: &str, #[case] expect: AAEvent) { + let entry = AAEvent::from_str(entry).unwrap(); + assert_eq!(entry, expect); + } }