From d170138ea876e301f5970c211bac63e401e0e9a0 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 4 Oct 2021 15:02:28 +0200 Subject: [PATCH] Add test for GenerateKeyImpl --- .../org/pgpainless/sop/GenerateKeyTest.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 pgpainless-sop/src/test/java/org/pgpainless/sop/GenerateKeyTest.java diff --git a/pgpainless-sop/src/test/java/org/pgpainless/sop/GenerateKeyTest.java b/pgpainless-sop/src/test/java/org/pgpainless/sop/GenerateKeyTest.java new file mode 100644 index 00000000..03e23063 --- /dev/null +++ b/pgpainless-sop/src/test/java/org/pgpainless/sop/GenerateKeyTest.java @@ -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 ") + .generate() + .getBytes(); + + PGPSecretKeyRing secretKeys = PGPainless.readKeyRing() + .secretKeyRing(bytes); + + assertTrue(PGPainless.inspectKeyRing(secretKeys) + .isUserIdValid("Alice ")); + } + + @Test + public void generateKeyWithMultipleUserIds() throws IOException { + byte[] bytes = sop.generateKey() + .userId("Alice ") + .userId("Al ") + .generate() + .getBytes(); + + PGPSecretKeyRing secretKeys = PGPainless.readKeyRing() + .secretKeyRing(bytes); + + KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys); + assertEquals("Alice ", info.getPrimaryUserId()); + assertTrue(info.isUserIdValid("Alice ")); + assertTrue(info.isUserIdValid("Al ")); + } + + @Test + public void unarmoredKey() throws IOException { + byte[] bytes = sop.generateKey() + .userId("Alice ") + .noArmor() + .generate() + .getBytes(); + + assertFalse(new String(bytes).startsWith("-----BEGIN PGP PRIVATE KEY BLOCK-----")); + } +}