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: * SignedPartsTest.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: import junit.framework.*;
054: import com.sun.xml.ws.policy.AssertionSet;
055: import com.sun.xml.ws.policy.Policy;
056: import com.sun.xml.ws.policy.PolicyAssertion;
057: import com.sun.xml.ws.policy.sourcemodel.AssertionData;
058: import com.sun.xml.ws.security.policy.SecurityAssertionValidator;
059: import com.sun.xml.ws.security.policy.Header;
060: import java.util.ArrayList;
061: import java.util.Collection;
062: import java.util.Collections;
063: import java.util.HashSet;
064: import java.util.Iterator;
065: import java.util.Map;
066: import java.util.Set;
067: import javax.xml.namespace.QName;
068:
069: /**
070: *
071: * @author Mayank.Mishra@SUN.com
072: */
073: public class SignedPartsTest extends TestCase {
074:
075: public SignedPartsTest(String testName) {
076: super (testName);
077: }
078:
079: protected void setUp() throws Exception {
080: }
081:
082: protected void tearDown() throws Exception {
083: }
084:
085: public static Test suite() {
086: TestSuite suite = new TestSuite(SignedPartsTest.class);
087:
088: return suite;
089: }
090:
091: private PolicySourceModel unmarshalPolicyResource(String resource)
092: throws PolicyException, IOException {
093: Reader reader = getResourceReader(resource);
094: PolicySourceModel model = PolicyModelUnmarshaller
095: .getXmlUnmarshaller().unmarshalModel(reader);
096: reader.close();
097: return model;
098: }
099:
100: private Reader getResourceReader(String resourceName) {
101: return new InputStreamReader(Thread.currentThread()
102: .getContextClassLoader().getResourceAsStream(
103: resourceName));
104: }
105:
106: public Policy unmarshalPolicy(String xmlFile) throws Exception {
107: PolicySourceModel model = unmarshalPolicyResource(xmlFile);
108: Policy mbp = PolicyModelTranslator.getTranslator().translate(
109: model);
110: return mbp;
111:
112: }
113:
114: public boolean isHeaderPresent(QName expected, Iterator headers) {
115: while (headers.hasNext()) {
116: Header header = (Header) headers.next();
117: if (expected.getLocalPart().equals(header.getLocalName())) {
118: if (expected.getNamespaceURI().equals(header.getURI())) {
119: return true;
120: }
121: }
122: }
123: return false;
124: }
125:
126: public void testSignParts1() throws Exception {
127: String fileName = "security/SignParts1.xml";
128: Policy policy = unmarshalPolicy(fileName);
129: assertNotNull(policy);
130: Iterator<AssertionSet> itr = policy.iterator();
131: if (itr.hasNext()) {
132: AssertionSet as = itr.next();
133: for (PolicyAssertion assertion : as) {
134: assertEquals("Invalid assertion", "SignedParts",
135: assertion.getName().getLocalPart());
136: SignedParts sp = (SignedParts) assertion;
137: assertEquals("Body not found ", true, sp.hasBody());
138:
139: assertTrue(isHeaderPresent(
140: new QName(
141: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
142: "To"), sp.getHeaders()));
143: assertTrue(isHeaderPresent(
144: new QName(
145: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
146: "From"), sp.getHeaders()));
147: assertTrue(isHeaderPresent(
148: new QName(
149: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
150: "FaultTo"), sp.getHeaders()));
151: assertTrue(isHeaderPresent(
152: new QName(
153: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
154: "ReplyTo"), sp.getHeaders()));
155: assertTrue(isHeaderPresent(
156: new QName(
157: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
158: "MessageID"), sp.getHeaders()));
159: assertTrue(isHeaderPresent(
160: new QName(
161: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
162: "RelatesTo"), sp.getHeaders()));
163: assertTrue(isHeaderPresent(
164: new QName(
165: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
166: "Action"), sp.getHeaders()));
167: }
168: } else {
169: throw new Exception(
170: "No Assertions found!. Unmarshalling of "
171: + fileName + "failed!");
172: }
173: }
174:
175: public void testSignParts2() throws Exception {
176: String fileName = "security/SignParts2.xml";
177: Policy policy = unmarshalPolicy(fileName);
178: assertNotNull(policy);
179: Iterator<AssertionSet> itr = policy.iterator();
180: if (itr.hasNext()) {
181: AssertionSet as = itr.next();
182: for (PolicyAssertion assertion : as) {
183: assertEquals("Invalid assertion", "SignedParts",
184: assertion.getName().getLocalPart());
185: SignedParts sp = (SignedParts) assertion;
186: assertEquals("Body not found ", true, sp.hasBody());
187: // assertTrue("Header elements should not be present",sp.getHeaders().hasNext());
188: }
189: } else {
190: throw new Exception(
191: "No Assertions found!. Unmarshalling of "
192: + fileName + "failed!");
193: }
194: }
195:
196: public void testSignParts3() throws Exception {
197: String fileName = "security/SignParts3.xml";
198: Policy policy = unmarshalPolicy(fileName);
199: assertNotNull(policy);
200: Iterator<AssertionSet> itr = policy.iterator();
201: if (itr.hasNext()) {
202: AssertionSet as = itr.next();
203: for (PolicyAssertion assertion : as) {
204: assertEquals("Invalid assertion", "SignedParts",
205: assertion.getName().getLocalPart());
206: SignedParts sp = (SignedParts) assertion;
207: assertEquals("Body should not be present ", false, sp
208: .hasBody());
209:
210: assertTrue(isHeaderPresent(
211: new QName(
212: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
213: "To"), sp.getHeaders()));
214: assertTrue(isHeaderPresent(
215: new QName(
216: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
217: "From"), sp.getHeaders()));
218: assertTrue(isHeaderPresent(
219: new QName(
220: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
221: "FaultTo"), sp.getHeaders()));
222: assertTrue(isHeaderPresent(
223: new QName(
224: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
225: "ReplyTo"), sp.getHeaders()));
226: assertTrue(isHeaderPresent(
227: new QName(
228: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
229: "MessageID"), sp.getHeaders()));
230: assertTrue(isHeaderPresent(
231: new QName(
232: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
233: "RelatesTo"), sp.getHeaders()));
234: assertTrue(isHeaderPresent(
235: new QName(
236: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
237: "Action"), sp.getHeaders()));
238: }
239: } else {
240: throw new Exception(
241: "No Assertions found!. Unmarshalling of "
242: + fileName + "failed!");
243: }
244: }
245:
246: public void testSignParts4() throws Exception {
247: String fileName = "security/SignParts4.xml";
248: try {
249: Policy policy = unmarshalPolicy(fileName);
250: } catch (PolicyException ex) {
251: //java.lang.reflect.InvocationTargetException for Namespace attribute is required under Header element
252: if (!"java.lang.reflect.InvocationTargetException"
253: .equals(ex.getMessage())) {
254: System.out.println("Exception is different "
255: + ex.getMessage());
256: throw ex;
257: }
258: }
259: }
260:
261: public void testSignParts5() throws Exception {
262: String fileName = "security/SignParts5.xml";
263: Policy policy = unmarshalPolicy(fileName);
264: assertNotNull(policy);
265: Iterator<AssertionSet> itr = policy.iterator();
266: if (itr.hasNext()) {
267: AssertionSet as = itr.next();
268: for (PolicyAssertion assertion : as) {
269: assertEquals("Invalid assertion", "SignedParts",
270: assertion.getName().getLocalPart());
271: SignedParts sp = (SignedParts) assertion;
272: assertEquals("Body should not be present ", false, sp
273: .hasBody());
274:
275: assertTrue(isHeaderPresent(
276: new QName(
277: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
278: ""), sp.getHeaders()));
279: assertTrue(isHeaderPresent(
280: new QName(
281: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
282: "FaultTo"), sp.getHeaders()));
283: assertTrue(isHeaderPresent(
284: new QName(
285: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
286: "ReplyTo"), sp.getHeaders()));
287: assertTrue(isHeaderPresent(
288: new QName(
289: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
290: "MessageID"), sp.getHeaders()));
291: assertTrue(isHeaderPresent(
292: new QName(
293: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
294: "RelatesTo"), sp.getHeaders()));
295: assertTrue(isHeaderPresent(
296: new QName(
297: "http://schemas.xmlsoap.org/ws/2004/08/addressing",
298: "Action"), sp.getHeaders()));
299: }
300: } else {
301: throw new Exception(
302: "No Assertions found!. Unmarshalling of "
303: + fileName + "failed!");
304: }
305: }
306:
307: public void testSignParts6() throws Exception {
308: String fileName = "security/SignParts6.xml";
309: Policy policy = unmarshalPolicy(fileName);
310: assertNotNull(policy);
311: Iterator<AssertionSet> itr = policy.iterator();
312: if (itr.hasNext()) {
313: AssertionSet as = itr.next();
314: for (PolicyAssertion assertion : as) {
315: assertEquals("Invalid assertion", "SignedParts",
316: assertion.getName().getLocalPart());
317: SignedParts sp = (SignedParts) assertion;
318: assertEquals("Body should not be present ", false, sp
319: .hasBody());
320: assertFalse("Headers should not be present", sp
321: .getHeaders().hasNext());
322: }
323: } else {
324: throw new Exception(
325: "No Assertions found!. Unmarshalling of "
326: + fileName + "failed!");
327: }
328: }
329:
330: }
|