3
// Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
4
// Copyright (c) 2005, University of Colorado at Boulder
5
// All rights reserved.
7
// Redistribution and use in source and binary forms, with or without
8
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
14
// * Redistributions in binary form must reproduce the above copyright
15
// notice, this list of conditions and the following disclaimer in the
16
// documentation and/or other materials provided with the distribution.
18
// * Neither the name of the University of Colorado at Boulder nor the
19
// names of its contributors may be used to endorse or promote
20
// products derived from this software without specific prior written
23
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
package org.xbill.DNS;
37
import junit.framework.TestCase;
39
public class DNSOutputTest extends TestCase
41
private DNSOutput m_do;
45
m_do = new DNSOutput( 1 );
48
private void assertEquals( byte[] exp, byte[] act )
50
assertTrue(java.util.Arrays.equals(exp, act));
53
public void test_default_ctor()
55
m_do = new DNSOutput();
56
assertEquals( 0, m_do.current() );
59
public void test_initial_state()
61
assertEquals( 0, m_do.current() );
64
fail( "IllegalStateException not thrown" );
66
catch( IllegalStateException e ){
71
fail( "IllegalArgumentException not thrown" );
73
catch( IllegalArgumentException e ){
78
public void test_writeU8_basic()
81
assertEquals( 1, m_do.current() );
83
byte[] curr = m_do.toByteArray();
84
assertEquals( 1, curr.length );
85
assertEquals( 1, curr[0] );
88
public void test_writeU8_expand()
94
assertEquals( 2, m_do.current() );
96
byte[] curr = m_do.toByteArray();
97
assertEquals( 2, curr.length );
98
assertEquals( 1, curr[0] );
99
assertEquals( 2, curr[1] );
102
public void test_writeU8_max()
105
byte[] curr = m_do.toByteArray();
106
assertEquals( (byte)0xFF, (byte)curr[0] );
109
public void test_writeU8_toobig()
112
m_do.writeU8( 0x1FF );
113
fail( "IllegalArgumentException not thrown" );
115
catch( IllegalArgumentException e ){
120
public void test_writeU16_basic()
122
m_do.writeU16(0x100);
123
assertEquals( 2, m_do.current() );
125
byte[] curr = m_do.toByteArray();
126
assertEquals( 2, curr.length );
127
assertEquals( 1, curr[0] );
128
assertEquals( 0, curr[1] );
131
public void test_writeU16_max()
133
m_do.writeU16(0xFFFF);
134
byte[] curr = m_do.toByteArray();
135
assertEquals( (byte)0xFF, (byte)curr[0] );
136
assertEquals( (byte)0XFF, (byte)curr[1] );
139
public void test_writeU16_toobig()
142
m_do.writeU16( 0x1FFFF );
143
fail( "IllegalArgumentException not thrown" );
145
catch( IllegalArgumentException e ){
150
public void test_writeU32_basic()
152
m_do.writeU32(0x11001011);
153
assertEquals( 4, m_do.current() );
155
byte[] curr = m_do.toByteArray();
156
assertEquals( 4, curr.length );
157
assertEquals( 0x11, curr[0] );
158
assertEquals( 0x00, curr[1] );
159
assertEquals( 0x10, curr[2] );
160
assertEquals( 0x11, curr[3] );
163
public void test_writeU32_max()
165
m_do.writeU32(0xFFFFFFFFL);
166
byte[] curr = m_do.toByteArray();
167
assertEquals( (byte)0xFF, (byte)curr[0] );
168
assertEquals( (byte)0XFF, (byte)curr[1] );
169
assertEquals( (byte)0XFF, (byte)curr[2] );
170
assertEquals( (byte)0XFF, (byte)curr[3] );
173
public void test_writeU32_toobig()
176
m_do.writeU32( 0x1FFFFFFFFL );
177
fail( "IllegalArgumentException not thrown" );
179
catch( IllegalArgumentException e ){
184
public void test_jump_basic()
186
m_do.writeU32(0x11223344L);
187
assertEquals( 4, m_do.current() );
189
assertEquals( 2, m_do.current() );
190
m_do.writeU8( 0x99 );
191
byte[] curr = m_do.toByteArray();
192
assertEquals( 3, curr.length );
193
assertEquals( 0x11, curr[0] );
194
assertEquals( 0x22, curr[1] );
195
assertEquals( (byte)0x99, (byte)curr[2] );
199
public void test_writeByteArray_1arg()
201
byte[] in = new byte[] { (byte)0xAB, (byte)0xCD, (byte)0xEF, (byte)0x12, (byte)0x34 };
202
m_do.writeByteArray( in );
203
assertEquals( 5, m_do.current() );
204
byte[] curr = m_do.toByteArray();
205
assertEquals( in, curr );
208
public void test_writeByteArray_3arg()
210
byte[] in = new byte[] { (byte)0xAB, (byte)0xCD, (byte)0xEF, (byte)0x12, (byte)0x34 };
211
m_do.writeByteArray( in, 2, 3 );
212
assertEquals( 3, m_do.current() );
213
byte[] exp = new byte[] { in[2], in[3], in[4] };
214
byte[] curr = m_do.toByteArray();
215
assertEquals( exp, curr );
218
public void test_writeCountedString_basic()
220
byte[] in = new byte[] { 'h', 'e', 'l', 'L', '0' };
221
m_do.writeCountedString( in );
222
assertEquals( in.length + 1, m_do.current() );
223
byte[] curr = m_do.toByteArray();
224
byte[] exp = new byte[] { (byte)(in.length), in[0], in[1], in[2], in[3], in[4] };
225
assertEquals( exp, curr );
228
public void test_writeCountedString_empty()
230
byte[] in = new byte[] {};
231
m_do.writeCountedString( in );
232
assertEquals( in.length + 1, m_do.current() );
233
byte[] curr = m_do.toByteArray();
234
byte[] exp = new byte[] { (byte)(in.length) };
235
assertEquals( exp, curr );
238
public void test_writeCountedString_toobig()
240
byte[] in = new byte [ 256 ];
242
m_do.writeCountedString(in);
243
fail( "IllegalArgumentException not thrown" );
245
catch( IllegalArgumentException e ){
250
public void test_save_restore()
252
m_do.writeU32( 0x12345678L );
253
assertEquals( 4, m_do.current() );
255
m_do.writeU16( 0xABCD );
256
assertEquals( 6, m_do.current() );
258
assertEquals( 4, m_do.current() );
261
fail( "IllegalArgumentException not thrown" );
263
catch( IllegalStateException e ){