290N/A/*
290N/A * CDDL HEADER START
290N/A *
290N/A * The contents of this file are subject to the terms of the
290N/A * Common Development and Distribution License, Version 1.0 only
290N/A * (the "License"). You may not use this file except in compliance
290N/A * with the License.
290N/A *
6982N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6982N/A * or http://forgerock.org/license/CDDLv1.0.html.
290N/A * See the License for the specific language governing permissions
290N/A * and limitations under the License.
290N/A *
290N/A * When distributing Covered Code, include this CDDL HEADER in each
6982N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6982N/A * If applicable, add the following below this CDDL HEADER, with the
6982N/A * fields enclosed by brackets "[]" replaced with your own identifying
6982N/A * information:
290N/A * Portions Copyright [yyyy] [name of copyright owner]
290N/A *
290N/A * CDDL HEADER END
290N/A *
290N/A *
3232N/A * Copyright 2006-2008 Sun Microsystems, Inc.
5488N/A * Portions copyright 2011 ForgeRock AS.
290N/A */
290N/Apackage org.opends.server.extensions;
290N/A
290N/A
290N/A
2248N/Aimport java.util.ArrayList;
2248N/Aimport java.util.Date;
2248N/Aimport java.util.HashMap;
290N/Aimport java.util.List;
290N/A
290N/Aimport org.testng.annotations.BeforeClass;
290N/Aimport org.testng.annotations.DataProvider;
290N/Aimport org.testng.annotations.Test;
290N/A
290N/Aimport org.opends.server.TestCaseUtils;
2086N/Aimport org.opends.messages.Message;
290N/Aimport org.opends.server.api.AccountStatusNotificationHandler;
5492N/Aimport org.opends.server.api.AuthenticationPolicy;
1242N/Aimport org.opends.server.admin.server.AdminTestCaseUtils;
1242N/Aimport org.opends.server.admin.std.meta.
1242N/A ErrorLogAccountStatusNotificationHandlerCfgDefn;
1242N/Aimport org.opends.server.admin.std.server.
1242N/A ErrorLogAccountStatusNotificationHandlerCfg;
290N/Aimport org.opends.server.config.ConfigEntry;
290N/Aimport org.opends.server.config.ConfigException;
290N/Aimport org.opends.server.core.DirectoryServer;
2248N/Aimport org.opends.server.core.PasswordPolicy;
2248N/Aimport org.opends.server.types.AccountStatusNotification;
2248N/Aimport org.opends.server.types.AccountStatusNotificationProperty;
290N/Aimport org.opends.server.types.AccountStatusNotificationType;
290N/Aimport org.opends.server.types.DN;
290N/Aimport org.opends.server.types.Entry;
338N/Aimport org.opends.server.types.InitializationException;
290N/A
290N/Aimport static org.testng.Assert.*;
290N/A
2248N/Aimport static org.opends.server.types.AccountStatusNotificationType.*;
2248N/Aimport static org.opends.server.types.AccountStatusNotificationProperty.*;
2248N/A
290N/A
290N/A
290N/A/**
290N/A * A set of test cases for the error log account status notification handler.
290N/A */
290N/Apublic class ErrorLogAccountStatusNotificationHandlerTestCase
290N/A extends ExtensionsTestCase
290N/A{
290N/A /**
290N/A * Ensures that the Directory Server is running.
290N/A *
290N/A * @throws Exception If an unexpected problem occurs.
290N/A */
290N/A @BeforeClass()
290N/A public void startServer()
290N/A throws Exception
290N/A {
290N/A TestCaseUtils.startServer();
290N/A }
290N/A
290N/A
290N/A
290N/A /**
290N/A * Retrieves a set of invalid configuration entries that should cause the
290N/A * notification handler initialization to fail.
290N/A *
290N/A * @return A set of invalid configuration entries that should cause the
290N/A * notification handler initialization to fail.
290N/A *
290N/A * @throws Exception If an unexpected problem occurs.
290N/A */
290N/A @DataProvider(name = "invalidConfigs")
290N/A public Object[][] getInvalidConfigs()
290N/A throws Exception
290N/A {
290N/A List<Entry> entries = TestCaseUtils.makeEntries(
290N/A "dn: cn=Error Log Handler,cn=Account Status Notification Handlers," +
290N/A "cn=config",
290N/A "objectClass: top",
290N/A "objectClass: ds-cfg-account-status-notification-handler",
290N/A "objectClass: ds-cfg-error-log-account-status-notification-handler",
290N/A "cn: Error Log Handler",
2624N/A "ds-cfg-java-class: org.opends." +
290N/A "server.extensions.ErrorLogAccountStatusNotificationHandler",
2624N/A "ds-cfg-enabled: true",
290N/A "",
290N/A "dn: cn=Error Log Handler,cn=Account Status Notification Handlers," +
290N/A "cn=config",
290N/A "objectClass: top",
290N/A "objectClass: ds-cfg-account-status-notification-handler",
290N/A "objectClass: ds-cfg-error-log-account-status-notification-handler",
290N/A "cn: Error Log Handler",
2624N/A "ds-cfg-java-class: org.opends." +
290N/A "server.extensions.ErrorLogAccountStatusNotificationHandler",
2624N/A "ds-cfg-enabled: true",
290N/A "ds-cfg-account-status-notification-type: invalid",
290N/A "",
290N/A "dn: cn=Error Log Handler,cn=Account Status Notification Handlers," +
290N/A "cn=config",
290N/A "objectClass: top",
290N/A "objectClass: ds-cfg-account-status-notification-handler",
290N/A "objectClass: ds-cfg-error-log-account-status-notification-handler",
290N/A "cn: Error Log Handler",
2624N/A "ds-cfg-java-class: org.opends." +
290N/A "server.extensions.ErrorLogAccountStatusNotificationHandler",
2624N/A "ds-cfg-enabled: true",
290N/A "ds-cfg-account-status-notification-type: password-reset",
290N/A "ds-cfg-account-status-notification-type: invalid");
290N/A
290N/A
290N/A Object[][] configEntries = new Object[entries.size()][1];
290N/A for (int i=0; i < configEntries.length; i++)
290N/A {
290N/A configEntries[i] = new Object[] { entries.get(i) };
290N/A }
290N/A
290N/A return configEntries;
290N/A }
290N/A
290N/A
290N/A
290N/A /**
290N/A * Tests to ensure that the notification handler initialization fails with an
290N/A * invalid configuration.
290N/A *
290N/A * @param e The configuration entry to use to initialize the account status
290N/A * notificaton handler.
290N/A *
290N/A * @throws Exception If an unexpected problem occurs.
290N/A */
290N/A @Test(dataProvider = "invalidConfigs",
290N/A expectedExceptions = { ConfigException.class,
290N/A InitializationException.class })
290N/A public void testInalidConfigs(Entry e)
290N/A throws Exception
290N/A {
290N/A DN parentDN =
290N/A DN.decode("cn=Account Status Notification Handlers,cn=config");
290N/A ConfigEntry parentEntry = DirectoryServer.getConfigEntry(parentDN);
290N/A ConfigEntry configEntry = new ConfigEntry(e, parentEntry);
290N/A
290N/A ErrorLogAccountStatusNotificationHandler handler =
290N/A new ErrorLogAccountStatusNotificationHandler();
1242N/A ErrorLogAccountStatusNotificationHandlerCfg configuration =
1242N/A AdminTestCaseUtils.getConfiguration(
1242N/A ErrorLogAccountStatusNotificationHandlerCfgDefn.getInstance(),
1242N/A configEntry.getEntry()
1242N/A );
1242N/A handler.initializeStatusNotificationHandler(configuration);
290N/A }
290N/A
290N/A
290N/A
290N/A /**
290N/A * Tests to ensure that the error log account status notification handler is
290N/A * configured and enabled within the Directory Server.
290N/A *
290N/A * @throws Exception If an unexpected problem occurs.
290N/A */
290N/A @Test()
290N/A public void testHandlerEnabled()
290N/A throws Exception
290N/A {
290N/A String dnStr = "cn=Error Log Handler,cn=Account Status Notification " +
290N/A "Handlers,cn=config";
290N/A DN handlerDN = DN.decode(dnStr);
5492N/A AccountStatusNotificationHandler<?> handler =
290N/A DirectoryServer.getAccountStatusNotificationHandler(handlerDN);
290N/A assertNotNull(handler);
290N/A assertTrue(handler instanceof ErrorLogAccountStatusNotificationHandler);
290N/A }
290N/A
290N/A
290N/A
290N/A /**
290N/A * Retrieves the set of valid notification types that may be used with an
290N/A * account status notification handler.
290N/A *
290N/A * @return The set of valid notification types that may be used with an
290N/A * account status notification handler.
290N/A */
290N/A @DataProvider(name = "notificationTypes")
290N/A public Object[][] getNotificationTypes()
290N/A {
290N/A AccountStatusNotificationType[] notificationTypes =
290N/A AccountStatusNotificationType.values();
290N/A
290N/A Object[][] typeArray = new Object[notificationTypes.length][1];
290N/A for (int i=0; i < notificationTypes.length; i++)
290N/A {
290N/A typeArray[i] = new Object[] { notificationTypes[i] };
290N/A }
290N/A
290N/A return typeArray;
290N/A }
290N/A
290N/A
290N/A
290N/A /**
290N/A * Tests the <CODE>handleStatusNotification</CODE> method.
290N/A *
2248N/A * @param notificationType The account status notification type to be
2248N/A * tested.
2248N/A *
290N/A * @throws Exception If an unexpected problem occurs.
290N/A */
290N/A @Test(dataProvider = "notificationTypes")
290N/A public void testHandleStatusNotification(
290N/A AccountStatusNotificationType notificationType)
290N/A throws Exception
290N/A {
2248N/A TestCaseUtils.initializeTestBackend(true);
2248N/A TestCaseUtils.addEntry(
2248N/A "dn: uid=test.user,o=test",
2248N/A "objectClass: top",
2248N/A "objectClass: person",
2248N/A "objectClass: organizationalPerson",
2248N/A "objectClass: inetOrgPerson",
2248N/A "uid: test.user",
2248N/A "givenName: Test",
2248N/A "sn: User",
2248N/A "cn: Test User",
2248N/A "userPassword: password");
2248N/A
290N/A String dnStr = "cn=Error Log Handler,cn=Account Status Notification " +
290N/A "Handlers,cn=config";
290N/A DN handlerDN = DN.decode(dnStr);
5492N/A AccountStatusNotificationHandler<?> handler =
290N/A DirectoryServer.getAccountStatusNotificationHandler(handlerDN);
290N/A assertNotNull(handler);
290N/A
2248N/A Entry userEntry =
2248N/A DirectoryServer.getEntry(DN.decode("uid=test.user,o=test"));
2248N/A
5492N/A PasswordPolicy policy = (PasswordPolicy) AuthenticationPolicy.forUser(
5492N/A userEntry, false);
2248N/A
2248N/A HashMap<AccountStatusNotificationProperty,List<String>>
2248N/A notificationProperties =
2248N/A new HashMap<AccountStatusNotificationProperty,List<String>>();
2248N/A
2248N/A ArrayList<String> propList = new ArrayList<String>(1);
5488N/A propList.add(policy.getDN().toString());
2248N/A notificationProperties.put(PASSWORD_POLICY_DN, propList);
2248N/A
2248N/A
2248N/A if (notificationType == ACCOUNT_TEMPORARILY_LOCKED)
2248N/A {
2248N/A propList = new ArrayList<String>(1);
2248N/A propList.add("300");
2248N/A notificationProperties.put(SECONDS_UNTIL_UNLOCK, propList);
2248N/A
2248N/A propList = new ArrayList<String>(1);
2248N/A propList.add("5 minutes");
2248N/A notificationProperties.put(TIME_UNTIL_UNLOCK, propList);
2248N/A
2248N/A propList = new ArrayList<String>(1);
2248N/A propList.add(new Date(System.currentTimeMillis() + 300000L).toString());
2248N/A notificationProperties.put(ACCOUNT_UNLOCK_TIME, propList);
2248N/A }
2248N/A else if (notificationType == PASSWORD_EXPIRING)
2248N/A {
2248N/A propList = new ArrayList<String>(1);
2248N/A propList.add("86400");
2248N/A notificationProperties.put(SECONDS_UNTIL_EXPIRATION, propList);
2248N/A
2248N/A propList = new ArrayList<String>(1);
2248N/A propList.add("1 day");
2248N/A notificationProperties.put(TIME_UNTIL_EXPIRATION, propList);
2248N/A
2248N/A propList = new ArrayList<String>(1);
2248N/A propList.add(new Date(System.currentTimeMillis() + 86400000L).toString());
2248N/A notificationProperties.put(PASSWORD_EXPIRATION_TIME, propList);
2248N/A }
2248N/A else if ((notificationType == PASSWORD_CHANGED) ||
2248N/A (notificationType == PASSWORD_RESET))
2248N/A {
2248N/A propList = new ArrayList<String>(1);
2248N/A propList.add("oldpassword");
2248N/A notificationProperties.put(OLD_PASSWORD, propList);
2248N/A
2248N/A propList = new ArrayList<String>(1);
2248N/A propList.add("newpassword");
2248N/A notificationProperties.put(NEW_PASSWORD, propList);
2248N/A }
2248N/A
2248N/A
2248N/A AccountStatusNotification notification =
2248N/A new AccountStatusNotification(notificationType, userEntry,
2248N/A Message.raw("Test Modification"),
2248N/A notificationProperties);
2248N/A handler.handleStatusNotification(notification);
290N/A }
290N/A}
290N/A