package net.windwards.dnsfrontend.backend;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.Assert;
import net.windwards.dnsfrontend.api.Protocol;
import net.windwards.dnsfrontend.util.TestingConfiguration;
import org.junit.Ignore;
import org.junit.Test;
import org.xbill.DNS.AAAARecord;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.DClass;
import org.xbill.DNS.Name;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;
import java.util.HashMap;
import java.util.Map;
public class TestJSONBackendProtocol {
public static JsonFactory factory = new JsonFactory();
public static ObjectMapper mapper = new ObjectMapper(factory);
@Test
public void encodeRequest() throws Exception {
TestingConfiguration conf = new TestingConfiguration("bar", "example.com.", 12345);
Protocol protocol = new JSONBackendProtocol();
Record rec = Record.newRecord(new Name("foo.example.com."), Type.A, DClass.IN);
byte[] data = protocol.encode(rec, conf);
String expectedStr = "{ \"type\": \"Request\", \"moniker\": \"bar\", \"name\": \"foo\" }";
Map<String, Object> expected = mapper.readValue(expectedStr, new TypeReference<HashMap<String, Object>>(){});
Map<String, Object> actual = mapper.readValue(data, new TypeReference<HashMap<String, Object>>(){});
Assert.assertEquals(expected, actual);
}
@Test
public void decodeIPv4Update() throws Exception {
TestingConfiguration conf = new TestingConfiguration("bar", "example.com.", 12345);
String update = "{ " +
"\"type\": \"Update\", " +
"\"name\": \"foo\", " +
"\"address\": \"192.168.0.1\", " +
"\"family\": \"ipv4\", " +
"\"moniker\": \"bar\" }";
Protocol protocol = new JSONBackendProtocol();
Record result = protocol.decode(update.getBytes(), conf);
ARecord rec = (ARecord) result;
Assert.assertEquals("foo.example.com.", rec.getName().toString());
Assert.assertEquals("192.168.0.1", rec.getAddress().getHostAddress());
Assert.assertEquals(12345, rec.getTTL());
}
@Test
public void decodeIPv6Update() throws Exception {
TestingConfiguration conf = new TestingConfiguration("bar", "example.com.", 12345);
String update = "{" +
"\"type\": \"Update\", " +
"\"name\": \"foo\", " +
"\"address\": \"2002:53fe:52a1:7:224:1dff:fe7d:6bef\", " +
"\"family\": \"ipv6\", " +
"\"moniker\": \"bar\" }";
Protocol protocol = new JSONBackendProtocol();
Record result = protocol.decode(update.getBytes(), conf);
AAAARecord rec = (AAAARecord) result;
Assert.assertEquals("foo.example.com.", rec.getName().toString());
Assert.assertEquals("2002:53fe:52a1:7:224:1dff:fe7d:6bef", rec.getAddress().getHostAddress());
Assert.assertEquals(12345, rec.getTTL());
}
@Ignore("Meaningful only when run alone")
@Test
public void firstSerialisationPerformance() throws Exception {
TestingConfiguration conf = new TestingConfiguration("bar", "example.com.", 12345);
Protocol protocol = new JSONBackendProtocol();
Record rec = Record.newRecord(new Name("foo.example.com."), Type.A, DClass.IN);
long start = System.currentTimeMillis();
byte[] data = protocol.encode(rec, conf);
long consumed = System.currentTimeMillis() - start;
System.out.println(consumed);
Assert.assertTrue(consumed < 25);
}
}