From cb58f75f68e4c4c4984838e61157f53745c3616d Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 30 Apr 2017 18:14:58 +0200 Subject: [PATCH] Add test for cv25519 decryption --- .../keychain/provider/Cv25519Test.java | 105 ++++++++++++++++++ .../resources/test-keys/cv25519-encrypted.asc | 8 ++ .../resources/test-keys/cv25519-key.sec.asc | 14 +++ 3 files changed, 127 insertions(+) create mode 100644 OpenKeychain/src/test/java/org/sufficientlysecure/keychain/provider/Cv25519Test.java create mode 100644 OpenKeychain/src/test/resources/test-keys/cv25519-encrypted.asc create mode 100644 OpenKeychain/src/test/resources/test-keys/cv25519-key.sec.asc diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/provider/Cv25519Test.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/provider/Cv25519Test.java new file mode 100644 index 000000000..fa0726e6f --- /dev/null +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/provider/Cv25519Test.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2017 Schürmann & Breitmoser GbR + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.provider; + + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +import android.app.Application; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openintents.openpgp.OpenPgpSignatureResult; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.shadows.ShadowLog; +import org.robolectric.util.Util; +import org.sufficientlysecure.keychain.KeychainTestRunner; +import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult; +import org.sufficientlysecure.keychain.operations.results.SaveKeyringResult; +import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel; +import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyOperation; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertArrayEquals; + + +@SuppressWarnings("WeakerAccess") +@RunWith(KeychainTestRunner.class) +public class Cv25519Test { + public static final String ENCRYPTED_PLAINTEXT = "hi\n"; + private KeyWritableRepository keyRepository; + private Application context; + + + @BeforeClass + public static void setUpOnce() throws Exception { + ShadowLog.stream = System.out; + } + + @Before + public void setUp() throws Exception { + context = RuntimeEnvironment.application; + keyRepository = KeyWritableRepository.create(context); + + } + + @Test + public void testDecryptX25519() throws Exception { + loadSecretKeyringFromResource("/test-keys/cv25519-key.sec.asc"); + + byte[] encryptedText = readBytesFromResource("/test-keys/cv25519-encrypted.asc"); + PgpDecryptVerifyInputParcel pgpDecryptVerifyInputParcel = + PgpDecryptVerifyInputParcel.builder().setInputBytes(encryptedText).build(); + + PgpDecryptVerifyOperation decryptVerifyOperation = new PgpDecryptVerifyOperation(context, keyRepository, null); + DecryptVerifyResult result = decryptVerifyOperation.execute(pgpDecryptVerifyInputParcel, CryptoInputParcel.createCryptoInputParcel()); + + assertTrue(result.success()); + assertEquals(OpenPgpSignatureResult.RESULT_NO_SIGNATURE, result.getSignatureResult().getResult()); + assertArrayEquals(ENCRYPTED_PLAINTEXT.getBytes(), result.getOutputBytes()); + } + + private UncachedKeyRing loadSecretKeyringFromResource(String name) throws Exception { + UncachedKeyRing ring = readRingFromResource(name); + SaveKeyringResult saveKeyringResult = keyRepository.saveSecretKeyRing(ring); + assertTrue(saveKeyringResult.success()); + assertFalse(saveKeyringResult.getLog().containsWarnings()); + return ring; + } + + private byte[] readBytesFromResource(String name) throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + InputStream input = Cv25519Test.class.getResourceAsStream(name); + + Util.copy(input, baos); + + return baos.toByteArray(); + } + + UncachedKeyRing readRingFromResource(String name) throws Exception { + return UncachedKeyRing.fromStream(Cv25519Test.class.getResourceAsStream(name)).next(); + } + +} \ No newline at end of file diff --git a/OpenKeychain/src/test/resources/test-keys/cv25519-encrypted.asc b/OpenKeychain/src/test/resources/test-keys/cv25519-encrypted.asc new file mode 100644 index 000000000..6907c57ed --- /dev/null +++ b/OpenKeychain/src/test/resources/test-keys/cv25519-encrypted.asc @@ -0,0 +1,8 @@ +-----BEGIN PGP MESSAGE----- + +hF4DJy6x5ZTOXfMSAQdAuUEh4g2wNp18u9jQk3K64tSNbpkLX2CJXzJiNeGV4TQw +k7OjaIka09odhhejsmD3nRg6LCKmUUJEJsE1iCExGTsOyw4wNbwABmoGcpcCV0me +0j4Bl7TlhKMWbtYOcSDn8rqHa4hJnMQZnIFt+L3qznmn8bIW6Y/4N3zmIMAUhSuE +6Kcc//vPlucognM99zQOHQ== +=iBYu +-----END PGP MESSAGE----- \ No newline at end of file diff --git a/OpenKeychain/src/test/resources/test-keys/cv25519-key.sec.asc b/OpenKeychain/src/test/resources/test-keys/cv25519-key.sec.asc new file mode 100644 index 000000000..22860a38b --- /dev/null +++ b/OpenKeychain/src/test/resources/test-keys/cv25519-key.sec.asc @@ -0,0 +1,14 @@ +-----BEGIN PGP PRIVATE KEY BLOCK----- + +lFgEWQURixYJKwYBBAHaRw8BAQdAIpQDhaf1xZgQNvvaQgWbO4z7opG27HbU9GO/ +5xPTwwQAAQCpAW8unz7f9EVO4/bzZ6W+EGu5A2WQ2Wscpqa6IFmGUhAEtAtNci4g +Q3YyNTUxOYiQBBMWCAA4FiEEygk6J8zDABl0THee927KXOCa9BUFAlkFEYsCGwMF +CwkIBwIGFQgJCgsCBBYCAwECHgECF4AACgkQ927KXOCa9BWxaQD/ZlBhwhm9x2+A +yxEKK90WmS0IAFf5UtWm/IGkvkzOHnsA/1y8WNwyveHOPP/5BKWaR/oxFh0w23mW +reNN428DxlgJnF0EWQURixIKKwYBBAGXVQEFAQEHQH/8G39ToD7jkPCTORsFiwl7 +8Q2b/EBc0xi2yOeZlYxCAwEIBwAA/0WvwrkkrWbDTdBQj0qsVo+LizwVT3rkQQS3 +lMdVHf2IEKGIeAQYFggAIBYhBMoJOifMwwAZdEx3nvduylzgmvQVBQJZBRGLAhsM +AAoJEPduylzgmvQVoPQA/R7P/kW99jFqmDDEZkFTHYDbHpjnyXOMjFg3hSs2BJZP +AQDl71K7mx2R/zsS2l0+dhktkD9l2Lmb75egpzy2il3vBA== +=T0w3 +-----END PGP PRIVATE KEY BLOCK----- \ No newline at end of file