From c73905d179c77c7aca04bd6a067dc09d15ec8a52 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 29 Aug 2022 11:12:42 +0200 Subject: [PATCH] Import RevocationStateTest from wot branch --- .../pgpainless/algorithm/RevocationState.java | 2 +- .../algorithm/RevocationStateTest.java | 96 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 pgpainless-core/src/test/java/org/pgpainless/algorithm/RevocationStateTest.java diff --git a/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationState.java b/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationState.java index bf17ca2b..8e4a60d3 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationState.java +++ b/pgpainless-core/src/main/java/org/pgpainless/algorithm/RevocationState.java @@ -10,7 +10,7 @@ import javax.annotation.Nonnull; import java.util.Date; import java.util.NoSuchElementException; -public class RevocationState implements Comparable { +public final class RevocationState implements Comparable { private final RevocationStateType type; private final Date date; diff --git a/pgpainless-core/src/test/java/org/pgpainless/algorithm/RevocationStateTest.java b/pgpainless-core/src/test/java/org/pgpainless/algorithm/RevocationStateTest.java new file mode 100644 index 00000000..c1e45413 --- /dev/null +++ b/pgpainless-core/src/test/java/org/pgpainless/algorithm/RevocationStateTest.java @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.algorithm; + +import org.junit.jupiter.api.Test; +import org.pgpainless.util.DateUtil; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.NoSuchElementException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class RevocationStateTest { + + @Test + public void testNotRevoked() { + RevocationState state = RevocationState.notRevoked(); + assertEquals(RevocationStateType.notRevoked, state.getType()); + assertTrue(state.isNotRevoked()); + assertFalse(state.isHardRevocation()); + assertFalse(state.isSoftRevocation()); + assertThrows(NoSuchElementException.class, state::getDate); + assertEquals("notRevoked", state.toString()); + } + + @Test + public void testHardRevoked() { + RevocationState state = RevocationState.hardRevoked(); + assertEquals(RevocationStateType.hardRevoked, state.getType()); + assertTrue(state.isHardRevocation()); + assertFalse(state.isSoftRevocation()); + assertFalse(state.isNotRevoked()); + + assertThrows(NoSuchElementException.class, state::getDate); + assertEquals("hardRevoked", state.toString()); + } + + @Test + public void testSoftRevoked() { + Date date = DateUtil.parseUTCDate("2022-08-03 18:26:35 UTC"); + assertNotNull(date); + + RevocationState state = RevocationState.softRevoked(date); + assertEquals(RevocationStateType.softRevoked, state.getType()); + assertTrue(state.isSoftRevocation()); + assertFalse(state.isHardRevocation()); + assertFalse(state.isNotRevoked()); + assertEquals(date, state.getDate()); + + assertEquals("softRevoked (2022-08-03 18:26:35 UTC)", state.toString()); + } + + @Test + public void testSoftRevokedNullDateThrows() { + assertThrows(NullPointerException.class, () -> RevocationState.softRevoked(null)); + } + + @Test + public void orderTest() { + assertEquals(RevocationState.notRevoked(), RevocationState.notRevoked()); + assertEquals(RevocationState.hardRevoked(), RevocationState.hardRevoked()); + Date now = new Date(); + assertEquals(RevocationState.softRevoked(now), RevocationState.softRevoked(now)); + + assertEquals(0, RevocationState.notRevoked().compareTo(RevocationState.notRevoked())); + assertEquals(0, RevocationState.hardRevoked().compareTo(RevocationState.hardRevoked())); + assertTrue(RevocationState.hardRevoked().compareTo(RevocationState.notRevoked()) > 0); + + List states = new ArrayList<>(); + RevocationState earlySoft = RevocationState.softRevoked(DateUtil.parseUTCDate("2000-05-12 10:44:01 UTC")); + RevocationState laterSoft = RevocationState.softRevoked(DateUtil.parseUTCDate("2022-08-03 18:26:35 UTC")); + RevocationState hard = RevocationState.hardRevoked(); + RevocationState not = RevocationState.notRevoked(); + RevocationState not2 = RevocationState.notRevoked(); + states.add(laterSoft); + states.add(not); + states.add(not2); + states.add(hard); + states.add(earlySoft); + + Collections.shuffle(states); + Collections.sort(states); + + assertEquals(states, Arrays.asList(not, not2, laterSoft, earlySoft, hard)); + } +}