add getSecretKeyType method to CachedPublicKeyRing

This commit is contained in:
Vincent Breitmoser 2014-09-03 01:22:31 +02:00
parent 35962cd254
commit e9b14585f5
3 changed files with 46 additions and 17 deletions

View File

@ -29,7 +29,10 @@ import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedPublicKey;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.service.OperationResults.SaveKeyringResult;
import org.sufficientlysecure.keychain.support.KeyringTestingHelper;
import org.sufficientlysecure.keychain.util.ProgressScaler;
import java.io.IOException;
@ -39,6 +42,8 @@ import java.util.Iterator;
@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19
public class ProviderHelperSaveTest {
ProviderHelper mProviderHelper = new ProviderHelper(Robolectric.application);
@BeforeClass
public static void setUpOnce() throws Exception {
ShadowLog.stream = System.out;
@ -81,28 +86,22 @@ public class ProviderHelperSaveTest {
SaveKeyringResult result;
// insert both keys, second should fail
result = new ProviderHelper(Robolectric.application).savePublicKeyRing(pub);
result = mProviderHelper.savePublicKeyRing(pub);
Assert.assertTrue("import of public keyring should succeed", result.success());
result = new ProviderHelper(Robolectric.application).saveSecretKeyRing(sec,
new ProgressScaler());
result = mProviderHelper.saveSecretKeyRing(sec, new ProgressScaler());
Assert.assertTrue("import of secret keyring should succeed", result.success());
CanonicalizedSecretKeyRing secRing =
new ProviderHelper(Robolectric.application).getCanonicalizedSecretKeyRing(keyId);
// make sure both the CanonicalizedSecretKeyRing as well as the CachedPublicKeyRing correctly
// indicate the secret key type
CachedPublicKeyRing cachedRing = mProviderHelper.getCachedPublicKeyRing(keyId);
CanonicalizedSecretKeyRing secRing = mProviderHelper.getCanonicalizedSecretKeyRing(keyId);
for (CanonicalizedSecretKey key : secRing.secretKeyIterator()) {
Assert.assertEquals("all subkeys should be divert-to-key",
Assert.assertEquals("all subkeys from CanonicalizedSecretKeyRing should be divert-to-key",
SecretKeyType.DIVERT_TO_CARD, key.getSecretKeyType());
Assert.assertEquals("all subkeys from CachedPublicKeyRing should be divert-to-key",
SecretKeyType.DIVERT_TO_CARD, cachedRing.getSecretKeyType(key.getKeyId()));
}
/*
CachedPublicKeyRing cachedRing =
new ProviderHelper(Robolectric.application).getCachedPublicKeyRing(keyId);
for (CanonicalizedSecretKey key : cachedRing.()) {
Assert.assertEquals("all subkeys should be divert-to-key",
SecretKeyType.DIVERT_TO_CARD, key.getSecretKeyType());
}
*/
}
UncachedKeyRing readRingFromResource(String name) throws Exception {

View File

@ -22,8 +22,11 @@ import android.database.Cursor;
import android.net.Uri;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainContract.Keys;
import org.sufficientlysecure.keychain.util.Log;
/** This implementation of KeyRing provides a cached view of PublicKeyRing
@ -222,4 +225,17 @@ public class CachedPublicKeyRing extends KeyRing {
Uri keysUri = KeychainContract.Keys.buildKeysUri(extractOrGetMasterKeyId());
return mProviderHelper.getContentResolver().query(keysUri, null, null, null, null);
}
public SecretKeyType getSecretKeyType(long keyId) throws PgpGeneralException {
try {
Object data = mProviderHelper.getGenericData(Keys.buildKeysUri(mUri),
KeyRings.HAS_SECRET,
ProviderHelper.FIELD_TYPE_INTEGER,
KeyRings.KEY_ID + " = " + Long.toString(keyId));
return SecretKeyType.fromNum(((Long) data).intValue());
} catch(ProviderHelper.NotFoundException e) {
throw new PgpGeneralException(e);
}
}
}

View File

@ -141,12 +141,22 @@ public class ProviderHelper {
public static final int FIELD_TYPE_BLOB = 5;
public Object getGenericData(Uri uri, String column, int type) throws NotFoundException {
return getGenericData(uri, new String[]{column}, new int[]{type}).get(column);
return getGenericData(uri, new String[]{column}, new int[]{type}, null).get(column);
}
public Object getGenericData(Uri uri, String column, int type, String selection)
throws NotFoundException {
return getGenericData(uri, new String[]{column}, new int[]{type}, selection).get(column);
}
public HashMap<String, Object> getGenericData(Uri uri, String[] proj, int[] types)
throws NotFoundException {
return getGenericData(uri, proj, types, null);
}
public HashMap<String, Object> getGenericData(Uri uri, String[] proj, int[] types, String selection)
throws NotFoundException {
Cursor cursor = mContentResolver.query(uri, proj, null, null, null);
Cursor cursor = mContentResolver.query(uri, proj, selection, null, null);
try {
HashMap<String, Object> result = new HashMap<String, Object>(proj.length);
@ -223,6 +233,10 @@ public class ProviderHelper {
return new CachedPublicKeyRing(this, queryUri);
}
public CachedPublicKeyRing getCachedPublicKeyRing(long id) {
return new CachedPublicKeyRing(this, KeyRings.buildUnifiedKeyRingUri(id));
}
public CanonicalizedPublicKeyRing getCanonicalizedPublicKeyRing(long id) throws NotFoundException {
return (CanonicalizedPublicKeyRing) getCanonicalizedKeyRing(KeyRings.buildUnifiedKeyRingUri(id), false);
}