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
1 changed files with 88 additions and 0 deletions

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-----"));
}
}