• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

input-output-hk / catalyst-libs / 17411934039

02 Sep 2025 06:01PM UTC coverage: 68.283% (+0.04%) from 68.243%
17411934039

push

github

web-flow
feat(rust/signed-doc): add 'NotSpecified' variant to Content Encoding (#529)

* Updated validation rule to be an enum with the NotSpecified variant.
* Updated tests and relevant modules.

51 of 57 new or added lines in 3 files covered. (89.47%)

13374 of 19586 relevant lines covered (68.28%)

3029.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.11
/rust/signed_doc/src/validator/rules/content_encoding.rs
1
//! `content-encoding` rule type impl.
2

3
use crate::{metadata::ContentEncoding, CatalystSignedDocument};
4

5
/// `content-encoding` field validation rule.
6
#[derive(Debug)]
7
pub(crate) enum ContentEncodingRule {
8
    /// Content Encoding field is optionally present in the document.
9
    Specified {
10
        /// expected `content-encoding` field.
11
        exp: ContentEncoding,
12
        /// optional flag for the `content-encoding` field.
13
        optional: bool,
14
    },
15
    /// Content Encoding field must not be present in the document.
16
    #[allow(dead_code)]
17
    NotSpecified,
18
}
19

20
impl ContentEncodingRule {
21
    /// Field validation rule
22
    #[allow(clippy::unused_async)]
23
    pub(crate) async fn check(
20✔
24
        &self,
20✔
25
        doc: &CatalystSignedDocument,
20✔
26
    ) -> anyhow::Result<bool> {
20✔
27
        let context = "Content Encoding Rule check";
20✔
28
        match self {
20✔
29
            Self::NotSpecified => {
30
                if let Some(content_encoding) = doc.doc_content_encoding() {
2✔
31
                    doc.report().unknown_field(
1✔
32
                        "content-encoding",
1✔
33
                        &content_encoding.to_string(),
1✔
34
                        &format!(
1✔
35
                            "{context}, document does not expect to have a content-encoding field"
1✔
36
                        ),
1✔
37
                    );
38
                    return Ok(false);
1✔
39
                }
1✔
40
            },
41
            Self::Specified { exp, optional } => {
18✔
42
                if let Some(content_encoding) = doc.doc_content_encoding() {
18✔
43
                    if content_encoding != *exp {
16✔
NEW
44
                        doc.report().invalid_value(
×
NEW
45
                            "content-encoding",
×
NEW
46
                            content_encoding.to_string().as_str(),
×
NEW
47
                            exp.to_string().as_str(),
×
NEW
48
                            "Invalid Document content-encoding value",
×
49
                        );
NEW
50
                        return Ok(false);
×
51
                    }
16✔
52
                    if content_encoding.decode(doc.encoded_content()).is_err() {
16✔
53
                        doc.report().invalid_value(
1✔
54
                            "payload",
1✔
55
                            &hex::encode(doc.encoded_content()),
1✔
56
                            &format!(
1✔
57
                                "Document content (payload) must decodable by the set content encoding type: {content_encoding}"
1✔
58
                            ),
1✔
59
                            "Invalid Document content value",
1✔
60
                        );
61
                        return Ok(false);
1✔
62
                    }
15✔
63
                } else if !optional {
2✔
64
                    doc.report().missing_field(
1✔
65
                        "content-encoding",
1✔
66
                        "Document must have a content-encoding field",
1✔
67
                    );
68
                    return Ok(false);
1✔
69
                }
1✔
70
            },
71
        }
72
        Ok(true)
17✔
73
    }
20✔
74
}
75

76
#[cfg(test)]
77
mod tests {
78
    use super::*;
79
    use crate::{builder::tests::Builder, metadata::SupportedField};
80

81
    #[tokio::test]
82
    async fn content_encoding_is_specified_rule_test() {
1✔
83
        let content_encoding = ContentEncoding::Brotli;
1✔
84

85
        let rule = ContentEncodingRule::Specified {
1✔
86
            exp: content_encoding,
1✔
87
            optional: true,
1✔
88
        };
1✔
89

90
        let doc = Builder::new()
1✔
91
            .with_metadata_field(SupportedField::ContentEncoding(content_encoding))
1✔
92
            .with_content(content_encoding.encode(&[1, 2, 3]).unwrap())
1✔
93
            .build();
1✔
94
        assert!(rule.check(&doc).await.unwrap());
1✔
95

96
        // empty content (empty bytes) could not be brotli decoded
97
        let doc = Builder::new()
1✔
98
            .with_metadata_field(SupportedField::ContentEncoding(content_encoding))
1✔
99
            .build();
1✔
100
        assert!(!rule.check(&doc).await.unwrap());
1✔
101

102
        let doc = Builder::new().build();
1✔
103
        assert!(rule.check(&doc).await.unwrap());
1✔
104

105
        let rule = ContentEncodingRule::Specified {
1✔
106
            exp: content_encoding,
1✔
107
            optional: false,
1✔
108
        };
1✔
109
        assert!(!rule.check(&doc).await.unwrap());
1✔
110
    }
1✔
111

112
    #[tokio::test]
113
    async fn content_encoding_is_not_specified_rule_test() {
1✔
114
        let content_encoding = ContentEncoding::Brotli;
1✔
115

116
        let rule = ContentEncodingRule::NotSpecified;
1✔
117

118
        // With Brotli content encoding
119
        let doc = Builder::new()
1✔
120
            .with_metadata_field(SupportedField::ContentEncoding(content_encoding))
1✔
121
            .build();
1✔
122
        assert!(!rule.check(&doc).await.unwrap());
1✔
123

124
        // No content encoding
125
        let doc = Builder::new().build();
1✔
126
        assert!(rule.check(&doc).await.unwrap());
1✔
127
    }
1✔
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc