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.io.IOException;
038: import junit.framework.TestCase;
039: import org.xbill.DNS.DNSInput;
040: import org.xbill.DNS.DNSOutput;
041: import org.xbill.DNS.Header;
042: import org.xbill.DNS.Opcode;
043: import org.xbill.DNS.Rcode;
044: import org.xbill.DNS.Flags;
045:
046: public class HeaderTest extends TestCase {
047: private Header m_h;
048:
049: public void setUp() {
050: m_h = new Header(0xABCD); // 43981
051: }
052:
053: public void test_fixture_state() {
054: assertEquals(0xABCD, m_h.getID());
055:
056: boolean[] flags = m_h.getFlags();
057: for (int i = 0; i < flags.length; ++i) {
058: assertFalse(flags[i]);
059: }
060: assertEquals(0, m_h.getRcode());
061: assertEquals(0, m_h.getOpcode());
062: assertEquals(0, m_h.getCount(0));
063: assertEquals(0, m_h.getCount(1));
064: assertEquals(0, m_h.getCount(2));
065: assertEquals(0, m_h.getCount(3));
066: }
067:
068: public void test_ctor_0arg() {
069: m_h = new Header();
070: assertTrue(0 <= m_h.getID() && m_h.getID() < 0xFFFF);
071:
072: boolean[] flags = m_h.getFlags();
073: for (int i = 0; i < flags.length; ++i) {
074: assertFalse(flags[i]);
075: }
076: assertEquals(0, m_h.getRcode());
077: assertEquals(0, m_h.getOpcode());
078: assertEquals(0, m_h.getCount(0));
079: assertEquals(0, m_h.getCount(1));
080: assertEquals(0, m_h.getCount(2));
081: assertEquals(0, m_h.getCount(3));
082: }
083:
084: public void test_ctor_DNSInput() throws IOException {
085: byte[] raw = new byte[] { (byte) 0x12, (byte) 0xAB, // ID
086: (byte) 0x8F, (byte) 0xBD, // flags: 1 0001 1 1 1 1 011 1101
087: (byte) 0x65, (byte) 0x1C, // QDCOUNT
088: (byte) 0x10, (byte) 0xF0, // ANCOUNT
089: (byte) 0x98, (byte) 0xBA, // NSCOUNT
090: (byte) 0x71, (byte) 0x90 }; // ARCOUNT
091:
092: m_h = new Header(new DNSInput(raw));
093:
094: assertEquals(0x12AB, m_h.getID());
095:
096: boolean[] flags = m_h.getFlags();
097:
098: assertTrue(flags[0]);
099:
100: assertEquals(1, m_h.getOpcode());
101:
102: assertTrue(flags[5]);
103:
104: assertTrue(flags[6]);
105:
106: assertTrue(flags[7]);
107:
108: assertTrue(flags[8]);
109:
110: assertFalse(flags[9]);
111: assertTrue(flags[10]);
112: assertTrue(flags[11]);
113:
114: assertEquals(0xD, m_h.getRcode());
115:
116: assertEquals(0x651C, m_h.getCount(0));
117: assertEquals(0x10F0, m_h.getCount(1));
118: assertEquals(0x98BA, m_h.getCount(2));
119: assertEquals(0x7190, m_h.getCount(3));
120: }
121:
122: public void test_toWire() throws IOException {
123: byte[] raw = new byte[] { (byte) 0x12, (byte) 0xAB, // ID
124: (byte) 0x8F, (byte) 0xBD, // flags: 1 0001 1 1 1 1 011 1101
125: (byte) 0x65, (byte) 0x1C, // QDCOUNT
126: (byte) 0x10, (byte) 0xF0, // ANCOUNT
127: (byte) 0x98, (byte) 0xBA, // NSCOUNT
128: (byte) 0x71, (byte) 0x90 }; // ARCOUNT
129:
130: m_h = new Header(raw);
131:
132: DNSOutput dout = new DNSOutput();
133: m_h.toWire(dout);
134:
135: byte[] out = dout.toByteArray();
136:
137: assertEquals(12, out.length);
138: for (int i = 0; i < out.length; ++i) {
139: assertEquals(raw[i], out[i]);
140: }
141:
142: m_h.setOpcode(0xA); // 1010
143: assertEquals(0xA, m_h.getOpcode());
144: m_h.setRcode(0x7); // 0111
145:
146: // flags is now: 1101 0111 1011 0111
147:
148: raw[2] = (byte) 0xD7;
149: raw[3] = (byte) 0xB7;
150:
151: out = m_h.toWire();
152:
153: assertEquals(12, out.length);
154: for (int i = 0; i < out.length; ++i) {
155: assertEquals("i=" + i, raw[i], out[i]);
156: }
157: }
158:
159: public void test_flags() {
160: m_h.setFlag(0);
161: m_h.setFlag(5);
162: assertTrue(m_h.getFlag(0));
163: assertTrue(m_h.getFlags()[0]);
164: assertTrue(m_h.getFlag(5));
165: assertTrue(m_h.getFlags()[5]);
166:
167: m_h.unsetFlag(0);
168: assertFalse(m_h.getFlag(0));
169: assertFalse(m_h.getFlags()[0]);
170: assertTrue(m_h.getFlag(5));
171: assertTrue(m_h.getFlags()[5]);
172:
173: m_h.unsetFlag(5);
174: assertFalse(m_h.getFlag(0));
175: assertFalse(m_h.getFlags()[0]);
176: assertFalse(m_h.getFlag(5));
177: assertFalse(m_h.getFlags()[5]);
178:
179: boolean[] flags = m_h.getFlags();
180: for (int i = 0; i < flags.length; ++i) {
181: if ((i > 0 && i < 5) || i > 11) {
182: continue;
183: }
184: assertFalse(flags[i]);
185: }
186: }
187:
188: public void test_flags_invalid() {
189: try {
190: m_h.setFlag(-1);
191: fail("IllegalArgumentException not thrown");
192: } catch (IllegalArgumentException e) {
193: }
194: try {
195: m_h.setFlag(1);
196: fail("IllegalArgumentException not thrown");
197: } catch (IllegalArgumentException e) {
198: }
199: try {
200: m_h.setFlag(16);
201: fail("IllegalArgumentException not thrown");
202: } catch (IllegalArgumentException e) {
203: }
204: try {
205: m_h.unsetFlag(-1);
206: fail("IllegalArgumentException not thrown");
207: } catch (IllegalArgumentException e) {
208: }
209: try {
210: m_h.unsetFlag(13);
211: fail("IllegalArgumentException not thrown");
212: } catch (IllegalArgumentException e) {
213: }
214: try {
215: m_h.unsetFlag(16);
216: fail("IllegalArgumentException not thrown");
217: } catch (IllegalArgumentException e) {
218: }
219: try {
220: m_h.getFlag(-1);
221: fail("IllegalArgumentException not thrown");
222: } catch (IllegalArgumentException e) {
223: }
224: try {
225: m_h.getFlag(4);
226: fail("IllegalArgumentException not thrown");
227: } catch (IllegalArgumentException e) {
228: }
229: try {
230: m_h.getFlag(16);
231: fail("IllegalArgumentException not thrown");
232: } catch (IllegalArgumentException e) {
233: }
234: }
235:
236: public void test_ID() {
237: assertEquals(0xABCD, m_h.getID());
238:
239: m_h = new Header();
240:
241: int id = m_h.getID();
242: assertEquals(id, m_h.getID());
243: assertTrue(id >= 0 && id < 0xffff);
244:
245: m_h.setID(0xDCBA);
246: assertEquals(0xDCBA, m_h.getID());
247: }
248:
249: public void test_setID_invalid() {
250: try {
251: m_h.setID(0x10000);
252: fail("IllegalArgumentException not thrown");
253: } catch (IllegalArgumentException e) {
254: }
255: try {
256: m_h.setID(-1);
257: fail("IllegalArgumentException not thrown");
258: } catch (IllegalArgumentException e) {
259: }
260: }
261:
262: public void test_Rcode() {
263: assertEquals(0, m_h.getRcode());
264:
265: m_h.setRcode(0xA); // 1010
266: assertEquals(0xA, m_h.getRcode());
267: for (int i = 0; i < 12; ++i) {
268: if ((i > 0 && i < 5) || i > 11) {
269: continue;
270: }
271: assertFalse(m_h.getFlag(i));
272: }
273: }
274:
275: public void test_setRcode_invalid() {
276: try {
277: m_h.setRcode(-1);
278: fail("IllegalArgumentException not thrown");
279: } catch (IllegalArgumentException e) {
280: }
281: try {
282: m_h.setRcode(0x100);
283: fail("IllegalArgumentException not thrown");
284: } catch (IllegalArgumentException e) {
285: }
286: }
287:
288: public void test_Opcode() {
289: assertEquals(0, m_h.getOpcode());
290:
291: m_h.setOpcode(0xE); // 1110
292: assertEquals(0xE, m_h.getOpcode());
293:
294: assertFalse(m_h.getFlag(0));
295: for (int i = 5; i < 12; ++i) {
296: assertFalse(m_h.getFlag(i));
297: }
298: assertEquals(0, m_h.getRcode());
299: }
300:
301: public void test_setOpcode_invalid() {
302: try {
303: m_h.setOpcode(-1);
304: fail("IllegalArgumentException not thrown");
305: } catch (IllegalArgumentException e) {
306: }
307: try {
308: m_h.setOpcode(0x100);
309: fail("IllegalArgumentException not thrown");
310: } catch (IllegalArgumentException e) {
311: }
312: }
313:
314: public void test_Count() {
315: m_h.setCount(2, 0x1E);
316: assertEquals(0, m_h.getCount(0));
317: assertEquals(0, m_h.getCount(1));
318: assertEquals(0x1E, m_h.getCount(2));
319: assertEquals(0, m_h.getCount(3));
320:
321: m_h.incCount(0);
322: assertEquals(1, m_h.getCount(0));
323:
324: m_h.decCount(2);
325: assertEquals(0x1E - 1, m_h.getCount(2));
326: }
327:
328: public void test_setCount_invalid() {
329: try {
330: m_h.setCount(-1, 0);
331: fail("ArrayIndexOutOfBoundsException not thrown");
332: } catch (ArrayIndexOutOfBoundsException e) {
333: }
334: try {
335: m_h.setCount(4, 0);
336: fail("ArrayIndexOutOfBoundsException not thrown");
337: } catch (ArrayIndexOutOfBoundsException e) {
338: }
339:
340: try {
341: m_h.setCount(0, -1);
342: fail("IllegalArgumentException not thrown");
343: } catch (IllegalArgumentException e) {
344: }
345: try {
346: m_h.setCount(3, 0x10000);
347: fail("IllegalArgumentException not thrown");
348: } catch (IllegalArgumentException e) {
349: }
350: }
351:
352: public void test_getCount_invalid() {
353: try {
354: m_h.getCount(-1);
355: fail("ArrayIndexOutOfBoundsException not thrown");
356: } catch (ArrayIndexOutOfBoundsException e) {
357: }
358: try {
359: m_h.getCount(4);
360: fail("ArrayIndexOutOfBoundsException not thrown");
361: } catch (ArrayIndexOutOfBoundsException e) {
362: }
363: }
364:
365: public void test_incCount_invalid() {
366: m_h.setCount(1, 0xFFFF);
367: try {
368: m_h.incCount(1);
369: fail("IllegalStateException not thrown");
370: } catch (IllegalStateException e) {
371: }
372: }
373:
374: public void test_decCount_invalid() {
375: m_h.setCount(2, 0);
376: try {
377: m_h.decCount(2);
378: fail("IllegalStateException not thrown");
379: } catch (IllegalStateException e) {
380: }
381: }
382:
383: public void test_toString() {
384: m_h.setOpcode(Opcode.value("STATUS"));
385: m_h.setRcode(Rcode.value("NXDOMAIN"));
386: m_h.setFlag(0); // qr
387: m_h.setFlag(7); // rd
388: m_h.setFlag(8); // ra
389: m_h.setFlag(11); // cd
390: m_h.setCount(1, 0xFF);
391: m_h.setCount(2, 0x0A);
392:
393: String text = m_h.toString();
394:
395: assertFalse(text.indexOf("id: 43981") == -1);
396: assertFalse(text.indexOf("opcode: STATUS") == -1);
397: assertFalse(text.indexOf("status: NXDOMAIN") == -1);
398: assertFalse(text.indexOf(" qr ") == -1);
399: assertFalse(text.indexOf(" rd ") == -1);
400: assertFalse(text.indexOf(" ra ") == -1);
401: assertFalse(text.indexOf(" cd ") == -1);
402: assertFalse(text.indexOf("qd: 0 ") == -1);
403: assertFalse(text.indexOf("an: 255 ") == -1);
404: assertFalse(text.indexOf("au: 10 ") == -1);
405: assertFalse(text.indexOf("ad: 0 ") == -1);
406: }
407:
408: public void test_clone() {
409: m_h.setOpcode(Opcode.value("IQUERY"));
410: m_h.setRcode(Rcode.value("SERVFAIL"));
411: m_h.setFlag(0); // qr
412: m_h.setFlag(7); // rd
413: m_h.setFlag(8); // ra
414: m_h.setFlag(11); // cd
415: m_h.setCount(1, 0xFF);
416: m_h.setCount(2, 0x0A);
417:
418: Header h2 = (Header) m_h.clone();
419:
420: assertNotSame(m_h, h2);
421: assertEquals(m_h.getID(), h2.getID());
422: for (int i = 0; i < 16; ++i) {
423: if ((i > 0 && i < 5) || i > 11) {
424: continue;
425: }
426: assertEquals(m_h.getFlag(i), h2.getFlag(i));
427: }
428: for (int i = 0; i < 4; ++i) {
429: assertEquals(m_h.getCount(i), h2.getCount(i));
430: }
431: }
432: }
|