From 13f785d0b04582bf4ccde97ab21eef40824df09d Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 22 Jun 2014 20:40:29 +0100 Subject: borrow tests from Haskell OpenPGP --- .../keychain/testsupport/KeyringTestingHelper.java | 10 +++++----- .../keychain/testsupport/TestDataUtil.java | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java index d4bc6c541..a565f0707 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java @@ -5,9 +5,10 @@ import android.content.Context; import org.sufficientlysecure.keychain.pgp.NullProgressable; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.provider.ProviderHelper; -import org.sufficientlysecure.keychain.remote.AppSettings; import org.sufficientlysecure.keychain.service.OperationResults; +import java.util.Collection; + /** * Helper for tests of the Keyring import in ProviderHelper. */ @@ -19,13 +20,11 @@ public class KeyringTestingHelper { this.context = robolectricContext; } - public boolean addKeyring() throws Exception { + public boolean addKeyring(Collection blobFiles) throws Exception { ProviderHelper providerHelper = new ProviderHelper(context); -// providerHelper.insertApiApp(new AppSettings("robo-test-package", new byte[]{5, 4, 3, 2, 1})); - - byte[] data = TestDataUtil.readFully(getClass().getResourceAsStream("/public-key-for-sample.blob")); + byte[] data = TestDataUtil.readAllFully(blobFiles); UncachedKeyRing ring = UncachedKeyRing.decodeFromData(data); long masterKeyId = ring.getMasterKeyId(); @@ -45,6 +44,7 @@ public class KeyringTestingHelper { return saveSuccess; } + private void retrieveKeyAndExpectNotFound(ProviderHelper providerHelper, long masterKeyId) { try { providerHelper.getWrappedPublicKeyRing(masterKeyId); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java index 06dc08eab..9e6ede761 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java @@ -3,6 +3,7 @@ package org.sufficientlysecure.keychain.testsupport; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Collection; /** * Misc support functions. Would just use Guava / Apache Commons but @@ -10,9 +11,14 @@ import java.io.InputStream; */ public class TestDataUtil { public static byte[] readFully(InputStream input) { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + appendToOutput(input, output); + return output.toByteArray(); + } + + private static void appendToOutput(InputStream input, ByteArrayOutputStream output) { byte[] buffer = new byte[8192]; int bytesRead; - ByteArrayOutputStream output = new ByteArrayOutputStream(); try { while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); @@ -20,6 +26,19 @@ public class TestDataUtil { } catch (IOException e) { throw new RuntimeException(e); } + } + + public static byte[] readAllFully(Collection inputResources) { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + + for (String inputResource : inputResources) { + appendToOutput(getResourceAsStream(inputResource), output); + } return output.toByteArray(); } + + + public static InputStream getResourceAsStream(String resourceName) { + return TestDataUtil.class.getResourceAsStream(resourceName); + } } -- cgit v1.2.3