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.io.IOException;
24  import java.nio.ByteBuffer;
25  import java.util.Arrays;
26  
27  import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
28  import org.apache.directory.server.kerberos.shared.io.encoder.EncryptionKeyEncoder;
29  import org.apache.directory.shared.asn1.codec.EncoderException;
30  
31  import junit.framework.TestCase;
32  
33  
34  /**
35   * Test the EncryptionKey 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 EncryptionKeyTest extends TestCase
41  {
42      public void testEncodingFast() throws Exception
43      {
44          EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, new byte[]
45              { 0x01, 0x02, 0x03 } );
46  
47          ByteBuffer encoded = ByteBuffer.allocate( ec.computeLength() );
48  
49          ec.encode( encoded );
50  
51          byte[] expectedResult = new byte[]
52              { 
53                0x30, 0x0c, 
54                  ( byte ) 0xA0, 0x03, 
55                    0x02, 0x01, 0x11, 
56                  ( byte ) 0xA1, 0x05, 
57                    0x04, 0x03, 0x01, 0x02, 0x03 
58              };
59  
60          assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
61      }
62  
63  
64      public void testEncodingNoStructureFast() throws Exception
65      {
66          EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, null );
67  
68          ByteBuffer encoded = ByteBuffer.allocate( ec.computeLength() );
69  
70          ec.encode( encoded );
71  
72          byte[] expectedResult = new byte[]
73              { 
74                0x30, 0x09, 
75                  ( byte ) 0xA0, 0x03, 
76                    0x02, 0x01, 0x11, 
77                  ( byte ) 0xA1, 0x02, 
78                    0x04, 0x00 
79              };
80  
81          assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
82      }
83  
84  
85      /*
86       public void testEncodingNoStructureSlow() throws Exception
87       {
88       EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, null );
89       
90       byte[] encoded = EncryptionKeyEncoder.encode( ec );
91       
92       byte[] expectedResult = new byte[]
93       {
94       0x30, 0x09, 
95       (byte)0xA0, 0x03,
96       0x02, 0x01, 0x11,
97       (byte)0xA1, 0x02,
98       0x04, 0x00
99       };
100 
101      assertTrue( Arrays.equals( expectedResult, encoded ) );
102      }
103      */
104 
105     public void testEncodingSlow() throws Exception
106     {
107         EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, new byte[]
108             { 0x01, 0x02, 0x03 } );
109 
110         byte[] encoded = EncryptionKeyEncoder.encode( ec );
111 
112         byte[] expectedResult = new byte[]
113             { 
114               0x30, 0x0c, 
115                 ( byte ) 0xA0, 0x03, 
116                   0x02, 0x01, 0x11, 
117                 ( byte ) 0xA1, 0x05, 
118                   0x04, 0x03, 0x01, 0x02, 0x03 
119             };
120 
121         assertTrue( Arrays.equals( expectedResult, encoded ) );
122     }
123 
124 
125     public void testPerfSlow() throws IOException
126     {
127         EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, new byte[]
128             { 0x01, 0x02, 0x03 } );
129         EncryptionKeyEncoder.encode( ec );
130 
131         long t0 = System.currentTimeMillis();
132 
133         //for ( int i = 0; i < 10000000; i++ )
134         {
135             EncryptionKeyEncoder.encode( ec );
136         }
137 
138         long t1 = System.currentTimeMillis();
139 
140         System.out.println( "Delta = " + ( t1 - t0 ) );
141     }
142 
143 
144     public void testPerfFast() throws EncoderException
145     {
146         EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, new byte[]
147             { 0x01, 0x02, 0x03 } );
148         ByteBuffer encoded = ByteBuffer.allocate( ec.computeLength() );
149         ec.encode( encoded );
150 
151         long t0 = System.currentTimeMillis();
152 
153         //for ( int i = 0; i < 40000000; i++ )
154         {
155             encoded = ByteBuffer.allocate( ec.computeLength() );
156 
157             ec.encode( encoded );
158         }
159 
160         long t1 = System.currentTimeMillis();
161 
162         System.out.println( "Delta2 = " + ( t1 - t0 ) );
163     }
164 }