Skip to content

Commit e236d9e

Browse files
authored
test(rust/signed-doc): Add valid alias decoding as parameters case (#382)
* feat: initial * chore: minor * feat: add postcheck * chore: minor assertion
1 parent bd68ac2 commit e236d9e

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

rust/signed_doc/tests/decoding.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,64 @@ struct TestCase {
2020
post_checks: Option<Box<PostCheck>>,
2121
}
2222

23+
fn signed_doc_with_valid_alias_case(alias: &'static str) -> TestCase {
24+
let uuid_v7 = UuidV7::new();
25+
let uuid_v4 = UuidV4::new();
26+
let doc_ref = DocumentRef::new(UuidV7::new(), UuidV7::new(), DocLocator::default());
27+
let doc_ref_cloned = doc_ref.clone();
28+
29+
TestCase {
30+
name:
31+
"Provided category_id, brand_id, campaign_id field should be processed as parameters.",
32+
bytes_gen: Box::new({
33+
move || {
34+
let mut e = Encoder::new(Vec::new());
35+
e.tag(Tag::new(98))?;
36+
e.array(4)?;
37+
38+
// protected headers (metadata fields)
39+
e.bytes({
40+
let mut p_headers = Encoder::new(Vec::new());
41+
p_headers.map(5)?;
42+
p_headers.u8(3)?.encode(ContentType::Json)?;
43+
p_headers
44+
.str("type")?
45+
.encode_with(uuid_v4, &mut catalyst_types::uuid::CborContext::Tagged)?;
46+
p_headers
47+
.str("id")?
48+
.encode_with(uuid_v7, &mut catalyst_types::uuid::CborContext::Tagged)?;
49+
p_headers
50+
.str("ver")?
51+
.encode_with(uuid_v7, &mut catalyst_types::uuid::CborContext::Tagged)?;
52+
p_headers
53+
.str(alias)?
54+
.encode_with(doc_ref.clone(), &mut ())?;
55+
56+
p_headers.into_writer().as_slice()
57+
})?;
58+
59+
// empty unprotected headers
60+
e.map(0)?;
61+
// content
62+
e.bytes(serde_json::to_vec(&serde_json::Value::Null)?.as_slice())?;
63+
// zero signatures
64+
e.array(0)?;
65+
66+
Ok(e)
67+
}
68+
}),
69+
can_decode: true,
70+
valid_doc: true,
71+
post_checks: Some(Box::new({
72+
move |doc| {
73+
let cmp = DocumentRefs::from(vec![doc_ref_cloned.clone()]);
74+
anyhow::ensure!(doc.doc_meta().parameters() == Some(&cmp));
75+
Ok(())
76+
}
77+
})),
78+
}
79+
}
80+
2381
// `parameters` value along with its aliases are not allowed to be presented
2482
fn signed_doc_with_parameters_and_aliases_case(aliases: &'static [&'static str]) -> TestCase {
2583
let uuid_v7 = UuidV7::new();
@@ -258,6 +316,9 @@ fn catalyst_signed_doc_decoding_test() {
258316
let test_cases = [
259317
decoding_empty_bytes_case(),
260318
signed_doc_with_all_fields_case(),
319+
signed_doc_with_valid_alias_case("category_id"),
320+
signed_doc_with_valid_alias_case("brand_id"),
321+
signed_doc_with_valid_alias_case("campaign_id"),
261322
signed_doc_with_parameters_and_aliases_case(&["parameters", "category_id"]),
262323
signed_doc_with_parameters_and_aliases_case(&["parameters", "brand_id"]),
263324
signed_doc_with_parameters_and_aliases_case(&["parameters", "campaign_id"]),

0 commit comments

Comments
 (0)