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.components;
21  
22  import java.nio.ByteBuffer;
23  import java.util.Arrays;
24  
25  import javax.security.auth.kerberos.KerberosPrincipal;
26  
27  import org.apache.directory.server.kerberos.shared.io.encoder.TicketEncoder;
28  import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
29  import org.apache.directory.server.kerberos.shared.store.TicketFactory;
30  import org.apache.directory.shared.ldap.util.StringTools;
31  
32  import junit.framework.TestCase;
33  
34  /**
35   * Test the Ticket encoding and decoding
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 542147 $, $Date: 2007-05-28 10:14:21 +0200 (Mon, 28 May 2007) $
39   */
40  public class TicketTest extends TestCase
41  {
42      public void testTicket() throws Exception
43      {
44          TicketFactory ticketFactory = new TicketFactory();
45  
46          KerberosPrincipal clientPrincipal = new KerberosPrincipal( "hnelson@EXAMPLE.COM" );
47          KerberosPrincipal serverPrincipal = new KerberosPrincipal( "kadmin/changepw@EXAMPLE.COM" );
48          String serverPassword = "s3crEt";
49  
50          EncryptionKey serverKey = ticketFactory.getServerKey( serverPrincipal, serverPassword );
51  
52          Ticket serviceTicket = ticketFactory.getTicket( clientPrincipal, serverPrincipal, serverKey );
53  
54          byte[] encodedTicket = TicketEncoder.encodeTicket( serviceTicket );
55          
56          ByteBuffer encoded = serviceTicket.encode( null );
57          
58          byte[] expectedResult = new byte[]
59              {
60                0x61, (byte)0x81, (byte)0xEF,
61                  0x30,  (byte)0x81, (byte)0xEC, 
62                    (byte)0xA0, 0x03,
63                      0x02, 0x01, 0x05,
64                    (byte)0xA1, 0x0D,
65                      0x1B, 0x0B, 
66                        'E', 'X', 'A', 'M', 'P', 'L', 'E', '.', 'C', 'O', 'M',
67                    (byte)0xA2, 0x1D,
68                      0x30, 0x1B,
69                        (byte)0xA0, 0x03, 
70                          0x02, 0x01, 0x01, 
71                        (byte)0xA1, 0x14, 
72                          0x30, 0x12, 
73                            0x1B, 0x06, 
74                              'k', 'a', 'd', 'm', 'i', 'n',
75                            0x1B, 0x08,
76                              'c', 'h', 'a', 'n', 'g', 'e', 'p', 'w',
77                    (byte)0xA3, (byte)0x81, (byte)0xB6, 
78                      0x30, (byte)0x81, (byte)0xB3,
79                        (byte)0xA0, 0x03,
80                          0x02, 0x01, 0x03,
81                        (byte)0xA2, (byte)0x81, (byte)0xAB,
82                          0x04, (byte)0x81, (byte)0xA8
83              };
84  
85          // We will just compared the first bytes (everyting before the encrypted data)
86          String expectedResultString = StringTools.dumpBytes( expectedResult );
87          String resultString = StringTools.dumpBytes( encoded.array() ).substring( 0,  expectedResultString.length() );
88          
89          assertEquals( expectedResultString, resultString );
90          assertTrue( Arrays.equals( encodedTicket, encodedTicket ) );
91      }
92  
93      /*
94      public void testTicketPerf() throws Exception
95      {
96          TicketFactory ticketFactory = new TicketFactory();
97  
98          KerberosPrincipal clientPrincipal = new KerberosPrincipal( "hnelson@EXAMPLE.COM" );
99          KerberosPrincipal serverPrincipal = new KerberosPrincipal( "kadmin/changepw@EXAMPLE.COM" );
100         String serverPassword = "s3crEt";
101 
102         EncryptionKey serverKey = ticketFactory.getServerKey( serverPrincipal, serverPassword );
103 
104         Ticket serviceTicket = ticketFactory.getTicket( clientPrincipal, serverPrincipal, serverKey );
105 
106         byte[] encodedTicket = TicketEncoder.encodeTicket( serviceTicket );
107         
108         long t0 = System.currentTimeMillis();
109         
110         for ( int i=0; i < 1000000; i++ )
111         {
112             TicketEncoder.encodeTicket( serviceTicket );
113         }
114         
115         long t1 = System.currentTimeMillis();
116         
117         System.out.println( "Delta slow = " + ( t1 - t0 ) );
118 
119         long t2 = System.currentTimeMillis();
120         
121         for ( int i=0; i < 1000000; i++ )
122         {
123             serviceTicket.encode( null );
124         }
125         
126         long t3 = System.currentTimeMillis();
127         
128         System.out.println( "Delta slow = " + ( t3 - t2 ) );
129 
130         assertTrue( Arrays.equals( encodedTicket, encodedTicket ) );
131     }
132     */
133 }