Search in sources :

Example 36 with SearchRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestGlobalNoControlsOidAndAlias.

/**
 * Test the decoding of a SearchRequest with no controls but with oid
 * attributes. The search filter is :
 * (&(|(objectclass=top)(2.5.4.11=contacts))(!(organizationalUnitName=ttt)))
 */
@Test
public void testDecodeSearchRequestGlobalNoControlsOidAndAlias() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0xA1);
    stream.put(new byte[] { 0x30, (byte) 0x81, // LDAPMessage ::=SEQUENCE {
    (byte) 0x9E, 0x02, 0x01, // messageID MessageID
    0x01, 0x63, (byte) 0x81, // CHOICE { ...,
    (byte) 0x98, // SearchRequest ::= APPLICATION[3] SEQUENCE {
    0x04, // baseObject LDAPDN,
    0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0A, 0x01, // scope
    0x01, // wholeSubtree (2) },
    0x0A, 0x01, // derefAliases ENUMERATED {
    0x03, // sizeLimit INTEGER (0 .. maxInt), (1000)
    0x02, 0x02, 0x03, (byte) 0xE8, // timeLimit INTEGER (0 .. maxInt), (1000)
    0x02, 0x02, 0x03, (byte) 0xE8, 0x01, 0x01, // typesOnly
    (byte) 0xFF, // filter Filter,
    (byte) 0xA0, // Filter ::= CHOICE {
    0x4D, // and [0] SET OF Filter,
    (byte) 0xA1, // or [1] SET of Filter,
    0x2A, (byte) 0xA3, // equalityMatch [3]
    0x12, // attributeDesc AttributeDescription (LDAPString),
    0x04, 0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', // assertionValue AssertionValue (OCTET STRING) }
    0x04, 0x03, 't', 'o', 'p', (byte) 0xA3, // equalityMatch
    0x14, // Assertion ::= SEQUENCE {
    0x04, 0x08, '2', '.', '5', '.', '4', '.', '1', // attributeDesc
    '1', // assertionValue AssertionValue (OCTET STRING) }
    0x04, 0x08, 'c', 'o', 'n', 't', 'a', 'c', 't', 's', (byte) 0xA2, // not
    0x1F, // Filter,
    (byte) 0xA3, // equalityMatch [3]
    0x1D, // attributeDesc AttributeDescription (LDAPString),
    0x04, 0x16, 'o', 'r', 'g', 'a', 'n', 'i', 'z', 'a', 't', 'i', 'o', 'n', 'a', 'l', 'U', 'n', 'i', 't', 'N', 'a', 'm', 'e', // assertionValue AssertionValue (OCTET STRING) }
    0x04, 0x03, 't', 't', 't', // attributes AttributeDescriptionList }
    0x30, // AttributeDescriptionList ::= SEQUENCE OF
    0x15, // AttributeDescription
    0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription
    '0', // ::= LDAPString
    0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription
    '1', // ::= LDAPString
    0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::=
    '2' // LDAPString
    });
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(1, searchRequest.getMessageId());
    assertEquals("uid=akarasulu,dc=example,dc=com", searchRequest.getBase().toString());
    assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(1000, searchRequest.getSizeLimit());
    assertEquals(1000, searchRequest.getTimeLimit());
    assertEquals(true, searchRequest.getTypesOnly());
    // (& (...
    ExprNode filter = searchRequest.getFilter();
    AndNode andNode = (AndNode) filter;
    assertNotNull(andNode);
    List<ExprNode> andNodes = andNode.getChildren();
    // (& (| (...
    assertEquals(2, andNodes.size());
    OrNode orFilter = (OrNode) andNodes.get(0);
    assertNotNull(orFilter);
    // (& (| (obectclass=top) (...
    List<ExprNode> orNodes = orFilter.getChildren();
    assertEquals(2, orNodes.size());
    EqualityNode<?> equalityNode = (EqualityNode<?>) orNodes.get(0);
    assertNotNull(equalityNode);
    assertEquals("objectclass", equalityNode.getAttribute());
    assertEquals("top", equalityNode.getValue().getValue());
    // (& (| (objectclass=top) (ou=contacts) ) (...
    equalityNode = (EqualityNode<?>) orNodes.get(1);
    assertNotNull(equalityNode);
    assertEquals("2.5.4.11", equalityNode.getAttribute());
    assertEquals("contacts", equalityNode.getValue().getValue());
    // (& (| (objectclass=top) (ou=contacts) ) (! ...
    NotNode notNode = (NotNode) andNodes.get(1);
    assertNotNull(notNode);
    // (& (| (objectclass=top) (ou=contacts) ) (! (objectclass=ttt) ) )
    equalityNode = (EqualityNode<?>) notNode.getFirstChild();
    assertNotNull(equalityNode);
    assertEquals("organizationalUnitName", equalityNode.getAttribute());
    assertEquals("ttt", equalityNode.getValue().getValue());
    List<String> attributes = searchRequest.getAttributes();
    for (String attribute : attributes) {
        assertNotNull(attribute);
    }
// We won't check the encoding, as it has changed because of
// attributes transformations
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) NotNode(org.apache.directory.api.ldap.model.filter.NotNode) AndNode(org.apache.directory.api.ldap.model.filter.AndNode) ByteBuffer(java.nio.ByteBuffer) ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) EqualityNode(org.apache.directory.api.ldap.model.filter.EqualityNode) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) OrNode(org.apache.directory.api.ldap.model.filter.OrNode) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 37 with SearchRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.

the class SearchRequestMatchingRuleAssertionTest method testDecodeSearchRequestExtensibleMatch.

/**
 * Tests an search request decode with a simple equality match filter.
 */
@Test
public void testDecodeSearchRequestExtensibleMatch() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x63);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x61, 0x02, 0x01, // messageID       MessageID,
    0x01, 0x63, // protocolOp      CHOICE {
    0x5C, // SearchRequest ::= [APPLICATION 3] SEQUENCE {
    0x04, // baseObject      LDAPDN, (dc=example,dc=com)
    0x11, 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', // scope           ENUMERATED {
    0x0A, 0x01, // baseObject              (0), ...
    0x00, // derefAliases    ENUMERATED {
    0x0A, 0x01, // derefFindingBaseObj     (2),...
    0x02, 0x02, 0x01, // sizeLimit       INTEGER (0 .. maxInt), (2)
    0x02, 0x02, 0x01, // timeLimit       INTEGER (0 .. maxInt), (3)
    0x03, 0x01, 0x01, // typesOnly       BOOLEAN, (true)
    (byte) 0xFF, (byte) 0xA9, // filter          Filter,
    0x21, // MatchingRuleAssertion ::= SEQUENCE {
    (byte) 0x81, // matchingRule    [1] MatchingRuleId OPTIONAL,
    0x02, 'c', 'n', (byte) 0x82, // type            [2] AttributeDescription OPTIONAL,
    0x13, '1', '.', '2', '.', '8', '4', '0', '.', '4', '8', '0', '1', '8', '.', '1', '.', '2', '.', '2', (byte) 0x83, // matchValue      [3] AssertionValue,
    0x03, 'a', 'o', 'k', // dnAttributes    [4] BOOLEAN DEFAULT FALSE  }
    (byte) 0x84, 0x01, (byte) 0xFF, 0x30, // attributes      AttributeDescriptionList }
    0x15, 0x04, 0x05, 'a', 't', 't', 'r', '0', 0x04, 0x05, 'a', 't', 't', 'r', '1', 0x04, 0x05, 'a', 't', 't', 'r', '2' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(1, searchRequest.getMessageId());
    assertEquals("dc=example,dc=com", searchRequest.getBase().toString());
    assertEquals(SearchScope.OBJECT, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_FINDING_BASE_OBJ, searchRequest.getDerefAliases());
    assertEquals(2, searchRequest.getSizeLimit());
    assertEquals(3, searchRequest.getTimeLimit());
    assertEquals(true, searchRequest.getTypesOnly());
    // The attributes
    List<String> attributes = searchRequest.getAttributes();
    for (String attribute : attributes) {
        assertNotNull(attribute);
    }
    // attributes may have been reordered
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x63, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x56), decodedPdu.substring(0, 0x56));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 38 with SearchRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.

the class SearchRequestMatchingRuleAssertionTest method testDecodeSearchRequestExtensibleMatchMatchingRuleEmptyType.

/**
 * Test the decoding of a SearchRequest with an extensible match and an
 * matching rule and an empty type
 */
@Test
public void testDecodeSearchRequestExtensibleMatchMatchingRuleEmptyType() {
    byte[] asn1BER = new byte[] { 0x30, 0x43, 0x02, 0x01, // messageID
    0x04, 0x63, 0x3E, 0x04, // baseObject LDAPDN,
    0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0A, 0x01, 0x01, 0x0A, 0x01, 0x03, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x01, 0x01, (byte) 0xFF, (byte) 0xA9, 0x08, (byte) 0x81, 0x04, 't', 'e', 's', 't', (byte) 0x82, // type            [2] AttributeDescription OPTIONAL,
    0x00, 0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
    0x02, 0x04, 0x00 };
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(asn1BER.length);
    stream.put(asn1BER);
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    // Decode a SearchRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(true);
        return;
    }
    fail("We should not reach this point");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 39 with SearchRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.

the class SearchRequestMatchingRuleAssertionTest method testDecodeSearchRequestExtensibleMatchDnAttributesEmptyType.

/**
 * Test the decoding of a SearchRequest with an extensible match and an
 * matching rule and an empty dnAttributes
 */
@Test
public void testDecodeSearchRequestExtensibleMatchDnAttributesEmptyType() {
    byte[] asn1BER = new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x60, 0x02, 0x01, // messageID       MessageID,
    0x01, 0x63, // protocolOp      CHOICE {
    0x5B, // SearchRequest ::= [APPLICATION 3] SEQUENCE {
    0x04, // baseObject      LDAPDN, (dc=example,dc=com)
    0x11, 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', // scope           ENUMERATED {
    0x0A, 0x01, // baseObject              (0), ...
    0x00, // derefAliases    ENUMERATED {
    0x0A, 0x01, // derefFindingBaseObj     (2),...
    0x02, 0x02, 0x01, // sizeLimit       INTEGER (0 .. maxInt), (2)
    0x02, 0x02, 0x01, // timeLimit       INTEGER (0 .. maxInt), (3)
    0x03, 0x01, 0x01, // typesOnly       BOOLEAN, (true)
    (byte) 0xFF, (byte) 0xA9, // filter          Filter,
    0x20, // MatchingRuleAssertion ::= SEQUENCE {
    (byte) 0x81, // matchingRule    [1] MatchingRuleId OPTIONAL,
    0x02, 'c', 'n', (byte) 0x82, // type            [2] AttributeDescription OPTIONAL,
    0x13, '1', '.', '2', '.', '8', '4', '0', '.', '4', '8', '0', '1', '8', '.', '1', '.', '2', '.', '2', (byte) 0x83, // matchValue      [3] AssertionValue,
    0x03, 'a', 'o', 'k', // dnAttributes    [4] BOOLEAN DEFAULT FALSE  }
    (byte) 0x84, 0x00, 0x30, // attributes      AttributeDescriptionList }
    0x15, 0x04, 0x05, 'a', 't', 't', 'r', '0', 0x04, 0x05, 'a', 't', 't', 'r', '1', 0x04, 0x05, 'a', 't', 't', 'r', '2' };
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(asn1BER.length);
    stream.put(asn1BER);
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    // Decode a SearchRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(true);
        return;
    }
    fail("We should not reach this point");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 40 with SearchRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.

the class SearchRequestMatchingRuleAssertionTest method testDecodeSearchRequestExtensibleMatchMatchingRuleAlone.

/**
 * Test the decoding of a SearchRequest with an extensible match and a
 * matching rule and nothing else
 */
@Test
public void testDecodeSearchRequestExtensibleMatchMatchingRuleAlone() {
    byte[] asn1BER = new byte[] { 0x30, 0x41, 0x02, 0x01, // messageID
    0x04, 0x63, 0x3C, 0x04, // baseObject LDAPDN,
    0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0A, 0x01, 0x01, 0x0A, 0x01, 0x03, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x01, 0x01, (byte) 0xFF, (byte) 0xA9, 0x06, (byte) 0x81, 0x04, 't', 'e', 's', 't', 0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
    0x02, 0x04, 0x00 };
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(asn1BER.length);
    stream.put(asn1BER);
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    // Decode a SearchRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(true);
        return;
    }
    fail("We should not reach this point");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)110 DecoderException (org.apache.directory.api.asn1.DecoderException)97 ByteBuffer (java.nio.ByteBuffer)85 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)85 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)85 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)85 Test (org.junit.Test)85 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)48 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)37 EncoderException (org.apache.directory.api.asn1.EncoderException)33 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)19 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)18 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)17 SubstringNode (org.apache.directory.api.ldap.model.filter.SubstringNode)12 OrNode (org.apache.directory.api.ldap.model.filter.OrNode)10 Filter (org.apache.directory.api.ldap.codec.search.Filter)9 PresenceNode (org.apache.directory.api.ldap.model.filter.PresenceNode)9 SubstringFilter (org.apache.directory.api.ldap.codec.search.SubstringFilter)6 GreaterEqNode (org.apache.directory.api.ldap.model.filter.GreaterEqNode)6 NotNode (org.apache.directory.api.ldap.model.filter.NotNode)6