1   /*
2    * Copyright 2005 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.vafer.jdeb.signing;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.ByteArrayOutputStream;
20  import java.io.InputStream;
21  import java.util.Arrays;
22  
23  import junit.framework.TestCase;
24  
25  public final class SigningTestCase extends TestCase {
26  
27  	public void testClearSign() throws Exception {
28  		
29  		final InputStream ring = getClass().getClassLoader().getResourceAsStream("org/vafer/gpg/secring.gpg");
30  		
31  		assertNotNull(ring);
32  		
33  		final String inputStr = "TEST1\nTEST2\nTEST3\n"; 
34  		final byte[] input = inputStr.getBytes("UTF-8");
35  		
36  		final String expectedOutputStr = 
37  			"-----BEGIN PGP SIGNED MESSAGE-----\n" + 
38  			"Hash: SHA1\n" + 
39  			"\n" + 
40  			"TEST1\r\n" + 
41  			"TEST2\r\n" + 
42  			"TEST3\r\n" + 
43  			"-----BEGIN PGP SIGNATURE-----\n" + 
44  			"Version: BCPG v1.29\n" + 
45  			"\n" + 
46  			"iEYEARECABAFAkax1rgJEHM9pIAuB02PAABIJgCghFmoCJCZ0CGiqgVLGGPd/Yh5\n" + 
47  			"FQQAnRVqvI2ij45JQSHYJBblZ0Vv2meN\n" + 
48  			"=aAAT\n" + 
49  			"-----END PGP SIGNATURE-----\n";
50  		
51  		final byte[] expectedOutput = expectedOutputStr.getBytes("UTF-8"); 
52  
53  		final ByteArrayOutputStream os = new ByteArrayOutputStream();
54  
55  		SigningUtils.clearSign(
56  				new ByteArrayInputStream(input),
57  				ring,
58  				"2E074D8F", "test",
59  				os);
60  		
61  		final byte[] output = os.toByteArray();
62  		
63  		final int from = expectedOutputStr.indexOf("iEYEAREC");
64  		final int until = expectedOutputStr.indexOf("=aAAT") + 5;
65  		Arrays.fill(output, from, until, (byte)'?');
66  		Arrays.fill(expectedOutput, from, until, (byte)'?');
67  
68  		assertEquals(new String(expectedOutput), new String(output));
69  	}
70  }