Skip to content

Commit

Permalink
Extending password policy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Aug 3, 2015
1 parent 88fd503 commit 218b63d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 10 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,7 @@
import com.evolveum.midpoint.schema.constants.MidPointConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
Expand Down Expand Up @@ -259,26 +260,48 @@ private void assertPassword(String passwd, ValuePolicyType pp) {
}

@Test
public void passwordValidationTest() throws JAXBException, SchemaException, IOException {
String filename = "password-policy-complex.xml";
String pathname = BASE_PATH + filename;
File file = new File(pathname);

LOGGER.error("Positive testing: passwordGeneratorComplexTest");
ValuePolicyType pp = (ValuePolicyType) PrismTestUtil.parseObject(file).asObjectable();
public void passwordValidationTestComplex() throws Exception {
final String TEST_NAME = "passwordValidationTestComplex";
TestUtil.displayTestTile(TEST_NAME);

ValuePolicyType pp = parsePasswordPolicy("password-policy-complex.xml");

// Test on all cases
// WHEN, THEN
AssertJUnit.assertTrue(pwdValidHelper("582a**A", pp));
AssertJUnit.assertFalse(pwdValidHelper("58", pp));
AssertJUnit.assertFalse(pwdValidHelper("333a**aGaa", pp));
AssertJUnit.assertFalse(pwdValidHelper("AAA4444", pp));
}

@Test
public void passwordValidationTestTri() throws Exception {
final String TEST_NAME = "passwordValidationTestTri";
TestUtil.displayTestTile(TEST_NAME);

ValuePolicyType pp = parsePasswordPolicy("password-policy-tri.xml");

// WHEN, THEN
AssertJUnit.assertTrue(pwdValidHelper("Password1", pp));
AssertJUnit.assertFalse(pwdValidHelper("password1", pp)); // no capital letter
AssertJUnit.assertFalse(pwdValidHelper("1PASSWORD", pp)); // no lowecase letter
AssertJUnit.assertFalse(pwdValidHelper("Password", pp)); // no numeral
AssertJUnit.assertFalse(pwdValidHelper("Pa1", pp)); // too short
AssertJUnit.assertFalse(pwdValidHelper("PPPPPPPPPPPPPPPPPPPPPPPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa11111111111111111111111111111111111111111111111111111111111111111111", pp)); // too long
}

private ValuePolicyType parsePasswordPolicy(String filename) throws SchemaException, IOException {
File file = new File(BASE_PATH, filename);
return (ValuePolicyType) PrismTestUtil.parseObject(file).asObjectable();
}

private boolean pwdValidHelper(String password, ValuePolicyType pp) {
OperationResult op = new OperationResult("Password Validator test with password:" + password);
PasswordPolicyUtils.validatePassword(password, pp, op);
op.computeStatus();
LOGGER.error(op.debugDump());
String msg = "-> Policy "+pp.getName()+", password '"+password+"': "+op.getStatus();
System.out.println(msg);
LOGGER.info(msg);
LOGGER.trace(op.debugDump());
return (op.isSuccess());
}

Expand Down
63 changes: 63 additions & 0 deletions infra/common/src/test/resources/policy/password-policy-tri.xml
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2015 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<valuePolicy xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3">
<name>Testing tri policy</name>
<lifetime>
<expiration>999</expiration>
<warnBeforeExpiration>9</warnBeforeExpiration>
<lockAfterExpiration>0</lockAfterExpiration>
<minPasswordAge>0</minPasswordAge>
<passwordHistoryLength>0</passwordHistoryLength>
</lifetime>
<stringPolicy>
<description>Testing string policy</description>
<limitations>
<minLength>5</minLength>
<maxLength>32</maxLength>
<minUniqueChars>3</minUniqueChars>
<checkAgainstDictionary>false</checkAgainstDictionary>
<checkPattern />
<limit>
<description>Lowercase alphanumeric characters</description>
<minOccurs>1</minOccurs>
<mustBeFirst>false</mustBeFirst>
<characterClass>
<value>abcdefghijklmnopqrstuvwxyz</value>
</characterClass>
</limit>
<limit>
<description>Uppercase alphanumeric characters</description>
<minOccurs>1</minOccurs>
<mustBeFirst>false</mustBeFirst>
<characterClass>
<value>ABCDEFGHIJKLMNOPQRSTUVWXYZ</value>
</characterClass>
</limit>
<limit>
<description>Numeric characters</description>
<minOccurs>1</minOccurs>
<mustBeFirst>false</mustBeFirst>
<characterClass>
<value>1234567890</value>
</characterClass>
</limit>
</limitations>
</stringPolicy>
</valuePolicy>

0 comments on commit 218b63d

Please sign in to comment.