1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.kerberos.shared.messages.value;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.server.kerberos.shared.messages.value.types.PrincipalNameType;
26  import org.apache.directory.shared.ldap.util.StringTools;
27  
28  import junit.framework.TestCase;
29  
30  
31  /**
32   * Test the PrincipalName encoding and decoding
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev: 542147 $, $Date: 2007-05-28 10:14:21 +0200 (Mon, 28 May 2007) $
36   */
37  public class PrincipalNameTest extends TestCase
38  {
39      public void testEncodingPrincipalNameOneName() throws Exception
40      {
41          PrincipalName principal = new PrincipalName( "Test@APACHE", PrincipalNameType.KRB_NT_PRINCIPAL );
42  
43          ByteBuffer encoded = ByteBuffer.allocate( principal.computeLength() );
44  
45          principal.encode( encoded );
46  
47          byte[] expectedResult = new byte[]
48              { 
49                0x30, 0x0F, 
50                  (byte) 0xA0, 0x03, 
51                    0x02, 0x01, 0x01, 
52                  (byte) 0xA1, 0x08, 
53                    0x30, 0x06, 
54                      0x1B, 0x04, 
55                        'T', 'e', 's', 't' 
56              };
57  
58          assertEquals( StringTools.dumpBytes( expectedResult ), StringTools.dumpBytes( encoded.array() ) );
59      }
60  
61  
62      public void testEncodingPrincipalName3names() throws Exception
63      {
64          PrincipalName principal = new PrincipalName( "Test1@APACHE", PrincipalNameType.KRB_NT_PRINCIPAL );
65          principal.addName( "Test2" );
66          principal.addName( "Test3" );
67  
68          ByteBuffer encoded = ByteBuffer.allocate( principal.computeLength() );
69  
70          principal.encode( encoded );
71  
72          byte[] expectedResult = new byte[]
73              { 
74                0x30, 0x1e, 
75                  (byte) 0xA0, 0x03, 
76                    0x02, 0x01, 0x01, 
77                  (byte) 0xA1, 0x17, 
78                    0x30, 0x15, 
79                      0x1B, 0x05, 
80                        'T', 'e', 's', 't', '1', 
81                      0x1B, 0x05, 
82                        'T', 'e', 's', 't', '2', 
83                      0x1B, 0x05, 
84                        'T', 'e', 's', 't', '3' 
85              };
86  
87          assertEquals( StringTools.dumpBytes( expectedResult ), StringTools.dumpBytes( encoded.array() ) );
88      }
89  
90  
91      public void testEncodingPrincipalNameNullName() throws Exception
92      {
93          PrincipalName principal = new PrincipalName( null, PrincipalNameType.KRB_NT_PRINCIPAL );
94  
95          ByteBuffer encoded = ByteBuffer.allocate( principal.computeLength() );
96  
97          principal.encode( encoded );
98  
99          byte[] expectedResult = new byte[]
100             { 
101               0x30, 0x09, 
102                 ( byte ) 0xA0, 0x03, 
103                   0x02, 0x01, 0x01, 
104                 ( byte ) 0xA1, 0x02, 
105                   0x30, 0x00 
106             };
107 
108         assertEquals( StringTools.dumpBytes( expectedResult ), StringTools.dumpBytes( encoded.array() ) );
109     }
110 }