001/*
002 * Copyright 2015-2024 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns.integrationtest;
012
013import static org.junit.jupiter.api.Assertions.assertEquals;
014
015import java.io.IOException;
016
017import org.minidns.DnsClient;
018import org.minidns.record.Record;
019import org.minidns.MiniDnsFuture;
020import org.minidns.dnsmessage.DnsMessage.RESPONSE_CODE;
021import org.minidns.dnsqueryresult.DnsQueryResult;
022import org.minidns.source.AbstractDnsDataSource;
023import org.minidns.source.AbstractDnsDataSource.QueryMode;
024import org.minidns.source.async.AsyncNetworkDataSource;
025
026public class AsyncApiTest {
027
028    public static void main(String[] args) throws IOException {
029        tcpAsyncApiTest();
030    }
031
032    public static void simpleAsyncApiTest() throws IOException {
033        DnsClient client = new DnsClient();
034        client.setDataSource(new AsyncNetworkDataSource());
035        client.getDataSource().setTimeout(60 * 60 * 1000);
036
037        MiniDnsFuture<DnsQueryResult, IOException> future = client.queryAsync("example.com", Record.TYPE.NS);
038        DnsQueryResult result = future.getOrThrow();
039        assertEquals(RESPONSE_CODE.NO_ERROR, result.response.responseCode);
040    }
041
042    public static void tcpAsyncApiTest() throws IOException {
043        AbstractDnsDataSource dataSource = new AsyncNetworkDataSource();
044        dataSource.setTimeout(60 * 60 * 1000);
045        dataSource.setUdpPayloadSize(256);
046        dataSource.setQueryMode(QueryMode.tcp);
047
048        DnsClient client = new DnsClient();
049        client.setDataSource(dataSource);
050        client.setAskForDnssec(true);
051
052        MiniDnsFuture<DnsQueryResult, IOException> future = client.queryAsync("google.com", Record.TYPE.AAAA);
053        DnsQueryResult result = future.getOrThrow();
054        assertEquals(RESPONSE_CODE.NO_ERROR, result.response.responseCode);
055    }
056}