Skip to content

Commit 582ee6c

Browse files
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 2191c4c commit 582ee6c

File tree

6 files changed

+132
-111
lines changed

6 files changed

+132
-111
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<parent>
3030
<groupId>org.jenkins-ci.plugins</groupId>
3131
<artifactId>plugin</artifactId>
32-
<version>5.9</version>
32+
<version>5.17</version>
3333
<relativePath />
3434
</parent>
3535

@@ -73,7 +73,7 @@
7373
<changelist>999999-SNAPSHOT</changelist>
7474
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
7575
<jenkins.baseline>2.479</jenkins.baseline>
76-
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
76+
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
7777
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
7878
</properties>
7979

@@ -95,7 +95,7 @@
9595
<dependency>
9696
<groupId>io.jenkins.tools.bom</groupId>
9797
<artifactId>bom-${jenkins.baseline}.x</artifactId>
98-
<version>3893.v213a_42768d35</version>
98+
<version>4948.vcf1d17350668</version>
9999
<scope>import</scope>
100100
<type>pom</type>
101101
</dependency>

src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyFIPSTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void nonCompliantKeysLaunchExceptionTest() throws Throwable {
3636
rule.then(BasicSSHUserPrivateKeyFIPSTest::checkNonCompliantKeysLaunchException);
3737
}
3838

39-
private static void checkNonCompliantKeysLaunchException(JenkinsRule r) throws IOException{
39+
private static void checkNonCompliantKeysLaunchException(JenkinsRule r) throws Exception {
4040
new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, "no-key", "user",
4141
null, null, "no key provided doesn't throw exceptions");
4242
assertThrows(IllegalArgumentException.class, () -> new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, "nopass-openssh-ed25519", "user",
@@ -67,7 +67,7 @@ public void invalidKeyIsNotSavedInFIPSModeTest() throws Throwable {
6767
rule.then(BasicSSHUserPrivateKeyFIPSTest::checkInvalidKeyIsNotSavedInFIPSMode);
6868
}
6969

70-
private static void checkInvalidKeyIsNotSavedInFIPSMode(JenkinsRule r) throws IOException {
70+
private static void checkInvalidKeyIsNotSavedInFIPSMode(JenkinsRule r) throws Exception {
7171
BasicSSHUserPrivateKey entry = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, "rsa2048", "user", getKey("rsa2048"), "fipsvalidpassword", "RSA 1024 accepted key");
7272
Iterator<CredentialsStore> stores = CredentialsProvider.lookupStores(r.jenkins).iterator();
7373
assertTrue(stores.hasNext());
@@ -112,15 +112,15 @@ public void formValidationTest() throws Throwable {
112112
rule.then(BasicSSHUserPrivateKeyFIPSTest::checkFormValidation);
113113
}
114114

115-
private static void checkFormValidation(JenkinsRule r) throws IOException {
115+
private static void checkFormValidation(JenkinsRule r) throws Exception {
116116
BasicSSHUserPrivateKey.DirectEntryPrivateKeySource.DescriptorImpl descriptor = ExtensionList.lookupSingleton(BasicSSHUserPrivateKey.DirectEntryPrivateKeySource.DescriptorImpl.class);
117117
FormValidation result = descriptor.doCheckPrivateKey(getKey("rsa2048").getPrivateKey().getPlainText(), "fipsvalidpassword");
118118
assertTrue(StringUtils.isBlank(result.getMessage()));
119119
result = descriptor.doCheckPrivateKey(getKey("rsa1024").getPrivateKey().getPlainText(), "fipsvalidpassword");
120120
assertTrue(StringUtils.isNotBlank(result.getMessage()));
121121
}
122122

123-
private static BasicSSHUserPrivateKey.DirectEntryPrivateKeySource getKey(String file) throws IOException {
123+
private static BasicSSHUserPrivateKey.DirectEntryPrivateKeySource getKey(String file) throws Exception {
124124
String keyText = FileUtils.readFileToString(Paths.get("src/test/resources/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyFIPSTest/nonCompliantKeysLaunchExceptionTest").resolve(file).toFile(), Charset.defaultCharset());
125125
return new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(keyText);
126126
}

src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/BasicSSHUserPrivateKeyTest.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,53 @@
3838
import hudson.security.ACL;
3939
import jenkins.model.Jenkins;
4040

41-
import org.junit.Test;
42-
4341
import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
4442
import static hudson.cli.CLICommandInvoker.Matcher.succeeded;
4543
import static org.hamcrest.MatcherAssert.assertThat;
4644
import static org.hamcrest.Matchers.containsString;
4745
import static org.hamcrest.Matchers.not;
48-
import static org.junit.Assert.*;
49-
import org.junit.Rule;
46+
import static org.junit.jupiter.api.Assertions.*;
47+
48+
import org.junit.jupiter.api.BeforeEach;
49+
import org.junit.jupiter.api.Test;
5050
import org.jvnet.hudson.test.Issue;
5151
import org.jvnet.hudson.test.JenkinsRule;
52+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
5253
import org.jvnet.hudson.test.recipes.LocalData;
5354

54-
public class BasicSSHUserPrivateKeyTest {
55+
@WithJenkins
56+
class BasicSSHUserPrivateKeyTest {
5557

56-
final static String TESTKEY_ID = "bc07f814-78bd-4b29-93d4-d25b93285f93";
57-
final static String TESTKEY_BEGIN = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAu1r+HHzmpybc4iwoP5+44FjvcaMkNEWeGQZlmPwLx70XW8+8";
58-
final static String TESTKEY_END = "sroT/IHW2jKMD0v8kKLUnKCZYzlw0By7+RvJ8lgzHB0D71f6EC1UWg==\n-----END RSA PRIVATE KEY-----\n";
58+
private static final String TESTKEY_ID = "bc07f814-78bd-4b29-93d4-d25b93285f93";
59+
private static final String TESTKEY_BEGIN = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAu1r+HHzmpybc4iwoP5+44FjvcaMkNEWeGQZlmPwLx70XW8+8";
60+
private static final String TESTKEY_END = "sroT/IHW2jKMD0v8kKLUnKCZYzlw0By7+RvJ8lgzHB0D71f6EC1UWg==\n-----END RSA PRIVATE KEY-----\n";
5961

60-
@Rule public JenkinsRule r = new JenkinsRule();
62+
private JenkinsRule r;
63+
64+
@BeforeEach
65+
void setUp(JenkinsRule rule) {
66+
r = rule;
67+
}
6168

6269
@LocalData
6370
@Test
64-
public void readOldCredentials() {
71+
void readOldCredentials() {
6572
SSHUserPrivateKey supk = CredentialsMatchers.firstOrNull(
6673
CredentialsProvider.lookupCredentials(SSHUserPrivateKey.class, Hudson.get(), ACL.SYSTEM,
6774
(List<DomainRequirement>)null),
6875
CredentialsMatchers.withId(TESTKEY_ID));
6976
assertNotNull(supk);
7077
List<String> keyList = supk.getPrivateKeys();
7178
assertNotNull(keyList);
72-
assertEquals(keyList.size(), 1);
79+
assertEquals(1, keyList.size());
7380
String privateKey = keyList.get(0);
7481
assertNotNull(privateKey);
7582
assertTrue(privateKey.startsWith(TESTKEY_BEGIN));
7683
assertTrue(privateKey.endsWith(TESTKEY_END));
7784
}
7885

7986
@Test
80-
public void ensureDirectEntryHasTrailingNewline() {
87+
void ensureDirectEntryHasTrailingNewline() {
8188
String key = (new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource("test")).getPrivateKey().getPlainText();
8289
assertEquals("test\n", key);
8390
}
@@ -87,20 +94,20 @@ public void ensureDirectEntryHasTrailingNewline() {
8794
@Test
8895
@LocalData
8996
@Issue("SECURITY-440")
90-
public void userWithoutRunScripts_cannotMigrateDangerousPrivateKeySource() throws Exception {
97+
void userWithoutRunScripts_cannotMigrateDangerousPrivateKeySource() throws Exception {
9198
Folder folder = r.jenkins.createProject(Folder.class, "folder1");
92-
99+
93100
FilePath updateFolder = r.jenkins.getRootPath().child("update_folder.xml");
94-
101+
95102
{ // as user with just configure, you cannot migrate
96103
CLICommandInvoker.Result result = new CLICommandInvoker(r, new UpdateJobCommand())
97104
.authorizedTo(Jenkins.READ, Job.READ, Job.CONFIGURE)
98105
.withStdin(updateFolder.read())
99106
.invokeWithArgs("folder1");
100-
107+
101108
assertThat(result.stderr(), containsString("user is missing the Overall/Administer permission"));
102109
assertThat(result, failedWith(1));
103-
110+
104111
// config file not touched
105112
String configFileContent = folder.getConfigFile().asString();
106113
assertThat(configFileContent, not(containsString("FileOnMasterPrivateKeySource")));
@@ -111,7 +118,7 @@ public void userWithoutRunScripts_cannotMigrateDangerousPrivateKeySource() throw
111118
.authorizedTo(Jenkins.ADMINISTER)
112119
.withStdin(updateFolder.read())
113120
.invokeWithArgs("folder1");
114-
121+
115122
assertThat(result, succeeded());
116123
String configFileContent = folder.getConfigFile().asString();
117124
assertThat(configFileContent, containsString("BasicSSHUserPrivateKey"));

src/test/java/com/cloudbees/jenkins/plugins/sshcredentials/impl/TrileadSSHPasswordAuthenticatorTest.java

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
import hudson.slaves.DumbSlave;
3737
import jenkins.security.MasterToSlaveCallable;
3838

39-
import org.junit.After;
40-
import org.junit.Before;
41-
import org.junit.Rule;
42-
import org.junit.Test;
39+
import org.junit.jupiter.api.AfterEach;
40+
import org.junit.jupiter.api.BeforeEach;
41+
import org.junit.jupiter.api.Disabled;
42+
import org.junit.jupiter.api.Test;
4343
import org.jvnet.hudson.test.JenkinsRule;
44+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
4445

46+
import java.io.Serial;
4547
import java.lang.reflect.InvocationTargetException;
4648
import java.lang.reflect.Proxy;
4749
import java.util.Collections;
@@ -50,24 +52,31 @@
5052
import java.util.logging.Logger;
5153

5254
import static org.hamcrest.CoreMatchers.is;
53-
import static org.junit.Assert.assertNotNull;
5455
import static org.hamcrest.MatcherAssert.assertThat;
56+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5557

56-
public class TrileadSSHPasswordAuthenticatorTest {
58+
@WithJenkins
59+
class TrileadSSHPasswordAuthenticatorTest {
5760

5861
private Connection connection;
5962
private StandardUsernamePasswordCredentials user;
6063
private Object sshd;
6164

62-
@Rule public JenkinsRule r = new JenkinsRule();
63-
64-
@After
65-
public void tearDown() {
65+
private JenkinsRule r;
66+
67+
@BeforeEach
68+
void setUp(JenkinsRule rule) {
69+
r = rule;
70+
user = (StandardUsernamePasswordCredentials) Items.XSTREAM.fromXML(Items.XSTREAM.toXML(new BasicSSHUserPassword(CredentialsScope.SYSTEM, null, "foobar", "foomanchu", null)));
71+
}
72+
73+
@AfterEach
74+
void tearDown() {
6675
if (connection != null) {
6776
connection.close();
6877
connection = null;
6978
}
70-
if (sshd!=null) {
79+
if (sshd != null) {
7180
try {
7281
invoke(sshd, "stop", new Class<?>[] {Boolean.TYPE}, new Object[] {true});
7382
} catch (Throwable t) {
@@ -76,9 +85,10 @@ public void tearDown() {
7685
}
7786
}
7887

79-
// disabled as Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator
80-
// so this test relies on having a local sshd which is keyboard interactive only
81-
public void dontTestKeyboardInteractive() throws Exception {
88+
@Test
89+
@Disabled("Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator " +
90+
"so this test relies on having a local sshd which is keyboard interactive only")
91+
void dontTestKeyboardInteractive() throws Exception {
8292
connection = new Connection("localhost");
8393
connection.connect(new NoVerifier());
8494
TrileadSSHPasswordAuthenticator instance =
@@ -91,13 +101,8 @@ public void dontTestKeyboardInteractive() throws Exception {
91101
assertThat(instance.isAuthenticated(), is(true));
92102
}
93103

94-
@Before
95-
public void setUp() {
96-
user =(StandardUsernamePasswordCredentials) Items.XSTREAM.fromXML(Items.XSTREAM.toXML(new BasicSSHUserPassword(CredentialsScope.SYSTEM, null, "foobar", "foomanchu", null)));
97-
}
98-
99104
@Test
100-
public void testPassword() throws Exception {
105+
void testPassword() throws Exception {
101106
sshd = createPasswordAuthenticatedSshServer();
102107
invoke(sshd, "start", null, null);
103108
int port = (Integer)invoke(sshd, "getPort", null, null);
@@ -111,11 +116,11 @@ public void testPassword() throws Exception {
111116
assertThat(instance.isAuthenticated(), is(true));
112117
}
113118

114-
private Object createPasswordAuthenticatedSshServer() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException {
119+
private Object createPasswordAuthenticatedSshServer() throws Exception {
115120
return createPasswordAuthenticatedSshServer(null);
116121
}
117122

118-
private Object createPasswordAuthenticatedSshServer(final String username) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException {
123+
private Object createPasswordAuthenticatedSshServer(final String username) throws Exception {
119124
Object sshd = newDefaultSshServer();
120125
Class<?> keyPairProviderClass = newKeyPairProviderClass();
121126
Object provider = newProvider();
@@ -132,7 +137,7 @@ private Object createPasswordAuthenticatedSshServer(final String username) throw
132137
}
133138

134139
@Test
135-
public void testFactory() throws Exception {
140+
void testFactory() throws Exception {
136141
sshd = createPasswordAuthenticatedSshServer();
137142
invoke(sshd, "start", null, null);
138143
int port = (Integer)invoke(sshd, "getPort", null, null);
@@ -146,7 +151,7 @@ public void testFactory() throws Exception {
146151
}
147152

148153
@Test
149-
public void testFactoryAltUsername() throws Exception {
154+
void testFactoryAltUsername() throws Exception {
150155
sshd = createPasswordAuthenticatedSshServer("bill");
151156
invoke(sshd, "start", null, null);
152157
int port = (Integer)invoke(sshd, "getPort", null, null);
@@ -170,7 +175,7 @@ public void testFactoryAltUsername() throws Exception {
170175
* Brings the {@link SSHAuthenticatorFactory} to a slave.
171176
*/
172177
@Test
173-
public void testSlave() throws Exception {
178+
void testSlave() throws Exception {
174179
Object sshd = createPasswordAuthenticatedSshServer();
175180
invoke(sshd, "start", null, null);
176181

@@ -216,15 +221,16 @@ public Void call() throws Exception {
216221
return null;
217222
}
218223

224+
@Serial
219225
private static final long serialVersionUID = 1L;
220226
}
221227

222-
private Object invoke(Object target, String methodName, Class<?>[] parameterTypes, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
228+
private Object invoke(Object target, String methodName, Class<?>[] parameterTypes, Object[] args) throws Exception {
223229
return target.getClass().getMethod(methodName, parameterTypes).invoke(target, args);
224230
}
225231

226-
private Object newDefaultSshServer() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
227-
Object server = null;
232+
private Object newDefaultSshServer() throws Exception {
233+
Object server;
228234
Class<?> serverClass;
229235
try {
230236
serverClass = Class.forName("org.apache.sshd.SshServer");
@@ -238,7 +244,7 @@ private Object newDefaultSshServer() throws ClassNotFoundException, NoSuchMethod
238244
return server;
239245
}
240246

241-
private Class<?> newKeyPairProviderClass() throws ClassNotFoundException {
247+
private Class<?> newKeyPairProviderClass() throws Exception {
242248
Class<?> keyPairProviderClass;
243249
try {
244250
keyPairProviderClass = Class.forName("org.apache.sshd.common.KeyPairProvider");
@@ -249,15 +255,15 @@ private Class<?> newKeyPairProviderClass() throws ClassNotFoundException {
249255
return keyPairProviderClass;
250256
}
251257

252-
private Object newProvider() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
258+
private Object newProvider() throws Exception {
253259
Class<?> providerClass = Class.forName("org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider");
254260
Object provider = providerClass.getConstructor().newInstance();
255261
assertNotNull(provider);
256262

257263
return provider;
258264
}
259265

260-
private Class<?> newAuthenticatorClass() throws ClassNotFoundException {
266+
private Class<?> newAuthenticatorClass() throws Exception {
261267
Class<?> authenticatorClass;
262268
try {
263269
authenticatorClass = Class.forName("org.apache.sshd.server.auth.password.PasswordAuthenticator");
@@ -268,7 +274,7 @@ private Class<?> newAuthenticatorClass() throws ClassNotFoundException {
268274
return authenticatorClass;
269275
}
270276

271-
private Object newAuthenticator(Class<?> authenticatorClass, final String userName) throws IllegalArgumentException {
277+
private Object newAuthenticator(Class<?> authenticatorClass, final String userName) {
272278
Object authenticator = Proxy.newProxyInstance(
273279
authenticatorClass.getClassLoader(), new Class<?>[]{authenticatorClass}, (proxy, method, args) ->
274280
method.getName().equals("authenticate") ?
@@ -278,8 +284,8 @@ private Object newAuthenticator(Class<?> authenticatorClass, final String userNa
278284
return authenticator;
279285
}
280286

281-
private Object newFactory() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
282-
Object factory = null;
287+
private Object newFactory() throws Exception {
288+
Object factory;
283289
Class<?> factoryClass;
284290
try {
285291
factoryClass = Class.forName("org.apache.sshd.server.auth.UserAuthPassword$Factory");

0 commit comments

Comments
 (0)