You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/08/08 10:46:41 UTC

[06/10] james-project git commit: JAMES-1589 Add tests for auto-detected IPs

JAMES-1589 Add tests for auto-detected IPs


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/f375965b
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/f375965b
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/f375965b

Branch: refs/heads/master
Commit: f375965bd3eda036ca3fa04e83df38898e0963fa
Parents: 2bbfdcf
Author: benwa <bt...@linagora.com>
Authored: Fri Jul 28 16:44:19 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Tue Aug 8 17:08:50 2017 +0700

----------------------------------------------------------------------
 .../domainlist/lib/AbstractDomainList.java      |  56 ++++++----
 .../AbstractDomainListPrivateMethodsTest.java   | 108 +++++++++++++++++--
 2 files changed, 134 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/f375965b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
index d103a09..afc8a1d 100644
--- a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
+++ b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
@@ -50,6 +50,9 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf
     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractDomainList.class);
 
     protected static final String LOCALHOST = "localhost";
+    public static final String CONFIGURE_AUTODETECT = "autodetect";
+    public static final String CONFIGURE_AUTODETECT_IP = "autodetectIP";
+    public static final String CONFIGURE_DEFAULT_DOMAIN = "defaultDomain";
 
     private DNSService dns;
     private boolean autoDetect = true;
@@ -74,14 +77,14 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf
     public void configure(HierarchicalConfiguration config) throws ConfigurationException {
         configureDefaultDomain(config);
 
-        setAutoDetect(config.getBoolean("autodetect", true));
-        setAutoDetectIP(config.getBoolean("autodetectIP", true));
+        setAutoDetect(config.getBoolean(CONFIGURE_AUTODETECT, true));
+        setAutoDetectIP(config.getBoolean(CONFIGURE_AUTODETECT_IP, true));
     }
 
     @VisibleForTesting void configureDefaultDomain(HierarchicalConfiguration config) throws ConfigurationException {
         
         try {
-            setDefaultDomain(config.getString("defaultDomain", LOCALHOST));
+            setDefaultDomain(config.getString(CONFIGURE_DEFAULT_DOMAIN, LOCALHOST));
 
             String hostName = InetAddress.getLocalHost().getHostName();
             if (mayChangeDefaultDomain()) {
@@ -99,7 +102,7 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf
     }
 
     private void setDefaultDomain(String defaultDomain) throws DomainListException {
-        if (!containsDomain(defaultDomain)) {
+        if (defaultDomain != null && !containsDomain(defaultDomain)) {
             addDomain(defaultDomain);
         }
         this.defaultDomain = defaultDomain;
@@ -117,11 +120,31 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf
     @Override
     public List<String> getDomains() throws DomainListException {
         List<String> domains = getDomainListInternal();
-        if (domains != null) {
 
-            // create mutable copy, some subclasses return ImmutableList
-            ArrayList<String> mutableDomains = new ArrayList<String>(domains);
+        // create mutable copy, some subclasses return ImmutableList
+        ArrayList<String> mutableDomains = new ArrayList<String>(domains);
+        List<String> detectedDomains = detectDomains();
+        mutableDomains.addAll(detectedDomains);
+        mutableDomains.addAll(detectIps(mutableDomains));
 
+        if (getLogger().isInfoEnabled()) {
+            for (String domain : mutableDomains) {
+                getLogger().debug("Handling mail for: " + domain);
+            }
+        }
+
+        return ImmutableList.copyOf(mutableDomains);
+    }
+
+    private List<String> detectIps(ArrayList<String> mutableDomains) {
+        if (autoDetectIP) {
+            return getDomainsIP(mutableDomains, dns, getLogger());
+        }
+        return ImmutableList.of();
+    }
+
+    private List<String> detectDomains() {
+        if (autoDetect) {
             String hostName;
             try {
                 hostName = getDNSServer().getHostName(getDNSServer().getLocalHost());
@@ -130,24 +153,11 @@ public abstract class AbstractDomainList implements DomainList, LogEnabled, Conf
             }
 
             getLogger().info("Local host is: " + hostName);
-
-            if (autoDetect && (!hostName.equals("localhost"))) {
-                mutableDomains.add(hostName.toLowerCase(Locale.US));
-            }
-
-            if (autoDetectIP) {
-                mutableDomains.addAll(getDomainsIP(mutableDomains, dns, getLogger()));
+            if (!hostName.equals("localhost")) {
+                return ImmutableList.of(hostName.toLowerCase(Locale.US));
             }
-
-            if (getLogger().isInfoEnabled()) {
-                for (String domain : mutableDomains) {
-                    getLogger().debug("Handling mail for: " + domain);
-                }
-            }
-
-            return ImmutableList.copyOf(mutableDomains);
         }
-        return ImmutableList.of();// empty list
+        return ImmutableList.of();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/f375965b/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java b/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java
index 8dce3d8..9255dfd 100644
--- a/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java
+++ b/server/data/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListPrivateMethodsTest.java
@@ -20,26 +20,38 @@
 package org.apache.james.domainlist.lib;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
 
 import java.net.InetAddress;
 import java.util.List;
 
 import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.domainlist.api.DomainListException;
 import org.junit.Before;
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 
 public class AbstractDomainListPrivateMethodsTest {
 
+    public static final Logger LOGGER = LoggerFactory.getLogger(AbstractDomainListPrivateMethodsTest.class);
     private MyDomainList domainList;
-    
+    private DNSService dnsService;
+
     @Before
     public void setup() {
         domainList = new MyDomainList();
+        dnsService = mock(DNSService.class);
+        domainList.setDNSService(dnsService);
+        domainList.setLog(LOGGER);
     }
 
     private static class MyDomainList extends AbstractDomainList {
@@ -75,7 +87,7 @@ public class AbstractDomainListPrivateMethodsTest {
     public void setDefaultDomainShouldSetFromConfigurationWhenDifferentFromLocalhost() throws Exception {
         HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
         String expectedDefaultDomain = "myDomain.org";
-        when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST))
+        when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, AbstractDomainList.LOCALHOST))
             .thenReturn(expectedDefaultDomain);
 
         domainList.configureDefaultDomain(configuration);
@@ -86,7 +98,7 @@ public class AbstractDomainListPrivateMethodsTest {
     @Test
     public void setDefaultDomainShouldSetFromHostnameWhenEqualsToLocalhost() throws Exception {
         HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
-        when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST))
+        when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, AbstractDomainList.LOCALHOST))
             .thenReturn(AbstractDomainList.LOCALHOST);
 
         String expectedDefaultDomain = InetAddress.getLocalHost().getHostName();
@@ -98,7 +110,7 @@ public class AbstractDomainListPrivateMethodsTest {
     @Test
     public void setDefaultDomainShouldCreateFromHostnameWhenEqualsToLocalhost() throws Exception {
         HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
-        when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST))
+        when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, AbstractDomainList.LOCALHOST))
             .thenReturn(AbstractDomainList.LOCALHOST);
 
         String expectedDefaultDomain = InetAddress.getLocalHost().getHostName();
@@ -110,7 +122,7 @@ public class AbstractDomainListPrivateMethodsTest {
     @Test
     public void setDefaultDomainShouldNotCreateTwiceWhenCallingTwoTimes() throws Exception {
         HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
-        when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST))
+        when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, AbstractDomainList.LOCALHOST))
             .thenReturn(AbstractDomainList.LOCALHOST);
 
         String expectedDefaultDomain = InetAddress.getLocalHost().getHostName();
@@ -124,7 +136,7 @@ public class AbstractDomainListPrivateMethodsTest {
     public void setDefaultDomainShouldAddDomainWhenNotContained() throws Exception {
         HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
         String expectedDefaultDomain = "myDomain.org";
-        when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST))
+        when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, AbstractDomainList.LOCALHOST))
             .thenReturn(expectedDefaultDomain);
 
         domainList.configureDefaultDomain(configuration);
@@ -136,7 +148,7 @@ public class AbstractDomainListPrivateMethodsTest {
     public void setDefaultDomainNotFailWhenDomainContained() throws Exception {
         HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
         String expectedDefaultDomain = "myDomain.org";
-        when(configuration.getString("defaultDomain", AbstractDomainList.LOCALHOST))
+        when(configuration.getString(AbstractDomainList.CONFIGURE_DEFAULT_DOMAIN, AbstractDomainList.LOCALHOST))
             .thenReturn(expectedDefaultDomain);
 
         domainList.addDomain(expectedDefaultDomain);
@@ -144,4 +156,86 @@ public class AbstractDomainListPrivateMethodsTest {
 
         assertThat(domainList.getDomainListInternal()).contains(expectedDefaultDomain);
     }
+
+    @Test
+    public void getDomainsShouldNotDetectDomainsWhenDisabled () throws Exception {
+        HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
+
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(false);
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(false);
+        domainList.configure(configuration);
+
+        assertThat(domainList.getDomains()).isEmpty();
+    }
+
+    @Test
+    public void getDomainsShouldNotInteractWithDNSWhenDisabled () throws Exception {
+        HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
+
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(false);
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(false);
+        domainList.configure(configuration);
+        domainList.getDomains();
+
+        verifyZeroInteractions(dnsService);
+    }
+
+    @Test
+    public void getDomainsShouldContainDetectedDomains() throws Exception {
+        HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
+
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(true);
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(false);
+        domainList.setLog(LOGGER);
+        domainList.configure(configuration);
+
+        String detected = "detected.tld";
+        when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected);
+
+        assertThat(domainList.getDomains()).containsOnly(detected);
+    }
+
+    @Test
+    public void getDomainsShouldContainDetectedDomainsAndIps() throws Exception {
+        HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
+
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(true);
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(true);
+        domainList.setLog(LOGGER);
+        domainList.configure(configuration);
+
+        String detected = "detected.tld";
+        String detectedIp = "148.25.32.1";
+        when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected);
+        InetAddress detectedAddress = mock(InetAddress.class);
+        when(detectedAddress.getHostAddress()).thenReturn(detectedIp);
+        when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress));
+
+        assertThat(domainList.getDomains()).containsOnly(detected, detectedIp);
+    }
+
+    @Test
+    public void getDomainsShouldContainDetectedDomainsAndIpsOfAddedDomains() throws Exception {
+        HierarchicalConfiguration configuration = mock(HierarchicalConfiguration.class);
+
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT, true)).thenReturn(true);
+        when(configuration.getBoolean(AbstractDomainList.CONFIGURE_AUTODETECT_IP, true)).thenReturn(true);
+        domainList.setLog(LOGGER);
+        domainList.configure(configuration);
+
+        String added = "added.tld";
+        String detected = "detected.tld";
+        String detectedIp1 = "148.25.32.1";
+        String detectedIp2 = "148.25.32.2";
+        when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected);
+        InetAddress detectedAddress1 = mock(InetAddress.class);
+        InetAddress detectedAddress2 = mock(InetAddress.class);
+        when(detectedAddress1.getHostAddress()).thenReturn(detectedIp1);
+        when(detectedAddress2.getHostAddress()).thenReturn(detectedIp2);
+        when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress1));
+        when(dnsService.getAllByName(added)).thenReturn(ImmutableList.of(detectedAddress2));
+        domainList.addDomain(added);
+
+        assertThat(domainList.getDomains()).containsOnly(detected, detectedIp1, added, detectedIp2);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org