From 3e75d325a8b4a509619539cd110eba579dd918ee Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 20 Feb 2021 01:05:29 +0100 Subject: [PATCH] Stabilize test The test was sometimes failing, as generating RSA keys can take longer than 1 second, which would result in the delta in the check being exceeded --- .../src/test/java/org/junit/JUtils.java | 25 +++++++++++++++++++ .../GenerateKeyWithAdditionalUserIdTest.java | 5 ++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 pgpainless-core/src/test/java/org/junit/JUtils.java diff --git a/pgpainless-core/src/test/java/org/junit/JUtils.java b/pgpainless-core/src/test/java/org/junit/JUtils.java new file mode 100644 index 00000000..c053027f --- /dev/null +++ b/pgpainless-core/src/test/java/org/junit/JUtils.java @@ -0,0 +1,25 @@ +/* + * 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.junit; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JUtils { + + public static void assertEquals(long a, long b, long delta) { + assertTrue(a - delta <= b && a + delta >= b); + } +} diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java index 2eb77269..afecc5a3 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java @@ -29,6 +29,7 @@ import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.junit.JUtils; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.algorithm.KeyFlag; @@ -42,7 +43,7 @@ public class GenerateKeyWithAdditionalUserIdTest { @Test public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { Date now = new Date(); - Date expiration = new Date(now.getTime() + 1000 * 5); + Date expiration = new Date(now.getTime() + 1000 * 60); PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing() .withPrimaryKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)) .withKeyFlags(KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA, KeyFlag.ENCRYPT_COMMS) @@ -56,7 +57,7 @@ public class GenerateKeyWithAdditionalUserIdTest { .build(); PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys); - assertEquals(expiration.getTime(), PGPainless.inspectKeyRing(publicKeys).getExpirationDate().getTime(), 2); + JUtils.assertEquals(expiration.getTime(), PGPainless.inspectKeyRing(publicKeys).getExpirationDate().getTime(),2000); Iterator userIds = publicKeys.getPublicKey().getUserIDs(); assertEquals("primary@user.id", userIds.next());