use autovalue for PromoteKeyringParcel

This commit is contained in:
Vincent Breitmoser
2017-05-22 12:24:38 +02:00
parent 820a308ba0
commit 0095df6d89
4 changed files with 29 additions and 52 deletions

View File

@@ -38,7 +38,6 @@ import org.sufficientlysecure.keychain.provider.KeyWritableRepository;
import org.sufficientlysecure.keychain.service.PromoteKeyringParcel; import org.sufficientlysecure.keychain.service.PromoteKeyringParcel;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.ProgressScaler;
/** An operation which promotes a public key ring to a secret one. /** An operation which promotes a public key ring to a secret one.
* *
@@ -67,17 +66,18 @@ public class PromoteKeyOperation extends BaseReadWriteOperation<PromoteKeyringPa
try { try {
log.add(LogType.MSG_PR_FETCHING, 1, log.add(LogType.MSG_PR_FETCHING, 1,
KeyFormattingUtils.convertKeyIdToHex(promoteKeyringParcel.mKeyRingId)); KeyFormattingUtils.convertKeyIdToHex(promoteKeyringParcel.getMasterKeyId()));
CanonicalizedPublicKeyRing pubRing = CanonicalizedPublicKeyRing pubRing =
mKeyRepository.getCanonicalizedPublicKeyRing(promoteKeyringParcel.mKeyRingId); mKeyRepository.getCanonicalizedPublicKeyRing(promoteKeyringParcel.getMasterKeyId());
if (promoteKeyringParcel.mSubKeyIds == null) { long[] subKeyIds = promoteKeyringParcel.getSubKeyIds();
if (subKeyIds == null) {
log.add(LogType.MSG_PR_ALL, 1); log.add(LogType.MSG_PR_ALL, 1);
} else { } else {
// sort for binary search // sort for binary search
for (CanonicalizedPublicKey key : pubRing.publicKeyIterator()) { for (CanonicalizedPublicKey key : pubRing.publicKeyIterator()) {
long subKeyId = key.getKeyId(); long subKeyId = key.getKeyId();
if (naiveIndexOf(promoteKeyringParcel.mSubKeyIds, subKeyId) != null) { if (naiveIndexOf(subKeyIds, subKeyId) != null) {
log.add(LogType.MSG_PR_SUBKEY_MATCH, 1, log.add(LogType.MSG_PR_SUBKEY_MATCH, 1,
KeyFormattingUtils.convertKeyIdToHex(subKeyId)); KeyFormattingUtils.convertKeyIdToHex(subKeyId));
} else { } else {
@@ -88,8 +88,7 @@ public class PromoteKeyOperation extends BaseReadWriteOperation<PromoteKeyringPa
} }
// create divert-to-card secret key from public key // create divert-to-card secret key from public key
promotedRing = pubRing.createDivertSecretRing(promoteKeyringParcel.mCardAid, promotedRing = pubRing.createDivertSecretRing(promoteKeyringParcel.getCardAid(), subKeyIds);
promoteKeyringParcel.mSubKeyIds);
} catch (NotFoundException e) { } catch (NotFoundException e) {
log.add(LogType.MSG_PR_ERROR_KEY_NOT_FOUND, 2); log.add(LogType.MSG_PR_ERROR_KEY_NOT_FOUND, 2);

View File

@@ -19,48 +19,23 @@
package org.sufficientlysecure.keychain.service; package org.sufficientlysecure.keychain.service;
import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.Nullable;
public class PromoteKeyringParcel implements Parcelable { import com.google.auto.value.AutoValue;
public long mKeyRingId;
public byte[] mCardAid;
public long[] mSubKeyIds;
public PromoteKeyringParcel(long keyRingId, byte[] cardAid, long[] subKeyIds) { @AutoValue
mKeyRingId = keyRingId; public abstract class PromoteKeyringParcel implements Parcelable {
mCardAid = cardAid; public abstract long getMasterKeyId();
mSubKeyIds = subKeyIds; @Nullable
public abstract byte[] getCardAid();
@Nullable
public abstract long[] getSubKeyIds();
public static PromoteKeyringParcel createPromoteKeyringParcel(long keyRingId, byte[] cardAid,
@Nullable long[] subKeyIds) {
return new AutoValue_PromoteKeyringParcel(keyRingId, cardAid, subKeyIds);
} }
protected PromoteKeyringParcel(Parcel in) {
mKeyRingId = in.readLong();
mCardAid = in.createByteArray();
mSubKeyIds = in.createLongArray();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(mKeyRingId);
dest.writeByteArray(mCardAid);
dest.writeLongArray(mSubKeyIds);
}
public static final Parcelable.Creator<PromoteKeyringParcel> CREATOR = new Parcelable.Creator<PromoteKeyringParcel>() {
@Override
public PromoteKeyringParcel createFromParcel(Parcel in) {
return new PromoteKeyringParcel(in);
}
@Override
public PromoteKeyringParcel[] newArray(int size) {
return new PromoteKeyringParcel[size];
}
};
} }

View File

@@ -213,7 +213,7 @@ public class ViewKeySecurityTokenFragment
@Override @Override
public PromoteKeyringParcel createOperationInput() { public PromoteKeyringParcel createOperationInput() {
return new PromoteKeyringParcel(mMasterKeyId, mCardAid, mSubKeyIds); return PromoteKeyringParcel.createPromoteKeyringParcel(mMasterKeyId, mCardAid, mSubKeyIds);
} }
@Override @Override

View File

@@ -106,7 +106,8 @@ public class PromoteKeyOperationTest {
PromoteKeyOperation op = new PromoteKeyOperation(RuntimeEnvironment.application, PromoteKeyOperation op = new PromoteKeyOperation(RuntimeEnvironment.application,
KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null, null); KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null, null);
PromoteKeyResult result = op.execute(new PromoteKeyringParcel(mStaticRing.getMasterKeyId(), null, null), null); PromoteKeyResult result = op.execute(
PromoteKeyringParcel.createPromoteKeyringParcel(mStaticRing.getMasterKeyId(), null, null), null);
Assert.assertTrue("promotion must succeed", result.success()); Assert.assertTrue("promotion must succeed", result.success());
@@ -132,7 +133,8 @@ public class PromoteKeyOperationTest {
byte[] aid = Hex.decode("D2760001240102000000012345670000"); byte[] aid = Hex.decode("D2760001240102000000012345670000");
PromoteKeyResult result = op.execute(new PromoteKeyringParcel(mStaticRing.getMasterKeyId(), aid, null), null); PromoteKeyResult result = op.execute(
PromoteKeyringParcel.createPromoteKeyringParcel(mStaticRing.getMasterKeyId(), aid, null), null);
Assert.assertTrue("promotion must succeed", result.success()); Assert.assertTrue("promotion must succeed", result.success());
@@ -160,9 +162,10 @@ public class PromoteKeyOperationTest {
// only promote the first, rest stays dummy // only promote the first, rest stays dummy
long keyId = KeyringTestingHelper.getSubkeyId(mStaticRing, 1); long keyId = KeyringTestingHelper.getSubkeyId(mStaticRing, 1);
PromoteKeyResult result = op.execute(new PromoteKeyringParcel(mStaticRing.getMasterKeyId(), aid, new long[] { PromoteKeyResult result = op.execute(
keyId PromoteKeyringParcel.createPromoteKeyringParcel(mStaticRing.getMasterKeyId(), aid, new long[] {
}), null); keyId
}), null);
Assert.assertTrue("promotion must succeed", result.success()); Assert.assertTrue("promotion must succeed", result.success());