use of com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateOperation in project ldapsdk by pingidentity.
the class ManageAccountTestCase method testSearchForEntries.
/**
* Tests the behavior when running with arguments used to search for entries
* rather than specifying them by DN.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testSearchForEntries() throws Exception {
// Get an instance of the manage-account tool that we will run to generate a
// sample variable rate data file. Not only can we use this file to get
// test coverage later, but we can also use the tool instance to get the
// argument parser available so that we can introspect it to get information
// about what arguments we can use.
final String variableRateDataFile = createTempFile().getAbsolutePath();
final ManageAccount tool = new ManageAccount(null, null);
assertEquals(tool.runTool("--generateSampleRateFile", variableRateDataFile), ResultCode.SUCCESS);
// Create some other files that will be used for testing.
final String rejectFile = createTempFile().getAbsolutePath();
final String filterFile = createTempFile("# Comment at the top", "(uid=user.1)", "# Comment in the middle. Also, blank line follows.", "", "(uid=user.2)", "(uid=user.3)", "(mail=user.4@example.com)", "", "# The following is not a valid filter", "this is not a valid filter", "", "(objectClass=*)", "# Comment at the end").getAbsolutePath();
final String userIDFile = createTempFile("# Comment at the top", "user.1", "# Comment in the middle. Also, blank line follows.", "", "user.2", "user.3", "user.4@example.com", "", "# Comment at the end").getAbsolutePath();
// Create an in-memory directory server instance with fake support for the
// password policy state extended operation.
final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
final String[] referralURLs = { "ldap://ds1.example.com:389/dc=example,dc=com", "ldap://ds2.example.com:389/dc=example,dc=com" };
final PasswordPolicyStateOperation[] resultOperations = { new PasswordPolicyStateOperation(PasswordPolicyStateOperation.OP_TYPE_GET_ACCOUNT_DISABLED_STATE, new ASN1OctetString[] { new ASN1OctetString("false") }), new PasswordPolicyStateOperation(PasswordPolicyStateOperation.OP_TYPE_GET_FAILURE_LOCKOUT_TIME, null), new PasswordPolicyStateOperation(PasswordPolicyStateOperation.OP_TYPE_GET_ACCOUNT_USABILITY_NOTICES, new ASN1OctetString[] { new ASN1OctetString("Notice 1"), new ASN1OctetString("Notice 2"), new ASN1OctetString("Notice 3") }) };
cfg.addExtendedOperationHandler(new CannedResponsePWPStateInMemoryExtendedOperationHandler(new PasswordPolicyStateExtendedResult(-1, ResultCode.SUCCESS, "Success", "ou=Matched DN,dc=example,dc=com", referralURLs, "uid=test.user,ou=People,dc=example,dc=com", resultOperations, null)));
final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(cfg);
ds.startListening();
try {
ds.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
ds.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
ds.add("dn: uid=user.1,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: user.1", "givenName: User", "sn: 1", "cn: User 1", "mail: user.1@example.com");
ds.add("dn: uid=user.4,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: user.4", "givenName: User", "sn: 4", "cn: User 4", "mail: user.4@example.com");
final ByteArrayOutputStream out = new ByteArrayOutputStream();
assertEquals(ManageAccount.main(out, out, "get-all", "--hostname", "127.0.0.1", "--port", String.valueOf(ds.getListenPort()), "--targetFilter", "(uid=user.1)", "--targetFilter", "(uid=user.2)", "--targetFilter", "(objectClass=*)", "--targetFilterFile", filterFile, "--targetUserID", "user.1", "--targetUserID", "user.2", "--targetUserIDFile", userIDFile, "--rejectFile", rejectFile, "--appendToRejectFile", "--numThreads", "10", "--numSearchThreads", "10", "--variableRateData", variableRateDataFile, "--simplePageSize", "1"), ResultCode.SUCCESS, "manage-account failed with output: " + StaticUtils.toUTF8String(out.toByteArray()));
out.reset();
assertEquals(ManageAccount.main(out, out, "get-all", "--hostname", "127.0.0.1", "--port", String.valueOf(ds.getListenPort()), "--targetFilter", "(uid=user.1)", "--targetFilter", "(uid=user.2)", "--targetFilter", "(objectClass=*)", "--targetFilterFile", filterFile, "--targetUserID", "user.1", "--targetUserID", "user.2", "--targetUserIDFile", userIDFile, "--rejectFile", rejectFile, "--numThreads", "10", "--numSearchThreads", "10", "--variableRateData", variableRateDataFile, "--baseDN", "ou=missing,dc=example,dc=com"), ResultCode.SUCCESS, "manage-account failed with output: " + StaticUtils.toUTF8String(out.toByteArray()));
out.reset();
assertEquals(ManageAccount.main(out, out, "get-all", "--hostname", "127.0.0.1", "--port", String.valueOf(ds.getListenPort()), "--targetFilter", "(uid=user.1)", "--targetFilter", "(uid=user.2)", "--targetFilter", "(objectClass=*)", "--targetFilterFile", filterFile, "--targetUserID", "user.1", "--targetUserID", "user.2", "--targetUserIDFile", userIDFile, "--rejectFile", rejectFile, "--appendToRejectFile", "--variableRateData", variableRateDataFile, "--baseDN", "ou=missing,dc=example,dc=com", "--simplePageSize", "1"), ResultCode.SUCCESS, "manage-account failed with output: " + StaticUtils.toUTF8String(out.toByteArray()));
} finally {
ds.shutDown(true);
}
}
use of com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateOperation in project ldapsdk by pingidentity.
the class ManageAccountTestCase method testAllSuccessResult.
/**
* Tests the behavior when running the tool and expecting a success result.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testAllSuccessResult() throws Exception {
// Get an instance of the manage-account tool that we will run to generate a
// sample variable rate data file. Not only can we use this file to get
// test coverage later, but we can also use the tool instance to get the
// argument parser available so that we can introspect it to get information
// about what arguments we can use.
final String variableRateDataFile = createTempFile().getAbsolutePath();
final ManageAccount tool = new ManageAccount(null, null);
assertEquals(tool.runTool("--generateSampleRateFile", variableRateDataFile), ResultCode.SUCCESS);
final ArgumentParser parser = tool.getArgumentParser();
assertNotNull(parser);
// Create some other files that will be used for testing.
final String rejectFile = createTempFile().getAbsolutePath();
final String dnFile = createTempFile("# Comment at the top", "uid=user.1,ou=People,dc=example,dc=com", "# Comment in the middle. Also, blank line follows.", "", "uid=user.2,ou=People,dc=example,dc=com", "dn:uid=user.3,ou=People,dc=example,dc=com", "dn: uid=user.4,ou=People,dc=example,dc=com", "dn::" + Base64.encode("uid=user.5,ou=People,dc=example,dc=com"), "dn:: " + Base64.encode("uid=user.6,ou=People,dc=example,dc=com"), "", "# Comment at the end").getAbsolutePath();
// Create an in-memory directory server instance with fake support for the
// password policy state extended operation.
final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
final String[] referralURLs = { "ldap://ds1.example.com:389/dc=example,dc=com", "ldap://ds2.example.com:389/dc=example,dc=com" };
final PasswordPolicyStateOperation[] resultOperations = { new PasswordPolicyStateOperation(PasswordPolicyStateOperation.OP_TYPE_GET_ACCOUNT_DISABLED_STATE, new ASN1OctetString[] { new ASN1OctetString("false") }), new PasswordPolicyStateOperation(PasswordPolicyStateOperation.OP_TYPE_GET_FAILURE_LOCKOUT_TIME, null), new PasswordPolicyStateOperation(PasswordPolicyStateOperation.OP_TYPE_GET_ACCOUNT_USABILITY_NOTICES, new ASN1OctetString[] { new ASN1OctetString(new PasswordPolicyStateAccountUsabilityNotice(PasswordPolicyStateAccountUsabilityNotice.NOTICE_TYPE_IN_MINIMUM_PASSWORD_AGE, PasswordPolicyStateAccountUsabilityNotice.NOTICE_NAME_IN_MINIMUM_PASSWORD_AGE, "notice message").toString()), new ASN1OctetString("Notice 2") }), new PasswordPolicyStateOperation(PasswordPolicyStateOperation.OP_TYPE_GET_ACCOUNT_USABILITY_WARNINGS, new ASN1OctetString[] { new ASN1OctetString(new PasswordPolicyStateAccountUsabilityWarning(PasswordPolicyStateAccountUsabilityWarning.WARNING_TYPE_ACCOUNT_EXPIRING, PasswordPolicyStateAccountUsabilityWarning.WARNING_NAME_ACCOUNT_EXPIRING, "warning message").toString()), new ASN1OctetString("Warning 2") }), new PasswordPolicyStateOperation(PasswordPolicyStateOperation.OP_TYPE_GET_ACCOUNT_USABILITY_ERRORS, new ASN1OctetString[] { new ASN1OctetString(new PasswordPolicyStateAccountUsabilityError(PasswordPolicyStateAccountUsabilityError.ERROR_TYPE_ACCOUNT_EXPIRED, PasswordPolicyStateAccountUsabilityError.ERROR_NAME_ACCOUNT_EXPIRED, "error message").toString()), new ASN1OctetString("Error 2") }) };
cfg.addExtendedOperationHandler(new CannedResponsePWPStateInMemoryExtendedOperationHandler(new PasswordPolicyStateExtendedResult(-1, ResultCode.SUCCESS, "Success", "ou=Matched DN,dc=example,dc=com", referralURLs, "uid=test.user,ou=People,dc=example,dc=com", resultOperations, null)));
final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(cfg);
ds.startListening();
try {
final ArrayList<String> argList = new ArrayList<String>(20);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
for (final ManageAccountSubCommandType t : ManageAccountSubCommandType.values()) {
for (final String name : t.getAllNames()) {
argList.clear();
argList.add(name);
argList.add("--hostname");
argList.add("127.0.0.1");
argList.add("--port");
argList.add(String.valueOf(ds.getListenPort()));
argList.add("--targetDN");
argList.add("uid=test.user,ou=People,dc=example,dc=com");
final SubCommand sc = parser.getSubCommand(name);
assertNotNull(sc);
final ArgumentParser subCommandParser = sc.getArgumentParser();
final Argument a = subCommandParser.getNamedArgument('O');
if (a == null) {
String[] args = argList.toArray(StaticUtils.NO_STRINGS);
assertEquals(ManageAccount.main(out, out, args), ResultCode.SUCCESS, "Failed with arguments " + argList + ": " + StaticUtils.toUTF8String(out.toByteArray()));
out.reset();
args = argList.toArray(StaticUtils.NO_STRINGS);
assertEquals(ManageAccount.main(out, out, args), ResultCode.SUCCESS, "Failed with arguments " + argList + ": " + StaticUtils.toUTF8String(out.toByteArray()));
continue;
}
if (!a.isRequired()) {
out.reset();
String[] args = argList.toArray(StaticUtils.NO_STRINGS);
assertEquals(ManageAccount.main(null, null, args), ResultCode.SUCCESS, "Failed with arguments " + argList + ": " + StaticUtils.toUTF8String(out.toByteArray()));
argList.add("--suppressEmptyResultOperations");
args = argList.toArray(StaticUtils.NO_STRINGS);
out.reset();
assertEquals(ManageAccount.main(null, null, args), ResultCode.SUCCESS, "Failed with arguments " + argList + ": " + StaticUtils.toUTF8String(out.toByteArray()));
argList.remove(argList.size() - 1);
}
final String value1;
final String value2;
if (a instanceof BooleanValueArgument) {
value1 = "true";
value2 = "false";
} else if (a instanceof StringArgument) {
if (sc.hasName("set-last-login-ip-address")) {
value1 = "1.2.3.4";
value2 = "5.6.7.8";
} else {
value1 = "value 1";
value2 = "value 2";
}
} else if (a instanceof TimestampArgument) {
final long now = System.currentTimeMillis();
value1 = StaticUtils.encodeGeneralizedTime(now - 1L);
value2 = StaticUtils.encodeGeneralizedTime(now);
} else {
throw new AssertionError("Unexpected argument type for argument " + a.getIdentifierString() + " in subcommand " + name + ": " + a.getClass().getName());
}
argList.add("--ratePerSecond");
argList.add("100");
argList.add("--variableRateData");
argList.add(variableRateDataFile);
argList.add("--rejectFile");
argList.add(rejectFile);
argList.add("--targetDNFile");
argList.add(dnFile);
argList.add("--numThreads");
argList.add("10");
argList.add("--numSearchThreads");
argList.add("10");
argList.add(a.getIdentifierString());
argList.add(value1);
out.reset();
String[] args = argList.toArray(StaticUtils.NO_STRINGS);
assertEquals(ManageAccount.main(null, null, args), ResultCode.SUCCESS, "Failed with arguments " + argList + ": " + StaticUtils.toUTF8String(out.toByteArray()));
out.reset();
argList.add("--suppressEmptyResultOperations");
args = argList.toArray(StaticUtils.NO_STRINGS);
assertEquals(ManageAccount.main(null, null, args), ResultCode.SUCCESS, "Failed with arguments " + argList + ": " + StaticUtils.toUTF8String(out.toByteArray()));
argList.remove(argList.size() - 1);
if (a.getMaxOccurrences() > 1) {
argList.add(a.getIdentifierString());
argList.add(value2);
out.reset();
args = argList.toArray(StaticUtils.NO_STRINGS);
assertEquals(ManageAccount.main(null, null, args), ResultCode.SUCCESS, "Failed with arguments " + argList + ": " + StaticUtils.toUTF8String(out.toByteArray()));
out.reset();
argList.add("--suppressEmptyResultOperations");
args = argList.toArray(StaticUtils.NO_STRINGS);
assertEquals(ManageAccount.main(null, null, args), ResultCode.SUCCESS, "Failed with arguments " + argList + ": " + StaticUtils.toUTF8String(out.toByteArray()));
argList.remove(argList.size() - 1);
}
}
}
} finally {
ds.shutDown(true);
}
}
use of com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateOperation in project ldapsdk by pingidentity.
the class ManageAccountProcessor method createResultEntry.
/**
* Creates an entry that encapsulates the content of the provided result.
*
* @param request The request that was processed.
* @param result The result of the processing.
*
* @return The entry that was created.
*/
@NotNull()
private Entry createResultEntry(@NotNull final PasswordPolicyStateExtendedRequest request, @NotNull final LDAPResult result) {
final Entry e = new Entry(request.getUserDN());
e.addAttribute("base-command-line", commandLine + " --targetDN " + StaticUtils.cleanExampleCommandLineArgument(e.getDN()));
e.addAttribute("result-code", String.valueOf(result.getResultCode().intValue()));
final String resultCodeName = result.getResultCode().getName();
if (resultCodeName != null) {
e.addAttribute("result-code-name", resultCodeName);
}
final String diagnosticMessage = result.getDiagnosticMessage();
if (diagnosticMessage != null) {
e.addAttribute("diagnostic-message", diagnosticMessage);
}
final String matchedDN = result.getMatchedDN();
if (matchedDN != null) {
e.addAttribute("matched-dn", matchedDN);
}
final String[] referralURLs = result.getReferralURLs();
if ((referralURLs != null) && (referralURLs.length > 0)) {
e.addAttribute("referral-url", referralURLs);
}
if (!(result instanceof PasswordPolicyStateExtendedResult)) {
return e;
}
final PasswordPolicyStateExtendedResult r = (PasswordPolicyStateExtendedResult) result;
for (final PasswordPolicyStateOperation o : r.getOperations()) {
final String[] values = o.getStringValues();
if (values.length == 0) {
if (suppressEmptyResultOperations) {
continue;
}
}
final String attrName;
final ManageAccountSubCommandType subcommandType = ManageAccountSubCommandType.forOperationType(o.getOperationType());
if (subcommandType == null) {
if (o.getOperationType() == 39) {
// This is a deprecated response that the client doesn't support, but
// older servers may return it.
attrName = "get-password-history";
} else {
// This result may come from a newer version of the server that has
// additional password policy state operation types.
attrName = "unrecognized-operation-type-" + o.getOperationType();
}
} else {
attrName = subcommandType.getPrimaryName();
}
if (values.length == 0) {
e.addAttribute(attrName, "");
} else if (subcommandType == null) {
e.addAttribute(attrName, values);
} else {
// those specially. Otherwise, just go with the string representations.
switch(subcommandType) {
case GET_ACCOUNT_USABILITY_NOTICES:
final String[] notices = new String[values.length];
for (int i = 0; i < values.length; i++) {
try {
notices[i] = new PasswordPolicyStateAccountUsabilityNotice(values[i]).getMessage();
} catch (final Exception ex) {
Debug.debugException(ex);
notices[i] = values[i];
}
}
e.addAttribute(attrName, notices);
break;
case GET_ACCOUNT_USABILITY_WARNINGS:
final String[] warnings = new String[values.length];
for (int i = 0; i < values.length; i++) {
try {
warnings[i] = new PasswordPolicyStateAccountUsabilityWarning(values[i]).getMessage();
} catch (final Exception ex) {
Debug.debugException(ex);
warnings[i] = values[i];
}
}
e.addAttribute(attrName, warnings);
break;
case GET_ACCOUNT_USABILITY_ERRORS:
final String[] errors = new String[values.length];
for (int i = 0; i < values.length; i++) {
try {
errors[i] = new PasswordPolicyStateAccountUsabilityError(values[i]).getMessage();
} catch (final Exception ex) {
Debug.debugException(ex);
errors[i] = values[i];
}
}
e.addAttribute(attrName, errors);
break;
default:
e.addAttribute(attrName, values);
break;
}
}
}
return e;
}
Aggregations