Author: erodriguez
Date: Thu Nov 4 15:24:39 2004
New Revision: 56633
Added:
incubator/directory/kerberos/trunk/kerberos/src/test/org/apache/kerberos/kdc/BootstrapStoreTest.java
Log:
Tests for the new auto-key creating bootstrap store.
Added:
incubator/directory/kerberos/trunk/kerberos/src/test/org/apache/kerberos/kdc/BootstrapStoreTest.java
==============================================================================
--- (empty file)
+++
incubator/directory/kerberos/trunk/kerberos/src/test/org/apache/kerberos/kdc/BootstrapStoreTest.java
Thu Nov 4 15:24:39 2004
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.kerberos.kdc;
+
+
+import junit.framework.TestCase;
+
+import javax.security.auth.kerberos.KerberosKey;
+import javax.security.auth.kerberos.KerberosPrincipal;
+import java.io.*;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import org.apache.kerberos.crypto.Confounder;
+import org.apache.kerberos.crypto.DesStringToKey;
+
+
+/**
+ * Document this class.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class BootstrapStoreTest extends TestCase
+{
+ public void testGetKdcConfiguration()
+ {
+ KdcConfiguration config = new KdcConfiguration();
+ assertNotNull( config.getKdcPrincipal().getName() );
+ assertNotNull( config.getKerberosKeysLocation() );
+ }
+
+ public void testBootstrapKeysDontExist()
+ {
+ KdcConfiguration config = new KdcConfiguration();
+ File tempBootstrapStore = new File(config.getKerberosKeysLocation());
+ if (tempBootstrapStore.exists()) {
+ tempBootstrapStore.delete();
+ }
+ assertFalse( tempBootstrapStore.exists() );
+ }
+
+ public void testKeyStoreCreation() throws Exception
+ {
+ KdcConfiguration config = new KdcConfiguration();
+
+ Map entries = new HashMap();
+
+ addToMap(entries, makeRandomKeyFor(config.getKdcPrincipal()));
+
+ if ( config.getChangepwPrincipal() != null )
+ {
+ addToMap(entries,
makeRandomKeyFor(config.getChangepwPrincipal()));
+ }
+
+ if ( config.getLdapPrincipal() != null )
+ {
+ addToMap(entries, makePredefinedKey(5, "1cb96792580404f8",
config.getLdapPrincipal()));
+ }
+
+ FileOutputStream out = new
FileOutputStream(config.getKerberosKeysLocation());
+ ObjectOutputStream s = new ObjectOutputStream(out);
+ s.writeObject(entries);
+ s.flush();
+ }
+
+ public void testKeyStore() throws Exception
+ {
+ KdcConfiguration config = new KdcConfiguration();
+
+ FileInputStream in = new
FileInputStream(config.getKerberosKeysLocation());
+ ObjectInputStream s = new ObjectInputStream(in);
+ Map map = (HashMap)s.readObject();
+ Iterator it = map.values().iterator();
+ while (it.hasNext()) {
+ KerberosKey key = (KerberosKey)it.next();
+ assertNotNull( key.getEncoded() );
+ }
+ }
+
+ private KerberosKey makePredefinedKey(int keyVersion, String hexKey,
KerberosPrincipal principal)
+ {
+ final int DES_KEY_TYPE = 3;
+
+ byte[] bytes = TestUtils.getBytesFromHexString(hexKey);
+ return new KerberosKey(principal, bytes, DES_KEY_TYPE,
keyVersion);
+ }
+
+
+ private KerberosKey makeRandomKeyFor(KerberosPrincipal principal)
+ {
+ final int DES_KEY_TYPE = 3;
+ int keyVersion = 1;
+
+ byte[] randomBytes = Confounder.bytes(8);
+ DesStringToKey randomKey = new DesStringToKey(new
String(randomBytes));
+ return new KerberosKey(principal, randomKey.getKey(),
DES_KEY_TYPE, keyVersion);
+ }
+
+ private void addToMap(Map map, KerberosKey key)
+ {
+ map.put(key.getPrincipal().getName(), key);
+ }
+}
+