use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestCompareFiltersNoControls.
/**
* Test the decoding of a SearchRequest with no controls. Test the various
* types of filter : >=, <=, ~= The search filter is :
* (&(|(objectclass~=top)(ou<=contacts))(!(objectclass>=ttt)))
*/
@Test
public void testDecodeSearchRequestCompareFiltersNoControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x90);
stream.put(new byte[] { 0x30, (byte) 0x81, // LDAPMessage ::=SEQUENCE {
(byte) 0x8D, 0x02, 0x01, // messageID MessageID
0x01, 0x63, (byte) 0x81, // CHOICE { ...,
(byte) 0x87, // SearchRequest ::= APPLICATION[3] SEQUENCE {
0x04, // baseObject LDAPDN,
0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0A, 0x01, // scope ENUMERATED {
0x01, // wholeSubtree (2) },
0x0A, 0x01, // derefAliases ENUMERATED {
0x03, // derefAlways (3) },
0x02, 0x02, 0x03, // sizeLimit INTEGER (0 .. maxInt), (1000)
(byte) 0xE8, 0x02, 0x02, 0x03, // timeLimit INTEGER (0 .. maxInt), (1000)
(byte) 0xE8, 0x01, 0x01, // typesOnly BOOLEAN, (TRUE)
(byte) 0xFF, // filter Filter,
(byte) 0xA0, // Filter ::= CHOICE {
0x3C, // and [0] SET OF Filter,
(byte) 0xA1, // or [1] SET of Filter,
0x24, (byte) 0xA8, // approxMatch [8]
0x12, // Assertion ::= SEQUENCE {
0x04, // attributeDesc AttributeDescription (LDAPString),
0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x04, // attributeDesc AttributeDescription (LDAPString),
0x03, 't', 'o', 'p', (byte) 0xA6, // lessOrEqual [3] Assertion,
0x0E, 0x04, // Assertion ::= SEQUENCE {
0x02, 'o', // attributeDesc AttributeDescription (LDAPString),
'u', 0x04, // assertionValue AssertionValue (OCTET STRING) }
0x08, 'c', 'o', 'n', 't', 'a', 'c', 't', 's', (byte) 0xA2, // not [2] Filter,
0x14, (byte) 0xA5, // greaterOrEqual [5] Assertion,
0x12, // Assertion ::= SEQUENCE {
0x04, // attributeDesc AttributeDescription (LDAPString),
0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x04, 0x03, 't', 't', // assertionValue AssertionValue (OCTET STRING) }
't', // attributes AttributeDescriptionList }
0x30, // AttributeDescriptionList ::= SEQUENCE OF
0x15, // AttributeDescription
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
'0', 0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
'1', 0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
'2' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a BindRequest Container
LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
SearchRequest searchRequest = ldapMessageContainer.getMessage();
assertEquals(1, searchRequest.getMessageId());
assertEquals("uid=akarasulu,dc=example,dc=com", searchRequest.getBase().toString());
assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
assertEquals(1000, searchRequest.getSizeLimit());
assertEquals(1000, searchRequest.getTimeLimit());
assertEquals(true, searchRequest.getTypesOnly());
// (& (...
ExprNode filter = searchRequest.getFilter();
AndNode andNode = (AndNode) filter;
assertNotNull(andNode);
List<ExprNode> andNodes = andNode.getChildren();
// (& (| (...
assertEquals(2, andNodes.size());
OrNode orFilter = (OrNode) andNodes.get(0);
assertNotNull(orFilter);
// (& (| (objectclass~=top) (...
List<ExprNode> orNodes = orFilter.getChildren();
assertEquals(2, orNodes.size());
ApproximateNode<?> approxNode = (ApproximateNode<?>) orNodes.get(0);
assertNotNull(approxNode);
assertEquals("objectclass", approxNode.getAttribute());
assertEquals("top", approxNode.getValue().getValue());
// (& (| (objectclass~=top) (ou<=contacts) ) (...
LessEqNode<?> lessOrEqualNode = (LessEqNode<?>) orNodes.get(1);
assertNotNull(lessOrEqualNode);
assertEquals("ou", lessOrEqualNode.getAttribute());
assertEquals("contacts", lessOrEqualNode.getValue().getValue());
// (& (| (objectclass~=top) (ou<=contacts) ) (! ...
NotNode notNode = (NotNode) andNodes.get(1);
assertNotNull(notNode);
// (& (| (objectclass~=top) (ou<=contacts) ) (! (objectclass>=ttt) ) )
GreaterEqNode<?> greaterOrEqual = (GreaterEqNode<?>) notNode.getFirstChild();
assertNotNull(greaterOrEqual);
assertEquals("objectclass", greaterOrEqual.getAttribute());
assertEquals("ttt", greaterOrEqual.getValue().getValue());
// The attributes
List<String> attributes = searchRequest.getAttributes();
for (String attribute : attributes) {
assertNotNull(attribute);
}
// attributes may have been reordered
try {
ByteBuffer bb = encoder.encodeMessage(searchRequest);
// Check the length
assertEquals(0x0090, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu.substring(0, 0x81), decodedPdu.substring(0, 0x81));
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestRootDse.
/**
* Test the decoding of a SearchRequest
* for rootDSE
*/
@Test
public void testDecodeSearchRequestRootDse() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x33);
stream.put(new byte[] { 0x30, (byte) 0x84, 0x00, 0x00, 0x00, 0x2D, 0x02, 0x01, 0x01, 0x63, (byte) 0x84, 0x00, 0x00, 0x00, 0x24, 0x04, 0x00, 0x0A, 0x01, 0x00, 0x0A, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x01, 0x01, 0x00, (byte) 0x87, 0x0B, 0x6F, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x43, 0x6C, 0x61, 0x73, 0x73, 0x30, (byte) 0x84, 0x00, 0x00, 0x00, 0x00 });
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.OBJECT, searchRequest.getScope());
assertEquals(AliasDerefMode.NEVER_DEREF_ALIASES, searchRequest.getDerefAliases());
assertEquals(0, searchRequest.getSizeLimit());
assertEquals(0, searchRequest.getTimeLimit());
assertEquals(false, searchRequest.getTypesOnly());
ExprNode filter = searchRequest.getFilter();
PresenceNode presenceNode = (PresenceNode) filter;
assertNotNull(presenceNode);
assertEquals("objectClass", presenceNode.getAttribute());
List<String> attributes = searchRequest.getAttributes();
assertEquals(0, attributes.size());
}
use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestEmptyGreaterOrEqualEmptyAttrDesc.
/**
* Test the decoding of a SearchRequest with a greaterOrEqual filter and an
* empty attributeDesc
*/
@Test
public void testDecodeSearchRequestEmptyGreaterOrEqualEmptyAttrDesc() {
byte[] asn1BER = new byte[] { 0x30, 0x39, 0x02, 0x01, // messageID
0x04, 0x63, 0x34, 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, 0x00, 0x0A, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x01, 0x01, (byte) 0xFF, (byte) 0xA5, 0x02, 0x04, 0x00 };
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(asn1BER.length);
stream.put(asn1BER);
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
// Decode a SearchRequest message
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
assertTrue(true);
return;
}
fail("We should not reach this point");
}
use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestWithStarAndAttr.
/**
* Test the decoding of a SearchRequest with a star and an attribute. The search
* filter is : (objectclass=*)
*/
@Test
public void testDecodeSearchRequestWithStarAndAttr() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x40);
stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
0x3E, 0x02, 0x01, // messageID MessageID
0x03, 0x63, // CHOICE { ..., searchRequest SearchRequest, ...
0x39, // SearchRequest ::= APPLICATION[3] SEQUENCE {
0x04, // baseObject LDAPDN,
0x12, 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', 0x0A, 0x01, // scope ENUMERATED {
0x00, // wholeSubtree (2) },
0x0A, 0x01, // derefAliases ENUMERATED {
0x03, // sizeLimit INTEGER (0 .. maxInt), (infinite)
0x02, 0x01, // timeLimit INTEGER (0 .. maxInt), (infinite)
0x00, 0x02, 0x01, 0x00, 0x01, 0x01, // typesOnly
0x00, // Filter ::= CHOICE {
(byte) 0x87, // present [7] AttributeDescription,
0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'C', 'l', 'a', 's', 's', // attributes AttributeDescriptionList }
0x30, // AttributeDescriptionList ::= SEQUENCE OF
0x07, // AttributeDescription
0x04, // Request for sn
0x02, 's', 'n', 0x04, 0x01, // * attribute
'*' });
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(3, searchRequest.getMessageId());
assertEquals("ou=users,ou=system", searchRequest.getBase().toString());
assertEquals(SearchScope.OBJECT, searchRequest.getScope());
assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
assertEquals(0, searchRequest.getSizeLimit());
assertEquals(0, searchRequest.getTimeLimit());
assertEquals(false, searchRequest.getTypesOnly());
// (objectClass = *)
ExprNode filter = searchRequest.getFilter();
PresenceNode presenceNode = (PresenceNode) filter;
assertNotNull(presenceNode);
assertEquals("objectClass", presenceNode.getAttribute());
// The attributes
List<String> attributes = searchRequest.getAttributes();
assertEquals(2, attributes.size());
Set<String> expectedAttrs = new HashSet<String>();
expectedAttrs.add("sn");
expectedAttrs.add("*");
for (String attribute : attributes) {
assertTrue(expectedAttrs.contains(attribute));
expectedAttrs.remove(attribute);
}
assertEquals(0, expectedAttrs.size());
}
use of org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestEmptyGreaterOrEqualEmptyAttrValueEmpty.
/**
* Test the decoding of a SearchRequest with a greaterOrEqual filter and an
* empty attributeValue, and an empty attribute List
*/
@Test
public void testDecodeSearchRequestEmptyGreaterOrEqualEmptyAttrValueEmpty() {
byte[] asn1BER = new byte[] { 0x30, 0x43, 0x02, 0x01, // messageID
0x04, 0x63, 0x3E, 0x04, // baseObject LDAPDN,
0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0A, 0x01, 0x01, 0x0A, 0x01, 0x03, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x01, 0x01, (byte) 0xFF, (byte) 0xA5, 0x08, 0x04, 0x04, 't', 'e', 's', 't', 0x04, 0x00, 0x30, // AttributeDescriptionList ::= SEQUENCE
0x02, // OF AttributeDescription
0x04, 0x00 };
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(asn1BER.length);
stream.put(asn1BER);
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
// Decode a SearchRequest message
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
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());
// >=
GreaterEqNode<?> greaterOrEqual = (GreaterEqNode<?>) searchRequest.getFilter();
assertNotNull(greaterOrEqual);
assertEquals("test", greaterOrEqual.getAttribute());
assertEquals("", greaterOrEqual.getValue().getValue());
List<String> attributes = searchRequest.getAttributes();
assertEquals(0, attributes.size());
}
Aggregations