Search in sources :

Example 16 with ASN1Sequence

use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.

the class PasswordPolicyStateExtendedResult method encodeValue.

/**
 * Encodes the provided information into a suitable value for this control.
 *
 * @param  userDN             The user DN from the response.
 * @param  operations         The set of operations from the response, mapped
 *                            from operation type to the corresponding
 *                            operation data.
 *
 * @return  An ASN.1 octet string containing the appropriately-encoded value
 *          for this control, or {@code null} if there should not be a value.
 */
@Nullable()
private static ASN1OctetString encodeValue(@Nullable final String userDN, @Nullable final PasswordPolicyStateOperation[] operations) {
    if ((userDN == null) && ((operations == null) || (operations.length == 0))) {
        return null;
    }
    final ArrayList<ASN1Element> elements = new ArrayList<>(2);
    elements.add(new ASN1OctetString(userDN));
    if ((operations != null) && (operations.length > 0)) {
        final ASN1Element[] opElements = new ASN1Element[operations.length];
        for (int i = 0; i < operations.length; i++) {
            opElements[i] = operations[i].encode();
        }
        elements.add(new ASN1Sequence(opElements));
    }
    return new ASN1OctetString(new ASN1Sequence(elements).encode());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) ArrayList(java.util.ArrayList) Nullable(com.unboundid.util.Nullable)

Example 17 with ASN1Sequence

use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.

the class MultiUpdateExtendedRequest method encodeValue.

/**
 * Generates an ASN.1 octet string suitable for use as the value of a
 * multi-update extended request.
 *
 * @param  errorBehavior  The behavior to exhibit if errors are encountered.
 *                        It must not be {@code null}.
 * @param  requests       The  set of requests to be processed.  It must not
 *                        be {@code null} or empty.  Only add, delete, modify,
 *                        modify DN, and certain extended requests (as
 *                        determined by the server) should be included.  Each
 *                        request may include zero or more controls that
 *                        should apply only to that request.
 *
 * @return  An ASN.1 octet string suitable for use as the value of a
 *          multi-update extended request.
 */
@NotNull()
private static ASN1OctetString encodeValue(@NotNull final MultiUpdateErrorBehavior errorBehavior, @NotNull final List<LDAPRequest> requests) {
    final ArrayList<ASN1Element> requestElements = new ArrayList<>(requests.size());
    for (final LDAPRequest r : requests) {
        final ArrayList<ASN1Element> rsElements = new ArrayList<>(2);
        switch(r.getOperationType()) {
            case ADD:
                rsElements.add(((AddRequest) r).encodeProtocolOp());
                break;
            case DELETE:
                rsElements.add(((DeleteRequest) r).encodeProtocolOp());
                break;
            case MODIFY:
                rsElements.add(((ModifyRequest) r).encodeProtocolOp());
                break;
            case MODIFY_DN:
                rsElements.add(((ModifyDNRequest) r).encodeProtocolOp());
                break;
            case EXTENDED:
                rsElements.add(((ExtendedRequest) r).encodeProtocolOp());
                break;
        }
        if (r.hasControl()) {
            rsElements.add(Control.encodeControls(r.getControls()));
        }
        requestElements.add(new ASN1Sequence(rsElements));
    }
    final ASN1Sequence valueSequence = new ASN1Sequence(new ASN1Enumerated(errorBehavior.intValue()), new ASN1Sequence(requestElements));
    return new ASN1OctetString(valueSequence.encode());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) LDAPRequest(com.unboundid.ldap.sdk.LDAPRequest) ASN1Enumerated(com.unboundid.asn1.ASN1Enumerated) ASN1Element(com.unboundid.asn1.ASN1Element) ArrayList(java.util.ArrayList) NotNull(com.unboundid.util.NotNull)

Example 18 with ASN1Sequence

use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.

the class IntermediateResponseProtocolOpTestCase method testDecodeInvalidElementType.

/**
 * Tests the behavior when trying to decode an intermediate response with an
 * invalid element type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeInvalidElementType() throws Exception {
    final ASN1Sequence s = new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_INTERMEDIATE_RESPONSE, new ASN1OctetString((byte) 0x79));
    IntermediateResponseProtocolOp.decodeProtocolOp(s);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) Test(org.testng.annotations.Test)

Example 19 with ASN1Sequence

use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.

the class AttributeTestCase method testConstructor1.

/**
 * Tests the first constructor, which takes an attribute name, using a valid
 * name.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor1() throws Exception {
    Attribute attr = new Attribute("cn");
    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);
    assertNull(attr.getValue());
    assertNull(attr.getValueByteArray());
    assertFalse(attr.hasValue());
    assertEquals(attr.getValues().length, 0);
    assertEquals(attr.getValueByteArrays().length, 0);
    assertEquals(attr.size(), 0);
    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 20 with ASN1Sequence

use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.

the class AttributeTestCase method testConstructor4Array.

/**
 * Tests the fourth constructor, which takes an attribute name and set of
 * string values, by providing the values as a string array.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor4Array() throws Exception {
    Attribute attr = new Attribute("cn", new String[] { "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)

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