001: // -*- Java -*-
002: //
003: // Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
004: // Copyright (c) 2005, University of Colorado at Boulder
005: // All rights reserved.
006: //
007: // Redistribution and use in source and binary forms, with or without
008: // modification, are permitted provided that the following conditions are
009: // met:
010: //
011: // * Redistributions of source code must retain the above copyright
012: // notice, this list of conditions and the following disclaimer.
013: //
014: // * Redistributions in binary form must reproduce the above copyright
015: // notice, this list of conditions and the following disclaimer in the
016: // documentation and/or other materials provided with the distribution.
017: //
018: // * Neither the name of the University of Colorado at Boulder nor the
019: // names of its contributors may be used to endorse or promote
020: // products derived from this software without specific prior written
021: // permission.
022: //
023: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034: //
035: package org.xbill.DNS;
036:
037: import java.net.InetAddress;
038: import java.net.UnknownHostException;
039: import java.util.Date;
040: import java.util.Iterator;
041: import junit.framework.TestCase;
042: import org.xbill.DNS.ARecord;
043: import org.xbill.DNS.CNAMERecord;
044: import org.xbill.DNS.DClass;
045: import org.xbill.DNS.Name;
046: import org.xbill.DNS.RRset;
047: import org.xbill.DNS.Record;
048: import org.xbill.DNS.RRSIGRecord;
049: import org.xbill.DNS.TextParseException;
050: import org.xbill.DNS.Type;
051:
052: public class RRsetTest extends TestCase {
053: private RRset m_rs;
054: Name m_name, m_name2;
055: long m_ttl;
056: ARecord m_a1, m_a2;
057: RRSIGRecord m_s1, m_s2;
058:
059: public void setUp() throws TextParseException, UnknownHostException {
060: m_rs = new RRset();
061: m_name = Name.fromString("this.is.a.test.");
062: m_name2 = Name.fromString("this.is.another.test.");
063: m_ttl = 0xABCDL;
064: m_a1 = new ARecord(m_name, DClass.IN, m_ttl, InetAddress
065: .getByName("192.169.232.11"));
066: m_a2 = new ARecord(m_name, DClass.IN, m_ttl + 1, InetAddress
067: .getByName("192.169.232.12"));
068:
069: m_s1 = new RRSIGRecord(m_name, DClass.IN, m_ttl, Type.A, 0xF,
070: 0xABCDEL, new Date(), new Date(), 0xA, m_name,
071: new byte[0]);
072: m_s2 = new RRSIGRecord(m_name, DClass.IN, m_ttl, Type.A, 0xF,
073: 0xABCDEL, new Date(), new Date(), 0xA, m_name2,
074: new byte[0]);
075: }
076:
077: public void test_ctor_0arg() {
078: assertEquals(0, m_rs.size());
079: try {
080: m_rs.getDClass();
081: fail("IllegalStateException not thrown");
082: } catch (IllegalStateException e) {
083: }
084: try {
085: m_rs.getType();
086: fail("IllegalStateException not thrown");
087: } catch (IllegalStateException e) {
088: }
089: try {
090: m_rs.getTTL();
091: fail("IllegalStateException not thrown");
092: } catch (IllegalStateException e) {
093: }
094: try {
095: m_rs.getName();
096: fail("IllegalStateException not thrown");
097: } catch (IllegalStateException e) {
098: }
099: try {
100: m_rs.first();
101: fail("IllegalStateException not thrown");
102: } catch (IllegalStateException e) {
103: }
104:
105: try {
106: m_rs.toString();
107: fail("IllegalStateException not thrown");
108: } catch (IllegalStateException e) {
109: }
110:
111: Iterator itr = m_rs.rrs();
112: assertNotNull(itr);
113: assertFalse(itr.hasNext());
114:
115: itr = m_rs.sigs();
116: assertNotNull(itr);
117: assertFalse(itr.hasNext());
118: }
119:
120: public void test_basics() throws TextParseException,
121: UnknownHostException {
122: m_rs.addRR(m_a1);
123:
124: assertEquals(1, m_rs.size());
125: assertEquals(DClass.IN, m_rs.getDClass());
126: assertEquals(m_a1, m_rs.first());
127: assertEquals(m_name, m_rs.getName());
128: assertEquals(m_ttl, m_rs.getTTL());
129: assertEquals(Type.A, m_rs.getType());
130:
131: // add it again, and make sure nothing changed
132: m_rs.addRR(m_a1);
133:
134: assertEquals(1, m_rs.size());
135: assertEquals(DClass.IN, m_rs.getDClass());
136: assertEquals(m_a1, m_rs.first());
137: assertEquals(m_name, m_rs.getName());
138: assertEquals(m_ttl, m_rs.getTTL());
139: assertEquals(Type.A, m_rs.getType());
140:
141: m_rs.addRR(m_a2);
142:
143: assertEquals(2, m_rs.size());
144: assertEquals(DClass.IN, m_rs.getDClass());
145: Record r = m_rs.first();
146: assertEquals(m_a1, r);
147: assertEquals(m_name, m_rs.getName());
148: assertEquals(m_ttl, m_rs.getTTL());
149: assertEquals(Type.A, m_rs.getType());
150:
151: Iterator itr = m_rs.rrs();
152: assertEquals(m_a1, itr.next());
153: assertEquals(m_a2, itr.next());
154:
155: // make sure that it rotates
156: itr = m_rs.rrs();
157: assertEquals(m_a2, itr.next());
158: assertEquals(m_a1, itr.next());
159: itr = m_rs.rrs();
160: assertEquals(m_a1, itr.next());
161: assertEquals(m_a2, itr.next());
162:
163: m_rs.deleteRR(m_a1);
164: assertEquals(1, m_rs.size());
165: assertEquals(DClass.IN, m_rs.getDClass());
166: assertEquals(m_a2, m_rs.first());
167: assertEquals(m_name, m_rs.getName());
168: assertEquals(m_ttl, m_rs.getTTL());
169: assertEquals(Type.A, m_rs.getType());
170:
171: // the signature records
172: m_rs.addRR(m_s1);
173: assertEquals(1, m_rs.size());
174: itr = m_rs.sigs();
175: assertEquals(m_s1, itr.next());
176: assertFalse(itr.hasNext());
177:
178: m_rs.addRR(m_s1);
179: itr = m_rs.sigs();
180: assertEquals(m_s1, itr.next());
181: assertFalse(itr.hasNext());
182:
183: m_rs.addRR(m_s2);
184: itr = m_rs.sigs();
185: assertEquals(m_s1, itr.next());
186: assertEquals(m_s2, itr.next());
187: assertFalse(itr.hasNext());
188:
189: m_rs.deleteRR(m_s1);
190: itr = m_rs.sigs();
191: assertEquals(m_s2, itr.next());
192: assertFalse(itr.hasNext());
193:
194: // clear it all
195: m_rs.clear();
196: assertEquals(0, m_rs.size());
197: assertFalse(m_rs.rrs().hasNext());
198: assertFalse(m_rs.sigs().hasNext());
199:
200: }
201:
202: public void test_ctor_1arg() {
203: m_rs.addRR(m_a1);
204: m_rs.addRR(m_a2);
205: m_rs.addRR(m_s1);
206: m_rs.addRR(m_s2);
207:
208: RRset rs2 = new RRset(m_rs);
209:
210: assertEquals(2, rs2.size());
211: assertEquals(m_a1, rs2.first());
212: Iterator itr = rs2.rrs();
213: assertEquals(m_a1, itr.next());
214: assertEquals(m_a2, itr.next());
215: assertFalse(itr.hasNext());
216:
217: itr = rs2.sigs();
218: assertTrue(itr.hasNext());
219: assertEquals(m_s1, itr.next());
220: assertTrue(itr.hasNext());
221: assertEquals(m_s2, itr.next());
222: assertFalse(itr.hasNext());
223: }
224:
225: public void test_toString() {
226: m_rs.addRR(m_a1);
227: m_rs.addRR(m_a2);
228: m_rs.addRR(m_s1);
229: m_rs.addRR(m_s2);
230:
231: String out = m_rs.toString();
232:
233: assertTrue(out.indexOf(m_name.toString()) != -1);
234: assertTrue(out.indexOf(" IN A ") != -1);
235: assertTrue(out.indexOf("[192.169.232.11]") != -1);
236: assertTrue(out.indexOf("[192.169.232.12]") != -1);
237: }
238:
239: public void test_addRR_invalidType() throws TextParseException {
240: m_rs.addRR(m_a1);
241:
242: CNAMERecord c = new CNAMERecord(m_name, DClass.IN, m_ttl, Name
243: .fromString("an.alias."));
244:
245: try {
246: m_rs.addRR(c);
247: fail("IllegalArgumentException not thrown");
248: } catch (IllegalArgumentException e) {
249: }
250: }
251:
252: public void test_addRR_invalidName() throws TextParseException,
253: UnknownHostException {
254: m_rs.addRR(m_a1);
255:
256: m_a2 = new ARecord(m_name2, DClass.IN, m_ttl, InetAddress
257: .getByName("192.169.232.11"));
258:
259: try {
260: m_rs.addRR(m_a2);
261: fail("IllegalArgumentException not thrown");
262: } catch (IllegalArgumentException e) {
263: }
264: }
265:
266: public void test_addRR_invalidDClass() throws TextParseException,
267: UnknownHostException {
268: m_rs.addRR(m_a1);
269:
270: m_a2 = new ARecord(m_name, DClass.CHAOS, m_ttl, InetAddress
271: .getByName("192.169.232.11"));
272:
273: try {
274: m_rs.addRR(m_a2);
275: fail("IllegalArgumentException not thrown");
276: } catch (IllegalArgumentException e) {
277: }
278: }
279:
280: public void test_TTLcalculation() {
281: m_rs.addRR(m_a2);
282: assertEquals(m_a2.getTTL(), m_rs.getTTL());
283: m_rs.addRR(m_a1);
284: assertEquals(m_a1.getTTL(), m_rs.getTTL());
285:
286: Iterator itr = m_rs.rrs();
287: while (itr.hasNext()) {
288: Record r = (Record) itr.next();
289: assertEquals(m_a1.getTTL(), r.getTTL());
290: }
291: }
292:
293: public void test_Record_placement() {
294: m_rs.addRR(m_a1);
295: m_rs.addRR(m_s1);
296: m_rs.addRR(m_a2);
297:
298: Iterator itr = m_rs.rrs();
299: assertTrue(itr.hasNext());
300: assertEquals(m_a1, itr.next());
301: assertTrue(itr.hasNext());
302: assertEquals(m_a2, itr.next());
303: assertFalse(itr.hasNext());
304:
305: itr = m_rs.sigs();
306: assertTrue(itr.hasNext());
307: assertEquals(m_s1, itr.next());
308: assertFalse(itr.hasNext());
309: }
310:
311: public void test_noncycling_iterator() {
312: m_rs.addRR(m_a1);
313: m_rs.addRR(m_a2);
314:
315: Iterator itr = m_rs.rrs(false);
316: assertTrue(itr.hasNext());
317: assertEquals(m_a1, itr.next());
318: assertTrue(itr.hasNext());
319: assertEquals(m_a2, itr.next());
320:
321: itr = m_rs.rrs(false);
322: assertTrue(itr.hasNext());
323: assertEquals(m_a1, itr.next());
324: assertTrue(itr.hasNext());
325: assertEquals(m_a2, itr.next());
326: }
327: }
|