From dab704a0c59bbda226adb3961bf24a50cefe84e2 Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Thu, 26 Jun 2025 10:15:50 +0200 Subject: [PATCH] Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup --- pom.xml | 6 +- .../impl/BasicSSHUserPrivateKeyFIPSTest.java | 8 +- .../impl/BasicSSHUserPrivateKeyTest.java | 43 +++++---- .../TrileadSSHPasswordAuthenticatorTest.java | 74 ++++++++------- .../TrileadSSHPublicKeyAuthenticatorTest.java | 93 ++++++++++--------- ...ImportRoundTripCasCSSHCredentialsTest.java | 19 ++-- 6 files changed, 132 insertions(+), 111 deletions(-) diff --git a/pom.xml b/pom.xml index 39cc1c25..904bd523 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ org.jenkins-ci.plugins plugin - 5.9 + 5.17 @@ -73,7 +73,7 @@ 999999-SNAPSHOT 2.479 - ${jenkins.baseline}.1 + ${jenkins.baseline}.3 jenkinsci/${project.artifactId}-plugin @@ -95,7 +95,7 @@ io.jenkins.tools.bom bom-${jenkins.baseline}.x - 3893.v213a_42768d35 + 4948.vcf1d17350668 import pom diff --git a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyFIPSTest.java b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyFIPSTest.java index f94b274b..4b509b28 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyFIPSTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyFIPSTest.java @@ -35,7 +35,7 @@ public void nonCompliantKeysLaunchExceptionTest() throws Throwable { rule.then(BasicSSHUserPrivateKeyFIPSTest::checkNonCompliantKeysLaunchException); } - private static void checkNonCompliantKeysLaunchException(JenkinsRule r) throws IOException{ + private static void checkNonCompliantKeysLaunchException(JenkinsRule r) throws Exception { new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, "no-key", "user", null, null, "no key provided doesn't throw exceptions"); assertThrows(IllegalArgumentException.class, () -> new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, "nopass-openssh-ed25519", "user", @@ -66,7 +66,7 @@ public void invalidKeyIsNotSavedInFIPSModeTest() throws Throwable { rule.then(BasicSSHUserPrivateKeyFIPSTest::checkInvalidKeyIsNotSavedInFIPSMode); } - private static void checkInvalidKeyIsNotSavedInFIPSMode(JenkinsRule r) throws IOException { + private static void checkInvalidKeyIsNotSavedInFIPSMode(JenkinsRule r) throws Exception { BasicSSHUserPrivateKey entry = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, "rsa2048", "user", getKey("rsa2048"), "fipsvalidpassword", "RSA 1024 accepted key"); Iterator stores = CredentialsProvider.lookupStores(r.jenkins).iterator(); assertTrue(stores.hasNext()); @@ -111,7 +111,7 @@ public void formValidationTest() throws Throwable { rule.then(BasicSSHUserPrivateKeyFIPSTest::checkFormValidation); } - private static void checkFormValidation(JenkinsRule r) throws IOException { + private static void checkFormValidation(JenkinsRule r) throws Exception { BasicSSHUserPrivateKey.DirectEntryPrivateKeySource.DescriptorImpl descriptor = ExtensionList.lookupSingleton(BasicSSHUserPrivateKey.DirectEntryPrivateKeySource.DescriptorImpl.class); FormValidation result = descriptor.doCheckPrivateKey(getKey("rsa2048").getPrivateKey().getPlainText(), "fipsvalidpassword"); assertNull(result.getMessage()); @@ -119,7 +119,7 @@ private static void checkFormValidation(JenkinsRule r) throws IOException { assertFalse(result.getMessage().isBlank()); } - private static BasicSSHUserPrivateKey.DirectEntryPrivateKeySource getKey(String file) throws IOException { + private static BasicSSHUserPrivateKey.DirectEntryPrivateKeySource getKey(String file) throws Exception { String keyText = FileUtils.readFileToString(Paths.get("src/test/resources/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyFIPSTest/nonCompliantKeysLaunchExceptionTest").resolve(file).toFile(), Charset.defaultCharset()); return new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(keyText); } diff --git a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyTest.java b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyTest.java index ff104cca..6d1cc33c 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyTest.java @@ -38,30 +38,37 @@ import hudson.security.ACL; import jenkins.model.Jenkins; -import org.junit.Test; - import static hudson.cli.CLICommandInvoker.Matcher.failedWith; import static hudson.cli.CLICommandInvoker.Matcher.succeeded; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; -import static org.junit.Assert.*; -import org.junit.Rule; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class BasicSSHUserPrivateKeyTest { +@WithJenkins +class BasicSSHUserPrivateKeyTest { - final static String TESTKEY_ID = "bc07f814-78bd-4b29-93d4-d25b93285f93"; - final static String TESTKEY_BEGIN = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAu1r+HHzmpybc4iwoP5+44FjvcaMkNEWeGQZlmPwLx70XW8+8"; - final static String TESTKEY_END = "sroT/IHW2jKMD0v8kKLUnKCZYzlw0By7+RvJ8lgzHB0D71f6EC1UWg==\n-----END RSA PRIVATE KEY-----\n"; + private static final String TESTKEY_ID = "bc07f814-78bd-4b29-93d4-d25b93285f93"; + private static final String TESTKEY_BEGIN = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAu1r+HHzmpybc4iwoP5+44FjvcaMkNEWeGQZlmPwLx70XW8+8"; + private static final String TESTKEY_END = "sroT/IHW2jKMD0v8kKLUnKCZYzlw0By7+RvJ8lgzHB0D71f6EC1UWg==\n-----END RSA PRIVATE KEY-----\n"; - @Rule public JenkinsRule r = new JenkinsRule(); + private JenkinsRule r; + + @BeforeEach + void setUp(JenkinsRule rule) { + r = rule; + } @LocalData @Test - public void readOldCredentials() { + void readOldCredentials() { SSHUserPrivateKey supk = CredentialsMatchers.firstOrNull( CredentialsProvider.lookupCredentials(SSHUserPrivateKey.class, Hudson.get(), ACL.SYSTEM, (List)null), @@ -69,7 +76,7 @@ public void readOldCredentials() { assertNotNull(supk); List keyList = supk.getPrivateKeys(); assertNotNull(keyList); - assertEquals(keyList.size(), 1); + assertEquals(1, keyList.size()); String privateKey = keyList.get(0); assertNotNull(privateKey); assertTrue(privateKey.startsWith(TESTKEY_BEGIN)); @@ -77,7 +84,7 @@ public void readOldCredentials() { } @Test - public void ensureDirectEntryHasTrailingNewline() { + void ensureDirectEntryHasTrailingNewline() { String key = (new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource("test")).getPrivateKey().getPlainText(); assertEquals("test\n", key); } @@ -87,20 +94,20 @@ public void ensureDirectEntryHasTrailingNewline() { @Test @LocalData @Issue("SECURITY-440") - public void userWithoutRunScripts_cannotMigrateDangerousPrivateKeySource() throws Exception { + void userWithoutRunScripts_cannotMigrateDangerousPrivateKeySource() throws Exception { Folder folder = r.jenkins.createProject(Folder.class, "folder1"); - + FilePath updateFolder = r.jenkins.getRootPath().child("update_folder.xml"); - + { // as user with just configure, you cannot migrate CLICommandInvoker.Result result = new CLICommandInvoker(r, new UpdateJobCommand()) .authorizedTo(Jenkins.READ, Job.READ, Job.CONFIGURE) .withStdin(updateFolder.read()) .invokeWithArgs("folder1"); - + assertThat(result.stderr(), containsString("user is missing the Overall/Administer permission")); assertThat(result, failedWith(1)); - + // config file not touched String configFileContent = folder.getConfigFile().asString(); assertThat(configFileContent, not(containsString("FileOnMasterPrivateKeySource"))); @@ -111,7 +118,7 @@ public void userWithoutRunScripts_cannotMigrateDangerousPrivateKeySource() throw .authorizedTo(Jenkins.ADMINISTER) .withStdin(updateFolder.read()) .invokeWithArgs("folder1"); - + assertThat(result, succeeded()); String configFileContent = folder.getConfigFile().asString(); assertThat(configFileContent, containsString("BasicSSHUserPrivateKey")); diff --git a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/TrileadSSHPasswordAuthenticatorTest.java b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/TrileadSSHPasswordAuthenticatorTest.java index 62b31955..1855c645 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/TrileadSSHPasswordAuthenticatorTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/TrileadSSHPasswordAuthenticatorTest.java @@ -36,12 +36,14 @@ import hudson.slaves.DumbSlave; import jenkins.security.MasterToSlaveCallable; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; +import java.io.Serial; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Proxy; import java.util.Collections; @@ -50,24 +52,31 @@ import java.util.logging.Logger; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertNotNull; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; -public class TrileadSSHPasswordAuthenticatorTest { +@WithJenkins +class TrileadSSHPasswordAuthenticatorTest { private Connection connection; private StandardUsernamePasswordCredentials user; private Object sshd; - @Rule public JenkinsRule r = new JenkinsRule(); - - @After - public void tearDown() { + private JenkinsRule r; + + @BeforeEach + void setUp(JenkinsRule rule) { + r = rule; + user = (StandardUsernamePasswordCredentials) Items.XSTREAM.fromXML(Items.XSTREAM.toXML(new BasicSSHUserPassword(CredentialsScope.SYSTEM, null, "foobar", "foomanchu", null))); + } + + @AfterEach + void tearDown() { if (connection != null) { connection.close(); connection = null; } - if (sshd!=null) { + if (sshd != null) { try { invoke(sshd, "stop", new Class[] {Boolean.TYPE}, new Object[] {true}); } catch (Throwable t) { @@ -76,9 +85,10 @@ public void tearDown() { } } - // disabled as Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator - // so this test relies on having a local sshd which is keyboard interactive only - public void dontTestKeyboardInteractive() throws Exception { + @Test + @Disabled("Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator " + + "so this test relies on having a local sshd which is keyboard interactive only") + void dontTestKeyboardInteractive() throws Exception { connection = new Connection("localhost"); connection.connect(new NoVerifier()); TrileadSSHPasswordAuthenticator instance = @@ -91,13 +101,8 @@ public void dontTestKeyboardInteractive() throws Exception { assertThat(instance.isAuthenticated(), is(true)); } - @Before - public void setUp() { - user =(StandardUsernamePasswordCredentials) Items.XSTREAM.fromXML(Items.XSTREAM.toXML(new BasicSSHUserPassword(CredentialsScope.SYSTEM, null, "foobar", "foomanchu", null))); - } - @Test - public void testPassword() throws Exception { + void testPassword() throws Exception { sshd = createPasswordAuthenticatedSshServer(); invoke(sshd, "start", null, null); int port = (Integer)invoke(sshd, "getPort", null, null); @@ -111,11 +116,11 @@ public void testPassword() throws Exception { assertThat(instance.isAuthenticated(), is(true)); } - private Object createPasswordAuthenticatedSshServer() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException { + private Object createPasswordAuthenticatedSshServer() throws Exception { return createPasswordAuthenticatedSshServer(null); } - private Object createPasswordAuthenticatedSshServer(final String username) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException { + private Object createPasswordAuthenticatedSshServer(final String username) throws Exception { Object sshd = newDefaultSshServer(); Class keyPairProviderClass = newKeyPairProviderClass(); Object provider = newProvider(); @@ -132,7 +137,7 @@ private Object createPasswordAuthenticatedSshServer(final String username) throw } @Test - public void testFactory() throws Exception { + void testFactory() throws Exception { sshd = createPasswordAuthenticatedSshServer(); invoke(sshd, "start", null, null); int port = (Integer)invoke(sshd, "getPort", null, null); @@ -146,7 +151,7 @@ public void testFactory() throws Exception { } @Test - public void testFactoryAltUsername() throws Exception { + void testFactoryAltUsername() throws Exception { sshd = createPasswordAuthenticatedSshServer("bill"); invoke(sshd, "start", null, null); int port = (Integer)invoke(sshd, "getPort", null, null); @@ -170,7 +175,7 @@ public void testFactoryAltUsername() throws Exception { * Brings the {@link SSHAuthenticatorFactory} to a slave. */ @Test - public void testSlave() throws Exception { + void testSlave() throws Exception { Object sshd = createPasswordAuthenticatedSshServer(); invoke(sshd, "start", null, null); @@ -216,15 +221,16 @@ public Void call() throws Exception { return null; } + @Serial private static final long serialVersionUID = 1L; } - private Object invoke(Object target, String methodName, Class[] parameterTypes, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + private Object invoke(Object target, String methodName, Class[] parameterTypes, Object[] args) throws Exception { return target.getClass().getMethod(methodName, parameterTypes).invoke(target, args); } - private Object newDefaultSshServer() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Object server = null; + private Object newDefaultSshServer() throws Exception { + Object server; Class serverClass; try { serverClass = Class.forName("org.apache.sshd.SshServer"); @@ -238,7 +244,7 @@ private Object newDefaultSshServer() throws ClassNotFoundException, NoSuchMethod return server; } - private Class newKeyPairProviderClass() throws ClassNotFoundException { + private Class newKeyPairProviderClass() throws Exception { Class keyPairProviderClass; try { keyPairProviderClass = Class.forName("org.apache.sshd.common.KeyPairProvider"); @@ -249,7 +255,7 @@ private Class newKeyPairProviderClass() throws ClassNotFoundException { return keyPairProviderClass; } - private Object newProvider() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + private Object newProvider() throws Exception { Class providerClass = Class.forName("org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider"); Object provider = providerClass.getConstructor().newInstance(); assertNotNull(provider); @@ -257,7 +263,7 @@ private Object newProvider() throws ClassNotFoundException, NoSuchMethodExceptio return provider; } - private Class newAuthenticatorClass() throws ClassNotFoundException { + private Class newAuthenticatorClass() throws Exception { Class authenticatorClass; try { authenticatorClass = Class.forName("org.apache.sshd.server.auth.password.PasswordAuthenticator"); @@ -268,7 +274,7 @@ private Class newAuthenticatorClass() throws ClassNotFoundException { return authenticatorClass; } - private Object newAuthenticator(Class authenticatorClass, final String userName) throws IllegalArgumentException { + private Object newAuthenticator(Class authenticatorClass, final String userName) { Object authenticator = Proxy.newProxyInstance( authenticatorClass.getClassLoader(), new Class[]{authenticatorClass}, (proxy, method, args) -> method.getName().equals("authenticate") ? @@ -278,8 +284,8 @@ private Object newAuthenticator(Class authenticatorClass, final String userNa return authenticator; } - private Object newFactory() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - Object factory = null; + private Object newFactory() throws Exception { + Object factory; Class factoryClass; try { factoryClass = Class.forName("org.apache.sshd.server.auth.UserAuthPassword$Factory"); diff --git a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/TrileadSSHPublicKeyAuthenticatorTest.java b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/TrileadSSHPublicKeyAuthenticatorTest.java index 50944200..199e1ea4 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/TrileadSSHPublicKeyAuthenticatorTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/TrileadSSHPublicKeyAuthenticatorTest.java @@ -33,11 +33,11 @@ import edu.umd.cs.findbugs.annotations.NonNull; import hudson.util.Secret; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import java.lang.reflect.InvocationTargetException; import java.util.Collections; @@ -48,25 +48,20 @@ import static java.lang.reflect.Proxy.*; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; -public class TrileadSSHPublicKeyAuthenticatorTest { +@WithJenkins +class TrileadSSHPublicKeyAuthenticatorTest { private Connection connection; private SSHUserPrivateKey user; - @Rule public JenkinsRule r = new JenkinsRule(); - - @After - public void tearDown() { - if (connection != null) { - connection.close(); - connection = null; - } - } + private JenkinsRule r; + + @BeforeEach + void setUp(JenkinsRule rule) { + r = rule; - @Before - public void setUp() { user = new SSHUserPrivateKey() { @NonNull @@ -108,27 +103,37 @@ public Secret getPassphrase() { public List getPrivateKeys() { // just want a valid key... I generated this and have thrown it away (other than here) // do not use other than in this test - return List.of("-----BEGIN RSA PRIVATE KEY-----\n" - + "MIICWQIBAAKBgQDADDwooNPJNQB4N4bJPiBgq/rkWKMABApX0w4trSkkX5q+l+CL\n" - + "CuddGGAsAu6XPari8v49ipbBmHqRLP9+X3ARGWKU2gDvGTBr99/ReUl2YgVjCwy+\n" - + "KMrGCN7SNTgRo6StwVaPhh6pUpNTQciDe/kOwUnQFWSM6/lwkOD1Uod45wIBIwKB\n" - + "gHi3O8HELVnmzRhdaqphkLHLL/0/B18Ye4epPBy1/JqFPLJQ1kjFBnUIAe/HVCSN\n" - + "KZX30wIcmUZ9GdeYoJiTwsfTy9t2KwHjqrapTfiekVZAW+3iDBqRZMxQ5MoK7b6g\n" - + "w5HrrtrtPfYuAsBnYjIS6qsKAVT3vdolJ5eai/RlPO4LAkEA76YuUozC/dW7Ox+R\n" - + "1Njd6cWJsRVXGemkSYY/rSh0SbfHAebqL/bDg8xXim9UiuD9Hc6md3glHQj6iKvl\n" - + "BxWq4QJBAM0moKiM16WFSFJP1wVDj0Bnx6DkJYSpf5u+C0ghBVoqIYKq6/P/gRE2\n" - + "+ColsLu6AYftaEJVpAgxeTU/IsGoJMcCQHRmqMkCipiMYkFJ2R49cxnGWNJa0ojt\n" - + "03QrQ3/9tNNZQ2dS5sbW8UAEKoURgNW9vMVVvpHMpE/uaw8u65W6ESsCQDTAyjn4\n" - + "VLWIrDJsTTveLCaBFhNt3cMHA45ysnGiF1GzD+5mdzAdITBP9qvAjIgLQjjlRrH4\n" - + "w8eXsXQXjJgyjR0CQHfvhiMPG5pWwmXpsEOFo6GKSvOC/5sNEcnddenuO/2T7WWi\n" - + "o1LQh9naeuX8gti0vNR8+KtMEaIcJJeWnk56AVY=\n" - + "-----END RSA PRIVATE KEY-----\n"); + return List.of(""" + -----BEGIN RSA PRIVATE KEY----- + MIICWQIBAAKBgQDADDwooNPJNQB4N4bJPiBgq/rkWKMABApX0w4trSkkX5q+l+CL + CuddGGAsAu6XPari8v49ipbBmHqRLP9+X3ARGWKU2gDvGTBr99/ReUl2YgVjCwy+ + KMrGCN7SNTgRo6StwVaPhh6pUpNTQciDe/kOwUnQFWSM6/lwkOD1Uod45wIBIwKB + gHi3O8HELVnmzRhdaqphkLHLL/0/B18Ye4epPBy1/JqFPLJQ1kjFBnUIAe/HVCSN + KZX30wIcmUZ9GdeYoJiTwsfTy9t2KwHjqrapTfiekVZAW+3iDBqRZMxQ5MoK7b6g + w5HrrtrtPfYuAsBnYjIS6qsKAVT3vdolJ5eai/RlPO4LAkEA76YuUozC/dW7Ox+R + 1Njd6cWJsRVXGemkSYY/rSh0SbfHAebqL/bDg8xXim9UiuD9Hc6md3glHQj6iKvl + BxWq4QJBAM0moKiM16WFSFJP1wVDj0Bnx6DkJYSpf5u+C0ghBVoqIYKq6/P/gRE2 + +ColsLu6AYftaEJVpAgxeTU/IsGoJMcCQHRmqMkCipiMYkFJ2R49cxnGWNJa0ojt + 03QrQ3/9tNNZQ2dS5sbW8UAEKoURgNW9vMVVvpHMpE/uaw8u65W6ESsCQDTAyjn4 + VLWIrDJsTTveLCaBFhNt3cMHA45ysnGiF1GzD+5mdzAdITBP9qvAjIgLQjjlRrH4 + w8eXsXQXjJgyjR0CQHfvhiMPG5pWwmXpsEOFo6GKSvOC/5sNEcnddenuO/2T7WWi + o1LQh9naeuX8gti0vNR8+KtMEaIcJJeWnk56AVY= + -----END RSA PRIVATE KEY----- + """); } }; } + @AfterEach + void tearDown() { + if (connection != null) { + connection.close(); + connection = null; + } + } + @Test - public void testAuthenticate() throws Exception { + void testAuthenticate() throws Exception { Object sshd = newDefaultSshServer(); Class keyPairProviderClass = newKeyPairProviderClass(); Object provider = newProvider(); @@ -163,7 +168,7 @@ public void testAuthenticate() throws Exception { } @Test - public void testFactory() throws Exception { + void testFactory() throws Exception { Object sshd = newDefaultSshServer(); Class keyPairProviderClass = newKeyPairProviderClass(); Object provider = newProvider(); @@ -196,7 +201,7 @@ public void testFactory() throws Exception { } @Test - public void testAltUsername() throws Exception { + void testAltUsername() throws Exception { Object sshd = newDefaultSshServer(); Class keyPairProviderClass = newKeyPairProviderClass(); Object provider = newProvider(); @@ -234,12 +239,12 @@ public void testAltUsername() throws Exception { } } - private Object invoke(Object target, String methodName, Class[] parameterTypes, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + private Object invoke(Object target, String methodName, Class[] parameterTypes, Object[] args) throws Exception { return target.getClass().getMethod(methodName, parameterTypes).invoke(target, args); } - private Object newDefaultSshServer() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Object sshd = null; + private Object newDefaultSshServer() throws Exception { + Object sshd; Class sshdClass; try { sshdClass = Class.forName("org.apache.sshd.SshServer"); @@ -253,7 +258,7 @@ private Object newDefaultSshServer() throws ClassNotFoundException, NoSuchMethod return sshd; } - private Class newKeyPairProviderClass() throws ClassNotFoundException { + private Class newKeyPairProviderClass() throws Exception { Class keyPairProviderClass; try { keyPairProviderClass = Class.forName("org.apache.sshd.common.KeyPairProvider"); @@ -264,7 +269,7 @@ private Class newKeyPairProviderClass() throws ClassNotFoundException { return keyPairProviderClass; } - private Object newProvider() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + private Object newProvider() throws Exception { Class providerClass = Class.forName("org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider"); Object provider = providerClass.getConstructor().newInstance(); assertNotNull(provider); @@ -272,7 +277,7 @@ private Object newProvider() throws ClassNotFoundException, NoSuchMethodExceptio return provider; } - private Class newAuthenticatorClass() throws ClassNotFoundException { + private Class newAuthenticatorClass() throws Exception { Class authenticatorClass; try { authenticatorClass = Class.forName("org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator"); @@ -283,7 +288,7 @@ private Class newAuthenticatorClass() throws ClassNotFoundException { return authenticatorClass; } - private Object newAuthenticator(Class authenticatorClass, final String userName) throws IllegalArgumentException { + private Object newAuthenticator(Class authenticatorClass, final String userName) { Object authenticator = newProxyInstance( authenticatorClass.getClassLoader(), new Class[]{authenticatorClass}, (proxy, method, args) -> method.getName().equals("authenticate") ? userName.equals(args[0]) : null); @@ -291,8 +296,8 @@ private Object newAuthenticator(Class authenticatorClass, final String userNa return authenticator; } - private Object newFactory() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - Object factory = null; + private Object newFactory() throws Exception { + Object factory; Class factoryClass; try { factoryClass = Class.forName("org.apache.sshd.server.auth.UserAuthPublicKey$Factory"); diff --git a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/jcasc/ExportImportRoundTripCasCSSHCredentialsTest.java b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/jcasc/ExportImportRoundTripCasCSSHCredentialsTest.java index 3d4eec1b..643e351e 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/jcasc/ExportImportRoundTripCasCSSHCredentialsTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/jcasc/ExportImportRoundTripCasCSSHCredentialsTest.java @@ -4,19 +4,22 @@ import com.cloudbees.plugins.credentials.CredentialsProvider; import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; import hudson.util.Secret; -import io.jenkins.plugins.casc.misc.RoundTripAbstractTest; +import io.jenkins.plugins.casc.misc.junit.jupiter.AbstractRoundTripTest; import jenkins.model.Jenkins; -import org.jvnet.hudson.test.RestartableJenkinsRule; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import java.util.Collections; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@WithJenkins +class ExportImportRoundTripCasCSSHCredentialsTest extends AbstractRoundTripTest { -public class ExportImportRoundTripCasCSSHCredentialsTest extends RoundTripAbstractTest { @Override - public void assertConfiguredAsExpected(RestartableJenkinsRule restartableJenkinsRule, String s) { + protected void assertConfiguredAsExpected(JenkinsRule rule, String s) { List creds = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class, Jenkins.getInstanceOrNull(), null, Collections.emptyList()); assertEquals(1, creds.size()); StandardUsernamePasswordCredentials cred = creds.get(0); @@ -24,7 +27,7 @@ public void assertConfiguredAsExpected(RestartableJenkinsRule restartableJenkins assertEquals("username-of-userid", cred.getUsername()); assertEquals("password-of-userid", cred.getPassword().getPlainText()); - List creds2 = CredentialsProvider.lookupCredentials(BasicSSHUserPrivateKey.class,Jenkins.getInstanceOrNull(), null, Collections.emptyList()); + List creds2 = CredentialsProvider.lookupCredentials(BasicSSHUserPrivateKey.class, Jenkins.getInstanceOrNull(), null, Collections.emptyList()); assertEquals(1, creds2.size()); BasicSSHUserPrivateKey cred2 = creds2.get(0); assertEquals("userid2", cred2.getId()); @@ -39,7 +42,7 @@ public void assertConfiguredAsExpected(RestartableJenkinsRule restartableJenkins } @Override - public String stringInLogExpected() { + protected String stringInLogExpected() { return "Setting class com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.passphrase = ****"; } }