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  
21  package org.apache.directory.server.core.security;
22  
23  
24  import static org.junit.Assert.*;
25  
26  import java.security.KeyPair;
27  import java.security.cert.X509Certificate;
28  import java.util.HashSet;
29  import java.util.Set;
30  
31  import org.apache.directory.server.core.entry.DefaultServerEntry;
32  import org.apache.directory.server.schema.bootstrap.ApacheSchema;
33  import org.apache.directory.server.schema.bootstrap.ApachemetaSchema;
34  import org.apache.directory.server.schema.bootstrap.BootstrapSchemaLoader;
35  import org.apache.directory.server.schema.bootstrap.CoreSchema;
36  import org.apache.directory.server.schema.bootstrap.CosineSchema;
37  import org.apache.directory.server.schema.bootstrap.InetorgpersonSchema;
38  import org.apache.directory.server.schema.bootstrap.Schema;
39  import org.apache.directory.server.schema.bootstrap.SystemSchema;
40  import org.apache.directory.server.schema.registries.DefaultOidRegistry;
41  import org.apache.directory.server.schema.registries.DefaultRegistries;
42  import org.apache.directory.server.schema.registries.OidRegistry;
43  import org.apache.directory.server.schema.registries.Registries;
44  import org.apache.directory.shared.ldap.constants.SchemaConstants;
45  import org.apache.directory.shared.ldap.name.LdapDN;
46  import org.junit.BeforeClass;
47  import org.junit.Test;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  
52  /**
53   * Test for the TlsKeyGenerator class.
54   *
55   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
56   * @version $Rev$, $Date$
57   */
58  public class TlsKeyGeneratorTest
59  {
60      private static final Logger LOG = LoggerFactory.getLogger( TlsKeyGeneratorTest.class );
61      private static BootstrapSchemaLoader loader;
62      private static Registries registries;
63      private static OidRegistry oidRegistry;
64      
65  
66      /**
67       * Initialize the registries once for the whole test suite
68       */
69      @BeforeClass
70      public static void setup() throws Exception
71      {
72          loader = new BootstrapSchemaLoader();
73          oidRegistry = new DefaultOidRegistry();
74          registries = new DefaultRegistries( "bootstrap", loader, oidRegistry );
75          
76          // load essential bootstrap schemas 
77          Set<Schema> bootstrapSchemas = new HashSet<Schema>();
78          bootstrapSchemas.add( new ApachemetaSchema() );
79          bootstrapSchemas.add( new ApacheSchema() );
80          bootstrapSchemas.add( new CoreSchema() );
81          bootstrapSchemas.add( new SystemSchema() );
82          bootstrapSchemas.add( new InetorgpersonSchema() );
83          bootstrapSchemas.add( new CosineSchema() );
84          loader.loadWithDependencies( bootstrapSchemas, registries );
85          
86      }
87      
88      
89      /**
90       * Test method for all methods in one.
91       */
92      @Test
93      public void testAll() throws Exception
94      {
95          DefaultServerEntry entry = new DefaultServerEntry( registries, new LdapDN() );
96          TlsKeyGenerator.addKeyPair( entry );
97          LOG.debug( "Entry: {}", entry );
98          assertTrue( entry.contains( SchemaConstants.OBJECT_CLASS_AT, TlsKeyGenerator.TLS_KEY_INFO_OC ) );
99          
100         KeyPair keyPair = TlsKeyGenerator.getKeyPair( entry );
101         assertNotNull( keyPair );
102         
103         X509Certificate cert = TlsKeyGenerator.getCertificate( entry );
104         assertNotNull( cert );
105     }
106 }