Search in sources :

Example 21 with ASN1Sequence

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());
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) Test(org.testng.annotations.Test)

Example 22 with ASN1Sequence

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());
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) Test(org.testng.annotations.Test)

Example 23 with ASN1Sequence

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());
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) Test(org.testng.annotations.Test)

Example 24 with ASN1Sequence

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());
}
Also used : ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 25 with ASN1Sequence

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();
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) ServerSocket(java.net.ServerSocket) BindResponseProtocolOp(com.unboundid.ldap.protocol.BindResponseProtocolOp) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) UnbindRequestProtocolOp(com.unboundid.ldap.protocol.UnbindRequestProtocolOp)

Aggregations

ASN1Sequence (com.unboundid.asn1.ASN1Sequence)455 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)397 Test (org.testng.annotations.Test)311 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)297 ASN1Element (com.unboundid.asn1.ASN1Element)231 ArrayList (java.util.ArrayList)184 IOException (java.io.IOException)141 NotNull (com.unboundid.util.NotNull)116 ASN1Enumerated (com.unboundid.asn1.ASN1Enumerated)95 ASN1Integer (com.unboundid.asn1.ASN1Integer)94 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)85 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)76 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)73 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)69 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)64 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)57 Enumeration (java.util.Enumeration)54 ASN1Boolean (com.unboundid.asn1.ASN1Boolean)53 X509Certificate (java.security.cert.X509Certificate)53 BigInteger (java.math.BigInteger)50