/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.directory.server.ssl;


import org.apache.directory.server.ldap.LdapServer;
import org.apache.directory.server.ssl.support.SSLSocketFactory;
import org.apache.directory.server.unit.AbstractServerTest;
import org.apache.directory.shared.ldap.message.AttributeImpl;
import org.apache.directory.shared.ldap.message.AttributesImpl;
import org.apache.mina.util.AvailablePortFinder;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Hashtable;


/**
 * Test case to verify DIREVE-216.  Starts up the server binds via SUN JNDI provider
 * to perform add modify operations on entries.
 * 
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 * @version $Rev$
 */
public class LdapsITest extends AbstractServerTest
{
    private static final String RDN = "cn=The Person";

    private DirContext ctx;


    /**
     * Create an entry for a person.
     */
    public void setUp() throws Exception
    {
        super.setUp();

        int ldapsPort = AvailablePortFinder.getNextAvailable( 8192 );
        LdapServer ldapsServer = new LdapServer( socketAcceptor, directoryService );
        ldapsServer.setEnableLdaps( true );
        ldapsServer.setLdapsCertificatePassword( "boguspw" );
        ldapsServer.setIpPort( ldapsPort );

        // Copy the bogus certificate to the certificates directory.
        InputStream in = getClass().getResourceAsStream( "/bogus.cert" );
        ldapsServer.getLdapsCertificateFile().getParentFile().mkdirs();

        FileOutputStream out = new FileOutputStream( ldapsServer.getLdapsCertificateFile() );

        for ( ;; )
        {
            int c = in.read();
            if ( c < 0 )
            {
                break;
            }
            out.write( c );
        }

        in.close();
        out.close();


        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" );
        env.put( "java.naming.provider.url", "ldap://localhost:" + ldapsPort + "/ou=system" );
        env.put( "java.naming.ldap.factory.socket", SSLSocketFactory.class.getName() );
        env.put( "java.naming.security.principal", "uid=admin,ou=system" );
        env.put( "java.naming.security.credentials", "secret" );
        env.put( "java.naming.security.authentication", "simple" );
        ctx = new InitialDirContext( env );
    }


    /**
     * Remove the person.
     */
    public void tearDown() throws Exception
    {
        ctx.unbind( RDN );
        ctx.close();
        ctx = null;
        super.tearDown();
    }


    /**
     * Just a little test to check if the connection is made successfully.
     * 
     * @throws NamingException cannot create person
     */
    public void testSetUpTearDown() throws NamingException
    {
        // Create a person
        Attributes attributes = new AttributesImpl( true );
        Attribute attribute = new AttributeImpl( "objectClass" );
        attribute.add( "top" );
        attribute.add( "person" );
        attributes.put( attribute );
        attributes.put( "cn", "The Person" );
        attributes.put( "sn", "Person" );
        attributes.put( "description", "this is a person" );
        DirContext person = ctx.createSubcontext( RDN, attributes );

        assertNotNull( person );
    }
}

@@ -1486,32 +1486,60 @@
 io.InputStream;%0A
+import java.io.IOException;%0A
 import java.util
@@ -1550,16 +1550,16 @@
 htable;%0A
-
 %0A%0A/**%0A *
@@ -2056,24 +2056,26 @@
 r.setUp();%0A%0A
+//
         int 
@@ -2128,24 +2128,29 @@
 le( 8192 );%0A
+//%0A//
         Ldap
@@ -2226,686 +2226,8 @@
  );%0A
-        ldapsServer.setEnableLdaps( true );%0A        ldapsServer.setLdapsCertificatePassword( %22boguspw%22 );%0A        ldapsServer.setIpPort( ldapsPort );%0A%0A        // Copy the bogus certificate to the certificates directory.%0A        InputStream in = getClass().getResourceAsStream( %22/bogus.cert%22 );%0A        ldapsServer.getLdapsCertificateFile().getParentFile().mkdirs();%0A%0A        FileOutputStream out = new FileOutputStream( ldapsServer.getLdapsCertificateFile() );%0A%0A        for ( ;; )%0A        %7B%0A            int c = in.read();%0A            if ( c %3C 0 )%0A            %7B%0A                break;%0A            %7D%0A            out.write( c );%0A        %7D%0A%0A        in.close();%0A        out.close();%0A
 %0A%0A  
@@ -2450,22 +2450,17 @@
 ost:%22 + 
-ldapsP
+p
 ort + %22/
@@ -2814,32 +2814,945 @@
  env );%0A    %7D%0A%0A%0A
+    @Override%0A    protected void configureLdapServer()%0A    %7B%0A        ldapServer.setEnableLdaps( true );%0A        ldapServer.setLdapsCertificatePassword( %22boguspw%22 );%0A//        ldapServer.setIpPort( ldapsPort );%0A%0A        // Copy the bogus certificate to the certificates directory.%0A        InputStream in = getClass().getResourceAsStream( %22/bogus.cert%22 );%0A        ldapServer.getLdapsCertificateFile().getParentFile().mkdirs();%0A%0A        try%0A        %7B%0A            FileOutputStream out = new FileOutputStream( ldapServer.getLdapsCertificateFile() );%0A%0A            for ( ;; )%0A            %7B%0A                int c = in.read();%0A                if ( c %3C 0 )%0A                %7B%0A                    break;%0A                %7D%0A                out.write( c );%0A            %7D%0A%0A            in.close();%0A            out.close();%0A        %7D catch ( IOException e )%0A        %7B%0A            throw new RuntimeException( e );%0A        %7D%0A%0A    %7D%0A%0A
     /**%0A     * R

fix LdapsITest