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 junit.framework.TestCase;
21  
22  import java.nio.ByteBuffer;
23  import java.util.Arrays;
24  
25  
26  /***
27   * Testing out round trip encode decode.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">
30   * Apache Directory Project</a>
31   * @version $Rev: 158144 $
32   */
33  public class EncodeDecodeTests extends TestCase
34  {
35      public void testAbandonRequest() throws Exception
36      {
37          byte[] pdu = {0x30, 0x06, 0x02, 0x01, 0x01, 0x50, 0x01, 0x03};
38  
39          DefaultMutableTupleNode root = ( DefaultMutableTupleNode )
40              TupleTreeDecoder.treeDecode( ByteBuffer.wrap( pdu ) ) ;
41          
42          ByteBuffer buf = ByteBuffer.allocate( root.size() ) ;
43          root.encode( buf ) ;
44          buf.flip();
45          byte[] actual = new byte[buf.remaining()] ;
46          buf.get( actual ) ;
47  
48          assertTrue( Arrays.equals( pdu, actual ) );
49      }
50  
51  
52      public void testBindRequest() throws Exception
53      {
54          byte[] pdu = {0x30, 0x33, 0x02, 0x01, 0x01, 0x60, 0x2E, 0x02, 0x01, 0x03, 0x04, 0x1F, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6B, 0x61, 0x72, 0x61, 0x73, 0x75, 0x6C, 0x75, 0x2C, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D, 0xFFFFFF80, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64};
55  
56           DefaultMutableTupleNode root =
57                   (DefaultMutableTupleNode) TupleTreeDecoder.treeDecode( ByteBuffer.wrap( pdu ) );
58  
59          ByteBuffer buf = ByteBuffer.allocate( root.size() ) ;
60          root.encode( buf ) ;
61          buf.flip() ;
62          byte[] actual = new byte[buf.remaining()] ;
63          buf.get( actual ) ;
64  
65          assertTrue( Arrays.equals( pdu, actual ) );
66      }
67  }