Search in sources :

Example 1 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestGlobalNoControlsOidAndAlias.

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

Example 2 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class BindResponseTest method testDecodeBindResponseWithControlSuccess.

/**
 * Test the decoding of a BindResponse with a control
 */
@Test
public void testDecodeBindResponseWithControlSuccess() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x3C);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x3A, 0x02, 0x01, // messageID MessageID
    0x01, 0x61, // CHOICE { ..., bindResponse BindResponse, ...
    0x07, // COMPONENTS OF LDAPResult,
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x00, // },
    0x04, // matchedDN LDAPDN,
    0x00, 0x04, // errorMessage LDAPString,
    0x00, // serverSaslCreds [7] OCTET STRING OPTIONAL }
    (byte) 0xa0, // controls
    0x2C, 0x30, // The PagedSearchControl
    0x2A, 0x04, // Oid : 1.2.840.113556.1.4.319
    0x16, 0x31, 0x2e, 0x32, 0x2e, 0x38, 0x34, 0x30, 0x2e, 0x31, 0x31, 0x33, 0x35, 0x35, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x33, 0x31, 0x39, 0x01, 0x01, // criticality: false
    (byte) 0xff, 0x04, 0x0D, 0x30, 0x0B, 0x02, 0x01, // Size = 5, cookie = "abcdef"
    0x05, 0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<BindResponseDecorator> container = new LdapMessageContainer<BindResponseDecorator>(codec);
    // Decode the BindResponse PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded BindResponse
    BindResponse bindResponse = container.getMessage();
    assertEquals(1, bindResponse.getMessageId());
    assertEquals(ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode());
    assertEquals("", bindResponse.getLdapResult().getMatchedDn().getName());
    assertEquals("", bindResponse.getLdapResult().getDiagnosticMessage());
    // Check the Control
    Map<String, Control> controls = bindResponse.getControls();
    assertEquals(1, controls.size());
    Control control = controls.get("1.2.840.113556.1.4.319");
    assertEquals("1.2.840.113556.1.4.319", control.getOid());
    assertTrue(control instanceof PagedResultsDecorator);
    PagedResultsDecorator pagedSearchControl = (PagedResultsDecorator) control;
    assertEquals(5, pagedSearchControl.getSize());
    assertTrue(Arrays.equals(Strings.getBytesUtf8("abcdef"), pagedSearchControl.getCookie()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(bindResponse);
        // Check the length
        assertEquals(0x3C, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) PagedResultsDecorator(org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) BindResponseDecorator(org.apache.directory.api.ldap.codec.decorators.BindResponseDecorator) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 3 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class ExtendedRequestTest method testDecodeEmptyOID.

/**
 * Test the decoding of an empty OID
 */
@Test
public void testDecodeEmptyOID() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x09);
    stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
    0x30, // LDAPMessage ::= SEQUENCE {
    0x07, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., extendedReq ExtendedRequest, ...
    0x77, // ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
    0x02, (byte) 0x80, 0x00 });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ExtendedRequestDecorator<?>> container = new LdapMessageContainer<ExtendedRequestDecorator<?>>(codec);
    // Decode a ExtendedRequest PDU
    try {
        ldapDecoder.decode(stream, container);
        fail("We should never reach this point !!!");
    } catch (DecoderException de) {
        assertTrue(true);
    }
}
Also used : ExtendedRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator) LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 4 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class LdapControlTest method testDecodeRequestWithControlsNullOID.

/**
 * Test the decoding of a Request with null OID controls
 */
@Test
public void testDecodeRequestWithControlsNullOID() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x19);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x17, 0x02, 0x01, // messageID MessageID
    0x03, 0x50, 0x01, // CHOICE { ..., abandonRequest
    0x02, // AbandonRequest,...
    (byte) 0xA0, // controls [0] Controls OPTIONAL }
    0x0F, 0x30, // Control ::= SEQUENCE {
    0x0D, // controlType LDAPOID,
    0x04, 0x00, // criticality BOOLEAN DEFAULT FALSE,
    0x01, 0x01, (byte) 0xFF, // controlValue OCTET STRING OPTIONAL }
    0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f' });
    stream.flip();
    // Allocate a LdapMessageContainer Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode the PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(true);
        return;
    }
    fail("We should not reach this point");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 5 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class LdapControlTest method testDecodeRequestWithControlsBadOID.

/**
 * Test the decoding of a Request with bad OID controls
 */
@Test
public void testDecodeRequestWithControlsBadOID() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x20);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x1E, 0x02, 0x01, // messageID MessageID
    0x03, 0x50, 0x01, // CHOICE { ..., abandonRequest
    0x02, // AbandonRequest,...
    (byte) 0xA0, // controls [0] Controls OPTIONAL }
    0x16, 0x30, // Control ::= SEQUENCE {
    0x14, // controlType LDAPOID,
    0x04, 0x07, 'b', 'a', 'd', ' ', 'o', 'i', 'd', // criticality BOOLEAN DEFAULT FALSE,
    0x01, 0x01, (byte) 0xFF, // controlValue OCTET STRING OPTIONAL }
    0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f' });
    stream.flip();
    // Allocate a LdapMessageContainer Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode the PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(true);
        return;
    }
    fail("We should not reach this point");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

DecoderException (org.apache.directory.api.asn1.DecoderException)14 ByteBuffer (java.nio.ByteBuffer)7 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)7 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)7 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)7 Test (org.junit.Test)7 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)6 EncoderException (org.apache.directory.api.asn1.EncoderException)3 Control (org.apache.directory.api.ldap.model.message.Control)3 ExtendedRequest (org.apache.directory.api.ldap.model.message.ExtendedRequest)3 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)3 Asn1Container (org.apache.directory.api.asn1.ber.Asn1Container)2 ExtendedRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator)2 ExtendedResponseDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)2 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)2 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)2 BigInteger (java.math.BigInteger)1 BufferOverflowException (java.nio.BufferOverflowException)1 NamingException (javax.naming.NamingException)1 Oid (org.apache.directory.api.asn1.util.Oid)1