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.util.Arrays;
038: import junit.framework.TestCase;
039:
040: public class DNSInputTest extends TestCase {
041: private byte[] m_raw;
042: private DNSInput m_di;
043:
044: private void assertEquals(byte[] exp, byte[] act) {
045: assertTrue(Arrays.equals(exp, act));
046: }
047:
048: public void setUp() {
049: m_raw = new byte[] { 0, 1, 2, 3, 4, 5, (byte) 255, (byte) 255,
050: (byte) 255, (byte) 255 };
051: m_di = new DNSInput(m_raw);
052: }
053:
054: public void test_initial_state() {
055: assertEquals(0, m_di.current());
056: assertEquals(10, m_di.remaining());
057: }
058:
059: public void test_jump1() {
060: m_di.jump(1);
061: assertEquals(1, m_di.current());
062: assertEquals(9, m_di.remaining());
063: }
064:
065: public void test_jump2() {
066: m_di.jump(9);
067: assertEquals(9, m_di.current());
068: assertEquals(1, m_di.remaining());
069: }
070:
071: public void test_jump_invalid() {
072: try {
073: m_di.jump(10);
074: fail("IllegalArgumentException not thrown");
075: } catch (IllegalArgumentException e) {
076: // pass
077: }
078: }
079:
080: public void test_setActive() {
081: m_di.setActive(5);
082: assertEquals(0, m_di.current());
083: assertEquals(5, m_di.remaining());
084: }
085:
086: public void test_setActive_boundary1() {
087: m_di.setActive(10);
088: assertEquals(0, m_di.current());
089: assertEquals(10, m_di.remaining());
090: }
091:
092: public void test_setActive_boundary2() {
093: m_di.setActive(0);
094: assertEquals(0, m_di.current());
095: assertEquals(0, m_di.remaining());
096: }
097:
098: public void test_setActive_invalid() {
099: try {
100: m_di.setActive(11);
101: fail("IllegalArgumentException not thrown");
102: } catch (IllegalArgumentException e) {
103: // pass
104: }
105: }
106:
107: public void test_clearActive() {
108: // first without setting active:
109: m_di.clearActive();
110: assertEquals(0, m_di.current());
111: assertEquals(10, m_di.remaining());
112:
113: m_di.setActive(5);
114: m_di.clearActive();
115: assertEquals(0, m_di.current());
116: assertEquals(10, m_di.remaining());
117: }
118:
119: public void test_restore_invalid() {
120: try {
121: m_di.restore();
122: fail("IllegalStateException not thrown");
123: } catch (IllegalStateException e) {
124: // pass
125: }
126: }
127:
128: public void test_save_restore() {
129: m_di.jump(4);
130: assertEquals(4, m_di.current());
131: assertEquals(6, m_di.remaining());
132:
133: m_di.save();
134: m_di.jump(0);
135: assertEquals(0, m_di.current());
136: assertEquals(10, m_di.remaining());
137:
138: m_di.restore();
139: assertEquals(4, m_di.current());
140: assertEquals(6, m_di.remaining());
141: }
142:
143: public void test_readU8_basic() throws WireParseException {
144: int v1 = m_di.readU8();
145: assertEquals(1, m_di.current());
146: assertEquals(9, m_di.remaining());
147: assertEquals(0, v1);
148: }
149:
150: public void test_readU8_maxval() throws WireParseException {
151: m_di.jump(9);
152: int v1 = m_di.readU8();
153: assertEquals(10, m_di.current());
154: assertEquals(0, m_di.remaining());
155: assertEquals(255, v1);
156:
157: try {
158: v1 = m_di.readU8();
159: fail("WireParseException not thrown");
160: } catch (WireParseException e) {
161: // pass
162: }
163: }
164:
165: public void test_readU16_basic() throws WireParseException {
166: int v1 = m_di.readU16();
167: assertEquals(2, m_di.current());
168: assertEquals(8, m_di.remaining());
169: assertEquals(1, v1);
170:
171: m_di.jump(1);
172: v1 = m_di.readU16();
173: assertEquals(258, v1);
174: }
175:
176: public void test_readU16_maxval() throws WireParseException {
177: m_di.jump(8);
178: int v = m_di.readU16();
179: assertEquals(10, m_di.current());
180: assertEquals(0, m_di.remaining());
181: assertEquals(0xFFFF, v);
182:
183: try {
184: m_di.jump(9);
185: m_di.readU16();
186: fail("WireParseException not thrown");
187: } catch (WireParseException e) {
188: // pass
189: }
190: }
191:
192: public void test_readU32_basic() throws WireParseException {
193: long v1 = m_di.readU32();
194: assertEquals(4, m_di.current());
195: assertEquals(6, m_di.remaining());
196: assertEquals(66051, v1);
197: }
198:
199: public void test_readU32_maxval() throws WireParseException {
200: m_di.jump(6);
201: long v = m_di.readU32();
202: assertEquals(10, m_di.current());
203: assertEquals(0, m_di.remaining());
204: assertEquals(0xFFFFFFFFL, v);
205:
206: try {
207: m_di.jump(7);
208: m_di.readU32();
209: fail("WireParseException not thrown");
210: } catch (WireParseException e) {
211: // pass
212: }
213: }
214:
215: public void test_readByteArray_0arg() throws WireParseException {
216: m_di.jump(1);
217: byte[] out = m_di.readByteArray();
218: assertEquals(10, m_di.current());
219: assertEquals(0, m_di.remaining());
220: assertEquals(9, out.length);
221: for (int i = 0; i < 9; ++i) {
222: assertEquals(m_raw[i + 1], out[i]);
223: }
224: }
225:
226: public void test_readByteArray_0arg_boundary()
227: throws WireParseException {
228: m_di.jump(9);
229: m_di.readU8();
230: byte[] out = m_di.readByteArray();
231: assertEquals(0, out.length);
232: }
233:
234: public void test_readByteArray_1arg() throws WireParseException {
235: byte[] out = m_di.readByteArray(2);
236: assertEquals(2, m_di.current());
237: assertEquals(8, m_di.remaining());
238: assertEquals(2, out.length);
239: assertEquals(0, out[0]);
240: assertEquals(1, out[1]);
241: }
242:
243: public void test_readByteArray_1arg_boundary()
244: throws WireParseException {
245: byte[] out = m_di.readByteArray(10);
246: assertEquals(10, m_di.current());
247: assertEquals(0, m_di.remaining());
248: assertEquals(m_raw, out);
249: }
250:
251: public void test_readByteArray_1arg_invalid() {
252: try {
253: m_di.readByteArray(11);
254: fail("WireParseException not thrown");
255: } catch (WireParseException e) {
256: // pass
257: }
258: }
259:
260: public void test_readByteArray_3arg() throws WireParseException {
261: byte[] data = new byte[5];
262: m_di.jump(4);
263:
264: m_di.readByteArray(data, 1, 4);
265: assertEquals(8, m_di.current());
266: assertEquals(0, data[0]);
267: for (int i = 0; i < 4; ++i) {
268: assertEquals(m_raw[i + 4], data[i + 1]);
269: }
270: }
271:
272: public void test_readCountedSting() throws WireParseException {
273: m_di.jump(1);
274: byte[] out = m_di.readCountedString();
275: assertEquals(1, out.length);
276: assertEquals(3, m_di.current());
277: assertEquals(out[0], 2);
278: }
279: }
|