mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 07:10:53 +03:00
Rewritten integration tests
This commit is contained in:
135
src/test/java/com/hierynomus/sshj/SshFixture.java
Normal file
135
src/test/java/com/hierynomus/sshj/SshFixture.java
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,40 +1,27 @@
|
||||
package com.hierynomus.sshj.connection.channel;
|
||||
|
||||
import com.hierynomus.sshj.SshIntegrationTestBase;
|
||||
import net.schmizz.sshj.SSHClient;
|
||||
import com.hierynomus.sshj.SshFixture;
|
||||
import net.schmizz.sshj.connection.channel.direct.Session;
|
||||
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
public class ChannelCloseEofTest extends SshIntegrationTestBase {
|
||||
private SSHClient sshClient;
|
||||
public class ChannelCloseEofTest {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
sshClient = new SSHClient();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws IOException {
|
||||
sshClient.disconnect();
|
||||
}
|
||||
@Rule
|
||||
public SshFixture fixture = new SshFixture();
|
||||
|
||||
@Test
|
||||
public void shouldCorrectlyHandleSessionChannelEof() throws IOException, InterruptedException {
|
||||
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
|
||||
sshClient.connect(server.getHost(), server.getPort());
|
||||
sshClient.authPassword("jeroen", "jeroen");
|
||||
Session session = sshClient.startSession();
|
||||
fixture.setupConnectedDefaultClient().authPassword("jeroen", "jeroen");
|
||||
Session session = fixture.getClient().startSession();
|
||||
session.allocateDefaultPTY();
|
||||
session.close();
|
||||
Thread.sleep(1000);
|
||||
assertThat("Should still be connected", sshClient.isConnected());
|
||||
assertThat("Should still be connected", fixture.getClient().isConnected());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,58 +1,39 @@
|
||||
/**
|
||||
* 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;
|
||||
package com.hierynomus.sshj.transport;
|
||||
|
||||
import com.hierynomus.sshj.SshFixture;
|
||||
import net.schmizz.sshj.SSHClient;
|
||||
import net.schmizz.sshj.common.DisconnectReason;
|
||||
import net.schmizz.sshj.util.BasicFixture;
|
||||
import org.junit.After;
|
||||
import net.schmizz.sshj.transport.DisconnectListener;
|
||||
import net.schmizz.sshj.transport.TransportException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class Disconnection {
|
||||
public class DisconnectionTest {
|
||||
private AtomicBoolean disconnected = null;
|
||||
|
||||
private final BasicFixture fixture = new BasicFixture();
|
||||
|
||||
private boolean notified;
|
||||
@Rule
|
||||
public SshFixture fixture = new SshFixture();
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
throws IOException {
|
||||
fixture.init();
|
||||
|
||||
notified = false;
|
||||
|
||||
fixture.getClient().getTransport().setDisconnectListener(new DisconnectListener() {
|
||||
public void setupFlag() throws IOException {
|
||||
disconnected = new AtomicBoolean(false);
|
||||
// Initialize the client
|
||||
SSHClient defaultClient = fixture.setupDefaultClient();
|
||||
defaultClient.getTransport().setDisconnectListener(new DisconnectListener() {
|
||||
@Override
|
||||
public void notifyDisconnect(DisconnectReason reason, String message) {
|
||||
notified = true;
|
||||
disconnected.set(true);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown()
|
||||
throws IOException, InterruptedException {
|
||||
fixture.done();
|
||||
fixture.connectClient(defaultClient);
|
||||
}
|
||||
|
||||
private boolean joinToClientTransport(int seconds) {
|
||||
@@ -67,8 +48,8 @@ public class Disconnection {
|
||||
@Test
|
||||
public void listenerNotifiedOnClientDisconnect()
|
||||
throws IOException {
|
||||
fixture.stopClient();
|
||||
assertTrue(notified);
|
||||
fixture.getClient().disconnect();
|
||||
assertTrue(disconnected.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -76,13 +57,13 @@ public class Disconnection {
|
||||
throws InterruptedException, IOException {
|
||||
fixture.stopServer();
|
||||
joinToClientTransport(2);
|
||||
assertTrue(notified);
|
||||
assertTrue(disconnected.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void joinNotifiedOnClientDisconnect()
|
||||
throws IOException {
|
||||
fixture.stopClient();
|
||||
fixture.getClient().disconnect();
|
||||
assertTrue(joinToClientTransport(2));
|
||||
}
|
||||
|
||||
@@ -93,4 +74,4 @@ public class Disconnection {
|
||||
assertFalse(joinToClientTransport(2));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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 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 {
|
||||
|
||||
@Rule
|
||||
public SshFixture fixture = new SshFixture();
|
||||
|
||||
private static final String LOGIN_CONTEXT_NAME = "TestLoginContext";
|
||||
|
||||
private static class TestAuthConfiguration extends Configuration {
|
||||
private AppConfigurationEntry entry = new AppConfigurationEntry(
|
||||
"testLoginModule",
|
||||
LoginModuleControlFlag.REQUIRED,
|
||||
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
|
||||
Collections.<String, Object> emptyMap());
|
||||
|
||||
@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
|
||||
public void authenticated() throws Exception {
|
||||
AuthGssApiWithMic authMethod = new AuthGssApiWithMic(
|
||||
@@ -59,8 +44,9 @@ public class GssApiTest {
|
||||
Collections.singletonList(BogusGSSManager.KRB5_MECH),
|
||||
new BogusGSSManager());
|
||||
|
||||
fixture.getClient().auth("user", authMethod);
|
||||
assertTrue(fixture.getClient().isAuthenticated());
|
||||
SSHClient defaultClient = fixture.setupConnectedDefaultClient();
|
||||
defaultClient.auth("user", authMethod);
|
||||
assertTrue(defaultClient.isAuthenticated());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user