Package net.windwards.dnsfrontend.frontend

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

package net.windwards.dnsfrontend.frontend;

import junit.framework.Assert;
import net.sf.ehcache.Ehcache;
import net.windwards.dnsfrontend.DNSFrontend;
import net.windwards.dnsfrontend.client.Client;
import net.windwards.dnsfrontend.dialog.Update;
import net.windwards.dnsfrontend.util.TestUtils;
import net.windwards.dnsfrontend.util.TestingConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xbill.DNS.AAAARecord;
import org.xbill.DNS.ARecord;
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.io.IOException;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;


public class TestIntegration {

    private DNSFrontend frontend;
    private TestingConfiguration conf;
    private Client client;

    @Before
    public void before() throws Exception {
        conf = new TestingConfiguration("conf", "example.com.", 1800);
        frontend = new DNSFrontend() {
            @Override
            protected void setupLogging() {
                this.debug = true;
                super.setupLogging();
            }
            @Override
            protected Ehcache attachCache() {
                return TestUtils.getEhcache();
            }
        };
        frontend.setup(conf);
        frontend.start();

        client = new Client();
        client.connect();
    }

    @After
    public void after() throws Exception {
        client.disconnect();
        frontend.stop();
    }

    @Test
    public void cacheHit() throws Exception {
        Update ipv4 = new Update();
        ipv4.name = "foo";
        ipv4.family = "ipv4";
        ipv4.moniker = "conf";
        ipv4.address = "1.2.3.4";
        client.send(ipv4);

        Update ipv6 = new Update();
        ipv6.name = "foo";
        ipv6.family = "ipv6";
        ipv6.moniker = "conf";
        ipv6.address = "2002:53fe:a000::1";
        client.send(ipv6);

        Thread.sleep(50);

        DatagramChannel channel = TestUtils.getDatagramChannel(conf.getPort());

        Message query =  Message.newQuery(
                Record.newRecord(new Name("foo.example.com."), Type.A, DClass.IN));
        channel.write(ByteBuffer.wrap(query.toWire()));

        Thread.sleep(10);

        ByteBuffer buf = ByteBuffer.allocate(100);
        channel.read(buf);

        Message message = new Message(buf.array());
        Record answer = message.getSectionArray(Section.ANSWER)[0];
        Assert.assertEquals(Type.A, answer.getType());
        String addr = ((ARecord)answer).getAddress().getHostAddress();
        Assert.assertEquals("1.2.3.4", addr);

        query =  Message.newQuery(
                Record.newRecord(new Name("foo.example.com."), Type.AAAA, DClass.IN));
        channel.write(ByteBuffer.wrap(query.toWire()));

        Thread.sleep(10);

        buf.clear();
        channel.read(buf);

        message = new Message(buf.array());
        answer = message.getSectionArray(Section.ANSWER)[0];
        Assert.assertEquals(Type.AAAA, answer.getType());
        addr = ((AAAARecord)answer).getAddress().getHostAddress();
        Assert.assertEquals("2002:53fe:a000:0:0:0:0:1", addr);
    }
}
TOP

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

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.