From 851c14c6850eafeaa54ff01fbf77aa0885291dd4 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Wed, 23 Feb 2022 20:55:41 +0100 Subject: [PATCH] Add CLI tests fetching test keys from github pages See https://github.com/pgpainless/pgpainless.github.io --- .../GithubPagesTestVectors.java | 37 +++++++++++++++++++ .../RedirectSystemStreamsTest.java | 31 ++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 wkd-java-cli/src/test/java/pgp/wkd/cli/online_test_vectors/GithubPagesTestVectors.java create mode 100644 wkd-java-cli/src/test/java/pgp/wkd/cli/online_test_vectors/RedirectSystemStreamsTest.java diff --git a/wkd-java-cli/src/test/java/pgp/wkd/cli/online_test_vectors/GithubPagesTestVectors.java b/wkd-java-cli/src/test/java/pgp/wkd/cli/online_test_vectors/GithubPagesTestVectors.java new file mode 100644 index 0000000..2ff66f8 --- /dev/null +++ b/wkd-java-cli/src/test/java/pgp/wkd/cli/online_test_vectors/GithubPagesTestVectors.java @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package pgp.wkd.cli.online_test_vectors; + +import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; +import org.junit.jupiter.api.Test; +import pgp.wkd.cli.WKDCLI; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class GithubPagesTestVectors extends RedirectSystemStreamsTest { + + // Valid WKD publication. + // Cert is available at https://pgpainless.github.io/.well-known/openpgpkey/hu/eprjcbeppbna3f6xabhtpddzpn41nknw + private static final String USERID_BASE = "WKD Test [Base Case - Valid User-ID]"; + private static final String MAIL_BASE = "wkd-test-base@pgpainless.github.io"; + + @Test + public void testFetchBaseKeyByMailAddress_Successful() { + WKDCLI.main(new String[] {"fetch", MAIL_BASE}); + assertEquals(718, outContent.size()); + } + + @Test + public void testFetchBaseKeyByUserID_Successful() { + WKDCLI.main(new String[] {"fetch", USERID_BASE}); + assertEquals(718, outContent.size()); + } + + @Test + @ExpectSystemExitWithStatus(1) + public void testFetchNonExistentKeyFails() { + WKDCLI.main(new String[] {"fetch", "wkd-test-nonexistent@pgpainless.github,io"}); + } +} diff --git a/wkd-java-cli/src/test/java/pgp/wkd/cli/online_test_vectors/RedirectSystemStreamsTest.java b/wkd-java-cli/src/test/java/pgp/wkd/cli/online_test_vectors/RedirectSystemStreamsTest.java new file mode 100644 index 0000000..043607f --- /dev/null +++ b/wkd-java-cli/src/test/java/pgp/wkd/cli/online_test_vectors/RedirectSystemStreamsTest.java @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package pgp.wkd.cli.online_test_vectors; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +public abstract class RedirectSystemStreamsTest { + + protected final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + protected final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private final PrintStream originalErr = System.err; + + @BeforeEach + public final void setUpStreams() { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @AfterEach + public void restoreStreams() { + System.setOut(originalOut); + System.setErr(originalErr); + } +}