Search in sources :

Example 1 with SearchRequestProtocolOp

use of com.unboundid.ldap.protocol.SearchRequestProtocolOp in project ldapsdk by pingidentity.

the class InterceptedSearchOperationTestCase method testBasics.

/**
 * Provides basic test coverage for an intercepted search operation.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasics() throws Exception {
    // Create an intercepted search operation.  We'll use a null connection,
    // which shouldn't happen naturally but will be sufficient for this test.
    final SearchRequestProtocolOp requestOp = new SearchRequestProtocolOp(new SearchRequest("dc=example,dc=com", SearchScope.BASE, "(objectClass=*)"));
    final InterceptedSearchOperation o = new InterceptedSearchOperation(null, 1, requestOp);
    assertNotNull(o.toString());
    // Test methods for a generic intercepted operation.
    assertNull(o.getClientConnection());
    assertEquals(o.getConnectionID(), -1L);
    assertNull(o.getConnectedAddress());
    assertEquals(o.getConnectedPort(), -1);
    assertEquals(o.getMessageID(), 1);
    assertNull(o.getProperty("propX"));
    o.setProperty("propX", "valX");
    assertNotNull(o.getProperty("propX"));
    assertEquals(o.getProperty("propX"), "valX");
    assertNotNull(o.toString());
    o.setProperty("propX", null);
    assertNull(o.getProperty("propX"));
    // Test methods specific to an intercepted search operation.
    assertNotNull(o.getRequest());
    assertEquals(o.getRequest().getScope(), SearchScope.BASE);
    assertNotNull(o.toString());
    final SearchRequest r = o.getRequest().duplicate();
    r.setScope(SearchScope.SUB);
    o.setRequest(r);
    assertNotNull(o.getRequest());
    assertEquals(o.getRequest().getScope(), SearchScope.SUB);
    assertNotNull(o.toString());
    assertNull(o.getResult());
    o.setResult(new LDAPResult(o.getMessageID(), ResultCode.SUCCESS));
    assertNotNull(o.getResult());
    assertNotNull(o.toString());
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) Test(org.testng.annotations.Test)

Example 2 with SearchRequestProtocolOp

use of com.unboundid.ldap.protocol.SearchRequestProtocolOp 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 3 with SearchRequestProtocolOp

use of com.unboundid.ldap.protocol.SearchRequestProtocolOp in project ldapsdk by pingidentity.

the class InMemoryOperationInterceptorRequestHandler method processSearchRequest.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public LDAPMessage processSearchRequest(final int messageID, @NotNull final SearchRequestProtocolOp request, @NotNull final List<Control> controls) {
    final InterceptedSearchOperation op = new InterceptedSearchOperation(connection, messageID, request, toArray(controls));
    activeOperations.put(messageID, op);
    try {
        for (final InMemoryOperationInterceptor i : interceptors) {
            try {
                i.processSearchRequest(op);
            } catch (final LDAPException le) {
                Debug.debugException(le);
                return new LDAPMessage(messageID, new SearchResultDoneProtocolOp(le.toLDAPResult()), le.getResponseControls());
            } catch (final Exception e) {
                Debug.debugException(e);
                return new LDAPMessage(messageID, new SearchResultDoneProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_REQUEST_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null));
            }
        }
        final LDAPMessage resultMessage = wrappedHandler.processSearchRequest(messageID, new SearchRequestProtocolOp((SearchRequest) op.getRequest()), op.getRequest().getControlList());
        op.setResult(resultMessage.getSearchResultDoneProtocolOp().toLDAPResult(toArray(resultMessage.getControls())));
        for (final InMemoryOperationInterceptor i : interceptors) {
            try {
                i.processSearchResult(op);
            } catch (final Exception e) {
                Debug.debugException(e);
                return new LDAPMessage(messageID, new SearchResultDoneProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_RESULT_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null));
            }
        }
        return new LDAPMessage(messageID, new SearchResultDoneProtocolOp(op.getResult()), op.getResult().getResponseControls());
    } finally {
        activeOperations.remove(messageID);
    }
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) LDAPException(com.unboundid.ldap.sdk.LDAPException) NotNull(com.unboundid.util.NotNull)

Example 4 with SearchRequestProtocolOp

use of com.unboundid.ldap.protocol.SearchRequestProtocolOp in project ldapsdk by pingidentity.

the class InterceptedSearchEntryTestCase method testBasics.

/**
 * Provides basic test coverage for an intercepted search entry.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasics() throws Exception {
    // Create an intercepted search entry.  We'll use a null connection, which
    // shouldn't happen naturally but will be sufficient for this test.
    final SearchRequestProtocolOp requestOp = new SearchRequestProtocolOp(new SearchRequest("dc=example,dc=com", SearchScope.BASE, "(objectClass=*)"));
    final InterceptedSearchEntry e = new InterceptedSearchEntry(new InterceptedSearchOperation(null, 1, requestOp), new SearchResultEntryProtocolOp(new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example")));
    assertNotNull(e.toString());
    // Test methods for a generic intercepted operation.
    assertNull(e.getClientConnection());
    assertEquals(e.getConnectionID(), -1L);
    assertNull(e.getConnectedAddress());
    assertEquals(e.getConnectedPort(), -1);
    assertEquals(e.getMessageID(), 1);
    assertNull(e.getProperty("propX"));
    e.setProperty("propX", "valX");
    assertNotNull(e.getProperty("propX"));
    assertEquals(e.getProperty("propX"), "valX");
    assertNotNull(e.toString());
    e.setProperty("propX", null);
    assertNull(e.getProperty("propX"));
    // Test methods specific to an intercepted compare operation.
    assertNotNull(e.getRequest());
    assertNotNull(e.getSearchEntry());
    assertFalse(e.getSearchEntry().hasAttribute("description"));
    assertNotNull(e.getSearchEntry().getControls());
    assertEquals(e.getSearchEntry().getControls().length, 0);
    assertNotNull(e.toString());
    e.setSearchEntry(new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example", "description: foo"));
    assertNotNull(e.getSearchEntry());
    assertTrue(e.getSearchEntry().hasAttributeValue("description", "foo"));
    assertNotNull(e.getSearchEntry().getControls());
    assertEquals(e.getSearchEntry().getControls().length, 0);
    assertNotNull(e.toString());
    e.setSearchEntry(new SearchResultEntry(new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example", "description: bar"), new Control("1.2.3.4"), new Control("1.2.3.5")));
    assertNotNull(e.getSearchEntry());
    assertTrue(e.getSearchEntry().hasAttributeValue("description", "bar"));
    assertNotNull(e.getSearchEntry().getControls());
    assertEquals(e.getSearchEntry().getControls().length, 2);
    assertNotNull(e.toString());
    e.setSearchEntry(null);
    assertNull(e.getSearchEntry());
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Entry(com.unboundid.ldap.sdk.Entry) Control(com.unboundid.ldap.sdk.Control) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Test(org.testng.annotations.Test)

Example 5 with SearchRequestProtocolOp

use of com.unboundid.ldap.protocol.SearchRequestProtocolOp in project ldapsdk by pingidentity.

the class InterceptedSearchReferenceTestCase method testBasics.

/**
 * Provides basic test coverage for an intercepted search reference.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasics() throws Exception {
    // Create an intercepted search reference.  We'll use a null connection,
    // which shouldn't happen naturally but will be sufficient for this test.
    final SearchRequestProtocolOp requestOp = new SearchRequestProtocolOp(new SearchRequest("dc=example,dc=com", SearchScope.BASE, "(objectClass=*)"));
    String[] referralURLs = { "ldap://ds1.example.com/" };
    final InterceptedSearchReference r = new InterceptedSearchReference(new InterceptedSearchOperation(null, 1, requestOp), new SearchResultReferenceProtocolOp(new SearchResultReference(1, referralURLs, null)));
    assertNotNull(r.toString());
    // Test methods for a generic intercepted operation.
    assertNull(r.getClientConnection());
    assertEquals(r.getConnectionID(), -1L);
    assertNull(r.getConnectedAddress());
    assertEquals(r.getConnectedPort(), -1);
    assertEquals(r.getMessageID(), 1);
    assertNull(r.getProperty("propX"));
    r.setProperty("propX", "valX");
    assertNotNull(r.getProperty("propX"));
    assertEquals(r.getProperty("propX"), "valX");
    assertNotNull(r.toString());
    r.setProperty("propX", null);
    assertNull(r.getProperty("propX"));
    // Test methods specific to an intercepted compare operation.
    assertNotNull(r.getRequest());
    assertNotNull(r.getSearchReference());
    assertEquals(r.getSearchReference().getReferralURLs().length, 1);
    assertNotNull(r.toString());
    referralURLs = new String[] { "ldap://ds1.example.com/", "ldap://ds2.example.com/" };
    r.setSearchReference(new SearchResultReference(1, referralURLs, null));
    assertNotNull(r.getSearchReference());
    assertEquals(r.getSearchReference().getReferralURLs().length, 2);
    assertNotNull(r.toString());
    r.setSearchReference(null);
    assertNull(r.getSearchReference());
    assertNotNull(r.toString());
}
Also used : SearchResultReferenceProtocolOp(com.unboundid.ldap.protocol.SearchResultReferenceProtocolOp) SearchRequest(com.unboundid.ldap.sdk.SearchRequest) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) SearchResultReference(com.unboundid.ldap.sdk.SearchResultReference) Test(org.testng.annotations.Test)

Aggregations

SearchRequestProtocolOp (com.unboundid.ldap.protocol.SearchRequestProtocolOp)7 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)4 SearchResultDoneProtocolOp (com.unboundid.ldap.protocol.SearchResultDoneProtocolOp)4 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)4 Test (org.testng.annotations.Test)4 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)3 Control (com.unboundid.ldap.sdk.Control)3 BindRequestProtocolOp (com.unboundid.ldap.protocol.BindRequestProtocolOp)2 BindResponseProtocolOp (com.unboundid.ldap.protocol.BindResponseProtocolOp)2 SearchResultEntryProtocolOp (com.unboundid.ldap.protocol.SearchResultEntryProtocolOp)2 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)2 SearchResultReference (com.unboundid.ldap.sdk.SearchResultReference)2 NotNull (com.unboundid.util.NotNull)2 ASN1Element (com.unboundid.asn1.ASN1Element)1 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)1 ASN1StreamReader (com.unboundid.asn1.ASN1StreamReader)1 AddRequestProtocolOp (com.unboundid.ldap.protocol.AddRequestProtocolOp)1 AddResponseProtocolOp (com.unboundid.ldap.protocol.AddResponseProtocolOp)1 CompareRequestProtocolOp (com.unboundid.ldap.protocol.CompareRequestProtocolOp)1 CompareResponseProtocolOp (com.unboundid.ldap.protocol.CompareResponseProtocolOp)1