1   package org.apache.commons.openpgp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import junit.framework.TestCase;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  
26  /**
27   * Test the open pgp signer.
28   *
29   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
30   * @todo test text input as well as binary - apparently it fails cross platform
31   */
32  public class BouncyCastleOpenPgpSignerTest
33      extends TestCase
34  {
35      private OpenPgpSigner signer = new BouncyCastleOpenPgpSigner();
36  
37      private OpenPgpSignatureVerifier verifier = new BouncyCastleOpenPgpSignatureVerifier();
38  
39      private String keyId = "A7D16BD4";
40  
41      private KeyRing keyRing;
42  
43      private static final String PASSWORD = "cop";
44  
45      protected void setUp()
46          throws Exception
47      {
48          super.setUp();
49  
50          keyRing = new BouncyCastleKeyRing( getClass().getResourceAsStream( "/secring.gpg" ),
51                                             getClass().getResourceAsStream( "/pubring.gpg" ), PASSWORD.toCharArray() );
52      }
53  
54      public void testSignDataDetachedBinary()
55          throws OpenPgpException, IOException
56      {
57          ByteArrayOutputStream signature = new ByteArrayOutputStream();
58          signer.detachedSign( getClass().getResourceAsStream( "/test-input" ), signature, keyId, keyRing, false );
59  
60          // TODO: can we get it to verify an ascii armored one?
61          SignatureStatus status = verifier.verifyDetachedSignature( getClass().getResourceAsStream( "/test-input" ),
62                                                                     new ByteArrayInputStream( signature.toByteArray() ),
63                                                                     keyRing, false );
64          assertNotNull( "check we got a status", status );
65          assertTrue( "check it was successful", status.isValid() );
66      }
67  
68      public void testVerifySignatureDetachedBinary()
69          throws IOException, OpenPgpException
70      {
71          // TODO: can we get it to verify an ascii armored one?
72          SignatureStatus status = verifier.verifyDetachedSignature( getClass().getResourceAsStream( "/test-input" ),
73                                                                     getClass().getResourceAsStream(
74                                                                         "/test-signature.bpg" ), keyRing, false );
75  
76          assertNotNull( "check we got a status", status );
77          assertTrue( "check it was successful", status.isValid() );
78      }
79  }