View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  package org.apache.james.dnsserver;
20  
21  import org.apache.avalon.framework.configuration.Configuration;
22  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
23  import org.apache.james.test.mock.avalon.MockLogger;
24  import org.xbill.DNS.Name;
25  import org.xbill.DNS.Record;
26  import org.xbill.DNS.Resolver;
27  import org.xbill.DNS.SetResponse;
28  import org.xbill.DNS.TextParseException;
29  import org.xbill.DNS.Zone;
30  
31  import java.io.ByteArrayInputStream;
32  import java.util.Collection;
33  import java.util.Iterator;
34  
35  import junit.framework.TestCase;
36  
37  public class DNSServerTest extends TestCase {
38  
39      private TestableDNSServer dnsServer;
40  
41      /***
42       * Please note that this is an hardcoded test that works because
43       * www.pippo.com. is an alias to pippo.com and pippo.com has
44       * "pippo.com.inbound.mxlogic.net." as its mx record.
45       * This is the first domain with a record proving a previous james bug.
46       * This test will be invalidated by any change in the pippo.com dns records
47       * 
48       * @param args
49       * @throws Exception
50       */
51      public void testINARecords() throws Exception {
52          Zone z = new Zone(Name.fromString("pippo.com."),getClass().getResource("pippo-com.zone").getFile());
53          dnsServer.setResolver(null);
54          dnsServer.setLookupper(new ZoneLookupper(z));
55          Collection records = dnsServer.findMXRecords("www.pippo.com.");
56          assertEquals(1, records.size());
57          assertEquals("pippo.com.inbound.mxlogic.net.", records.iterator()
58                  .next());
59      }
60  
61      /***
62       * @throws Exception
63       */
64      public void testMXCatches() throws Exception {
65          Zone z = new Zone(Name.fromString("test-zone.com."),getClass().getResource("test-zone-com.zone").getFile());
66          dnsServer.setResolver(null);
67          dnsServer.setLookupper(new ZoneLookupper(z));
68          Collection res = dnsServer.findMXRecords("test-zone.com.");
69          try {
70              res.add(new Object());
71              fail("MX Collection should not be modifiable");
72          } catch (UnsupportedOperationException e) {
73          }
74          assertEquals(1,res.size());
75          assertEquals("mail.test-zone.com.",res.iterator().next());
76      }
77      
78      /***
79       * Please note that this is an hardcoded test that works because
80       * brandilyncollins.com. has an MX record that point to mxmail.register.com
81       * and this is a CNAME to the real address.
82       * This test will be invalidated by any change in the brandilyncollins.com dns records
83       * 
84       * @param args
85       * @throws Exception
86       */
87      public void testCNAMEasMXrecords() throws Exception {
88          Zone z = new Zone(Name.fromString("brandilyncollins.com."),getClass().getResource("brandilyncollins-com.zone").getFile());
89          dnsServer.setResolver(null);
90          dnsServer.setLookupper(new ZoneLookupper(z));
91          Iterator records = dnsServer.getSMTPHostAddresses("brandilyncollins.com.");
92          assertEquals(true, records.hasNext());
93      }
94  
95      protected void setUp() throws Exception {
96          dnsServer = new TestableDNSServer();
97          DefaultConfigurationBuilder db = new DefaultConfigurationBuilder();
98  
99          Configuration c = db.build(
100                 new ByteArrayInputStream("<dnsserver><autodiscover>true</autodiscover><authoritative>false</authoritative></dnsserver>".getBytes()),
101                 "dnsserver");
102         dnsServer.enableLogging(new MockLogger());
103         dnsServer.configure(c);
104         dnsServer.initialize();
105     }
106 
107     protected void tearDown() throws Exception {
108         dnsServer.setLookupper(null);
109         dnsServer.dispose();
110     }
111 
112     private class ZoneLookupper implements Lookupper {
113         private final Zone z;
114 
115         private ZoneLookupper(Zone z) {
116             super();
117             this.z = z;
118         }
119 
120         public SetResponse lookup(Name name, int type) {
121             SetResponse s = z.findRecords(name,type);
122             System.out.println("Zone Lookup: "+name+" "+type+" = "+s);
123             return s; 
124         }
125     }
126 
127     private interface Lookupper {
128         SetResponse lookup(Name name, int type);
129     }
130     
131     private final class TestableDNSServer extends DNSServer {
132         
133         private Lookupper lookupper;
134 
135         public void setLookupper(Lookupper l) {
136             this.lookupper = l;
137         }
138         
139         public Record[] lookup(String name, int type) {
140             if (lookupper != null) {
141                 try {
142                     SetResponse lookup = lookupper.lookup(Name.fromString(name), type);
143                     if (lookup != null && lookup.isSuccessful()) {
144                         return processSetResponse(lookup);
145                     } else {
146                         return null;
147                     }
148                 } catch (TextParseException e) {
149                     e.printStackTrace();
150                     return null;
151                 }
152             } else {
153                 return super.lookup(name, type);
154             }
155         }
156 
157         public void setResolver(Resolver r) {
158             resolver = r;
159         }
160 
161         public Resolver getResolver() {
162             return resolver;
163         }
164     }
165 
166 }