Modified: mina/sshd/trunk/src/main/java/org/apache/sshd/server/session/ServerSession.java URL: http://svn.apache.org/viewvc/mina/sshd/trunk/src/main/java/org/apache/sshd/server/session/ServerSession.java?rev=728642&r1=728641&r2=728642&view=diff ============================================================================== --- mina/sshd/trunk/src/main/java/org/apache/sshd/server/session/ServerSession.java (original) +++ mina/sshd/trunk/src/main/java/org/apache/sshd/server/session/ServerSession.java Mon Dec 22 02:48:39 2008 @@ -35,6 +35,7 @@ import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.SshException; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.future.CloseFuture; import org.apache.sshd.common.util.Buffer; import org.apache.mina.transport.socket.SocketSessionConfig; import org.apache.mina.core.session.IoSession; @@ -82,9 +83,9 @@ } @Override - public void close() { - super.close(); + public CloseFuture close(boolean immediately) { unscheduleAuthTimer(); + return super.close(immediately); } public String getNegociated(int index) { @@ -111,7 +112,7 @@ int code = buffer.getInt(); String msg = buffer.getString(); log.info("Received SSH_MSG_DISCONNECT (reason={}, msg={})", code, msg); - close(); + close(false); break; } case SSH_MSG_UNIMPLEMENTED: { @@ -246,10 +247,9 @@ } private void processAuthTimer() throws IOException { - if (!closed && !authed) { + if (!authed) { disconnect(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR, "User authentication has timed out"); - close(); } } @@ -355,6 +355,16 @@ log.info("Received SSH_MSG_CHANNEL_OPEN {}", type); + if (closing) { + buffer = createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE); + buffer.putInt(id); + buffer.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED); + buffer.putString("SSH server is shutting down: " + type); + buffer.putString(""); + writePacket(buffer); + return; + } + ServerChannel channel = null; for (NamedFactory<ServerChannel> factory : getServerFactoryManager().getChannelFactories()) { if (factory.getName().equals(type)) {
Modified: mina/sshd/trunk/src/test/java/org/apache/sshd/CipherTest.java URL: http://svn.apache.org/viewvc/mina/sshd/trunk/src/test/java/org/apache/sshd/CipherTest.java?rev=728642&r1=728641&r2=728642&view=diff ============================================================================== --- mina/sshd/trunk/src/test/java/org/apache/sshd/CipherTest.java (original) +++ mina/sshd/trunk/src/test/java/org/apache/sshd/CipherTest.java Mon Dec 22 02:48:39 2008 @@ -102,7 +102,6 @@ public void tearDown() throws Exception { if (sshd != null) { sshd.stop(); - Thread.sleep(50); } } Modified: mina/sshd/trunk/src/test/java/org/apache/sshd/ClientTest.java URL: http://svn.apache.org/viewvc/mina/sshd/trunk/src/test/java/org/apache/sshd/ClientTest.java?rev=728642&r1=728641&r2=728642&view=diff ============================================================================== --- mina/sshd/trunk/src/test/java/org/apache/sshd/ClientTest.java (original) +++ mina/sshd/trunk/src/test/java/org/apache/sshd/ClientTest.java Mon Dec 22 02:48:39 2008 @@ -21,14 +21,19 @@ import java.io.PipedOutputStream; import java.io.PipedInputStream; import java.io.ByteArrayOutputStream; +import java.io.ByteArrayInputStream; import java.net.ServerSocket; import org.apache.sshd.common.keyprovider.FileKeyPairProvider; import org.apache.sshd.common.util.BufferUtils; +import org.apache.sshd.common.SshException; +import org.apache.sshd.common.future.CloseFuture; import org.apache.sshd.ClientChannel; import org.apache.sshd.ClientSession; import org.apache.sshd.SshClient; import org.apache.sshd.SshServer; +import org.apache.sshd.client.future.AuthFuture; +import org.apache.sshd.client.future.OpenFuture; import org.apache.sshd.util.EchoShellFactory; import org.apache.sshd.util.BogusPasswordAuthenticator; import org.apache.sshd.util.TeePipedOutputStream; @@ -75,9 +80,11 @@ public void testClient() throws Exception { SshClient client = SshClient.setUpDefaultClient(); client.start(); - ClientSession session = client.connect("localhost", port); - session.authPassword("smx", "smx"); - ClientChannel channel = session.createChannel("shell"); + ClientSession session = client.connect("localhost", port).await().getSession(); + session.authPassword("smx", "smx").await().isSuccess(); + ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL); + + ByteArrayOutputStream sent = new ByteArrayOutputStream(); PipedOutputStream pipedIn = new TeePipedOutputStream(sent); channel.setIn(new PipedInputStream(pipedIn)); @@ -102,7 +109,7 @@ channel.waitFor(ClientChannel.CLOSED, 0); - channel.close(); + channel.close(false); client.stop(); assertArrayEquals(sent.toByteArray(), out.toByteArray()); @@ -117,9 +124,9 @@ // sshd.getProperties().put(SshServer.WINDOW_SIZE, Integer.toString(0x20000)); // sshd.getProperties().put(SshServer.MAX_PACKET_SIZE, Integer.toString(0x1000)); client.start(); - ClientSession session = client.connect("localhost", port); + ClientSession session = client.connect("localhost", port).await().getSession(); session.authPassword("smx", "smx"); - ClientChannel channel = session.createChannel("shell"); + ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL); ByteArrayOutputStream sent = new ByteArrayOutputStream(); PipedOutputStream pipedIn = new TeePipedOutputStream(sent); channel.setIn(new PipedInputStream(pipedIn)); @@ -127,7 +134,7 @@ ByteArrayOutputStream err = new ByteArrayOutputStream(); channel.setOut(out); channel.setErr(err); - channel.open(); + channel.open().await(); long t0 = System.currentTimeMillis(); @@ -152,13 +159,81 @@ channel.waitFor(ClientChannel.CLOSED, 0); - channel.close(); + channel.close(false); client.stop(); assertTrue(BufferUtils.equals(sent.toByteArray(), out.toByteArray())); //assertArrayEquals(sent.toByteArray(), out.toByteArray()); } + @Test(expected = SshException.class) + public void testOpenChannelOnClosedSession() throws Exception { + SshClient client = SshClient.setUpDefaultClient(); + client.start(); + ClientSession session = client.connect("localhost", port).await().getSession(); + session.authPassword("smx", "smx").await().isSuccess(); + ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL); + session.close(false); + + ByteArrayOutputStream sent = new ByteArrayOutputStream(); + PipedOutputStream pipedIn = new TeePipedOutputStream(sent); + channel.setIn(new PipedInputStream(pipedIn)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + channel.setOut(out); + channel.setErr(err); + channel.open(); + } + + @Test + public void testCloseBeforeAuthSucceed() throws Exception { + SshClient client = SshClient.setUpDefaultClient(); + client.start(); + ClientSession session = client.connect("localhost", port).await().getSession(); + AuthFuture authFuture = session.authPassword("smx", "smx"); + CloseFuture closeFuture = session.close(false); + authFuture.await(); + closeFuture.await(); + assertNotNull(authFuture.getException()); + + } + + @Test + public void testCloseCleanBeforeChannelOpened() throws Exception { + SshClient client = SshClient.setUpDefaultClient(); + client.start(); + ClientSession session = client.connect("localhost", port).await().getSession(); + session.authPassword("smx", "smx").await(); + ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL); + channel.setIn(new ByteArrayInputStream(new byte[0])); + channel.setOut(new ByteArrayOutputStream()); + channel.setErr(new ByteArrayOutputStream()); + OpenFuture openFuture = channel.open(); + CloseFuture closeFuture = session.close(false); + openFuture.await(); + closeFuture.await(); + assertNotNull(openFuture.isOpened()); + assertNotNull(closeFuture.isClosed()); + } + + @Test + public void testCloseImmediateBeforeChannelOpened() throws Exception { + SshClient client = SshClient.setUpDefaultClient(); + client.start(); + ClientSession session = client.connect("localhost", port).await().getSession(); + session.authPassword("smx", "smx").await(); + ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL); + channel.setIn(new ByteArrayInputStream(new byte[0])); + channel.setOut(new ByteArrayOutputStream()); + channel.setErr(new ByteArrayOutputStream()); + OpenFuture openFuture = channel.open(); + CloseFuture closeFuture = session.close(true); + openFuture.await(); + closeFuture.await(); + assertNotNull(openFuture.getException()); + assertNotNull(closeFuture.isClosed()); + } + public static void main(String[] args) throws Exception { SshClient.main(args); } Modified: mina/sshd/trunk/src/test/java/org/apache/sshd/ServerTest.java URL: http://svn.apache.org/viewvc/mina/sshd/trunk/src/test/java/org/apache/sshd/ServerTest.java?rev=728642&r1=728641&r2=728642&view=diff ============================================================================== --- mina/sshd/trunk/src/test/java/org/apache/sshd/ServerTest.java (original) +++ mina/sshd/trunk/src/test/java/org/apache/sshd/ServerTest.java Mon Dec 22 02:48:39 2008 @@ -72,7 +72,7 @@ SshClient client = SshClient.setUpDefaultClient(); client.start(); - ClientSession s = client.connect("localhost", port); + ClientSession s = client.connect("localhost", port).await().getSession(); int nbTrials = 0; int res = 0; while ((res & ClientSession.CLOSED) == 0) { @@ -89,8 +89,8 @@ SshClient client = SshClient.setUpDefaultClient(); client.start(); - ClientSession s = client.connect("localhost", port); - int res = s.waitFor(ClientSession.CLOSED, 1500); + ClientSession s = client.connect("localhost", port).await().getSession(); + int res = s.waitFor(ClientSession.CLOSED, 5000); Assert.assertTrue((res & ClientSession.CLOSED) != 0); } Modified: mina/sshd/trunk/src/test/resources/log4j.properties URL: http://svn.apache.org/viewvc/mina/sshd/trunk/src/test/resources/log4j.properties?rev=728642&r1=728641&r2=728642&view=diff ============================================================================== --- mina/sshd/trunk/src/test/resources/log4j.properties (original) +++ mina/sshd/trunk/src/test/resources/log4j.properties Mon Dec 22 02:48:39 2008 @@ -21,7 +21,7 @@ # # The logging properties used during tests.. # -log4j.rootLogger=WARN, stdout +log4j.rootLogger=DEBUG, stdout #log4j.logger.org.apache.mina=TRACE #log4j.logger.org.apache.sshd.common.channel.Window=DEBUG
