Search in sources :

Example 81 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class UNBOUNDIDTESTServer method run.

/**
 * Performs the processing for this server.
 */
@Override()
public void run() {
    try {
        serverSocket = new ServerSocket(0);
        listenPort = serverSocket.getLocalPort();
        while (!stopRequested.get()) {
            // Accept a connection from a client.
            clientSocket = serverSocket.accept();
            final InputStream inputStream = clientSocket.getInputStream();
            final OutputStream outputStream = clientSocket.getOutputStream();
            final ASN1StreamReader asn1Reader = new ASN1StreamReader(inputStream, 0);
            // The client must first send an UNBOUNDID-TEST bind request with no
            // credentials.
            LDAPMessage requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            BindRequestProtocolOp bindRequestOp = requestMessage.getBindRequestProtocolOp();
            assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
            assertNull(bindRequestOp.getSASLCredentials());
            // Return a "SASL bind in progress" response.
            LDAPMessage responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SASL_BIND_IN_PROGRESS_INT_VALUE, null, null, null, null));
            outputStream.write(responseMessage.encode().encode());
            outputStream.flush();
            // The next request must be an UNBOUNDID-TEST bind request with
            // credentials.  We won't do anything to validate the credentials, but
            // we will look at the third element to see what QoP the client
            // requested.
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            bindRequestOp = requestMessage.getBindRequestProtocolOp();
            assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
            assertNotNull(bindRequestOp.getSASLCredentials());
            final ASN1Sequence credSequence = ASN1Sequence.decodeAsSequence(bindRequestOp.getSASLCredentials().getValue());
            final ASN1Element[] credElements = credSequence.elements();
            final SASLQualityOfProtection qop = SASLQualityOfProtection.forName(ASN1OctetString.decodeAsOctetString(credElements[2]).stringValue());
            assertNotNull(qop);
            final boolean qopEncode = ((qop == SASLQualityOfProtection.AUTH_INT) || (qop == SASLQualityOfProtection.AUTH_CONF));
            // Return a "success" response.  Include server SASL credentials with
            // the requested QoP.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null, new ASN1OctetString(qop.toString())));
            outputStream.write(responseMessage.encode().encode());
            outputStream.flush();
            // request.
            if (qopEncode) {
                for (int i = 0; i < 4; i++) {
                    inputStream.read();
                }
            }
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            final SearchRequestProtocolOp searchRequestOp = requestMessage.getSearchRequestProtocolOp();
            assertEquals(searchRequestOp.getBaseDN(), "");
            assertEquals(searchRequestOp.getScope(), SearchScope.BASE);
            assertEquals(searchRequestOp.getFilter(), Filter.createPresenceFilter("objectClass"));
            assertEquals(searchRequestOp.getAttributes(), Arrays.asList("1.1"));
            // Return a search result entry message with a DN but no attributes.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultEntryProtocolOp("", Collections.<Attribute>emptyList()));
            byte[] messageBytes = responseMessage.encode().encode();
            if (qopEncode) {
                // Since we know it's a tiny response, we know the length will be
                // less than 127 bytes, so we can cheat.
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(messageBytes.length);
            }
            outputStream.write(messageBytes);
            outputStream.flush();
            // Return a "success" search result done message.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultDoneProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null));
            messageBytes = responseMessage.encode().encode();
            if (qopEncode) {
                // Since we know it's a tiny response, we know the length will be
                // less than 127 bytes, so we can cheat.
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(messageBytes.length);
            }
            outputStream.write(messageBytes);
            outputStream.flush();
            // The next request should be an unbind request.
            if (qopEncode) {
                for (int i = 0; i < 4; i++) {
                    inputStream.read();
                }
            }
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            final UnbindRequestProtocolOp unbindRequestOp = requestMessage.getUnbindRequestProtocolOp();
            // Close the connection.
            try {
                asn1Reader.close();
            } catch (final Exception e) {
            }
            try {
                outputStream.close();
            } catch (final Exception e) {
            }
            try {
                clientSocket.close();
            } catch (final Exception e) {
            }
            clientSocket = null;
        }
    } catch (final Exception e) {
        stopServer();
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) ServerSocket(java.net.ServerSocket) BindResponseProtocolOp(com.unboundid.ldap.protocol.BindResponseProtocolOp) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) UnbindRequestProtocolOp(com.unboundid.ldap.protocol.UnbindRequestProtocolOp)

Example 82 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class PLAINBindRequestTestCase method testConstructor6.

/**
 * Tests the sixth constructor.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor6() throws Exception {
    PLAINBindRequest r = new PLAINBindRequest("u:test.user", "u:test2.user", new ASN1OctetString("password"));
    r = r.duplicate();
    assertNotNull(r.getAuthenticationID());
    assertEquals(r.getAuthenticationID(), "u:test.user");
    assertNotNull(r.getAuthorizationID());
    assertEquals(r.getAuthorizationID(), "u:test2.user");
    assertNotNull(r.getPasswordString());
    assertEquals(r.getPasswordString(), "password");
    assertNotNull(r.getPasswordBytes());
    assertTrue(Arrays.equals(r.getPasswordBytes(), "password".getBytes("UTF-8")));
    assertEquals(r.getBindType(), "PLAIN");
    assertEquals(r.getSASLMechanismName(), "PLAIN");
    assertNotNull(r.getControls());
    assertEquals(r.getControls().length, 0);
    assertNotNull(r.getRebindRequest("server.example.com", 389));
    assertTrue(r.getRebindRequest("server.example.com", 389) instanceof PLAINBindRequest);
    assertNotNull(r.toString());
    final ArrayList<String> toCodeLines = new ArrayList<String>(10);
    r.toCode(toCodeLines, "foo", 0, false);
    assertFalse(toCodeLines.isEmpty());
    toCodeLines.clear();
    r.toCode(toCodeLines, "bar", 4, true);
    assertFalse(toCodeLines.isEmpty());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ArrayList(java.util.ArrayList) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Test(org.testng.annotations.Test)

Example 83 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class PLAINBindRequestTestCase method testConstructor9.

/**
 * Tests the ninth constructor.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor9() throws Exception {
    Control[] controls = { new Control("1.2.3.4"), new Control("1.2.3.5", true, null) };
    PLAINBindRequest r = new PLAINBindRequest("u:test.user", new ASN1OctetString("password"), controls);
    r = r.duplicate();
    assertNotNull(r.getAuthenticationID());
    assertEquals(r.getAuthenticationID(), "u:test.user");
    assertNull(r.getAuthorizationID());
    assertNotNull(r.getPasswordString());
    assertEquals(r.getPasswordString(), "password");
    assertNotNull(r.getPasswordBytes());
    assertTrue(Arrays.equals(r.getPasswordBytes(), "password".getBytes("UTF-8")));
    assertEquals(r.getBindType(), "PLAIN");
    assertEquals(r.getSASLMechanismName(), "PLAIN");
    assertNotNull(r.getControls());
    assertEquals(r.getControls().length, 2);
    assertNotNull(r.getRebindRequest("server.example.com", 389));
    assertTrue(r.getRebindRequest("server.example.com", 389) instanceof PLAINBindRequest);
    assertNotNull(r.toString());
    final ArrayList<String> toCodeLines = new ArrayList<String>(10);
    r.toCode(toCodeLines, "foo", 0, false);
    assertFalse(toCodeLines.isEmpty());
    toCodeLines.clear();
    r.toCode(toCodeLines, "bar", 4, true);
    assertFalse(toCodeLines.isEmpty());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ArrayList(java.util.ArrayList) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Test(org.testng.annotations.Test)

Example 84 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class PLAINBindRequestTestCase method testConstructor12.

/**
 * Tests the twelfth constructor.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor12() throws Exception {
    Control[] controls = { new Control("1.2.3.4"), new Control("1.2.3.5", true, null) };
    PLAINBindRequest r = new PLAINBindRequest("u:test.user", "u:test2.user", new ASN1OctetString("password"), controls);
    r = r.duplicate();
    assertNotNull(r.getAuthenticationID());
    assertEquals(r.getAuthenticationID(), "u:test.user");
    assertNotNull(r.getAuthorizationID());
    assertEquals(r.getAuthorizationID(), "u:test2.user");
    assertNotNull(r.getPasswordString());
    assertEquals(r.getPasswordString(), "password");
    assertNotNull(r.getPasswordBytes());
    assertTrue(Arrays.equals(r.getPasswordBytes(), "password".getBytes("UTF-8")));
    assertEquals(r.getBindType(), "PLAIN");
    assertEquals(r.getSASLMechanismName(), "PLAIN");
    assertNotNull(r.getControls());
    assertEquals(r.getControls().length, 2);
    assertNotNull(r.getRebindRequest("server.example.com", 389));
    assertTrue(r.getRebindRequest("server.example.com", 389) instanceof PLAINBindRequest);
    assertNotNull(r.toString());
    final ArrayList<String> toCodeLines = new ArrayList<String>(10);
    r.toCode(toCodeLines, "foo", 0, false);
    assertFalse(toCodeLines.isEmpty());
    toCodeLines.clear();
    r.toCode(toCodeLines, "bar", 4, true);
    assertFalse(toCodeLines.isEmpty());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ArrayList(java.util.ArrayList) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Test(org.testng.annotations.Test)

Example 85 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class RDNNameValuePairTestCase method testNameValuePairWithoutSchema.

/**
 * Tests the basic behavior of the {@code RDNNameValuePair} class when no
 * {@code Schema} object is provided.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testNameValuePairWithoutSchema() throws Exception {
    final RDNNameValuePair p = new RDNNameValuePair("givenName", new ASN1OctetString("foo"), null);
    assertNotNull(p.getAttributeName());
    assertEquals(p.getAttributeName(), "givenName");
    assertNotNull(p.getNormalizedAttributeName());
    assertEquals(p.getNormalizedAttributeName(), "givenname");
    // The second time should use a cached version.
    assertNotNull(p.getNormalizedAttributeName());
    assertEquals(p.getNormalizedAttributeName(), "givenname");
    assertNotNull(p.getAttributeValue());
    assertEquals(p.getAttributeValue(), "foo");
    assertNotNull(p.getAttributeValueBytes());
    assertEquals(p.getAttributeValueBytes(), "foo".getBytes(StandardCharsets.UTF_8));
    assertNotNull(p.getRawAttributeValue());
    assertEquals(p.getRawAttributeValue(), new ASN1OctetString("foo"));
    assertNotNull(p.toString());
    assertEquals(p.toString(), "givenName=foo");
    // The second time should use a cached version.
    assertNotNull(p.toString());
    assertEquals(p.toString(), "givenName=foo");
    final StringBuilder buffer = new StringBuilder();
    p.toString(buffer, false);
    assertEquals(buffer.toString(), p.toString());
    assertNotNull(p.toMinimallyEncodedString());
    assertEquals(p.toMinimallyEncodedString(), "givenName=foo");
    assertNotNull(p.toNormalizedString());
    assertEquals(p.toNormalizedString(), "givenname=foo");
    // The second time should use a cached version.
    assertNotNull(p.toNormalizedString());
    assertEquals(p.toNormalizedString(), "givenname=foo");
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Test(org.testng.annotations.Test)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1173 Test (org.testng.annotations.Test)852 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)382 Control (com.unboundid.ldap.sdk.Control)310 ASN1Element (com.unboundid.asn1.ASN1Element)237 ArrayList (java.util.ArrayList)204 NotNull (com.unboundid.util.NotNull)191 LDAPException (com.unboundid.ldap.sdk.LDAPException)142 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)133 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)99 ASN1Enumerated (com.unboundid.asn1.ASN1Enumerated)92 IOException (java.io.IOException)88 ASN1Integer (com.unboundid.asn1.ASN1Integer)80 ExtendedRequest (com.unboundid.ldap.sdk.ExtendedRequest)69 DN (com.unboundid.ldap.sdk.DN)65 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)64 ByteArrayInputStream (java.io.ByteArrayInputStream)56 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)53 ASN1Boolean (com.unboundid.asn1.ASN1Boolean)52 AuthorizationIdentityResponseControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl)49