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.
|
|
|
|
*/
|
2020-10-16 12:46:58 +02:00
|
|
|
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
|
|
|
|
2020-10-16 12:46:58 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2021-02-13 12:22:28 +01:00
|
|
|
import java.util.Date;
|
2020-10-16 13:18:07 +02:00
|
|
|
import java.util.Iterator;
|
2020-10-16 12:46:58 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2020-12-08 19:14:52 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2021-02-20 01:05:29 +01:00
|
|
|
import org.junit.JUtils;
|
2021-02-25 23:27:08 +01:00
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
2020-10-16 12:46:58 +02:00
|
|
|
import org.pgpainless.PGPainless;
|
2021-01-03 16:26:53 +01:00
|
|
|
import org.pgpainless.algorithm.KeyFlag;
|
2021-02-25 23:27:08 +01:00
|
|
|
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;
|
2021-05-30 18:05:38 +02:00
|
|
|
import org.pgpainless.key.util.UserId;
|
2021-07-03 12:42:17 +02:00
|
|
|
import org.pgpainless.util.DateUtil;
|
2020-10-16 12:46:58 +02:00
|
|
|
|
|
|
|
public class GenerateKeyWithAdditionalUserIdTest {
|
|
|
|
|
2021-02-25 23:27:08 +01:00
|
|
|
@ParameterizedTest
|
2021-07-03 12:42:12 +02:00
|
|
|
@MethodSource("org.pgpainless.util.TestImplementationFactoryProvider#provideImplementationFactories")
|
2021-02-25 23:27:08 +01:00
|
|
|
public void test(ImplementationFactory implementationFactory) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
|
|
|
|
ImplementationFactory.setFactoryImplementation(implementationFactory);
|
2021-07-03 12:42:17 +02:00
|
|
|
Date expiration = new Date(DateUtil.now().getTime() + 60 * 1000);
|
2020-12-08 19:14:52 +01:00
|
|
|
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
|
2021-09-20 12:30:03 +02:00
|
|
|
.setPrimaryKey(KeySpec.getBuilder(
|
2021-09-13 19:20:19 +02:00
|
|
|
KeyType.RSA(RsaLength._3072),
|
2021-09-20 12:30:03 +02:00
|
|
|
KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA, KeyFlag.ENCRYPT_COMMS))
|
|
|
|
.addUserId(UserId.onlyEmail("primary@user.id"))
|
|
|
|
.addUserId(UserId.onlyEmail("additional@user.id"))
|
|
|
|
.addUserId(UserId.onlyEmail("additional2@user.id"))
|
|
|
|
.addUserId("\ttrimThis@user.id ")
|
2021-02-13 12:22:28 +01:00
|
|
|
.setExpirationDate(expiration)
|
2020-10-16 12:46:58 +02:00
|
|
|
.build();
|
2020-12-08 19:14:52 +01:00
|
|
|
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
|
2020-10-16 12:46:58 +02:00
|
|
|
|
2021-07-03 12:42:17 +02:00
|
|
|
JUtils.assertEquals(expiration.getTime(), PGPainless.inspectKeyRing(publicKeys).getPrimaryKeyExpirationDate().getTime(), 2000);
|
2021-02-13 12:22:28 +01:00
|
|
|
|
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());
|
2020-10-20 22:14:56 +02:00
|
|
|
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());
|
2020-10-16 12:46:58 +02:00
|
|
|
}
|
|
|
|
}
|