rfc822-mailbox-address-test.vala 15.4 KB
Newer Older
1
/*
2
 * Copyright 2016-2019 Michael Gratton <mike@vee.net>
3 4 5 6 7
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later). See the COPYING file in this distribution.
 */

8
class Geary.RFC822.MailboxAddressTest : TestCase {
9 10 11 12

    public MailboxAddressTest() {
        base("Geary.RFC822.MailboxAddressTest");
        add_test("is_valid_address", is_valid_address);
13
        add_test("unescaped_constructor", unescaped_constructor);
14
        add_test("from_rfc822_string_encoded", from_rfc822_string_encoded);
15 16 17 18
        add_test("is_spoofed", is_spoofed);
        add_test("has_distinct_name", has_distinct_name);
        add_test("to_full_display", to_full_display);
        add_test("to_short_display", to_short_display);
19 20
        // latter depends on the former, so test that first
        add_test("to_rfc822_address", to_rfc822_address);
21
        add_test("to_rfc822_string", to_rfc822_string);
22
        add_test("equal_to", equal_to);
23 24
    }

25
    public void is_valid_address() throws Error {
26 27
        assert(Geary.RFC822.MailboxAddress.is_valid_address("john@dep.aol.museum") == true);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("test@example.com") == true);
28 29 30
        assert(Geary.RFC822.MailboxAddress.is_valid_address("test.other@example.com") == true);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("test@localhost") == true);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("test2@localhost") == true);
31 32
        assert(Geary.RFC822.MailboxAddress.is_valid_address("some context test@example.com text") == true);

33
        assert(Geary.RFC822.MailboxAddress.is_valid_address("test@example") == false);
34 35 36 37 38 39 40
        assert(Geary.RFC822.MailboxAddress.is_valid_address("john@aol...com") == false);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("@example.com") == false);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("@example") == false);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("test") == false);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("test@") == false);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("@") == false);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("") == false);
41 42

        assert(Geary.RFC822.MailboxAddress.is_valid_address("\"Surname, Name\" <mail@example.com>") == true);
43 44
    }

45
    public void unescaped_constructor() throws Error {
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
        MailboxAddress addr1 = new MailboxAddress("test1", "test2@example.com");
        assert(addr1.name == "test1");
        assert(addr1.address == "test2@example.com");
        assert(addr1.mailbox == "test2");
        assert(addr1.domain == "example.com");

        MailboxAddress addr2 = new MailboxAddress(null, "test1@test2@example.com");
        assert(addr2.address == "test1@test2@example.com");
        assert(addr2.mailbox == "test1@test2");
        assert(addr2.domain == "example.com");

        MailboxAddress addr3 = new MailboxAddress(null, "©@example.com");
        assert(addr3.address == "©@example.com");
        assert(addr3.mailbox == "©");
        assert(addr3.domain == "example.com");

        MailboxAddress addr4 = new MailboxAddress(null, "😸@example.com");
        assert(addr4.address == "😸@example.com");
        assert(addr4.mailbox == "😸");
        assert(addr4.domain == "example.com");

        MailboxAddress addr5 = new MailboxAddress(null, "example.com");
        assert(addr5.address == "example.com");
        assert(addr5.mailbox == "");
        assert(addr5.domain == "");
    }

73
    public void from_rfc822_string_encoded() throws Error {
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
        try {
            MailboxAddress addr = new MailboxAddress.from_rfc822_string("test@example.com");
            assert(addr.name == null);
            assert(addr.mailbox == "test");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("\"test\"@example.com");
            assert(addr.name == null);
            assert(addr.address == "test@example.com");
            assert(addr.mailbox == "test");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("=?UTF-8?b?dGVzdA==?=@example.com");
            assert(addr.name == null);
            assert(addr.address == "test@example.com");
            assert(addr.mailbox == "test");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("\"=?UTF-8?b?dGVzdA==?=\"@example.com");
            assert(addr.name == null);
            assert(addr.address == "test@example.com");
            assert(addr.mailbox == "test");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("<test@example.com>");
            assert(addr.name == null);
            assert(addr.address == "test@example.com");
            assert(addr.mailbox == "test");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("<\"test\"@example.com>");
            assert(addr.name == null);
            assert(addr.address == "test@example.com");
            assert(addr.mailbox == "test");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("Test 1 <test2@example.com>");
            assert(addr.name == "Test 1");
            assert(addr.address == "test2@example.com");
            assert(addr.mailbox == "test2");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("\"Test 1\" <test2@example.com>");
            assert(addr.name == "Test 1");
            assert(addr.address == "test2@example.com");
            assert(addr.mailbox == "test2");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("Test 1 <\"test2\"@example.com>");
            assert(addr.name == "Test 1");
            assert(addr.address == "test2@example.com");
            assert(addr.mailbox == "test2");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("=?UTF-8?b?VGVzdCAx?= <test2@example.com>");
            assert(addr.name == "Test 1");
            assert(addr.address == "test2@example.com");
            assert(addr.mailbox == "test2");
            assert(addr.domain == "example.com");

            addr = new MailboxAddress.from_rfc822_string("\"=?UTF-8?b?VGVzdCAx?=\" <test2@example.com>");
            assert(addr.name == "Test 1");
            assert(addr.address == "test2@example.com");
            assert(addr.mailbox == "test2");
            assert(addr.domain == "example.com");

            // Courtesy Mailsploit https://www.mailsploit.com
            addr = new MailboxAddress.from_rfc822_string("\"=?utf-8?b?dGVzdCIgPHBvdHVzQHdoaXRlaG91c2UuZ292Pg==?==?utf-8?Q?=00=0A?=\" <demo@mailsploit.com>");
            assert(addr.name == "test <potus@whitehouse.gov>?\n");
            assert(addr.address == "demo@mailsploit.com");

            // Courtesy Mailsploit https://www.mailsploit.com
            addr = new MailboxAddress.from_rfc822_string("\"=?utf-8?Q?=42=45=47=49=4E=20=2F=20=28=7C=29=7C=3C=7C=3E=7C=40=7C=2C=7C=3B=7C=3A=7C=5C=7C=22=7C=2F=7C=5B=7C=5D=7C=3F=7C=2E=7C=3D=20=2F=20=00=20=50=41=53=53=45=44=20=4E=55=4C=4C=20=42=59=54=45=20=2F=20=0D=0A=20=50=41=53=53=45=44=20=43=52=4C=46=20=2F=20?==?utf-8?b?RU5E=?=\"");
            assert(addr.name == null);
            assert(addr.address == "BEGIN / (|)|<|>|@|,|;|:|\\|\"|/|[|]|?|.|= / ? PASSED NULL BYTE / \r\n PASSED CRLF / END");
        } catch (Error err) {
            assert_not_reached();
        }
    }

154
    public void is_spoofed() throws Error {
155 156 157 158 159 160 161
        assert(new MailboxAddress(null, "example@example.com").is_spoofed() == false);
        assert(new MailboxAddress("", "example@example.com").is_spoofed() == false);
        assert(new MailboxAddress("", "example@example.com").is_spoofed() == false);
        assert(new MailboxAddress("test", "example@example.com").is_spoofed() == false);
        assert(new MailboxAddress("test test", "example@example.com").is_spoofed() == false);
        assert(new MailboxAddress("test  test", "example@example.com").is_spoofed() == false);
        assert(new MailboxAddress("test?", "example@example.com").is_spoofed() == false);
162
        assert(new MailboxAddress("test@example.com", "test@example.com").is_spoofed() == false);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

        assert(new MailboxAddress("test@example.com", "example@example.com").is_spoofed() == true);
        assert(new MailboxAddress("test @ example . com", "example@example.com").is_spoofed() == true);
        assert(new MailboxAddress("\n", "example@example.com").is_spoofed() == true);
        assert(new MailboxAddress("\n", "example@example.com").is_spoofed() == true);
        assert(new MailboxAddress("test", "example@\nexample@example.com").is_spoofed() == true);
        assert(new MailboxAddress("test", "example@example@example.com").is_spoofed() == true);

        try {
            assert(new MailboxAddress.from_rfc822_string("\"=?utf-8?b?dGVzdCIgPHBvdHVzQHdoaXRlaG91c2UuZ292Pg==?==?utf-8?Q?=00=0A?=\" <demo@mailsploit.com>")
                   .is_spoofed() == true);
        } catch (Error err) {
            assert_no_error(err);
        }
    }

179
    public void has_distinct_name() throws Error {
180 181 182 183 184 185 186 187 188
        assert(new MailboxAddress("example", "example@example.com").has_distinct_name() == true);

        assert(new MailboxAddress("", "example@example.com").has_distinct_name() == false);
        assert(new MailboxAddress(" ", "example@example.com").has_distinct_name() == false);
        assert(new MailboxAddress("example@example.com", "example@example.com").has_distinct_name() == false);
        assert(new MailboxAddress(" example@example.com ", "example@example.com").has_distinct_name() == false);
        assert(new MailboxAddress(" example@example.com ", "example@example.com").has_distinct_name() == false);
    }

189
    public void to_full_display() throws Error {
190 191 192 193 194 195 196 197
        assert(new MailboxAddress("", "example@example.com").to_full_display() ==
               "example@example.com");
        assert(new MailboxAddress("Test", "example@example.com").to_full_display() ==
               "Test <example@example.com>");
        assert(new MailboxAddress("example@example.com", "example@example.com").to_full_display() ==
               "example@example.com");
        assert(new MailboxAddress("Test", "example@example@example.com").to_full_display() ==
               "example@example@example.com");
198 199 200 201 202

        assert_string(
            "\"Testerson, Test\" <test@example.com>",
            new MailboxAddress("Testerson, Test", "test@example.com").to_full_display()
        );
203 204
    }

205
    public void to_short_display() throws Error {
206 207 208 209 210 211 212 213 214 215
        assert(new MailboxAddress("", "example@example.com").to_short_display() ==
               "example@example.com");
        assert(new MailboxAddress("Test", "example@example.com").to_short_display() ==
               "Test");
        assert(new MailboxAddress("example@example.com", "example@example.com").to_short_display() ==
               "example@example.com");
        assert(new MailboxAddress("Test", "example@example@example.com").to_short_display() ==
               "example@example@example.com");
    }

216 217 218 219 220 221 222 223 224
    public void to_rfc822_address() throws GLib.Error {
        assert_string(
            "example@example.com",
            new MailboxAddress(null, "example@example.com").to_rfc822_address()
        );
        assert_string(
            "test.account@example.com",
            new MailboxAddress(null, "test.account@example.com").to_rfc822_address()
        );
225 226 227 228 229 230
        //assert(new MailboxAddress(null, "test test@example.com").to_rfc822_address() ==
        //       "\"test test\"@example.com");
        //assert(new MailboxAddress(null, "test\" test@example.com").to_rfc822_address() ==
        //       "\"test\" test\"@example.com");
        //assert(new MailboxAddress(null, "test\"test@example.com").to_rfc822_address() ==
        //       "\"test\"test\"@example.com");
231

232 233 234 235 236 237 238 239
        assert_string(
            "$test@example.com",
            new MailboxAddress(null, "$test@example.com").to_rfc822_address()
        );
        assert_string(
            "\"test@test\"@example.com",
            new MailboxAddress(null, "test@test@example.com").to_rfc822_address()
        );
240

241 242 243 244 245 246 247 248 249
        // RFC 2047 reserved words in the local-part must be used
        // as-is, and in particular not encoded per that RFC. See RFC
        // 2047 §5 and GNOME/geary#336
        string RFC_2074 = "libc-alpha-sc.1553427554.ndgdflaalknmibgfkpak-hi-angel=yandex.ru@sourceware.org";
        assert_string(
            RFC_2074,
            new MailboxAddress(null, RFC_2074).to_rfc822_address()
        );

250 251 252
        // Likewise, Unicode chars should be passed through. Note that
        // these can only be sent if a UTF8 connection is negotiated
        // with the SMTP server
253
        assert_string(
254
            "©@example.com",
255 256 257
            new MailboxAddress(null, "©@example.com").to_rfc822_address()
        );
        assert_string(
258
            "😸@example.com",
259 260
            new MailboxAddress(null, "😸@example.com").to_rfc822_address()
        );
261

262 263
    }

264
    public void to_rfc822_string() throws Error {
265 266 267 268 269 270 271 272 273 274 275
        assert(new MailboxAddress("", "example@example.com").to_rfc822_string() ==
               "example@example.com");
        assert(new MailboxAddress(" ", "example@example.com").to_rfc822_string() ==
               "example@example.com");
        assert(new MailboxAddress("test", "example@example.com").to_rfc822_string() ==
               "test <example@example.com>");
        assert(new MailboxAddress("test test", "example@example.com").to_rfc822_string() ==
               "test test <example@example.com>");
        assert(new MailboxAddress("example@example.com", "example@example.com").to_rfc822_string() ==
               "example@example.com");
        assert(new MailboxAddress("test?", "example@example.com").to_rfc822_string() ==
276 277 278
               "test? <example@example.com>");
        assert(new MailboxAddress("test@test", "example@example.com").to_rfc822_string() ==
               "\"test@test\" <example@example.com>");
279 280
        assert(new MailboxAddress(";", "example@example.com").to_rfc822_string() ==
               "\";\" <example@example.com>");
281 282 283 284
        assert(new MailboxAddress("©", "example@example.com").to_rfc822_string() ==
               "=?iso-8859-1?b?qQ==?= <example@example.com>");
        assert(new MailboxAddress("😸", "example@example.com").to_rfc822_string() ==
               "=?UTF-8?b?8J+YuA==?= <example@example.com>");
285 286 287 288 289 290 291 292 293 294 295

        assert_string(
            "\"Surname, Name\" <example@example.com>",
            new MailboxAddress("Surname, Name", "example@example.com").to_rfc822_string()
        );
        assert_string(
            "\"Surname, Name\" <example@example.com>",
            new MailboxAddress
            .from_rfc822_string("\"Surname, Name\" <example@example.com>")
            .to_rfc822_string()
        );
296
    }
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    public void equal_to() throws GLib.Error {
        MailboxAddress test = new MailboxAddress("test", "example@example.com");

        assert_true(
            test.equal_to(test),
            "Object identity equality"
        );
        assert_true(
            test.equal_to(new MailboxAddress("test", "example@example.com")),
            "Mailbox identity equality"
        );
        assert_true(
            test.equal_to(new MailboxAddress(null, "example@example.com")),
            "Address equality"
        );
        assert_false(
            test.equal_to(new MailboxAddress(null, "blarg@example.com")),
            "Address inequality"
        );
    }
318
}