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.asn1.codec.DecoderException;
23  
24  
25  /***
26   * Test the Bit String primitive
27   *
28   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
29   */
30  public class BitStringTest extends TestCase
31  {
32      //~ Methods ------------------------------------------------------------------------------------
33  
34      /***
35       * Test a null BitString
36       */
37      public void testBitStringNull()
38      {
39  
40          BitString bitString = new BitString();
41  
42          bitString.setData( null );
43  
44          try
45          {
46              bitString.getBit( 0 );
47              Assert.fail( "Should not reach this point ..." );
48          }
49          catch ( DecoderException de )
50          {
51              Assert.assertTrue( true );
52          }
53      }
54  
55      /***
56       * Test an empty BitString
57       */
58      public void testBitStringEmpty()
59      {
60  
61          BitString bitString = new BitString();
62  
63          bitString.setData( new byte[] {} );
64  
65          try
66          {
67              bitString.getBit( 0 );
68              Assert.fail( "Should not reach this point ..." );
69          }
70          catch ( DecoderException de )
71          {
72              Assert.assertTrue( true );
73          }
74      }
75  
76      /***
77       * Test a single bit BitString BitString
78       */
79      public void testSingleBitBitString() throws DecoderException
80      {
81  
82          BitString bitString = new BitString( 1 );
83  
84          bitString.setData( new byte[] { 0x07, ( byte ) 0x80 } );
85  
86          Assert.assertEquals( true, bitString.getBit( 0 ) );
87      }
88  
89      /***
90       * Test a 32 bits BitString
91       */
92      public void test32BitsBitString() throws DecoderException
93      {
94  
95          BitString bitString = new BitString( 32 );
96  
97          bitString.setData(
98              new byte[] { 0x00, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF } );
99  
100         for ( int i = 0; i < 32; i++ )
101         {
102             Assert.assertEquals( true, bitString.getBit( i ) );
103         }
104     }
105 
106     /***
107      * Test a 33 bits BitString
108      */
109     public void test33BitsBitString() throws DecoderException
110     {
111 
112         BitString bitString = new BitString( 33 );
113 
114         bitString.setData(
115             new byte[]
116             {
117                 0x07, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0x80
118             } );
119 
120         for ( int i = 0; i < 33; i++ )
121         {
122             Assert.assertEquals( true, bitString.getBit( i ) );
123         }
124 
125         Assert.assertEquals( true, bitString.getBit( 32 ) );
126     }
127 
128 
129     /***
130      * Test all bits from 0 to 128 BitString
131      */
132     public void test0to128BitString() throws DecoderException
133     {
134 
135         // bit number 14
136         BitString bitString14 = new BitString( 14 );
137 
138         bitString14.setData( new byte[] { 0x02, ( byte ) 0xFF, ( byte ) 0xFC } );
139 
140         for ( int i = 0; i < 14; i++ )
141         {
142             Assert.assertEquals( true, bitString14.getBit( i ) );
143         }
144 
145         // bit number 31
146         BitString bitString31 = new BitString( 31 );
147 
148         bitString31.setData(
149             new byte[] { 0x01, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFE } );
150 
151         for ( int i = 0; i < 31; i++ )
152         {
153             Assert.assertEquals( true, bitString31.getBit( i ) );
154         }
155 
156         // bit number 128
157         BitString bitString128 = new BitString( 128 );
158 
159         bitString128.setData(
160             new byte[]
161             {
162                 0x00, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
163                 ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
164                 ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
165                 ( byte ) 0xFF
166             } );
167 
168         for ( int i = 0; i < 128; i++ )
169         {
170             Assert.assertEquals( true, bitString128.getBit( i ) );
171         }
172     }
173 }