[geary/wip/791275-mailsploit-mitigation: 6/8] Fix MailboxAddresses.to_rfc822_string formatting, add unit tests.



commit 178ce35113c7408d7ca8b3353f430040dda84fb3
Author: Michael James Gratton <mike vee net>
Date:   Wed Jan 31 11:09:09 2018 +1030

    Fix MailboxAddresses.to_rfc822_string formatting, add unit tests.

 src/engine/rfc822/rfc822-mailbox-addresses.vala |   15 ++++---
 test/CMakeLists.txt                             |    1 +
 test/engine/rfc822-mailbox-addresses-test.vala  |   46 +++++++++++++++++++++++
 test/meson.build                                |    1 +
 test/test-engine.vala                           |    1 +
 5 files changed, 57 insertions(+), 7 deletions(-)
---
diff --git a/src/engine/rfc822/rfc822-mailbox-addresses.vala b/src/engine/rfc822/rfc822-mailbox-addresses.vala
index 2667ff1..a90c747 100644
--- a/src/engine/rfc822/rfc822-mailbox-addresses.vala
+++ b/src/engine/rfc822/rfc822-mailbox-addresses.vala
@@ -105,15 +105,16 @@ public class Geary.RFC822.MailboxAddresses : Geary.MessageData.AbstractMessageDa
     }
 
     /**
-     * Returns the addresses suitable for insertion into an RFC822 message.  RFC822 quoting is
-     * performed if required.
+     * Returns the addresses suitable for insertion into an RFC822 message.
+     *
+     * RFC822 quoting is performed if required.
      *
      * @see MailboxAddress.to_rfc822_string
      */
     public string to_rfc822_string() {
-        return MailboxAddress.list_to_string(addrs, "", (a) => a.to_rfc822_string());
+        return MailboxAddress.list_to_string(addrs, ", ", (a) => a.to_rfc822_string());
     }
-    
+
     public uint hash() {
         // create sorted set to ensure ordering no matter the list's order
         Gee.TreeSet<string> sorted_addresses = traverse<RFC822.MailboxAddress>(addrs)
@@ -143,14 +144,14 @@ public class Geary.RFC822.MailboxAddresses : Geary.MessageData.AbstractMessageDa
         
         return Collection.are_sets_equal<RFC822.MailboxAddress>(first, second);
     }
-    
+
     /**
      * See Geary.MessageData.SearchableMessageData.
      */
     public string to_searchable_string() {
-        return MailboxAddress.list_to_string(addrs, "", (a) => a.to_searchable_string());
+        return MailboxAddress.list_to_string(addrs, " ", (a) => a.to_searchable_string());
     }
-    
+
     public override string to_string() {
         return MailboxAddress.list_to_string(addrs, "(no addresses)", (a) => a.to_string());
     }
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index adae1bd..6f9ab8c 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -20,6 +20,7 @@ set(TEST_ENGINE_SRC
   engine/imap-engine/account-processor-test.vala
   engine/mime-content-type-test.vala
   engine/rfc822-mailbox-address-test.vala
+  engine/rfc822-mailbox-addresses-test.vala
   engine/rfc822-message-test.vala
   engine/rfc822-message-data-test.vala
   engine/rfc822-utils-test.vala
diff --git a/test/engine/rfc822-mailbox-addresses-test.vala b/test/engine/rfc822-mailbox-addresses-test.vala
new file mode 100644
index 0000000..3c252d8
--- /dev/null
+++ b/test/engine/rfc822-mailbox-addresses-test.vala
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2018 Michael Gratton <mike vee net>
+ *
+ * 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.MailboxAddressesTest : Gee.TestCase {
+
+    public MailboxAddressesTest() {
+        base("Geary.RFC822.MailboxAddressesTest");
+        add_test("from_rfc822_string_encoded", from_rfc822_string_encoded);
+        add_test("to_rfc822_string", to_rfc822_string);
+    }
+
+    public void from_rfc822_string_encoded() {
+        MailboxAddresses addrs = new MailboxAddresses.from_rfc822_string("test example com");
+        assert(addrs.size == 1);
+
+        addrs = new MailboxAddresses.from_rfc822_string("test1 example com, test2 example com");
+        assert(addrs.size == 2);
+
+        // Courtesy Mailsploit https://www.mailsploit.com
+        addrs = new 
MailboxAddresses.from_rfc822_string("\"=?utf-8?b?dGVzdCIgPHBvdHVzQHdoaXRlaG91c2UuZ292Pg==?==?utf-8?Q?=00=0A?=\"
 <demo mailsploit com>");
+        assert(addrs.size == 1);
+
+        // Courtesy Mailsploit https://www.mailsploit.com
+        addrs = new 
MailboxAddresses.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=?=\",
        <demo mailsploit com>");
+        assert(addrs.size == 2);
+    }
+
+    public void to_rfc822_string() {
+        assert(new_addreses({ "test1 example com" }).to_rfc822_string() ==
+               "test1 example com");
+        assert(new_addreses({ "test1 example com", "test2 example com" }).to_rfc822_string() ==
+               "test1 example com, test2 example com");
+    }
+
+    private MailboxAddresses new_addreses(string[] address_strings) {
+        Gee.List<MailboxAddress> addresses = new Gee.LinkedList<MailboxAddress>();
+        foreach (string address in address_strings) {
+            addresses.add(new MailboxAddress(null, address));
+        }
+        return new MailboxAddresses(addresses);
+    }
+}
diff --git a/test/meson.build b/test/meson.build
index d9bee04..4afad25 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -16,6 +16,7 @@ geary_test_engine_sources = [
   'engine/imap-engine/account-processor-test.vala',
   'engine/mime-content-type-test.vala',
   'engine/rfc822-mailbox-address-test.vala',
+  'engine/rfc822-mailbox-addresses-test.vala',
   'engine/rfc822-message-test.vala',
   'engine/rfc822-message-data-test.vala',
   'engine/rfc822-utils-test.vala',
diff --git a/test/test-engine.vala b/test/test-engine.vala
index d947f4b..d5c06bc 100644
--- a/test/test-engine.vala
+++ b/test/test-engine.vala
@@ -37,6 +37,7 @@ int main(string[] args) {
     engine.add_suite(new Geary.JS.Test().get_suite());
     engine.add_suite(new Geary.Mime.ContentTypeTest().get_suite());
     engine.add_suite(new Geary.RFC822.MailboxAddressTest().get_suite());
+    engine.add_suite(new Geary.RFC822.MailboxAddressesTest().get_suite());
     engine.add_suite(new Geary.RFC822.MessageTest().get_suite());
     engine.add_suite(new Geary.RFC822.MessageDataTest().get_suite());
     engine.add_suite(new Geary.RFC822.Utils.Test().get_suite());


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]