001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: /*
037: * KerberosTokenTest.java
038: * JUnit based test
039: *
040: * Created on August 24, 2006, 12:26 AM
041: */
042:
043: package com.sun.xml.ws.security.impl.policy;
044:
045: import com.sun.xml.ws.policy.Policy;
046: import com.sun.xml.ws.policy.PolicyException;
047: import com.sun.xml.ws.policy.sourcemodel.PolicyModelTranslator;
048: import com.sun.xml.ws.policy.sourcemodel.PolicyModelUnmarshaller;
049: import com.sun.xml.ws.policy.sourcemodel.PolicySourceModel;
050: import java.io.IOException;
051: import java.io.InputStreamReader;
052: import java.io.Reader;
053:
054: import junit.framework.*;
055: import com.sun.xml.ws.policy.AssertionSet;
056: import com.sun.xml.ws.policy.NestedPolicy;
057: import com.sun.xml.ws.policy.PolicyAssertion;
058: import com.sun.xml.ws.policy.sourcemodel.AssertionData;
059: import com.sun.xml.ws.security.policy.SecurityAssertionValidator;
060: import java.util.ArrayList;
061: import java.util.Collection;
062: import java.util.Collections;
063: import java.util.Iterator;
064: import java.util.List;
065: import java.util.UUID;
066: import java.util.logging.Level;
067: import javax.xml.namespace.QName;
068: import static com.sun.xml.ws.security.impl.policy.Constants.*;
069:
070: /**
071: *
072: * @author Mayank.Mishra@SUN.com
073: */
074: public class KerberosTokenTest extends TestCase {
075:
076: public KerberosTokenTest(String testName) {
077: super (testName);
078: }
079:
080: protected void setUp() throws Exception {
081: }
082:
083: protected void tearDown() throws Exception {
084: }
085:
086: public static Test suite() {
087: TestSuite suite = new TestSuite(KerberosTokenTest.class);
088:
089: return suite;
090: }
091:
092: private PolicySourceModel unmarshalPolicyResource(String resource)
093: throws PolicyException, IOException {
094: Reader reader = getResourceReader(resource);
095: PolicySourceModel model = PolicyModelUnmarshaller
096: .getXmlUnmarshaller().unmarshalModel(reader);
097: reader.close();
098: return model;
099: }
100:
101: private Reader getResourceReader(String resourceName) {
102: return new InputStreamReader(Thread.currentThread()
103: .getContextClassLoader().getResourceAsStream(
104: resourceName));
105: }
106:
107: public Policy unmarshalPolicy(String xmlFile) throws Exception {
108: PolicySourceModel model = unmarshalPolicyResource(xmlFile);
109: Policy mbp = PolicyModelTranslator.getTranslator().translate(
110: model);
111: return mbp;
112:
113: }
114:
115: public void testKerberosTokenAssertions() throws Exception {
116: testKerberosTokenType(
117: "security/KerberosTokenAssertions1.xml",
118: com.sun.xml.ws.security.impl.policy.KerberosToken.WSSKERBEROS_V5_AP_REQ_TOKEN11);
119: testKerberosTokenType(
120: "security/KerberosTokenAssertions2.xml",
121: com.sun.xml.ws.security.impl.policy.KerberosToken.WSSKERBEROS_GSS_V5_AP_REQ_TOKEN11);
122: testKerberosTokenKeys();
123: testKerberosTokenReference();
124: }
125:
126: public void testKerberosTokenType(String fileName, String TokenType)
127: throws Exception {
128:
129: Policy policy = unmarshalPolicy(fileName);
130: Iterator<AssertionSet> itr = policy.iterator();
131: if (itr.hasNext()) {
132: AssertionSet as = itr.next();
133: for (PolicyAssertion assertion : as) {
134: assertEquals("Invalid assertion", "KerberosToken",
135: assertion.getName().getLocalPart());
136: KerberosToken kt = (KerberosToken) assertion;
137: assertTrue(TokenType.equals(kt.getTokenType()));
138: }
139: } else {
140: throw new Exception(
141: "No Assertions found!. Unmarshalling of "
142: + fileName + " failed!");
143: }
144: }
145:
146: public void testKerberosTokenKeys() throws Exception {
147: String fileName = "security/KerberosTokenAssertions3.xml";
148: Policy policy = unmarshalPolicy(fileName);
149: Iterator<AssertionSet> itr = policy.iterator();
150: if (itr.hasNext()) {
151: AssertionSet as = itr.next();
152: for (PolicyAssertion assertion : as) {
153: assertEquals("Invalid assertion", "KerberosToken",
154: assertion.getName().getLocalPart());
155: KerberosToken kt = (KerberosToken) assertion;
156: assertTrue(kt.isRequireDerivedKeys());
157: }
158: } else {
159: throw new Exception(
160: "No Assertions found!. Unmarshalling of "
161: + fileName + " failed!");
162: }
163: }
164:
165: public void testKerberosTokenReference() throws Exception {
166: String fileName = "security/KerberosTokenAssertions4.xml";
167: Policy policy = unmarshalPolicy(fileName);
168: Iterator<AssertionSet> itr = policy.iterator();
169: if (itr.hasNext()) {
170: AssertionSet as = itr.next();
171: for (PolicyAssertion assertion : as) {
172: assertEquals("Invalid assertion", "KerberosToken",
173: assertion.getName().getLocalPart());
174: KerberosToken kt = (KerberosToken) assertion;
175: Iterator itrkt = kt.getTokenRefernceType();
176: if (itr.hasNext()) {
177: assertTrue(((String) itrkt.next())
178: .equals(com.sun.xml.ws.security.impl.policy.KerberosToken.REQUIRE_KEY_IDENTIFIER_REFERENCE));
179: }
180: }
181: } else {
182: throw new Exception(
183: "No Assertions found!. Unmarshalling of "
184: + fileName + " failed!");
185: }
186: }
187:
188: }
|