Search in sources :

Example 1 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions in project vert.x by eclipse.

the class HostnameResolutionTest method testMultipleSearchDomain.

@Test
public void testMultipleSearchDomain() throws Exception {
    Map<String, String> records = new HashMap<>();
    records.put("host1.foo.com", "127.0.0.1");
    records.put("host2.bar.com", "127.0.0.2");
    records.put("host3.bar.com", "127.0.0.3");
    records.put("host3.foo.com", "127.0.0.4");
    dnsServer.stop();
    dnsServer = FakeDNSServer.testResolveA(records);
    dnsServer.start();
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false).addSearchDomain("foo.com").addSearchDomain("bar.com")));
    // "host1" resolves via the "foo.com" search path
    CountDownLatch latch1 = new CountDownLatch(1);
    vertx.resolveAddress("host1", onSuccess(resolved -> {
        assertEquals("127.0.0.1", resolved.getHostAddress());
        latch1.countDown();
    }));
    awaitLatch(latch1);
    // "host2" resolves via the "bar.com" search path
    CountDownLatch latch2 = new CountDownLatch(1);
    vertx.resolveAddress("host2", onSuccess(resolved -> {
        assertEquals("127.0.0.2", resolved.getHostAddress());
        latch2.countDown();
    }));
    awaitLatch(latch2);
    // "host3" resolves via the the "foo.com" search path as it is the first one
    CountDownLatch latch3 = new CountDownLatch(1);
    vertx.resolveAddress("host3", onSuccess(resolved -> {
        assertEquals("127.0.0.4", resolved.getHostAddress());
        latch3.countDown();
    }));
    awaitLatch(latch3);
    // "host4" does not resolve
    vertx.resolveAddress("host4", onFailure(cause -> {
        assertTrue(cause instanceof UnknownHostException);
        testComplete();
    }));
    await();
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) VertxInternal(io.vertx.core.impl.VertxInternal) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 2 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions in project vert.x by eclipse.

the class HostnameResolutionTest method testSearchDomainWithNdots0.

@Test
public void testSearchDomainWithNdots0() throws Exception {
    Map<String, String> records = new HashMap<>();
    records.put("host1", "127.0.0.2");
    records.put("host1.foo.com", "127.0.0.3");
    dnsServer.stop();
    dnsServer = FakeDNSServer.testResolveA(records);
    dnsServer.start();
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false).addSearchDomain("foo.com").setNdots(0)));
    // "host1" resolves directly as ndots = 0
    CountDownLatch latch1 = new CountDownLatch(1);
    vertx.resolveAddress("host1", onSuccess(resolved -> {
        assertEquals("127.0.0.2", resolved.getHostAddress());
        latch1.countDown();
    }));
    awaitLatch(latch1);
    // "host1.foo.com" resolves to host1.foo.com
    CountDownLatch latch2 = new CountDownLatch(1);
    vertx.resolveAddress("host1.foo.com", onSuccess(resolved -> {
        assertEquals("127.0.0.3", resolved.getHostAddress());
        latch2.countDown();
    }));
    awaitLatch(latch2);
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) VertxInternal(io.vertx.core.impl.VertxInternal) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 3 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions in project vert.x by eclipse.

the class HostnameResolutionTest method testResolveFromBuffer.

@Test
public void testResolveFromBuffer() {
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsValue(Buffer.buffer("192.168.0.15 server.net"))));
    vertx.resolveAddress("server.net", onSuccess(addr -> {
        assertEquals("192.168.0.15", addr.getHostAddress());
        assertEquals("server.net", addr.getHostName());
        testComplete();
    }));
    await();
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) VertxInternal(io.vertx.core.impl.VertxInternal) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 4 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions in project vert.x by eclipse.

the class HostnameResolutionTest method testOptions.

@Test
public void testOptions() {
    AddressResolverOptions options = new AddressResolverOptions();
    assertEquals(AddressResolverOptions.DEFAULT_OPT_RESOURCE_ENABLED, options.isOptResourceEnabled());
    assertEquals(AddressResolverOptions.DEFAULT_SERVERS, options.getServers());
    assertEquals(AddressResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE, options.getCacheMinTimeToLive());
    assertEquals(AddressResolverOptions.DEFAULT_CACHE_MAX_TIME_TO_LIVE, options.getCacheMaxTimeToLive());
    assertEquals(AddressResolverOptions.DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE, options.getCacheNegativeTimeToLive());
    assertEquals(AddressResolverOptions.DEFAULT_QUERY_TIMEOUT, options.getQueryTimeout());
    assertEquals(AddressResolverOptions.DEFAULT_MAX_QUERIES, options.getMaxQueries());
    assertEquals(AddressResolverOptions.DEFAULT_RD_FLAG, options.getRdFlag());
    assertEquals(AddressResolverOptions.DEFAULT_NDOTS, options.getNdots());
    assertEquals(AddressResolverOptions.DEFAULT_SEACH_DOMAINS, options.getSearchDomains());
    boolean optResourceEnabled = TestUtils.randomBoolean();
    List<String> servers = Arrays.asList("1.2.3.4", "5.6.7.8");
    int minTTL = TestUtils.randomPositiveInt();
    int maxTTL = minTTL + 1000;
    int negativeTTL = TestUtils.randomPositiveInt();
    int queryTimeout = 1 + TestUtils.randomPositiveInt();
    int maxQueries = 1 + TestUtils.randomPositiveInt();
    boolean rdFlag = TestUtils.randomBoolean();
    int ndots = TestUtils.randomPositiveInt() - 2;
    List<String> searchDomains = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
        searchDomains.add(TestUtils.randomAlphaString(15));
    }
    assertSame(options, options.setOptResourceEnabled(optResourceEnabled));
    assertSame(options, options.setServers(new ArrayList<>(servers)));
    assertSame(options, options.setCacheMinTimeToLive(0));
    assertSame(options, options.setCacheMinTimeToLive(minTTL));
    try {
        options.setCacheMinTimeToLive(-1);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertSame(options, options.setCacheMaxTimeToLive(0));
    assertSame(options, options.setCacheMaxTimeToLive(maxTTL));
    try {
        options.setCacheMaxTimeToLive(-1);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertSame(options, options.setCacheNegativeTimeToLive(0));
    assertSame(options, options.setCacheNegativeTimeToLive(negativeTTL));
    try {
        options.setCacheNegativeTimeToLive(-1);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertSame(options, options.setQueryTimeout(queryTimeout));
    try {
        options.setQueryTimeout(0);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertSame(options, options.setMaxQueries(maxQueries));
    try {
        options.setMaxQueries(0);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertSame(options, options.setRdFlag(rdFlag));
    assertSame(options, options.setSearchDomains(searchDomains));
    assertSame(options, options.setNdots(ndots));
    try {
        options.setNdots(-2);
        fail("Should throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertEquals(optResourceEnabled, options.isOptResourceEnabled());
    assertEquals(servers, options.getServers());
    assertEquals(minTTL, options.getCacheMinTimeToLive());
    assertEquals(maxTTL, options.getCacheMaxTimeToLive());
    assertEquals(negativeTTL, options.getCacheNegativeTimeToLive());
    assertEquals(queryTimeout, options.getQueryTimeout());
    assertEquals(maxQueries, options.getMaxQueries());
    assertEquals(rdFlag, options.getRdFlag());
    assertEquals(ndots, options.getNdots());
    assertEquals(searchDomains, options.getSearchDomains());
    // Test copy and json copy
    AddressResolverOptions copy = new AddressResolverOptions(options);
    AddressResolverOptions jsonCopy = new AddressResolverOptions(options.toJson());
    options.setOptResourceEnabled(AddressResolverOptions.DEFAULT_OPT_RESOURCE_ENABLED);
    options.getServers().clear();
    options.setCacheMinTimeToLive(AddressResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE);
    options.setCacheMaxTimeToLive(AddressResolverOptions.DEFAULT_CACHE_MAX_TIME_TO_LIVE);
    options.setCacheNegativeTimeToLive(AddressResolverOptions.DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE);
    options.setQueryTimeout(AddressResolverOptions.DEFAULT_QUERY_TIMEOUT);
    options.setMaxQueries(AddressResolverOptions.DEFAULT_MAX_QUERIES);
    options.setRdFlag(AddressResolverOptions.DEFAULT_RD_FLAG);
    options.setNdots(AddressResolverOptions.DEFAULT_NDOTS);
    options.setSearchDomains(AddressResolverOptions.DEFAULT_SEACH_DOMAINS);
    assertEquals(optResourceEnabled, copy.isOptResourceEnabled());
    assertEquals(servers, copy.getServers());
    assertEquals(minTTL, copy.getCacheMinTimeToLive());
    assertEquals(maxTTL, copy.getCacheMaxTimeToLive());
    assertEquals(negativeTTL, copy.getCacheNegativeTimeToLive());
    assertEquals(queryTimeout, copy.getQueryTimeout());
    assertEquals(maxQueries, copy.getMaxQueries());
    assertEquals(rdFlag, copy.getRdFlag());
    assertEquals(ndots, copy.getNdots());
    assertEquals(searchDomains, copy.getSearchDomains());
    assertEquals(optResourceEnabled, jsonCopy.isOptResourceEnabled());
    assertEquals(servers, jsonCopy.getServers());
    assertEquals(minTTL, jsonCopy.getCacheMinTimeToLive());
    assertEquals(maxTTL, jsonCopy.getCacheMaxTimeToLive());
    assertEquals(negativeTTL, jsonCopy.getCacheNegativeTimeToLive());
    assertEquals(queryTimeout, jsonCopy.getQueryTimeout());
    assertEquals(maxQueries, jsonCopy.getMaxQueries());
    assertEquals(rdFlag, jsonCopy.getRdFlag());
    assertEquals(ndots, jsonCopy.getNdots());
    assertEquals(searchDomains, jsonCopy.getSearchDomains());
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions in project vert.x by eclipse.

the class HostnameResolutionTest method testResolveFromClasspath.

@Test
public void testResolveFromClasspath() {
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath("hosts_config.txt")));
    vertx.resolveAddress("server.net", onSuccess(addr -> {
        assertEquals("192.168.0.15", addr.getHostAddress());
        assertEquals("server.net", addr.getHostName());
        testComplete();
    }));
    await();
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) VertxInternal(io.vertx.core.impl.VertxInternal) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Aggregations

AddressResolverOptions (io.vertx.core.dns.AddressResolverOptions)26 Test (org.junit.Test)22 VertxOptions (io.vertx.core.VertxOptions)12 AddressResolver (io.vertx.core.impl.AddressResolver)11 InetAddress (java.net.InetAddress)11 CompletableFuture (java.util.concurrent.CompletableFuture)11 FakeDNSServer (io.vertx.test.fakedns.FakeDNSServer)10 InetSocketAddress (java.net.InetSocketAddress)10 ArrayList (java.util.ArrayList)10 VertxException (io.vertx.core.VertxException)9 Buffer (io.vertx.core.buffer.Buffer)9 VertxInternal (io.vertx.core.impl.VertxInternal)9 JsonObject (io.vertx.core.json.JsonObject)9 NetClient (io.vertx.core.net.NetClient)9 NetServer (io.vertx.core.net.NetServer)9 NetServerOptions (io.vertx.core.net.NetServerOptions)9 HashMap (java.util.HashMap)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 Channel (io.netty.channel.Channel)8