760N/A/*
760N/A * CDDL HEADER START
760N/A *
760N/A * The contents of this file are subject to the terms of the
760N/A * Common Development and Distribution License, Version 1.0 only
760N/A * (the "License"). You may not use this file except in compliance
760N/A * with the License.
760N/A *
760N/A * You can obtain a copy of the license at
760N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
760N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
760N/A * See the License for the specific language governing permissions
760N/A * and limitations under the License.
760N/A *
760N/A * When distributing Covered Code, include this CDDL HEADER in each
760N/A * file and include the License file at
760N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
760N/A * add the following below this CDDL HEADER, with the fields enclosed
873N/A * by brackets "[]" replaced with your own identifying information:
760N/A * Portions Copyright [yyyy] [name of copyright owner]
760N/A *
760N/A * CDDL HEADER END
760N/A *
760N/A *
4450N/A * Copyright 2008-2009 Sun Microsystems, Inc.
760N/A */
760N/Apackage org.opends.server.controls;
760N/A
760N/A
760N/A
760N/Aimport org.testng.annotations.BeforeClass;
760N/Aimport org.testng.annotations.Test;
760N/A
760N/Aimport org.opends.server.TestCaseUtils;
4134N/Aimport org.opends.server.protocols.ldap.LDAPControl;
4134N/Aimport org.opends.server.protocols.asn1.ASN1Writer;
4134N/Aimport org.opends.server.protocols.asn1.ASN1;
4134N/Aimport org.opends.server.types.*;
760N/A
760N/Aimport static org.testng.Assert.*;
760N/A
760N/Aimport static org.opends.server.util.ServerConstants.*;
760N/A
760N/A
760N/A
760N/A/**
760N/A * This class contains a number of test cases for the proxied authorization v1
760N/A * control.
760N/A */
760N/Apublic class ProxiedAuthV1ControlTestCase
760N/A extends ControlsTestCase
760N/A{
760N/A /**
760N/A * Make sure that the server is running.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @BeforeClass()
760N/A public void startServer()
760N/A throws Exception
760N/A {
760N/A TestCaseUtils.startServer();
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the first constructor, which creates an instance of the control using
760N/A * a raw, unprocessed DN.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test()
760N/A public void testConstructor1()
760N/A throws Exception
760N/A {
760N/A // Try a DN of "null", which is not valid and will fail on the attempt to
760N/A // create the control
760N/A ProxiedAuthV1Control proxyControl;
760N/A try
760N/A {
4134N/A proxyControl = new ProxiedAuthV1Control((ByteString) null);
760N/A throw new AssertionError("Expected a failure when creating a proxied " +
760N/A "auth V1 control with a null octet string.");
760N/A } catch (Throwable t) {}
760N/A
760N/A
760N/A // Try an empty DN, which is acceptable.
4134N/A proxyControl = new ProxiedAuthV1Control(ByteString.valueOf(""));
760N/A assertTrue(proxyControl.getOID().equals(OID_PROXIED_AUTH_V1));
760N/A assertTrue(proxyControl.isCritical());
760N/A assertTrue(proxyControl.getAuthorizationDN().isNullDN());
760N/A
760N/A
760N/A // Try a valid DN, which is acceptable.
760N/A proxyControl =
4134N/A new ProxiedAuthV1Control(ByteString.valueOf("uid=test,o=test"));
760N/A assertTrue(proxyControl.getOID().equals(OID_PROXIED_AUTH_V1));
760N/A assertTrue(proxyControl.isCritical());
760N/A assertEquals(proxyControl.getAuthorizationDN(),
760N/A DN.decode("uid=test,o=test"));
760N/A
760N/A
760N/A // Try an invalid DN, which will be initally accepted but will fail when
760N/A // attempting to get the authorization DN.
4134N/A proxyControl = new ProxiedAuthV1Control(ByteString.valueOf("invalid"));
760N/A assertTrue(proxyControl.getOID().equals(OID_PROXIED_AUTH_V1));
760N/A assertTrue(proxyControl.isCritical());
760N/A try
760N/A {
760N/A proxyControl.getAuthorizationDN();
760N/A throw new AssertionError("Expected a failure when creating a proxied " +
760N/A "auth V1 control with an invalid DN string.");
760N/A } catch (Exception e) {}
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the second constructor, which creates an instance of the control
760N/A * using a processed DN.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test()
760N/A public void testConstructor2()
760N/A throws Exception
760N/A {
760N/A // Try a DN of "null", which is not valid and will fail on the attempt to
760N/A // create the control
760N/A ProxiedAuthV1Control proxyControl;
760N/A try
760N/A {
760N/A proxyControl = new ProxiedAuthV1Control((DN) null);
760N/A throw new AssertionError("Expected a failure when creating a proxied " +
760N/A "auth V1 control with a null octet string.");
760N/A } catch (Throwable t) {}
760N/A
760N/A
760N/A // Try an empty DN, which is acceptable.
760N/A proxyControl = new ProxiedAuthV1Control(DN.nullDN());
760N/A assertTrue(proxyControl.getOID().equals(OID_PROXIED_AUTH_V1));
760N/A assertTrue(proxyControl.isCritical());
760N/A assertTrue(proxyControl.getAuthorizationDN().isNullDN());
760N/A
760N/A
760N/A // Try a valid DN, which is acceptable.
760N/A proxyControl =
760N/A new ProxiedAuthV1Control(DN.decode("uid=test,o=test"));
760N/A assertTrue(proxyControl.getOID().equals(OID_PROXIED_AUTH_V1));
760N/A assertTrue(proxyControl.isCritical());
760N/A assertEquals(proxyControl.getAuthorizationDN(),
760N/A DN.decode("uid=test,o=test"));
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code decodeControl} method when the provided control has a
760N/A * criticality of "false".
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
4134N/A @Test(expectedExceptions = { DirectoryException.class })
760N/A public void testDecodeControlNotCritical()
760N/A throws Exception
760N/A {
4134N/A ByteStringBuilder bsb = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(bsb);
4134N/A writer.writeStartSequence();
4134N/A writer.writeOctetString("uid=test,o=test");
4134N/A writer.writeEndSequence();
4134N/A LDAPControl c =
4134N/A new LDAPControl(OID_PROXIED_AUTH_V1, false, bsb.toByteString());
760N/A
4134N/A ProxiedAuthV1Control.DECODER.decode(c.isCritical(), c.getValue());
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code decodeControl} method when the provided control does not
760N/A * have a value.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
4134N/A @Test(expectedExceptions = { DirectoryException.class })
760N/A public void testDecodeControlNoValue()
760N/A throws Exception
760N/A {
4134N/A LDAPControl c = new LDAPControl(OID_PROXIED_AUTH_V1, true);
760N/A
4134N/A ProxiedAuthV1Control.DECODER.decode(c.isCritical(), c.getValue());
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code decodeControl} method when the control value is not a
760N/A * sequence.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
4134N/A @Test(expectedExceptions = { DirectoryException.class })
760N/A public void testDecodeControlValueNotSequence()
760N/A throws Exception
760N/A {
4134N/A LDAPControl c =
4134N/A new LDAPControl(OID_PROXIED_AUTH_V1, true,
4134N/A ByteString.valueOf("uid=test,o=test"));
760N/A
4134N/A ProxiedAuthV1Control.DECODER.decode(c.isCritical(), c.getValue());
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code decodeControl} method when the control value is a sequence
760N/A * with zero elements.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
4134N/A @Test(expectedExceptions = { DirectoryException.class })
760N/A public void testDecodeControlValueEmptySequence()
760N/A throws Exception
760N/A {
4134N/A ByteStringBuilder bsb = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(bsb);
4134N/A writer.writeStartSequence();
4134N/A writer.writeEndSequence();
4134N/A LDAPControl c =
4134N/A new LDAPControl(OID_PROXIED_AUTH_V1, true, bsb.toByteString());
760N/A
4134N/A ProxiedAuthV1Control.DECODER.decode(c.isCritical(), c.getValue());
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code decodeControl} method when the control value is a sequence
760N/A * with multiple elements.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
4450N/A @Test
760N/A public void testDecodeControlValueMultiElementSequence()
760N/A throws Exception
760N/A {
4134N/A ByteStringBuilder bsb = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(bsb);
4134N/A writer.writeStartSequence();
4134N/A writer.writeOctetString("uid=element1,o=test");
4134N/A writer.writeOctetString("uid=element2,o=test");
4134N/A writer.writeEndSequence();
4134N/A LDAPControl c =
4134N/A new LDAPControl(OID_PROXIED_AUTH_V1, true, bsb.toByteString());
760N/A
4450N/A assertEquals(ByteString.valueOf("uid=element1,o=test"),
4450N/A ProxiedAuthV1Control.DECODER.decode(c.isCritical(),
4450N/A c.getValue()).getRawAuthorizationDN());
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code decodeControl} method when the control value is a valid
760N/A * octet string that contains an invalid DN.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test(expectedExceptions = { DirectoryException.class })
760N/A public void testDecodeControlValueInvalidDN()
760N/A throws Exception
760N/A {
4134N/A ByteStringBuilder bsb = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(bsb);
4134N/A writer.writeStartSequence();
4134N/A writer.writeOctetString("invaliddn");
4134N/A writer.writeEndSequence();
4134N/A LDAPControl c =
4134N/A new LDAPControl(OID_PROXIED_AUTH_V1, true, bsb.toByteString());
760N/A
4134N/A ProxiedAuthV1Control.DECODER.decode(c.isCritical(), c.getValue());
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code decodeControl} method when the control value is a valid
760N/A * octet string that contains an valid empty DN.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test()
760N/A public void testDecodeControlValueEmptyDN()
760N/A throws Exception
760N/A {
4134N/A ByteStringBuilder bsb = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(bsb);
4134N/A writer.writeStartSequence();
4134N/A writer.writeOctetString("");
4134N/A writer.writeEndSequence();
4134N/A LDAPControl c =
4134N/A new LDAPControl(OID_PROXIED_AUTH_V1, true, bsb.toByteString());
760N/A
4134N/A ProxiedAuthV1Control proxyControl = ProxiedAuthV1Control.DECODER.decode(c.isCritical(), c.getValue());
760N/A assertTrue(proxyControl.getAuthorizationDN().isNullDN());
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code decodeControl} method when the control value is a valid
760N/A * octet string that contains an valid non-empty DN.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test()
760N/A public void testDecodeControlValueNonEmptyDN()
760N/A throws Exception
760N/A {
4134N/A ByteStringBuilder bsb = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(bsb);
4134N/A writer.writeStartSequence();
4134N/A writer.writeOctetString("uid=test,o=test");
4134N/A writer.writeEndSequence();
4134N/A LDAPControl c =
4134N/A new LDAPControl(OID_PROXIED_AUTH_V1, true, bsb.toByteString());
760N/A
4134N/A ProxiedAuthV1Control proxyControl = ProxiedAuthV1Control.DECODER.decode(c.isCritical(), c.getValue());
760N/A assertEquals(proxyControl.getAuthorizationDN(),
760N/A DN.decode("uid=test,o=test"));
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code getRawAuthorizationDN} and {@code setRawAuthorizationDN}
760N/A * methods.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test()
760N/A public void testGetAndSetRawAuthorizationDN()
760N/A throws Exception
760N/A {
760N/A ProxiedAuthV1Control proxyControl =
4134N/A new ProxiedAuthV1Control(ByteString.valueOf(""));
4134N/A assertEquals(proxyControl.getRawAuthorizationDN(), ByteString.valueOf(""));
760N/A
4134N/A proxyControl =
4134N/A new ProxiedAuthV1Control(ByteString.valueOf("uid=test,o=test"));
760N/A assertEquals(proxyControl.getRawAuthorizationDN(),
4134N/A ByteString.valueOf("uid=test,o=test"));
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code getAuthorizationDN} and {@code setRawAuthorizationDN}
760N/A * methods.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test()
760N/A public void testGetAndSetAuthorizationDN()
760N/A throws Exception
760N/A {
760N/A ProxiedAuthV1Control proxyControl =
760N/A new ProxiedAuthV1Control(DN.nullDN());
4134N/A assertEquals(proxyControl.getRawAuthorizationDN(), ByteString.valueOf(""));
760N/A assertEquals(proxyControl.getAuthorizationDN(), DN.nullDN());
760N/A
4134N/A proxyControl =
4134N/A new ProxiedAuthV1Control(DN.decode("uid=test,o=test"));
760N/A assertEquals(proxyControl.getRawAuthorizationDN(),
4134N/A ByteString.valueOf("uid=test,o=test"));
760N/A assertEquals(proxyControl.getAuthorizationDN(),
760N/A DN.decode("uid=test,o=test"));
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code getValidatedAuthorizationDN} method for the null DN.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test()
760N/A public void testGetValidatedAuthorizationDNNullDN()
760N/A throws Exception
760N/A {
760N/A ProxiedAuthV1Control proxyControl =
760N/A new ProxiedAuthV1Control(DN.nullDN());
760N/A
773N/A assertNull(proxyControl.getAuthorizationEntry());
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code getValidatedAuthorizationDN} method for a normal user
760N/A * that exists in the directory data and doesn't have any restrictions on its
760N/A * use.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test()
760N/A public void testGetValidatedAuthorizationExistingNormalUser()
760N/A throws Exception
760N/A {
760N/A TestCaseUtils.initializeTestBackend(true);
760N/A TestCaseUtils.addEntry(
760N/A "dn: uid=test,o=test",
760N/A "objectClass: top",
760N/A "objectClass: person",
760N/A "objectClass: organizationalPerson",
760N/A "objectClass: inetOrgPerson",
760N/A "uid: test",
760N/A "givenName: Test",
760N/A "sn: User",
760N/A "cn: Test User");
760N/A
760N/A ProxiedAuthV1Control proxyControl =
760N/A new ProxiedAuthV1Control(DN.decode("uid=test,o=test"));
760N/A
773N/A assertEquals(proxyControl.getAuthorizationEntry().getDN(),
760N/A DN.decode("uid=test,o=test"));
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code getValidatedAuthorizationDN} method for a user that
760N/A * doesn't exist in the directory data.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test(expectedExceptions = { DirectoryException.class })
760N/A public void testGetValidatedAuthorizationNonExistingNormalUser()
760N/A throws Exception
760N/A {
760N/A TestCaseUtils.initializeTestBackend(true);
760N/A
760N/A ProxiedAuthV1Control proxyControl =
760N/A new ProxiedAuthV1Control(DN.decode("uid=test,o=test"));
760N/A
773N/A proxyControl.getAuthorizationEntry();
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code getValidatedAuthorizationDN} method for a disabled user.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test(expectedExceptions = { DirectoryException.class })
760N/A public void testGetValidatedAuthorizationDisabledUser()
760N/A throws Exception
760N/A {
760N/A TestCaseUtils.initializeTestBackend(true);
760N/A TestCaseUtils.addEntry(
760N/A "dn: uid=test,o=test",
760N/A "objectClass: top",
760N/A "objectClass: person",
760N/A "objectClass: organizationalPerson",
760N/A "objectClass: inetOrgPerson",
760N/A "uid: test",
760N/A "givenName: Test",
760N/A "sn: User",
760N/A "cn: Test User",
760N/A "ds-pwp-account-disabled: true");
760N/A
760N/A ProxiedAuthV1Control proxyControl =
760N/A new ProxiedAuthV1Control(DN.decode("uid=test,o=test"));
760N/A
773N/A proxyControl.getAuthorizationEntry();
760N/A }
760N/A
760N/A
760N/A
760N/A /**
760N/A * Tests the {@code toString} methods.
760N/A *
760N/A * @throws Exception If an unexpected problem occurs.
760N/A */
760N/A @Test()
760N/A public void testToString()
760N/A throws Exception
760N/A {
760N/A // The default toString() calls the version that takes a string builder
760N/A // argument, so we only need to use the default version to cover both cases.
760N/A ProxiedAuthV1Control proxyControl =
4134N/A new ProxiedAuthV1Control(ByteString.valueOf("uid=test,o=test"));
760N/A proxyControl.toString();
760N/A
760N/A proxyControl = new ProxiedAuthV1Control(DN.decode("uid=test,o=test"));
760N/A proxyControl.toString();
760N/A }
760N/A}
760N/A