1   /*
2    *   Copyright 2005 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.asn1new.primitives;
18  
19  import junit.framework.Assert;
20  import junit.framework.TestCase;
21  
22  import org.apache.asn1new.ber.tlv.Value;
23  import org.apache.asn1new.util.IntegerDecoder;
24  import org.apache.asn1new.util.IntegerDecoderException;
25  import org.apache.log4j.PropertyConfigurator;
26  
27  /***
28   * Test the Primitives
29   *
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public class PrimitivesTest extends TestCase
34  {
35      //~ Static fields/initializers -----------------------------------------------------------------
36  
37      static
38      {
39          PropertyConfigurator.configure( "conf/log4j.conf" );
40      }
41  
42      //~ Methods ------------------------------------------------------------------------------------
43  
44      /***
45       * Test the Integer Primitive
46       */
47      public void testIntegerPrimitive() throws IntegerDecoderException
48      {
49          Value value = new Value();
50  
51          value.init(1);
52          value.setData( new byte[] { 0x00 } ); // res = 0
53          Assert.assertEquals( 0, IntegerDecoder.parse( value ) );
54          value.reset();
55  
56          value.init(1);
57          value.setData( new byte[] { 0x01 } ); // res = 1
58          Assert.assertEquals( 1, IntegerDecoder.parse( value ) );
59          value.reset();
60  
61          value.init(1);
62          value.setData( new byte[] { ( byte ) 0xFF } ); // res = 255
63          Assert.assertEquals( -1, IntegerDecoder.parse( value ) );
64          value.reset();
65  
66          value.init(2);
67          value.setData( new byte[] { 0x00, 0x01 } ); // res = 1
68          Assert.assertEquals( 1, IntegerDecoder.parse( value ) );
69          value.reset();
70  
71          value.init(2);
72          value.setData( new byte[] { 0x01, 0x00 } ); // res = 256
73          Assert.assertEquals( 256, IntegerDecoder.parse( value ) );
74          value.reset();
75  
76          value.init(2);
77          value.setData( new byte[] { 0x01, 0x01 } ); // res = 257
78          Assert.assertEquals( 257, IntegerDecoder.parse( value ) );
79          value.reset();
80  
81          value.init(2);
82          value.setData( new byte[] { 0x01, ( byte ) 0xFF } ); // res = 511
83          Assert.assertEquals( 511, IntegerDecoder.parse( value ) );
84          value.reset();
85  
86          value.init(2);
87          value.setData( new byte[] { 0x02, 0x00 } ); // res = 512
88          Assert.assertEquals( 512, IntegerDecoder.parse( value ) );
89          value.reset();
90  
91          value.init(3);
92          value.setData( new byte[] { 0x00, ( byte ) 0xFF, ( byte ) 0xFF } ); // res = 65535
93          Assert.assertEquals( 65535, IntegerDecoder.parse( value ) );
94          value.reset();
95  
96          value.init(4);
97          value.setData(
98              new byte[] { ( byte ) 0x7F, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF } ); // res = 2^31 - 1 = MaxInt
99          Assert.assertEquals( Integer.MAX_VALUE, IntegerDecoder.parse( value ) );
100         value.reset();
101 
102         value.init(4);
103         value.setData(
104             new byte[] { ( byte ) 0x80, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00 } ); // res = 2^31 = MinInt
105         Assert.assertEquals( Integer.MIN_VALUE, IntegerDecoder.parse( value ) );
106         value.reset();
107     }
108 } // end class TLVTagDecoderTest