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 OID primitive
27   *
28   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
29   */
30  public class OIDTest extends TestCase
31  {
32      //~ Methods ------------------------------------------------------------------------------------
33  
34      /***
35       * Test a null OID
36       */
37      public void testOidNull()
38      {
39  
40          OID oid = new OID();
41  
42          try
43          {
44              oid.setOID( ( byte[] ) null );
45              Assert.fail( "Should not reach this point ..." );
46          }
47          catch ( DecoderException de )
48          {
49              Assert.assertTrue( true );
50          }
51      }
52  
53      /***
54       * Test an empty OID
55       */
56      public void testOidEmpty()
57      {
58  
59          OID oid = new OID();
60  
61          try
62          {
63              oid.setOID( new byte[] {} );
64              Assert.fail( "Should not reach this point ..." );
65          }
66          catch ( DecoderException de )
67          {
68              Assert.assertTrue( true );
69          }
70      }
71  
72      /***
73       * Test itu-t OID tree
74       */
75      public void testOidItuT()
76      {
77  
78          OID oid = new OID();
79  
80          try
81          {
82  
83              // itu-t(0), recommendation(0), series a-z (0..26)
84              for ( int i = 1; i < 27; i++ )
85              {
86                  oid.setOID( new byte[] { 0x00, ( byte ) i } );
87                  Assert.assertEquals( "0.0." + i, oid.toString() );
88              }
89  
90              // itu-t(0), question(1)
91              oid.setOID( new byte[] { 0x01 } );
92              Assert.assertEquals( "0.1", oid.toString() );
93  
94              // itu-t(0), administration(2), country(202 .. 748)
95              for ( int i = 202; i < 748; i++ )
96              {
97                  oid.setOID(
98                      new byte[] { 0x02, ( byte ) ( ( i / 128 ) | 0x0080 ), ( byte ) ( i % 128 ) } );
99                  Assert.assertEquals( "0.2." + i, oid.toString() );
100             }
101 
102             // itu-t(0), network-operator(3), operator(2023 .. 41363)
103             for ( int i = 2023; i < 41363; i++ )
104             {
105 
106                 if ( i < ( 128 * 128 ) )
107                 {
108                     oid.setOID(
109                         new byte[] { 0x03, ( byte ) ( ( i / 128 ) | 0x0080 ), ( byte ) ( i % 128 ) } );
110                     Assert.assertEquals( "0.3." + i, oid.toString() );
111                 }
112                 else
113                 {
114                     oid.setOID(
115                         new byte[]
116                         {
117                             0x03, ( byte ) ( ( i / ( 128 * 128 ) ) | 0x0080 ),
118                             ( byte ) ( ( ( i / 128 ) % 128 ) | 0x0080 ), ( byte ) ( i % 128 )
119                         } );
120                     Assert.assertEquals( "0.3." + i, oid.toString() );
121 
122                 }
123             }
124         }
125         catch ( DecoderException de )
126         {
127             Assert.fail();
128         }
129     }
130 
131     /***
132      * Test iso OID tree
133      */
134     public void testOidIso()
135     {
136 
137         OID oid = new OID();
138 
139         try
140         {
141 
142             // iso(1), standard(0)
143             oid.setOID( new byte[] { 40 + 0 } );
144             Assert.assertEquals( "1.0", oid.toString() );
145 
146             // iso(1), registration-authority(1)
147             oid.setOID( new byte[] { 40 + 1 } );
148             Assert.assertEquals( "1.1", oid.toString() );
149 
150             // iso(1), member-body(2)
151             oid.setOID( new byte[] { 40 + 2 } );
152             Assert.assertEquals( "1.2", oid.toString() );
153 
154             // iso(1), identified-organization(3) | org(3) | organization(3)
155             oid.setOID( new byte[] { 40 + 3 } );
156             Assert.assertEquals( "1.3", oid.toString() );
157         }
158         catch ( DecoderException de )
159         {
160             Assert.fail();
161         }
162     }
163 
164     /***
165      * Test joint-iso-itu-t OID tree
166      */
167     public void testOidJointIsoItuT()
168     {
169 
170         OID oid = new OID();
171 
172         try
173         {
174 
175             // joint-iso-itu-t(2), presentation(0)
176             oid.setOID( new byte[] { 80 + 0 } );
177             Assert.assertEquals( "2.0", oid.toString() );
178 
179             // joint-iso-itu-t(2), asn1(1)
180             oid.setOID( new byte[] { 80 + 1 } );
181             Assert.assertEquals( "2.1", oid.toString() );
182 
183             // joint-iso-itu-t(2), association-control(2)
184             oid.setOID( new byte[] { 80 + 2 } );
185             Assert.assertEquals( "2.2", oid.toString() );
186 
187             // joint-iso-itu-t(2), reliable-transfer(3)
188             oid.setOID( new byte[] { 80 + 3 } );
189             Assert.assertEquals( "2.3", oid.toString() );
190 
191             // ...
192             // joint-iso-itu-t(2), upu(40)
193             oid.setOID( new byte[] { 80 + 40 } );
194             Assert.assertEquals( "2.40", oid.toString() );
195 
196             // ...
197             // joint-iso-itu-t(2), xxx(100)
198             oid.setOID( new byte[] { ( byte ) ( 0x81 ), 0x34 } );
199             Assert.assertEquals( "2.100", oid.toString() );
200         }
201         catch ( DecoderException de )
202         {
203             Assert.fail();
204         }
205     }
206 
207     /***
208      * Test valid String OIDs
209      */
210     public void testOidStringGood()
211     {
212 
213         OID oid = new OID();
214 
215         try
216         {
217             oid.setOID( "0.0" );
218             Assert.assertEquals( "0.0", oid.toString() );
219 
220             oid.setOID( "0.0.0.0.0" );
221             Assert.assertEquals( "0.0.0.0.0", oid.toString() );
222 
223             oid.setOID( "0.1.2.3.4" );
224             Assert.assertEquals( "0.1.2.3.4", oid.toString() );
225 
226             oid.setOID( "2.123456" );
227             Assert.assertEquals( "2.123456", oid.toString() );
228 
229             oid.setOID( "1.2.840.113554.1.2.2" );
230             Assert.assertEquals( "1.2.840.113554.1.2.2", oid.toString() );
231         }
232         catch ( DecoderException de )
233         {
234             Assert.fail();
235         }
236     }
237 
238     /***
239      * Test invalid String OIDs
240      */
241     public void testOidStringBad()
242     {
243 
244         OID oid = new OID();
245 
246         try
247         {
248             oid.setOID( "0" );
249         }
250         catch ( DecoderException de )
251         {
252             Assert.assertTrue( true );
253         }
254 
255         try
256         {
257             oid.setOID( "0." );
258         }
259         catch ( DecoderException de )
260         {
261             Assert.assertTrue( true );
262         }
263 
264         try
265         {
266             oid.setOID( "." );
267         }
268         catch ( DecoderException de )
269         {
270             Assert.assertTrue( true );
271         }
272 
273         try
274         {
275             oid.setOID( "0.1.2." );
276         }
277         catch ( DecoderException de )
278         {
279             Assert.assertTrue( true );
280         }
281 
282         try
283         {
284             oid.setOID( "3.1" );
285         }
286         catch ( DecoderException de )
287         {
288             Assert.assertTrue( true );
289         }
290 
291         try
292         {
293             oid.setOID( "0..1" );
294         }
295         catch ( DecoderException de )
296         {
297             Assert.assertTrue( true );
298         }
299 
300         try
301         {
302             oid.setOID( "0..12" );
303         }
304         catch ( DecoderException de )
305         {
306             Assert.assertTrue( true );
307         }
308 
309         try
310         {
311             oid.setOID( "0.a.2" );
312         }
313         catch ( DecoderException de )
314         {
315             Assert.assertTrue( true );
316         }
317 
318         try
319         {
320             oid.setOID( "0.123456" );
321         }
322         catch ( DecoderException de )
323         {
324             Assert.assertTrue( true );
325         }
326 
327         try
328         {
329             oid.setOID( "1.123456" );
330         }
331         catch ( DecoderException de )
332         {
333             Assert.assertTrue( true );
334         }
335 
336     }
337 
338     /***
339      * Test Spnego OID
340      */
341     public void testOidSpnego()
342     {
343 
344         OID oid = new OID();
345 
346         try
347         {
348             oid.setOID( new byte[] { 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02 } );
349 
350             Assert.assertEquals( "1.3.6.1.5.5.2", oid.toString() );
351         }
352         catch ( DecoderException de )
353         {
354             Assert.fail();
355         }
356     }
357 
358     /***
359      * Test Kerberos V5 OID
360      */
361     public void testOidKerberosV5()
362     {
363 
364         OID oid = new OID();
365 
366         try
367         {
368             oid.setOID(
369                 new byte[]
370                 {
371                     0x2a, ( byte ) 0x86, 0x48, ( byte ) 0x86, ( byte ) 0xf7, 0x12, 0x01, 0x02,
372                     0x02
373                 } );
374 
375             Assert.assertEquals( "1.2.840.113554.1.2.2", oid.toString() );
376         }
377         catch ( DecoderException de )
378         {
379             Assert.fail();
380         }
381     }
382 
383     /***
384      * Test OIDs bytes
385      */
386     public void testOidBytes()
387     {
388         OID oid = new OID();
389         OID oid2 = new OID();
390 
391         try
392         {
393             oid.setOID( "0.0" );
394             oid2.setOID(oid.getOID());
395             Assert.assertEquals( oid.toString(), oid2.toString());
396 
397             oid.setOID( "0.0.0.0.0" );
398             oid2.setOID(oid.getOID());
399             Assert.assertEquals( oid.toString(), oid2.toString());
400 
401             oid.setOID( "0.1.2.3.4" );
402             oid2.setOID(oid.getOID());
403             Assert.assertEquals( oid.toString(), oid2.toString());
404 
405             oid.setOID( "2.123456" );
406             oid2.setOID(oid.getOID());
407             Assert.assertEquals( oid.toString(), oid2.toString());
408 
409             oid.setOID( "1.2.840.113554.1.2.2" );
410             oid2.setOID(oid.getOID());
411             Assert.assertEquals( oid.toString(), oid2.toString());
412         }
413         catch ( DecoderException de )
414         {
415             Assert.fail();
416         }
417     }
418 }