/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 org.apache.sshd.client.channel; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; import org.apache.sshd.client.SshClient; import org.apache.sshd.client.session.ClientSession; import org.apache.sshd.server.SshServer; import org.apache.sshd.util.test.BaseTestSupport; import org.apache.sshd.util.test.CommandExecutionHelper; import org.apache.sshd.util.test.Utils; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; /** * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a> */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ChannelExecTest extends BaseTestSupport { private static SshServer sshd; private static int port; private static SshClient client; public ChannelExecTest() { super(); } @BeforeClass public static void setupClientAndServer() throws Exception { sshd = Utils.setupTestServer(ChannelExecTest.class); sshd.setCommandFactory(command -> new CommandExecutionHelper(command) { @Override protected boolean handleCommandLine(String command) throws Exception { OutputStream stdout = getOut(); stdout.write(command.getBytes(StandardCharsets.US_ASCII)); stdout.flush(); return false; } }); sshd.start(); port = sshd.getPort(); client = Utils.setupTestClient(ChannelExecTest.class); client.start(); } @AfterClass public static void tearDownClientAndServer() throws Exception { if (sshd != null) { try { sshd.stop(true); } finally { sshd = null; } } if (client != null) { try { client.stop(); } finally { client = null; } } } @Test // see SSHD-692 public void testMultipleRemoteCommandExecutions() throws Exception { try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); for (int index = 1; index <= Byte.SIZE; index++) { String expected = getCurrentTestName() + "[" + index + "]"; String actual = session.executeRemoteCommand(expected + "\n"); assertEquals("Mismatched reply", expected, actual); } } } }