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: * IssuedTokenTest.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.Policy;
058: import com.sun.xml.ws.policy.PolicyAssertion;
059: import com.sun.xml.ws.policy.PolicyAssertion;
060: import com.sun.xml.ws.policy.sourcemodel.AssertionData;
061: import com.sun.xml.ws.security.policy.Issuer;
062: import com.sun.xml.ws.security.policy.RequestSecurityTokenTemplate;
063: import java.util.ArrayList;
064: import java.util.Collection;
065: import java.util.Iterator;
066: import java.util.List;
067: import java.util.Map;
068: import java.util.Set;
069: import java.util.UUID;
070: import java.util.logging.Level;
071: import javax.xml.namespace.QName;
072: import static com.sun.xml.ws.security.impl.policy.Constants.*;
073: import com.sun.xml.ws.security.policy.SecurityAssertionValidator;
074:
075: /**
076: *
077: * @author Mayank.Mishra@SUN.com
078: */
079: public class IssuedTokenTest extends TestCase {
080:
081: public IssuedTokenTest(String testName) {
082: super (testName);
083: }
084:
085: protected void setUp() throws Exception {
086: }
087:
088: protected void tearDown() throws Exception {
089: }
090:
091: public static Test suite() {
092: TestSuite suite = new TestSuite(IssuedTokenTest.class);
093:
094: return suite;
095: }
096:
097: private PolicySourceModel unmarshalPolicyResource(String resource)
098: throws PolicyException, IOException {
099: Reader reader = getResourceReader(resource);
100: PolicySourceModel model = PolicyModelUnmarshaller
101: .getXmlUnmarshaller().unmarshalModel(reader);
102: reader.close();
103: return model;
104: }
105:
106: private Reader getResourceReader(String resourceName) {
107: return new InputStreamReader(Thread.currentThread()
108: .getContextClassLoader().getResourceAsStream(
109: resourceName));
110: }
111:
112: public Policy unmarshalPolicy(String xmlFile) throws Exception {
113: PolicySourceModel model = unmarshalPolicyResource(xmlFile);
114: Policy mbp = PolicyModelTranslator.getTranslator().translate(
115: model);
116: return mbp;
117:
118: }
119:
120: public void testIssuedTokenAssertions1() throws Exception {
121: String fileName = "security/IssuedTokenAssertions1.xml";
122: Policy policy = unmarshalPolicy(fileName);
123: Iterator<AssertionSet> itr = policy.iterator();
124: if (itr.hasNext()) {
125: AssertionSet as = itr.next();
126: for (PolicyAssertion assertion : as) {
127: assertEquals("Invalid assertion", "IssuedToken",
128: assertion.getName().getLocalPart());
129: IssuedToken it = (IssuedToken) assertion;
130: assertEquals("Invalid Dervied Keys",
131: "RequireDerivedKeys", it.REQUIRE_DERIVED_KEYS);
132: }
133: } else {
134: throw new Exception(
135: "No Assertions found!. Unmarshalling of "
136: + fileName + " failed!");
137: }
138: }
139:
140: public void testIssuedTokenAssertions2() throws Exception {
141: String fileName = "security/IssuedTokenAssertions2.xml";
142: Policy policy = unmarshalPolicy(fileName);
143: String rType = com.sun.xml.ws.security.policy.IssuedToken.REQUIRE_EXTERNAL_REFERENCE;
144: Iterator<AssertionSet> itr = policy.iterator();
145: if (itr.hasNext()) {
146: AssertionSet as = itr.next();
147: for (PolicyAssertion assertion : as) {
148: assertEquals("Invalid assertion", "IssuedToken",
149: assertion.getName().getLocalPart());
150: IssuedToken it = (IssuedToken) assertion;
151: System.out.println(it.getIncludeToken());
152: Iterator itrIt = it.getTokenRefernceType();
153: if (itrIt.hasNext()) {
154: assertTrue(((String) itrIt.next()).equals(rType));
155: }
156: }
157: } else {
158: throw new Exception(
159: "No Assertions found!. Unmarshalling of "
160: + fileName + " failed!");
161: }
162: }
163:
164: public void testIssuedTokenAssertions3() throws Exception {
165: String fileName = "security/IssuedTokenAssertions3.xml";
166: String rType = com.sun.xml.ws.security.policy.IssuedToken.REQUIRE_INTERNAL_REFERENCE;
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", "IssuedToken",
173: assertion.getName().getLocalPart());
174: IssuedToken it = (IssuedToken) assertion;
175: System.out.println(it.getIncludeToken());
176: Iterator itrIt = it.getTokenRefernceType();
177: if (itrIt.hasNext()) {
178: assertTrue(((String) itrIt.next()).equals(rType));
179: }
180: }
181: } else {
182: throw new Exception(
183: "No Assertions found!. Unmarshalling of "
184: + fileName + " failed!");
185: }
186: }
187:
188: public void testIssuedTokenAssertions4() throws Exception {
189: // test for bug
190: //https://wsit.dev.java.net/issues/show_bug.cgi?id=314
191: String fileName = "security/IssuedTokenAssertions_issuer.xml";
192: Policy policy = unmarshalPolicy(fileName);
193: Iterator<AssertionSet> itr = policy.iterator();
194: if (itr.hasNext()) {
195: AssertionSet as = itr.next();
196: for (PolicyAssertion assertion : as) {
197: assertEquals("Invalid assertion", "IssuedToken",
198: assertion.getName().getLocalPart());
199: IssuedToken it = (IssuedToken) assertion;
200: assertNotNull(it.getIssuer().getAddress().getURI());
201: }
202: } else {
203: throw new Exception(
204: "No Assertions found!. Unmarshalling of "
205: + fileName + " failed!");
206: }
207: }
208:
209: }
|