Skip to content

Commit 026c146

Browse files
authored
Merge branch 'main' into chore/build-verify-and-publish
2 parents 4bd2a67 + cac0865 commit 026c146

File tree

2 files changed

+58
-35
lines changed

2 files changed

+58
-35
lines changed

rust/signed_doc/src/validator/rules/content_encoding.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ impl CatalystSignedDocumentValidationRule for ContentEncodingRule {
2929
doc: &CatalystSignedDocument,
3030
_provider: &dyn Provider,
3131
) -> anyhow::Result<bool> {
32-
Ok(self.check_inner(doc))
32+
self.check_inner(doc);
33+
Ok(!doc.report().is_problematic())
3334
}
3435
}
3536

@@ -63,7 +64,7 @@ impl ContentEncodingRule {
6364
fn check_inner(
6465
&self,
6566
doc: &CatalystSignedDocument,
66-
) -> bool {
67+
) {
6768
let context = "Content Encoding Rule check";
6869
match self {
6970
Self::NotSpecified => {
@@ -75,7 +76,6 @@ impl ContentEncodingRule {
7576
"{context}, document does not expect to have a content-encoding field"
7677
),
7778
);
78-
return false;
7979
}
8080
},
8181
Self::Specified { exp, optional } => {
@@ -90,7 +90,6 @@ impl ContentEncodingRule {
9090
.join(", "),
9191
"Invalid document content-encoding value",
9292
);
93-
return false;
9493
}
9594
if content_encoding.decode(doc.encoded_content()).is_err() {
9695
doc.report().invalid_value(
@@ -99,18 +98,15 @@ impl ContentEncodingRule {
9998
content_encoding.to_string().as_str(),
10099
"Document content is not decodable with the expected content-encoding",
101100
);
102-
return false;
103101
}
104102
} else if !optional {
105103
doc.report().missing_field(
106104
"content-encoding",
107105
"Document must have a content-encoding field",
108106
);
109-
return false;
110107
}
111108
},
112109
}
113-
true
114110
}
115111
}
116112

@@ -128,26 +124,38 @@ mod tests {
128124
optional: true,
129125
};
130126

131-
let doc = Builder::new()
127+
let doc = Builder::with_required_fields()
132128
.with_metadata_field(SupportedField::ContentEncoding(content_encoding))
133129
.with_content(content_encoding.encode(&[1, 2, 3]).unwrap())
134130
.build();
135-
assert!(rule.check_inner(&doc));
131+
rule.check_inner(&doc);
132+
assert!(!doc.report().is_problematic());
136133

137134
// empty content (empty bytes) could not be brotli decoded
138-
let doc = Builder::new()
135+
let doc = Builder::with_required_fields()
139136
.with_metadata_field(SupportedField::ContentEncoding(content_encoding))
140137
.build();
141-
assert!(!rule.check_inner(&doc));
142-
143-
let doc = Builder::new().build();
144-
assert!(rule.check_inner(&doc));
138+
rule.check_inner(&doc);
139+
let report = format!("{:?}", doc.report());
140+
println!("{report}");
141+
assert!(doc.report().is_problematic());
142+
assert!(
143+
report.contains("Document content is not decodable with the expected content-encoding")
144+
);
145+
146+
let doc = Builder::with_required_fields().build();
147+
rule.check_inner(&doc);
148+
assert!(!doc.report().is_problematic());
145149

146150
let rule = ContentEncodingRule::Specified {
147151
exp: vec![content_encoding],
148152
optional: false,
149153
};
150-
assert!(!rule.check_inner(&doc));
154+
rule.check_inner(&doc);
155+
let report = format!("{:?}", doc.report());
156+
println!("{report}");
157+
assert!(doc.report().is_problematic());
158+
assert!(report.contains("Document must have a content-encoding field"));
151159
}
152160

153161
#[test]
@@ -157,13 +165,18 @@ mod tests {
157165
let rule = ContentEncodingRule::NotSpecified;
158166

159167
// With Brotli content encoding
160-
let doc = Builder::new()
168+
let doc = Builder::with_required_fields()
161169
.with_metadata_field(SupportedField::ContentEncoding(content_encoding))
162170
.build();
163-
assert!(!rule.check_inner(&doc));
171+
rule.check_inner(&doc);
172+
let report = format!("{:?}", doc.report());
173+
println!("{report}");
174+
assert!(doc.report().is_problematic());
175+
assert!(report.contains("document does not expect to have a content-encoding field"));
164176

165177
// No content encoding
166-
let doc = Builder::new().build();
167-
assert!(rule.check_inner(&doc));
178+
let doc = Builder::with_required_fields().build();
179+
rule.check_inner(&doc);
180+
assert!(!doc.report().is_problematic());
168181
}
169182
}

rust/signed_doc/src/validator/rules/section.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ impl CatalystSignedDocumentValidationRule for SectionRule {
2323
doc: &CatalystSignedDocument,
2424
_provider: &dyn Provider,
2525
) -> anyhow::Result<bool> {
26-
Ok(self.check_inner(doc))
26+
self.check_inner(doc);
27+
Ok(!doc.report().is_problematic())
2728
}
2829
}
2930

@@ -32,14 +33,13 @@ impl SectionRule {
3233
fn check_inner(
3334
&self,
3435
doc: &CatalystSignedDocument,
35-
) -> bool {
36+
) {
3637
if let Self::Specified { optional } = self
3738
&& doc.doc_meta().section().is_none()
3839
&& !optional
3940
{
4041
doc.report()
4142
.missing_field("section", "Document must have a section field");
42-
return false;
4343
}
4444
if let Self::NotSpecified = self
4545
&& let Some(section) = doc.doc_meta().section()
@@ -49,10 +49,7 @@ impl SectionRule {
4949
&section.to_string(),
5050
"Document does not expect to have a section field",
5151
);
52-
return false;
5352
}
54-
55-
true
5653
}
5754
}
5855

@@ -63,31 +60,44 @@ mod tests {
6360

6461
#[test]
6562
fn section_rule_specified_test() {
66-
let doc = Builder::new()
63+
let doc = Builder::with_required_fields()
6764
.with_metadata_field(SupportedField::Section("$".parse().unwrap()))
6865
.build();
6966
let rule = SectionRule::Specified { optional: false };
70-
assert!(rule.check_inner(&doc));
67+
rule.check_inner(&doc);
68+
assert!(!doc.report().is_problematic());
7169

72-
let doc = Builder::new().build();
70+
let doc = Builder::with_required_fields().build();
7371
let rule = SectionRule::Specified { optional: true };
74-
assert!(rule.check_inner(&doc));
72+
rule.check_inner(&doc);
73+
assert!(!doc.report().is_problematic());
7574

76-
let doc = Builder::new().build();
75+
let doc = Builder::with_required_fields().build();
7776
let rule = SectionRule::Specified { optional: false };
78-
assert!(!rule.check_inner(&doc));
77+
rule.check_inner(&doc);
78+
let report = format!("{:?}", doc.report());
79+
println!("{report}");
80+
assert!(doc.report().is_problematic());
81+
assert!(report.contains("Document must have a section field"));
82+
assert_eq!(1, doc.report().entries().count());
7983
}
8084

8185
#[test]
8286
fn section_rule_not_specified_test() {
8387
let rule = SectionRule::NotSpecified;
8488

85-
let doc = Builder::new().build();
86-
assert!(rule.check_inner(&doc));
89+
let doc = Builder::with_required_fields().build();
90+
rule.check_inner(&doc);
91+
assert!(!doc.report().is_problematic());
8792

88-
let doc = Builder::new()
93+
let doc = Builder::with_required_fields()
8994
.with_metadata_field(SupportedField::Section("$".parse().unwrap()))
9095
.build();
91-
assert!(!rule.check_inner(&doc));
96+
rule.check_inner(&doc);
97+
let report = format!("{:?}", doc.report());
98+
println!("{report}");
99+
assert!(doc.report().is_problematic());
100+
assert!(report.contains("Document does not expect to have a section field"));
101+
assert_eq!(1, doc.report().entries().count());
92102
}
93103
}

0 commit comments

Comments
 (0)