001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.dnsserver;
019:
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
022: import org.apache.james.test.mock.avalon.MockLogger;
023: import org.xbill.DNS.Name;
024: import org.xbill.DNS.Record;
025: import org.xbill.DNS.Resolver;
026: import org.xbill.DNS.SetResponse;
027: import org.xbill.DNS.TextParseException;
028: import org.xbill.DNS.Zone;
029:
030: import java.io.ByteArrayInputStream;
031: import java.util.Collection;
032: import java.util.Iterator;
033:
034: import junit.framework.TestCase;
035:
036: public class DNSServerTest extends TestCase {
037:
038: private TestableDNSServer dnsServer;
039:
040: /**
041: * Please note that this is an hardcoded test that works because
042: * www.pippo.com. is an alias to pippo.com and pippo.com has
043: * "pippo.com.inbound.mxlogic.net." as its mx record.
044: * This is the first domain with a record proving a previous james bug.
045: * This test will be invalidated by any change in the pippo.com dns records
046: *
047: * @param args
048: * @throws Exception
049: */
050: public void testINARecords() throws Exception {
051: Zone z = new Zone(Name.fromString("pippo.com."), getClass()
052: .getResource("pippo-com.zone").getFile());
053: dnsServer.setResolver(null);
054: dnsServer.setLookupper(new ZoneLookupper(z));
055: Collection records = dnsServer.findMXRecords("www.pippo.com.");
056: assertEquals(1, records.size());
057: assertEquals("pippo.com.inbound.mxlogic.net.", records
058: .iterator().next());
059: }
060:
061: /**
062: * @throws Exception
063: */
064: public void testMXCatches() throws Exception {
065: Zone z = new Zone(Name.fromString("test-zone.com."), getClass()
066: .getResource("test-zone-com.zone").getFile());
067: dnsServer.setResolver(null);
068: dnsServer.setLookupper(new ZoneLookupper(z));
069: Collection res = dnsServer.findMXRecords("test-zone.com.");
070: try {
071: res.add(new Object());
072: fail("MX Collection should not be modifiable");
073: } catch (UnsupportedOperationException e) {
074: }
075: assertEquals(1, res.size());
076: assertEquals("mail.test-zone.com.", res.iterator().next());
077: }
078:
079: /**
080: * Please note that this is an hardcoded test that works because
081: * brandilyncollins.com. has an MX record that point to mxmail.register.com
082: * and this is a CNAME to the real address.
083: * This test will be invalidated by any change in the brandilyncollins.com dns records
084: *
085: * @param args
086: * @throws Exception
087: */
088: public void testCNAMEasMXrecords() throws Exception {
089: Zone z = new Zone(Name.fromString("brandilyncollins.com."),
090: getClass().getResource("brandilyncollins-com.zone")
091: .getFile());
092: dnsServer.setResolver(null);
093: dnsServer.setLookupper(new ZoneLookupper(z));
094: Iterator records = dnsServer
095: .getSMTPHostAddresses("brandilyncollins.com.");
096: assertEquals(true, records.hasNext());
097: }
098:
099: protected void setUp() throws Exception {
100: dnsServer = new TestableDNSServer();
101: DefaultConfigurationBuilder db = new DefaultConfigurationBuilder();
102:
103: Configuration c = db
104: .build(
105: new ByteArrayInputStream(
106: "<dnsserver><autodiscover>true</autodiscover><authoritative>false</authoritative></dnsserver>"
107: .getBytes()), "dnsserver");
108: dnsServer.enableLogging(new MockLogger());
109: dnsServer.configure(c);
110: dnsServer.initialize();
111: }
112:
113: protected void tearDown() throws Exception {
114: dnsServer.setLookupper(null);
115: dnsServer.dispose();
116: }
117:
118: private class ZoneLookupper implements Lookupper {
119: private final Zone z;
120:
121: private ZoneLookupper(Zone z) {
122: super ();
123: this .z = z;
124: }
125:
126: public SetResponse lookup(Name name, int type) {
127: SetResponse s = z.findRecords(name, type);
128: System.out.println("Zone Lookup: " + name + " " + type
129: + " = " + s);
130: return s;
131: }
132: }
133:
134: private interface Lookupper {
135: SetResponse lookup(Name name, int type);
136: }
137:
138: private final class TestableDNSServer extends DNSServer {
139:
140: private Lookupper lookupper;
141:
142: public void setLookupper(Lookupper l) {
143: this .lookupper = l;
144: }
145:
146: public Record[] lookup(String name, int type) {
147: if (lookupper != null) {
148: try {
149: SetResponse lookup = lookupper.lookup(Name
150: .fromString(name), type);
151: if (lookup != null && lookup.isSuccessful()) {
152: return processSetResponse(lookup);
153: } else {
154: return null;
155: }
156: } catch (TextParseException e) {
157: e.printStackTrace();
158: return null;
159: }
160: } else {
161: return super .lookup(name, type);
162: }
163: }
164:
165: public void setResolver(Resolver r) {
166: resolver = r;
167: }
168:
169: public Resolver getResolver() {
170: return resolver;
171: }
172: }
173:
174: }
|