[CXF-7429] Preventing NPE
[cxf.git] / systests / ws-security / src / test / java / org / apache / cxf / systest / ws / mtom / MTOMSecurityTest.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.cxf.systest.ws.mtom;
21
22 import java.io.File;
23 import java.net.URL;
24
25 import javax.activation.DataHandler;
26 import javax.activation.DataSource;
27 import javax.activation.FileDataSource;
28 import javax.xml.namespace.QName;
29 import javax.xml.ws.Service;
30
31 import org.apache.cxf.Bus;
32 import org.apache.cxf.bus.spring.SpringBusFactory;
33 import org.apache.cxf.systest.ws.common.SecurityTestUtil;
34 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
35 import org.example.contract.doubleit.DoubleItMtomPortType;
36 import org.example.contract.doubleit.DoubleItPortType;
37 import org.example.schema.doubleit.DoubleIt4;
38 import org.junit.BeforeClass;
39
40 /**
41 * A set of secured MTOM
42 */
43 public class MTOMSecurityTest extends AbstractBusClientServerTestBase {
44 public static final String PORT = allocatePort(Server.class);
45
46 private static final String NAMESPACE = "http://www.example.org/contract/DoubleIt";
47 private static final QName SERVICE_QNAME = new QName(NAMESPACE, "DoubleItService");
48
49 @BeforeClass
50 public static void startServers() throws Exception {
51 assertTrue(
52 "Server failed to launch",
53 // run the server in the same process
54 // set this to false to fork
55 launchServer(Server.class, true)
56 );
57 }
58
59 @org.junit.AfterClass
60 public static void cleanup() throws Exception {
61 SecurityTestUtil.cleanup();
62 stopAllServers();
63 }
64
65 // The attachment is inlined + the SOAP Body signed
66 @org.junit.Test
67 public void testSignedMTOMInline() throws Exception {
68
69 SpringBusFactory bf = new SpringBusFactory();
70 URL busFile = MTOMSecurityTest.class.getResource("client.xml");
71
72 Bus bus = bf.createBus(busFile.toString());
73 SpringBusFactory.setDefaultBus(bus);
74 SpringBusFactory.setThreadDefaultBus(bus);
75
76 URL wsdl = MTOMSecurityTest.class.getResource("DoubleItMtom.wsdl");
77 Service service = Service.create(wsdl, SERVICE_QNAME);
78 QName portQName = new QName(NAMESPACE, "DoubleItSignedMTOMInlinePort");
79 DoubleItMtomPortType port =
80 service.getPort(portQName, DoubleItMtomPortType.class);
81 updateAddressPort(port, PORT);
82
83 DataSource source = new FileDataSource(new File("src/test/resources/java.jpg"));
84 DoubleIt4 doubleIt = new DoubleIt4();
85 doubleIt.setNumberToDouble(25);
86 port.doubleIt4(25, new DataHandler(source));
87
88 ((java.io.Closeable)port).close();
89 bus.shutdown(true);
90 }
91
92 // Here we are not-inlining, but the attachments are signed (as is the SOAP Body)
93 @org.junit.Test
94 public void testSignedMTOMSwA() throws Exception {
95
96 SpringBusFactory bf = new SpringBusFactory();
97 URL busFile = MTOMSecurityTest.class.getResource("client.xml");
98
99 Bus bus = bf.createBus(busFile.toString());
100 SpringBusFactory.setDefaultBus(bus);
101 SpringBusFactory.setThreadDefaultBus(bus);
102
103 URL wsdl = MTOMSecurityTest.class.getResource("DoubleItMtom.wsdl");
104 Service service = Service.create(wsdl, SERVICE_QNAME);
105 QName portQName = new QName(NAMESPACE, "DoubleItSignedMTOMSwAPort");
106 DoubleItMtomPortType port =
107 service.getPort(portQName, DoubleItMtomPortType.class);
108 updateAddressPort(port, PORT);
109
110 DataSource source = new FileDataSource(new File("src/test/resources/java.jpg"));
111 DoubleIt4 doubleIt = new DoubleIt4();
112 doubleIt.setNumberToDouble(25);
113 port.doubleIt4(25, new DataHandler(source));
114
115 ((java.io.Closeable)port).close();
116 bus.shutdown(true);
117 }
118
119 // Here we moving encrypted bytes to attachments instead, and referencing them via xop:Include
120 // This avoids the BASE-64 encoding/decoding step when the raw bytes are included in the SOAP Envelope
121 @org.junit.Test
122 public void testAsymmetricBytesInAttachment() throws Exception {
123
124 SpringBusFactory bf = new SpringBusFactory();
125 URL busFile = MTOMSecurityTest.class.getResource("client.xml");
126
127 Bus bus = bf.createBus(busFile.toString());
128 SpringBusFactory.setDefaultBus(bus);
129 SpringBusFactory.setThreadDefaultBus(bus);
130
131 URL wsdl = MTOMSecurityTest.class.getResource("DoubleItMtom.wsdl");
132 Service service = Service.create(wsdl, SERVICE_QNAME);
133 QName portQName = new QName(NAMESPACE, "DoubleItAsymmetricPort");
134 DoubleItPortType port =
135 service.getPort(portQName, DoubleItPortType.class);
136 updateAddressPort(port, PORT);
137
138 int result = port.doubleIt(25);
139 assertEquals(result, 50);
140
141 ((java.io.Closeable)port).close();
142 bus.shutdown(true);
143 }
144
145 @org.junit.Test
146 public void testSymmetricBytesInAttachment() throws Exception {
147
148 SpringBusFactory bf = new SpringBusFactory();
149 URL busFile = MTOMSecurityTest.class.getResource("client.xml");
150
151 Bus bus = bf.createBus(busFile.toString());
152 SpringBusFactory.setDefaultBus(bus);
153 SpringBusFactory.setThreadDefaultBus(bus);
154
155 URL wsdl = MTOMSecurityTest.class.getResource("DoubleItMtom.wsdl");
156 Service service = Service.create(wsdl, SERVICE_QNAME);
157 QName portQName = new QName(NAMESPACE, "DoubleItSymmetricPort");
158 DoubleItPortType port =
159 service.getPort(portQName, DoubleItPortType.class);
160 updateAddressPort(port, PORT);
161
162 int result = port.doubleIt(25);
163 assertEquals(result, 50);
164
165 ((java.io.Closeable)port).close();
166 bus.shutdown(true);
167 }
168
169 @org.junit.Test
170 public void testActionBytesInAttachment() throws Exception {
171
172 SpringBusFactory bf = new SpringBusFactory();
173 URL busFile = MTOMSecurityTest.class.getResource("client.xml");
174
175 Bus bus = bf.createBus(busFile.toString());
176 SpringBusFactory.setDefaultBus(bus);
177 SpringBusFactory.setThreadDefaultBus(bus);
178
179 URL wsdl = MTOMSecurityTest.class.getResource("DoubleItMtom.wsdl");
180 Service service = Service.create(wsdl, SERVICE_QNAME);
181 QName portQName = new QName(NAMESPACE, "DoubleItActionPort");
182 DoubleItPortType port =
183 service.getPort(portQName, DoubleItPortType.class);
184 updateAddressPort(port, PORT);
185
186 int result = port.doubleIt(25);
187 assertEquals(result, 50);
188
189 ((java.io.Closeable)port).close();
190 bus.shutdown(true);
191 }
192 }