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:
parent
c0ae6d75ba
commit
d170138ea8
1 changed files with 88 additions and 0 deletions
|
@ -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-----"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue