DNSJavaService implements DNSService, DNSServiceMBean, Configurable { @Override public Collection<String> findMXRecords(String hostname) throws TemporaryResolutionException { TimeMetric timeMetric = metricFactory.timer("findMXRecords"); List<String> servers = new ArrayList<>(); try { servers = findMXRecordsRaw(hostname); return Collections.unmodifiableCollection(servers); } finally { if (servers.size() == 0) { LOGGER.info("Couldn't resolve MX records for domain {}.", hostname); try { getByName(hostname); servers.add(hostname); } catch (UnknownHostException uhe) { LOGGER.error("Couldn't resolve IP address for host {}.", hostname, uhe); } } timeMetric.stopAndPublish(); } } @Inject  DNSJavaService(MetricFactory metricFactory); @Override void configure(HierarchicalConfiguration<ImmutableNode> configuration); @PostConstruct void init(); @Override String[] getDNSServers(); Name[] getSearchPaths(); @Override Collection<String> findMXRecords(String hostname); @Override InetAddress getByName(String host); @Override Collection<InetAddress> getAllByName(String host); @Override Collection<String> findTXTRecords(String hostname); @Override String getHostName(InetAddress addr); @Override InetAddress getLocalHost(); @Override int getMaximumCacheSize(); @Override int getCurrentCacheSize(); @Override void clearCache();  }
@Test public void testNoMX() throws Exception { doAnswer(new ZoneCacheLookupRecordsAnswer(loadZone("dnstest.com."))) .when(mockedCache).lookupRecords(any(Name.class), anyInt(), anyInt()); dnsServer.setCache(mockedCache); Collection<String> records = dnsServer.findMXRecords("nomx.dnstest.com."); assertThat(records.size()).isEqualTo(1); assertThat(records.iterator().next()).isEqualTo("nomx.dnstest.com."); }
@Test public void testBadMX() throws Exception { doAnswer(new ZoneCacheLookupRecordsAnswer(loadZone("dnstest.com."))) .when(mockedCache).lookupRecords(any(Name.class), anyInt(), anyInt()); dnsServer.setCache(mockedCache); Collection<String> records = dnsServer.findMXRecords("badmx.dnstest.com."); assertThat(records.size()).isEqualTo(1); assertThat(records.iterator().next()).isEqualTo("badhost.dnstest.com."); }
@Test public void testINARecords() throws Exception { doAnswer(new ZoneCacheLookupRecordsAnswer(loadZone("pippo.com."))) .when(mockedCache).lookupRecords(any(Name.class), anyInt(), anyInt()); dnsServer.setCache(mockedCache); Collection<String> records = dnsServer.findMXRecords("www.pippo.com."); assertThat(records.size()).isEqualTo(1); assertThat(records.iterator().next()).isEqualTo("pippo.com.inbound.mxlogic.net."); }
@Test public void testMXCatches() throws Exception { doAnswer(new ZoneCacheLookupRecordsAnswer(loadZone("test-zone.com."))) .when(mockedCache).lookupRecords(any(Name.class), anyInt(), anyInt()); dnsServer.setCache(mockedCache); Collection<String> res = dnsServer.findMXRecords("test-zone.com."); try { res.add(""); fail("MX Collection should not be modifiable"); } catch (UnsupportedOperationException e) { LOGGER.info("Ignored error", e); } assertThat(res.size()).isEqualTo(1); assertThat(res.iterator().next()).isEqualTo("mail.test-zone.com."); }
@Test public void testTwoMXSamePrio() throws Exception { doAnswer(new ZoneCacheLookupRecordsAnswer(loadZone("two-mx.sameprio."))) .when(mockedCache).lookupRecords(any(Name.class), anyInt(), anyInt()); dnsServer.setCache(mockedCache); Collection<String> records = dnsServer.findMXRecords("two-mx.sameprio."); assertThat(records.size()).isEqualTo(2); assertThat(records.contains("mx1.two-mx.sameprio.")).isTrue(); assertThat(records.contains("mx2.two-mx.sameprio.")).isTrue(); }
@Test public void testThreeMX() throws Exception { doAnswer(new ZoneCacheLookupRecordsAnswer(loadZone("three-mx.bar."))) .when(mockedCache).lookupRecords(any(Name.class), anyInt(), anyInt()); dnsServer.setCache(mockedCache); ArrayList<String> records = new ArrayList<>(dnsServer.findMXRecords("three-mx.bar.")); assertThat(records.size()).isEqualTo(3); assertThat(records.contains("mx1.three-mx.bar.")).isTrue(); assertThat(records.contains("mx2.three-mx.bar.")).isTrue(); assertThat(records.get(2)).isEqualTo("mx3.three-mx.bar."); }
@Test public void testTwoMXDifferentPrio() throws Exception { doAnswer(new ZoneCacheLookupRecordsAnswer(loadZone("two-mx.differentprio."))) .when(mockedCache).lookupRecords(any(Name.class), anyInt(), anyInt()); dnsServer.setCache(mockedCache); Collection<String> records = dnsServer.findMXRecords("two-mx.differentprio."); assertThat(records.size()).isEqualTo(2); assertThat(records.contains("mx1.two-mx.differentprio.")).isTrue(); assertThat(records.contains("mx2.two-mx.differentprio.")).isTrue(); }
@Test public void testOneMX() throws Exception { doAnswer(new ZoneCacheLookupRecordsAnswer(loadZone("one-mx.bar."))) .when(mockedCache).lookupRecords(any(Name.class), anyInt(), anyInt()); dnsServer.setCache(mockedCache); Collection<String> records = dnsServer.findMXRecords("one-mx.bar."); assertThat(records.size()).isEqualTo(1); assertThat(records.contains("mx1.one-mx.bar.")).isTrue(); }