Ayush0612005 commented on code in PR #31040: URL: https://github.com/apache/camel/pull/31040#discussion_r4726181768
########## components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshConfigurationErrorsTest.java: ########## @@ -0,0 +1,239 @@ +/* + * 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.camel.component.ssh; + +import java.io.IOException; +import java.nio.file.Paths; +import java.time.Duration; +import java.util.List; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.ResolveEndpointFailedException; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.sshd.client.SshClient; +import org.apache.sshd.common.NamedResource; +import org.apache.sshd.common.channel.Channel; +import org.apache.sshd.common.channel.ChannelFactory; +import org.apache.sshd.common.kex.AbstractDH; +import org.apache.sshd.common.kex.BuiltinDHFactories; +import org.apache.sshd.common.kex.DHFactory; +import org.apache.sshd.common.kex.KeyExchangeFactory; +import org.apache.sshd.common.keyprovider.FileKeyPairProvider; +import org.apache.sshd.common.session.Session; +import org.apache.sshd.core.CoreModuleProperties; +import org.apache.sshd.server.SshServer; +import org.apache.sshd.server.channel.ChannelSessionFactory; +import org.apache.sshd.server.forward.DirectTcpipFactory; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A misconfigured endpoint fails with a message that names the problem, instead of a NullPointerException, and the + * options that had no effect apply. + */ +public class SshConfigurationErrorsTest extends SshComponentTestSupport { + + @Test + public void testRefusedChannelSaysSo() throws Exception { + // a server that accepts the session but refuses the session channel + SshServer refusing = SshServer.setUpDefaultServer(); + refusing.setPort(0); + refusing.setKeyPairProvider(new FileKeyPairProvider(Paths.get(getHostKey()))); + refusing.setPasswordAuthenticator((username, password, session) -> true); + // only forwarding channels: the session channel that runs the command is refused + refusing.setChannelFactories(List.of(DirectTcpipFactory.INSTANCE)); + refusing.start(); + try { + String uri = "ssh://smx:smx@localhost:" + refusing.getPort() + "?timeout=30000"; + CamelExecutionException e + = assertThrows(CamelExecutionException.class, () -> template.requestBody(uri, "test")); + + Throwable cause = causeStartingWith(e, "The server refused to open the exec channel to localhost:"); + assertTrue(cause.getCause() != null, "the reason from the server is the cause"); + } finally { + refusing.stop(true); + } + } + + @Test + public void testChannelThatDoesNotOpenInTimeFailsWithChannelOpenTimeout() throws Exception { + // a server that takes 3 seconds to open the session channel + SshServer slow = SshServer.setUpDefaultServer(); + slow.setPort(0); + slow.setKeyPairProvider(new FileKeyPairProvider(Paths.get(getHostKey()))); + slow.setPasswordAuthenticator((username, password, session) -> true); + slow.setCommandFactory(new TestEchoCommandFactory()); + slow.setChannelFactories(List.of(new ChannelFactory() { + @Override + public String getName() { + return ChannelSessionFactory.INSTANCE.getName(); + } + + @Override + public Channel createChannel(Session session) throws IOException { + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } Review Comment: Done in d52f67a: the channel factory now waits on a `CountDownLatch` (with a 30 s cap), and the `finally` block counts it down before `slow.stop(true)`, so there is no `Thread.sleep` and the server thread is released at once. Thanks @davsclaus and @gnodet, and thanks for the regen commit. _Claude Code on behalf of Ayush0612005_ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
