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 org.apache.asn1.ber.AbstractDecoderTestCase;
21  import org.apache.asn1.ber.BERDecoderState;
22  
23  
24  /***
25   * Tests single byte length encodings in a BER TLV.
26   *
27   * @author <a href="mailto:dev@directory.apache.org">
28   * Apache Directory Project</a>
29   * @version $Rev: 157644 $
30   */
31  public class MultiByteLengthTests extends AbstractDecoderTestCase
32  {
33      /***
34       * Creates a single byte lenth test case.
35       * 
36       * @param name the name of this test
37       */
38      public MultiByteLengthTests( String name )
39      {
40          super( name ) ;
41      }
42  
43      
44      public void testLength128() throws Exception
45      {
46          Tuple tlv = decode( "01000001" ) ;
47          assertEquals( 1, tlv.id ) ;
48          assertEquals( 0, tlvList.size() ) ;
49          assertEquals( true, tlv.isPrimitive ) ;
50          assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
51          assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
52          assertEquals( 0, tlv.length ) ;
53          
54          tlv = decode( "10000001" ) ;
55          assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
56          assertEquals( 0, tlv.length ) ;
57  
58          tlv = decode( "10000000" ) ;
59          assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
60          assertEquals( 128, tlv.length ) ;
61      }
62  
63  
64      public void testLength129() throws Exception
65      {
66          Tuple tlv = decode( "01000001" ) ;
67          assertEquals( 1, tlv.id ) ;
68          assertEquals( 0, tlvList.size() ) ;
69          assertEquals( true, tlv.isPrimitive ) ;
70          assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
71          assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
72          assertEquals( 0, tlv.length ) ;
73          
74          tlv = decode( "10000001" ) ;
75          assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
76          assertEquals( 0, tlv.length ) ;
77  
78          tlv = decode( "10000001" ) ;
79          assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
80          assertEquals( 129, tlv.length ) ;
81      }
82  
83  
84      public void testLength255() throws Exception
85      {
86          Tuple tlv = decode( "01000001" ) ;
87          assertEquals( 1, tlv.id ) ;
88          assertEquals( 0, tlvList.size() ) ;
89          assertEquals( true, tlv.isPrimitive ) ;
90          assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
91          assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
92          assertEquals( 0, tlv.length ) ;
93          
94          tlv = decode( "10000001" ) ;
95          assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
96          assertEquals( 0, tlv.length ) ;
97  
98          tlv = decode( "11111111" ) ;
99          assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
100         assertEquals( 255, tlv.length ) ;
101     }
102     
103     
104     public void testLength32768() throws Exception
105     {
106         Tuple tlv = decode( "01000001" ) ;
107         assertEquals( 1, tlv.id ) ;
108         assertEquals( 0, tlvList.size() ) ;
109         assertEquals( true, tlv.isPrimitive ) ;
110         assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
111         assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
112         assertEquals( 0, tlv.length ) ;
113         
114         tlv = decode( (byte)0x82 ) ;
115         assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
116         assertEquals( 0, tlv.length ) ;
117 
118         tlv = decode( (byte) 0x80 ) ;
119         assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
120         assertEquals( 0, tlv.length ) ;
121 
122         tlv = decode( (byte) 0x00 ) ;
123         assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
124         assertEquals( 32768, tlv.length ) ;
125     }
126     
127     
128     public void testLength65535() throws Exception
129     {
130         Tuple tlv = decode( "01000001" ) ;
131         assertEquals( 1, tlv.id ) ;
132         assertEquals( 0, tlvList.size() ) ;
133         assertEquals( true, tlv.isPrimitive ) ;
134         assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
135         assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
136         assertEquals( 0, tlv.length ) ;
137         
138         tlv = decode( "10000010" ) ;
139         assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
140         assertEquals( 0, tlv.length ) ;
141 
142         tlv = decode( "11111111" ) ;
143         assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
144         assertEquals( 0, tlv.length ) ;
145 
146         tlv = decode( "11111111" ) ;
147         assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
148         assertEquals( 65535, tlv.length ) ;
149     }
150 }