Skip to content
Permalink
Browse files
[JENKINS-34336] Use JenkinsRule in tests
  • Loading branch information
armfergom committed Apr 19, 2016
1 parent 45e1d5e commit 23d013ab5e8719debab7227044b7fc959571d4f5
Showing 4 changed files with 60 additions and 34 deletions.
@@ -24,9 +24,7 @@
package com.cloudbees.jenkins.plugins.sshcredentials.impl;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator;
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPassword;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.jcraft.jsch.HostKey;
import com.jcraft.jsch.HostKeyRepository;
@@ -40,29 +38,33 @@
import org.apache.sshd.server.auth.UserAuthPassword;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class JSchSSHPasswordAuthenticatorTest extends HudsonTestCase {
public class JSchSSHPasswordAuthenticatorTest {

private JSchConnector connector;
private StandardUsernamePasswordCredentials user;

@Override
protected void tearDown() throws Exception {
@Rule public JenkinsRule r = new JenkinsRule();

@After
public void tearDown() throws Exception {
if (connector != null) {
connector.close();
connector = null;
}
super.tearDown();
}

// disabled as Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator
@@ -87,11 +89,12 @@ public void dontTestKeyboardInteractive() throws Exception {
assertThat(connector.getSession().isConnected(), is(true));
}

protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
user =(StandardUsernamePasswordCredentials) Items.XSTREAM.fromXML(Items.XSTREAM.toXML(new BasicSSHUserPassword(CredentialsScope.SYSTEM, null, "foobar", "foomanchu", null)));
}

@Test
public void testPassword() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
@@ -123,6 +126,7 @@ public boolean authenticate(String username, String password, ServerSession sess
}
}

@Test
public void testFactory() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
@@ -27,8 +27,6 @@
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.ServerHostKeyVerifier;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.util.Secret;
@@ -39,7 +37,11 @@
import org.apache.sshd.server.auth.UserAuthPublicKey;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.security.PublicKey;
import java.util.Arrays;
@@ -52,22 +54,23 @@
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class JSchSSHPublicKeyAuthenticatorTest extends HudsonTestCase {
public class JSchSSHPublicKeyAuthenticatorTest {

private JSchConnector connector;
private SSHUserPrivateKey user;

@Override
protected void tearDown() throws Exception {
@Rule public JenkinsRule r = new JenkinsRule();

@After
public void tearDown() throws Exception {
if (connector != null) {
connector.close();
connector = null;
}
super.tearDown();
}

protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
user = new SSHUserPrivateKey() {

@NonNull
@@ -132,6 +135,7 @@ public List<String> getPrivateKeys() {
};
}

@Test
public void testAuthenticate() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
@@ -164,6 +168,7 @@ public boolean authenticate(String username, PublicKey key, ServerSession sessio
}
}

@Test
public void testFactory() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
@@ -41,7 +41,11 @@
import org.apache.sshd.server.auth.UserAuthPassword;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Arrays;
import java.util.logging.Level;
@@ -50,14 +54,16 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class TrileadSSHPasswordAuthenticatorTest extends HudsonTestCase {
public class TrileadSSHPasswordAuthenticatorTest {

private Connection connection;
private StandardUsernamePasswordCredentials user;
private SshServer sshd;

@Override
protected void tearDown() throws Exception {
@Rule public JenkinsRule r = new JenkinsRule();

@After
public void tearDown() throws Exception {
if (connection != null) {
connection.close();
connection = null;
@@ -69,7 +75,6 @@ protected void tearDown() throws Exception {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Problems shutting down ssh server", t);
}
}
super.tearDown();
}

// disabled as Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator
@@ -87,11 +92,12 @@ public void dontTestKeyboardInteractive() throws Exception {
assertThat(instance.isAuthenticated(), is(true));
}

protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
user =(StandardUsernamePasswordCredentials) Items.XSTREAM.fromXML(Items.XSTREAM.toXML(new BasicSSHUserPassword(CredentialsScope.SYSTEM, null, "foobar", "foomanchu", null)));
}

@Test
public void testPassword() throws Exception {
sshd = createPasswordAuthenticatedSshServer();
sshd.start();
@@ -122,6 +128,7 @@ public boolean authenticate(String _username, String password, ServerSession ses
return sshd;
}

@Test
public void testFactory() throws Exception {
sshd = createPasswordAuthenticatedSshServer();
sshd.start();
@@ -134,6 +141,7 @@ public void testFactory() throws Exception {
assertThat(instance.isAuthenticated(), is(true));
}

@Test
public void testFactoryAltUsername() throws Exception {
sshd = createPasswordAuthenticatedSshServer("bill");
sshd.start();
@@ -156,11 +164,12 @@ public void testFactoryAltUsername() throws Exception {
/**
* Brings the {@link SSHAuthenticatorFactory} to a slave.
*/
@Test
public void testSlave() throws Exception {
SshServer sshd = createPasswordAuthenticatedSshServer();
sshd.start();

DumbSlave s = createSlave();
DumbSlave s = r.createSlave();
Computer c = s.toComputer();
c.connect(false).get();

@@ -39,7 +39,11 @@
import org.apache.sshd.server.auth.UserAuthPublicKey;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.security.PublicKey;
import java.util.Arrays;
@@ -51,22 +55,23 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class TrileadSSHPublicKeyAuthenticatorTest extends HudsonTestCase {
public class TrileadSSHPublicKeyAuthenticatorTest {

private Connection connection;
private SSHUserPrivateKey user;

@Override
protected void tearDown() throws Exception {
@Rule public JenkinsRule r = new JenkinsRule();

@After
public void tearDown() throws Exception {
if (connection != null) {
connection.close();
connection = null;
}
super.tearDown();
}

protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
user = new SSHUserPrivateKey() {

@NonNull
@@ -130,6 +135,7 @@ public List<String> getPrivateKeys() {
};
}

@Test
public void testAuthenticate() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
@@ -165,6 +171,7 @@ public boolean verifyServerHostKey(String hostname, int port, String serverHostK
}
}

@Test
public void testFactory() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
@@ -199,6 +206,7 @@ public boolean verifyServerHostKey(String hostname, int port, String serverHostK
}
}

@Test
public void testAltUsername() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);

0 comments on commit 23d013a

Please sign in to comment.