use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.
the class ModifyRequestTest method testDecodeModifyRequest2AttrsSuccess.
/**
* Test the decoding of a ModifyRequest
*/
@Test
public void testDecodeModifyRequest2AttrsSuccess() throws LdapException {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x54);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x52, 0x02, 0x01, // messageID MessageID
0x01, 0x66, // CHOICE { ..., modifyRequest ModifyRequest, ...
0x4d, // object LDAPDN,
0x04, 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', 0x30, 0x29, // modification SEQUENCE OF SEQUENCE {
0x30, 0x11, 0x0A, 0x01, // operation ENUMERATED {
0x02, // modification AttributeTypeAndValues } }
0x30, // AttributeTypeAndValues ::= SEQUENCE {
0x0c, 0x04, 0x01, // type AttributeDescription,
'l', 0x31, // vals SET OF AttributeValue }
0x07, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x30, // modification SEQUENCE OF *SEQUENCE* {
0x14, 0x0A, 0x01, // operation ENUMERATED {
0x00, // modification AttributeTypeAndValues } }
0x30, // AttributeTypeAndValues ::= SEQUENCE {
0x0f, // type AttributeDescription,
0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET OF AttributeValue }
0x06, 0x04, 0x04, 't', 'e', 's', 't' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
// Decode a ModifyRequest PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded PDU
ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
assertEquals(1, modifyRequest.getMessageId());
assertEquals("cn=testModify,ou=users,ou=system", modifyRequest.getName().toString());
Collection<Modification> modifications = modifyRequest.getModifications();
assertEquals(2, modifications.size());
for (Modification modification : modifications) {
Attribute attribute = modification.getAttribute();
if ("l".equalsIgnoreCase(attribute.getUpId())) {
String attrValue = attribute.getString();
assertEquals("Paris", attrValue);
} else if ("attrs".equalsIgnoreCase(attribute.getUpId())) {
String attrValue = attribute.getString();
assertEquals("test", attrValue);
}
}
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(modifyRequest);
// 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.model.message.ModifyRequest in project directory-ldap-api by apache.
the class ModifyRequestTest method testDecodeModifyRequestAddOperationModificationTypeEmptyValsWithControls.
/**
* Test the decoding of a ModifyRequest with an add operation, and a
* modification with a type and an empty vals wuth controls
*/
@Test
public void testDecodeModifyRequestAddOperationModificationTypeEmptyValsWithControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x54);
stream.put(new byte[] { 0x30, // LdapMessage
0x52, 0x02, 0x01, // Message ID : 49
0x31, 0x66, // ModifyRequest
0x30, 0x04, 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', 0x30, 0x0C, 0x30, 0x0A, 0x0A, 0x01, 0x00, 0x30, 0x05, 0x04, 0x01, 'l', 0x31, 0x00, (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<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
// Decode a ModifyRequest PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded PDU
ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
assertEquals(49, modifyRequest.getMessageId());
assertEquals("cn=testModify,ou=users,ou=system", modifyRequest.getName().toString());
Object[] modifications = modifyRequest.getModifications().toArray();
assertEquals(1, modifications.length);
Modification modification = (Modification) modifications[0];
Attribute attributeValue = modification.getAttribute();
assertEquals("l", Strings.toLowerCaseAscii(attributeValue.getUpId()));
assertEquals(0, attributeValue.size());
// Check the Control
Map<String, Control> controls = modifyRequest.getControls();
assertEquals(1, controls.size());
@SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) modifyRequest.getControl("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(modifyRequest);
// 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.model.message.ModifyRequest in project directory-ldap-api by apache.
the class ModifyRequestTest method testDecodeModifyRequestAddOperationModificationTypeEmptyVals.
/**
* Test the decoding of a ModifyRequest with an add operation, and a
* modification with a type and an empty vals
*/
@Test
public void testDecodeModifyRequestAddOperationModificationTypeEmptyVals() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x37);
stream.put(new byte[] { 0x30, // LdapMessage
0x35, 0x02, 0x01, // Message ID : 49
0x31, 0x66, // ModifyRequest
0x30, 0x04, 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', 0x30, 0x0C, 0x30, 0x0A, 0x0A, 0x01, 0x00, 0x30, 0x05, 0x04, 0x01, 'l', 0x31, 0x00 });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
// Decode a ModifyRequest PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded PDU
ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
assertEquals(49, modifyRequest.getMessageId());
assertEquals("cn=testModify,ou=users,ou=system", modifyRequest.getName().toString());
Object[] modifications = modifyRequest.getModifications().toArray();
assertEquals(1, modifications.length);
Modification modification = (Modification) modifications[0];
Attribute attributeValue = modification.getAttribute();
assertEquals("l", Strings.toLowerCaseAscii(attributeValue.getUpId()));
assertEquals(0, attributeValue.size());
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(modifyRequest);
// Check the length
assertEquals(0x37, 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.model.message.ModifyRequest in project directory-ldap-api by apache.
the class ModifyRequestTest method testRequestWith1ModificationBase64Value.
/**
* Test parsing of a request with a Modification element with Base64 Value
* @throws NamingException
*/
@Test
public void testRequestWith1ModificationBase64Value() throws LdapException {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(ModifyRequestTest.class.getResource("request_with_1_modification_base64_value.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
Collection<Modification> modifications = modifyRequest.getModifications();
assertEquals(1, modifications.size());
Modification modification = modifications.iterator().next();
Attribute attribute = modification.getAttribute();
assertEquals(ModificationOperation.ADD_ATTRIBUTE, modification.getOperation());
assertEquals("directreport", attribute.getId());
String expected = new String(new byte[] { 'c', 'n', '=', 'E', 'm', 'm', 'a', 'n', 'u', 'e', 'l', ' ', 'L', (byte) 0xc3, (byte) 0xa9, 'c', 'h', 'a', 'r', 'n', 'y', ',', ' ', 'o', 'u', '=', 'p', 'e', 'o', 'p', 'l', 'e', ',', ' ', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', ' ', 'd', 'c', '=', 'c', 'o', 'm' }, StandardCharsets.UTF_8);
assertEquals(expected, attribute.get().getValue());
}
use of org.apache.directory.api.ldap.model.message.ModifyRequest in project directory-ldap-api by apache.
the class ModifyRequestTest method testRequestWith3ControlsWithoutValue.
/**
* Test parsing of a request with 3 (optional) Control elements without value
*/
@Test
public void testRequestWith3ControlsWithoutValue() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(ModifyRequestTest.class.getResource("request_with_3_controls_without_value.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
Map<String, Control> controls = modifyRequest.getControls();
assertEquals(3, modifyRequest.getControls().size());
Control control = controls.get("1.2.840.113556.1.4.456");
assertNotNull(control);
assertTrue(control.isCritical());
assertEquals("1.2.840.113556.1.4.456", control.getOid());
assertFalse(((DsmlControl<?>) control).hasValue());
}
Aggregations