Skip to content
Merged
67 changes: 67 additions & 0 deletions rust/signed_doc/tests/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,60 @@ struct TestCase {
post_checks: Option<Box<PostCheck>>,
}

// `parameters` value along with its aliases are not allowed to be presented
fn signed_doc_with_parameters_and_aliases_case(aliases: &'static [&'static str]) -> TestCase {
let uuid_v7 = UuidV7::new();
let uuid_v4 = UuidV4::new();
let doc_ref = DocumentRef::new(UuidV7::new(), UuidV7::new(), DocLocator::default());

TestCase {
name: "Multiple definitions of campaign_id, brand_id, category_id and parameters at once.",
bytes_gen: Box::new({
move || {
let mut e = Encoder::new(Vec::new());
e.tag(Tag::new(98))?;
e.array(4)?;

// protected headers (metadata fields)
e.bytes({
let mut p_headers = Encoder::new(Vec::new());
p_headers.map(4u64.overflowing_add(u64::try_from(aliases.len())?).0)?;
p_headers.u8(3)?.encode(ContentType::Json)?;
p_headers
.str("type")?
.encode_with(uuid_v4, &mut catalyst_types::uuid::CborContext::Tagged)?;
p_headers
.str("id")?
.encode_with(uuid_v7, &mut catalyst_types::uuid::CborContext::Tagged)?;
p_headers
.str("ver")?
.encode_with(uuid_v7, &mut catalyst_types::uuid::CborContext::Tagged)?;

for alias in aliases {
p_headers
.str(alias)?
.encode_with(doc_ref.clone(), &mut ())?;
}

p_headers.into_writer().as_slice()
})?;

// empty unprotected headers
e.map(0)?;
// content
e.bytes(serde_json::to_vec(&serde_json::Value::Null)?.as_slice())?;
// zero signatures
e.array(0)?;

Ok(e)
}
}),
can_decode: true,
valid_doc: false,
post_checks: None,
}
}

fn decoding_empty_bytes_case() -> TestCase {
TestCase {
name: "Decoding empty bytes",
Expand Down Expand Up @@ -91,6 +145,19 @@ fn catalyst_signed_doc_decoding_test() {
let test_cases = [
decoding_empty_bytes_case(),
signed_doc_with_all_fields_case(),
signed_doc_with_parameters_and_aliases_case(&["parameters", "category_id"]),
signed_doc_with_parameters_and_aliases_case(&["parameters", "brand_id"]),
signed_doc_with_parameters_and_aliases_case(&["parameters", "campaign_id"]),
signed_doc_with_parameters_and_aliases_case(&["category_id", "campaign_id"]),
signed_doc_with_parameters_and_aliases_case(&["category_id", "brand_id"]),
signed_doc_with_parameters_and_aliases_case(&["brand_id", "campaign_id"]),
signed_doc_with_parameters_and_aliases_case(&["category_id", "brand_id", "campaign_id"]),
signed_doc_with_parameters_and_aliases_case(&[
"parameters",
"category_id",
"brand_id",
"campaign_id",
]),
];

for case in test_cases {
Expand Down