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 junit.framework.TestCase;
038:
039: public class DNSOutputTest extends TestCase {
040: private DNSOutput m_do;
041:
042: public void setUp() {
043: m_do = new DNSOutput(1);
044: }
045:
046: private void assertEquals(byte[] exp, byte[] act) {
047: assertTrue(java.util.Arrays.equals(exp, act));
048: }
049:
050: public void test_default_ctor() {
051: m_do = new DNSOutput();
052: assertEquals(0, m_do.current());
053: }
054:
055: public void test_initial_state() {
056: assertEquals(0, m_do.current());
057: try {
058: m_do.restore();
059: fail("IllegalStateException not thrown");
060: } catch (IllegalStateException e) {
061: // pass
062: }
063: try {
064: m_do.jump(1);
065: fail("IllegalArgumentException not thrown");
066: } catch (IllegalArgumentException e) {
067: // pass
068: }
069: }
070:
071: public void test_writeU8_basic() {
072: m_do.writeU8(1);
073: assertEquals(1, m_do.current());
074:
075: byte[] curr = m_do.toByteArray();
076: assertEquals(1, curr.length);
077: assertEquals(1, curr[0]);
078: }
079:
080: public void test_writeU8_expand() {
081: // starts off at 1;
082: m_do.writeU8(1);
083: m_do.writeU8(2);
084:
085: assertEquals(2, m_do.current());
086:
087: byte[] curr = m_do.toByteArray();
088: assertEquals(2, curr.length);
089: assertEquals(1, curr[0]);
090: assertEquals(2, curr[1]);
091: }
092:
093: public void test_writeU8_max() {
094: m_do.writeU8(0xFF);
095: byte[] curr = m_do.toByteArray();
096: assertEquals((byte) 0xFF, (byte) curr[0]);
097: }
098:
099: public void test_writeU8_toobig() {
100: try {
101: m_do.writeU8(0x1FF);
102: fail("IllegalArgumentException not thrown");
103: } catch (IllegalArgumentException e) {
104: // pass
105: }
106: }
107:
108: public void test_writeU16_basic() {
109: m_do.writeU16(0x100);
110: assertEquals(2, m_do.current());
111:
112: byte[] curr = m_do.toByteArray();
113: assertEquals(2, curr.length);
114: assertEquals(1, curr[0]);
115: assertEquals(0, curr[1]);
116: }
117:
118: public void test_writeU16_max() {
119: m_do.writeU16(0xFFFF);
120: byte[] curr = m_do.toByteArray();
121: assertEquals((byte) 0xFF, (byte) curr[0]);
122: assertEquals((byte) 0XFF, (byte) curr[1]);
123: }
124:
125: public void test_writeU16_toobig() {
126: try {
127: m_do.writeU16(0x1FFFF);
128: fail("IllegalArgumentException not thrown");
129: } catch (IllegalArgumentException e) {
130: // pass
131: }
132: }
133:
134: public void test_writeU32_basic() {
135: m_do.writeU32(0x11001011);
136: assertEquals(4, m_do.current());
137:
138: byte[] curr = m_do.toByteArray();
139: assertEquals(4, curr.length);
140: assertEquals(0x11, curr[0]);
141: assertEquals(0x00, curr[1]);
142: assertEquals(0x10, curr[2]);
143: assertEquals(0x11, curr[3]);
144: }
145:
146: public void test_writeU32_max() {
147: m_do.writeU32(0xFFFFFFFFL);
148: byte[] curr = m_do.toByteArray();
149: assertEquals((byte) 0xFF, (byte) curr[0]);
150: assertEquals((byte) 0XFF, (byte) curr[1]);
151: assertEquals((byte) 0XFF, (byte) curr[2]);
152: assertEquals((byte) 0XFF, (byte) curr[3]);
153: }
154:
155: public void test_writeU32_toobig() {
156: try {
157: m_do.writeU32(0x1FFFFFFFFL);
158: fail("IllegalArgumentException not thrown");
159: } catch (IllegalArgumentException e) {
160: // pass
161: }
162: }
163:
164: public void test_jump_basic() {
165: m_do.writeU32(0x11223344L);
166: assertEquals(4, m_do.current());
167: m_do.jump(2);
168: assertEquals(2, m_do.current());
169: m_do.writeU8(0x99);
170: byte[] curr = m_do.toByteArray();
171: assertEquals(3, curr.length);
172: assertEquals(0x11, curr[0]);
173: assertEquals(0x22, curr[1]);
174: assertEquals((byte) 0x99, (byte) curr[2]);
175:
176: }
177:
178: public void test_writeByteArray_1arg() {
179: byte[] in = new byte[] { (byte) 0xAB, (byte) 0xCD, (byte) 0xEF,
180: (byte) 0x12, (byte) 0x34 };
181: m_do.writeByteArray(in);
182: assertEquals(5, m_do.current());
183: byte[] curr = m_do.toByteArray();
184: assertEquals(in, curr);
185: }
186:
187: public void test_writeByteArray_3arg() {
188: byte[] in = new byte[] { (byte) 0xAB, (byte) 0xCD, (byte) 0xEF,
189: (byte) 0x12, (byte) 0x34 };
190: m_do.writeByteArray(in, 2, 3);
191: assertEquals(3, m_do.current());
192: byte[] exp = new byte[] { in[2], in[3], in[4] };
193: byte[] curr = m_do.toByteArray();
194: assertEquals(exp, curr);
195: }
196:
197: public void test_writeCountedString_basic() {
198: byte[] in = new byte[] { 'h', 'e', 'l', 'L', '0' };
199: m_do.writeCountedString(in);
200: assertEquals(in.length + 1, m_do.current());
201: byte[] curr = m_do.toByteArray();
202: byte[] exp = new byte[] { (byte) (in.length), in[0], in[1],
203: in[2], in[3], in[4] };
204: assertEquals(exp, curr);
205: }
206:
207: public void test_writeCountedString_empty() {
208: byte[] in = new byte[] {};
209: m_do.writeCountedString(in);
210: assertEquals(in.length + 1, m_do.current());
211: byte[] curr = m_do.toByteArray();
212: byte[] exp = new byte[] { (byte) (in.length) };
213: assertEquals(exp, curr);
214: }
215:
216: public void test_writeCountedString_toobig() {
217: byte[] in = new byte[256];
218: try {
219: m_do.writeCountedString(in);
220: fail("IllegalArgumentException not thrown");
221: } catch (IllegalArgumentException e) {
222: // pass
223: }
224: }
225:
226: public void test_save_restore() {
227: m_do.writeU32(0x12345678L);
228: assertEquals(4, m_do.current());
229: m_do.save();
230: m_do.writeU16(0xABCD);
231: assertEquals(6, m_do.current());
232: m_do.restore();
233: assertEquals(4, m_do.current());
234: try {
235: m_do.restore();
236: fail("IllegalArgumentException not thrown");
237: } catch (IllegalStateException e) {
238: // pass
239: }
240: }
241:
242: }
|