TestAttributeEvaluator.java revision d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster/**
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Copyright (c) 2008 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: TestAttributeEvaluator.java,v 1.2 2009/11/12 18:37:40 veiming Exp $
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell *
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell * Portions Copyrighted 2014 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.PolicyPrivilegeManager;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.opensso.SubjectUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.util.AuthUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.util.IdRepoUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.idm.AMIdentity;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.security.AdminTokenAction;
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 Fosterimport javax.security.auth.Subject;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.testng.annotations.AfterClass;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.testng.annotations.BeforeClass;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.testng.annotations.Test;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterpublic class TestAttributeEvaluator {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String APPL_NAME = "TestEvaluatorAppl";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String PRIVILEGE1_NAME = "entitlementPrivilege1";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String USER1_NAME = "privilegeEvalTestUser1";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String URL1 = "http://www.testevaluator.com:80/private";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private SSOToken adminToken = (SSOToken) 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 private AMIdentity user1;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private String attrName = "mail";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private String attrValue = "u1@sun.com";
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 Application appl = new Application("/", APPL_NAME,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationTypeManager.getAppplicationType(adminSubject,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationTypeManager.URL_APPLICATION_TYPE_NAME));
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> avaliableResources = new HashSet<String>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster avaliableResources.add("http://www.testevaluator.com:80/*");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster appl.addResources(avaliableResources);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster appl.setEntitlementCombiner(DenyOverride.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationManager.saveApplication(adminSubject, "/", appl);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster PrivilegeManager pm = new PolicyPrivilegeManager();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster pm.initialize("/", adminSubject);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Boolean> actions = new HashMap<String, Boolean>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster actions.put("GET", Boolean.TRUE);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Entitlement ent = new Entitlement(APPL_NAME, URL1, actions);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Set<String>> attrValues =new
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster HashMap<String, Set<String>>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> set = new HashSet<String>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster set.add(attrValue);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster attrValues.put(attrName, set);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster user1 = IdRepoUtils.createUser("/", USER1_NAME, attrValues);
f3a51ed2101fb9ce9e603c00544f73e2f1c854aaRich Riley AttributeSubject as = new AttributeSubject(attrName, attrValue);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster EntitlementSubject es1 = as;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Privilege privilege = Privilege.getNewInstance();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege.setName(PRIVILEGE1_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege.setEntitlement(ent);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege.setSubject(es1);
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell pm.add(privilege);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @AfterClass
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void cleanup() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!migrated) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster PrivilegeManager pm = new PolicyPrivilegeManager();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster pm.initialize("/", SubjectUtils.createSubject(adminToken));
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell pm.remove(PRIVILEGE1_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster IdRepoUtils.deleteIdentity("/", user1);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster ApplicationManager.deleteApplication(adminSubject, "/", APPL_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void postiveTest()
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!migrated) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!evaluate(URL1)) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("TestEvaluator.postiveTest failed");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private boolean evaluate(String res)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throws EntitlementException {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Subject subject = AuthUtils.createSubject(user1.getUniversalId());
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set actions = new HashSet();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster actions.add("GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Evaluator evaluator = new Evaluator(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster SubjectUtils.createSubject(adminToken),
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster APPL_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return evaluator.hasEntitlement("/", subject,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster new Entitlement(res, actions), Collections.EMPTY_MAP);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster}