private void sendQuery() { Header queryHeader = new Header(); // If we set the RA (Recursion Available) flag and our message ID to 0 // then the packet matches the real mDNS query packet as displayed in Wireshark queryHeader.setFlag(org.xbill.DNS.Flags.RA); queryHeader.setID(0); Record question = null; try { // We need to create our "Question" DNS query that is a pointer record to // the mDNS Query "Name" question = Record.newRecord(new Name(NvmDNS.MDNS_QUERY), Type.PTR, DClass.IN); } catch (TextParseException e) { Log.e("NvmDNS Query", e.getMessage()); return; } // We combine our header and our question into a single message Message query = new Message(); query.setHeader(queryHeader); query.addRecord(question, Section.QUESTION); // Convert the message into Network Byte Order byte[] wireQuery = query.toWire(); Log.i("NvmDNS Query", query.toString()); // Convert our byte array into a Packet DatagramPacket transmitPacket = new DatagramPacket(wireQuery, wireQuery.length); transmitPacket.setAddress(NvmDNS.MDNS_MULTICAST_ADDRESS); transmitPacket.setPort(NvmDNS.MDNS_PORT); // And (attempt) to send the packet try { Log.d("NvmDNS Query", "Blocking on this.nvstream_socket.send(transmitPacket)"); this.socket.send(transmitPacket); Log.d("NvmDNS Query", "Passed this.nvstream_socket.send(transmitPacket)"); } catch (IOException e) { Log.e("NvmDNS Query", "There was an error sending the DNS query."); Log.e("NvmDNS Query", e.getMessage()); } }
@Before public void before() throws Exception { // force the xn field in the request Random random = mock(Random.class); Mockito.when(random.nextInt(0xffff)).thenReturn(65535); whenNew(Random.class).withNoArguments().thenReturn(random); mockStatic(System.class); when(System.currentTimeMillis()).thenReturn(144140678000L).thenReturn(144140678345L); when(System.nanoTime()).thenReturn(100000000L, 100000000L + 345123000L); mockStatic(Logger.class); when(Logger.getLogger("com.comcast.cdn.traffic_control.traffic_router.core.access")) .thenReturn(accessLogger); header = new Header(); header.setID(65535); header.setFlag(Flags.QR); client = Inet4Address.getByAddress(new byte[] {(byte) 192, (byte) 168, 23, 45}); nameServer = mock(NameServer.class); }