use of com.mindbright.asn1.ASN1OctetString 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.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.
the class PLAINBindRequestTestCase method testConstructor6.
/**
* Tests the sixth constructor.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testConstructor6() throws Exception {
PLAINBindRequest r = new PLAINBindRequest("u:test.user", "u:test2.user", new ASN1OctetString("password"));
r = r.duplicate();
assertNotNull(r.getAuthenticationID());
assertEquals(r.getAuthenticationID(), "u:test.user");
assertNotNull(r.getAuthorizationID());
assertEquals(r.getAuthorizationID(), "u:test2.user");
assertNotNull(r.getPasswordString());
assertEquals(r.getPasswordString(), "password");
assertNotNull(r.getPasswordBytes());
assertTrue(Arrays.equals(r.getPasswordBytes(), "password".getBytes("UTF-8")));
assertEquals(r.getBindType(), "PLAIN");
assertEquals(r.getSASLMechanismName(), "PLAIN");
assertNotNull(r.getControls());
assertEquals(r.getControls().length, 0);
assertNotNull(r.getRebindRequest("server.example.com", 389));
assertTrue(r.getRebindRequest("server.example.com", 389) instanceof PLAINBindRequest);
assertNotNull(r.toString());
final ArrayList<String> toCodeLines = new ArrayList<String>(10);
r.toCode(toCodeLines, "foo", 0, false);
assertFalse(toCodeLines.isEmpty());
toCodeLines.clear();
r.toCode(toCodeLines, "bar", 4, true);
assertFalse(toCodeLines.isEmpty());
}
use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.
the class PLAINBindRequestTestCase method testConstructor9.
/**
* Tests the ninth constructor.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testConstructor9() throws Exception {
Control[] controls = { new Control("1.2.3.4"), new Control("1.2.3.5", true, null) };
PLAINBindRequest r = new PLAINBindRequest("u:test.user", new ASN1OctetString("password"), controls);
r = r.duplicate();
assertNotNull(r.getAuthenticationID());
assertEquals(r.getAuthenticationID(), "u:test.user");
assertNull(r.getAuthorizationID());
assertNotNull(r.getPasswordString());
assertEquals(r.getPasswordString(), "password");
assertNotNull(r.getPasswordBytes());
assertTrue(Arrays.equals(r.getPasswordBytes(), "password".getBytes("UTF-8")));
assertEquals(r.getBindType(), "PLAIN");
assertEquals(r.getSASLMechanismName(), "PLAIN");
assertNotNull(r.getControls());
assertEquals(r.getControls().length, 2);
assertNotNull(r.getRebindRequest("server.example.com", 389));
assertTrue(r.getRebindRequest("server.example.com", 389) instanceof PLAINBindRequest);
assertNotNull(r.toString());
final ArrayList<String> toCodeLines = new ArrayList<String>(10);
r.toCode(toCodeLines, "foo", 0, false);
assertFalse(toCodeLines.isEmpty());
toCodeLines.clear();
r.toCode(toCodeLines, "bar", 4, true);
assertFalse(toCodeLines.isEmpty());
}
use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.
the class PLAINBindRequestTestCase method testConstructor12.
/**
* Tests the twelfth constructor.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testConstructor12() throws Exception {
Control[] controls = { new Control("1.2.3.4"), new Control("1.2.3.5", true, null) };
PLAINBindRequest r = new PLAINBindRequest("u:test.user", "u:test2.user", new ASN1OctetString("password"), controls);
r = r.duplicate();
assertNotNull(r.getAuthenticationID());
assertEquals(r.getAuthenticationID(), "u:test.user");
assertNotNull(r.getAuthorizationID());
assertEquals(r.getAuthorizationID(), "u:test2.user");
assertNotNull(r.getPasswordString());
assertEquals(r.getPasswordString(), "password");
assertNotNull(r.getPasswordBytes());
assertTrue(Arrays.equals(r.getPasswordBytes(), "password".getBytes("UTF-8")));
assertEquals(r.getBindType(), "PLAIN");
assertEquals(r.getSASLMechanismName(), "PLAIN");
assertNotNull(r.getControls());
assertEquals(r.getControls().length, 2);
assertNotNull(r.getRebindRequest("server.example.com", 389));
assertTrue(r.getRebindRequest("server.example.com", 389) instanceof PLAINBindRequest);
assertNotNull(r.toString());
final ArrayList<String> toCodeLines = new ArrayList<String>(10);
r.toCode(toCodeLines, "foo", 0, false);
assertFalse(toCodeLines.isEmpty());
toCodeLines.clear();
r.toCode(toCodeLines, "bar", 4, true);
assertFalse(toCodeLines.isEmpty());
}
use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.
the class RDNNameValuePairTestCase method testNameValuePairWithoutSchema.
/**
* Tests the basic behavior of the {@code RDNNameValuePair} class when no
* {@code Schema} object is provided.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testNameValuePairWithoutSchema() throws Exception {
final RDNNameValuePair p = new RDNNameValuePair("givenName", new ASN1OctetString("foo"), null);
assertNotNull(p.getAttributeName());
assertEquals(p.getAttributeName(), "givenName");
assertNotNull(p.getNormalizedAttributeName());
assertEquals(p.getNormalizedAttributeName(), "givenname");
// The second time should use a cached version.
assertNotNull(p.getNormalizedAttributeName());
assertEquals(p.getNormalizedAttributeName(), "givenname");
assertNotNull(p.getAttributeValue());
assertEquals(p.getAttributeValue(), "foo");
assertNotNull(p.getAttributeValueBytes());
assertEquals(p.getAttributeValueBytes(), "foo".getBytes(StandardCharsets.UTF_8));
assertNotNull(p.getRawAttributeValue());
assertEquals(p.getRawAttributeValue(), new ASN1OctetString("foo"));
assertNotNull(p.toString());
assertEquals(p.toString(), "givenName=foo");
// The second time should use a cached version.
assertNotNull(p.toString());
assertEquals(p.toString(), "givenName=foo");
final StringBuilder buffer = new StringBuilder();
p.toString(buffer, false);
assertEquals(buffer.toString(), p.toString());
assertNotNull(p.toMinimallyEncodedString());
assertEquals(p.toMinimallyEncodedString(), "givenName=foo");
assertNotNull(p.toNormalizedString());
assertEquals(p.toNormalizedString(), "givenname=foo");
// The second time should use a cached version.
assertNotNull(p.toNormalizedString());
assertEquals(p.toNormalizedString(), "givenname=foo");
}
Aggregations