~ubuntu-branches/ubuntu/wily/dnsjava/wily-proposed

« back to all changes in this revision

Viewing changes to tests/org/xbill/DNS/DNSOutputTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2009-07-21 15:17:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090721151703-6v0107p1s3h7gv1c
Tags: upstream-2.0.6
ImportĀ upstreamĀ versionĀ 2.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Java -*-
 
2
//
 
3
// Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
 
4
// Copyright (c) 2005, University of Colorado at Boulder
 
5
// All rights reserved.
 
6
// 
 
7
// Redistribution and use in source and binary forms, with or without
 
8
// modification, are permitted provided that the following conditions are
 
9
// met:
 
10
// 
 
11
// * Redistributions of source code must retain the above copyright
 
12
//   notice, this list of conditions and the following disclaimer.
 
13
// 
 
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.
 
17
// 
 
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
 
21
//   permission.
 
22
// 
 
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.
 
34
//
 
35
package org.xbill.DNS;
 
36
 
 
37
import junit.framework.TestCase;
 
38
 
 
39
public class DNSOutputTest extends TestCase
 
40
{
 
41
    private DNSOutput m_do;
 
42
 
 
43
    public void setUp()
 
44
    {
 
45
        m_do = new DNSOutput( 1 );
 
46
    }
 
47
 
 
48
    private void assertEquals( byte[] exp, byte[] act )
 
49
    {
 
50
        assertTrue(java.util.Arrays.equals(exp, act));
 
51
    }
 
52
 
 
53
    public void test_default_ctor()
 
54
    {
 
55
        m_do = new DNSOutput();
 
56
        assertEquals( 0, m_do.current() );
 
57
    }
 
58
 
 
59
    public void test_initial_state()
 
60
    {
 
61
        assertEquals( 0, m_do.current() );
 
62
        try {
 
63
            m_do.restore();
 
64
            fail( "IllegalStateException not thrown" );
 
65
        }
 
66
        catch( IllegalStateException e ){
 
67
            // pass
 
68
        }
 
69
        try {
 
70
            m_do.jump(1);
 
71
            fail( "IllegalArgumentException not thrown" );
 
72
        }
 
73
        catch( IllegalArgumentException e ){
 
74
            // pass
 
75
        }
 
76
    }
 
77
 
 
78
    public void test_writeU8_basic()
 
79
    {
 
80
        m_do.writeU8(1);
 
81
        assertEquals( 1, m_do.current() );
 
82
 
 
83
        byte[] curr = m_do.toByteArray();
 
84
        assertEquals( 1, curr.length );
 
85
        assertEquals( 1, curr[0] );
 
86
    }
 
87
 
 
88
    public void test_writeU8_expand()
 
89
    {
 
90
        // starts off at 1;
 
91
        m_do.writeU8(1);
 
92
        m_do.writeU8(2);
 
93
 
 
94
        assertEquals( 2, m_do.current() );
 
95
 
 
96
        byte[] curr = m_do.toByteArray();
 
97
        assertEquals( 2, curr.length );
 
98
        assertEquals( 1, curr[0] );
 
99
        assertEquals( 2, curr[1] );
 
100
    }
 
101
 
 
102
    public void test_writeU8_max()
 
103
    {
 
104
        m_do.writeU8(0xFF);
 
105
        byte[] curr = m_do.toByteArray();
 
106
        assertEquals( (byte)0xFF, (byte)curr[0] );
 
107
    }
 
108
    
 
109
    public void test_writeU8_toobig()
 
110
    {
 
111
        try {
 
112
            m_do.writeU8( 0x1FF );
 
113
            fail( "IllegalArgumentException not thrown" );
 
114
        }
 
115
        catch( IllegalArgumentException e ){
 
116
            // pass
 
117
        }
 
118
    }
 
119
 
 
120
    public void test_writeU16_basic()
 
121
    {
 
122
        m_do.writeU16(0x100);
 
123
        assertEquals( 2, m_do.current() );
 
124
 
 
125
        byte[] curr = m_do.toByteArray();
 
126
        assertEquals( 2, curr.length );
 
127
        assertEquals( 1, curr[0] );
 
128
        assertEquals( 0, curr[1] );
 
129
    }
 
130
 
 
131
    public void test_writeU16_max()
 
132
    {
 
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] );
 
137
    }
 
138
    
 
139
    public void test_writeU16_toobig()
 
140
    {
 
141
        try {
 
142
            m_do.writeU16( 0x1FFFF );
 
143
            fail( "IllegalArgumentException not thrown" );
 
144
        }
 
145
        catch( IllegalArgumentException e ){
 
146
            // pass
 
147
        }
 
148
    }
 
149
 
 
150
    public void test_writeU32_basic()
 
151
    {
 
152
        m_do.writeU32(0x11001011);
 
153
        assertEquals( 4, m_do.current() );
 
154
 
 
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] );
 
161
    }
 
162
 
 
163
    public void test_writeU32_max()
 
164
    {
 
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] );
 
171
    }
 
172
    
 
173
    public void test_writeU32_toobig()
 
174
    {
 
175
        try {
 
176
            m_do.writeU32( 0x1FFFFFFFFL );
 
177
            fail( "IllegalArgumentException not thrown" );
 
178
        }
 
179
        catch( IllegalArgumentException e ){
 
180
            // pass
 
181
        }
 
182
    }
 
183
 
 
184
    public void test_jump_basic()
 
185
    {
 
186
        m_do.writeU32(0x11223344L);
 
187
        assertEquals( 4, m_do.current() );
 
188
        m_do.jump( 2 );
 
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] );
 
196
        
 
197
    }
 
198
 
 
199
    public void test_writeByteArray_1arg()
 
200
    {
 
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 );
 
206
    }
 
207
 
 
208
    public void test_writeByteArray_3arg()
 
209
    {
 
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 );
 
216
    }
 
217
 
 
218
    public void test_writeCountedString_basic()
 
219
    {
 
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 );
 
226
    }
 
227
 
 
228
    public void test_writeCountedString_empty()
 
229
    {
 
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 );
 
236
    }
 
237
 
 
238
    public void test_writeCountedString_toobig()
 
239
    {
 
240
        byte[] in = new byte [ 256 ];
 
241
        try {
 
242
            m_do.writeCountedString(in);
 
243
            fail( "IllegalArgumentException not thrown" );
 
244
        }
 
245
        catch( IllegalArgumentException e ){
 
246
            // pass
 
247
        }
 
248
    }
 
249
 
 
250
    public void test_save_restore()
 
251
    {
 
252
        m_do.writeU32( 0x12345678L );
 
253
        assertEquals( 4, m_do.current() );
 
254
        m_do.save();
 
255
        m_do.writeU16( 0xABCD );
 
256
        assertEquals( 6, m_do.current() );
 
257
        m_do.restore();
 
258
        assertEquals( 4, m_do.current() );
 
259
        try {
 
260
            m_do.restore();
 
261
            fail( "IllegalArgumentException not thrown" );
 
262
        }
 
263
        catch( IllegalStateException e ){
 
264
            // pass
 
265
        }
 
266
    }
 
267
 
 
268
}