use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class LDAPMessageTestCase method testIntermediateResponseMessage.
/**
* Tests the behavior of the {@code LDAPMessage} class with an intermediate
* response protocol op.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testIntermediateResponseMessage() throws Exception {
LinkedList<Control> controls = new LinkedList<Control>();
controls.add(new Control("1.2.3.4"));
controls.add(new Control("1.2.3.5", true, new ASN1OctetString()));
IntermediateResponseProtocolOp op = new IntermediateResponseProtocolOp("1.2.3.4", new ASN1OctetString());
LDAPMessage m = new LDAPMessage(1, op, controls);
ASN1Buffer b = new ASN1Buffer();
m.writeTo(b);
ByteArrayInputStream inputStream = new ByteArrayInputStream(b.toByteArray());
ASN1StreamReader reader = new ASN1StreamReader(inputStream);
m = LDAPMessage.readFrom(reader, true);
m = LDAPMessage.decode(m.encode());
assertEquals(m.getMessageID(), 1);
assertNotNull(m.getProtocolOp());
assertTrue(m.getProtocolOp() instanceof IntermediateResponseProtocolOp);
assertEquals(m.getProtocolOpType(), LDAPMessage.PROTOCOL_OP_TYPE_INTERMEDIATE_RESPONSE);
assertNotNull(m.getControls());
assertFalse(m.getControls().isEmpty());
assertNotNull(m.getIntermediateResponseProtocolOp());
inputStream = new ByteArrayInputStream(b.toByteArray());
reader = new ASN1StreamReader(inputStream);
LDAPResponse r = LDAPMessage.readLDAPResponseFrom(reader, true);
assertNotNull(m.toString());
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class UNBOUNDIDTESTServer method run.
/**
* Performs the processing for this server.
*/
@Override()
public void run() {
try {
serverSocket = new ServerSocket(0);
listenPort = serverSocket.getLocalPort();
while (!stopRequested.get()) {
// Accept a connection from a client.
clientSocket = serverSocket.accept();
final InputStream inputStream = clientSocket.getInputStream();
final OutputStream outputStream = clientSocket.getOutputStream();
final ASN1StreamReader asn1Reader = new ASN1StreamReader(inputStream, 0);
// The client must first send an UNBOUNDID-TEST bind request with no
// credentials.
LDAPMessage requestMessage = LDAPMessage.readFrom(asn1Reader, false);
BindRequestProtocolOp bindRequestOp = requestMessage.getBindRequestProtocolOp();
assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
assertNull(bindRequestOp.getSASLCredentials());
// Return a "SASL bind in progress" response.
LDAPMessage responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SASL_BIND_IN_PROGRESS_INT_VALUE, null, null, null, null));
outputStream.write(responseMessage.encode().encode());
outputStream.flush();
// The next request must be an UNBOUNDID-TEST bind request with
// credentials. We won't do anything to validate the credentials, but
// we will look at the third element to see what QoP the client
// requested.
requestMessage = LDAPMessage.readFrom(asn1Reader, false);
bindRequestOp = requestMessage.getBindRequestProtocolOp();
assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
assertNotNull(bindRequestOp.getSASLCredentials());
final ASN1Sequence credSequence = ASN1Sequence.decodeAsSequence(bindRequestOp.getSASLCredentials().getValue());
final ASN1Element[] credElements = credSequence.elements();
final SASLQualityOfProtection qop = SASLQualityOfProtection.forName(ASN1OctetString.decodeAsOctetString(credElements[2]).stringValue());
assertNotNull(qop);
final boolean qopEncode = ((qop == SASLQualityOfProtection.AUTH_INT) || (qop == SASLQualityOfProtection.AUTH_CONF));
// Return a "success" response. Include server SASL credentials with
// the requested QoP.
responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null, new ASN1OctetString(qop.toString())));
outputStream.write(responseMessage.encode().encode());
outputStream.flush();
// request.
if (qopEncode) {
for (int i = 0; i < 4; i++) {
inputStream.read();
}
}
requestMessage = LDAPMessage.readFrom(asn1Reader, false);
final SearchRequestProtocolOp searchRequestOp = requestMessage.getSearchRequestProtocolOp();
assertEquals(searchRequestOp.getBaseDN(), "");
assertEquals(searchRequestOp.getScope(), SearchScope.BASE);
assertEquals(searchRequestOp.getFilter(), Filter.createPresenceFilter("objectClass"));
assertEquals(searchRequestOp.getAttributes(), Arrays.asList("1.1"));
// Return a search result entry message with a DN but no attributes.
responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultEntryProtocolOp("", Collections.<Attribute>emptyList()));
byte[] messageBytes = responseMessage.encode().encode();
if (qopEncode) {
// Since we know it's a tiny response, we know the length will be
// less than 127 bytes, so we can cheat.
outputStream.write(0);
outputStream.write(0);
outputStream.write(0);
outputStream.write(messageBytes.length);
}
outputStream.write(messageBytes);
outputStream.flush();
// Return a "success" search result done message.
responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultDoneProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null));
messageBytes = responseMessage.encode().encode();
if (qopEncode) {
// Since we know it's a tiny response, we know the length will be
// less than 127 bytes, so we can cheat.
outputStream.write(0);
outputStream.write(0);
outputStream.write(0);
outputStream.write(messageBytes.length);
}
outputStream.write(messageBytes);
outputStream.flush();
// The next request should be an unbind request.
if (qopEncode) {
for (int i = 0; i < 4; i++) {
inputStream.read();
}
}
requestMessage = LDAPMessage.readFrom(asn1Reader, false);
final UnbindRequestProtocolOp unbindRequestOp = requestMessage.getUnbindRequestProtocolOp();
// Close the connection.
try {
asn1Reader.close();
} catch (final Exception e) {
}
try {
outputStream.close();
} catch (final Exception e) {
}
try {
clientSocket.close();
} catch (final Exception e) {
}
clientSocket = null;
}
} catch (final Exception e) {
stopServer();
}
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class SearchResultEntryTestCase method testReadSearchEntryFromMalformedAttribute.
/**
* Tests the {@code readBindResultFrom} method with an element containing
* a response sequence with a malformed attribute.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { LDAPException.class })
public void testReadSearchEntryFromMalformedAttribute() throws Exception {
ASN1Buffer b = new ASN1Buffer();
ASN1BufferSequence msgSequence = b.beginSequence();
b.addInteger(1);
ASN1BufferSequence opSequence = b.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_ENTRY);
b.addOctetString("dc=example,dc=com");
ASN1BufferSequence attrSequence = b.beginSequence();
b.addEnumerated(1);
attrSequence.end();
opSequence.end();
msgSequence.end();
ByteArrayInputStream inputStream = new ByteArrayInputStream(b.toByteArray());
ASN1StreamReader reader = new ASN1StreamReader(inputStream);
LDAPMessage.readLDAPResponseFrom(reader, true);
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class SearchResultEntryTestCase method testReadSearchEntryFromTooShort.
/**
* Tests the {@code readSearchEntryFrom} method with an element containing
* a response sequence that is too short.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { LDAPException.class })
public void testReadSearchEntryFromTooShort() throws Exception {
ASN1Buffer b = new ASN1Buffer();
ASN1BufferSequence msgSequence = b.beginSequence();
b.addInteger(1);
ASN1BufferSequence opSequence = b.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_ENTRY);
b.addOctetString("dc=example,dc=com");
opSequence.end();
msgSequence.end();
ByteArrayInputStream inputStream = new ByteArrayInputStream(b.toByteArray());
ASN1StreamReader reader = new ASN1StreamReader(inputStream);
LDAPMessage.readLDAPResponseFrom(reader, true);
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class FilterTestCase method testReadAndWrite.
/**
* Tests the {@code readFrom} and {@code writeTo} methods.
*
* @param filter The string representation of the filter to test.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(dataProvider = "testValidFilterStrings")
public void testReadAndWrite(final String filter) throws Exception {
Filter provided = Filter.create(filter);
ASN1Buffer b = new ASN1Buffer();
provided.writeTo(b);
ByteArrayInputStream inputStream = new ByteArrayInputStream(b.toByteArray());
ASN1StreamReader reader = new ASN1StreamReader(inputStream);
Filter decoded = Filter.readFrom(reader);
assertEquals(decoded, provided);
}
Aggregations