Java Code Examples for org.apache.directory.api.ldap.model.message.SearchRequest#getAttributes()

The following examples show how to use org.apache.directory.api.ldap.model.message.SearchRequest#getAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with an Attributes element with 1 Attribute element
 * @throws NamingException
 */
@Test
public void testRequestWithAttributes1Attribute() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( SearchRequestTest.class.getResource( "request_with_attributes_1_attribute.xml" )
            .openStream(), "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    SearchRequest searchRequest = ( SearchRequest ) parser.getBatchRequest().getCurrentRequest();

    List<String> attributes = searchRequest.getAttributes();
    assertEquals( 1, attributes.size() );

    String attribute = attributes.get( 0 );
    assertEquals( "sn", attribute );
}
 
Example 2
Source File: SearchRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with an Attributes element with 2 Attribute elements
 * @throws NamingException
 */
@Test
public void testRequestWithAttributes2Attribute() throws LdapException
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( SearchRequestTest.class.getResource( "request_with_attributes_2_attribute.xml" )
            .openStream(), "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    SearchRequest searchRequest = ( SearchRequest ) parser.getBatchRequest().getCurrentRequest();

    List<String> attributes = searchRequest.getAttributes();
    assertEquals( 2, attributes.size() );

    String attribute1 = attributes.get( 0 );
    assertEquals( "sn", attribute1 );

    String attribute2 = attributes.get( 1 );
    assertEquals( "givenName", attribute2 );
}
 
Example 3
Source File: SearchRequestDsml.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Element toDsml( Element root )
{
    Element element = super.toDsml( root );

    SearchRequest request = getDecorated();

    // Dn
    if ( request.getBase() != null )
    {
        element.addAttribute( "dn", request.getBase().getName() );
    }

    // Scope
    SearchScope scope = request.getScope();
    if ( scope != null )
    {
        if ( scope == SearchScope.OBJECT )
        {
            element.addAttribute( "scope", "baseObject" );
        }
        else if ( scope == SearchScope.ONELEVEL )
        {
            element.addAttribute( "scope", "singleLevel" );
        }
        else if ( scope == SearchScope.SUBTREE )
        {
            element.addAttribute( "scope", "wholeSubtree" );
        }
    }

    // DerefAliases
    AliasDerefMode derefAliases = request.getDerefAliases();

    switch ( derefAliases )
    {
        case NEVER_DEREF_ALIASES:
            element.addAttribute( DEREF_ALIASES, "neverDerefAliases" );
            break;

        case DEREF_ALWAYS:
            element.addAttribute( DEREF_ALIASES, "derefAlways" );
            break;

        case DEREF_FINDING_BASE_OBJ:
            element.addAttribute( DEREF_ALIASES, "derefFindingBaseObj" );
            break;

        case DEREF_IN_SEARCHING:
            element.addAttribute( DEREF_ALIASES, "derefInSearching" );
            break;

        default:
            throw new IllegalStateException( I18n.err( I18n.ERR_03043_UNEXPECTED_DEREF_ALIAS, derefAliases ) );
    }

    // SizeLimit
    if ( request.getSizeLimit() != 0L )
    {
        element.addAttribute( "sizeLimit", Long.toString( request.getSizeLimit() ) );
    }

    // TimeLimit
    if ( request.getTimeLimit() != 0 )
    {
        element.addAttribute( "timeLimit", Integer.toString( request.getTimeLimit() ) );
    }

    // TypesOnly
    if ( request.getTypesOnly() )
    {
        element.addAttribute( "typesOnly", "true" );
    }

    // Filter
    Element filterElement = element.addElement( "filter" );
    toDsml( filterElement, request.getFilter() );

    // Attributes
    List<String> attributes = request.getAttributes();

    if ( !attributes.isEmpty() )
    {
        Element attributesElement = element.addElement( "attributes" );

        for ( String entryAttribute : attributes )
        {
            attributesElement.addElement( "attribute" ).addAttribute( NAME, entryAttribute );
        }
    }

    return element;
}
 
Example 4
Source File: SearchRequestFactory.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Encode the SearchRequest message to a PDU.
 * <br>
 * SearchRequest :
 * <pre>
 * 0x63 LL
 *   0x04 LL baseObject
 *   0x0A 01 scope
 *   0x0A 01 derefAliases
 *   0x02 0N sizeLimit
 *   0x02 0N timeLimit
 *   0x01 0x01 typesOnly
 *   filter.encode()
 *   0x30 LL attributeDescriptionList
 *     0x04 LL attributeDescription
 *     ...
 *     0x04 LL attributeDescription
 * </pre>
 *
 * @param codec The LdapApiService instance
 * @param buffer The buffer where to put the PDU
 * @param message the ModifyRequest to encode
 */
@Override
public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
{
    int start = buffer.getPos();
    SearchRequest searchRequest = ( SearchRequest ) message;

    // The attributes, if any
    List<String> attributes = searchRequest.getAttributes();

    if ( ( attributes != null ) && ( !attributes.isEmpty() ) )
    {
        for ( int i = attributes.size(); i > 0; i-- )
        {
            BerValue.encodeOctetString( buffer, attributes.get( i - 1 ) );
        }
    }

    // The attributes sequence
    BerValue.encodeSequence( buffer, start );

    // The filter
    encodeFilter( buffer, searchRequest.getFilter() );

    // The typesOnly
    BerValue.encodeBoolean( buffer, searchRequest.getTypesOnly() );

    // The timeLimit
    BerValue.encodeInteger( buffer, searchRequest.getTimeLimit() );

    // The sizeLimit
    BerValue.encodeInteger( buffer, searchRequest.getSizeLimit() );

    // The derefAliases
    BerValue.encodeEnumerated( buffer, searchRequest.getDerefAliases().getValue() );

    // The scope
    BerValue.encodeEnumerated( buffer, searchRequest.getScope().getScope() );

    // The base object
    BerValue.encodeOctetString( buffer, Strings.getBytesUtf8( searchRequest.getBase().getName() ) );

    // The SearchRequest tag
    BerValue.encodeSequence( buffer, LdapCodecConstants.SEARCH_REQUEST_TAG, start );
}
 
Example 5
Source File: SearchRequestSubstringTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with a substring filter. Test the
 * initial filter : (objectclass=t*)
 */
@Test
public void testDecodeSearchRequestSubstringInitialAny() throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x64 );
    stream.put( new byte[]
        {
            0x30, 0x62,                             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,                     // messageID
              0x63, 0x5D,                           //      CHOICE { ..., searchRequest SearchRequest, ...
                                                    // SearchRequest ::= APPLICATION[3] SEQUENCE {
                0x04, 0x1F,                         // baseObject LDAPDN,
                  '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,                   // scope ENUMERATED {
                                                    // baseObject (0),
                                                    // singleLevel (1),
                                                    // wholeSubtree (2) },
                0x0A, 0x01, 0x03,                   // derefAliases ENUMERATED {
                                                    // neverDerefAliases (0),
                                                    // derefInSearching (1),
                                                    // derefFindingBaseObj (2),
                                                    // derefAlways (3) },
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // sizeLimit INTEGER (0 .. maxInt), (1000)
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // timeLimit INTEGER (0 .. maxInt), (1000)
                0x01, 0x01, ( byte ) 0xFF,          // typesOnly BOOLEAN, (TRUE) filter Filter,
                ( byte ) 0xA4, 0x12,                // Filter ::= CHOICE {
                                                    // substrings [4] SubstringFilter
                                                    // }
                                                    // SubstringFilter ::= SEQUENCE {
                  0x04, 0x0B,
                    'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x30, 0x03,
                    ( byte ) 0x80, 0x01,
                      't',
                0x30, 0x15,                         // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2'         // AttributeDescription ::= LDAPString
    } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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() );

    // (objectclass=t*)
    ExprNode node = searchRequest.getFilter();
    SubstringNode substringNode = ( SubstringNode ) node;
    assertNotNull( substringNode );

    assertEquals( "objectclass", substringNode.getAttribute() );
    assertEquals( "t", substringNode.getInitial() );

    // The attributes
    List<String> attributes = searchRequest.getAttributes();

    for ( String attribute : attributes )
    {
        assertNotNull( attribute );
    }

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 6
Source File: SearchRequestSubstringTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with a substring filter. Test the
 * any filter : (objectclass=*t*)
 */
@Test
public void testDecodeSearchRequestSubstringAny() throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x64 );
    stream.put( new byte[]
        {
            0x30, 0x62,                         // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,             // messageID
              0x63, 0x5D,                           // CHOICE { ..., searchRequest SearchRequest, ...
                                                // SearchRequest ::= APPLICATION[3] SEQUENCE {
                0x04, 0x1F,                     // baseObject LDAPDN,
                  '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,               // scope ENUMERATED {
                                                // baseObject (0),
                                                // singleLevel (1),
                                                // wholeSubtree (2) },
                0x0A, 0x01, 0x03,               // derefAliases ENUMERATED {
                                                // neverDerefAliases (0),
                                                // derefInSearching (1),
                                                // derefFindingBaseObj (2),
                                                // derefAlways (3) },
                0x02, 0x02, 0x03, ( byte ) 0xE8,// sizeLimit INTEGER (0 .. maxInt), (1000)
                0x02, 0x02, 0x03, ( byte ) 0xE8,// timeLimit INTEGER (0 .. maxInt), (1000)
                0x01, 0x01, ( byte ) 0xFF,      // typesOnly BOOLEAN, (TRUE) filter Filter,
                ( byte ) 0xA4, 0x12,            // Filter ::= CHOICE {
                                                // substrings [4] SubstringFilter
                                                // }
                                                // SubstringFilter ::= SEQUENCE {
                  0x04, 0x0B,
                    'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x30, 0x03,
                    ( byte ) 0x81, 0x01,
                      't',
                0x30, 0x15,                     // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',    // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',    // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2'     // AttributeDescription ::= LDAPString
    } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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() );

    // (objectclass=t*)
    ExprNode node = searchRequest.getFilter();
    SubstringNode substringNode = ( SubstringNode ) node;
    assertNotNull( substringNode );

    assertEquals( "objectclass", substringNode.getAttribute() );
    assertEquals( null, substringNode.getInitial() );
    assertEquals( "t", substringNode.getAny().get( 0 ) );
    assertEquals( null, substringNode.getFinal() );

    // The attributes
    List<String> attributes = searchRequest.getAttributes();

    for ( String attribute : attributes )
    {
        assertNotNull( attribute );
    }

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 7
Source File: SearchRequestSubstringTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with a substring filter. Test the
 * initial filter : (objectclass=*t*t)
 */
@Test
public void testDecodeSearchRequestSubstringAnyFinal() throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x67 );
    stream.put( new byte[]
        {
            0x30, 0x65,                             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,                     // messageID
              0x63, 0x60,                           // CHOICE { ..., searchRequest SearchRequest, ...
                                                    // SearchRequest ::= APPLICATION[3] SEQUENCE {
                0x04, 0x1F,                         // baseObject LDAPDN,
                  '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,                   // scope ENUMERATED {
                                                    // baseObject (0),
                                                    // singleLevel (1),
                                                    // wholeSubtree (2) },
                0x0A, 0x01, 0x03,                   // derefAliases ENUMERATED {
                                                    // neverDerefAliases (0),
                                                    // derefInSearching (1),
                                                    // derefFindingBaseObj (2),
                                                    // derefAlways (3) },
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // sizeLimit INTEGER (0 .. maxInt), (1000)
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // timeLimit INTEGER (0 .. maxInt), (1000)
                0x01, 0x01, ( byte ) 0xFF,          // typesOnly BOOLEAN, (TRUE) filter Filter,
                ( byte ) 0xA4, 0x15,                // Filter ::= CHOICE {
                                                    // substrings [4] SubstringFilter
                                                    // }
                                                    // SubstringFilter ::= SEQUENCE {
                  0x04, 0x0B,
                    'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x30, 0x06,
                    ( byte ) 0x81, 0x01,
                      't',
                    ( byte ) 0x82, 0x01,
                      't',
                0x30, 0x15,                         // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2'         // AttributeDescription ::= LDAPString
    } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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() );

    // (objectclass=t*)
    ExprNode node = searchRequest.getFilter();
    SubstringNode substringNode = ( SubstringNode ) node;
    assertNotNull( substringNode );

    assertEquals( "objectclass", substringNode.getAttribute() );
    assertEquals( null, substringNode.getInitial() );
    assertEquals( "t", substringNode.getAny().get( 0 ) );
    assertEquals( "t", substringNode.getFinal() );

    // The attributes
    List<String> attributes = searchRequest.getAttributes();

    for ( String attribute : attributes )
    {
        assertNotNull( attribute );
    }

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 8
Source File: SearchRequestSubstringTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with a substring filter. Test the
 * initial filter : (objectclass=t*t*t)
 */
@Test
public void testDecodeSearchRequestSubstringInitialAnyFinal()
    throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x6A );
    stream.put( new byte[]
        {
            0x30, 0x68,                             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,                     // messageID
              0x63, 0x63,                           // CHOICE { ..., searchRequest SearchRequest, ...
                                                    // SearchRequest ::= APPLICATION[3] SEQUENCE {
                0x04, 0x1F,                         // baseObject LDAPDN,
                  '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,                   // scope ENUMERATED {
                                                    // baseObject (0),
                                                    // singleLevel (1),
                                                    // wholeSubtree (2) },
                0x0A, 0x01, 0x03,                   // derefAliases ENUMERATED {
                                                    // neverDerefAliases (0),
                                                    // derefInSearching (1),
                                                    // derefFindingBaseObj (2),
                                                    // derefAlways (3) },
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // sizeLimit INTEGER (0 .. maxInt), (1000)
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // timeLimit INTEGER (0 .. maxInt), (1000)
                0x01, 0x01, ( byte ) 0xFF,          // typesOnly BOOLEAN, (TRUE) filter Filter,
                ( byte ) 0xA4, 0x18,                // Filter ::= CHOICE {
                                                    // substrings [4] SubstringFilter
                                                    // }
                                                    // SubstringFilter ::= SEQUENCE {
                  0x04, 0x0B,
                    'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x30, 0x09,
                    ( byte ) 0x80, 0x01,
                      't',
                    ( byte ) 0x81, 0x01,
                      't',
                    ( byte ) 0x82, 0x01,
                      't',
                0x30, 0x15,                         // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2'         // AttributeDescription ::= LDAPString
    } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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() );

    // (objectclass=t*)
    ExprNode node = searchRequest.getFilter();
    SubstringNode substringNode = ( SubstringNode ) node;
    assertNotNull( substringNode );

    assertEquals( "objectclass", substringNode.getAttribute() );
    assertEquals( "t", substringNode.getInitial() );
    assertEquals( "t", substringNode.getAny().get( 0 ) );
    assertEquals( "t", substringNode.getFinal() );

    // The attributes
    List<String> attributes = searchRequest.getAttributes();

    for ( String attribute : attributes )
    {
        assertNotNull( attribute );
    }

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 9
Source File: SearchRequestSubstringTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with a substring filter. Test the
 * initial filter : (objectclass=t*t*)
 */
@Test
public void testDecodeSearchRequestSubstringInitialAnyAnyFinal()
    throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x67 );
    stream.put( new byte[]
        {
            0x30, 0x65,                             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,                     // messageID
              0x63, 0x60,                           // CHOICE { ..., searchRequest SearchRequest, ...
                                                    // SearchRequest ::= APPLICATION[3] SEQUENCE {
                0x04, 0x1F,                         // baseObject LDAPDN,
                  '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,                   // scope ENUMERATED {
                                                    // baseObject (0),
                                                    // singleLevel (1),
                                                    // wholeSubtree (2) },
                0x0A, 0x01, 0x03,                   // derefAliases ENUMERATED {
                                                    // neverDerefAliases (0),
                                                    // derefInSearching (1),
                                                    // derefFindingBaseObj (2),
                                                    // derefAlways (3) },
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // sizeLimit INTEGER (0 .. maxInt), (1000)
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // timeLimit INTEGER (0 .. maxInt), (1000)
                0x01, 0x01, ( byte ) 0xFF,          // typesOnly BOOLEAN, (TRUE) filter Filter,
                ( byte ) 0xA4, 0x15,                // Filter ::= CHOICE {
                                                    // substrings [4] SubstringFilter
                                                    // }
                                                    // SubstringFilter ::= SEQUENCE {
                  0x04, 0x0B,
                    'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x30, 0x06,
                    ( byte ) 0x80, 0x01,
                      't',
                    ( byte ) 0x81, 0x01,
                      't',
                0x30, 0x15,                         // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2'         // AttributeDescription ::= LDAPString
    } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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() );

    // (objectclass=t*)
    ExprNode node = searchRequest.getFilter();
    SubstringNode substringNode = ( SubstringNode ) node;
    assertNotNull( substringNode );

    assertEquals( "objectclass", substringNode.getAttribute() );
    assertEquals( "t", substringNode.getInitial() );
    assertEquals( "t", substringNode.getAny().get( 0 ) );

    // The attributes
    List<String> attributes = searchRequest.getAttributes();

    for ( String attribute : attributes )
    {
        assertNotNull( attribute );
    }

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 10
Source File: SearchRequestSubstringTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with a substring filter. Test the
 * initial filter : (objectclass=*t*t*t)
 */
@Test
public void testDecodeSearchRequestSubstringAnyAnyFinal() throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x6A );
    stream.put( new byte[]
        {
            0x30, 0x68,                             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,                     // messageID
              0x63, 0x63,                           // CHOICE { ..., searchRequest SearchRequest, ...
                                                    // SearchRequest ::= APPLICATION[3] SEQUENCE {
                0x04, 0x1F,                         // baseObject LDAPDN,
                  '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,                   // scope ENUMERATED {
                                                    // baseObject (0),
                                                    // singleLevel (1),
                                                    // wholeSubtree (2) },
                0x0A, 0x01, 0x03,                   // derefAliases ENUMERATED {
                                                    // neverDerefAliases (0),
                                                    // derefInSearching (1),
                                                    // derefFindingBaseObj (2),
                                                    // derefAlways (3) },
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // sizeLimit INTEGER (0 .. maxInt), (1000)
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // timeLimit INTEGER (0 .. maxInt), (1000)
                0x01, 0x01, ( byte ) 0xFF,          // typesOnly BOOLEAN, (TRUE) filter Filter,
                                                    // filter Filter,
                ( byte ) 0xA4, 0x18,                // Filter ::= CHOICE {
                                                    // substrings [4] SubstringFilter }
                                                    // SubstringFilter ::= SEQUENCE {
                  0x04, 0x0B,
                    'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x30, 0x09,
                    ( byte ) 0x81, 0x01,
                      't',
                    ( byte ) 0x81, 0x01,
                      't',
                    ( byte ) 0x82, 0x01,
                      't',
                0x30, 0x15,                         // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2'         // AttributeDescription ::= LDAPString
    } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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() );

    // (objectclass=t*)
    ExprNode node = searchRequest.getFilter();
    SubstringNode substringNode = ( SubstringNode ) node;
    assertNotNull( substringNode );

    assertEquals( "objectclass", substringNode.getAttribute() );
    assertEquals( null, substringNode.getInitial() );
    assertEquals( "t", substringNode.getAny().get( 0 ) );
    assertEquals( "t", substringNode.getAny().get( 1 ) );
    assertEquals( "t", substringNode.getFinal() );

    // The attributes
    List<String> attributes = searchRequest.getAttributes();

    for ( String attribute : attributes )
    {
        assertNotNull( attribute );
    }

    // Check the encoding
    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertTrue( Arrays.equals( stream.array(), buffer.getBytes().array() ) );
}
 
Example 11
Source File: SearchRequestSubstringTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with a substring filter. Test the
 * initial filter : (objectclass=t*)
 */
@Test
public void testDecodeSearchRequestSubstringInitialAnyAny() throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x67 );
    stream.put( new byte[]
        {
            0x30, 0x65,                             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,                     // messageID
              0x63, 0x60,                           // CHOICE { ..., searchRequest SearchRequest, ...
                                                    // SearchRequest ::= APPLICATION[3] SEQUENCE {
                0x04, 0x1F,                         // baseObject LDAPDN,
                  '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,                   // scope ENUMERATED {
                                                    // baseObject (0),
                                                    // singleLevel (1),
                                                    // wholeSubtree (2) },
                0x0A, 0x01, 0x03,                   // derefAliases ENUMERATED {
                                                    // neverDerefAliases (0),
                                                    // derefInSearching (1),
                                                    // derefFindingBaseObj (2),
                                                    // derefAlways (3) },
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // sizeLimit INTEGER (0 .. maxInt), (1000)
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // timeLimit INTEGER (0 .. maxInt), (1000)
                0x01, 0x01, ( byte ) 0xFF,          // typesOnly BOOLEAN, (TRUE) filter Filter,
                ( byte ) 0xA4, 0x15,                // Filter ::= CHOICE {
                                                    // substrings [4] SubstringFilter
                                                    // }
                                                    // SubstringFilter ::= SEQUENCE {
                  0x04, 0x0B,
                    'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x30, 0x06,
                    ( byte ) 0x80, 0x01,
                      't',
                    ( byte ) 0x81, 0x01,
                      '*',
                0x30, 0x15,                         // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',    // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2'     // AttributeDescription ::= LDAPString
    } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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() );

    // (objectclass=t*)
    ExprNode node = searchRequest.getFilter();
    SubstringNode substringNode = ( SubstringNode ) node;
    assertNotNull( substringNode );

    assertEquals( "objectclass", substringNode.getAttribute() );
    assertEquals( "t", substringNode.getInitial() );
    assertEquals( "*", substringNode.getAny().get( 0 ) );

    // The attributes
    List<String> attributes = searchRequest.getAttributes();

    for ( String attribute : attributes )
    {
        assertNotNull( attribute );
    }

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 12
Source File: SearchRequestSubstringTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with a substring filter. Test the
 * initial filter : (objectclass=*t*t*t*)
 */
@Test
public void testDecodeSearchRequestSubstringAnyAnyAny() throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x6A );
    stream.put( new byte[]
        {
            0x30, 0x68,                             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,                     // messageID
              0x63, 0x63,                           // CHOICE { ..., searchRequest SearchRequest, ...
                                                    // SearchRequest ::= APPLICATION[3] SEQUENCE {
                0x04, 0x1F,                         // baseObject LDAPDN,
                  '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,                   // scope ENUMERATED {
                                                    // baseObject (0),
                                                    // singleLevel (1),
                                                    // wholeSubtree (2) },
                0x0A, 0x01, 0x03,                   // derefAliases ENUMERATED {
                                                    // neverDerefAliases (0),
                                                    // derefInSearching (1),
                                                    // derefFindingBaseObj (2),
                                                    // derefAlways (3) },
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // sizeLimit INTEGER (0 .. maxInt), (1000)
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // timeLimit INTEGER (0 .. maxInt), (1000)
                0x01, 0x01, ( byte ) 0xFF,          // typesOnly BOOLEAN, (TRUE) filter Filter,
                ( byte ) 0xA4, 0x18,                // Filter ::= CHOICE {
                                                    // substrings [4] SubstringFilter
                                                    // }
                                                    // SubstringFilter ::= SEQUENCE {
                  0x04, 0x0B,
                    'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x30, 0x09,
                    ( byte ) 0x81, 0x01,
                      't',
                    ( byte ) 0x81, 0x01,
                      't',
                    ( byte ) 0x81, 0x01,
                      't',
                0x30, 0x15,                         // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2'         // AttributeDescription ::= LDAPString
    } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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() );

    // (objectclass=t*)
    ExprNode node = searchRequest.getFilter();
    SubstringNode substringNode = ( SubstringNode ) node;
    assertNotNull( substringNode );

    assertEquals( "objectclass", substringNode.getAttribute() );
    assertEquals( null, substringNode.getInitial() );
    assertEquals( "t", substringNode.getAny().get( 0 ) );
    assertEquals( "t", substringNode.getAny().get( 1 ) );
    assertEquals( "t", substringNode.getAny().get( 2 ) );
    assertEquals( null, substringNode.getFinal() );

    // The attributes
    List<String> attributes = searchRequest.getAttributes();

    for ( String attribute : attributes )
    {
        assertNotNull( attribute );
    }

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 13
Source File: SearchRequestSubstringTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with a substring filter. Test the
 * initial filter : (objectclass=*Amos)
 */
@Test
public void testDecodeSearchRequestSubstringFinal() throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x67 );
    stream.put( new byte[]
        {
            0x30, 0x65,                             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,                     // messageID
              0x63, 0x60,                           // CHOICE { ..., searchRequest SearchRequest, ...
                                                    // SearchRequest ::= APPLICATION[3] SEQUENCE {
                0x04, 0x1F,                         // baseObject LDAPDN,
                  '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,                   // scope ENUMERATED {
                                                    // baseObject (0),
                                                    // singleLevel (1),
                                                    // wholeSubtree (2) },
                0x0A, 0x01, 0x03,                   // derefAliases ENUMERATED {
                                                    // neverDerefAliases (0),
                                                    // derefInSearching (1),
                                                    // derefFindingBaseObj (2),
                                                    // derefAlways (3) },
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // sizeLimit INTEGER (0 .. maxInt), (1000)
                0x02, 0x02, 0x03, ( byte ) 0xE8,    // timeLimit INTEGER (0 .. maxInt), (1000)
                0x01, 0x01, ( byte ) 0xFF,          // typesOnly BOOLEAN, (TRUE) filter Filter,
                ( byte ) 0xA4, 0x15,                // Filter ::= CHOICE {
                                                    // substrings [4] SubstringFilter
                                                    // }
                                                    // SubstringFilter ::= SEQUENCE {
                  0x04, 0x0B,
                    'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's',
                  0x30, 0x06,
                    ( byte ) 0x82, 0x04,
                      'A', 'm', 'o', 's',
                0x30, 0x15,                         // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',        // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2'         // AttributeDescription ::= LDAPString
    } );

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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() );

    // (objectclass=t*)
    ExprNode node = searchRequest.getFilter();
    SubstringNode substringNode = ( SubstringNode ) node;
    assertNotNull( substringNode );

    assertEquals( "objectclass", substringNode.getAttribute() );
    assertEquals( null, substringNode.getInitial() );
    assertEquals( 0, substringNode.getAny().size() );
    assertEquals( "Amos", substringNode.getFinal() );

    // The attributes
    List<String> attributes = searchRequest.getAttributes();

    for ( String attribute : attributes )
    {
        assertNotNull( attribute );
    }

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 14
Source File: SearchRequestMatchingRuleAssertionTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Tests an search request decode with a simple equality match filter.
 */
@Test
public void testDecodeSearchRequestExtensibleMatch() throws DecoderException, EncoderException, LdapException
{

    ByteBuffer stream = ByteBuffer.allocate( 0x63 );
    stream.put( new byte[]
        {
            0x30, 0x61,                         // LDAPMessage ::= SEQUENCE {
              0x02, 0x01, 0x01,                 // messageID
              0x63, 0x5C,                       //   protocolOp      CHOICE {
                                                //     searchRequest   SearchRequest,
                                                //
                                                // SearchRequest ::= [APPLICATION 3] SEQUENCE {
                0x04, 0x11,                     // "dc=example,dc=com"
                  'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm',
                                                //   scope           ENUMERATED {
                0x0A, 0x01, 0x00,               //      baseObject              (0), ...
                                                //   derefAliases    ENUMERATED {
                0x0A, 0x01, 0x02,               //     derefFindingBaseObj     (2),...
                0x02, 0x01, 0x02,               //   sizeLimit       INTEGER (0 .. maxInt), (2)
                0x02, 0x01, 0x03,               //   timeLimit       INTEGER (0 .. maxInt), (3)
                0x01, 0x01, ( byte ) 0xFF,      //   typesOnly       BOOLEAN, (true)
                ( byte ) 0xA9, 0x21,            //   filter          Filter,
                                                //
                                                // Filter ::= CHOICE {
                                                //   extensibleMatch [9] MatchingRuleAssertion }
                                                //
                                                // MatchingRuleAssertion ::= SEQUENCE {
                  ( byte ) 0x81, 0x13,          //    matchingRule    [1] MatchingRuleId OPTIONAL,
                    '1', '.', '2', '.', '8', '4', '0', '.', '4', '8', '0', '1', '8', '.', '1', '.', '2', '.', '2',
                  ( byte ) 0x82, 0x02,          //    type            [2] AttributeDescription OPTIONAL,
                    'c', 'n',
                  ( byte ) 0x83, 0x03,          //    matchValue      [3] AssertionValue,
                    'a', 'o', 'k',
                                                //    dnAttributes    [4] BOOLEAN DEFAULT FALSE  }
                  ( byte ) 0x84, 0x01, ( byte ) 0xFF,
                0x30, 0x15,                     // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
                  0x04, 0x05,
                    'a', 't', 't', 'r', '0',    // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '1',    // AttributeDescription ::= LDAPString
                  0x04, 0x05,
                    'a', 't', 't', 'r', '2',    // AttributeDescription ::= LDAPString
        });

    stream.flip();

    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    Asn1Decoder.decode( stream, ldapMessageContainer );

    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 );
    }

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example 15
Source File: SearchRequestMatchingRuleAssertionTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a SearchRequest with an extensible match and an
 * empty matchValue
 */
@Test
public void testDecodeSearchRequestExtensibleMatchEmptyMatchValue() throws DecoderException, EncoderException, LdapException
{
    byte[] asn1BER = new byte[]
        {
            0x30, 0x41,
              0x02, 0x01, 0x04,             // messageID
              0x63, 0x3C,
                0x04, 0x1F,                 // baseObject LDAPDN,
                  '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 ) 0x83, 0x00,      //    matchValue      [3] AssertionValue,
                0x30, 0x00                  // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
        };

    ByteBuffer stream = ByteBuffer.allocate( asn1BER.length );
    stream.put( asn1BER );
    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<SearchRequest> ldapMessageContainer = new LdapMessageContainer<>( codec );

    // Decode a SearchRequest message
    Asn1Decoder.decode( stream, ldapMessageContainer );

    SearchRequest searchRequest = ldapMessageContainer.getMessage();

    assertEquals( 4, searchRequest.getMessageId() );
    assertEquals( "uid=akarasulu,dc=example,dc=com", 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() );

    // Extended
    ExprNode filter = searchRequest.getFilter();
    ExtensibleNode extensibleNode = ( ExtensibleNode ) filter;
    assertNotNull( extensibleNode );

    assertEquals( "test", extensibleNode.getMatchingRuleId() );
    assertNull( extensibleNode.getAttribute() );
    assertEquals( "", extensibleNode.getValue().getString() );
    assertFalse( extensibleNode.hasDnAttributes() );

    List<String> attributes = searchRequest.getAttributes();

    assertEquals( 0, attributes.size() );

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, searchRequest );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}