mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-26 14:22:05 +01:00
Test FileBasedMultiPassStrategy
This commit is contained in:
parent
af6fe0aaf7
commit
8e02df9a3e
2 changed files with 72 additions and 2 deletions
|
@ -13,29 +13,34 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.pgpainless.signature;
|
package org.pgpainless.decryption_verification;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
import org.bouncycastle.openpgp.PGPException;
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPSignature;
|
import org.bouncycastle.openpgp.PGPSignature;
|
||||||
|
import org.bouncycastle.util.io.Streams;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.pgpainless.PGPainless;
|
import org.pgpainless.PGPainless;
|
||||||
import org.pgpainless.key.TestKeys;
|
import org.pgpainless.key.TestKeys;
|
||||||
import org.pgpainless.signature.cleartext_signatures.CleartextSignatureProcessor;
|
import org.pgpainless.signature.cleartext_signatures.CleartextSignatureProcessor;
|
||||||
import org.pgpainless.signature.cleartext_signatures.InMemoryMultiPassStrategy;
|
import org.pgpainless.signature.cleartext_signatures.InMemoryMultiPassStrategy;
|
||||||
import org.pgpainless.signature.cleartext_signatures.MultiPassStrategy;
|
import org.pgpainless.signature.cleartext_signatures.MultiPassStrategy;
|
||||||
|
import org.pgpainless.util.TestUtils;
|
||||||
|
|
||||||
public class CleartextSignatureVerificationTest {
|
public class CleartextSignatureVerificationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void cleartextSignVerification() throws IOException, PGPException {
|
public void cleartextSignVerification_InMemoryMultiPassStrategy() throws IOException, PGPException {
|
||||||
String message = "Ah, Juliet, if the measure of thy joy\n" +
|
String message = "Ah, Juliet, if the measure of thy joy\n" +
|
||||||
"Be heaped like mine, and that thy skill be more\n" +
|
"Be heaped like mine, and that thy skill be more\n" +
|
||||||
"To blazon it, then sweeten with thy breath\n" +
|
"To blazon it, then sweeten with thy breath\n" +
|
||||||
|
@ -71,4 +76,48 @@ public class CleartextSignatureVerificationTest {
|
||||||
assertEquals(signature.getKeyID(), signingKeys.getPublicKey().getKeyID());
|
assertEquals(signature.getKeyID(), signingKeys.getPublicKey().getKeyID());
|
||||||
assertArrayEquals(message.getBytes(StandardCharsets.UTF_8), multiPassStrategy.getBytes());
|
assertArrayEquals(message.getBytes(StandardCharsets.UTF_8), multiPassStrategy.getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void cleartextSignVerification_FileBasedMultiPassStrategy() throws IOException, PGPException {
|
||||||
|
String message = "Ah, Juliet, if the measure of thy joy\n" +
|
||||||
|
"Be heaped like mine, and that thy skill be more\n" +
|
||||||
|
"To blazon it, then sweeten with thy breath\n" +
|
||||||
|
"This neighbor air, and let rich music’s tongue\n" +
|
||||||
|
"Unfold the imagined happiness that both\n" +
|
||||||
|
"Receive in either by this dear encounter.\n";
|
||||||
|
String signed = "-----BEGIN PGP SIGNED MESSAGE-----\n" +
|
||||||
|
"Hash: SHA512\n" +
|
||||||
|
"\n" +
|
||||||
|
"Ah, Juliet, if the measure of thy joy\n" +
|
||||||
|
"Be heaped like mine, and that thy skill be more\n" +
|
||||||
|
"To blazon it, then sweeten with thy breath\n" +
|
||||||
|
"This neighbor air, and let rich music’s tongue\n" +
|
||||||
|
"Unfold the imagined happiness that both\n" +
|
||||||
|
"Receive in either by this dear encounter.\n" +
|
||||||
|
"-----BEGIN PGP SIGNATURE-----\n" +
|
||||||
|
"\n" +
|
||||||
|
"iHUEARMKAB0WIQRPZlxNwsRmC8ZCXkFXNuaTGs83DAUCYJ/x5gAKCRBXNuaTGs83\n" +
|
||||||
|
"DFRwAP9/4wMvV3WcX59Clo7mkRce6iwW3VBdiN+yMu3tjmHB2wD/RfE28Q1v4+eo\n" +
|
||||||
|
"ySNgbyvqYYsNr0fnBwaG3aaj+u5ExiE=\n" +
|
||||||
|
"=Z2SO\n" +
|
||||||
|
"-----END PGP SIGNATURE-----";
|
||||||
|
PGPPublicKeyRing signingKeys = TestKeys.getEmilPublicKeyRing();
|
||||||
|
|
||||||
|
File tempDir = TestUtils.createTempDirectory();
|
||||||
|
File file = new File(tempDir, "file");
|
||||||
|
MultiPassStrategy multiPassStrategy = MultiPassStrategy.writeMessageToFile(file);
|
||||||
|
CleartextSignatureProcessor processor = PGPainless.verifyCleartextSignedMessage()
|
||||||
|
.onInputStream(new ByteArrayInputStream(signed.getBytes(StandardCharsets.UTF_8)))
|
||||||
|
.withStrategy(multiPassStrategy)
|
||||||
|
.verifyWith(signingKeys);
|
||||||
|
|
||||||
|
PGPSignature signature = processor.process();
|
||||||
|
|
||||||
|
assertEquals(signature.getKeyID(), signingKeys.getPublicKey().getKeyID());
|
||||||
|
FileInputStream fileIn = new FileInputStream(file);
|
||||||
|
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||||
|
Streams.pipeAll(fileIn, bytes);
|
||||||
|
fileIn.close();
|
||||||
|
assertArrayEquals(message.getBytes(StandardCharsets.UTF_8), bytes.toByteArray());
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -15,14 +15,20 @@
|
||||||
*/
|
*/
|
||||||
package org.pgpainless.util;
|
package org.pgpainless.util;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class TestUtils {
|
public class TestUtils {
|
||||||
|
|
||||||
public static SimpleDateFormat UTC_PARSER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
|
public static SimpleDateFormat UTC_PARSER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
|
||||||
|
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
|
|
||||||
public static Date getUTCDate(String dateString) {
|
public static Date getUTCDate(String dateString) {
|
||||||
try {
|
try {
|
||||||
|
@ -40,4 +46,19 @@ public class TestUtils {
|
||||||
}
|
}
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static File createTempDirectory() throws IOException {
|
||||||
|
String name = randomString(10);
|
||||||
|
File dir = Files.createTempDirectory(name).toFile();
|
||||||
|
// dir.deleteOnExit();
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String randomString(int length) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
sb.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length())));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue