001: // Missing test cases:
002: // Failed lookup returns *correct* error
003: // MX record glue
004:
005: package CustomDNS.Tests;
006:
007: import java.lang.RuntimeException;
008: import java.net.*;
009: import java.util.Hashtable;
010:
011: import junit.framework.*;
012: import junit.extensions.*;
013:
014: import CustomDNS.Tests.CustomDNSTestCase;
015: import CustomDNS.VirtualDNS.VirtualDNS;
016:
017: // We need a good DNS client which can query our server effectively. Since
018: // we're already using Brian Wellington's code, we can just use his.
019: import org.xbill.DNS.*;
020:
021: public class TestVirtualDNS extends CustomDNSTestCase {
022:
023: // The port to run our test DNS servers on. This should be greater
024: // than 1024 (to prevent us from needing root privs under Unix).
025: static final short dnsPort = 5299;
026:
027: // Our standard constructor.
028: public TestVirtualDNS(String name) {
029: super (name);
030: }
031:
032: // An overridable method which returns the DNS port we should use.
033: protected short getDNSPort() {
034: return this .dnsPort;
035: }
036:
037: // Before running each test, create a new resolver (pointed at the
038: // right server, and with an empty cache).
039: public void setUp() throws Exception {
040: SimpleResolver resolver = new SimpleResolver("127.0.0.1");
041: resolver.setPort(getDNSPort());
042: dns.setResolver(resolver);
043: }
044:
045: // A local variety of test assertion--make sure that a given hostname
046: // returns a particular address when we look it up.
047: protected static void assertAddress(String host, String correctAddr) {
048: try {
049: InetAddress addr = Address.getByName(host);
050: assertEquals(addr.getHostAddress(), correctAddr);
051: } catch (UnknownHostException e) {
052: fail("Unknown host " + host + ", expected address "
053: + correctAddr);
054: }
055: }
056:
057: public void testAddress() {
058: assertAddress("sample.myzone.test", "10.0.1.2");
059: }
060:
061: public void testCNAME() {
062: assertAddress("sample2.myzone.test", "10.0.1.2");
063: }
064:
065: public void testUnknownHost() {
066: try {
067: Address.getByName("unknown.myzone.test");
068: fail("expected UnknownHostException");
069: } catch (UnknownHostException e) {
070: }
071: }
072:
073: public void testNS() {
074: // Fetch back a set of records.
075: Record[] records = dns.getAnyRecords("myzone.test", Type.NS);
076: assertEquals(records.length, 3);
077:
078: // Check to make sure the right names were present.
079: Hashtable names = new Hashtable();
080: for (int i = 0; i < records.length; i++) {
081: NSRecord nsr = (NSRecord) records[i];
082: names.put(nsr.getTarget().toString(), new Integer(0));
083: }
084: assert (names.get("dns1.otherzone.test.") != null);
085: assert (names.get("dns2.otherzone.test.") != null);
086: assert (names.get("dns3.otherzone.test.") != null);
087: }
088:
089: public void testUpperCaseQuery() {
090: assertAddress("SAMPLE2.MYZONE.TEST.", "10.0.1.2");
091: }
092:
093: // Return our test suite object.
094: public static Test suite() {
095: // Build our test suite.
096: TestSuite suite = new TestSuite(TestVirtualDNS.class);
097:
098: // Wrap it with a TestSetup object that boots our DNS server.
099: // (This only works once--we can't shut it down.)
100: final String zonefile = CustomDNSTestCase
101: .fullPath("test.master");
102: TestSetup wrapper = new TestSetup(suite) {
103: VirtualDNS mDNS = null;
104:
105: public void setUp() throws Exception {
106: if (mDNS == null) {
107: new VirtualDNS(zonefile, dnsPort);
108: // Wait for DNS server to load.
109: // XXX - We want real semaphores!
110: Thread.sleep(2000);
111: } else {
112: fail("Can only start DNS server once");
113: }
114: }
115: };
116: return wrapper;
117: }
118: }
|