summaryrefslogtreecommitdiffstats
path: root/tests/test_regex.rs
blob: 16c879a43683cc390c3f05582bccd8bc1617f815 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2019 Alexandros Frantzis
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0

use mda::{Email, EmailRegex};

static TEST_EMAIL: &'static str = "Return-Path: <me@source.com>
To: Destination <someone.else@destination.com>
Cc: firstcc <firstcc@destination.com>,
 secondcc <secondcc@destination.com>,
    thirsdcc <thirdcc@destination.com>
X-Test-Field: name123=value456
Content-Type: text/plain; charset=utf-8

To: Body <body@destination.com>
Body body body
Σὰ βγεῖς στὸν πηγαιμὸ γιὰ τὴν Ἰθάκη,
νὰ εὔχεσαι νἆναι μακρὺς ὁ δρόμος,
γεμάτος περιπέτειες, γεμάτος γνώσεις.
";

#[test]
fn header_search() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    assert!(email.header().search(r"^(Cc|To).*someone\.else@destination\.com").unwrap());
    assert!(!email.header().search(r"^(Cc|To).*body@destination\.com") .unwrap());
}

#[test]
fn header_search_multiline() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    assert!(email.header().search(r"^Cc.*secondcc@destination\.com").unwrap());
    assert!(email.header().search(r"^Cc.*thirdcc@destination\.com").unwrap());
}

#[test]
fn body_search() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    assert!(email.body().search(r"^(Cc|To).*body@destination\.com").unwrap());
    assert!(!email.body().search(r"^(Cc|To).*someone\.else@destination\.com").unwrap());
}

#[test]
fn data_search() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    assert!(email.data().search(r"^(Cc|To).*firstcc@destination\.com").unwrap());
    assert!(email.data().search(r"^(Cc|To).*body@destination\.com").unwrap());
    assert!(!email.data().search(r"^(Cc|To).*unknown@destination\.com").unwrap());
}

#[test]
fn invalid_regex() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    assert!(email.body().search(r"^(Cc|To).*(body@destination\.com").is_err());
}

#[test]
fn header_search_set() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    let search =
        email.header().search_set(
            &[
                r"^(Cc|To).*someone\.else@destination\.com",
                r"^(Cc|To).*body@destination\.com",
            ]
        ).unwrap();

    let search: Vec<_> = search.into_iter().collect();
    assert_eq!(search, vec![0]);
}

#[test]
fn body_search_set() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    let search =
        email.body().search_set(
            &[
                r"^(Cc|To).*someone\.else@destination\.com",
                r"^(Cc|To).*body@destination\.com",
            ]
        ).unwrap();

    let search: Vec<_> = search.into_iter().collect();
    assert_eq!(search, vec![1]);
}

#[test]
fn data_search_set() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    let search =
        email.data().search_set(
            &[
                r"^(Cc|To).*someone\.else@destination\.com",
                r"^(Cc|To).*body@destination\.com",
            ]
        ).unwrap();

    let search: Vec<_> = search.into_iter().collect();
    assert_eq!(search, vec![0, 1]);
}

#[test]
fn search_set_invalid() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    let search =
        email.data().search_set(
            &[
                r"^((Cc|To).*someone\.else@destination\.com",
                r"^(Cc|To).*body@destination\.com",
            ]
        );

    assert!(search.is_err());
}

#[test]
fn unicode_support() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    assert!(email.body().search(r"νἆναι μακρὺς ὁ δρόμος").unwrap());
    assert!(email.body().search(r"νἆναι μακρὺς ὁ δρόμος").unwrap());

    assert_eq!(
        email.body().search_set(
            &[
                r"Τοὺς Λαιστρυγόνας καὶ τοὺς Κύκλωπας",
                r"νἆναι μακρὺς ὁ δρόμος",
            ]
        ).unwrap().into_iter().collect::<Vec<_>>(),
        vec![1]
    );

    assert_eq!(
        email.data().search_set(
            &[
                r"Τοὺς Λαιστρυγόνας καὶ τοὺς Κύκλωπας",
                r"νἆναι μακρὺς ὁ δρόμος",
            ]
        ).unwrap().into_iter().collect::<Vec<_>>(),
        vec![1]
    );
}

#[test]
fn captures() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    let header = email.header();
    let captures =
        header
            .search_with_captures(r"^X-Test-Field: *(?P<name>\w+)=(?P<value>\w+)")
            .unwrap()
            .unwrap();

    assert_eq!(captures.name("name").map(|m| m.as_bytes()), Some("name123".as_bytes()));
    assert_eq!(captures.name("value").map(|m| m.as_bytes()), Some("value456".as_bytes()));
}

#[test]
fn multiline_headers() {
    let email = Email::from_vec(TEST_EMAIL.to_string().into_bytes()).unwrap();

    let header = email.header();
    let captures =
        header
            .search_with_captures(r"^X-Test-Field: *(?P<name>\w+)=(?P<value>\w+)")
            .unwrap()
            .unwrap();

    assert_eq!(captures.name("name").map(|m| m.as_bytes()), Some("name123".as_bytes()));
    assert_eq!(captures.name("value").map(|m| m.as_bytes()), Some("value456".as_bytes()));
}