Search in sources :

Example 11 with OrNode

use of org.apache.directory.api.ldap.model.filter.OrNode in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestComplexFilterWithControl.

/**
 * Test the decoding of a SearchRequest with a complex filter :
 * (&(objectClass=person)(|(cn=Tori*)(sn=Jagger)))
 */
@Test
public void testDecodeSearchRequestComplexFilterWithControl() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x77);
    stream.put(new byte[] { // LdapMessage
    0x30, // LdapMessage
    0x75, 0x02, 0x01, // message Id = 6
    0x06, 0x63, // SearchRequest
    0x53, 0x04, // BaseDN 'ou=system'
    0x09, 0x6F, 0x75, 0x3D, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x0A, 0x01, // scope = SUBTREE
    0x02, 0x0A, 0x01, // derefAlias = 3
    0x03, 0x02, 0x01, // sizeLimit = none
    0x00, 0x02, 0x01, // timeLimit = none
    0x00, 0x01, 0x01, // types only = false
    0x00, (byte) 0xA0, // AND
    0x35, (byte) 0xA3, // equals
    0x15, 0x04, // 'objectclass'
    0x0B, 0x6F, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x43, 0x6C, 0x61, 0x73, 0x73, 0x04, // 'person'
    0x06, 0x70, 0x65, 0x72, 0x73, 0x6F, 0x6E, (byte) 0xA1, // OR
    0x1C, (byte) 0xA4, // substrings : 'cn=Tori*'
    0x0C, 0x04, // 'cn'
    0x02, 0x63, 0x6E, 0x30, // initial = 'Tori'
    0x06, (byte) 0x80, 0x04, 0x54, 0x6F, 0x72, 0x69, (byte) 0xA3, // equals
    0x0C, 0x04, // 'sn'
    0x02, 0x73, 0x6E, 0x04, // 'Jagger'
    0x06, 0x4A, 0x61, 0x67, 0x67, 0x65, 0x72, 0x30, // Control
    0x00, (byte) 0xA0, 0x1B, 0x30, 0x19, 0x04, 0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' });
    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(6, searchRequest.getMessageId());
    assertEquals("ou=system", searchRequest.getBase().toString());
    assertEquals(SearchScope.SUBTREE, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(0, searchRequest.getSizeLimit());
    assertEquals(0, searchRequest.getTimeLimit());
    assertEquals(false, searchRequest.getTypesOnly());
    // (&(...
    ExprNode filter = searchRequest.getFilter();
    AndNode andNode = (AndNode) filter;
    assertNotNull(andNode);
    List<ExprNode> andNodes = andNode.getChildren();
    assertEquals(2, andNodes.size());
    // (&(objectClass=person)..
    EqualityNode<?> equalityNode = (EqualityNode<?>) andNodes.get(0);
    assertNotNull(equalityNode);
    assertEquals("objectClass", equalityNode.getAttribute());
    assertEquals("person", equalityNode.getValue().getValue());
    // (&(a=b)(|
    OrNode orNode = (OrNode) andNodes.get(1);
    assertNotNull(orNode);
    List<ExprNode> orNodes = orNode.getChildren();
    assertEquals(2, orNodes.size());
    // (&(a=b)(|(cn=Tori*
    SubstringNode substringNode = (SubstringNode) orNodes.get(0);
    assertNotNull(substringNode);
    assertEquals("cn", substringNode.getAttribute());
    assertEquals("Tori", substringNode.getInitial());
    assertEquals(0, substringNode.getAny().size());
    assertEquals(null, substringNode.getFinal());
    // (&(a=b)(|(cn=Tori*)(sn=Jagger)))
    equalityNode = (EqualityNode<?>) orNodes.get(1);
    assertNotNull(equalityNode);
    assertEquals("sn", equalityNode.getAttribute());
    assertEquals("Jagger", equalityNode.getValue().getValue());
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) SubstringNode(org.apache.directory.api.ldap.model.filter.SubstringNode) 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 12 with OrNode

use of org.apache.directory.api.ldap.model.filter.OrNode in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestCompareFiltersNoControls.

/**
 * Test the decoding of a SearchRequest with no controls. Test the various
 * types of filter : >=, <=, ~= The search filter is :
 * (&(|(objectclass~=top)(ou<=contacts))(!(objectclass>=ttt)))
 */
@Test
public void testDecodeSearchRequestCompareFiltersNoControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x90);
    stream.put(new byte[] { 0x30, (byte) 0x81, // LDAPMessage ::=SEQUENCE {
    (byte) 0x8D, 0x02, 0x01, // messageID MessageID
    0x01, 0x63, (byte) 0x81, // CHOICE { ...,
    (byte) 0x87, // 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 ENUMERATED {
    0x01, // wholeSubtree (2) },
    0x0A, 0x01, // derefAliases ENUMERATED {
    0x03, // derefAlways (3) },
    0x02, 0x02, 0x03, // sizeLimit INTEGER (0 .. maxInt), (1000)
    (byte) 0xE8, 0x02, 0x02, 0x03, // timeLimit INTEGER (0 .. maxInt), (1000)
    (byte) 0xE8, 0x01, 0x01, // typesOnly BOOLEAN, (TRUE)
    (byte) 0xFF, // filter Filter,
    (byte) 0xA0, // Filter ::= CHOICE {
    0x3C, // and [0] SET OF Filter,
    (byte) 0xA1, // or [1] SET of Filter,
    0x24, (byte) 0xA8, // approxMatch [8]
    0x12, // Assertion ::= SEQUENCE {
    0x04, // attributeDesc AttributeDescription (LDAPString),
    0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x04, // attributeDesc AttributeDescription (LDAPString),
    0x03, 't', 'o', 'p', (byte) 0xA6, // lessOrEqual [3] Assertion,
    0x0E, 0x04, // Assertion ::= SEQUENCE {
    0x02, 'o', // attributeDesc AttributeDescription (LDAPString),
    'u', 0x04, // assertionValue AssertionValue (OCTET STRING) }
    0x08, 'c', 'o', 'n', 't', 'a', 'c', 't', 's', (byte) 0xA2, // not [2] Filter,
    0x14, (byte) 0xA5, // greaterOrEqual [5] Assertion,
    0x12, // Assertion ::= SEQUENCE {
    0x04, // attributeDesc AttributeDescription (LDAPString),
    0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x04, 0x03, 't', 't', // assertionValue AssertionValue (OCTET STRING) }
    't', // attributes AttributeDescriptionList }
    0x30, // AttributeDescriptionList ::= SEQUENCE OF
    0x15, // AttributeDescription
    0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
    '0', 0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
    '1', 0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
    '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());
    }
    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);
    // (& (| (objectclass~=top) (...
    List<ExprNode> orNodes = orFilter.getChildren();
    assertEquals(2, orNodes.size());
    ApproximateNode<?> approxNode = (ApproximateNode<?>) orNodes.get(0);
    assertNotNull(approxNode);
    assertEquals("objectclass", approxNode.getAttribute());
    assertEquals("top", approxNode.getValue().getValue());
    // (& (| (objectclass~=top) (ou<=contacts) ) (...
    LessEqNode<?> lessOrEqualNode = (LessEqNode<?>) orNodes.get(1);
    assertNotNull(lessOrEqualNode);
    assertEquals("ou", lessOrEqualNode.getAttribute());
    assertEquals("contacts", lessOrEqualNode.getValue().getValue());
    // (& (| (objectclass~=top) (ou<=contacts) ) (! ...
    NotNode notNode = (NotNode) andNodes.get(1);
    assertNotNull(notNode);
    // (& (| (objectclass~=top) (ou<=contacts) ) (! (objectclass>=ttt) ) )
    GreaterEqNode<?> greaterOrEqual = (GreaterEqNode<?>) notNode.getFirstChild();
    assertNotNull(greaterOrEqual);
    assertEquals("objectclass", greaterOrEqual.getAttribute());
    assertEquals("ttt", greaterOrEqual.getValue().getValue());
    // 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(0x0090, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x81), decodedPdu.substring(0, 0x81));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) ApproximateNode(org.apache.directory.api.ldap.model.filter.ApproximateNode) 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) EncoderException(org.apache.directory.api.asn1.EncoderException) GreaterEqNode(org.apache.directory.api.ldap.model.filter.GreaterEqNode) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) LessEqNode(org.apache.directory.api.ldap.model.filter.LessEqNode) 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 13 with OrNode

use of org.apache.directory.api.ldap.model.filter.OrNode in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestAndEq_OrEqEq.

/**
 * Test the decoding of a SearchRequest
 * (&(a=b)(|(a=b)(c=d)))
 */
@Test
public void testDecodeSearchRequestAndEq_OrEqEq() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x39);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x37, 0x02, 0x01, // messageID MessageID
    0x01, 0x63, // CHOICE { ...,
    0x32, // SearchRequest ::= APPLICATION[3] SEQUENCE {
    0x04, // baseObject LDAPDN,
    0x03, 'a', '=', 'b', 0x0A, 0x01, // scope ENUMERATED {
    0x01, // wholeSubtree (2) },
    0x0A, 0x01, // derefAliases ENUMERATED {
    0x03, // derefAlways (3) },
    0x02, 0x01, // sizeLimit INTEGER (0 .. maxInt), (0)
    0x00, 0x02, 0x01, // timeLimit INTEGER (0 .. maxInt), (1000)
    0x00, 0x01, 0x01, // typesOnly BOOLEAN, (TRUE)
    (byte) 0xFF, // filter Filter,
    (byte) 0xA0, // Filter ::= CHOICE { and             [0] SET OF Filter,
    0x1A, (byte) 0xA3, // equalityMatch [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'a', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'b', (byte) 0xA1, // Filter ::= CHOICE { or             [1] SET OF Filter,
    0x10, (byte) 0xA3, // equalityMatch [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'c', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'd', (byte) 0xA3, // equalityMatch [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'e', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'f', 0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
    0x00 });
    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());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(1, searchRequest.getMessageId());
    assertEquals("a=b", searchRequest.getBase().toString());
    assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(0, searchRequest.getSizeLimit());
    assertEquals(0, searchRequest.getTimeLimit());
    assertEquals(true, searchRequest.getTypesOnly());
    // (&(...
    ExprNode exprNode = searchRequest.getFilter();
    AndNode andNode = (AndNode) exprNode;
    assertNotNull(andNode);
    List<ExprNode> andNodes = andNode.getChildren();
    assertEquals(2, andNodes.size());
    // (&(a=b)..
    EqualityNode<?> equalityNode = (EqualityNode<?>) andNodes.get(0);
    assertNotNull(equalityNode);
    assertEquals("a", equalityNode.getAttribute());
    assertEquals("b", equalityNode.getValue().getValue());
    // (&(a=b)(|(...
    OrNode orNode = (OrNode) andNodes.get(1);
    assertNotNull(orNode);
    List<ExprNode> orNodes = orNode.getChildren();
    assertEquals(2, orNodes.size());
    // (&(a=b)(|(c=d)...
    equalityNode = (EqualityNode<?>) orNodes.get(0);
    assertNotNull(equalityNode);
    assertEquals("c", equalityNode.getAttribute());
    assertEquals("d", equalityNode.getValue().getValue());
    // (&(a=b)(|(c=d)(e=f)))
    equalityNode = (EqualityNode<?>) orNodes.get(1);
    assertNotNull(equalityNode);
    assertEquals("e", equalityNode.getAttribute());
    assertEquals("f", equalityNode.getValue().getValue());
    List<String> attributes = searchRequest.getAttributes();
    assertEquals(0, attributes.size());
    // attributes may have been reordered
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x39, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x39), decodedPdu.substring(0, 0x39));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) 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) EncoderException(org.apache.directory.api.asn1.EncoderException) 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 14 with OrNode

use of org.apache.directory.api.ldap.model.filter.OrNode in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestEmptyBaseDnNoControls.

/**
 * Test the decoding of a SearchRequest with no controls. The search filter
 * is : (&(|(objectclass=top)(ou=contacts))(!(objectclass=ttt)))
 */
@Test
public void testDecodeSearchRequestEmptyBaseDnNoControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x6F);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x6D, 0x02, 0x01, // messageID MessageID
    0x01, 0x63, // CHOICE { ...,
    0x68, // SearchRequest ::= APPLICATION[3] SEQUENCE {
    0x04, // baseObject LDAPDN,
    0x00, 0x0A, 0x01, // scope ENUMERATED {
    0x01, // wholeSubtree (2) },
    0x0A, 0x01, // derefAliases ENUMERATED {
    0x03, // derefAlways (3) },
    0x02, 0x02, 0x03, // sizeLimit INTEGER (0 .. maxInt), (1000)
    (byte) 0xE8, 0x02, 0x02, 0x03, // timeLimit INTEGER (0 .. maxInt), (1000)
    (byte) 0xE8, 0x01, 0x01, // typesOnly  BOOLEAN, (TRUE)
    (byte) 0xFF, // filter Filter,
    (byte) 0xA0, // Filter ::= CHOICE {
    0x3C, // and [0] SET OF Filter,
    (byte) 0xA1, // or [1] SET of Filter,
    0x24, (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 [3] Assertion,
    0x0E, // Assertion ::= SEQUENCE {
    0x04, 0x02, 'o', // attributeDesc AttributeDescription (LDAPString),
    'u', // assertionValue AssertionValue (OCTET STRING) }
    0x04, 0x08, 'c', 'o', 'n', 't', 'a', 'c', 't', 's', (byte) 0xA2, // not [2] Filter,
    0x14, (byte) 0xA3, // equalityMatch [3] Assertion,
    0x12, // attributeDesc AttributeDescription (LDAPString),
    0x04, 0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', // assertionValue AssertionValue (OCTET STRING) }
    0x04, 0x03, 't', 't', 't', // attributes AttributeDescriptionList }
    0x30, // AttributeDescriptionList ::= SEQUENCE OF
    0x15, // AttributeDescription
    0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
    '0', 0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
    '1', 0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
    '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());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(1, searchRequest.getMessageId());
    assertEquals("", 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("ou", 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("objectclass", equalityNode.getAttribute());
    assertEquals("ttt", equalityNode.getValue().getValue());
    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(0x6F, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x6F), decodedPdu.substring(0, 0x6F));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
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) EncoderException(org.apache.directory.api.asn1.EncoderException) 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 15 with OrNode

use of org.apache.directory.api.ldap.model.filter.OrNode in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestAnd_OrPrPr_NotGEq.

/**
 * Test the decoding of a SearchRequest
 * (&(|(abcdef=*)(ghijkl=*))(!(e>=f)))
 */
@Test
public void testDecodeSearchRequestAnd_OrPrPr_NotGEq() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x3B);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x39, 0x02, 0x01, // messageID MessageID
    0x01, 0x63, // CHOICE { ...,
    0x34, // SearchRequest ::= APPLICATION[3] SEQUENCE {
    0x04, // baseObject LDAPDN,
    0x03, 'a', '=', 'b', 0x0A, 0x01, // scope ENUMERATED {
    0x01, // wholeSubtree (2) },
    0x0A, 0x01, // derefAliases ENUMERATED {
    0x03, // derefAlways (3) },
    0x02, 0x01, // sizeLimit INTEGER (0 .. maxInt), (0)
    0x00, 0x02, 0x01, // timeLimit INTEGER (0 .. maxInt), (1000)
    0x00, 0x01, 0x01, // typesOnly BOOLEAN, (TRUE)
    (byte) 0xFF, // filter Filter,
    (byte) 0xA0, // Filter ::= CHOICE { and             [0] SET OF Filter,
    0x1C, (byte) 0xA1, // Filter ::= CHOICE { or             [0] SET OF Filter,
    0x10, (byte) 0x87, // present [7] AttributeDescription,
    0x06, 'a', 'b', // AttributeDescription ::= LDAPString
    'c', 'd', 'e', 'f', (byte) 0x87, // present [7] AttributeDescription,
    0x06, 'g', 'h', // AttributeDescription ::= LDAPString
    'i', 'j', 'k', 'l', (byte) 0xA2, // Filter ::= CHOICE { not             Filter,
    0x08, (byte) 0xA5, // greaterOrEqual [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'e', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'f', 0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
    0x00 });
    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());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(1, searchRequest.getMessageId());
    assertEquals("a=b", searchRequest.getBase().toString());
    assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(0, searchRequest.getSizeLimit());
    assertEquals(0, 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);
    List<ExprNode> orNodes = orFilter.getChildren();
    assertEquals(2, orNodes.size());
    // (&(&(abcdef=*)...
    PresenceNode presenceNode = (PresenceNode) orNodes.get(0);
    assertNotNull(presenceNode);
    assertEquals("abcdef", presenceNode.getAttribute());
    // (&(&(abcdef=*)(ghijkl=*))...
    presenceNode = (PresenceNode) orNodes.get(1);
    assertNotNull(presenceNode);
    assertEquals("ghijkl", presenceNode.getAttribute());
    // (&(&(abcdef=*)(ghijkl=*))(&...
    NotNode notNode = (NotNode) andNodes.get(1);
    assertNotNull(notNode);
    // (&(&(abcdef=*)(ghijkl=*))(&(e>=f)))
    GreaterEqNode<?> greaterEqNode = (GreaterEqNode<?>) notNode.getFirstChild();
    assertNotNull(greaterEqNode);
    assertEquals("e", greaterEqNode.getAttribute());
    assertEquals("f", greaterEqNode.getValue().getValue());
    List<String> attributes = searchRequest.getAttributes();
    assertEquals(0, attributes.size());
    // attributes may have been reordered
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x3B, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x3B), decodedPdu.substring(0, 0x3B));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
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) PresenceNode(org.apache.directory.api.ldap.model.filter.PresenceNode) 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) EncoderException(org.apache.directory.api.asn1.EncoderException) GreaterEqNode(org.apache.directory.api.ldap.model.filter.GreaterEqNode) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) 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)

Aggregations

OrNode (org.apache.directory.api.ldap.model.filter.OrNode)16 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)14 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)13 DecoderException (org.apache.directory.api.asn1.DecoderException)11 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)11 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)11 Test (org.junit.Test)11 ByteBuffer (java.nio.ByteBuffer)10 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)10 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)10 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)10 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)10 NotNode (org.apache.directory.api.ldap.model.filter.NotNode)9 PresenceNode (org.apache.directory.api.ldap.model.filter.PresenceNode)8 EncoderException (org.apache.directory.api.asn1.EncoderException)7 GreaterEqNode (org.apache.directory.api.ldap.model.filter.GreaterEqNode)7 SubstringNode (org.apache.directory.api.ldap.model.filter.SubstringNode)6 ApproximateNode (org.apache.directory.api.ldap.model.filter.ApproximateNode)5 Value (org.apache.directory.api.ldap.model.entry.Value)4 ExtensibleNode (org.apache.directory.api.ldap.model.filter.ExtensibleNode)4