View Javadoc

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 static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  /**
31   * Test the open pgp signer.
32   *
33   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
34   * @todo test text input as well as binary - apparently it fails cross platform
35   */
36  public class BouncyCastleOpenPgpStreamingSignerTest
37  {
38      private String keyId = "A7D16BD4";
39  
40      private KeyRing keyRing;
41  
42      private static final String PASSWORD = "cop";
43  
44      @Before
45      public void setUp()
46          throws Exception
47      {
48          keyRing = new BouncyCastleKeyRing( getClass().getResourceAsStream( "/secring.gpg" ),
49                                             getClass().getResourceAsStream( "/pubring.gpg" ), PASSWORD.toCharArray() );
50      }
51  
52      @Test
53      public void testSignDataDetachedBinary()
54          throws OpenPgpException, IOException
55      {
56          OpenPgpStreamingSigner signer = new BouncyCastleOpenPgpStreamingSigner( keyId, keyRing, false );
57  
58          InputStream in = getClass().getResourceAsStream( "/test-input" );
59          byte[] buf = new byte[8192];
60          int len;
61          try
62          {
63              do
64              {
65                  len = in.read( buf, 0, 8192 );
66                  if ( len > 0 )
67                  {
68                      signer.update( buf, 0, len );
69                  }
70              }
71              while ( len >= 0 );
72          }
73          finally
74          {
75              in.close();
76          }
77  
78          byte[] signature = signer.finish();
79  
80          OpenPgpSignatureVerifier verifier = new BouncyCastleOpenPgpSignatureVerifier();
81  
82          SignatureStatus status =
83              verifier.verifyDetachedSignature( getClass().getResourceAsStream( "/test-input" ),
84                                                new ByteArrayInputStream( signature ), keyRing );
85          assertNotNull( "check we got a status", status );
86          assertTrue( "check it was successful", status.isValid() );
87      }
88  
89      @Test
90      public void testVerifySignatureDetachedBinary()
91          throws IOException, OpenPgpException
92      {
93          InputStream signature = getClass().getResourceAsStream( "/test-input.bpg" );
94          OpenPgpStreamingSignatureVerifier verifier =
95              new BouncyCastleOpenPgpStreamingSignatureVerifier( signature, keyRing );
96  
97          InputStream in = getClass().getResourceAsStream( "/test-input" );
98          byte[] buf = new byte[8192];
99          int len;
100         try
101         {
102             do
103             {
104                 len = in.read( buf, 0, 8192 );
105                 if ( len > 0 )
106                 {
107                     verifier.update( buf, 0, len );
108                 }
109             }
110             while ( len >= 0 );
111         }
112         finally
113         {
114             in.close();
115         }
116 
117         SignatureStatus status = verifier.finish();
118 
119         assertNotNull( "check we got a status", status );
120         assertTrue( "check it was successful", status.isValid() );
121     }
122 
123     @Test
124     public void testVerifySignatureDetachedBinaryGpg()
125         throws IOException, OpenPgpException
126     {
127         InputStream signature = getClass().getResourceAsStream( "/test-input.sig" );
128         OpenPgpStreamingSignatureVerifier verifier =
129             new BouncyCastleOpenPgpStreamingSignatureVerifier( signature, keyRing );
130 
131         InputStream in = getClass().getResourceAsStream( "/test-input" );
132         byte[] buf = new byte[8192];
133         int len;
134         try
135         {
136             do
137             {
138                 len = in.read( buf, 0, 8192 );
139                 if ( len > 0 )
140                 {
141                     verifier.update( buf, 0, len );
142                 }
143             }
144             while ( len >= 0 );
145         }
146         finally
147         {
148             in.close();
149         }
150 
151         SignatureStatus status = verifier.finish();
152 
153         assertNotNull( "check we got a status", status );
154         assertTrue( "check it was successful", status.isValid() );
155     }
156 
157     @Test
158     public void testSignDataDetachedAscii()
159         throws OpenPgpException, IOException
160     {
161         OpenPgpStreamingSigner signer = new BouncyCastleOpenPgpStreamingSigner( keyId, keyRing, true );
162 
163         InputStream in = getClass().getResourceAsStream( "/test-input" );
164         byte[] buf = new byte[8192];
165         int len;
166         try
167         {
168             do
169             {
170                 len = in.read( buf, 0, 8192 );
171                 if ( len > 0 )
172                 {
173                     signer.update( buf, 0, len );
174                 }
175             }
176             while ( len >= 0 );
177         }
178         finally
179         {
180             in.close();
181         }
182 
183         byte[] signature = signer.finish();
184 
185         OpenPgpSignatureVerifier verifier = new BouncyCastleOpenPgpSignatureVerifier();
186 
187         SignatureStatus status =
188             verifier.verifyDetachedSignature( getClass().getResourceAsStream( "/test-input" ),
189                                               new ByteArrayInputStream( signature ), keyRing );
190         assertNotNull( "check we got a status", status );
191         assertTrue( "check it was successful", status.isValid() );
192     }
193 
194     @Test
195     public void testVerifySignatureDetachedAscii()
196         throws IOException, OpenPgpException
197     {
198         InputStream signature = getClass().getResourceAsStream( "/test-input.asc" );
199         OpenPgpStreamingSignatureVerifier verifier =
200             new BouncyCastleOpenPgpStreamingSignatureVerifier( signature, keyRing );
201 
202         InputStream in = getClass().getResourceAsStream( "/test-input" );
203         byte[] buf = new byte[8192];
204         int len;
205         try
206         {
207             do
208             {
209                 len = in.read( buf, 0, 8192 );
210                 if ( len > 0 )
211                 {
212                     verifier.update( buf, 0, len );
213                 }
214             }
215             while ( len >= 0 );
216         }
217         finally
218         {
219             in.close();
220         }
221 
222         SignatureStatus status = verifier.finish();
223 
224         assertNotNull( "check we got a status", status );
225         assertTrue( "check it was successful", status.isValid() );
226     }
227 }