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  import java.util.Arrays;
25  
26  import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
27  import org.apache.directory.shared.ldap.util.StringTools;
28  
29  import junit.framework.TestCase;
30  
31  
32  /**
33   * Test the EncryptedData encoding and decoding
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev: 542147 $, $Date: 2007-05-28 10:14:21 +0200 (Mon, 28 May 2007) $
37   */
38  public class EncryptedDataTest extends TestCase
39  {
40      public void testEncodingEncryptedData() throws Exception
41      {
42          EncryptedData ed = new EncryptedData( EncryptionType.AES128_CTS_HMAC_SHA1_96, 1, new byte[]
43              { 0x01, 0x02, 0x03, 0x04 } );
44  
45          ByteBuffer encoded = ByteBuffer.allocate( ed.computeLength() );
46  
47          ed.encode( encoded );
48  
49          byte[] expectedResult = new byte[]
50              { 
51                  0x30, 0x12, 
52                    ( byte ) 0xA0, 0x03, 
53                      0x02, 0x01, 0x11, 
54                    ( byte ) 0xA1, 0x03, 
55                      0x02, 0x01, 0x01, 
56                    ( byte ) 0xA2, 0x06, 
57                      0x04, 0x04, 0x01, 0x02, 0x03, 0x04 
58              };
59  
60          assertEquals( StringTools.dumpBytes( expectedResult ), StringTools.dumpBytes( encoded.array() ) );
61      }
62  
63  
64      public void testEncodingEncryptedDataNullCipher() throws Exception
65      {
66          EncryptedData ed = new EncryptedData( EncryptionType.AES128_CTS_HMAC_SHA1_96, 1, null );
67  
68          ByteBuffer encoded = ByteBuffer.allocate( ed.computeLength() );
69  
70          ed.encode( encoded );
71  
72          byte[] expectedResult = new byte[]
73              { 
74                0x30, 0x0E, 
75                  ( byte ) 0xA0, 0x03, 
76                    0x02, 0x01, 0x11, 
77                  ( byte ) 0xA1, 0x03, 
78                    0x02, 0x01, 0x01, 
79                  ( byte ) 0xA2, 0x02, 
80                    0x04, 0x00 
81              };
82  
83          assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
84      }
85  
86  
87      public void testEncodingEncryptedDataNoKvno() throws Exception
88      {
89          EncryptedData ed = new EncryptedData( EncryptionType.AES128_CTS_HMAC_SHA1_96, new byte[]
90              { 0x01, 0x02, 0x03, 0x04 } );
91  
92          ByteBuffer encoded = ByteBuffer.allocate( ed.computeLength() );
93  
94          ed.encode( encoded );
95  
96          byte[] expectedResult = new byte[]
97              { 
98                0x30, 0x0D, 
99                  ( byte ) 0xA0, 0x03, 
100                   0x02, 0x01, 0x11, 
101                 ( byte ) 0xA2, 0x06, 
102                   0x04, 0x04, 0x01, 0x02, 0x03, 0x04 
103             };
104 
105         assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
106     }
107 
108 
109     public void testEncodingEncryptedDataNoKvnoNullCipher() throws Exception
110     {
111         EncryptedData ed = new EncryptedData( EncryptionType.AES128_CTS_HMAC_SHA1_96, null );
112 
113         ByteBuffer encoded = ByteBuffer.allocate( ed.computeLength() );
114 
115         ed.encode( encoded );
116 
117         byte[] expectedResult = new byte[]
118             { 
119               0x30, 0x09, 
120                 ( byte ) 0xA0, 0x03, 
121                   0x02, 0x01, 0x11, 
122                 ( byte ) 0xA2, 0x02, 
123                   0x04, 0x00 
124             };
125 
126         assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
127     }
128 }