Rewritten integration tests

This commit is contained in:
Jeroen van Erp
2015-06-17 12:11:58 +02:00
parent 8776500fa0
commit e334525da5
5 changed files with 186 additions and 157 deletions

View File

@@ -0,0 +1,135 @@
package com.hierynomus.sshj;
import net.schmizz.sshj.Config;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.util.gss.BogusGSSAuthenticator;
import org.apache.sshd.SshServer;
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
import org.apache.sshd.server.PasswordAuthenticator;
import org.apache.sshd.server.session.ServerSession;
import org.junit.rules.ExternalResource;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Can be used as a rule to ensure the server is teared down after each test.
*/
public class SshFixture extends ExternalResource {
public static final String hostkey = "src/test/resources/hostkey.pem";
public static final String fingerprint = "ce:a7:c1:cf:17:3f:96:49:6a:53:1a:05:0b:ba:90:db";
private SshServer server = defaultSshServer();
private SSHClient client = null;
private AtomicBoolean started = new AtomicBoolean(false);
private boolean autoStart = true;
public SshFixture(boolean autoStart) {
this.autoStart = autoStart;
}
public SshFixture() {
}
@Override
protected void before() throws Throwable {
if (autoStart) {
start();
}
}
@Override
protected void after() {
stopClient();
stopServer();
}
public void start() throws IOException {
server.start();
started.set(true);
}
public SSHClient setupConnectedDefaultClient() throws IOException {
return connectClient(setupDefaultClient());
}
public SSHClient setupDefaultClient() {
return setupClient(new DefaultConfig());
}
public SSHClient setupClient(Config config) {
if (client == null) {
client = new SSHClient(config);
client.addHostKeyVerifier(fingerprint);
}
return client;
}
public SSHClient getClient() {
if (client != null) {
return client;
}
throw new IllegalStateException("First call one of the setup*Client methods");
}
public SSHClient connectClient(SSHClient client) throws IOException {
client.connect(server.getHost(), server.getPort());
return client;
}
private SshServer defaultSshServer() {
SshServer sshServer = SshServer.setUpDefaultServer();
sshServer.setPort(randomPort());
sshServer.setKeyPairProvider(new FileKeyPairProvider(new String[]{hostkey}));
sshServer.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(String username, String password, ServerSession session) {
return username.equals(password);
}
});
sshServer.setGSSAuthenticator(new BogusGSSAuthenticator());
return sshServer;
}
private int randomPort() {
try {
ServerSocket s = null;
try {
s = new ServerSocket(0);
return s.getLocalPort();
} finally {
if (s != null)
s.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void stopClient() {
if (client != null && client.isConnected()) {
try {
client.disconnect();
} catch (IOException e) {
// Ignore
} finally {
client = null;
}
} else if (client != null) {
client = null;
}
}
public void stopServer() {
if (started.get()) {
try {
server.stop();
} catch (InterruptedException e) {
// ignore
}
}
}
}

View File

@@ -1,60 +0,0 @@
package com.hierynomus.sshj;
import org.apache.sshd.SshServer;
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
import org.apache.sshd.server.PasswordAuthenticator;
import org.apache.sshd.server.session.ServerSession;
import org.junit.After;
import org.junit.Before;
import java.io.IOException;
import java.net.ServerSocket;
public class SshIntegrationTestBase {
public static final String hostkey = "src/test/resources/hostkey.pem";
public static final String fingerprint = "ce:a7:c1:cf:17:3f:96:49:6a:53:1a:05:0b:ba:90:db";
public static final String hostname = "localhost";
protected SshServer server = null;
@Before
public void setupSshServer() throws IOException {
server = SshServer.setUpDefaultServer();
server.setPort(randomPort());
configureSshServer();
server.start();
}
@After
public void stopSshServer() throws Exception {
server.stop();
}
protected void configureSshServer() {
server.setKeyPairProvider(new FileKeyPairProvider(new String[]{hostkey}));
server.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(String username, String password, ServerSession session) {
return username.equals(password);
}
});
}
private int randomPort() {
try {
ServerSocket s = null;
try {
s = new ServerSocket(0);
return s.getLocalPort();
} finally {
if (s != null)
s.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,40 +1,27 @@
package com.hierynomus.sshj.connection.channel; package com.hierynomus.sshj.connection.channel;
import com.hierynomus.sshj.SshIntegrationTestBase; import com.hierynomus.sshj.SshFixture;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier; import org.junit.Rule;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
public class ChannelCloseEofTest extends SshIntegrationTestBase { public class ChannelCloseEofTest {
private SSHClient sshClient;
@Before @Rule
public void setUp() throws Exception { public SshFixture fixture = new SshFixture();
sshClient = new SSHClient();
}
@After
public void tearDown() throws IOException {
sshClient.disconnect();
}
@Test @Test
public void shouldCorrectlyHandleSessionChannelEof() throws IOException, InterruptedException { public void shouldCorrectlyHandleSessionChannelEof() throws IOException, InterruptedException {
sshClient.addHostKeyVerifier(new PromiscuousVerifier()); fixture.setupConnectedDefaultClient().authPassword("jeroen", "jeroen");
sshClient.connect(server.getHost(), server.getPort()); Session session = fixture.getClient().startSession();
sshClient.authPassword("jeroen", "jeroen");
Session session = sshClient.startSession();
session.allocateDefaultPTY(); session.allocateDefaultPTY();
session.close(); session.close();
Thread.sleep(1000); Thread.sleep(1000);
assertThat("Should still be connected", sshClient.isConnected()); assertThat("Should still be connected", fixture.getClient().isConnected());
} }
} }

View File

@@ -1,58 +1,39 @@
/** package com.hierynomus.sshj.transport;
* Copyright 2009 sshj contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.transport;
import com.hierynomus.sshj.SshFixture;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.DisconnectReason; import net.schmizz.sshj.common.DisconnectReason;
import net.schmizz.sshj.util.BasicFixture; import net.schmizz.sshj.transport.DisconnectListener;
import org.junit.After; import net.schmizz.sshj.transport.TransportException;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class Disconnection { public class DisconnectionTest {
private AtomicBoolean disconnected = null;
private final BasicFixture fixture = new BasicFixture(); @Rule
public SshFixture fixture = new SshFixture();
private boolean notified;
@Before @Before
public void setUp() public void setupFlag() throws IOException {
throws IOException { disconnected = new AtomicBoolean(false);
fixture.init(); // Initialize the client
SSHClient defaultClient = fixture.setupDefaultClient();
notified = false; defaultClient.getTransport().setDisconnectListener(new DisconnectListener() {
fixture.getClient().getTransport().setDisconnectListener(new DisconnectListener() {
@Override @Override
public void notifyDisconnect(DisconnectReason reason, String message) { public void notifyDisconnect(DisconnectReason reason, String message) {
notified = true; disconnected.set(true);
} }
}); });
fixture.connectClient(defaultClient);
}
@After
public void tearDown()
throws IOException, InterruptedException {
fixture.done();
} }
private boolean joinToClientTransport(int seconds) { private boolean joinToClientTransport(int seconds) {
@@ -67,8 +48,8 @@ public class Disconnection {
@Test @Test
public void listenerNotifiedOnClientDisconnect() public void listenerNotifiedOnClientDisconnect()
throws IOException { throws IOException {
fixture.stopClient(); fixture.getClient().disconnect();
assertTrue(notified); assertTrue(disconnected.get());
} }
@Test @Test
@@ -76,13 +57,13 @@ public class Disconnection {
throws InterruptedException, IOException { throws InterruptedException, IOException {
fixture.stopServer(); fixture.stopServer();
joinToClientTransport(2); joinToClientTransport(2);
assertTrue(notified); assertTrue(disconnected.get());
} }
@Test @Test
public void joinNotifiedOnClientDisconnect() public void joinNotifiedOnClientDisconnect()
throws IOException { throws IOException {
fixture.stopClient(); fixture.getClient().disconnect();
assertTrue(joinToClientTransport(2)); assertTrue(joinToClientTransport(2));
} }

View File

@@ -1,32 +1,30 @@
package net.schmizz.sshj.userauth; package com.hierynomus.sshj.userauth;
import com.hierynomus.sshj.SshFixture;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.userauth.method.AuthGssApiWithMic;
import net.schmizz.sshj.util.gss.BogusGSSManager;
import org.junit.Rule;
import org.junit.Test;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import java.util.Collections;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Collections;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import net.schmizz.sshj.userauth.method.AuthGssApiWithMic;
import net.schmizz.sshj.util.BasicFixture;
import net.schmizz.sshj.util.gss.BogusGSSAuthenticator;
import net.schmizz.sshj.util.gss.BogusGSSManager;
public class GssApiTest { public class GssApiTest {
@Rule
public SshFixture fixture = new SshFixture();
private static final String LOGIN_CONTEXT_NAME = "TestLoginContext"; private static final String LOGIN_CONTEXT_NAME = "TestLoginContext";
private static class TestAuthConfiguration extends Configuration { private static class TestAuthConfiguration extends Configuration {
private AppConfigurationEntry entry = new AppConfigurationEntry( private AppConfigurationEntry entry = new AppConfigurationEntry(
"testLoginModule", "testLoginModule",
LoginModuleControlFlag.REQUIRED, AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
Collections.<String, Object> emptyMap()); Collections.<String, Object> emptyMap());
@Override @Override
@@ -39,19 +37,6 @@ public class GssApiTest {
} }
} }
private final BasicFixture fixture = new BasicFixture();
@Before
public void setUp() throws Exception {
fixture.setGssAuthenticator(new BogusGSSAuthenticator());
fixture.init(false);
}
@After
public void tearDown() throws IOException, InterruptedException {
fixture.done();
}
@Test @Test
public void authenticated() throws Exception { public void authenticated() throws Exception {
AuthGssApiWithMic authMethod = new AuthGssApiWithMic( AuthGssApiWithMic authMethod = new AuthGssApiWithMic(
@@ -59,8 +44,9 @@ public class GssApiTest {
Collections.singletonList(BogusGSSManager.KRB5_MECH), Collections.singletonList(BogusGSSManager.KRB5_MECH),
new BogusGSSManager()); new BogusGSSManager());
fixture.getClient().auth("user", authMethod); SSHClient defaultClient = fixture.setupConnectedDefaultClient();
assertTrue(fixture.getClient().isAuthenticated()); defaultClient.auth("user", authMethod);
assertTrue(defaultClient.isAuthenticated());
} }
} }