001 package org.apache.commons.openpgp; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import static org.junit.Assert.assertNotNull; 021 import static org.junit.Assert.assertTrue; 022 023 import java.io.ByteArrayInputStream; 024 import java.io.IOException; 025 import java.io.InputStream; 026 027 import org.junit.Before; 028 import org.junit.Test; 029 030 /** 031 * Test the open pgp signer. 032 * 033 * @author <a href="mailto:brett@apache.org">Brett Porter</a> 034 * @todo test text input as well as binary - apparently it fails cross platform 035 */ 036 public class BouncyCastleOpenPgpStreamingSignerTest 037 { 038 private String keyId = "A7D16BD4"; 039 040 private KeyRing keyRing; 041 042 private static final String PASSWORD = "cop"; 043 044 @Before 045 public void setUp() 046 throws Exception 047 { 048 keyRing = new BouncyCastleKeyRing( getClass().getResourceAsStream( "/secring.gpg" ), 049 getClass().getResourceAsStream( "/pubring.gpg" ), PASSWORD.toCharArray() ); 050 } 051 052 @Test 053 public void testSignDataDetachedBinary() 054 throws OpenPgpException, IOException 055 { 056 OpenPgpStreamingSigner signer = new BouncyCastleOpenPgpStreamingSigner( keyId, keyRing, false ); 057 058 InputStream in = getClass().getResourceAsStream( "/test-input" ); 059 byte[] buf = new byte[8192]; 060 int len; 061 try 062 { 063 do 064 { 065 len = in.read( buf, 0, 8192 ); 066 if ( len > 0 ) 067 { 068 signer.update( buf, 0, len ); 069 } 070 } 071 while ( len >= 0 ); 072 } 073 finally 074 { 075 in.close(); 076 } 077 078 byte[] signature = signer.finish(); 079 080 OpenPgpSignatureVerifier verifier = new BouncyCastleOpenPgpSignatureVerifier(); 081 082 SignatureStatus status = 083 verifier.verifyDetachedSignature( getClass().getResourceAsStream( "/test-input" ), 084 new ByteArrayInputStream( signature ), keyRing ); 085 assertNotNull( "check we got a status", status ); 086 assertTrue( "check it was successful", status.isValid() ); 087 } 088 089 @Test 090 public void testVerifySignatureDetachedBinary() 091 throws IOException, OpenPgpException 092 { 093 InputStream signature = getClass().getResourceAsStream( "/test-input.bpg" ); 094 OpenPgpStreamingSignatureVerifier verifier = 095 new BouncyCastleOpenPgpStreamingSignatureVerifier( signature, keyRing ); 096 097 InputStream in = getClass().getResourceAsStream( "/test-input" ); 098 byte[] buf = new byte[8192]; 099 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 }