| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.iterative; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
import java.util.List; |
| 15 |
|
| 16 |
import org.minidns.cache.LruCache; |
| 17 |
import org.minidns.dnsmessage.DnsMessage; |
| 18 |
import org.minidns.dnsqueryresult.DnsQueryResult; |
| 19 |
import org.minidns.record.A; |
| 20 |
import org.minidns.record.Data; |
| 21 |
import org.minidns.record.Record; |
| 22 |
import org.minidns.record.Record.TYPE; |
| 23 |
import org.junit.jupiter.api.Test; |
| 24 |
|
| 25 |
import static org.minidns.DnsWorld.a; |
| 26 |
import static org.minidns.DnsWorld.applyZones; |
| 27 |
import static org.minidns.DnsWorld.ns; |
| 28 |
import static org.minidns.DnsWorld.record; |
| 29 |
import static org.minidns.DnsWorld.rootZone; |
| 30 |
import static org.minidns.DnsWorld.zone; |
| 31 |
import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 32 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 |
import static org.junit.jupiter.api.Assertions.assertThrows; |
| 34 |
|
| 35 |
public class IterativeDnsClientTest { |
| 36 |
|
| 37 |
@SuppressWarnings("unchecked") |
| 38 |
@Test |
| 39 |
public void basicIterativeTest() throws IOException { |
| 40 |
IterativeDnsClient client = new IterativeDnsClient(new LruCache(0)); |
| 41 |
applyZones(client, |
| 42 |
rootZone( |
| 43 |
record("com", ns("ns.com")), |
| 44 |
record("ns.com", a("1.1.1.1")) |
| 45 |
), zone("com", "ns.com", "1.1.1.1", |
| 46 |
record("example.com", ns("ns.example.com")), |
| 47 |
record("ns.example.com", a("1.1.1.2")) |
| 48 |
), zone("example.com", "ns.example.com", "1.1.1.2", |
| 49 |
record("www.example.com", a("1.1.1.3")) |
| 50 |
) |
| 51 |
); |
| 52 |
DnsQueryResult result = client.query("www.example.com", TYPE.A); |
| 53 |
DnsMessage message = result.response; |
| 54 |
List<Record<? extends Data>> answers = message.answerSection; |
| 55 |
assertEquals(1, answers.size()); |
| 56 |
assertEquals(TYPE.A, answers.get(0).type); |
| 57 |
assertArrayEquals(new byte[] {1, 1, 1, 3}, ((A) answers.get(0).payloadData).getIp()); |
| 58 |
} |
| 59 |
|
| 60 |
@SuppressWarnings("unchecked") |
| 61 |
@Test |
| 62 |
public void loopIterativeTest() throws IOException { |
| 63 |
IterativeDnsClient client = new IterativeDnsClient(new LruCache(0)); |
| 64 |
applyZones(client, |
| 65 |
rootZone( |
| 66 |
record("a", ns("a.ns")), |
| 67 |
record("b", ns("b.ns")), |
| 68 |
record("a.ns", a("1.1.1.1")), |
| 69 |
record("b.ns", a("1.1.1.2")) |
| 70 |
), zone("a", "a.ns", "1.1.1.1", |
| 71 |
record("test.a", ns("a.test.b")) |
| 72 |
), zone("b", "b.ns", "1.1.1.2", |
| 73 |
record("test.b", ns("b.test.a")) |
| 74 |
) |
| 75 |
); |
| 76 |
|
| 77 |
assertThrows(IterativeClientException.LoopDetected.class, () -> |
| 78 |
client.query("www.test.a", TYPE.A) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
@SuppressWarnings("unchecked") |
| 83 |
@Test |
| 84 |
public void notGluedNsTest() throws IOException { |
| 85 |
IterativeDnsClient client = new IterativeDnsClient(new LruCache(0)); |
| 86 |
applyZones(client, |
| 87 |
rootZone( |
| 88 |
record("com", ns("ns.com")), |
| 89 |
record("net", ns("ns.net")), |
| 90 |
record("ns.com", a("1.1.1.1")), |
| 91 |
record("ns.net", a("1.1.2.1")) |
| 92 |
), zone("com", "ns.com", "1.1.1.1", |
| 93 |
record("example.com", ns("example.ns.net")) |
| 94 |
), zone("net", "ns.net", "1.1.2.1", |
| 95 |
record("example.ns.net", a("1.1.2.2")) |
| 96 |
), zone("example.com", "example.ns.net", "1.1.2.2", |
| 97 |
record("www.example.com", a("1.1.1.3")) |
| 98 |
) |
| 99 |
); |
| 100 |
DnsQueryResult result = client.query("www.example.com", TYPE.A); |
| 101 |
DnsMessage message = result.response; |
| 102 |
List<Record<? extends Data>> answers = message.answerSection; |
| 103 |
assertEquals(1, answers.size()); |
| 104 |
assertEquals(TYPE.A, answers.get(0).type); |
| 105 |
assertArrayEquals(new byte[] {1, 1, 1, 3}, ((A) answers.get(0).payloadData).getIp()); |
| 106 |
} |
| 107 |
} |