pgpainless/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUs...

84 lines
3.6 KiB
Java
Raw Normal View History

2020-10-20 22:58:45 +02:00
/*
* Copyright 2018 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.key.generation;
2020-11-13 16:31:59 +01:00
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
2020-10-16 13:18:07 +02:00
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
2020-10-16 13:18:07 +02:00
import java.util.Iterator;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPException;
2020-12-08 19:14:52 +01:00
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.JUtils;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.PGPainless;
2021-01-03 16:26:53 +01:00
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.implementation.ImplementationFactory;
2020-11-07 18:24:12 +01:00
import org.pgpainless.key.generation.type.KeyType;
2020-12-08 20:02:41 +01:00
import org.pgpainless.key.generation.type.rsa.RsaLength;
2020-12-08 19:14:52 +01:00
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.ArmoredOutputStreamFactory;
public class GenerateKeyWithAdditionalUserIdTest {
@ParameterizedTest
@MethodSource("org.pgpainless.util.TestUtil#provideImplementationFactories")
public void test(ImplementationFactory implementationFactory) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
ImplementationFactory.setFactoryImplementation(implementationFactory);
Date now = new Date();
Date expiration = new Date(now.getTime() + 1000 * 60);
2020-12-08 19:14:52 +01:00
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
.withPrimaryKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072))
2021-01-03 16:26:53 +01:00
.withKeyFlags(KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA, KeyFlag.ENCRYPT_COMMS)
.withDefaultAlgorithms())
.withPrimaryUserId("primary@user.id")
.withAdditionalUserId("additional@user.id")
.withAdditionalUserId("additional2@user.id")
2020-10-20 23:23:25 +02:00
.withAdditionalUserId("\ttrimThis@user.id ")
.setExpirationDate(expiration)
.withoutPassphrase()
.build();
2020-12-08 19:14:52 +01:00
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
2021-04-26 13:38:12 +02:00
JUtils.assertEquals(expiration.getTime(), PGPainless.inspectKeyRing(publicKeys).getPrimaryKeyExpirationDate().getTime(),2000);
2020-12-08 19:14:52 +01:00
Iterator<String> userIds = publicKeys.getPublicKey().getUserIDs();
2020-10-16 13:18:07 +02:00
assertEquals("primary@user.id", userIds.next());
assertEquals("additional@user.id", userIds.next());
assertEquals("additional2@user.id", userIds.next());
2020-10-20 23:23:25 +02:00
assertEquals("trimThis@user.id", userIds.next());
2020-10-16 13:18:07 +02:00
assertFalse(userIds.hasNext());
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(byteOut);
2020-12-08 19:14:52 +01:00
secretKeys.encode(armor);
armor.close();
2020-10-16 13:18:07 +02:00
// echo this | gpg --list-packets
2020-10-20 22:58:45 +02:00
// CHECKSTYLE:OFF
System.out.println(byteOut.toString("UTF-8"));
2020-10-20 22:58:45 +02:00
// CHECKSTYLE:ON
}
}