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

class Geary.RFC822.MailboxAddressTest : Gee.TestCase {

    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 23
    }

24
    public void is_valid_address() throws Error {
25 26
        assert(Geary.RFC822.MailboxAddress.is_valid_address("john@dep.aol.museum") == true);
        assert(Geary.RFC822.MailboxAddress.is_valid_address("test@example.com") == true);
27 28 29
        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);
30 31
        assert(Geary.RFC822.MailboxAddress.is_valid_address("some context test@example.com text") == true);

32
        assert(Geary.RFC822.MailboxAddress.is_valid_address("test@example") == false);
33 34 35 36 37 38 39 40 41
        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);
    }

42
    public void unescaped_constructor() throws Error {
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
        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 == "");
    }

70
    public void from_rfc822_string_encoded() throws Error {
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
        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();
        }
    }

151
    public void is_spoofed() throws Error {
152 153 154 155 156 157 158
        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);
159
        assert(new MailboxAddress("test@example.com", "test@example.com").is_spoofed() == false);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

        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);
        }
    }

176
    public void has_distinct_name() throws Error {
177 178 179 180 181 182 183 184 185
        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);
    }

186
    public void to_full_display() throws Error {
187 188 189 190 191 192 193 194 195 196
        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");
    }

197
    public void to_short_display() throws Error {
198 199 200 201 202 203 204 205 206 207
        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");
    }

208
    public void to_rfc822_address() throws Error {
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
        assert(new MailboxAddress(null, "example@example.com").to_rfc822_address() ==
               "example@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");
        //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, "©@example.com").to_rfc822_address() ==
               "\"=?iso-8859-1?b?qQ==?=\"@example.com");
        assert(new MailboxAddress(null, "😸@example.com").to_rfc822_address() ==
               "\"=?UTF-8?b?8J+YuA==?=\"@example.com");
    }

225
    public void to_rfc822_string() throws Error {
226 227 228 229 230 231 232 233 234 235 236
        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() ==
237 238 239
               "test? <example@example.com>");
        assert(new MailboxAddress("test@test", "example@example.com").to_rfc822_string() ==
               "\"test@test\" <example@example.com>");
240 241
        assert(new MailboxAddress(";", "example@example.com").to_rfc822_string() ==
               "\";\" <example@example.com>");
242 243 244 245
        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>");
246
    }
247

248
}