Search in sources :

Example 21 with EqualityNode

use of org.apache.directory.api.ldap.model.filter.EqualityNode 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 22 with EqualityNode

use of org.apache.directory.api.ldap.model.filter.EqualityNode 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 23 with EqualityNode

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

the class SearchRequestTest method testDecodeSearchRequestAndAndEqEq.

/**
 * Test the decoding of a SearchRequest
 * (&(&(a=b)(c=d))
 */
@Test
public void testDecodeSearchRequestAndAndEqEq() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x31);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x2F, 0x02, 0x01, // messageID MessageID
    0x01, 0x63, // CHOICE { ...,
    0x2A, // 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,
    0x12, (byte) 0xA0, // Filter ::= CHOICE { and             [0] SET OF Filter,
    0x10, (byte) 0xA3, 0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'a', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'b', (byte) 0xA3, 0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'c', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'd', 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(1, andNodes.size());
    // (&(&(..
    AndNode andNode2 = (AndNode) andNodes.get(0);
    assertNotNull(andNode2);
    List<ExprNode> andNodes2 = andNode2.getChildren();
    assertEquals(2, andNodes2.size());
    // (&(&(a=b)...
    EqualityNode<?> equalityNode = (EqualityNode<?>) andNodes2.get(0);
    assertNotNull(equalityNode);
    assertEquals("a", equalityNode.getAttribute());
    assertEquals("b", equalityNode.getValue().getValue());
    // (&(&(a=b)(c=d)
    equalityNode = (EqualityNode<?>) andNodes2.get(1);
    assertNotNull(equalityNode);
    assertEquals("c", equalityNode.getAttribute());
    assertEquals("d", 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(0x31, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x31), decodedPdu.substring(0, 0x31));
    } 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) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 24 with EqualityNode

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

the class SearchRequestTest method testDecodeSearchRequestOrFilters.

/**
 * Tests an search request decode with a simple equality match filter.
 */
@Test
public void testDecodeSearchRequestOrFilters() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x96);
    stream.put(new byte[] { 0x30, (byte) 0x81, (byte) 0x93, 0x02, 0x01, 0x21, 0x63, (byte) 0x81, // "dc=example,dc=com"
    (byte) 0x8D, 0x04, 0x11, 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0A, 0x01, 0x00, 0x0A, 0x01, 0x02, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03, 0x01, 0x01, (byte) 0xFF, (byte) 0xA1, // ( |
    0x52, (byte) 0xA3, // ( uid=akarasulu )
    0x10, 0x04, 0x03, 'u', 'i', 'd', 0x04, 0x09, 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', (byte) 0xA3, // ( cn=aok )
    0x09, 0x04, 0x02, 'c', 'n', 0x04, 0x03, 'a', 'o', 'k', (byte) 0xA3, // ( ou=Human Resources )
    0x15, 0x04, 0x02, 'o', 'u', 0x04, 0x0F, 'H', 'u', 'm', 'a', 'n', ' ', 'R', 'e', 's', 'o', 'u', 'r', 'c', 'e', 's', (byte) 0xA3, 0x10, 0x04, 0x01, // (l=Santa Clara )
    'l', 0x04, 0x0B, 'S', 'a', 'n', 't', 'a', ' ', 'C', 'l', 'a', 'r', 'a', (byte) 0xA3, // ( cn=abok ))
    0x0A, 0x04, 0x02, 'c', 'n', 0x04, 0x04, 'a', 'b', 'o', 'k', 0x30, // Attributes
    0x15, 0x04, 0x05, 'a', 't', 't', 'r', // attr0
    '0', 0x04, 0x05, 'a', 't', 't', 'r', // attr1
    '1', 0x04, 0x05, 'a', 't', 't', 'r', // attr2
    '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(33, 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());
    // (objectclass=t*)
    OrNode orNode = (OrNode) searchRequest.getFilter();
    assertNotNull(orNode);
    assertEquals(5, orNode.getChildren().size());
    // uid=akarasulu
    EqualityNode<?> equalityNode = (EqualityNode<?>) orNode.getChildren().get(0);
    assertEquals("uid", equalityNode.getAttribute());
    assertEquals("akarasulu", equalityNode.getValue().getValue());
    // cn=aok
    equalityNode = (EqualityNode<?>) orNode.getChildren().get(1);
    assertEquals("cn", equalityNode.getAttribute());
    assertEquals("aok", equalityNode.getValue().getValue());
    // ou = Human Resources
    equalityNode = (EqualityNode<?>) orNode.getChildren().get(2);
    assertEquals("ou", equalityNode.getAttribute());
    assertEquals("Human Resources", equalityNode.getValue().getValue());
    // l=Santa Clara
    equalityNode = (EqualityNode<?>) orNode.getChildren().get(3);
    assertEquals("l", equalityNode.getAttribute());
    assertEquals("Santa Clara", equalityNode.getValue().getValue());
    // cn=abok
    equalityNode = (EqualityNode<?>) orNode.getChildren().get(4);
    assertEquals("cn", equalityNode.getAttribute());
    assertEquals("abok", equalityNode.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(0x0096, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x87), decodedPdu.substring(0, 0x87));
    } 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) ByteBuffer(java.nio.ByteBuffer) 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 25 with EqualityNode

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

the class SearchRequestTest method testDecodeSearchRequestAnd_AndEqEq_Eq.

/**
 * Test the decoding of a SearchRequest
 * (&(&(a=b)(c=d))(e=f))
 */
@Test
public void testDecodeSearchRequestAnd_AndEqEq_Eq() {
    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) 0xA0, // Filter ::= CHOICE { and             [0] SET OF Filter,
    0x10, (byte) 0xA3, // equalityMatch [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'a', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'b', (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 filter = searchRequest.getFilter();
    AndNode andNode = (AndNode) filter;
    assertNotNull(andNode);
    List<ExprNode> andNodes = andNode.getChildren();
    assertEquals(2, andNodes.size());
    // (&(&(..
    AndNode andNode2 = (AndNode) andNodes.get(0);
    assertNotNull(andNode2);
    List<ExprNode> andNodes2 = andNode2.getChildren();
    assertEquals(2, andNodes2.size());
    // (&(&(a=b)...
    EqualityNode<?> equalityNode = (EqualityNode<?>) andNodes2.get(0);
    assertNotNull(equalityNode);
    assertEquals("a", equalityNode.getAttribute());
    assertEquals("b", equalityNode.getValue().getValue());
    // (&(&(a=b)(c=d)...
    equalityNode = (EqualityNode<?>) andNodes2.get(1);
    assertNotNull(equalityNode);
    assertEquals("c", equalityNode.getAttribute());
    assertEquals("d", equalityNode.getValue().getValue());
    // (&(&(a=b)(c=d))(e=f))
    equalityNode = (EqualityNode<?>) andNodes.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) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

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