1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.asn1.codec.stateful.examples;
18
19 import java.nio.ByteBuffer;
20 import java.util.Random;
21
22 import junit.framework.TestCase;
23
24 import org.apache.asn1.codec.DecoderException;
25 import org.apache.asn1.codec.binary.Hex;
26 import org.apache.asn1.codec.stateful.DecoderCallback;
27 import org.apache.asn1.codec.stateful.StatefulDecoder;
28
29 /***
30 * Document me.
31 *
32 * @author <a href="mailto:dev@directory.apache.org"> Apache Directory
33 * Project</a> $Rev: 161723 $
34 */
35 public class HexDecoderTest extends TestCase implements DecoderCallback
36 {
37 private HexDecoder decoder = null;
38 byte[] encoded = null;
39 byte[] decoded = null;
40 byte[] original = null;
41
42
43 protected void setUp() throws Exception
44 {
45 super.setUp();
46 decoder = new HexDecoder();
47 decoder.setCallback( this );
48 }
49
50
51 protected void tearDown() throws Exception
52 {
53 super.tearDown();
54 decoder = null;
55 encoded = null;
56 decoded = null;
57 original = null;
58 }
59
60
61 private void generateData( int amount )
62 {
63 Random rand = new Random( System.currentTimeMillis() ) ;
64 original = new byte[amount/2];
65 rand.nextBytes( original );
66 char[] chars = Hex.encodeHex( original );
67 encoded = new byte[amount];
68 for ( int ii = 0; ii < amount; ii++ )
69 {
70 encoded[ii] = (byte) chars[ii];
71 }
72 }
73
74
75 public void decodeOccurred( StatefulDecoder decoder, Object obj )
76 {
77 ByteBuffer decodedBuf = ( ByteBuffer ) obj ;
78
79 if ( decoded == null )
80 {
81 decoded = new byte[decodedBuf.remaining()];
82 decodedBuf.get( decoded ) ;
83 }
84 else
85 {
86 byte[] temp = decoded ;
87 decoded = new byte[decodedBuf.remaining() + temp.length];
88 System.arraycopy( temp, 0, decoded, 0, temp.length );
89 decodedBuf.get( decoded, temp.length, decodedBuf.remaining() );
90 }
91 }
92
93
94 public void testDecode0() throws DecoderException
95 {
96 generateData( 0 );
97 decoder.decode( ByteBuffer.wrap( encoded ) );
98
99 if ( decoded == null )
100 {
101 decoded = new byte[0];
102 }
103
104 assertDecoded();
105 }
106
107
108 public void testDecode2() throws DecoderException
109 {
110 generateData( 2 );
111 decoder.decode( ByteBuffer.wrap( encoded ) );
112 assertDecoded();
113 }
114
115
116 public void testDecode26() throws DecoderException
117 {
118 generateData( 26 );
119 decoder.decode( ByteBuffer.wrap( encoded ) );
120 assertDecoded();
121 }
122
123
124 public void testDecode254() throws DecoderException
125 {
126 generateData( 254 );
127 decoder.decode( ByteBuffer.wrap( encoded ) );
128 assertDecoded();
129 }
130
131
132 public void testDecode256() throws DecoderException
133 {
134 generateData( 256 );
135 decoder.decode( ByteBuffer.wrap( encoded ) );
136 assertDecoded();
137 }
138
139
140 public void testDecode258() throws DecoderException
141 {
142 generateData( 258 );
143 decoder.decode( ByteBuffer.wrap( encoded ) );
144 assertDecoded();
145 }
146
147
148 public void testDecode2048() throws DecoderException
149 {
150 generateData( 2048 );
151 decoder.decode( ByteBuffer.wrap( encoded ) );
152 assertDecoded();
153 }
154
155
156 public void testPartialDecode2() throws DecoderException
157 {
158 generateData( 2 );
159
160 decoder.decode( ByteBuffer.wrap( encoded, 0, 1 ) );
161
162 try
163 {
164 assertDecoded();
165 fail( "should not get here" ) ;
166 }
167 catch( NullPointerException e )
168 {
169 }
170
171 decoder.decode( ByteBuffer.wrap( encoded, 1, 1 ) );
172
173 assertDecoded();
174 }
175
176
177 public void testPartialDecode30() throws DecoderException
178 {
179 generateData( 30 );
180
181 for ( int ii = 0; ii < 30; ii += 3 )
182 {
183 decoder.decode( ByteBuffer.wrap( encoded, ii, 3 ) );
184 }
185
186 assertDecoded();
187 }
188
189
190 public void testPartialDecode300() throws DecoderException
191 {
192 generateData( 300 );
193
194 for ( int ii = 0; ii < 300; ii += 5 )
195 {
196 decoder.decode( ByteBuffer.wrap( encoded, ii, 5 ) );
197 }
198
199 assertDecoded();
200 }
201
202
203 private void assertDecoded()
204 {
205 if ( decoded.length != original.length )
206 {
207 fail( "decoded length of " + decoded.length
208 + " did not match expected original data length of "
209 + original.length ) ;
210 }
211
212 for ( int ii = 0; ii < decoded.length; ii++ )
213 {
214 if ( decoded[ii] != original[ii] )
215 {
216 fail( "decode failed - decoded array does not match" ) ;
217 }
218 }
219 }
220 }