use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class AttributeTestCase method testConstructor2.
/**
* Tests the second constructor, which takes an attribute name and a string
* value.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testConstructor2() throws Exception {
Attribute attr = new Attribute("cn", "John Doe");
assertEquals(attr.getName(), "cn");
assertEquals(attr.getBaseName(), "cn");
assertFalse(attr.hasOptions());
assertFalse(attr.hasOption("lang=en-US"));
assertTrue(attr.getOptions().isEmpty());
assertNotNull(attr.getMatchingRule());
assertEquals(attr.getMatchingRule(), MATCHING_RULE);
assertEquals(attr.getValue(), "John Doe");
assertTrue(Arrays.equals(attr.getValueByteArray(), "John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue());
assertTrue(attr.hasValue("John Doe"));
assertTrue(attr.hasValue("John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue("john doe"));
assertTrue(attr.hasValue(" john doe "));
assertFalse(attr.hasValue("Johnny Doe"));
assertEquals(attr.getValues().length, 1);
assertEquals(attr.getValueByteArrays().length, 1);
assertEquals(attr.size(), 1);
ASN1Sequence attrSequence = attr.encode();
Attribute attr2 = Attribute.decode(attrSequence);
assertTrue(attr.equals(attr2));
assertTrue(attr2.equals(attr));
assertEquals(attr.hashCode(), attr2.hashCode());
assertNotNull(attr.toString());
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class AttributeTestCase method testConstructor4Varargs.
/**
* Tests the fourth constructor, which takes an attribute name and set of
* string values, by providing the values as varargs.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testConstructor4Varargs() throws Exception {
Attribute attr = new Attribute("cn", "John Doe", "Johnathan Doe");
assertEquals(attr.getName(), "cn");
assertEquals(attr.getBaseName(), "cn");
assertFalse(attr.hasOptions());
assertFalse(attr.hasOption("lang=en-US"));
assertTrue(attr.getOptions().isEmpty());
assertNotNull(attr.getMatchingRule());
assertEquals(attr.getMatchingRule(), MATCHING_RULE);
assertEquals(attr.getValue(), "John Doe");
assertTrue(Arrays.equals(attr.getValueByteArray(), "John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue());
assertTrue(attr.hasValue("John Doe"));
assertTrue(attr.hasValue("John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue("john doe"));
assertTrue(attr.hasValue(" john doe "));
assertTrue(attr.hasValue("johnathan doe"));
assertFalse(attr.hasValue("Johnny Doe"));
assertEquals(attr.getValues().length, 2);
assertEquals(attr.getValueByteArrays().length, 2);
assertEquals(attr.size(), 2);
ASN1Sequence attrSequence = attr.encode();
Attribute attr2 = Attribute.decode(attrSequence);
assertTrue(attr.equals(attr2));
assertTrue(attr2.equals(attr));
assertEquals(attr.hashCode(), attr2.hashCode());
assertNotNull(attr.toString());
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class AttributeTestCase method testConstructor10.
/**
* Tests the tenth constructor, which takes an attribute name, a matching
* rule, and set of string values, by providing the values as varargs.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testConstructor10() throws Exception {
Attribute attr = new Attribute("cn", MATCHING_RULE, "John Doe", "Johnathan Doe");
assertEquals(attr.getName(), "cn");
assertEquals(attr.getBaseName(), "cn");
assertFalse(attr.hasOptions());
assertFalse(attr.hasOption("lang=en-US"));
assertTrue(attr.getOptions().isEmpty());
assertNotNull(attr.getMatchingRule());
assertEquals(attr.getMatchingRule(), MATCHING_RULE);
assertEquals(attr.getValue(), "John Doe");
assertTrue(Arrays.equals(attr.getValueByteArray(), "John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue());
assertTrue(attr.hasValue("John Doe"));
assertTrue(attr.hasValue("John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue("john doe"));
assertTrue(attr.hasValue(" john doe "));
assertTrue(attr.hasValue("johnathan doe"));
assertFalse(attr.hasValue("Johnny Doe"));
assertEquals(attr.getValues().length, 2);
assertEquals(attr.getValueByteArrays().length, 2);
assertEquals(attr.size(), 2);
ASN1Sequence attrSequence = attr.encode();
Attribute attr2 = Attribute.decode(attrSequence);
assertTrue(attr.equals(attr2));
assertTrue(attr2.equals(attr));
assertEquals(attr.hashCode(), attr2.hashCode());
assertNotNull(attr.toString());
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class AttributeTestCase method testConstructor12.
/**
* Tests the twelfth constructor, which takes an attribute name, a matching
* rule, and a collection of string values, using a list with multiple values.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testConstructor12() throws Exception {
LinkedList<String> values = new LinkedList<String>();
values.add("John Doe");
values.add("Johnathan Doe");
Attribute attr = new Attribute("cn", MATCHING_RULE, values);
assertEquals(attr.getName(), "cn");
assertEquals(attr.getBaseName(), "cn");
assertFalse(attr.hasOptions());
assertFalse(attr.hasOption("lang=en-US"));
assertTrue(attr.getOptions().isEmpty());
assertNotNull(attr.getMatchingRule());
assertEquals(attr.getMatchingRule(), MATCHING_RULE);
assertEquals(attr.getValue(), "John Doe");
assertTrue(Arrays.equals(attr.getValueByteArray(), "John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue());
assertTrue(attr.hasValue("John Doe"));
assertTrue(attr.hasValue("John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue("john doe"));
assertTrue(attr.hasValue(" john doe "));
assertTrue(attr.hasValue("johnathan doe"));
assertFalse(attr.hasValue("Johnny Doe"));
assertEquals(attr.getValues().length, 2);
assertEquals(attr.getValueByteArrays().length, 2);
assertEquals(attr.size(), 2);
ASN1Sequence attrSequence = attr.encode();
Attribute attr2 = Attribute.decode(attrSequence);
assertTrue(attr.equals(attr2));
assertTrue(attr2.equals(attr));
assertEquals(attr.hashCode(), attr2.hashCode());
assertNotNull(attr.toString());
}
use of com.github.zhenwei.core.asn1.ASN1Sequence 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();
}
}
Aggregations