Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 4e6d642d authored by Wiktor Kwapisiewicz's avatar Wiktor Kwapisiewicz
Browse files

Disallow Addresses with `null` address part

This change ensures that Address objects are always created with a valid
`address` part. If an Address contains only `personal` part then it is
silently skipped from parsed list.

Unit tests have been adjusted to this new requirement.

Removed inserting Address with `null` address component introduced in
commit 68cecb16.

Fixes #3652.

See: https://github.com/k9mail/k-9/pull/3739#issuecomment-441242481
parent 0455157e
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line

package com.fsck.k9.mail;

import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;

import java.io.Serializable;
@@ -28,6 +29,7 @@ public class Address implements Serializable {
     */
    private static final Address[] EMPTY_ADDRESS_ARRAY = new Address[0];

    @NonNull
    private String mAddress;

    private String mPersonal;
@@ -46,10 +48,16 @@ public class Address implements Serializable {
    }

    private Address(String address, String personal, boolean parse) {
        if (address == null) {
            throw new IllegalArgumentException("address");
        }
        if (parse) {
            Rfc822Token[] tokens =  Rfc822Tokenizer.tokenize(address);
            if (tokens.length > 0) {
                Rfc822Token token = tokens[0];
                if (token.getAddress() == null) {
                    throw new IllegalArgumentException("token.getAddress()");
                }
                mAddress = token.getAddress();
                String name = token.getName();
                if (!TextUtils.isEmpty(name)) {
@@ -91,6 +99,9 @@ public class Address implements Serializable {
    }

    public void setAddress(String address) {
        if (address == null) {
            throw new IllegalArgumentException("address");
        }
        this.mAddress = address;
    }

@@ -151,8 +162,7 @@ public class Address implements Serializable {
            }
        } catch (MimeException pe) {
            Timber.e(pe, "MimeException in Address.parse()");
            //but we do an silent failover : we just use the given string as name with empty address
            addresses.add(new Address(null, addressList, false));
            // broken addresses are never added to the resulting array
        }
        return addresses.toArray(EMPTY_ADDRESS_ARRAY);
    }
+4 −37
Original line number Diff line number Diff line
@@ -21,9 +21,7 @@ public class AddressTest {
    public void parse_withMissingEmail__shouldSetPersonal() {
        Address[] addresses = Address.parse("NAME ONLY");

        assertEquals(1, addresses.length);
        assertEquals(null, addresses[0].getAddress());
        assertEquals("NAME ONLY", addresses[0].getPersonal());
        assertEquals(0, addresses.length);
    }

    /**
@@ -116,10 +114,9 @@ public class AddressTest {

    @Test
    public void hashCode_withoutAddress() throws Exception {
        Address address = Address.parse("name only")[0];
        assertNull(address.getAddress());
        Address[] addresses = Address.parse("name only");

        address.hashCode();
        assertEquals(0, addresses.length);
    }

    @Test
@@ -130,27 +127,6 @@ public class AddressTest {
        address.hashCode();
    }

    @Test
    public void equals_withoutAddress_matchesSame() throws Exception {
        Address address = Address.parse("name only")[0];
        Address address2 = Address.parse("name only")[0];
        assertNull(address.getAddress());

        boolean result = address.equals(address2);

        assertTrue(result);
    }

    @Test
    public void equals_withoutAddress_doesNotMatchWithAddress() throws Exception {
        Address address = Address.parse("name only")[0];
        Address address2 = Address.parse("name <alice.example.com>")[0];

        boolean result = address.equals(address2);

        assertFalse(result);
    }

    @Test
    public void equals_withoutPersonal_matchesSame() throws Exception {
        Address address = Address.parse("alice@example.org")[0];
@@ -172,15 +148,6 @@ public class AddressTest {
        assertFalse(result);
    }

    @Test
    public void getHostname_withoutAddress_isNull() throws Exception {
        Address address = Address.parse("Alice")[0];

        String result = address.getHostname();

        assertNull(result);
    }

    @Test
    public void handlesInvalidBase64Encoding() throws Exception {
        Address address = Address.parse("=?utf-8?b?invalid#?= <oops@example.com>")[0];