001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.auth.tests.javax.security.auth.kerberos;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.ObjectOutputStream;
023: import java.util.Arrays;
024:
025: import javax.security.auth.kerberos.KerberosKey;
026: import javax.security.auth.kerberos.KerberosPrincipal;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * Tests KerberosKey class implementation.
032: *
033: * @see http://www.ietf.org/rfc/rfc3961.txt
034: */
035: public class KerberosKeyTest extends TestCase {
036:
037: // principal object for testing
038: private final KerberosPrincipal principal = new KerberosPrincipal(
039: "name@aaa.com", 1);
040:
041: // byte array for testing
042: private final byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02,
043: 0x03, 0x04, 0x05, 0x06, 0x07 };
044:
045: /**
046: * @tests javax.security.auth.kerberos.KerberosKey#KerberosKey(
047: * javax.security.auth.kerberos.KerberosPrincipal, byte[], int, int)
048: */
049: public void test_Ctor1() {
050:
051: // OK to pass null value for principal parameter
052: assertNull(new KerberosKey(null, keyBytes, 0, 0).getPrincipal());
053:
054: // NPE for null keyBytes parameter
055: try {
056: new KerberosKey(principal, null, 0, 0);
057: fail("No expected NullPointerException");
058: } catch (NullPointerException e) {
059: }
060:
061: // construct with DES algorithm
062: KerberosKey key = new KerberosKey(principal, keyBytes, 1, 123);
063: assertEquals("DES algorithm", "DES", key.getAlgorithm());
064: assertEquals("version number", 123, key.getVersionNumber());
065: assertEquals("format", "RAW", key.getFormat());
066: assertSame("principal", principal, key.getPrincipal());
067: assertFalse("is destroyed", key.isDestroyed());
068:
069: // construct with NULL algorithm
070: key = new KerberosKey(principal, keyBytes, 0, 0);
071: assertEquals("NULL algorithm", "NULL", key.getAlgorithm());
072: assertEquals("version number", 0, key.getVersionNumber());
073: }
074:
075: /**
076: * @tests javax.security.auth.kerberos.KerberosKey#KerberosKey(
077: * javax.security.auth.kerberos.KerberosPrincipal, char[],
078: * java.lang.String)
079: */
080: public void test_Ctor2() {
081:
082: // NPE for null value for principal parameter
083: try {
084: new KerberosKey(null, new char[10], "DES");
085: fail("No expected NullPointerException");
086: } catch (NullPointerException e) {
087: }
088:
089: // NPE for null password value
090: try {
091: new KerberosKey(principal, null, "DES");
092: fail("No expected NullPointerException");
093: } catch (NullPointerException e) {
094: }
095:
096: // IAE for unsupported algorithm
097: try {
098: new KerberosKey(principal, new char[10],
099: "there_is_no_such_algorithm");
100: fail("No expected IllegalArgumentException");
101: } catch (IllegalArgumentException e) {
102: }
103:
104: // if algorithm parameter is null then DES is used
105: KerberosKey key = new KerberosKey(principal, new char[10], null);
106:
107: assertEquals("algorithm", "DES", key.getAlgorithm());
108: assertEquals("format", "RAW", key.getFormat());
109: assertEquals("key type", 3, key.getKeyType());
110: assertEquals("version number", 0, key.getVersionNumber());
111: assertFalse("is destroyed", key.isDestroyed());
112: assertSame("principal", principal, key.getPrincipal());
113: }
114:
115: /**
116: * @tests javax.security.auth.kerberos.KerberosKey#getEncoded()
117: */
118: public void test_getEncoded() {
119:
120: KerberosKey key = new KerberosKey(principal, keyBytes, 1, 123);
121:
122: byte[] keyBytes1 = key.getEncoded();
123: assertTrue("encoded", Arrays.equals(keyBytes, keyBytes1));
124:
125: // bytes are copied each time we invoke the method
126: assertNotSame("keyBytes immutability 1 ", keyBytes, keyBytes1);
127: assertNotSame("keyBytes immutability 2 ", keyBytes1, key
128: .getEncoded());
129:
130: // Test generation of DES key from password
131: // test data from RFC 3961 (http://www.ietf.org/rfc/rfc3961.txt)
132: // see A.2 test vectors
133: // test data format: principal/password/DES key
134: Object[][] testcases = {
135: {
136: "raeburn@ATHENA.MIT.EDU",
137: "password",
138: new byte[] { (byte) 0xcb, (byte) 0xc2,
139: (byte) 0x2f, (byte) 0xae, (byte) 0x23,
140: (byte) 0x52, (byte) 0x98, (byte) 0xe3 } },
141: {
142: "danny@WHITEHOUSE.GOV",
143: "potatoe",
144: new byte[] { (byte) 0xdf, (byte) 0x3d,
145: (byte) 0x32, (byte) 0xa7, (byte) 0x4f,
146: (byte) 0xd9, (byte) 0x2a, (byte) 0x01 } },
147: // TODO add "pianist@EXAMPLE.COM" and "Juri ... @ATHENA.MIT.EDU"
148: };
149:
150: for (Object[] element : testcases) {
151: KerberosPrincipal kp = new KerberosPrincipal(
152: (String) element[0], 1);
153:
154: key = new KerberosKey(kp, ((String) element[1])
155: .toCharArray(), "DES");
156:
157: assertTrue("Testcase: " + (String) element[0], Arrays
158: .equals((byte[]) element[2], key.getEncoded()));
159: }
160: }
161:
162: /**
163: * @tests javax.security.auth.kerberos.KerberosKey#destroy()
164: */
165: public void test_destroy() throws Exception {
166:
167: KerberosKey key = new KerberosKey(principal, new char[10],
168: "DES");
169:
170: assertFalse("not destroyed", key.isDestroyed());
171:
172: key.destroy();
173: assertTrue("destroyed", key.isDestroyed());
174:
175: // no exceptions for second destroy() call
176: key.destroy();
177:
178: // check that IllegalStateException is thrown for certain methods
179: try {
180: key.getAlgorithm();
181: fail("No expected IllegalStateException");
182: } catch (IllegalStateException e) {
183: }
184:
185: try {
186: key.getEncoded();
187: fail("No expected IllegalStateException");
188: } catch (IllegalStateException e) {
189: }
190:
191: try {
192: key.getFormat();
193: fail("No expected IllegalStateException");
194: } catch (IllegalStateException e) {
195: }
196:
197: try {
198: key.getKeyType();
199: fail("No expected IllegalStateException");
200: } catch (IllegalStateException e) {
201: }
202:
203: try {
204: key.getPrincipal();
205: fail("No expected IllegalStateException");
206: } catch (IllegalStateException e) {
207: }
208:
209: try {
210: key.getVersionNumber();
211: fail("No expected IllegalStateException");
212: } catch (IllegalStateException e) {
213: }
214:
215: try {
216: // but for serialization IOException is expected
217: ObjectOutputStream out = new ObjectOutputStream(
218: new ByteArrayOutputStream());
219: out.writeObject(key);
220: fail("No expected IOException");
221: } catch (IOException e) {
222: }
223:
224: try {
225: key.toString();
226: fail("No expected IllegalStateException");
227: } catch (IllegalStateException e) {
228: }
229: }
230: }
|