From 9ffa99a029f6a0d02374c8705d2db32b23731779 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 16 Jul 2008 09:17:42 +0000 Subject: [PATCH] fail with a meaningful error of remoteDir is not parseable. PR 42770. Submitted by Scott Johnson git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@677206 13f79535-47bb-0310-9956-ffa450edef68 --- CONTRIBUTORS | 1 + contributors.xml | 4 +++ .../tools/ant/taskdefs/optional/ssh/Scp.java | 19 ++++++++++-- .../ant/taskdefs/optional/ssh/ScpTest.java | 30 ++++++++++++++++++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 706793e99..f1ca99bc5 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -239,6 +239,7 @@ Sam Ruby Sandra Metz Scott Carlson Scott Ellsworth +Scott Johnson Scott M. Stirling Sean Egan Sean P. Kane diff --git a/contributors.xml b/contributors.xml index a9c51256e..46d612978 100644 --- a/contributors.xml +++ b/contributors.xml @@ -967,6 +967,10 @@ Scott Ellsworth + + Scott + Johnson + Scott M. diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java index 7ba4b20e2..f80376d5e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java @@ -101,6 +101,7 @@ public class Scp extends SSHBase { * @since Ant 1.6.2 */ public void setRemoteFile(String aFromUri) { + validateRemoteUri("remoteFile", aFromUri); setFromUri(aFromUri); this.isFromRemote = true; } @@ -133,10 +134,21 @@ public class Scp extends SSHBase { * @since Ant 1.6.2 */ public void setRemoteTodir(String aToUri) { + validateRemoteUri("remoteToDir", aToUri); setToUri(aToUri); this.isToRemote = true; } + private static void validateRemoteUri(String type, String aToUri) { + if (!isRemoteUri(aToUri)) { + throw new BuildException(type + " '" + aToUri + "' is invalid. " + + "The 'remoteToDir' attribute must " + + "have syntax like the " + + "following: user:password@host:/path" + + " - the :password part is optional"); + } + } + /** * Changes the file name to the given name while receiving it, * only useful if receiving a single file. @@ -155,6 +167,7 @@ public class Scp extends SSHBase { * @since Ant 1.6.2 */ public void setRemoteTofile(String aToUri) { + validateRemoteUri("remoteToFile", aToUri); setToUri(aToUri); this.isToRemote = true; } @@ -339,9 +352,11 @@ public class Scp extends SSHBase { } setUsername(uri.substring(0, indexOfColon)); setPassword(uri.substring(indexOfColon + 1, indexOfAt)); - } else { + } else if (indexOfAt > -1) { // no password, will require passphrase setUsername(uri.substring(0, indexOfAt)); + } else { + throw new BuildException("no username was given. Can't authenticate."); } if (getUserInfo().getPassword() == null @@ -364,7 +379,7 @@ public class Scp extends SSHBase { return remotePath; } - private boolean isRemoteUri(String uri) { + private static boolean isRemoteUri(String uri) { boolean isRemote = true; int indexOfAt = uri.indexOf('@'); if (indexOfAt < 0) { diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java index 8c21c8d0f..f970e0ed7 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.ArrayList; import java.util.Iterator; +import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.condition.FilesMatch; import org.apache.tools.ant.types.FileSet; @@ -48,7 +49,7 @@ import org.apache.tools.ant.types.selectors.FilenameSelector; */ public class ScpTest extends TestCase { - private File tempDir = new File( System.getProperty("scp.tmp") ); + private File tempDir; private String sshHostUri = System.getProperty("scp.host"); private int port = Integer.parseInt( System.getProperty( "scp.port", "22" ) ); private String knownHosts = System.getProperty("scp.known.hosts"); @@ -57,6 +58,9 @@ public class ScpTest extends TestCase { public ScpTest(String testname) { super(testname); + if (System.getProperty("scp.tmp") != null) { + tempDir = new File(System.getProperty("scp.tmp")); + } } protected void setUp() { @@ -71,8 +75,10 @@ public class ScpTest extends TestCase { } public void testSingleFileUploadAndDownload() throws IOException { + assertNotNull("system property scp.tmp must be set", tempDir); File uploadFile = createTemporaryFile(); + // upload Scp scpTask = createTask(); scpTask.setFile( uploadFile.getPath() ); scpTask.setTodir( sshHostUri ); @@ -84,6 +90,8 @@ public class ScpTest extends TestCase { assertTrue( "Assert that the testFile does not exist.", !testFile.exists() ); + // download + scpTask = createTask(); scpTask.setFile( sshHostUri + "/" + uploadFile.getName() ); scpTask.setTodir( testFile.getPath() ); scpTask.execute(); @@ -93,6 +101,7 @@ public class ScpTest extends TestCase { } public void testMultiUploadAndDownload() throws IOException { + assertNotNull("system property scp.tmp must be set", tempDir); List uploadList = new ArrayList(); for( int i = 0; i < 5; i++ ) { uploadList.add( createTemporaryFile() ); @@ -128,6 +137,25 @@ public class ScpTest extends TestCase { } } + public void testRemoteToDir() throws IOException { + Scp scpTask = createTask(); + + // first try an invalid URI + try { + scpTask.setRemoteTodir( "host:/a/path/without/an/at" ); + fail("Expected a BuildException to be thrown due to invalid" + + " remoteToDir"); + } + catch (BuildException e) + { + // expected + } + + // And this one should work + scpTask.setRemoteTodir( "user:password@host:/a/path/with/an/at" ); + // no exception + } + public void addCleanup( File file ) { cleanUpList.add( file ); }