aboutsummaryrefslogtreecommitdiff
path: root/src/parser/deserializer.rs
blob: fc81b6699277a0fe089a116a825acd75038829b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::io::Read;
use std::str::FromStr;

use crate::data_structures;

fn deserialize_dmarc_report<R: Read>(
    reader: R,
) -> Result<data_structures::Feedback, serde_xml_rs::Error> {
    serde_xml_rs::from_reader(reader)
}

impl From<String> for data_structures::DMARCAlignment {
    fn from(item: String) -> Self {
        match item.as_str() {
            "r" => data_structures::DMARCAlignment::Relaxed(item),
            "s" => data_structures::DMARCAlignment::Strict(item),
            _ => {
                panic!("DMARC alignment value '{:?}' is not allowed", item);
            }
        }
    }
}

impl From<String> for data_structures::DMARCPolicy {
    fn from(item: String) -> Self {
        match item.as_str() {
            "none" => data_structures::DMARCPolicy::None(item),
            "quarantine" => data_structures::DMARCPolicy::Quarantine(item),
            "reject" => data_structures::DMARCPolicy::Reject(item),
            _ => {
                panic!("DMARC policy value '{:?}' is not allowed", item);
            }
        }
    }
}

impl From<String> for data_structures::DMARCFailureOption {
    fn from(item: String) -> Self {
        match item.as_str() {
            "0" => data_structures::DMARCFailureOption::AlignedPassFailure(item),
            "1" => data_structures::DMARCFailureOption::OtherAlignedPassFailure(item),
            "2" => data_structures::DMARCFailureOption::SignatureAlignmentFailure(item),
            "3" => data_structures::DMARCFailureOption::SPFFailure(item),
            _ => {
                panic!("DMARC failure option '{:?}' is not allowed", item);
            }
        }
    }
}

impl data_structures::DMARCReport {
    pub fn from_raw_report(report: data_structures::Feedback) -> data_structures::DMARCReport {
        data_structures::DMARCReport {
            id: report.report_metadata.report_id,
            org: report.report_metadata.org_name,
            record: data_structures::DMARCRecord {
                domain: report.policy_published.domain,
                adkim: data_structures::DMARCAlignment::from(report.policy_published.adkim),
                aspf: data_structures::DMARCAlignment::from(report.policy_published.aspf),
                policy: data_structures::DMARCPolicy::from(report.policy_published.p),
                subdomain_policy: data_structures::DMARCPolicy::from(report.policy_published.sp),
                percentage: match u64::from_str(&report.policy_published.pct) {
                    Ok(pct) => pct,
                    Err(_) => {
                        panic!(
                            "Percentage value '{:?}' for DMARC record is not valid",
                            report.policy_published.pct
                        );
                    }
                },
                failure_opt: data_structures::DMARCFailureOption::from(report.policy_published.fo),
            },
            dkim_pass: match report.record.auth_results.dkim.result.as_str() {
                "pass" => true,
                _ => false,
            },
            spf_pass: match report.record.auth_results.spf.result.as_str() {
                "pass" => true,
                _ => false,
            },
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use std::fs::File;
    use std::io::BufReader;

    #[test]
    fn test_deserialize_dmarc_report() {
        let dmarc_report_file = File::open("fixtures/dmarc_report_fixture.xml").unwrap();

        let reader = BufReader::new(dmarc_report_file);

        let expected_version = "1.0".to_string();
        let expected_report_id = "3374fb5148ba40c1a5cf8e3d36f34a34".to_string();
        let expected_begin_date = "1707436800".to_string();
        let expected_policy_published_domain = "boitalett.re".to_string();
        let expected_record_row_source_ip = "195.154.102.43".to_string();
        let expected_record_row_policy_evaluated_dkim = "pass".to_string();
        let expected_identifiers_envelope_to = "babilou.com".to_string();
        let expected_auth_results_dkim_selector = "2023101501".to_string();
        let expected_auth_results_spf_scope = "mfrom".to_string();

        match deserialize_dmarc_report(reader) {
            Ok(report) => {
                assert_eq!(report.version, expected_version);
                assert_eq!(report.report_metadata.report_id, expected_report_id);
                assert_eq!(report.report_metadata.date_range.begin, expected_begin_date);
                assert_eq!(
                    report.policy_published.domain,
                    expected_policy_published_domain
                );
                assert_eq!(report.record.row.source_ip, expected_record_row_source_ip);
                assert_eq!(
                    report.record.row.policy_evaluated.dkim,
                    expected_record_row_policy_evaluated_dkim
                );
                assert_eq!(
                    report.record.identifiers.envelope_to,
                    expected_identifiers_envelope_to
                );
                assert_eq!(
                    report.record.auth_results.dkim.selector,
                    expected_auth_results_dkim_selector
                );
                assert_eq!(
                    report.record.auth_results.spf.scope,
                    expected_auth_results_spf_scope
                );
            }
            Err(err) => {
                panic!("{:?}", err);
            }
        }
    }

    #[test]
    fn test_parse_dmarc_report() {
        let dmarc_report_file = File::open("fixtures/dmarc_report_fixture.xml").unwrap();

        let reader = BufReader::new(dmarc_report_file);

        let raw_report = deserialize_dmarc_report(reader).unwrap();

        let expected_dmarc_report_id = "3374fb5148ba40c1a5cf8e3d36f34a34".to_string();

        let dmarc_report = data_structures::DMARCReport::from_raw_report(raw_report);

        assert_eq!(dmarc_report.id, expected_dmarc_report_id);
    }
}