mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

davidely
03.13.2007 77077b7b3b33b117384c7bcaf73d839777ae638c
opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
@@ -45,6 +45,7 @@
import java.net.ServerSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.Socket;
import org.opends.server.backends.MemoryBackend;
import org.opends.server.backends.jeb.BackendImpl;
@@ -61,6 +62,12 @@
import org.opends.server.loggers.debug.DebugLogger;
import org.opends.server.plugins.InvocationCounterPlugin;
import org.opends.server.protocols.internal.InternalClientConnection;
import org.opends.server.protocols.asn1.ASN1Reader;
import org.opends.server.protocols.asn1.ASN1Writer;
import org.opends.server.protocols.asn1.ASN1OctetString;
import org.opends.server.protocols.ldap.BindRequestProtocolOp;
import org.opends.server.protocols.ldap.LDAPMessage;
import org.opends.server.protocols.ldap.BindResponseProtocolOp;
import org.opends.server.tools.LDAPModify;
import org.opends.server.types.DN;
import org.opends.server.types.FilePermission;
@@ -75,6 +82,7 @@
import org.opends.server.util.ModifyDNChangeRecordEntry;
import static org.testng.Assert.*;
import static org.testng.Assert.assertEquals;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
@@ -787,6 +795,40 @@
  public static boolean canBind(String dn, String pw) throws Exception
  {
    // Check that the user can bind.
    Socket s = null;
    try {
      s = new Socket("127.0.0.1", TestCaseUtils.getServerLdapPort());
      ASN1Reader r = new ASN1Reader(s);
      ASN1Writer w = new ASN1Writer(s);
      r.setIOTimeout(3000);
      BindRequestProtocolOp bindRequest =
        new BindRequestProtocolOp(
                 new ASN1OctetString(dn),
                 3,
                new ASN1OctetString(pw));
      LDAPMessage message = new LDAPMessage(1, bindRequest);
      w.writeElement(message.encode());
      message = LDAPMessage.decode(r.readElement().decodeAsSequence());
      BindResponseProtocolOp bindResponse = message.getBindResponseProtocolOp();
      if (bindResponse.getResultCode() == 0) {
        return true;
      }
    } catch (Exception t) {
      t.printStackTrace();
    } finally {
      if (s != null) {
        s.close();
      }
    }
    return false;
  }
  /**
   * Adds the provided entry to the Directory Server using an internal
   * operation.
@@ -889,6 +931,8 @@
  /**
   * Creates a temporary text file with the specified contents.  It will be
   * marked for automatic deletion when the JVM exits.
@@ -1044,6 +1088,29 @@
    return new String(bytes);
  }
  /**
   * Returns the contents of file as a List of the lines as defined by
   * java.io.BufferedReader#readLine() (i.e. the line terminator is not
   * included).  An ArrayList is explicitly returned, so that callers know that
   * random access is not expensive.
   */
  public static ArrayList<String> readFileToLines(File file)
          throws IOException {
    BufferedReader reader =
      new BufferedReader(
        new InputStreamReader(
          new DataInputStream(
                  new FileInputStream(file))));
    ArrayList<String> lines = new ArrayList<String>();
    String line;
    while ((line = reader.readLine()) != null)   {
      lines.add(line);
    }
    return lines;
  }
  /**
   * Read the contents of a file and return it as a String.