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: * SymmetricBindingTest.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 com.sun.xml.ws.security.policy.AlgorithmSuiteValue;
051: import java.io.IOException;
052: import java.io.InputStreamReader;
053: import java.io.Reader;
054: import java.util.Iterator;
055:
056: import junit.framework.*;
057: import com.sun.xml.ws.policy.AssertionSet;
058: import com.sun.xml.ws.policy.NestedPolicy;
059: import com.sun.xml.ws.policy.Policy;
060: import com.sun.xml.ws.policy.PolicyAssertion;
061: import com.sun.xml.ws.policy.sourcemodel.AssertionData;
062: import com.sun.xml.ws.security.policy.AlgorithmSuite;
063: import com.sun.xml.ws.security.policy.Binding;
064: import com.sun.xml.ws.security.policy.MessageLayout;
065: import com.sun.xml.ws.security.policy.Token;
066: import com.sun.xml.ws.security.policy.SecurityAssertionValidator;
067: import java.util.Collection;
068: import java.util.Iterator;
069: import java.util.List;
070: import java.util.Map;
071: import java.util.logging.Level;
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 SymmetricBindingTest extends TestCase {
080:
081: public SymmetricBindingTest(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(SymmetricBindingTest.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: ClassLoader cl = Thread.currentThread().getContextClassLoader();
108: if (cl == null) {
109: return new InputStreamReader(ClassLoader
110: .getSystemResourceAsStream(resourceName));
111: } else {
112: return new InputStreamReader(cl
113: .getResourceAsStream(resourceName));
114: }
115:
116: }
117:
118: public Policy unmarshalPolicy(String xmlFile) throws Exception {
119: PolicySourceModel model = unmarshalPolicyResource(xmlFile);
120: Policy mbp = PolicyModelTranslator.getTranslator().translate(
121: model);
122: return mbp;
123:
124: }
125:
126: public void testSymmerticBinding1() throws Exception {
127: String fileName = "security/SymmetricBindingAssertion1.xml";
128: Policy policy = unmarshalPolicy(fileName);
129: Iterator<AssertionSet> itr = policy.iterator();
130: if (itr.hasNext()) {
131: AssertionSet as = itr.next();
132: for (PolicyAssertion assertion : as) {
133: assertEquals("Invalid assertion", "SymmetricBinding",
134: assertion.getName().getLocalPart());
135: SymmetricBinding sb = (SymmetricBinding) assertion;
136:
137: // System.out.println((sb.getProtectionToken().getIncludeToken()));
138: assertTrue(sb.getTokenProtection());
139:
140: AlgorithmSuite aSuite = sb.getAlgorithmSuite();
141: assertEquals("Unmatched Algorithm", aSuite
142: .getEncryptionAlgorithm(),
143: AlgorithmSuiteValue.Basic128.getEncAlgorithm());
144:
145: assertTrue(sb.isIncludeTimeStamp());
146:
147: assertFalse("Signature is Encrypted", sb
148: .getSignatureProtection());
149:
150: assertFalse(sb.isSignContent());
151: }
152: }
153: }
154:
155: public void testSymmerticBinding2() throws Exception {
156: String fileName = "security/SymmetricBindingAssertion2.xml";
157: Policy policy = unmarshalPolicy(fileName);
158: Iterator<AssertionSet> itr = policy.iterator();
159: if (itr.hasNext()) {
160: AssertionSet as = itr.next();
161: for (PolicyAssertion assertion : as) {
162: assertEquals("Invalid assertion", "SymmetricBinding",
163: assertion.getName().getLocalPart());
164: SymmetricBinding sb = (SymmetricBinding) assertion;
165:
166: X509Token tkn1 = (X509Token) sb.getSignatureToken();
167: assertTrue(tkn1
168: .getTokenType()
169: .equals(
170: com.sun.xml.ws.security.impl.policy.X509Token.WSSX509V1TOKEN10));
171:
172: X509Token tkn2 = (X509Token) sb.getEncryptionToken();
173: assertTrue(tkn2
174: .getTokenType()
175: .equals(
176: com.sun.xml.ws.security.impl.policy.X509Token.WSSX509V3TOKEN10));
177:
178: AlgorithmSuite aSuite = sb.getAlgorithmSuite();
179: assertEquals("Unmatched Algorithm", aSuite
180: .getEncryptionAlgorithm(),
181: AlgorithmSuiteValue.TripleDesRsa15
182: .getEncAlgorithm());
183:
184: assertTrue(sb.isIncludeTimeStamp());
185:
186: assertTrue("Signature is not Encypted", sb
187: .getSignatureProtection());
188:
189: assertFalse("Tokens are protected", sb
190: .getTokenProtection());
191: }
192: }
193: }
194:
195: public void testSymmetricIssuedTokenCR6419493() throws Exception {
196: String fileName = "security/IssuedTokenCR.xml";
197: Policy policy = unmarshalPolicy(fileName);
198: Iterator<AssertionSet> itr = policy.iterator();
199: if (itr.hasNext()) {
200: AssertionSet as = itr.next();
201: for (PolicyAssertion assertion : as) {
202: assertEquals("Invalid assertion", "SymmetricBinding",
203: assertion.getName().getLocalPart());
204: SymmetricBinding sb = (SymmetricBinding) assertion;
205:
206: IssuedToken tkn1 = (IssuedToken) sb
207: .getProtectionToken();
208: assertTrue(tkn1.getIncludeToken().equals(
209: Token.INCLUDE_ALWAYS));
210: }
211: }
212: }
213:
214: }
|