From d89a02957a04130c41942d652c71424bbbc305e6 Mon Sep 17 00:00:00 2001 From: ericek111 Date: Fri, 25 Sep 2026 11:03:41 +0000 Subject: [PATCH] Test SRV lookup against stubbed records, not the live DNS The test resolved voice.teamspeak.com for real, so it failed whenever that name, or the network, did not answer. It now checks the ordering and parsing with a stubbed resolver. Co-Authored-By: Claude Opus 5.5 --- .../github/manevolent/ts3j/TS3DNSTest.java | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/test/java/com/github/manevolent/ts3j/TS3DNSTest.java b/src/test/java/com/github/manevolent/ts3j/TS3DNSTest.java index a34fbb6..fcb1205 100644 --- a/src/test/java/com/github/manevolent/ts3j/TS3DNSTest.java +++ b/src/test/java/com/github/manevolent/ts3j/TS3DNSTest.java @@ -1,27 +1,47 @@ package com.github.manevolent.ts3j; import com.github.manevolent.ts3j.protocol.TS3DNS; -import com.github.manevolent.ts3j.util.Ts3Debugging; import junit.framework.TestCase; import java.net.InetSocketAddress; -import java.net.SocketAddress; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +/** SRV answers are stubbed: the test must not depend on what the network resolves today. */ public class TS3DNSTest extends TestCase { - public static void main(String[] args) throws Exception { - new TS3DNSTest().testParser(); + @Override + protected void tearDown() { + TS3DNS.setSrvLookup(null); } - public void testParser() throws Exception { - Ts3Debugging.setEnabled(true); + public void testOrdersRecordsByPriorityThenWeight() throws Exception { + TS3DNS.setSrvLookup(name -> { + assertEquals("_ts3._udp.voice.example.com", name); + return Arrays.asList("20 0 9989 backup.example.com.", "10 5 9988 b.example.com.", + "10 1 9987 a.example.com."); + }); - List soaddr = TS3DNS.lookup("voice.teamspeak.com"); + List addresses = TS3DNS.lookup("voice.example.com"); - assertTrue(soaddr.size() > 0); + assertEquals(3, addresses.size()); + assertEquals("a.example.com", addresses.get(0).getHostString()); + assertEquals(9987, addresses.get(0).getPort()); + assertEquals("b.example.com", addresses.get(1).getHostString()); + assertEquals("backup.example.com", addresses.get(2).getHostString()); + } - for (SocketAddress socketAddress : soaddr) - Ts3Debugging.debug(socketAddress); + public void testNoRecordMeansTheHostIsNotFound() { + TS3DNS.setSrvLookup(name -> Collections.emptyList()); + try { + TS3DNS.lookup("voice.example.com"); + fail("expected UnknownHostException"); + } catch (UnknownHostException expected) { + // the caller then connects to the address itself + } catch (Exception e) { + fail("unexpected " + e); + } } }