mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2024-12-22 12:57:57 +01:00
Add more armor/dearmor tests
This commit is contained in:
parent
670aa0f242
commit
4726362df8
3 changed files with 189 additions and 8 deletions
|
@ -274,6 +274,7 @@ public class ExternalSOP implements SOP {
|
||||||
outputStream.write(buf, 0, r);
|
outputStream.write(buf, 0, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outputStream.flush();
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
ExternalSOP.finish(process);
|
ExternalSOP.finish(process);
|
||||||
}
|
}
|
||||||
|
@ -299,19 +300,17 @@ public class ExternalSOP implements SOP {
|
||||||
while ((r = standardIn.read(buf)) > 0) {
|
while ((r = standardIn.read(buf)) > 0) {
|
||||||
processOut.write(buf, 0, r);
|
processOut.write(buf, 0, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
standardIn.close();
|
standardIn.close();
|
||||||
try {
|
|
||||||
processOut.close();
|
processOut.flush();
|
||||||
} catch (IOException e) {
|
processOut.close();
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((r = processIn.read(buf)) > 0) {
|
while ((r = processIn.read(buf)) > 0) {
|
||||||
outputStream.write(buf, 0 , r);
|
outputStream.write(buf, 0 , r);
|
||||||
}
|
}
|
||||||
|
|
||||||
processIn.close();
|
processIn.close();
|
||||||
|
|
||||||
|
outputStream.flush();
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
|
|
||||||
finish(process);
|
finish(process);
|
||||||
|
|
|
@ -10,9 +10,11 @@ import org.junit.jupiter.api.condition.EnabledIf;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static sop.external.JUtils.arrayStartsWith;
|
import static sop.external.JUtils.arrayStartsWith;
|
||||||
import static sop.external.JUtils.assertArrayStartsWith;
|
import static sop.external.JUtils.assertArrayStartsWith;
|
||||||
|
import static sop.external.JUtils.assertAsciiArmorEquals;
|
||||||
|
|
||||||
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
|
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
|
||||||
public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
|
public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
|
||||||
|
@ -21,6 +23,10 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
|
||||||
private static final byte[] BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES = BEGIN_PGP_PRIVATE_KEY_BLOCK.getBytes(StandardCharsets.UTF_8);
|
private static final byte[] BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES = BEGIN_PGP_PRIVATE_KEY_BLOCK.getBytes(StandardCharsets.UTF_8);
|
||||||
private static final String BEGIN_PGP_PUBLIC_KEY_BLOCK = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n";
|
private static final String BEGIN_PGP_PUBLIC_KEY_BLOCK = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n";
|
||||||
private static final byte[] BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES = BEGIN_PGP_PUBLIC_KEY_BLOCK.getBytes(StandardCharsets.UTF_8);
|
private static final byte[] BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES = BEGIN_PGP_PUBLIC_KEY_BLOCK.getBytes(StandardCharsets.UTF_8);
|
||||||
|
private static final String BEGIN_PGP_MESSAGE = "-----BEGIN PGP MESSAGE-----\n";
|
||||||
|
private static final byte[] BEGIN_PGP_MESSAGE_BYTES = BEGIN_PGP_MESSAGE.getBytes(StandardCharsets.UTF_8);
|
||||||
|
private static final String BEGIN_PGP_SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n";
|
||||||
|
private static final byte[] BEGIN_PGP_SIGNATURE_BYTES = BEGIN_PGP_SIGNATURE.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dearmorArmorAliceKey() throws IOException {
|
public void dearmorArmorAliceKey() throws IOException {
|
||||||
|
@ -37,9 +43,9 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
|
||||||
.getBytes();
|
.getBytes();
|
||||||
|
|
||||||
assertArrayStartsWith(armored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES);
|
assertArrayStartsWith(armored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES);
|
||||||
|
assertAsciiArmorEquals(aliceKey, armored);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dearmorArmorAliceCert() throws IOException {
|
public void dearmorArmorAliceCert() throws IOException {
|
||||||
byte[] aliceCert = TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8);
|
byte[] aliceCert = TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8);
|
||||||
|
@ -55,5 +61,162 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
|
||||||
.getBytes();
|
.getBytes();
|
||||||
|
|
||||||
assertArrayStartsWith(armored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES);
|
assertArrayStartsWith(armored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES);
|
||||||
|
assertAsciiArmorEquals(aliceCert, armored);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dearmorArmorBobKey() throws IOException {
|
||||||
|
byte[] bobKey = TestKeys.BOB_KEY.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
byte[] dearmored = getSop().dearmor()
|
||||||
|
.data(bobKey)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES));
|
||||||
|
|
||||||
|
byte[] armored = getSop().armor()
|
||||||
|
.data(dearmored)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertArrayStartsWith(armored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES);
|
||||||
|
assertAsciiArmorEquals(bobKey, armored);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dearmorArmorBobCert() throws IOException {
|
||||||
|
byte[] bobCert = TestKeys.BOB_CERT.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
byte[] dearmored = getSop().dearmor()
|
||||||
|
.data(bobCert)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES));
|
||||||
|
|
||||||
|
byte[] armored = getSop().armor()
|
||||||
|
.data(dearmored)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertArrayStartsWith(armored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES);
|
||||||
|
assertAsciiArmorEquals(bobCert, armored);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dearmorArmorCarolKey() throws IOException {
|
||||||
|
byte[] carolKey = TestKeys.CAROL_KEY.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
byte[] dearmored = getSop().dearmor()
|
||||||
|
.data(carolKey)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES));
|
||||||
|
|
||||||
|
byte[] armored = getSop().armor()
|
||||||
|
.data(dearmored)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertArrayStartsWith(armored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES);
|
||||||
|
assertAsciiArmorEquals(carolKey, armored);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dearmorArmorCarolCert() throws IOException {
|
||||||
|
byte[] carolCert = TestKeys.CAROL_CERT.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
byte[] dearmored = getSop().dearmor()
|
||||||
|
.data(carolCert)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES));
|
||||||
|
|
||||||
|
byte[] armored = getSop().armor()
|
||||||
|
.data(dearmored)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertArrayStartsWith(armored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES);
|
||||||
|
assertAsciiArmorEquals(carolCert, armored);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dearmorArmorMessage() throws IOException {
|
||||||
|
ignoreIf("sqop", Is.leq, "0.26.1"); // falsely reports Invalid Data Type
|
||||||
|
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" +
|
||||||
|
"\n" +
|
||||||
|
"wV4DR2b2udXyHrYSAQdAMZy9Iqb1IxszjI3v+TsfK//0lnJ9PKHDqVAB5ohp+RMw\n" +
|
||||||
|
"8fmuL3phS9uISFT/DrizC8ALJhMqw5R+lLB/RvTTA/qS6tN5dRyL+YLFU3/N0CRF\n" +
|
||||||
|
"0j8BtQEsMmRo60LzUq/OBI0dFjwFq1efpfOGkpRYkuIzndCjBEgnLUkrHzUc1uD9\n" +
|
||||||
|
"CePQFpprprnGEzpE3flQLUc=\n" +
|
||||||
|
"=ZiFR\n" +
|
||||||
|
"-----END PGP MESSAGE-----\n").getBytes(StandardCharsets.UTF_8);
|
||||||
|
byte[] dearmored = getSop().dearmor()
|
||||||
|
.data(message)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_MESSAGE_BYTES));
|
||||||
|
|
||||||
|
byte[] armored = getSop().armor()
|
||||||
|
.data(dearmored)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertArrayStartsWith(armored, BEGIN_PGP_MESSAGE_BYTES);
|
||||||
|
assertAsciiArmorEquals(message, armored);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dearmorArmorSignature() throws IOException {
|
||||||
|
byte[] signature = ("-----BEGIN PGP SIGNATURE-----\n" +
|
||||||
|
"\n" +
|
||||||
|
"wr0EABYKAG8FgmPBdRAJEPIxVQxPR+OORxQAAAAAAB4AIHNhbHRAbm90YXRpb25z\n" +
|
||||||
|
"LnNlcXVvaWEtcGdwLm9yZ2un17fF3C46Adgzp0mU4RG8Txy/T/zOBcBw/NYaLGrQ\n" +
|
||||||
|
"FiEE64W7X6M6deFelE5j8jFVDE9H444AAMiEAP9LBQWLo4oP5IrFZPuSUQSPsUxB\n" +
|
||||||
|
"c+Qu1raXDKzS/8Q9IAD+LnHIjRHcqNPobNHXF/saXIYXeZR+LJKszTJozzwqdQE=\n" +
|
||||||
|
"=GHvQ\n" +
|
||||||
|
"-----END PGP SIGNATURE-----\n").getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
byte[] dearmored = getSop().dearmor()
|
||||||
|
.data(signature)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_SIGNATURE_BYTES));
|
||||||
|
|
||||||
|
byte[] armored = getSop().armor()
|
||||||
|
.data(dearmored)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertArrayStartsWith(armored, BEGIN_PGP_SIGNATURE_BYTES);
|
||||||
|
assertAsciiArmorEquals(signature, armored);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDearmoringTwiceIsIdempotent() throws IOException {
|
||||||
|
ignoreIf("sqop", Is.eq, "0.27.2"); // IO error because: EOF
|
||||||
|
|
||||||
|
byte[] dearmored = getSop().dearmor()
|
||||||
|
.data(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
byte[] dearmoredAgain = getSop().dearmor()
|
||||||
|
.data(dearmored)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertArrayEquals(dearmored, dearmoredAgain);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testArmoringTwiceIsIdempotent() throws IOException {
|
||||||
|
byte[] armored = ("-----BEGIN PGP SIGNATURE-----\n" +
|
||||||
|
"\n" +
|
||||||
|
"wr0EABYKAG8FgmPBdRAJEPIxVQxPR+OORxQAAAAAAB4AIHNhbHRAbm90YXRpb25z\n" +
|
||||||
|
"LnNlcXVvaWEtcGdwLm9yZ2un17fF3C46Adgzp0mU4RG8Txy/T/zOBcBw/NYaLGrQ\n" +
|
||||||
|
"FiEE64W7X6M6deFelE5j8jFVDE9H444AAMiEAP9LBQWLo4oP5IrFZPuSUQSPsUxB\n" +
|
||||||
|
"c+Qu1raXDKzS/8Q9IAD+LnHIjRHcqNPobNHXF/saXIYXeZR+LJKszTJozzwqdQE=\n" +
|
||||||
|
"=GHvQ\n" +
|
||||||
|
"-----END PGP SIGNATURE-----\n").getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
byte[] armoredAgain = getSop().armor()
|
||||||
|
.data(armored)
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
|
assertAsciiArmorEquals(armored, armoredAgain);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,10 @@
|
||||||
|
|
||||||
package sop.external;
|
package sop.external;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
public class JUtils {
|
public class JUtils {
|
||||||
|
@ -50,4 +52,21 @@ public class JUtils {
|
||||||
"Actual: <" + Arrays.toString(actual) + ">");
|
"Actual: <" + Arrays.toString(actual) + ">");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void assertAsciiArmorEquals(byte[] first, byte[] second) {
|
||||||
|
byte[] firstCleaned = removeArmorHeaders(first);
|
||||||
|
byte[] secondCleaned = removeArmorHeaders(second);
|
||||||
|
|
||||||
|
assertArrayEquals(firstCleaned, secondCleaned);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] removeArmorHeaders(byte[] armor) {
|
||||||
|
String string = new String(armor, StandardCharsets.UTF_8);
|
||||||
|
string = string.replaceAll("Comment: .+\\R", "")
|
||||||
|
.replaceAll("Version: .+\\R", "")
|
||||||
|
.replaceAll("MessageID: .+\\R", "")
|
||||||
|
.replaceAll("Hash: .+\\R", "")
|
||||||
|
.replaceAll("Charset: .+\\R", "");
|
||||||
|
return string.getBytes(StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue