1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
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 }