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: * AsymmetricBindingTest.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 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.MessageLayout;
064: import com.sun.xml.ws.security.policy.Token;
065: import com.sun.xml.ws.security.policy.SecurityAssertionValidator;
066: import java.util.Collection;
067: import java.util.Iterator;
068: import java.util.Map;
069: import java.util.logging.Level;
070: import java.util.logging.Logger;
071: import javax.xml.namespace.QName;
072: import static com.sun.xml.ws.security.impl.policy.Constants.*;
073:
074: /**
075: *
076: * @author Mayank.Mishra@SUN.com
077: */
078: public class AsymmetricBindingTest extends TestCase {
079:
080: public AsymmetricBindingTest(String testName) {
081: super (testName);
082: }
083:
084: protected void setUp() throws Exception {
085: }
086:
087: protected void tearDown() throws Exception {
088: }
089:
090: public static Test suite() {
091: TestSuite suite = new TestSuite(AsymmetricBindingTest.class);
092:
093: return suite;
094: }
095:
096: private PolicySourceModel unmarshalPolicyResource(String resource)
097: throws PolicyException, IOException {
098: Reader reader = getResourceReader(resource);
099: PolicySourceModel model = PolicyModelUnmarshaller
100: .getXmlUnmarshaller().unmarshalModel(reader);
101: reader.close();
102: return model;
103: }
104:
105: private Reader getResourceReader(String resourceName) {
106: return new InputStreamReader(Thread.currentThread()
107: .getContextClassLoader().getResourceAsStream(
108: resourceName));
109: }
110:
111: public Policy unmarshalPolicy(String xmlFile) throws Exception {
112: PolicySourceModel model = unmarshalPolicyResource(xmlFile);
113: Policy mbp = PolicyModelTranslator.getTranslator().translate(
114: model);
115: return mbp;
116:
117: }
118:
119: public void testAsymmerticBinding1() throws Exception {
120: String fileName = "security/AsymmetricBindingAssertion1.xml";
121: Policy policy = unmarshalPolicy(fileName);
122: Iterator<AssertionSet> itr = policy.iterator();
123: if (itr.hasNext()) {
124: AssertionSet as = itr.next();
125: for (PolicyAssertion assertion : as) {
126: assertEquals("Invalid assertion", "AsymmetricBinding",
127: assertion.getName().getLocalPart());
128: AsymmetricBinding asb = (AsymmetricBinding) assertion;
129:
130: X509Token tkn1 = (X509Token) asb.getInitiatorToken();
131: assertTrue(tkn1
132: .getTokenType()
133: .equals(
134: com.sun.xml.ws.security.impl.policy.X509Token.WSSX509V1TOKEN10));
135:
136: X509Token tkn2 = (X509Token) asb.getRecipientToken();
137: assertTrue(tkn2
138: .getTokenType()
139: .equals(
140: com.sun.xml.ws.security.impl.policy.X509Token.WSSX509V3TOKEN11));
141:
142: assertFalse("Tokens are Protected", asb
143: .getTokenProtection());
144:
145: AlgorithmSuite aSuite = asb.getAlgorithmSuite();
146: assertEquals("Unmatched Algorithm", aSuite
147: .getEncryptionAlgorithm(),
148: AlgorithmSuiteValue.TripleDesRsa15
149: .getEncAlgorithm());
150:
151: assertTrue(asb.isIncludeTimeStamp());
152:
153: assertFalse("Signature is Encrypted", asb
154: .getSignatureProtection());
155:
156: assertFalse(asb.isSignContent());
157: }
158: }
159: }
160:
161: public void testAsymmerticBinding2() throws Exception {
162: String fileName = "security/AsymmetricBindingAssertion2.xml";
163: Policy policy = unmarshalPolicy(fileName);
164: Iterator<AssertionSet> itr = policy.iterator();
165: if (itr.hasNext()) {
166: AssertionSet as = itr.next();
167: for (PolicyAssertion assertion : as) {
168: assertEquals("Invalid assertion", "AsymmetricBinding",
169: assertion.getName().getLocalPart());
170: AsymmetricBinding asb = (AsymmetricBinding) assertion;
171:
172: X509Token tkn1 = (X509Token) asb.getInitiatorToken();
173: assertTrue(tkn1
174: .getTokenType()
175: .equals(
176: com.sun.xml.ws.security.impl.policy.X509Token.WSSX509V1TOKEN10));
177:
178: X509Token tkn2 = (X509Token) asb.getRecipientToken();
179: assertTrue(tkn2
180: .getTokenType()
181: .equals(
182: com.sun.xml.ws.security.impl.policy.X509Token.WSSX509V3TOKEN11));
183:
184: AlgorithmSuite aSuite = asb.getAlgorithmSuite();
185: assertEquals("Unmatched Algorithm", aSuite
186: .getEncryptionAlgorithm(),
187: AlgorithmSuiteValue.TripleDesRsa15
188: .getEncAlgorithm());
189:
190: assertTrue(asb.isIncludeTimeStamp());
191:
192: assertTrue("Signature is not Encrypted", asb
193: .getSignatureProtection());
194:
195: assertTrue("Tokens are not protected", asb
196: .getTokenProtection());
197:
198: assertEquals(
199: "SIGN_ENCRYPT is the protection order",
200: com.sun.xml.ws.security.impl.policy.AsymmetricBinding.ENCRYPT_SIGN,
201: asb.getProtectionOrder());
202: }
203: }
204: }
205:
206: public void testAsymmerticCR6420594() throws Exception {
207: String fileName = "security/AsymmetricCR.xml";
208: Policy policy = unmarshalPolicy(fileName);
209: Iterator<AssertionSet> itr = policy.iterator();
210: if (itr.hasNext()) {
211: AssertionSet as = itr.next();
212: for (PolicyAssertion assertion : as) {
213: assertEquals("Invalid assertion", "AsymmetricBinding",
214: assertion.getName().getLocalPart());
215: AsymmetricBinding asb = (AsymmetricBinding) assertion;
216:
217: X509Token tkn1 = (X509Token) asb.getInitiatorToken();
218: assertTrue(tkn1.getIncludeToken().equals(
219: Token.INCLUDE_ALWAYS));
220: assertTrue(tkn1
221: .getTokenType()
222: .equals(
223: com.sun.xml.ws.security.impl.policy.X509Token.WSSX509V3TOKEN10));
224:
225: X509Token tkn2 = (X509Token) asb.getRecipientToken();
226: assertTrue(tkn2.getIncludeToken().equals(
227: Token.INCLUDE_ALWAYS));
228: assertTrue(tkn2
229: .getTokenType()
230: .equals(
231: com.sun.xml.ws.security.impl.policy.X509Token.WSSX509V3TOKEN10));
232: }
233: }
234: }
235:
236: }
|