package net.windwards.dnsfrontend.frontend;
import junit.framework.Assert;
import net.windwards.dnsfrontend.util.TestingConfiguration;
import org.junit.Test;
import org.xbill.DNS.DClass;
import org.xbill.DNS.Message;
import org.xbill.DNS.Name;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class TestUDPServer {
@Test
public void receiveAQuery() throws Exception {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
ResolveTaskFactory factory = new ResolveTaskFactory(null, null, new TestingConfiguration("foo", "example.com.", 1800));
UDPServer server = new UDPServer(queue);
server.port = 5354;
server.setResolveTaskFactory(factory);
server.start();
Record rec = Record.newRecord(new Name("foo.example.com."), Type.A, DClass.IN);
Message query = Message.newQuery(rec);
byte[] qdata = query.toWire();
DatagramPacket packet = new DatagramPacket(qdata, qdata.length, InetAddress.getLocalHost(), 5354);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
Runnable res = queue.poll(50, TimeUnit.MILLISECONDS);
Assert.assertTrue(res instanceof ResolveTask);
server.shutdown();
}
}