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 org.minidns.DnsClient;
014import org.minidns.cache.LruCache;
015import org.minidns.dnsqueryresult.DnsQueryResult;
016import org.minidns.record.Data;
017import org.minidns.record.Record;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023
024import static org.junit.jupiter.api.Assertions.assertEquals;
025import static org.junit.jupiter.api.Assertions.assertNotNull;
026import static org.junit.jupiter.api.Assertions.assertTrue;
027
028public class CoreTest {
029    @IntegrationTest
030    public static void testExampleCom() throws IOException {
031        DnsClient client = new DnsClient(new LruCache(1024));
032        String exampleIp4 = "93.184.216.34"; // stable?
033        String exampleIp6 = "2606:2800:220:1:248:1893:25c8:1946"; // stable?
034        assertEquals(client.query("example.com", Record.TYPE.A).response.answerSection.get(0).payloadData.toString(), exampleIp4);
035        assertEquals(client.query("www.example.com", Record.TYPE.A).response.answerSection.get(0).payloadData.toString(), exampleIp4);
036        assertEquals(client.query("example.com", Record.TYPE.AAAA).response.answerSection.get(0).payloadData.toString(), exampleIp6);
037        assertEquals(client.query("www.example.com", Record.TYPE.AAAA).response.answerSection.get(0).payloadData.toString(), exampleIp6);
038
039        DnsQueryResult nsResult = client.query("example.com", Record.TYPE.NS);
040        List<String> values = new ArrayList<>();
041        for (Record<? extends Data> record : nsResult.response.answerSection) {
042            values.add(record.payloadData.toString());
043        }
044        Collections.sort(values);
045        assertEquals(values.get(0), "a.iana-servers.net.");
046        assertEquals(values.get(1), "b.iana-servers.net.");
047    }
048
049    @IntegrationTest
050    public static void testTcpAnswer() throws IOException {
051        DnsClient client = new DnsClient(new LruCache(1024));
052        client.setAskForDnssec(true);
053        client.setDisableResultFilter(true);
054        DnsQueryResult result = client.query("www-nsec.example.com", Record.TYPE.A);
055        assertNotNull(result);
056        assertTrue(result.response.toArray().length > 512);
057    }
058}