This project has retired. For details please refer to its Attic page.
DigSigUtilTest xref
View Javadoc
1   /*
2    * Copyright 2013 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.v3.client;
17  
18  import java.security.cert.CertificateException;
19  import java.util.List;
20  import java.util.concurrent.atomic.AtomicReference;
21  
22  import javax.xml.crypto.dsig.CanonicalizationMethod;
23  
24  import org.apache.juddi.v3.client.cryptor.DigSigUtil;
25  import org.junit.Assert;
26  import org.junit.Test;
27  import org.uddi.api_v3.BindingTemplate;
28  import org.uddi.api_v3.BusinessEntity;
29  import org.uddi.api_v3.BusinessService;
30  import org.uddi.api_v3.Description;
31  import org.uddi.api_v3.DiscoveryURL;
32  import org.uddi.api_v3.DiscoveryURLs;
33  import org.uddi.api_v3.Name;
34  import org.uddi.api_v3.PublisherAssertion;
35  import org.uddi.api_v3.TModel;
36  import org.w3._2000._09.xmldsig_.SignatureType;
37  
38  /**
39   *
40   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
41   */
42  public class DigSigUtilTest {
43  
44      org.apache.juddi.v3.client.cryptor.DigSigUtil ds = null;
45  
46      public DigSigUtilTest() throws Exception {
47          if (System.getProperty("debug") != null && System.getProperty("debug").equalsIgnoreCase("true")) {
48              serialize = true;
49          }
50          Default();
51      }
52      static boolean serialize = false;
53  
54      void SetCertStoreSettigns() {
55          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILE, "./src/test/resources/keystore.jks");
56          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILETYPE, "JKS");
57          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILE_PASSWORD, "Test");
58          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_KEY_ALIAS, "Test");
59          ds.put(DigSigUtil.TRUSTSTORE_FILE, "./src/test/resources/truststore.jks");
60          ds.put(DigSigUtil.TRUSTSTORE_FILETYPE, "JKS");
61          ds.put(DigSigUtil.TRUSTSTORE_FILE_PASSWORD, "Test");
62      }
63  
64      void Default() throws CertificateException {
65          ds = new DigSigUtil();
66          SetCertStoreSettigns();
67          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_BASE64, "true");
68      }
69  
70      void Everything() throws CertificateException {
71          ds = new DigSigUtil();
72          SetCertStoreSettigns();
73          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_BASE64, "true");
74          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SUBJECTDN, "true");
75          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SERIAL, "true");
76      }
77  
78      void SubjectDNOnly() throws CertificateException {
79          ds = new DigSigUtil();
80          SetCertStoreSettigns();
81          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SUBJECTDN, "true");
82      }
83  
84      void SerialAndIssuerOnly() throws CertificateException {
85          ds = new DigSigUtil();
86          SetCertStoreSettigns();
87          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SERIAL, "true");
88      }
89  
90      @Test
91      public void testSignBusinessSubjectDNOnly() throws CertificateException {
92  
93          SubjectDNOnly();
94          System.out.println("testSignBusinessSubjectDNOnly signing");
95          BusinessEntity be = new BusinessEntity();
96          be.setBusinessKey("uddi:juddi.apache.org:testkey");
97          be.setDiscoveryURLs(new DiscoveryURLs());
98          be.getDiscoveryURLs().getDiscoveryURL().add(new DiscoveryURL("website", "http://localhost"));
99          be.getDescription().add(new Description("a description", "en"));
100         be.getName().add(new Name("My biz", "en"));
101 
102         BusinessEntity signUDDI_JAXBObject = ds.signUddiEntity(be);
103         if (serialize)
104         DigSigUtil.JAXB_ToStdOut(signUDDI_JAXBObject);
105         Assert.assertNotSame("items are the same", be, signUDDI_JAXBObject);
106         //System.out.println("verifing");
107         AtomicReference<String> msg = new AtomicReference<String>();
108         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
109         if (verifySigned_UDDI_JAXB_Object) {
110             //System.out.println("signature validation passed (expected)");
111         } else {
112             System.out.println("signature validation failed (not expected)");
113             Assert.fail(msg.get());
114         }
115         validAllSignatureElementsArePresent(signUDDI_JAXBObject.getSignature());
116     }
117 
118     @Test
119     public void testSignBusinessSerialAndIssuerOnly() throws CertificateException {
120         System.out.println("testSignBusinessSerialAndIssuerOnly signing");
121         SerialAndIssuerOnly();
122 
123         BusinessEntity be = new BusinessEntity();
124         be.setBusinessKey("uddi:juddi.apache.org:testkey");
125         be.setDiscoveryURLs(new DiscoveryURLs());
126         be.getDiscoveryURLs().getDiscoveryURL().add(new DiscoveryURL("website", "http://localhost"));
127         be.getDescription().add(new Description("a description", "en"));
128         be.getName().add(new Name("My biz", "en"));
129 
130         BusinessEntity signUDDI_JAXBObject = ds.signUddiEntity(be);
131         if (serialize)
132         DigSigUtil.JAXB_ToStdOut(signUDDI_JAXBObject);
133         Assert.assertNotSame("items are the same", be, signUDDI_JAXBObject);
134         //System.out.println("verifing");
135         AtomicReference<String> msg = new AtomicReference<String>();
136         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
137         if (verifySigned_UDDI_JAXB_Object) {
138             //System.out.println("signature validation passed (expected)");
139         } else {
140             System.out.println("signature validation failed (not expected)");
141             Assert.fail(msg.get());
142         }
143         validAllSignatureElementsArePresent(signUDDI_JAXBObject.getSignature());
144     }
145 
146     @Test
147     public void testSignBusinessEverything() throws CertificateException {
148         System.out.println("testSignBusinessEverything signing");
149         Everything();
150 
151         BusinessEntity be = new BusinessEntity();
152         be.setBusinessKey("uddi:juddi.apache.org:testkey");
153         be.setDiscoveryURLs(new DiscoveryURLs());
154         be.getDiscoveryURLs().getDiscoveryURL().add(new DiscoveryURL("website", "http://localhost"));
155         be.getDescription().add(new Description("a description", "en"));
156         be.getName().add(new Name("My biz", "en"));
157 
158         BusinessEntity signUDDI_JAXBObject = ds.signUddiEntity(be);
159         if (serialize)
160         DigSigUtil.JAXB_ToStdOut(signUDDI_JAXBObject);
161         Assert.assertNotSame("items are the same", be, signUDDI_JAXBObject);
162         //System.out.println("verifing");
163         AtomicReference<String> msg = new AtomicReference<String>();
164         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
165         if (verifySigned_UDDI_JAXB_Object) {
166             //System.out.println("signature validation passed (expected)");
167         } else {
168             System.out.println("signature validation failed (not expected)");
169             Assert.fail(msg.get());
170         }
171         validAllSignatureElementsArePresent(signUDDI_JAXBObject.getSignature());
172     }
173 
174     @Test
175     public void testSignBusiness() throws CertificateException {
176         Default();
177 
178 
179         System.out.println("testSignBusiness signing");
180         BusinessEntity be = new BusinessEntity();
181         be.setBusinessKey("uddi:juddi.apache.org:testkey");
182         be.setDiscoveryURLs(new DiscoveryURLs());
183         be.getDiscoveryURLs().getDiscoveryURL().add(new DiscoveryURL("website", "http://localhost"));
184         be.getDescription().add(new Description("a description", "en"));
185         be.getName().add(new Name("My biz", "en"));
186 
187         BusinessEntity signUDDI_JAXBObject = ds.signUddiEntity(be);
188         Assert.assertNotSame("items are the same", be, signUDDI_JAXBObject);
189         //System.out.println("verifing");
190         AtomicReference<String> msg = new AtomicReference<String>();
191         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
192         if (verifySigned_UDDI_JAXB_Object) {
193             //System.out.println("signature validation passed (expected)");
194         } else {
195             System.out.println("signature validation failed (not expected)");
196             Assert.fail(msg.get());
197         }
198         validAllSignatureElementsArePresent(signUDDI_JAXBObject.getSignature());
199     }
200 
201     @Test
202     public void testSignService() throws CertificateException {
203         Default();
204         System.out.println("testSignService signing");
205         BusinessService be = new BusinessService();
206         be.setBusinessKey("uddi:juddi.apache.org:testkey");
207 
208         be.getDescription().add(new Description("a description", "en"));
209         be.getName().add(new Name("My biz", "en"));
210 
211         BusinessService signUDDI_JAXBObject = ds.signUddiEntity(be);
212         Assert.assertNotSame("items are the same", be, signUDDI_JAXBObject);
213         //System.out.println("verifing");
214         AtomicReference<String> msg = new AtomicReference<String>();
215         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
216         if (verifySigned_UDDI_JAXB_Object) {
217             //System.out.println("signature validation passed (expected)");
218         } else {
219             System.out.println("signature validation failed (not expected)");
220             Assert.fail(msg.get());
221         }
222         validAllSignatureElementsArePresent(signUDDI_JAXBObject.getSignature());
223     }
224 
225     @Test
226     public void testSignTmodel() throws CertificateException {
227         Default();
228         System.out.println("testSignTmodel signing");
229         TModel be = new TModel();
230         be.setTModelKey("uddi:juddi.apache.org:testkey");
231 
232         be.getDescription().add(new Description("a description", "en"));
233         be.setName(new Name("My biz", "en"));
234 
235         TModel signUDDI_JAXBObject = ds.signUddiEntity(be);
236         Assert.assertNotSame("items are the same", be, signUDDI_JAXBObject);
237         //System.out.println("verifing");
238         AtomicReference<String> msg = new AtomicReference<String>();
239         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
240         if (verifySigned_UDDI_JAXB_Object) {
241             //System.out.println("signature validation passed (expected)");
242         } else {
243             System.out.println("signature validation failed (not expected)");
244             Assert.fail(msg.get());
245         }
246         validAllSignatureElementsArePresent(signUDDI_JAXBObject.getSignature());
247     }
248 
249     @Test
250     public void testSignBinding() throws CertificateException {
251         Default();
252         System.out.println("testSignBinding signing");
253         BindingTemplate be = new BindingTemplate();
254         be.setBindingKey("uddi:juddi.apache.org:testkey");
255 
256         be.getDescription().add(new Description("a description", "en"));
257 
258 
259         BindingTemplate signUDDI_JAXBObject = ds.signUddiEntity(be);
260         Assert.assertNotSame("items are the same", be, signUDDI_JAXBObject);
261         //System.out.println("verifing");
262         AtomicReference<String> msg = new AtomicReference<String>();
263         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
264         if (verifySigned_UDDI_JAXB_Object) {
265             //System.out.println("signature validation passed (expected)");
266         } else {
267             System.out.println("signature validation failed (not expected)");
268             Assert.fail(msg.get());
269         }
270         validAllSignatureElementsArePresent(signUDDI_JAXBObject.getSignature());
271     }
272 
273     @Test
274     public void testSignPublisherAssertion() throws CertificateException {
275         Default();
276         System.out.println("testSignPublisherAssertion signing");
277         PublisherAssertion be = new PublisherAssertion();
278         be.setFromKey("uddi:juddi.apache.org:testkey");
279         be.setToKey("uddi:juddi.apache.org:testkey");
280 
281         PublisherAssertion signUDDI_JAXBObject = ds.signUddiEntity(be);
282         Assert.assertNotSame("items are the same", be, signUDDI_JAXBObject);
283         //System.out.println("verifing");
284         AtomicReference<String> msg = new AtomicReference<String>();
285         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
286         if (verifySigned_UDDI_JAXB_Object) {
287             //System.out.println("signature validation passed (expected)");
288         } else {
289             System.out.println("signature validation failed (not expected)");
290             Assert.fail(msg.get());
291         }
292         validAllSignatureElementsArePresent(signUDDI_JAXBObject.getSignature());
293 
294 
295     }
296 
297     static void validAllSignatureElementsArePresent(List<SignatureType> sigs) {
298         Assert.assertNotNull(sigs);
299         Assert.assertFalse(sigs.isEmpty());
300         for (int i = 0; i < sigs.size(); i++) {
301             Assert.assertFalse(sigs.get(i).getKeyInfo().getContent().isEmpty());
302             for (int k = 0; k < sigs.get(i).getSignedInfo().getCanonicalizationMethod().getContent().size(); k++) {
303                 Assert.assertTrue(sigs.get(i).getSignedInfo().getCanonicalizationMethod().getContent().get(k).equals(CanonicalizationMethod.EXCLUSIVE));
304             }
305         }
306     }
307 }