handle one-octet length headers in public key block reformatting

This commit is contained in:
Vincent Breitmoser
2017-09-19 15:02:40 +02:00
parent 534f816e35
commit 90b7a0f4f1
2 changed files with 63 additions and 7 deletions

View File

@@ -17,7 +17,7 @@ import static junit.framework.Assert.assertNotNull;
@RunWith(KeychainTestRunner.class)
public class PgpHelperTest {
static final String INPUT_KEY_BLOCK = "-----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v2 " +
static final String INPUT_KEY_BLOCK_TWO_OCTET_LENGTH = "-----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v2 " +
"mQENBFnA7Y0BCAC+pdQ1mV9QguWvAyMsKiKkzeP5VxbIDUyQ8OBDFKIrZKZGTzjZ " +
"xvZs22j5pXWYlyDi4HU4+nuQmAMo6jxJMKQQkW7Y5AmRYijboLN+lZ8L28bYJC4o " +
"PcAnS3xlib2lE7aNK2BRUbctkhahhb2hohAxiXTUdGmfr9sHgZ2+B9sPTfuhrvtI " +
@@ -44,10 +44,23 @@ public class PgpHelperTest {
"O/eTJGOfMl6hC4rRxRUbM+piZzbYcQ0lO3R2yPlEwzlO+asM9820V9bkviJUrXiY " +
"c5EX44mwFdhpXuHbRS18DJjCVcMhEsPG6rQ0Qy/6/dafow5HExRBmZl6ZkfjR2Lb " +
"alOZH0SNi47bvn6QKqKgiqT4f9mImyEDtSj/ =2V66 -----END PGP PUBLIC KEY BLOCK-----";
static final String INPUT_KEY_BLOCK_ONE_OCTET_LENGTH = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
"\n" +
"mFIEVk2iwRMIKoZIzj0DAQcCAwTBaWEpVYOfZDm85s/zszd4J4CBW8FesYiYQTeX\n" +
"5WMtwXsqrG5/ZcIgHNBzI0EvUbm/oSBFUJNk7RhmOk6MpS2gtAdNci4gRUNDiHkE\n" +
"ExMIACEFAlZNosECGwMFCwkIBwIGFQgJCgsCBBYCAwECHgECF4AACgkQt60Zc7T/\n" +
"SfQTPAD/bZ0ld3UyqAt8oPoHyJduGMkbur5KYoht1w/MMtiogG0BAN8Anhy55kTe\n" +
"H4VmMWxzK9M+kIFPzqEVHOzsuE5nhJOouFYEVk2iwRIIKoZIzj0DAQcCAwSvfTrq\n" +
"kkVeD0cVM8FZwhjTaG+B9wgk7yeoMgjIrSuZLiRjGAYC7Kq+6OiczduoItC2oMuK\n" +
"GpymTF6t+CmQpUfuAwEIB4hhBBgTCAAJBQJWTaLBAhsMAAoJELetGXO0/0n00BwA\n" +
"/2d1w/A4xMwfIFrKDwHeHALUBaIOuhF2AKd/43HujmuLAQDdcWf3h/0zjgBTjSoB\n" +
"bcVr5AE/huKUnwKYa7SP7wzoZg==\n" +
"=ou9N\n" +
"-----END PGP PUBLIC KEY BLOCK-----\n";
@Test
public void reformatPgpPublicKeyBlock() throws Exception {
String reformattedKey = PgpHelper.reformatPgpPublicKeyBlock(INPUT_KEY_BLOCK);
String reformattedKey = PgpHelper.reformatPgpPublicKeyBlock(INPUT_KEY_BLOCK_TWO_OCTET_LENGTH);
assertNotNull(reformattedKey);
UncachedKeyRing.decodeFromData(reformattedKey.getBytes());
@@ -55,7 +68,8 @@ public class PgpHelperTest {
@Test
public void reformatPgpPublicKeyBlock_consecutiveKeys() throws Exception {
String reformattedKey = PgpHelper.reformatPgpPublicKeyBlock(INPUT_KEY_BLOCK + INPUT_KEY_BLOCK);
String reformattedKey = PgpHelper.reformatPgpPublicKeyBlock(
INPUT_KEY_BLOCK_TWO_OCTET_LENGTH + INPUT_KEY_BLOCK_TWO_OCTET_LENGTH);
assertNotNull(reformattedKey);
IteratorWithIOThrow<UncachedKeyRing> uncachedKeyRingIteratorWithIOThrow =
@@ -67,10 +81,18 @@ public class PgpHelperTest {
@Test
public void reformatPgpPublicKeyBlock_shouldBeIdempotent() throws Exception {
String reformattedKey1 = PgpHelper.reformatPgpPublicKeyBlock(INPUT_KEY_BLOCK);
String reformattedKey2 = PgpHelper.reformatPgpPublicKeyBlock(reformattedKey1);
String reformattedKey1 = PgpHelper.reformatPgpPublicKeyBlock(INPUT_KEY_BLOCK_TWO_OCTET_LENGTH);
assertNotNull(reformattedKey1);
String reformattedKey2 = PgpHelper.reformatPgpPublicKeyBlock(reformattedKey1);
assertEquals(reformattedKey1, reformattedKey2);
}
@Test
public void reformatPgpPublicKeyBlock_withOneOctetLengthHeader() throws Exception {
String reformattedKey = PgpHelper.reformatPgpPublicKeyBlock(INPUT_KEY_BLOCK_ONE_OCTET_LENGTH);
assertNotNull(reformattedKey);
UncachedKeyRing.decodeFromData(reformattedKey.getBytes());
}
}