aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/PgpVerifyTestingHelper.java
blob: d5edae5f07e938dac65beed0cf06d92c12e9fc3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.sufficientlysecure.keychain.testsupport;

import android.content.Context;
import android.net.Uri;

import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyResult;
import org.sufficientlysecure.keychain.pgp.WrappedPublicKeyRing;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.util.InputData;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * For functional tests of PgpDecryptVerify
 */
public class PgpVerifyTestingHelper {

    private final Context context;

    public PgpVerifyTestingHelper(Context robolectricContext) {
            this.context=robolectricContext;
    }

    public int doTestFile(String testFileName) throws Exception {
        ProviderHelper providerHelper = new ProviderHelperStub(context);

        PgpDecryptVerify.PassphraseCache passphraseCache = new PgpDecryptVerify.PassphraseCache() {
            public String getCachedPassphrase(long masterKeyId) {
                return "I am a passphrase";
            }
        };

        InputStream sampleInput = getClass().getResourceAsStream(testFileName);
        assert null != sampleInput;
        InputData data = new InputData(sampleInput, 705);
        OutputStream outStream = new ByteArrayOutputStream();

        PgpDecryptVerify verify = new PgpDecryptVerify.Builder(providerHelper, passphraseCache, data, outStream).build();
        PgpDecryptVerifyResult result = verify.execute();

        return result.getSignatureResult().getStatus();
    }


    static class ProviderHelperStub extends ProviderHelper {
        public ProviderHelperStub(Context context) {
            super(context);
        }

        @Override
        public WrappedPublicKeyRing getWrappedPublicKeyRing(Uri id) throws NotFoundException {
            try {
                byte[] data = readFully(getClass().getResourceAsStream("/public-key-for-sample.blob"));
                return new WrappedPublicKeyRing(data, false, 0);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static byte[] readFully(InputStream input) throws IOException
    {
        byte[] buffer = new byte[8192];
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        while ((bytesRead = input.read(buffer)) != -1)
        {
            output.write(buffer, 0, bytesRead);
        }
        return output.toByteArray();
    }




}