use of org.apache.directory.api.ldap.codec.api.CodecControl in project directory-ldap-api by apache.
the class DelRequestTest method testDecodeDelRequestSuccessWithControls.
/**
* Test the decoding of a full DelRequest with controls
*/
@Test
public void testDecodeDelRequestSuccessWithControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x44);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x42, 0x02, 0x01, // messageID MessageID
0x01, // DelRequest ::= [APPLICATION 10] LDAPDN;
0x4A, 0x20, 'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y', ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', (byte) 0xA0, // A control
0x1B, 0x30, 0x19, 0x04, 0x17, 0x32, 0x2E, 0x31, 0x36, 0x2E, 0x38, 0x34, 0x30, 0x2E, 0x31, 0x2E, 0x31, 0x31, 0x33, 0x37, 0x33, 0x30, 0x2E, 0x33, 0x2E, 0x34, 0x2E, 0x32 });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<DeleteRequestDecorator> container = new LdapMessageContainer<DeleteRequestDecorator>(codec);
// Decode a DelRequest PDU
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded DelRequest PDU
DeleteRequest delRequest = container.getMessage();
assertEquals(1, delRequest.getMessageId());
assertEquals("cn=testModify,ou=users,ou=system", delRequest.getName().toString());
// Check the Control
Map<String, Control> controls = delRequest.getControls();
assertEquals(1, controls.size());
@SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("2.16.840.1.113730.3.4.2");
assertEquals("2.16.840.1.113730.3.4.2", control.getOid());
assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
DeleteRequest internalDeleteRequest = new DeleteRequestImpl();
internalDeleteRequest.setMessageId(delRequest.getMessageId());
internalDeleteRequest.setName(delRequest.getName());
internalDeleteRequest.addControl(control);
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(internalDeleteRequest);
// Check the length
assertEquals(0x44, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.codec.api.CodecControl in project directory-ldap-api by apache.
the class ExtendedRequestTest method testDecodeExtendedRequestWithControls.
/**
* Test the decoding of a full ExtendedRequest with controls
*/
@Test
public void testDecodeExtendedRequestWithControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x3A);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x38, 0x02, 0x01, // messageID MessageID
0x01, // CHOICE { ..., extendedReq ExtendedRequest, ...
0x77, // ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
0x16, // requestName [0] LDAPOID,
(byte) 0x80, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // requestValue [1] OCTET STRING OPTIONAL }
(byte) 0x81, 0x05, 'v', 'a', 'l', 'u', 'e', (byte) 0xA0, // A control
0x1B, 0x30, 0x19, 0x04, 0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<ExtendedRequestDecorator<?>> container = new LdapMessageContainer<ExtendedRequestDecorator<?>>(codec);
// Decode the ExtendedRequest PDU
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded ExtendedRequest PDU
ExtendedRequestDecorator<?> extendedRequest = container.getMessage();
assertEquals(1, extendedRequest.getMessageId());
assertEquals("1.3.6.1.5.5.2", extendedRequest.getRequestName());
assertEquals("value", Strings.utf8ToString(extendedRequest.getRequestValue()));
// Check the Control
Map<String, Control> controls = extendedRequest.getControls();
assertEquals(1, controls.size());
assertTrue(extendedRequest.hasControl("2.16.840.1.113730.3.4.2"));
@SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) extendedRequest.getControl("2.16.840.1.113730.3.4.2");
assertEquals("", Strings.dumpBytes(control.getValue()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(extendedRequest);
// Check the length
assertEquals(0x3A, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.codec.api.CodecControl in project directory-ldap-api by apache.
the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyValsWithControls.
/**
* Test the decoding of a SearchResultEntry with an empty vals with controls
*/
@Test
public void testDecodeSearchResultEntryEmptyValsWithControls() throws NamingException {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x54);
stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
0x52, 0x02, 0x01, // messageID MessageID
0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
0x30, // objectName LDAPDN,
0x04, 0x1b, 'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',', 'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
0x30, 0x11, 0x30, 0x0F, // type AttributeDescription,
0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x31, 0x00, (byte) 0xA0, // A
0x1B, // control
0x30, 0x19, 0x04, 0x17, 0x32, 0x2E, 0x31, 0x36, 0x2E, 0x38, 0x34, 0x30, 0x2E, 0x31, 0x2E, 0x31, 0x31, 0x33, 0x37, 0x33, 0x30, 0x2E, 0x33, 0x2E, 0x34, 0x2E, 0x32 });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a BindRequest Container
LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();
assertEquals(1, searchResultEntry.getMessageId());
assertEquals("ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString());
Entry entry = searchResultEntry.getEntry();
assertEquals(1, entry.size());
for (int i = 0; i < entry.size(); i++) {
Attribute attribute = entry.get("objectclass");
assertEquals(Strings.toLowerCaseAscii("objectClass"), Strings.toLowerCaseAscii(attribute.getUpId()));
assertEquals(0, attribute.size());
}
// Check the Control
Map<String, Control> controls = searchResultEntry.getControls();
assertEquals(1, controls.size());
@SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("2.16.840.1.113730.3.4.2");
assertEquals("2.16.840.1.113730.3.4.2", control.getOid());
assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
// Check the length
assertEquals(0x54, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.codec.api.CodecControl in project directory-ldap-api by apache.
the class UnBindRequestTest method testDecodeUnBindRequestWithControls.
/**
* Test the decoding of a UnBindRequest with controls
*/
@Test
public void testDecodeUnBindRequestWithControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x24);
stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
0x22, 0x02, 0x01, // messageID MessageID
0x01, 0x42, // CHOICE { ..., unbindRequest UnbindRequest,...
0x00, // UnbindRequest ::= [APPLICATION 2] NULL
(byte) 0xA0, // A control
0x1B, 0x30, 0x19, 0x04, 0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a BindRequest Container
LdapMessageContainer<UnbindRequestDecorator> ldapMessageContainer = new LdapMessageContainer<UnbindRequestDecorator>(codec);
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
UnbindRequest unbindRequest = ldapMessageContainer.getMessage();
assertEquals(1, unbindRequest.getMessageId());
// Check the Control
Map<String, Control> controls = unbindRequest.getControls();
assertEquals(1, controls.size());
@SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("2.16.840.1.113730.3.4.2");
assertEquals("2.16.840.1.113730.3.4.2", control.getOid());
assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
// Check the encoding
UnbindRequest internalUnbindRequest = new UnbindRequestImpl();
internalUnbindRequest.setMessageId(unbindRequest.getMessageId());
internalUnbindRequest.addControl(control);
try {
ByteBuffer bb = encoder.encodeMessage(internalUnbindRequest);
// Check the length
assertEquals(0x24, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations