Package net.windwards.dnsfrontend.frontend

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

package net.windwards.dnsfrontend.frontend;

import junit.framework.Assert;
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.Section;
import org.xbill.DNS.Type;

import java.net.DatagramPacket;
import java.net.InetAddress;


public class TestQuery {
    @Test
    public void explodeBeforeInterpretation() {
        byte[] qdata = new byte[10];
        DatagramPacket packet = new DatagramPacket(qdata, qdata.length);
        Query query = new UDPQuery(packet, null);
        try {
            query.getMessage();
            Assert.fail("Should fail before interpret()");
        } catch (IllegalStateException e) { }
    }

    @Test
    public void interpretUDPQuery() throws Exception {
        Record rec = Record.newRecord(Name.fromString("foo.example.com."), Type.A, DClass.IN);
        Message query = Message.newQuery(rec);

        byte[] qdata = query.toWire();
        DatagramPacket packet = new DatagramPacket(qdata, qdata.length,
                InetAddress.getLoopbackAddress(), 53);

        Query res = new UDPQuery(packet, null);
        res.interpret();

        Name name = res.getMessage().getQuestion().getName();
        Assert.assertEquals("foo.example.com.", name.toString());
        Assert.assertEquals("localhost", res.getRemote().getHostName());
    }
}
TOP

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

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.