| | |
| | | package org.opends.server.controls; |
| | | |
| | | import static org.opends.server.util.ServerConstants.*; |
| | | import static org.testng.Assert.*; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Set; |
| | | |
| | | import org.opends.server.types.*; |
| | | import org.forgerock.opendj.io.ASN1; |
| | | import org.forgerock.opendj.io.ASN1Writer; |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | | import org.forgerock.opendj.ldap.ByteStringBuilder; |
| | | import org.opends.server.protocols.ldap.LDAPControl; |
| | | import org.opends.server.protocols.ldap.LDAPReader; |
| | | import org.forgerock.opendj.io.ASN1; |
| | | import org.forgerock.opendj.io.ASN1Writer; |
| | | import org.opends.server.types.DirectoryException; |
| | | import org.testng.annotations.DataProvider; |
| | | import org.testng.annotations.Test; |
| | | |
| | | import static org.testng.Assert.*; |
| | | |
| | | /** |
| | | * Test password control. |
| | | */ |
| | | @SuppressWarnings("javadoc") |
| | | public class PasswordControlTest |
| | | extends ControlsTestCase |
| | | { |
| | |
| | | @DataProvider(name = "passwordPolicyErrorTypeData") |
| | | public Object[][] createPasswordPolicyErrorTypeData() |
| | | { |
| | | |
| | | HashMap<Integer, String> values = new HashMap<Integer, String>(); |
| | | values.put(0, "passwordExpired"); |
| | | values.put(1, "accountLocked"); |
| | |
| | | values.put(6, "passwordTooShort"); |
| | | values.put(7, "passwordTooYoung"); |
| | | values.put(8, "passwordInHistory"); |
| | | return new Object[][] |
| | | { |
| | | { values } }; |
| | | return new Object[][] { { values } }; |
| | | } |
| | | |
| | | /** |
| | |
| | | @DataProvider(name = "passwordPolicyWarningTypeData") |
| | | public Object[][] createPasswordPolicyWarningTypeData() |
| | | { |
| | | |
| | | HashMap<Byte, String> values = new HashMap<Byte, String>(); |
| | | values.put((byte)0x80, "timeBeforeExpiration"); |
| | | values.put((byte)0x81, "graceAuthNsRemaining"); |
| | | return new Object[][] |
| | | { |
| | | { values } }; |
| | | return new Object[][] { { values } }; |
| | | } |
| | | |
| | | /** |
| | |
| | | // Retrieve the values |
| | | PasswordPolicyWarningType[] vals = PasswordPolicyWarningType.values(); |
| | | |
| | | // Check if we have the correct munber |
| | | // Check if we have the correct number |
| | | assertEquals(vals.length, exceptedValues.size()); |
| | | |
| | | // Check if we have the correct byte value |
| | |
| | | for (int i = 0x70; i < 0x90; i++) |
| | | { |
| | | byte b = new Integer(i).byteValue(); |
| | | if (keys.contains(b)) |
| | | { |
| | | continue; |
| | | } |
| | | else |
| | | if (!keys.contains(b)) |
| | | { |
| | | assertNull(PasswordPolicyWarningType.valueOf(b)); |
| | | PasswordPolicyWarningType val = PasswordPolicyWarningType.valueOf(b); |
| | | assertNull(val); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | @DataProvider(name = "passwordExpiredControlData") |
| | | public Object[][] createPasswordExpiredControlData() |
| | | { |
| | | |
| | | return new Object[][] { |
| | | { true }, |
| | | { false }, |
| | |
| | | try |
| | | { |
| | | pec = PasswordExpiredControl.DECODER.decode(control.isCritical(), control.getValue()); |
| | | assertTrue(false, |
| | | "should be allow to create a passwordExpiredControl with value"); |
| | | fail("should be allow to create a passwordExpiredControl with value"); |
| | | } |
| | | catch (DirectoryException e) |
| | | catch (DirectoryException expected) |
| | | { |
| | | // Normal case |
| | | assertTrue(true, |
| | | "should be allow to create a passwordExpiredControl with value"); |
| | | } |
| | | |
| | | // Check toString |
| | |
| | | @DataProvider(name = "passwordExpiringControlData") |
| | | public Object[][] createPasswordExpiringControlData() |
| | | { |
| | | |
| | | return new Object[][] { |
| | | { true, 1}, |
| | | { false, 2}, |
| | |
| | | try |
| | | { |
| | | pec = PasswordExpiringControl.DECODER.decode(control.isCritical(), control.getValue()); |
| | | assertTrue(false, |
| | | "shouldn't be allow to create PasswordExpiringControl without value"); |
| | | fail("shouldn't be allowed to create PasswordExpiringControl without value"); |
| | | } |
| | | catch (DirectoryException e) |
| | | catch (DirectoryException expected) |
| | | { |
| | | // Normal case |
| | | assertTrue(true, |
| | | "shouldn't be allow to create PasswordExpiringControl without value"); |
| | | } |
| | | |
| | | control = new LDAPControl(OID_NS_PASSWORD_EXPIRING, isCritical, |
| | |
| | | try |
| | | { |
| | | pec = PasswordExpiringControl.DECODER.decode(control.isCritical(), control.getValue()); |
| | | assertTrue(false, |
| | | "shouldn't be allow to create PasswordExpiringControl with a wrong value"); |
| | | fail("shouldn't be allowed to create PasswordExpiringControl with a wrong value"); |
| | | } |
| | | catch (DirectoryException e) |
| | | catch (DirectoryException expected) |
| | | { |
| | | // Normal case |
| | | assertTrue(true, |
| | | "shouldn't be allow to create PasswordExpiringControl with a wrong value"); |
| | | } |
| | | |
| | | // Check encode/decode |
| | |
| | | @DataProvider(name = "passwordPolicyRequestControlData") |
| | | public Object[][] createPasswordPolicyRequestControlData() |
| | | { |
| | | |
| | | return new Object[][] { |
| | | { true}, |
| | | { false}, |
| | |
| | | try |
| | | { |
| | | pec = PasswordPolicyRequestControl.DECODER.decode(control.isCritical(), control.getValue()); |
| | | assertTrue(false, |
| | | "should be allow to create a PasswordPolicyRequestControl with value"); |
| | | fail("should be allow to create a PasswordPolicyRequestControl with value"); |
| | | } |
| | | catch (DirectoryException e) |
| | | catch (DirectoryException expected) |
| | | { |
| | | // Normal case |
| | | assertTrue(true, |
| | | "should be allow to create a PasswordPolicyRequestControl with value"); |
| | | } |
| | | |
| | | // Check toString |
| | |
| | | @DataProvider(name = "passwordPolicyResponseControl") |
| | | public Object[][] createPasswordPolicyResponseControlData() |
| | | { |
| | | |
| | | return new Object[][] { |
| | | { true , -1}, |
| | | { false , -1}, |
| | |
| | | * Test PasswordPolicyResponseControl. |
| | | */ |
| | | @Test(dataProvider = "passwordPolicyResponseControl") |
| | | public void passwordPolicyResponseControlTest( |
| | | boolean isCritical, int warningValue) |
| | | public void passwordPolicyResponseControlTest(boolean isCritical, int warningValue) |
| | | throws Exception |
| | | { |
| | | // Check default constructor |
| | |
| | | // PasswordPolicyErrorType errorType) |
| | | for (PasswordPolicyErrorType errorType : PasswordPolicyErrorType.values()) |
| | | { |
| | | for (PasswordPolicyWarningType warningType : PasswordPolicyWarningType |
| | | .values()) |
| | | for (PasswordPolicyWarningType warningType : PasswordPolicyWarningType.values()) |
| | | { |
| | | pprc = new PasswordPolicyResponseControl(isCritical, |
| | | warningType, warningValue, errorType); |
| | |
| | | |
| | | |
| | | // check encode/decode |
| | | PasswordPolicyResponseControl control ; |
| | | ByteStringBuilder bsb = new ByteStringBuilder(); |
| | | ASN1Writer writer = ASN1.getWriter(bsb); |
| | | for (PasswordPolicyErrorType errorType : PasswordPolicyErrorType.values()) |
| | | { |
| | | for (PasswordPolicyWarningType warningType : PasswordPolicyWarningType |
| | | .values()) |
| | | for (PasswordPolicyWarningType warningType : PasswordPolicyWarningType.values()) |
| | | { |
| | | bsb.clear(); |
| | | control = new PasswordPolicyResponseControl(isCritical, |
| | | warningType, warningValue, errorType); |
| | | PasswordPolicyResponseControl control = new PasswordPolicyResponseControl( |
| | | isCritical, warningType, warningValue, errorType); |
| | | control.write(writer); |
| | | LDAPControl c = LDAPReader.readControl(ASN1.getReader(bsb)); |
| | | pprc = PasswordPolicyResponseControl.DECODER.decode(c.isCritical(), c.getValue()); |
| | |
| | | { |
| | | c = new LDAPControl(OID_PASSWORD_POLICY_CONTROL, isCritical); |
| | | pprc = PasswordPolicyResponseControl.DECODER.decode(c.isCritical(), c.getValue()); |
| | | assertTrue(false,"the control should have a value"); |
| | | fail("the control should have a value"); |
| | | } |
| | | catch (DirectoryException e) |
| | | catch (DirectoryException expected) |
| | | { |
| | | // normal case |
| | | assertTrue(true,"the control should have a value"); |
| | | } |
| | | |
| | | |
| | |
| | | null, warningValue, errorType); |
| | | control.write(writer); |
| | | c = LDAPReader.readControl(ASN1.getReader(bsb)); |
| | | try |
| | | { |
| | | pprc = PasswordPolicyResponseControl.DECODER.decode(c.isCritical(), c.getValue()); |
| | | assertNull(pprc.getWarningType()); |
| | | } |
| | | catch (DirectoryException e) |
| | | { |
| | | assertTrue(false,"We should be able to decode the control"); |
| | | } |
| | | pprc = PasswordPolicyResponseControl.DECODER.decode(c.isCritical(), c.getValue()); |
| | | assertNull(pprc.getWarningType()); |
| | | |
| | | // check null error type |
| | | bsb.clear(); |
| | |
| | | warningType, warningValue, null); |
| | | control.write(writer); |
| | | c = LDAPReader.readControl(ASN1.getReader(bsb)); |
| | | try |
| | | { |
| | | pprc = PasswordPolicyResponseControl.DECODER.decode(c.isCritical(), c.getValue()); |
| | | assertNull(pprc.getErrorType()); |
| | | } |
| | | catch (DirectoryException e) |
| | | { |
| | | assertTrue(false,"We should be able to decode the control"); |
| | | } |
| | | pprc = PasswordPolicyResponseControl.DECODER.decode(c.isCritical(), c.getValue()); |
| | | assertNull(pprc.getErrorType()); |
| | | } |
| | | } |
| | | } |