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.ws.security.message;
21  
22  import org.apache.ws.security.WSSConfig;
23  import org.apache.ws.security.WSSecurityException;
24  import org.apache.ws.security.common.SOAPUtil;
25  import org.apache.ws.security.components.crypto.CryptoFactory;
26  import org.w3c.dom.Document;
27  
28  
29  /**
30   * This class tests for error messages that apply to certificates, e.g. when a bad
31   * "username" is used for encryption or signature. See WSS-137.
32   */
33  public class CertErrorTest extends org.junit.Assert {
34      
35      public CertErrorTest() {
36          WSSConfig.init();
37      }
38  
39      /**
40       * Test for when a bad certificate is used for Signature
41       */
42      @org.junit.Test
43      public void testX509Signature() throws Exception {
44          WSSecSignature builder = new WSSecSignature();
45          builder.setUserInfo("bob", "security");
46          Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
47          WSSecHeader secHeader = new WSSecHeader();
48          secHeader.insertSecurityHeader(doc);
49          try {
50              builder.build(doc, CryptoFactory.getInstance(), secHeader);
51              fail("Expected failure on a bad username");
52          } catch (WSSecurityException ex) {
53              String expectedError = "No certificates for user bob were found for signature";
54              assertTrue(ex.getMessage().indexOf(expectedError) != -1);
55          }
56      }
57      
58      /**
59       * Test for when a bad certificate is used for Encryption
60       */
61      @org.junit.Test
62      public void testEncryption() throws Exception {
63          WSSecEncrypt builder = new WSSecEncrypt();
64          builder.setUserInfo("alice");
65          Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
66          WSSecHeader secHeader = new WSSecHeader();
67          secHeader.insertSecurityHeader(doc);
68          try {
69              builder.build(doc, CryptoFactory.getInstance(), secHeader);
70              fail("Expected failure on a bad username");
71          } catch (WSSecurityException ex) {
72              String expectedError = "No certificates for user alice were found for encryption";
73              assertTrue(ex.getMessage().indexOf(expectedError) != -1);
74          }
75      }
76  
77  }