1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.asn1.ber ;
18  
19  
20  import junit.framework.TestCase ;
21  
22  import java.nio.ByteBuffer ;
23  
24  import org.apache.asn1.codec.DecoderException;
25  import org.apache.asn1.ber.Length;
26  
27  
28  /***
29   * Tests the Length class.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">
32   * Apache Directory Project</a>
33   * @version $Rev: 157644 $
34   */
35  public class LengthTest extends TestCase
36  {
37      /***
38       * Tests the long form when a byte is used for the length's length and
39       * another is used for the value length for a total of two bytes for the
40       * length field itself.
41       */
42      public void testLongTwoBytes() throws DecoderException
43      {
44          ByteBuffer list = ByteBuffer.allocate( 2 ) ;
45          list.put( (byte) 0x81 ) ;
46          list.put( (byte) 0x01 ) ;
47          list.flip();
48          assertEquals( 0x01, Length.getLength( list ) );
49  
50          list = ByteBuffer.allocate( 2 ) ;
51          list.put( (byte) 0x81 ) ;
52          list.put( (byte) 0x05 ) ;
53          list.flip();
54          assertEquals( 0x05, Length.getLength( list ) );
55  
56          list = ByteBuffer.allocate( 2 ) ;
57          list.put( (byte) 0x81 ) ;
58          list.put( (byte) 0xFF ) ;
59          list.flip();
60          assertEquals( 0xFF, Length.getLength( list ) );
61      }
62  
63  
64      /***
65       * Tests the long form when a byte is used for the length's length and
66       * two more are used for the value length for a total of three bytes for the
67       * length field itself.
68       */
69      public void testLongThreeBytes() throws DecoderException
70      {
71          ByteBuffer list = ByteBuffer.allocate( 3 ) ;
72          list.put( (byte) 0x82 ) ;
73          list.put( (byte) 0x00 ) ;
74          list.put( (byte) 0x01 ) ;
75          list.flip();
76          assertEquals( 0x01, Length.getLength( list ) );
77  
78          list = ByteBuffer.allocate( 3 ) ;
79          list.put( (byte) 0x82 ) ;
80          list.put( (byte) 0x00 ) ;
81          list.put( (byte) 0x05 ) ;
82          list.flip();
83          assertEquals( 0x05, Length.getLength( list ) );
84  
85          list = ByteBuffer.allocate( 3 ) ;
86          list.put( (byte) 0x82 ) ;
87          list.put( (byte) 0x00 ) ;
88          list.put( (byte) 0xFF ) ;
89          list.flip();
90          assertEquals( 0xFF, Length.getLength( list ) );
91  
92          list = ByteBuffer.allocate( 3 ) ;
93          list.put( (byte) 0x82 ) ;
94          list.put( (byte) 0x01 ) ;
95          list.put( (byte) 0x05 ) ;
96          list.flip();
97          assertEquals( 0x0105, Length.getLength( list ) );
98  
99          list = ByteBuffer.allocate( 3 ) ;
100         list.put( (byte) 0x82 ) ;
101         list.put( (byte) 0x01 ) ;
102         list.put( (byte) 0xFF ) ;
103         list.flip();
104         assertEquals( 0x01FF, Length.getLength( list ) );
105 
106         list = ByteBuffer.allocate( 3 ) ;
107         list.put( (byte) 0x82 ) ;
108         list.put( (byte) 0x80 ) ;
109         list.put( (byte) 0x00 ) ;
110         list.flip();
111         assertEquals( 32768, Length.getLength( list ) );
112     }
113 
114 
115     /***
116      * Tests the long form when a byte is used for the length's length and
117      * three more are used for the value length for a total of four bytes for
118      * the length field itself.
119      */
120     public void testLongFourBytes() throws DecoderException
121     {
122         ByteBuffer list = ByteBuffer.allocate( 4 ) ;
123         list.put( (byte) 0x83 ) ;
124         list.put( (byte) 0x00 ) ;
125         list.put( (byte) 0x00 ) ;
126         list.put( (byte) 0x01 ) ;
127         list.flip();
128         assertEquals( 0x01, Length.getLength( list ) );
129 
130         list = ByteBuffer.allocate( 4 ) ;
131         list.put( (byte) 0x83 ) ;
132         list.put( (byte) 0x00 ) ;
133         list.put( (byte) 0x00 ) ;
134         list.put( (byte) 0x05 ) ;
135         list.flip();
136         assertEquals( 0x05, Length.getLength( list ) );
137 
138         list = ByteBuffer.allocate( 4 ) ;
139         list.put( (byte) 0x83 ) ;
140         list.put( (byte) 0x00 ) ;
141         list.put( (byte) 0x00 ) ;
142         list.put( (byte) 0xFF ) ;
143         list.flip();
144         assertEquals( 0xFF, Length.getLength( list ) );
145     }
146 
147 
148     /***
149      * Tests the long form when a byte is used for the length's length and
150      * four more are used for the value length for a total of five bytes for
151      * the length field itself.
152      */
153     public void testLongFiveBytes() throws DecoderException
154     {
155         ByteBuffer list = ByteBuffer.allocate( 5 ) ;
156         list.put( (byte) 0x84 ) ;
157         list.put( (byte) 0x00 ) ;
158         list.put( (byte) 0x00 ) ;
159         list.put( (byte) 0x00 ) ;
160         list.put( (byte) 0x01 ) ;
161         list.flip();
162         assertEquals( 0x01, Length.getLength( list ) );
163 
164         list = ByteBuffer.allocate( 5 ) ;
165         list.put( (byte) 0x84 ) ;
166         list.put( (byte) 0x00 ) ;
167         list.put( (byte) 0x00 ) ;
168         list.put( (byte) 0x00 ) ;
169         list.put( (byte) 0x05 ) ;
170         list.flip();
171         assertEquals( 0x05, Length.getLength( list ) );
172 
173         list = ByteBuffer.allocate( 5 ) ;
174         list.put( (byte) 0x84 ) ;
175         list.put( (byte) 0x00 ) ;
176         list.put( (byte) 0x00 ) ;
177         list.put( (byte) 0x00 ) ;
178         list.put( (byte) 0xFF ) ;
179         list.flip();
180         assertEquals( 0xFF, Length.getLength( list ) );
181     }
182 
183 
184     /***
185      * Tests to make sure certain length sizes are not allowed.  Basically we
186      * are capping off the length at 2^32-1 which corresponds to 5 total length
187      * bytes in the long form or the indeterminate form.
188      */
189     public void testMaxLength() throws Exception
190     {
191         ByteBuffer list = ByteBuffer.allocate( 6 ) ;
192         list.put( (byte) 0x1 ) ;
193         list.put( (byte) 0x1 ) ;
194         list.put( (byte) 0x1 ) ;
195         list.put( (byte) 0x1 ) ;
196         list.put( (byte) 0x1 ) ;
197         list.put( (byte) 0x1 ) ;
198         list.flip();
199 
200         try
201         {
202             Length.getLength( list ) ;
203             fail( "should fail before we get here" ) ;
204         }
205         catch ( DecoderException t )
206         {
207             assertNotNull( t ) ;
208         }
209         
210         
211         list.clear() ;
212         list.put(( byte ) 0x7 ) ;
213         list.flip() ;
214         assertEquals( 7, Length.getLength( list ) ) ;
215     }
216 
217 
218     /***
219      * Makes sure no additions can be made after short form fixation.
220      */
221     public void testShortFixation() throws Exception
222     {
223         byte[] bites = { (byte) 0x01, (byte) 0xff } ;
224         
225         Length length = new Length() ;
226         length.add( bites[0] ) ;
227         
228         try
229         {
230             length.add( bites[1] ) ;
231             fail( "should never get here due to illegal state" ) ;
232         }
233         catch ( Throwable t ) 
234         {
235             assertNotNull( t ) ;
236         }
237 
238         assertEquals( 1, length.getLength() );
239     }
240 
241     /***
242      * Test that a Length could not begin with a 0xFF byte, which is
243      * reserved for future extensions.
244      *
245      */
246     public void testRestrictedValueForFutureExtension() 
247     {
248     	Length length = new Length();
249 
250         try
251         {
252         	length.add( (byte) 0xFF ) ;
253         	length.add( (byte) 0x01 ) ;
254             fail( "should fail before we get here" ) ;
255         }
256         catch ( DecoderException t )
257         {
258             assertNotNull( t ) ;
259         }
260     }
261 
262     /***
263      * Test that an indefinite Length form is rejected. 
264      *
265      */
266     public void testIndefiniteLength() 
267     {
268     	Length length = new Length();
269 
270         try
271         {
272         	length.add( (byte) 0x80 ) ;
273         	length.add( (byte) 0x01 ) ;
274         	length.add( (byte) 0x00 ) ;
275         	length.add( (byte) 0x00 ) ;
276             fail( "should fail before we get here" ) ;
277         }
278         catch ( Throwable t )
279         {
280             assertNotNull( t ) ;
281         }
282     }
283 }
284