a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington/*
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * The contents of this file are subject to the terms
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * of the Common Development and Distribution License
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * (the License). You may not use this file except in
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * compliance with the License.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * You can obtain a copy of the License at
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * opensso/legal/CDDLv1.0.txt
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * See the License for the specific language governing
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * permission and limitations under the License.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * When distributing Covered Code, include this CDDL
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Header Notice in each file and include the License file
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * at opensso/legal/CDDLv1.0.txt.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * If applicable, add the following below the CDDL Header,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * with the fields enclosed by brackets [] replaced by
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * your own identifying information:
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * $Id: ReferredResourcesTest.java,v 1.2 2009/11/05 21:13:46 veiming Exp $
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell *
a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington * Portions Copyrighted 2014-2015 ForgeRock AS.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster */
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterpackage com.sun.identity.entitlement;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.iplanet.sso.SSOToken;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.opensso.OpenSSOIndexStore;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.opensso.SubjectUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.policy.PolicyManager;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.security.AdminTokenAction;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.sm.OrganizationConfigManager;
8f8f8324cd7eb3d7d1d6b446be35096b9eff5f58Andrew Forrestimport org.testng.annotations.AfterClass;
8f8f8324cd7eb3d7d1d6b446be35096b9eff5f58Andrew Forrestimport org.testng.annotations.BeforeClass;
8f8f8324cd7eb3d7d1d6b446be35096b9eff5f58Andrew Forrestimport org.testng.annotations.Test;
8f8f8324cd7eb3d7d1d6b446be35096b9eff5f58Andrew Forrest
8f8f8324cd7eb3d7d1d6b446be35096b9eff5f58Andrew Forrestimport javax.security.auth.Subject;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.security.AccessController;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.Collections;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.HashMap;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.HashSet;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.Map;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.Set;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterpublic class ReferredResourcesTest {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String APPL_NAME = "ReferredResourcesTestAppl";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String REFERRAL_NAME1 = "ReferredResourcesTestR1";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String REFERRAL_NAME2 = "ReferredResourcesTestR2";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String SUB_REALM1 = "/ReferredResourcesTest1";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String SUB_REALM2 = "/ReferredResourcesTest2";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String SUB_REALM3 = "/ReferredResourcesTest3";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private SSOToken adminToken = (SSOToken)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster AccessController.doPrivileged(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster AdminTokenAction.getInstance());
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private Subject adminSubject = SubjectUtils.createSubject(adminToken);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private boolean migrated = EntitlementConfiguration.getInstance(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster adminSubject, "/").migratedToEntitlementService();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @BeforeClass
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void setup() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!migrated) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster createOrgs();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster createAppl(adminSubject);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster createReferral1(adminSubject);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster createReferral2(adminSubject);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private void createOrgs() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster OrganizationConfigManager ocm = new OrganizationConfigManager(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster adminToken, "/");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String subRealm = SUB_REALM1.substring(1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Map<String, Set<String>>> map = new
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster HashMap<String, Map<String, Set<String>>>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Set<String>> idRepoService = new
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster HashMap<String, Set<String>>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> set = new HashSet<String>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster set.add("a.com");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster idRepoService.put(PolicyManager.ORG_ALIAS, set);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster map.put(PolicyManager.ID_REPO_SERVICE, idRepoService);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ocm.createSubOrganization(subRealm, map);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster subRealm = SUB_REALM2.substring(1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ocm.createSubOrganization(subRealm, Collections.EMPTY_MAP);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster subRealm = SUB_REALM3.substring(1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ocm.createSubOrganization(subRealm, Collections.EMPTY_MAP);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private void createAppl(Subject adminSubject) throws EntitlementException,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster InstantiationException, IllegalAccessException {
1350b637af96e2390d247918800c02a7f531b47dJaco Jooste Application appl = new Application(APPL_NAME,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationTypeManager.getAppplicationType(adminSubject,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationTypeManager.URL_APPLICATION_TYPE_NAME));
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest // Test disabled, unable to fix model change
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest // Set<String> appResources = new HashSet<String>();
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest // appResources.add("http://www.ReferredResourcesTest.com/*");
74dca04245920444925c2544c591c3da5dad607eAndrew Forrest // appl.addResources(appResources);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster appl.setEntitlementCombiner(DenyOverride.class);
ba3008548cd047b233fcd32bb3c5d69926eed22fAndrew Forrest ApplicationManager.saveApplication(adminSubject, "/", appl);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private void createReferral1(Subject adminSubject)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throws EntitlementException {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Set<String>> map = new HashMap<String, Set<String>>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> set = new HashSet<String>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster set.add("http://www.ReferredResourcesTest.com/1/");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster map.put(APPL_NAME, set);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> realms = new HashSet<String>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster realms.add(SUB_REALM1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster realms.add(SUB_REALM2);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ReferralPrivilege r1 = new ReferralPrivilege(REFERRAL_NAME1,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster map, realms);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private void createReferral2(Subject adminSubject)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throws EntitlementException {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Set<String>> map = new HashMap<String, Set<String>>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> set = new HashSet<String>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster set.add("http://www.ReferredResourcesTest.com/1/2");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster map.put(APPL_NAME, set);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> realms = new HashSet<String>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster realms.add(SUB_REALM1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ReferralPrivilege r1 = new ReferralPrivilege(REFERRAL_NAME2, map,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster realms);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @AfterClass
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void cleanup() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!migrated) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
ba3008548cd047b233fcd32bb3c5d69926eed22fAndrew Forrest ApplicationManager.deleteApplication(adminSubject, "/", APPL_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster OrganizationConfigManager ocm = new OrganizationConfigManager(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster adminToken, "/");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String subRealm = SUB_REALM1.substring(1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ocm.deleteSubOrganization(subRealm, true);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster subRealm = SUB_REALM2.substring(1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ocm.deleteSubOrganization(subRealm, true);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster subRealm = SUB_REALM3.substring(1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ocm.deleteSubOrganization(subRealm, true);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void test() throws EntitlementException, Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!migrated) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
ba3008548cd047b233fcd32bb3c5d69926eed22fAndrew Forrest Set<String> resources = ApplicationManager.getReferredResources(
ba3008548cd047b233fcd32bb3c5d69926eed22fAndrew Forrest PrivilegeManager.superAdminSubject, SUB_REALM1,
ba3008548cd047b233fcd32bb3c5d69926eed22fAndrew Forrest ApplicationTypeManager.URL_APPLICATION_TYPE_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (OpenSSOIndexStore.isOrgAliasMappingResourceEnabled(adminToken)) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (resources.size() != 4) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "ReferredResourcesTest.test: failed incorrect number of" +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "resources");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster } else {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (resources.size() != 2) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "ReferredResourcesTest.test: failed incorrect number of" +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "resources");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!resources.contains("http://www.ReferredResourcesTest.com/1/")) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("ReferredResourcesTest.test: referred from parent realm failed");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!resources.contains("http://www.ReferredResourcesTest.com/1/2")) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("ReferredResourcesTest.test: referred from peer realm failed");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster}