1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-23 20:14:49 +02:00
pgpainless/pgpainless-sop/src/test/java/org/pgpainless/sop/GenerateKeyTest.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
3.3 KiB
Java
Raw Permalink Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
2021-10-04 15:02:28 +02:00
package org.pgpainless.sop;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
2022-12-15 12:30:30 +01:00
import static org.junit.jupiter.api.Assertions.assertNotNull;
2023-04-14 16:17:32 +02:00
import static org.junit.jupiter.api.Assertions.assertThrows;
2021-10-04 15:02:28 +02:00
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
2022-12-15 12:30:30 +01:00
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKey;
2021-10-04 15:02:28 +02:00
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.info.KeyRingInfo;
2022-12-15 12:30:30 +01:00
import org.pgpainless.key.protection.UnlockSecretKey;
import org.pgpainless.util.Passphrase;
2021-10-04 15:02:28 +02:00
import sop.SOP;
2023-04-14 16:17:32 +02:00
import sop.exception.SOPGPException;
2021-10-04 15:02:28 +02:00
public class GenerateKeyTest {
private SOP sop;
@BeforeEach
public void prepare() {
sop = new SOPImpl();
}
@Test
public void generateKey() throws IOException {
byte[] bytes = sop.generateKey()
.userId("Alice <alice@pgpainless.org>")
.generate()
.getBytes();
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing()
.secretKeyRing(bytes);
assertTrue(PGPainless.inspectKeyRing(secretKeys)
.isUserIdValid("Alice <alice@pgpainless.org>"));
}
@Test
public void generateKeyWithMultipleUserIds() throws IOException {
byte[] bytes = sop.generateKey()
.userId("Alice <alice@pgpainless.org>")
.userId("Al <al@example.org>")
.generate()
.getBytes();
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing()
.secretKeyRing(bytes);
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
assertEquals("Alice <alice@pgpainless.org>", info.getPrimaryUserId());
assertTrue(info.isUserIdValid("Alice <alice@pgpainless.org>"));
assertTrue(info.isUserIdValid("Al <al@example.org>"));
}
@Test
public void unarmoredKey() throws IOException {
byte[] bytes = sop.generateKey()
.userId("Alice <alice@pgpainless.org>")
.noArmor()
.generate()
.getBytes();
assertFalse(new String(bytes).startsWith("-----BEGIN PGP PRIVATE KEY BLOCK-----"));
}
2022-12-15 12:30:30 +01:00
@Test
public void protectedMultiUserIdKey() throws IOException, PGPException {
byte[] bytes = sop.generateKey()
.userId("Alice")
.userId("Bob")
.withKeyPassword("sw0rdf1sh")
.generate()
.getBytes();
PGPSecretKeyRing secretKey = PGPainless.readKeyRing().secretKeyRing(bytes);
KeyRingInfo info = PGPainless.inspectKeyRing(secretKey);
assertTrue(info.getUserIds().contains("Alice"));
assertTrue(info.getUserIds().contains("Bob"));
for (PGPSecretKey key : secretKey) {
assertNotNull(UnlockSecretKey.unlockSecretKey(key, Passphrase.fromPassword("sw0rdf1sh")));
}
}
2023-04-14 16:17:32 +02:00
@Test
public void invalidProfile() {
assertThrows(SOPGPException.UnsupportedProfile.class, () ->
sop.generateKey().profile("invalid"));
}
2021-10-04 15:02:28 +02:00
}