Skip to content

Commit b658cb4

Browse files
apskhemMr-Leshiy
andauthored
test(rust/signed-doc): Add a test case for invalid params and aliases in protected header (#377)
* feat: initial test * chore: minor cleanup * feat: full change * chore: fmtfix * chore: minor lintfix * fix: lintfix * Update rust/signed_doc/tests/decoding.rs Co-authored-by: Alex Pozhylenkov <leshiy12345678@gmail.com> * Update rust/signed_doc/tests/decoding.rs Co-authored-by: Alex Pozhylenkov <leshiy12345678@gmail.com> * Update rust/signed_doc/tests/decoding.rs Co-authored-by: Alex Pozhylenkov <leshiy12345678@gmail.com> * fix: comments * chore: remove signature * chore: minor rename * chore: minor --------- Co-authored-by: Mr-Leshiy <leshiy12345678@gmail.com>
1 parent 11d9ed7 commit b658cb4

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

rust/signed_doc/tests/decoding.rs

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

23+
// `parameters` value along with its aliases are not allowed to be presented
24+
fn signed_doc_with_parameters_and_aliases_case(aliases: &'static [&'static str]) -> TestCase {
25+
let uuid_v7 = UuidV7::new();
26+
let uuid_v4 = UuidV4::new();
27+
let doc_ref = DocumentRef::new(UuidV7::new(), UuidV7::new(), DocLocator::default());
28+
29+
TestCase {
30+
name: "Multiple definitions of campaign_id, brand_id, category_id and parameters at once.",
31+
bytes_gen: Box::new({
32+
move || {
33+
let mut e = Encoder::new(Vec::new());
34+
e.tag(Tag::new(98))?;
35+
e.array(4)?;
36+
37+
// protected headers (metadata fields)
38+
e.bytes({
39+
let mut p_headers = Encoder::new(Vec::new());
40+
p_headers.map(4u64.overflowing_add(u64::try_from(aliases.len())?).0)?;
41+
p_headers.u8(3)?.encode(ContentType::Json)?;
42+
p_headers
43+
.str("type")?
44+
.encode_with(uuid_v4, &mut catalyst_types::uuid::CborContext::Tagged)?;
45+
p_headers
46+
.str("id")?
47+
.encode_with(uuid_v7, &mut catalyst_types::uuid::CborContext::Tagged)?;
48+
p_headers
49+
.str("ver")?
50+
.encode_with(uuid_v7, &mut catalyst_types::uuid::CborContext::Tagged)?;
51+
52+
for alias in aliases {
53+
p_headers
54+
.str(alias)?
55+
.encode_with(doc_ref.clone(), &mut ())?;
56+
}
57+
58+
p_headers.into_writer().as_slice()
59+
})?;
60+
61+
// empty unprotected headers
62+
e.map(0)?;
63+
// content
64+
e.bytes(serde_json::to_vec(&serde_json::Value::Null)?.as_slice())?;
65+
// zero signatures
66+
e.array(0)?;
67+
68+
Ok(e)
69+
}
70+
}),
71+
can_decode: true,
72+
valid_doc: false,
73+
post_checks: None,
74+
}
75+
}
76+
2377
fn decoding_empty_bytes_case() -> TestCase {
2478
TestCase {
2579
name: "Decoding empty bytes",
@@ -91,6 +145,19 @@ fn catalyst_signed_doc_decoding_test() {
91145
let test_cases = [
92146
decoding_empty_bytes_case(),
93147
signed_doc_with_all_fields_case(),
148+
signed_doc_with_parameters_and_aliases_case(&["parameters", "category_id"]),
149+
signed_doc_with_parameters_and_aliases_case(&["parameters", "brand_id"]),
150+
signed_doc_with_parameters_and_aliases_case(&["parameters", "campaign_id"]),
151+
signed_doc_with_parameters_and_aliases_case(&["category_id", "campaign_id"]),
152+
signed_doc_with_parameters_and_aliases_case(&["category_id", "brand_id"]),
153+
signed_doc_with_parameters_and_aliases_case(&["brand_id", "campaign_id"]),
154+
signed_doc_with_parameters_and_aliases_case(&["category_id", "brand_id", "campaign_id"]),
155+
signed_doc_with_parameters_and_aliases_case(&[
156+
"parameters",
157+
"category_id",
158+
"brand_id",
159+
"campaign_id",
160+
]),
94161
];
95162

96163
for case in test_cases {

0 commit comments

Comments
 (0)