Skip to content

Commit

Permalink
Merge pull request #16229 from iterate-ch/bugfix/MD-21806
Browse files Browse the repository at this point in the history
Add test coverage for reachability implementations
  • Loading branch information
dkocher authored Sep 2, 2024
2 parents 49ff9c2 + 5b53afa commit f0ba00f
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void testMonitor() {
@Test
public void testIsReachable() {
final Reachability r = new SystemConfigurationReachability();
assertFalse(r.isReachable(new Host(new TestProtocol(Scheme.http))));
assertTrue(r.isReachable(new Host(new TestProtocol(Scheme.http), "cyberduck.io")));
assertTrue(r.isReachable(new Host(new TestProtocol(Scheme.https), "cyberduck.io")));
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/ch/cyberduck/core/Host.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public String getHostname() {
* @param hostname Server
*/
public void setHostname(final String hostname) {
this.hostname = hostname.trim();
this.hostname = StringUtils.trim(hostname);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public ChainedReachability(final Reachability... delegates) {
this.delegates = delegates;
}


@Override
public void test(final Host bookmark) throws BackgroundException {
for(Reachability delegate : delegates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ch.cyberduck.core.HostnameConfiguratorFactory;
import ch.cyberduck.core.exception.BackgroundException;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -40,6 +41,9 @@ public class DefaultInetAddressReachability extends DisabledReachability {
@Override
public void test(final Host bookmark) throws BackgroundException {
try {
if(StringUtils.isBlank(bookmark.getHostname())) {
throw new ConnectException();
}
if(!InetAddress.getByName(HostnameConfiguratorFactory.get(bookmark.getProtocol()).getHostname(bookmark.getHostname())).isReachable(
ConnectionTimeoutFactory.get(bookmark).getTimeout() * 1000
)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ch.cyberduck.core.diagnostics;

/*
* Copyright (c) 2002-2024 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import ch.cyberduck.core.Host;
import ch.cyberduck.core.HostnameConfigurator;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.ResolveFailedException;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class HostnameReachability implements Reachability {
private static final Logger log = LogManager.getLogger(HostnameReachability.class);

@Override
public void test(final Host bookmark) throws BackgroundException {
final HostnameConfigurator configurator = bookmark.getProtocol().getFeature(HostnameConfigurator.class);
final String hostname = configurator.getHostname(bookmark.getHostname());
if(StringUtils.isBlank(hostname)) {
if(log.isWarnEnabled()) {
log.warn(String.format("Missing hostname in %s", bookmark));
}
throw new ResolveFailedException();
}
}

@Override
public Monitor monitor(final Host bookmark, final Callback callback) {
return Monitor.disabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void test(final Host bookmark) throws BackgroundException {
break;
case https:
case http:
new ChainedReachability(monitor, new ResolverReachability(), new HttpReachability()).test(bookmark);
new ChainedReachability(monitor, new HostnameReachability(), new ResolverReachability(), new HttpReachability()).test(bookmark);
break;
case sftp:
final Host jumphost = JumpHostConfiguratorFactory.get(bookmark.getProtocol()).getJumphost(bookmark.getHostname());
Expand All @@ -67,7 +67,7 @@ public void test(final Host bookmark) throws BackgroundException {
}
break;
default:
new ChainedReachability(monitor, new ResolverReachability(), new TcpReachability()).test(bookmark);
new ChainedReachability(monitor, new HostnameReachability(), new ResolverReachability(), new TcpReachability()).test(bookmark);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
public class ResolveFailedException extends BackgroundException {
private static final long serialVersionUID = 5213700700233301556L;

public ResolveFailedException() {
super(LocaleFactory.localizedString("Connection failed", "Error"), (Throwable) null);
}

public ResolveFailedException(final String detail, final Throwable cause) {
super(LocaleFactory.localizedString("Connection failed", "Error"), detail, cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class DefaultInetAddressReachabilityTest {
@Test
public void testIsReachable() {
final Reachability r = new DefaultInetAddressReachability();
assertFalse(r.isReachable(
new Host(new TestProtocol(), "cyberduck.io")
));
assertTrue(r.isReachable(
new Host(new TestProtocol(), "cyberduck.io")
));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ch.cyberduck.core.diagnostics;

/*
* Copyright (c) 2002-2024 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import ch.cyberduck.core.Host;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.TestProtocol;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class HostnameReachabilityTest {

@Test
public void testIsReachable() {
final Reachability r = new HostnameReachability();
assertFalse(r.isReachable(
new Host(new TestProtocol(Scheme.http))
));
assertTrue(r.isReachable(
new Host(new TestProtocol(Scheme.http), "cyberduck.io")
));
assertTrue(r.isReachable(
new Host(new TestProtocol(Scheme.http) {
@Override
public String getDefaultHostname() {
return "cyberduck.io";
}
})
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class HttpReachabilityTest {
@Test
public void testIsReachable() {
final Reachability r = new HttpReachability();
assertFalse(r.isReachable(
new Host(new TestProtocol(Scheme.http))
));
assertTrue(r.isReachable(
new Host(new TestProtocol(Scheme.http), "test.cyberduck.io")
));
Expand Down
6 changes: 4 additions & 2 deletions s3/src/test/java/ch/cyberduck/core/s3/S3ProtocolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public void testSchemes() {
@Test
public void testEquals() throws Exception {
final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())));
assertEquals(new ProfilePlistReader(factory).read(
this.getClass().getResourceAsStream("/S3 (Credentials from Instance Metadata).cyberduckprofile")),
final Profile profile = new ProfilePlistReader(factory).read(
this.getClass().getResourceAsStream("/S3 (Credentials from Instance Metadata).cyberduckprofile"));
assertEquals("s3.amazonaws.com", profile.getDefaultHostname());
assertEquals(profile,
new ProfilePlistReader(factory).read(
this.getClass().getResourceAsStream("/S3 (Credentials from Instance Metadata).cyberduckprofile")));
}
Expand Down

0 comments on commit f0ba00f

Please sign in to comment.