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: * SecureConversationTokenTest.java
038: * JUnit based test
039: *
040: * Created on August 24, 2006, 12:27 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.security.policy.SecurityAssertionValidator;
060: import com.sun.xml.ws.policy.sourcemodel.AssertionData;
061: import java.util.Collection;
062: import java.util.Collections;
063: import java.util.HashSet;
064: import java.util.Set;
065: import java.util.logging.Level;
066: import com.sun.xml.ws.policy.Policy;
067: import java.util.ArrayList;
068: import java.util.Iterator;
069: import java.util.List;
070: import java.util.Map;
071: import java.util.UUID;
072: import javax.xml.namespace.QName;
073: import static com.sun.xml.ws.security.impl.policy.Constants.*;
074:
075: /**
076: *
077: * @author Mayank.Mishra@SUN.com
078: */
079: public class SecureConversationTokenTest extends TestCase {
080:
081: public SecureConversationTokenTest(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(
093: SecureConversationTokenTest.class);
094:
095: return suite;
096: }
097:
098: private PolicySourceModel unmarshalPolicyResource(String resource)
099: throws PolicyException, IOException {
100: Reader reader = getResourceReader(resource);
101: PolicySourceModel model = PolicyModelUnmarshaller
102: .getXmlUnmarshaller().unmarshalModel(reader);
103: reader.close();
104: return model;
105: }
106:
107: private Reader getResourceReader(String resourceName) {
108: return new InputStreamReader(Thread.currentThread()
109: .getContextClassLoader().getResourceAsStream(
110: resourceName));
111: }
112:
113: public Policy unmarshalPolicy(String xmlFile) throws Exception {
114: PolicySourceModel model = unmarshalPolicyResource(xmlFile);
115: Policy mbp = PolicyModelTranslator.getTranslator().translate(
116: model);
117: return mbp;
118:
119: }
120:
121: public void testSecureConversationToken1() throws Exception {
122: String fileName = "security/SecureConversationTokenAssertions1.xml";
123: Policy policy = unmarshalPolicy(fileName);
124: Iterator<AssertionSet> itr = policy.iterator();
125: if (itr.hasNext()) {
126: AssertionSet as = itr.next();
127: for (PolicyAssertion assertion : as) {
128: assertEquals("Invalid assertion",
129: "SecureConversationToken", assertion.getName()
130: .getLocalPart());
131: SecureConversationToken sct = (SecureConversationToken) assertion;
132: assertTrue(sct.isRequireDerivedKeys());
133: }
134: } else {
135: throw new Exception(
136: "No Assertions found!. Unmarshalling of "
137: + fileName + " failed!");
138: }
139: }
140:
141: public void testSecureConversationToken2() throws Exception {
142: String fileName = "security/SecureConversationTokenAssertions2.xml";
143: Policy policy = unmarshalPolicy(fileName);
144: Iterator<AssertionSet> itr = policy.iterator();
145: if (itr.hasNext()) {
146: AssertionSet as = itr.next();
147: for (PolicyAssertion assertion : as) {
148: assertEquals("Invalid assertion",
149: "SecureConversationToken", assertion.getName()
150: .getLocalPart());
151: SecureConversationToken sct = (SecureConversationToken) assertion;
152: assertTrue(sct
153: .getTokenRefernceTypes()
154: .contains(
155: com.sun.xml.ws.security.impl.policy.SecureConversationToken.REQUIRE_EXTERNAL_URI_REFERENCE));
156: }
157: } else {
158: throw new Exception(
159: "No Assertions found!. Unmarshalling of "
160: + fileName + " failed!");
161: }
162: }
163:
164: public void testSecureConversationToken3() throws Exception {
165: String fileName = "security/SecureConversationTokenAssertions3.xml";
166: Policy policy = unmarshalPolicy(fileName);
167: Iterator<AssertionSet> itr = policy.iterator();
168: if (itr.hasNext()) {
169: AssertionSet as = itr.next();
170: for (PolicyAssertion assertion : as) {
171: assertEquals("Invalid assertion",
172: "SecureConversationToken", assertion.getName()
173: .getLocalPart());
174: SecureConversationToken sct = (SecureConversationToken) assertion;
175: assertTrue(sct
176: .getTokenType()
177: .equals(
178: com.sun.xml.ws.security.impl.policy.SecureConversationToken.SC10_SECURITYCONTEXT_TOKEN));
179: }
180: } else {
181: throw new Exception(
182: "No Assertions found!. Unmarshalling of "
183: + fileName + " failed!");
184: }
185: }
186:
187: }
|