Package net.windwards.dnsfrontend.frontend

Source Code of net.windwards.dnsfrontend.frontend.TestUDPServer

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();
    }
}
TOP

Related Classes of net.windwards.dnsfrontend.frontend.TestUDPServer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.