Skip to content

Commit

Permalink
fixing value generation according to the policy when minLength == 0
Browse files Browse the repository at this point in the history
  • Loading branch information
katkav committed May 14, 2014
1 parent 98b2981 commit f6e03c8
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 14 deletions.
Expand Up @@ -95,7 +95,7 @@ public static String generate(StringPolicyType policy, int defaultLength, boolea
if (minLen != 0 && minLen > defaultLength) {
defaultLength = minLen;
}
int maxLen = (policy.getLimitations().getMaxLength() == null ? defaultLength : policy.getLimitations()
int maxLen = (policy.getLimitations().getMaxLength() == null ? 0 : policy.getLimitations()
.getMaxLength().intValue());
int unique = policy.getLimitations().getMinUniqueChars() == null ? minLen : policy.getLimitations()
.getMinUniqueChars().intValue();
Expand All @@ -110,6 +110,7 @@ public static String generate(StringPolicyType policy, int defaultLength, boolea

if (minLen == 0 && maxLen == 0) {
minLen = defaultLength;
maxLen = defaultLength;
generateMinimalSize = true;
}

Expand Down
Expand Up @@ -190,6 +190,29 @@ public void testValueGenerate() throws Exception {
assertPassword(psswd, pp);

}

@Test
public void testValueGenerateEmpty() throws Exception {
LOGGER.info("===[ {} ]===", "testValueGenerateEmpty");
String pathname = BASE_PATH + "value-policy-generate-empty.xml";
File file = new File(pathname);
LOGGER.info("Positive testing {}: {}", "testValueGenerate", "value-policy-generate-empty.xml");
ValuePolicyType pp = (ValuePolicyType) PrismTestUtil.parseObject(file).asObjectable();
OperationResult op = new OperationResult("testValueGenerateEmpty");

String psswd;
psswd = ValuePolicyGenerator.generate(pp.getStringPolicy(), 10, true, op);
LOGGER.info("Generated password:" + psswd);
System.out.println("Generated password: " + psswd);
op.computeStatus();
if (!op.isSuccess()) {
LOGGER.info("Result:" + op.debugDump());
AssertJUnit.fail("Password generator failed:\n"+op.debugDump());
}
assertNotNull(psswd);
assertPassword(psswd, pp);

}

public void passwordGeneratorTest(final String TEST_NAME, String policyFilename) throws JAXBException, SchemaException, IOException {
LOGGER.info("===[ {} ]===", TEST_NAME);
Expand Down
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2010-2013 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"
oid="81818181-76e0-59e2-8888-3d4f02d3fffb"
version="10">
<name>
<orig xmlns="http://prism.evolveum.com/xml/ns/public/types-3">Numeric PIN Policy</orig>
<norm xmlns="http://prism.evolveum.com/xml/ns/public/types-3">numeric pin policy</norm>
</name>
<description>A policy useful for generating PIN.</description>
<c:lifetime xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:icfs="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3"
xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3"
xmlns:icfc="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/connector-schema-3"
xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3"
xmlns:cap="http://midpoint.evolveum.com/xml/ns/public/resource/capabilities-3"
xmlns:apti="http://midpoint.evolveum.com/xml/ns/public/common/api-types-3"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:enc="http://www.w3.org/2001/04/xmlenc#">
<c:expiration>999</c:expiration>
<c:warnBeforeExpiration>9</c:warnBeforeExpiration>
<c:lockAfterExpiration>0</c:lockAfterExpiration>
<c:minPasswordAge>0</c:minPasswordAge>
<c:passwordHistoryLength>0</c:passwordHistoryLength>
</c:lifetime>
<c:stringPolicy xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:icfs="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3"
xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3"
xmlns:icfc="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/connector-schema-3"
xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3"
xmlns:cap="http://midpoint.evolveum.com/xml/ns/public/resource/capabilities-3"
xmlns:apti="http://midpoint.evolveum.com/xml/ns/public/common/api-types-3"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:enc="http://www.w3.org/2001/04/xmlenc#">
<c:description>String validation policy</c:description>
<c:limitations>
<c:minLength>0</c:minLength>
<c:minUniqueChars>0</c:minUniqueChars>
<c:checkAgainstDictionary>true</c:checkAgainstDictionary>
<c:checkPattern/>
</c:limitations>
</c:stringPolicy>
</valuePolicy>
Expand Up @@ -55,6 +55,7 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.GenerateExpressionEvaluatorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.StringPolicyType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;

/**
Expand Down Expand Up @@ -83,6 +84,17 @@ public class GenerateExpressionEvaluator<V extends PrismValue> implements Expres
/* (non-Javadoc)
* @see com.evolveum.midpoint.common.expression.ExpressionEvaluator#evaluate(java.util.Collection, java.util.Map, boolean, java.lang.String, com.evolveum.midpoint.schema.result.OperationResult)
*/

private boolean isNotEmptyMinLength(StringPolicyType policy){
Integer minLength = policy.getLimitations().getMinLength();
if (minLength != null){
if (minLength.intValue() == 0){
return false;
}
return true;
}
return false;
}
@Override
public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext params) throws SchemaException,
ExpressionEvaluationException, ObjectNotFoundException {
Expand All @@ -107,7 +119,7 @@ public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext params)
// TODO: generate value based on stringPolicyType (if not null)
String stringValue = null;
if (stringPolicyType != null) {
if (stringPolicyType.getLimitations().getMinLength() != null) {
if (isNotEmptyMinLength(stringPolicyType)) {
stringValue = ValuePolicyGenerator.generate(stringPolicyType, DEFAULT_LENGTH, true, params.getResult());
} else{
stringValue = ValuePolicyGenerator.generate(stringPolicyType, DEFAULT_LENGTH, false, params.getResult());
Expand Down
Expand Up @@ -1118,7 +1118,7 @@ public void testGenerateDefault() throws Exception {
PrismAsserts.assertTripleNoMinus(outputTriple);

System.out.println("Generated value (1): " + value1);
assertGeneratedValue(value1, stringPolicy, null, false);
assertGeneratedValue(value1, stringPolicy, null, false, false);

// WHEN (2)
mapping.evaluate(null, opResult);
Expand All @@ -1127,7 +1127,7 @@ public void testGenerateDefault() throws Exception {
outputTriple = mapping.getOutputTriple();
String value2 = MappingTestEvaluator.getSingleValue("plus set", outputTriple.getZeroSet());
System.out.println("Generated value (2): " + value2);
assertGeneratedValue(value2, stringPolicy, null, false);
assertGeneratedValue(value2, stringPolicy, null, false, false);
PrismAsserts.assertTripleNoPlus(outputTriple);
PrismAsserts.assertTripleNoMinus(outputTriple);

Expand All @@ -1137,14 +1137,20 @@ public void testGenerateDefault() throws Exception {
@Test
public void testGeneratePolicy() throws Exception {
final String TEST_NAME = "testGeneratePolicy";
generatePolicy(TEST_NAME, "mapping-generate-policy.xml", "c0c010c0-d34d-b33f-f00d-999888111111.xml", null);
generatePolicy(TEST_NAME, "mapping-generate-policy.xml", "c0c010c0-d34d-b33f-f00d-999888111111.xml", null, false);
}

@Test
public void testGeneratePolicyEmpty() throws Exception {
final String TEST_NAME = "testGeneratePolicy";
generatePolicy(TEST_NAME, "mapping-generate-policy-empty.xml", "c0c010c0-d34d-b33f-f00d-999888111114.xml", null, true);
}

@Test
public void testGeneratePolicyBad() throws Exception {
final String TEST_NAME = "testGeneratePolicy";
try {
generatePolicy(TEST_NAME, "mapping-generate-policy-bad.xml", "c0c010c0-d34d-b33f-f00d-999888111113.xml", null);
generatePolicy(TEST_NAME, "mapping-generate-policy-bad.xml", "c0c010c0-d34d-b33f-f00d-999888111113.xml", null, false);
AssertJUnit.fail("Unexpected success");
} catch (ExpressionEvaluationException e) {
// This is expected, the policy is broken
Expand All @@ -1155,10 +1161,10 @@ public void testGeneratePolicyBad() throws Exception {
public void testGeneratePolicyNumericString() throws Exception {
final String TEST_NAME = "testGeneratePolicyNumericString";
generatePolicy(TEST_NAME, "mapping-generate-policy-numeric.xml", "c0c010c0-d34d-b33f-f00d-999888111112.xml",
PATTERN_NUMERIC);
PATTERN_NUMERIC, false);
}

private void generatePolicy(final String TEST_NAME, String mappingFileName, String policyFileName, String pattern)
private void generatePolicy(final String TEST_NAME, String mappingFileName, String policyFileName, String pattern, boolean ignoreMax)
throws Exception {
TestUtil.displayTestTile(TEST_NAME);

Expand All @@ -1183,7 +1189,7 @@ private void generatePolicy(final String TEST_NAME, String mappingFileName, Stri

System.out.println("Generated value (1): " + value1);
assertNotNull("Generated null value", value1);
assertGeneratedValue(value1, stringPolicy, pattern, false);
assertGeneratedValue(value1, stringPolicy, pattern, false, ignoreMax);

// WHEN (2)
mapping.evaluate(null, opResult);
Expand All @@ -1193,21 +1199,23 @@ private void generatePolicy(final String TEST_NAME, String mappingFileName, Stri
String value2 = MappingTestEvaluator.getSingleValue("plus set", outputTriple.getZeroSet());
System.out.println("Generated value (2): " + value2);
assertNotNull("Generated null value", value2);
assertGeneratedValue(value2, stringPolicy, pattern, false);
assertGeneratedValue(value2, stringPolicy, pattern, false, ignoreMax);
PrismAsserts.assertTripleNoPlus(outputTriple);
PrismAsserts.assertTripleNoMinus(outputTriple);

assertFalse("Generated the same value", value1.equals(value2));
}

private void assertGeneratedValue(String value, StringPolicyType stringPolicy, String pattern, boolean ignoreMin) {
private void assertGeneratedValue(String value, StringPolicyType stringPolicy, String pattern, boolean ignoreMin, boolean ignoreMax) {
if (stringPolicy == null) {
assertEquals("Unexpected generated value length", GenerateExpressionEvaluator.DEFAULT_LENGTH, value.length());
} else {
if (!ignoreMin) {
assertTrue("Value '"+value+"' too short, minLength="+stringPolicy.getLimitations().getMinLength()+", length="+value.length(), value.length() >= stringPolicy.getLimitations().getMinLength());
}
assertTrue("Value '"+value+"' too long, maxLength="+stringPolicy.getLimitations().getMaxLength()+", length="+value.length(), value.length() <= stringPolicy.getLimitations().getMaxLength());
if (!ignoreMax) {
assertTrue("Value '"+value+"' too long, maxLength="+stringPolicy.getLimitations().getMaxLength()+", length="+value.length(), value.length() <= stringPolicy.getLimitations().getMaxLength());
}
// TODO: better validation
}
if (pattern != null) {
Expand Down Expand Up @@ -1264,7 +1272,7 @@ TEST_NAME, stringPolicy, new ItemPath(
System.out.println("Generated value (1): " + value1);
assertNotNull("Generated null value", value1);
// We need to ignore the minLength. Conversion string -> number -> string may lose the leading zeroes
assertGeneratedValue(value1.toString(), stringPolicy, PATTERN_NUMERIC, true);
assertGeneratedValue(value1.toString(), stringPolicy, PATTERN_NUMERIC, true, false);

// WHEN (2)
mapping.evaluate(null, opResult);
Expand All @@ -1278,7 +1286,7 @@ TEST_NAME, stringPolicy, new ItemPath(
PrismAsserts.assertTripleNoMinus(outputTriple);

assertFalse("Generated the same value", value1.equals(value2));
assertGeneratedValue(value1.toString(), stringPolicy, PATTERN_NUMERIC, true);
assertGeneratedValue(value1.toString(), stringPolicy, PATTERN_NUMERIC, true, false);
}

@Test
Expand Down
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2010-2013 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.
-->

<mapping xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3">
<expression>
<generate>
<valuePolicyRef oid="c0c010c0-d34d-b33f-f00d-999888111114"/>
</generate>
</expression>
</mapping>

0 comments on commit f6e03c8

Please sign in to comment.