MDNSDiscover { static Result decode(byte[] packet, int packetLength) throws IOException { Result result = new Result(); decode(packet, packetLength, result); return result; }  static void discover(String serviceType, Callback callback, int timeout); static Result resolve(String serviceName, int timeout);  }
@Test public void testReplyPacket() throws IOException { byte[] packet = createReplyPacket(); Result r = decode(packet, packet.length); assertEquals("192.168.1.100", r.a.ipaddr); assertEquals("dev0123456789.local", r.a.fqdn); assertEquals(10, r.a.ttl); assertEquals(0, r.srv.priority); assertEquals(0, r.srv.weight); assertEquals(1234, r.srv.port); assertEquals("dev0123456789.local", r.srv.target); assertEquals("device-1234._example._tcp.local", r.srv.fqdn); assertEquals(10, r.srv.ttl); Map<String, String> expectedTxt = new HashMap<>(); expectedTxt.put("foo", "bar"); expectedTxt.put("bar", null); expectedTxt.put("txtvers", "1"); assertEquals(expectedTxt, r.txt.dict); assertEquals("device-1234._example._tcp.local", r.txt.fqdn); assertEquals(10, r.txt.ttl); }
@Test public void testTruncatedReplyPacketsWithResizedArray() { byte[] packet = createReplyPacket(); for (int truncatedLength = 0; truncatedLength < packet.length; truncatedLength++) { byte[] truncatedPacket = Arrays.copyOf(packet, truncatedLength); try { decode(truncatedPacket, truncatedPacket.length); fail("decoding a truncated packet did not throw IOException"); } catch (IOException e) { } } }
@Test public void testTruncatedReplyPacketsWithOriginalArray() { byte[] packet = createReplyPacket(); for (int truncatedLength = 0; truncatedLength < packet.length; truncatedLength++) { try { decode(packet, truncatedLength); fail("decoding a truncated packet did not throw IOException"); } catch (IOException e) { } } }
@Test(expected=EOFException.class) public void testAbortOnPointerOutOfRange() throws IOException { byte[] packet = new ByteBuilder() .hex("0000 8400") .hex("0000") .hex("0001") .hex("0000") .hex("0000") .hex("04").ascii("TEST") .hex("ffff") .hex("0001 0001") .hex("0000000a 0004") .hex("c0 a8 01 64") .build(); decode(packet, packet.length); }
@Test(expected=IOException.class) public void testAbortOnInfiniteDomainName() throws IOException { byte[] packet = new ByteBuilder() .hex("0000 8400") .hex("0000") .hex("0001") .hex("0000") .hex("0000") .hex("04").ascii("TEST") .hex("c00c") .hex("0001 0001") .hex("0000000a 0004") .hex("c0 a8 01 64") .build(); decode(packet, packet.length); }
@Test(expected=IOException.class) public void testAbortOnCyclicEmptyDomainName() throws IOException { byte[] packet = new ByteBuilder() .hex("0000 8400") .hex("0000") .hex("0001") .hex("0000") .hex("0000") .hex("04").ascii("TEST") .hex("c013") .hex("c011") .hex("0001 0001") .hex("0000000a 0004") .hex("c0 a8 01 64") .build(); decode(packet, packet.length); }
@Test public void testDecodeRandomPackets() { final int SEED = 0x5FC45975; final int NUM_PACKETS = 10000; final int MAX_PACKET_LENGTH = 1536; Random random = new Random(SEED); for (int i = 0; i < NUM_PACKETS; i++) { int packetLength = random.nextInt(MAX_PACKET_LENGTH+1); byte[] packet = new byte[packetLength]; random.nextBytes(packet); try { decode(packet, packetLength); fail("decoding a random packet did not throw IOException"); } catch (IOException e) { } } }