use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class AddRequestTest method testDecodeAddRequestNullType.
/**
* Test the decoding of a AddRequest with a empty attributeList
*/
@Test
public void testDecodeAddRequestNullType() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x2F);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x2D, 0x02, 0x01, // messageID MessageID
0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
0x28, // entry 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', // attributes AttributeList }
0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
0x04, 0x30, // attribute 1
0x02, 0x04, // type AttributeDescription,
0x00 });
stream.flip();
// Allocate a LdapMessage Container
Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
// Decode a AddRequest message
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
assertTrue(de instanceof ResponseCarryingException);
Message response = ((ResponseCarryingException) de).getResponse();
assertTrue(response instanceof AddResponseImpl);
assertEquals(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, ((AddResponseImpl) response).getLdapResult().getResultCode());
return;
}
fail("We should not reach this point");
}
use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class AddResponseTest method testDecodeAddResponseEmptyResult.
/**
* Test the decoding of a AddResponse with no LdapResult
*/
@Test
public void testDecodeAddResponseEmptyResult() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x0E);
stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
0x30, // LDAPMessage ::=SEQUENCE {
0x0C, 0x02, 0x01, // messageID MessageID
0x01, 0x69, // CHOICE { ..., addResponse AddResponse, ...
0x00 });
stream.flip();
// Allocate a LdapMessage Container
Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
// Decode a AddResponse 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.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class BindRequestTest method testDecodeBindRequestEmptyCredentials.
/**
* Test the decoding of a BindRequest with an bad mechanism
*/
/* This test is not valid. I don't know how to generate a UnsupportedEncodingException ...
@Test
public void testDecodeBindRequestBadMechanism()
{
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate( 0x11 );
stream.put( new byte[]
{
0x30, 0x0F, // LDAPMessage ::=SEQUENCE {
0x02, 0x01, 0x01, // messageID MessageID
0x60, 0x0A, // CHOICE { ..., bindRequest BindRequest, ...
0x02, 0x01, 0x03, // version INTEGER (1..127),
0x04, 0x00,
( byte ) 0xA3, 0x03,
0x04, 0x01, (byte)0xFF
} );
stream.flip();
// Allocate a LdapMessage Container
Asn1Container container = new LdapMessageContainer();
// Decode the BindRequest PDU
try
{
ldapDecoder.decode( stream, container );
}
catch ( DecoderException de )
{
assertTrue( de instanceof ResponseCarryingException );
Message response = ((ResponseCarryingException)de).getResponse();
assertTrue( response instanceof BindResponseImpl );
assertEquals( ResultCodeEnum.INAPPROPRIATEAUTHENTICATION, ((BindResponseImpl)response).getLdapResult().getResultCode() );
return;
}
fail( "We should not reach this point" );
}
*/
/**
* Test the decoding of a BindRequest with an empty credentials
*/
@Test
public void testDecodeBindRequestEmptyCredentials() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x12);
stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
0x30, // LDAPMessage ::=SEQUENCE {
0x10, 0x02, 0x01, // messageID MessageID
0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
0x0B, 0x02, 0x01, // version INTEGER (1..127),
0x03, 0x04, 0x00, (byte) 0xA3, 0x04, 0x04, 0x00, 0x04, 0x00 });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<BindRequestDecorator> container = new LdapMessageContainer<BindRequestDecorator>(codec);
// Decode the BindRequest PDU
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded BindRequest
BindRequest bindRequest = container.getMessage();
assertEquals(1, bindRequest.getMessageId());
assertTrue(bindRequest.isVersion3());
assertEquals("", bindRequest.getName());
assertFalse(bindRequest.isSimple());
assertEquals("", bindRequest.getSaslMechanism());
assertEquals("", Strings.utf8ToString(bindRequest.getCredentials()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(bindRequest);
// Check the length
assertEquals(0x12, 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.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class BindRequestTest method testDecodeBindRequestSimpleNoName.
/**
* Test the decoding of a BindRequest with Simple authentication, no name
* and no controls
*/
@Test
public void testDecodeBindRequestSimpleNoName() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x15);
stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
0x30, // LDAPMessage ::=SEQUENCE {
0x13, 0x02, 0x01, // messageID MessageID
0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
0x0D, // BindRequest ::= APPLICATION[0] SEQUENCE {
0x02, 0x01, // version INTEGER (1..127),
0x03, (byte) 0x80, // authentication AuthenticationChoice
0x08, // ...
'p', 'a', 's', 's', 'w', 'o', 'r', 'd' });
stream.flip();
// Allocate a LdapMessage Container
Asn1Container container = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
// Decode the BindRequest PDU
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
assertEquals("ERR_01200_BAD_TRANSITION_FROM_STATE Bad transition from state VERSION_STATE, tag 0x80", de.getMessage());
return;
}
fail("Should never reach this point.");
}
use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.
the class BindRequestTest method testDecodeBindRequestSimpleWithControls.
/**
* Test the decoding of a BindRequest with Simple authentication and no
* controls
*/
/* Not used in unit tests
@Test
public void testDecodeBindRequestSimpleNoControlsPerf()
{
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate( 0x52 );
stream.put( new byte[]
{
0x30, 0x50, // LDAPMessage ::=SEQUENCE {
0x02, 0x01, 0x01, // messageID MessageID
0x60, 0x2E, // CHOICE { ..., bindRequest BindRequest, ...
// BindRequest ::= APPLICATION[0] SEQUENCE {
0x02, 0x01, 0x03, // version INTEGER (1..127),
0x04, 0x1F, // name 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',
( byte ) 0x80, 0x08, // authentication AuthenticationChoice
// AuthenticationChoice ::= CHOICE { simple [0] OCTET STRING,
// ...
'p', 'a', 's', 's', 'w', 'o', 'r', 'd',
( byte ) 0xA0, 0x1B, // A 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 = StringTools.dumpBytes( stream.array() );
stream.flip();
// Allocate a LdapMessage Container
Asn1Container container = new LdapMessageContainer();
// Decode the BindRequest PDU
try
{
long t0 = System.currentTimeMillis();
for ( int i = 0; i < 10000; i++ )
{
ldapDecoder.decode( stream, container );
container).clean();
stream.flip();
}
long t1 = System.currentTimeMillis();
System.out.println( "Delta = " + ( t1 - t0 ) );
ldapDecoder.decode( stream, container );
}
catch ( DecoderException de )
{
de.printStackTrace();
fail( de.getMessage() );
}
// Check the decoded BindRequest
LdapMessage message = container.getLdapMessage();
BindRequest br = message.getMessage();
assertEquals( 1, message.getMessageId() );
assertEquals( 3, br.getVersion() );
assertEquals( "uid=akarasulu,dc=example,dc=com", br.getName().toString() );
assertEquals( true, ( br.getAuthentication() instanceof SimpleAuthentication ) );
assertEquals( "password", StringTools.utf8ToString( ( ( SimpleAuthentication ) br.getAuthentication() )
.getSimple() ) );
// Check the Control
List controls = message.getControls();
assertEquals( 1, controls.size() );
Control control = message.getControls( 0 );
assertEquals( "2.16.840.1.113730.3.4.2", control.getOid() );
assertEquals( "", StringTools.dumpBytes( ( byte[] ) control.getValue() ) );
// Check the length
assertEquals( 0x52, message.computeLength() );
// Check the encoding
try
{
ByteBuffer bb = message.encode();
String encodedPdu = StringTools.dumpBytes( bb.array() );
assertEquals( encodedPdu, decodedPdu );
}
catch ( EncoderException ee )
{
ee.printStackTrace();
fail( ee.getMessage() );
}
}
*/
/**
* Test the decoding of a BindRequest with Simple authentication and
* controls
*/
@Test
public void testDecodeBindRequestSimpleWithControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x35);
stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
0x33, 0x02, 0x01, // messageID MessageID
0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
0x02, 0x01, // version INTEGER (1..127),
0x03, 0x04, // name 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', (byte) 0x80, // authentication AuthenticationChoice
0x08, // ...
'p', 'a', 's', 's', 'w', 'o', 'r', 'd' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<BindRequestDecorator> container = new LdapMessageContainer<BindRequestDecorator>(codec);
// Decode the BindRequest PDU
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded BindRequest
BindRequest bindRequest = container.getMessage();
assertEquals(1, bindRequest.getMessageId());
assertTrue(bindRequest.isVersion3());
assertEquals("uid=akarasulu,dc=example,dc=com", bindRequest.getName());
assertTrue(bindRequest.isSimple());
assertEquals("password", Strings.utf8ToString(bindRequest.getCredentials()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(bindRequest);
// Check the length
assertEquals(0x35, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations