1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 06:12:06 +01:00

Add test for GenerateKeyImpl

This commit is contained in:
Paul Schaub 2021-10-04 15:02:28 +02:00
parent c0ae6d75ba
commit d170138ea8
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -0,0 +1,88 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.sop;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
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;
import sop.SOP;
import sop.exception.SOPGPException;
public class GenerateKeyTest {
private SOP sop;
@BeforeEach
public void prepare() {
sop = new SOPImpl();
}
@Test
public void testMissingUserId() {
assertThrows(SOPGPException.MissingArg.class, () -> sop.generateKey().generate());
}
@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-----"));
}
}