<commit_before>/* Copyright 2004, 2005 Acegi Technology Pty Limited
 *
 * 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.acegisecurity.providers.ldap;

import org.apache.ldap.server.configuration.StartupConfiguration;
import org.apache.ldap.server.configuration.MutableDirectoryPartitionConfiguration;
import org.apache.ldap.server.configuration.MutableStartupConfiguration;
import org.apache.ldap.server.configuration.Configuration;
import org.apache.ldap.server.configuration.MutableServerStartupConfiguration;
import org.apache.ldap.server.jndi.CoreContextFactory;
import org.apache.ldap.server.jndi.ServerContextFactory;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NameAlreadyBoundException;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.Attribute;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import java.util.Properties;
import java.util.Set;
import java.util.HashSet;

/**
 * @author <NAME>
 * @version $Id$
 */
public class LdapTestServer {

    //~ Instance fields ========================================================

    private DirContext serverContext;

    private StartupConfiguration cfg;

    //~ Constructors ================================================================

    /**
     * Starts up and configures ApacheDS.
     *
     * @param embedded if false the server will listen for connections on port 10389
     *
     */
    public LdapTestServer(boolean embedded) {
        startLdapServer(embedded);
        createManagerUser();
        initTestData();
    }

    //~ Methods ================================================================


    private void startLdapServer(boolean embedded) {
        if(embedded) {
            cfg = new MutableStartupConfiguration();
        } else {
            cfg = new MutableServerStartupConfiguration();
        }

        initConfiguration();

        Properties env = new Properties();

        env.setProperty( Context.PROVIDER_URL, "dc=acegisecurity,dc=org" );
        env.setProperty( Context.INITIAL_CONTEXT_FACTORY,
                embedded ? CoreContextFactory.class.getName() : ServerContextFactory.class.getName() );
        env.putAll( cfg.toJndiEnvironment() );

        try {
            // TODO: Remove all children on startup
            serverContext = new InitialDirContext( env );
        } catch (NamingException e) {
            System.err.println("Failed to start Apache DS");
            e.printStackTrace();
        }
    }

    private void initTestData() {
        createOu("people");
        createOu("groups");
        createUser("bob","<NAME>", "bobspassword");
        createUser("ben","<NAME>", "{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
        String[] developers = new String[]
                {"uid=ben,ou=people,dc=acegisecurity,dc=org", "uid=bob,ou=people,dc=acegisecurity,dc=org"};
        createGroup("developers","developer",developers);
        createGroup("managers","manager", new String[] { developers[0]});
    }

    private void createManagerUser() {
        Attributes user = new BasicAttributes( "cn", "manager" , true );
        user.put( "userPassword", "<PASSWORD>" );
        Attribute objectClass = new BasicAttribute("objectClass");
        user.put( objectClass );
        objectClass.add( "top" );
        objectClass.add( "person" );
        objectClass.add( "organizationalPerson" );
        objectClass.add( "inetOrgPerson" );
        user.put( "sn", "Manager" );
        user.put( "cn", "manager" );
        try {
            serverContext.createSubcontext("cn=manager", user );
        } catch(NameAlreadyBoundException ignore) {
 //           System.out.println("Manager user already exists.");
        } catch (NamingException ne) {
            System.err.println("Failed to create manager user.");
            ne.printStackTrace();
        }
    }

    public void createUser( String uid, String cn, String password ) {
        Attributes user = new BasicAttributes("uid", uid);
        user.put( "cn", cn);
        user.put( "userPassword", LdapUtils.getUtf8Bytes(password) );
        Attribute objectClass = new BasicAttribute( "objectClass" );
        user.put( objectClass );
        objectClass.add( "top" );
        objectClass.add( "person" );
        objectClass.add( "organizationalPerson" );
        objectClass.add( "inetOrgPerson" );
        user.put( "sn", uid );

        try {
            serverContext.createSubcontext( "uid="+uid+",ou=people", user );
        } catch(NameAlreadyBoundException ignore) {
//            System.out.println(" user " + uid + " already exists.");
        } catch (NamingException ne) {
            System.err.println("Failed to create  user.");
            ne.printStackTrace();
        }
    }

    public void createOu(String name) {
        Attributes ou = new BasicAttributes( "ou", name );
        Attribute objectClass = new BasicAttribute( "objectClass" );
        objectClass.add("top");
        objectClass.add("organizationalUnit");
        ou.put(objectClass);

        try {
            serverContext.createSubcontext( "ou="+name, ou);
        } catch(NameAlreadyBoundException ignore) {
 //           System.out.println(" ou " + name + " already exists.");
        } catch (NamingException ne) {
            System.err.println("Failed to create ou.");
            ne.printStackTrace();
        }

    }

    public void createGroup( String cn, String ou, String[] memberDns ) {
        Attributes group = new BasicAttributes("cn", cn);
        Attribute members = new BasicAttribute("member");
        Attribute orgUnit = new BasicAttribute("ou", ou);

        for(int i=0; i < memberDns.length; i++) {
            members.add(memberDns[i]);
        }

        Attribute objectClass = new BasicAttribute( "objectClass" );
        objectClass.add( "top" );
        objectClass.add( "groupOfNames" );

        group.put(objectClass);
        group.put(members);
        group.put(orgUnit);

        try {
            serverContext.createSubcontext( "cn="+cn+",ou=groups", group );
        } catch(NameAlreadyBoundException ignore) {
            System.out.println(" group " + cn + " already exists.");
        } catch (NamingException ne) {
            System.err.println("Failed to create group.");
            ne.printStackTrace();
        }
    }

    private void initConfiguration() {
        MutableDirectoryPartitionConfiguration acegiDit = new MutableDirectoryPartitionConfiguration();
        acegiDit.setName("acegisecurity");
        acegiDit.setSuffix("dc=acegisecurity,dc=org");
        BasicAttributes attributes = new BasicAttributes();
        BasicAttribute objectClass = new BasicAttribute("objectClass");
        objectClass.add("top");
        objectClass.add("domain");
        objectClass.add("extensibleObject");
        attributes.put(objectClass);
        acegiDit.setContextEntry(attributes);

        Set indexedAttrs = new HashSet();
        indexedAttrs.add("objectClass");
        indexedAttrs.add("uid");
        indexedAttrs.add("cn");
        indexedAttrs.add("ou");
        indexedAttrs.add("member");

        acegiDit.setIndexedAttributes(indexedAttrs);

        Set partitions = new HashSet();
        partitions.add(acegiDit);

        if(cfg instanceof MutableServerStartupConfiguration) {
            MutableServerStartupConfiguration serverCfg = (MutableServerStartupConfiguration)cfg;
            serverCfg.setLdapPort(10389);
            serverCfg.setContextPartitionConfigurations(partitions);<commit_msg>Move the apacheDS working directory to java.io.tmpdir<commit_after>/* Copyright 2004, 2005 Acegi Technology Pty Limited
 *
 * 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.acegisecurity.providers.ldap;

import org.apache.ldap.server.configuration.StartupConfiguration;
import org.apache.ldap.server.configuration.MutableDirectoryPartitionConfiguration;
import org.apache.ldap.server.configuration.MutableStartupConfiguration;
import org.apache.ldap.server.configuration.Configuration;
import org.apache.ldap.server.configuration.MutableServerStartupConfiguration;
import org.apache.ldap.server.jndi.CoreContextFactory;
import org.apache.ldap.server.jndi.ServerContextFactory;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NameAlreadyBoundException;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.Attribute;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import java.util.Properties;
import java.util.Set;
import java.util.HashSet;
import java.io.File;
import java.io.IOException;

/**
 * An embedded LDAP test server, complete with test data for running the
 * unit tests against.
 *
 * @author <NAME>
 * @version $Id$
 */
public class LdapTestServer {

    //~ Instance fields ========================================================

    private DirContext serverContext;

    private StartupConfiguration cfg;

    // Move the working dir to the temp directory
    private File workingDir = new File( System.getProperty("java.io.tmpdir")
            + File.separator + "apacheds-work" );


    //~ Constructors ================================================================

    /**
     * Starts up and configures ApacheDS.
     *
     * @param embedded if false the server will listen for connections on port 10389
     *
     */
    public LdapTestServer(boolean embedded) {
        startLdapServer(embedded);
        createManagerUser();
        initTestData();
    }

    //~ Methods ================================================================


    private void startLdapServer(boolean embedded) {

        if(embedded) {
            cfg = new MutableStartupConfiguration();
            ((MutableStartupConfiguration)cfg).setWorkingDirectory(workingDir);
        } else {
            cfg = new MutableServerStartupConfiguration();
            ((MutableServerStartupConfiguration)cfg).setWorkingDirectory(workingDir);
        }

        System.out.println("Working directory is " + workingDir.getAbsolutePath());

        initConfiguration();

        Properties env = new Properties();

        env.setProperty( Context.PROVIDER_URL, "dc=acegisecurity,dc=org" );
        env.setProperty( Context.INITIAL_CONTEXT_FACTORY,
                embedded ? CoreContextFactory.class.getName() : ServerContextFactory.class.getName() );
        env.putAll( cfg.toJndiEnvironment() );

        try {
            serverContext = new InitialDirContext( env );
        } catch (NamingException e) {
            System.err.println("Failed to start Apache DS");
            e.printStackTrace();
        }
    }

    private void initTestData() {
        createOu("people");
        createOu("groups");
        createUser("bob","<NAME>", "bobspassword");
        createUser("ben","<NAME>", "{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
        String[] developers = new String[]
                {"uid=ben,ou=people,dc=acegisecurity,dc=org", "uid=bob,ou=people,dc=acegisecurity,dc=org"};
        createGroup("developers","developer",developers);
        createGroup("managers","manager", new String[] { developers[0]});
    }

    private void createManagerUser() {
        Attributes user = new BasicAttributes( "cn", "manager" , true );
        user.put( "userPassword", "<PASSWORD>" );
        Attribute objectClass = new BasicAttribute("objectClass");
        user.put( objectClass );
        objectClass.add( "top" );
        objectClass.add( "person" );
        objectClass.add( "organizationalPerson" );
        objectClass.add( "inetOrgPerson" );
        user.put( "sn", "Manager" );
        user.put( "cn", "manager" );
        try {
            serverContext.createSubcontext("cn=manager", user );
        } catch(NameAlreadyBoundException ignore) {
 //           System.out.println("Manager user already exists.");
        } catch (NamingException ne) {
            System.err.println("Failed to create manager user.");
            ne.printStackTrace();
        }
    }

    public void createUser( String uid, String cn, String password ) {
        Attributes user = new BasicAttributes("uid", uid);
        user.put( "cn", cn);
        user.put( "userPassword", LdapUtils.getUtf8Bytes(password) );
        Attribute objectClass = new BasicAttribute( "objectClass" );
        user.put( objectClass );
        objectClass.add( "top" );
        objectClass.add( "person" );
        objectClass.add( "organizationalPerson" );
        objectClass.add( "inetOrgPerson" );
        user.put( "sn", uid );

        try {
            serverContext.createSubcontext( "uid="+uid+",ou=people", user );
        } catch(NameAlreadyBoundException ignore) {
//            System.out.println(" user " + uid + " already exists.");
        } catch (NamingException ne) {
            System.err.println("Failed to create  user.");
            ne.printStackTrace();
        }
    }

    public void createOu(String name) {
        Attributes ou = new BasicAttributes( "ou", name );
        Attribute objectClass = new BasicAttribute( "objectClass" );
        objectClass.add("top");
        objectClass.add("organizationalUnit");
        ou.put(objectClass);

        try {
            serverContext.createSubcontext( "ou="+name, ou);
        } catch(NameAlreadyBoundException ignore) {
 //           System.out.println(" ou " + name + " already exists.");
        } catch (NamingException ne) {
            System.err.println("Failed to create ou.");
            ne.printStackTrace();
        }

    }

    public void createGroup( String cn, String ou, String[] memberDns ) {
        Attributes group = new BasicAttributes("cn", cn);
        Attribute members = new BasicAttribute("member");
        Attribute orgUnit = new BasicAttribute("ou", ou);

        for(int i=0; i < memberDns.length; i++) {
            members.add(memberDns[i]);
        }

        Attribute objectClass = new BasicAttribute( "objectClass" );
        objectClass.add( "top" );
        objectClass.add( "groupOfNames" );

        group.put(objectClass);
        group.put(members);
        group.put(orgUnit);

        try {
            serverContext.createSubcontext( "cn="+cn+",ou=groups", group );
        } catch(NameAlreadyBoundException ignore) {
//            System.out.println(" group " + cn + " already exists.");
        } catch (NamingException ne) {
            System.err.println("Failed to create group.");
            ne.printStackTrace();
        }
    }

    private void initConfiguration() {

        // Create the partition for the acegi tests
        MutableDirectoryPartitionConfiguration acegiDit = new MutableDirectoryPartitionConfiguration();
        acegiDit.setName("acegisecurity");
        acegiDit.setSuffix("dc=acegisecurity,dc=org");
        BasicAttributes attributes = new BasicAttributes();
        BasicAttribute objectClass = new BasicAttribute("objectClass");
        objectClass.add("top");
        objectClass.add("domain");
        objectClass.add("extensibleObject");
        attributes.put(objectClass);
        acegiDit.setContextEntry(attributes);

        Set indexedAttrs = new HashSet();
        indexedAttrs.add("objectClass");
        indexedAttrs.add("uid");
        indexedAttrs.add("cn");
        indexedAttrs.add("ou");
        indexedAttrs.add("member");

        acegiDit.setIndexedAttributes(indexedAttrs);

        Set partitions = new HashSet();
        partitions.add(acegiDit);

        if(cfg instanceof MutableServerStartupConfiguration) {
            MutableServerStartupConfiguration serverCfg = (MutableServerStartupConfiguration)cfg;
            serverCfg.setLdapPort(10389);
            serverCfg.setContextPartitionConfigurations(partitions);
{'fraction_non_alphanumeric': 0.1331918906176332, 'fraction_numerical': 0.0033003300330033004, 'mean_word_length': 2.9628678187762727, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5030390', 'n_tokens_mistral': 4577, 'n_tokens_neox': 4417, 'n_words': 1015}