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.primitives ;
18  
19  
20  import junit.framework.TestCase ;
21  
22  import java.math.BigInteger ;
23  
24  import org.apache.commons.lang.ArrayUtils;
25  import org.apache.asn1.ber.primitives.PrimitiveUtils;
26  
27  
28  /***
29   * Tests the PrimitiveUtil methods.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 157644 $
33   */
34  public class PrimitiveUtilsTest extends TestCase
35  {
36      public static byte[] bites0 = { ( byte ) 0x96, 0x46 } ;
37      public static byte[] bites1 = { ( byte ) 0x76, 0x46 } ;
38      public static byte[] bites2 = { ( byte ) 0x96 } ;
39      public static byte[] bites3 = { ( byte ) 0x46 } ;
40      public static byte[] bites4 = { ( byte ) 0x46, 0x34, 0x12 } ;
41      public static byte[] bites5 = { ( byte ) 0x96, 0x34, 0x12 } ;
42      public static byte[] bites6 = { ( byte ) 0x4F, 0x46, 0x34, 0x12 } ;
43      public static byte[] bites7 = { ( byte ) 0xFF, 0x26, 0x34, 0x12 } ;
44  
45      public static byte[][] byteArrays = {
46          bites0, bites1, bites2, bites3, bites4, bites5, bites6, bites7
47      } ;
48  
49      public static int[] values = {
50          ( new BigInteger( bites0 ) ).intValue(),
51          ( new BigInteger( bites1 ) ).intValue(),
52          ( new BigInteger( bites2 ) ).intValue(),
53          ( new BigInteger( bites3 ) ).intValue(),
54          ( new BigInteger( bites4 ) ).intValue(),
55          ( new BigInteger( bites5 ) ).intValue(),
56          ( new BigInteger( bites6 ) ).intValue(),
57          ( new BigInteger( bites7 ) ).intValue()
58      };
59  
60  
61      /***
62       * Tests the PrimitiveUtils.decodeInt(byte[], int, int) method.
63       * Uses the BigInteger class to verify correct encoding because
64       * that's what a BigInteger uses.
65       */
66      public void testDecodeInt()
67      {
68          byte[] bites = new byte[1];
69          bites[0] = (byte)0x80;
70          assertEquals( -128, PrimitiveUtils.decodeInt( bites, 0, 1 ) );
71  
72          bites = new byte[2];
73          bites[0] = 0;
74          bites[1] = (byte)0x80;
75          assertEquals( 128, PrimitiveUtils.decodeInt( bites, 0, 2 ) );
76  
77          bites = new byte[1];
78          bites[0] = (byte)0x80;
79          assertEquals( -128, PrimitiveUtils.decodeInt( bites, 0, 1 ) );
80  
81          assertEquals( 0, PrimitiveUtils.decodeInt( null, 0, 0 ) ) ;
82  
83          for ( int ii = 0; ii < byteArrays.length; ii++ )
84          {
85              int value = PrimitiveUtils.decodeInt( byteArrays[ii], 0,
86                      byteArrays[ii].length ) ;
87              assertEquals( values[ii], value ) ;
88          }
89  
90          try
91          {
92              PrimitiveUtils.decodeInt( bites7, 0, -1 ) ;
93              fail( "should never get here due to an exception" ) ;
94          }
95          catch( IllegalArgumentException e )
96          {
97              assertNotNull( e ) ;
98          }
99      }
100 
101 
102     public void testEncodeInt()
103     {
104         byte[] encoded = PrimitiveUtils.encodeInt( 0 );
105         byte[] actual = new BigInteger( "0" ).toByteArray();
106         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
107         assertEquals( 0, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
108 
109         encoded = PrimitiveUtils.encodeInt( -1 );
110         actual = new BigInteger( "-1" ).toByteArray();
111         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
112         assertEquals( -1, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
113 
114         encoded = PrimitiveUtils.encodeInt( 1 );
115         actual = new BigInteger( "1" ).toByteArray();
116         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
117         assertEquals( 1, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
118 
119         encoded = PrimitiveUtils.encodeInt( -100 );
120         actual = new BigInteger( "-100" ).toByteArray();
121         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
122         assertEquals( -100, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
123 
124         encoded = PrimitiveUtils.encodeInt( 100 );
125         actual = new BigInteger( "100" ).toByteArray();
126         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
127         assertEquals( 100, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
128 
129         encoded = PrimitiveUtils.encodeInt( -128 );
130         actual = new BigInteger( "-128" ).toByteArray();
131         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
132         assertEquals( -128, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
133 
134         encoded = PrimitiveUtils.encodeInt( 127 );
135         actual = new BigInteger( "127" ).toByteArray();
136         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
137         assertEquals( 127, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
138 
139         // --------------------------------------------------------------------
140         // TWO BYTES
141         // --------------------------------------------------------------------
142 
143         encoded = PrimitiveUtils.encodeInt( 128 );
144         actual = new BigInteger( "128" ).toByteArray();
145         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
146         assertEquals( 128, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
147 
148         encoded = PrimitiveUtils.encodeInt( -129 );
149         actual = new BigInteger( "-129" ).toByteArray();
150         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
151         assertEquals( -129, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
152 
153         encoded = PrimitiveUtils.encodeInt( 129 );
154         actual = new BigInteger( "129" ).toByteArray();
155         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
156         assertEquals( 129, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
157 
158         encoded = PrimitiveUtils.encodeInt( -1000 );
159         actual = new BigInteger( "-1000" ).toByteArray();
160         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
161         assertEquals( -1000, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
162 
163         encoded = PrimitiveUtils.encodeInt( 1000 );
164         actual = new BigInteger( "1000" ).toByteArray();
165         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
166         assertEquals( 1000, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
167 
168         encoded = PrimitiveUtils.encodeInt( -(1<<15) );
169         actual = new BigInteger( new Integer(-(1<<15))
170                 .toString() ).toByteArray();
171         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
172         assertEquals( -(1<<15), PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
173 
174         encoded = PrimitiveUtils.encodeInt( (1<<15)-1 );
175         actual = new BigInteger( new Integer((1<<15)-1)
176                 .toString() ).toByteArray();
177         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
178         assertEquals( (1<<15)-1, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
179 
180 
181         // --------------------------------------------------------------------
182         // THREE BYTES
183         // --------------------------------------------------------------------
184 
185         encoded = PrimitiveUtils.encodeInt( (1<<15) );
186         actual = new BigInteger( new Integer((1<<15))
187                 .toString() ).toByteArray();
188         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
189         assertEquals( (1<<15), PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
190 
191         encoded = PrimitiveUtils.encodeInt( (-(1<<15))-1 );
192         actual = new BigInteger( new Integer((-(1<<15))-1)
193                 .toString() ).toByteArray();
194         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
195         assertEquals( (-(1<<15))-1, PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
196 
197         encoded = PrimitiveUtils.encodeInt( (1<<15)+1000 );
198         actual = new BigInteger( new Integer((1<<15)+1000)
199                 .toString() ).toByteArray();
200         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
201         assertEquals( (1<<15)+1000, PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
202 
203         encoded = PrimitiveUtils.encodeInt( (-(1<<15))-1000 );
204         actual = new BigInteger( new Integer((-(1<<15))-1000)
205                 .toString() ).toByteArray();
206         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
207         assertEquals( (-(1<<15))-1000, PrimitiveUtils
208                 .decodeInt( encoded, 0, 3 ) );
209 
210         encoded = PrimitiveUtils.encodeInt( (1<<23)-1 );
211         actual = new BigInteger( new Integer((1<<23)-1)
212                 .toString() ).toByteArray();
213         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
214         assertEquals( (1<<23)-1, PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
215 
216         encoded = PrimitiveUtils.encodeInt( (-(1<<23) ));
217         actual = new BigInteger( new Integer(-(1<<23))
218                 .toString() ).toByteArray();
219         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
220         assertEquals( -(1<<23), PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
221 
222         // --------------------------------------------------------------------
223         // FOUR BYTES
224         // --------------------------------------------------------------------
225 
226         encoded = PrimitiveUtils.encodeInt( (1<<23) );
227         actual = new BigInteger( new Integer((1<<23))
228                 .toString() ).toByteArray();
229         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
230         assertEquals( (1<<23), PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
231 
232         encoded = PrimitiveUtils.encodeInt( (-(1<<23))-1 );
233         actual = new BigInteger( new Integer((-(1<<23))-1)
234                 .toString() ).toByteArray();
235         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
236         assertEquals( (-(1<<23))-1, PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
237 
238         encoded = PrimitiveUtils.encodeInt( (1<<23) + 10000 );
239         actual = new BigInteger( new Integer((1<<23) + 10000 )
240                 .toString() ).toByteArray();
241         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
242         assertEquals( (1<<23) + 10000,
243                 PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
244 
245         encoded = PrimitiveUtils.encodeInt( (-(1<<23))-10000 );
246         actual = new BigInteger( new Integer((-(1<<23))-10000 )
247                 .toString() ).toByteArray();
248         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
249         assertEquals( (-(1<<23))-10000,
250                 PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
251 
252         encoded = PrimitiveUtils.encodeInt( Integer.MAX_VALUE );
253         actual = new BigInteger( new Integer( Integer.MAX_VALUE )
254                 .toString() ).toByteArray();
255         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
256         assertEquals( Integer.MAX_VALUE,
257                 PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
258 
259         encoded = PrimitiveUtils.encodeInt( Integer.MIN_VALUE );
260         actual = new BigInteger( new Integer( Integer.MIN_VALUE )
261                 .toString() ).toByteArray();
262         assertTrue( ArrayUtils.isEquals( actual, encoded ) );
263         assertEquals( Integer.MIN_VALUE,
264                 PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
265 
266         for ( int ii = 0; ii < values.length; ii++ )
267         {
268             encoded = PrimitiveUtils.encodeInt( values[ii] ) ;
269             assertTrue( ArrayUtils.isEquals( byteArrays[ii], encoded ) );
270         }
271     }
272 }