2023-01-09 14:56:53 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.external;
|
|
|
|
|
2023-01-22 16:53:50 +01:00
|
|
|
import org.junit.jupiter.api.condition.EnabledIf;
|
2023-01-22 16:47:44 +01:00
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
|
|
|
import sop.SOP;
|
2023-01-27 00:35:38 +01:00
|
|
|
import sop.testing.JUtils;
|
2023-01-09 14:56:53 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2023-01-09 19:48:25 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
2023-01-27 00:35:38 +01:00
|
|
|
import static sop.testing.JUtils.assertArrayEndsWithIgnoreNewlines;
|
|
|
|
import static sop.testing.JUtils.assertArrayStartsWith;
|
|
|
|
import static sop.testing.TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK;
|
|
|
|
import static sop.testing.TestData.END_PGP_PRIVATE_KEY_BLOCK;
|
2023-01-09 14:56:53 +01:00
|
|
|
|
2023-01-22 16:53:50 +01:00
|
|
|
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends")
|
2023-01-09 14:56:53 +01:00
|
|
|
public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void generateKeyTest(SOP sop) throws IOException {
|
|
|
|
byte[] key = sop.generateKey()
|
2023-01-09 19:48:25 +01:00
|
|
|
.userId("Alice <alice@openpgp.org>")
|
|
|
|
.generate()
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK);
|
|
|
|
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK);
|
2023-01-09 19:48:25 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void generateKeyNoArmor(SOP sop) throws IOException {
|
|
|
|
byte[] key = sop.generateKey()
|
2023-01-09 19:48:25 +01:00
|
|
|
.userId("Alice <alice@openpgp.org>")
|
|
|
|
.noArmor()
|
|
|
|
.generate()
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
assertFalse(JUtils.arrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK));
|
2023-01-09 19:48:25 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void generateKeyWithMultipleUserIdsTest(SOP sop) throws IOException {
|
|
|
|
byte[] key = sop.generateKey()
|
2023-01-09 19:48:25 +01:00
|
|
|
.userId("Alice <alice@openpgp.org>")
|
|
|
|
.userId("Bob <bob@openpgp.org>")
|
|
|
|
.generate()
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK);
|
|
|
|
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK);
|
2023-01-09 19:48:25 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void generateKeyWithoutUserIdTest(SOP sop) throws IOException {
|
|
|
|
byte[] key = sop.generateKey()
|
2023-01-09 19:48:25 +01:00
|
|
|
.generate()
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK);
|
|
|
|
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK);
|
2023-01-09 14:56:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void generateKeyWithPasswordTest(SOP sop) throws IOException {
|
|
|
|
byte[] key = sop.generateKey()
|
2023-01-09 19:48:25 +01:00
|
|
|
.userId("Alice <alice@openpgp.org>")
|
|
|
|
.withKeyPassword("sw0rdf1sh")
|
|
|
|
.generate()
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK);
|
|
|
|
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK);
|
2023-01-09 14:56:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void generateKeyWithMultipleUserIdsAndPassword(SOP sop) throws IOException {
|
|
|
|
byte[] key = sop.generateKey()
|
2023-01-09 19:48:25 +01:00
|
|
|
.userId("Alice <alice@openpgp.org>")
|
|
|
|
.userId("Bob <bob@openpgp.org>")
|
|
|
|
.withKeyPassword("sw0rdf1sh")
|
|
|
|
.generate()
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK);
|
|
|
|
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK);
|
2023-01-09 19:48:25 +01:00
|
|
|
}
|
2023-01-09 14:56:53 +01:00
|
|
|
}
|