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: * XWSSPolicyContainerTest.java
038: * JUnit based test
039: *
040: * Created on August 24, 2006, 3:54 AM
041: */
042:
043: package com.sun.xml.ws.security.impl.policyconv;
044:
045: import com.sun.xml.wss.impl.policy.mls.EncryptionTarget;
046: import com.sun.xml.wss.impl.policy.mls.SignatureTarget;
047: import com.sun.xml.wss.impl.policy.mls.Target;
048: import com.sun.xml.wss.impl.policy.mls.TimestampPolicy;
049: import com.sun.xml.wss.impl.policy.mls.WSSPolicy;
050: import junit.framework.*;
051: import com.sun.xml.wss.impl.PolicyTypeUtil;
052: import com.sun.xml.wss.impl.policy.MLSPolicy;
053: import com.sun.xml.wss.impl.policy.SecurityPolicy;
054: import com.sun.xml.ws.security.policy.MessageLayout;
055: import com.sun.xml.wss.impl.policy.mls.AuthenticationTokenPolicy;
056: import com.sun.xml.wss.impl.policy.mls.EncryptionPolicy;
057: import com.sun.xml.wss.impl.policy.mls.MessagePolicy;
058: import com.sun.xml.wss.impl.policy.mls.SignaturePolicy;
059: import java.util.ArrayList;
060: import java.util.List;
061:
062: /**
063: *
064: * @author Mayank.Mishra@SUN.com
065: */
066: public class XWSSPolicyContainerTest extends TestCase {
067:
068: public XWSSPolicyContainerTest(String testName) {
069: super (testName);
070: }
071:
072: protected void setUp() throws Exception {
073: }
074:
075: protected void tearDown() throws Exception {
076: }
077:
078: public static Test suite() {
079: TestSuite suite = new TestSuite(XWSSPolicyContainerTest.class);
080:
081: return suite;
082: }
083:
084: //for client outgoing
085: public void testXWSSPolicyContainer1() throws Exception {
086: XWSSPolicyContainer container = new XWSSPolicyContainer(false,
087: false);
088: SignaturePolicy sigPolicy = new SignaturePolicy();
089: SignatureTarget t = new SignatureTarget();
090: t.setType("uri");
091: t.setValue("sig1");
092: SignaturePolicy.FeatureBinding featureBinding = (SignaturePolicy.FeatureBinding) sigPolicy
093: .getFeatureBinding();
094:
095: featureBinding.addTargetBinding(t);
096: ((AuthenticationTokenPolicy.X509CertificateBinding) sigPolicy
097: .newX509CertificateKeyBinding())
098: .setReferenceType("Direct");
099:
100: EncryptionPolicy encPolicy = new EncryptionPolicy();
101: EncryptionTarget t1 = new EncryptionTarget();
102: t.setType("uri");
103: t.setValue("enc1");
104: EncryptionPolicy.FeatureBinding featureBinding1 = (EncryptionPolicy.FeatureBinding) encPolicy
105: .getFeatureBinding();
106:
107: featureBinding1.addTargetBinding(t1);
108: ((AuthenticationTokenPolicy.X509CertificateBinding) encPolicy
109: .newX509CertificateKeyBinding())
110: .setReferenceType("Direct");
111:
112: TimestampPolicy tp = new TimestampPolicy();
113: AuthenticationTokenPolicy atp = new AuthenticationTokenPolicy();
114: AuthenticationTokenPolicy.UsernameTokenBinding ut = new AuthenticationTokenPolicy.UsernameTokenBinding();
115: atp.setFeatureBinding(ut);
116:
117: container.setMessageMode(false, false);
118: container.setPolicyContainerMode(MessageLayout.Lax);
119: container.insert(sigPolicy);
120: container.insert(encPolicy);
121: container.insert(tp);
122: container.insert(atp);
123:
124: print(container.getMessagePolicy());
125:
126: System.out.println("\n\n\n-------------------------\n");
127:
128: }
129:
130: //for client incoming
131: public void testXWSSPolicyContainer2() throws Exception {
132: XWSSPolicyContainer container = new XWSSPolicyContainer(false,
133: true);
134: SignaturePolicy sigPolicy = new SignaturePolicy();
135: SignatureTarget t = new SignatureTarget();
136: t.setType("uri");
137: t.setValue("sig1");
138: SignaturePolicy.FeatureBinding featureBinding = (SignaturePolicy.FeatureBinding) sigPolicy
139: .getFeatureBinding();
140:
141: featureBinding.addTargetBinding(t);
142: ((AuthenticationTokenPolicy.X509CertificateBinding) sigPolicy
143: .newX509CertificateKeyBinding())
144: .setReferenceType("Direct");
145:
146: EncryptionPolicy encPolicy = new EncryptionPolicy();
147: EncryptionTarget t1 = new EncryptionTarget();
148: t.setType("uri");
149: t.setValue("enc1");
150: EncryptionPolicy.FeatureBinding featureBinding1 = (EncryptionPolicy.FeatureBinding) encPolicy
151: .getFeatureBinding();
152:
153: featureBinding1.addTargetBinding(t1);
154: ((AuthenticationTokenPolicy.X509CertificateBinding) encPolicy
155: .newX509CertificateKeyBinding())
156: .setReferenceType("Direct");
157:
158: TimestampPolicy tp = new TimestampPolicy();
159: AuthenticationTokenPolicy atp = new AuthenticationTokenPolicy();
160: AuthenticationTokenPolicy.UsernameTokenBinding ut = new AuthenticationTokenPolicy.UsernameTokenBinding();
161: atp.setFeatureBinding(ut);
162:
163: container.setMessageMode(false, true);
164: container.setPolicyContainerMode(MessageLayout.Lax);
165: container.insert(sigPolicy);
166: container.insert(encPolicy);
167: container.insert(tp);
168: container.insert(atp);
169:
170: print(container.getMessagePolicy());
171:
172: System.out.println("\n\n\n-------------------------\n");
173:
174: }
175:
176: //for server outgoing
177: public void testXWSSPolicyContainer3() throws Exception {
178: XWSSPolicyContainer container = new XWSSPolicyContainer(true,
179: false);
180: SignaturePolicy sigPolicy = new SignaturePolicy();
181: SignatureTarget t = new SignatureTarget();
182: t.setType("uri");
183: t.setValue("sig1");
184: SignaturePolicy.FeatureBinding featureBinding = (SignaturePolicy.FeatureBinding) sigPolicy
185: .getFeatureBinding();
186:
187: featureBinding.addTargetBinding(t);
188: ((AuthenticationTokenPolicy.X509CertificateBinding) sigPolicy
189: .newX509CertificateKeyBinding())
190: .setReferenceType("Direct");
191:
192: EncryptionPolicy encPolicy = new EncryptionPolicy();
193: EncryptionTarget t1 = new EncryptionTarget();
194: t.setType("uri");
195: t.setValue("enc1");
196: EncryptionPolicy.FeatureBinding featureBinding1 = (EncryptionPolicy.FeatureBinding) encPolicy
197: .getFeatureBinding();
198:
199: featureBinding1.addTargetBinding(t1);
200: ((AuthenticationTokenPolicy.X509CertificateBinding) encPolicy
201: .newX509CertificateKeyBinding())
202: .setReferenceType("Direct");
203:
204: TimestampPolicy tp = new TimestampPolicy();
205: AuthenticationTokenPolicy atp = new AuthenticationTokenPolicy();
206: AuthenticationTokenPolicy.UsernameTokenBinding ut = new AuthenticationTokenPolicy.UsernameTokenBinding();
207: atp.setFeatureBinding(ut);
208:
209: container.setMessageMode(true, false);
210: container.setPolicyContainerMode(MessageLayout.Lax);
211: container.insert(sigPolicy);
212: container.insert(encPolicy);
213: container.insert(tp);
214: container.insert(atp);
215:
216: print(container.getMessagePolicy());
217:
218: System.out.println("\n\n\n-------------------------\n");
219:
220: }
221:
222: //for server incoming
223: public void testXWSSPolicyContainer4() throws Exception {
224: XWSSPolicyContainer container = new XWSSPolicyContainer(true,
225: true);
226: SignaturePolicy sigPolicy = new SignaturePolicy();
227: SignatureTarget t = new SignatureTarget();
228: t.setType("uri");
229: t.setValue("sig1");
230: SignaturePolicy.FeatureBinding featureBinding = (SignaturePolicy.FeatureBinding) sigPolicy
231: .getFeatureBinding();
232:
233: featureBinding.addTargetBinding(t);
234: ((AuthenticationTokenPolicy.X509CertificateBinding) sigPolicy
235: .newX509CertificateKeyBinding())
236: .setReferenceType("Direct");
237:
238: EncryptionPolicy encPolicy = new EncryptionPolicy();
239: EncryptionTarget t1 = new EncryptionTarget();
240: t.setType("uri");
241: t.setValue("enc1");
242: EncryptionPolicy.FeatureBinding featureBinding1 = (EncryptionPolicy.FeatureBinding) encPolicy
243: .getFeatureBinding();
244:
245: featureBinding1.addTargetBinding(t1);
246: ((AuthenticationTokenPolicy.X509CertificateBinding) encPolicy
247: .newX509CertificateKeyBinding())
248: .setReferenceType("Direct");
249:
250: TimestampPolicy tp = new TimestampPolicy();
251: AuthenticationTokenPolicy atp = new AuthenticationTokenPolicy();
252: AuthenticationTokenPolicy.UsernameTokenBinding ut = new AuthenticationTokenPolicy.UsernameTokenBinding();
253: atp.setFeatureBinding(ut);
254:
255: container.setMessageMode(true, true);
256: container.setPolicyContainerMode(MessageLayout.Lax);
257: container.insert(sigPolicy);
258: container.insert(encPolicy);
259: container.insert(tp);
260: container.insert(atp);
261:
262: print(container.getMessagePolicy());
263:
264: System.out.println("\n\n\n-------------------------\n");
265:
266: }
267:
268: public void print(MessagePolicy generated) throws Exception {
269: for (int i = 0; i < generated.size(); i++) {
270: System.out.println("Type : " + generated.get(i).getType());
271:
272: if (PolicyTypeUtil.signaturePolicy(generated.get(i))) {
273: WSSPolicy p = (WSSPolicy) generated.get(i);
274: System.out.println("KeyBinding : "
275: + p.getKeyBinding().getType());
276:
277: SignaturePolicy.FeatureBinding f1 = (SignaturePolicy.FeatureBinding) p
278: .getFeatureBinding();
279: List<Target> t1 = f1.getTargetBindings();
280: System.out.println("No of Targets : " + t1.size());
281: for (Target t : t1) {
282: System.out.println(t.getType() + " "
283: + t.getValue());
284: }
285: }
286:
287: if (PolicyTypeUtil.encryptionPolicy(generated.get(i))) {
288: WSSPolicy p = (WSSPolicy) generated.get(i);
289: System.out.println("KeyBinding : "
290: + p.getKeyBinding().getType());
291:
292: EncryptionPolicy.FeatureBinding f1 = (EncryptionPolicy.FeatureBinding) p
293: .getFeatureBinding();
294: List<Target> t1 = f1.getTargetBindings();
295: System.out.println("No of Targets : " + t1.size());
296: for (Target t : t1) {
297: System.out.println(t.getType() + " "
298: + t.getValue());
299: }
300: }
301: }
302: }
303:
304: }
|