Browse Source

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
master
Stefan Bodewig 17 years ago
parent
commit
9ffa99a029
4 changed files with 51 additions and 3 deletions
  1. +1
    -0
      CONTRIBUTORS
  2. +4
    -0
      contributors.xml
  3. +17
    -2
      src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java
  4. +29
    -1
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java

+ 1
- 0
CONTRIBUTORS View File

@@ -239,6 +239,7 @@ Sam Ruby
Sandra Metz Sandra Metz
Scott Carlson Scott Carlson
Scott Ellsworth Scott Ellsworth
Scott Johnson
Scott M. Stirling Scott M. Stirling
Sean Egan Sean Egan
Sean P. Kane Sean P. Kane


+ 4
- 0
contributors.xml View File

@@ -967,6 +967,10 @@
<first>Scott</first> <first>Scott</first>
<last>Ellsworth</last> <last>Ellsworth</last>
</name> </name>
<name>
<first>Scott</first>
<last>Johnson</last>
</name>
<name> <name>
<first>Scott</first> <first>Scott</first>
<middle>M.</middle> <middle>M.</middle>


+ 17
- 2
src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java View File

@@ -101,6 +101,7 @@ public class Scp extends SSHBase {
* @since Ant 1.6.2 * @since Ant 1.6.2
*/ */
public void setRemoteFile(String aFromUri) { public void setRemoteFile(String aFromUri) {
validateRemoteUri("remoteFile", aFromUri);
setFromUri(aFromUri); setFromUri(aFromUri);
this.isFromRemote = true; this.isFromRemote = true;
} }
@@ -133,10 +134,21 @@ public class Scp extends SSHBase {
* @since Ant 1.6.2 * @since Ant 1.6.2
*/ */
public void setRemoteTodir(String aToUri) { public void setRemoteTodir(String aToUri) {
validateRemoteUri("remoteToDir", aToUri);
setToUri(aToUri); setToUri(aToUri);
this.isToRemote = true; 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, * Changes the file name to the given name while receiving it,
* only useful if receiving a single file. * only useful if receiving a single file.
@@ -155,6 +167,7 @@ public class Scp extends SSHBase {
* @since Ant 1.6.2 * @since Ant 1.6.2
*/ */
public void setRemoteTofile(String aToUri) { public void setRemoteTofile(String aToUri) {
validateRemoteUri("remoteToFile", aToUri);
setToUri(aToUri); setToUri(aToUri);
this.isToRemote = true; this.isToRemote = true;
} }
@@ -339,9 +352,11 @@ public class Scp extends SSHBase {
} }
setUsername(uri.substring(0, indexOfColon)); setUsername(uri.substring(0, indexOfColon));
setPassword(uri.substring(indexOfColon + 1, indexOfAt)); setPassword(uri.substring(indexOfColon + 1, indexOfAt));
} else {
} else if (indexOfAt > -1) {
// no password, will require passphrase // no password, will require passphrase
setUsername(uri.substring(0, indexOfAt)); setUsername(uri.substring(0, indexOfAt));
} else {
throw new BuildException("no username was given. Can't authenticate.");
} }


if (getUserInfo().getPassword() == null if (getUserInfo().getPassword() == null
@@ -364,7 +379,7 @@ public class Scp extends SSHBase {
return remotePath; return remotePath;
} }


private boolean isRemoteUri(String uri) {
private static boolean isRemoteUri(String uri) {
boolean isRemote = true; boolean isRemote = true;
int indexOfAt = uri.indexOf('@'); int indexOfAt = uri.indexOf('@');
if (indexOfAt < 0) { if (indexOfAt < 0) {


+ 29
- 1
src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java View File

@@ -25,6 +25,7 @@ import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;


import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.condition.FilesMatch; import org.apache.tools.ant.taskdefs.condition.FilesMatch;
import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.FileSet;
@@ -48,7 +49,7 @@ import org.apache.tools.ant.types.selectors.FilenameSelector;
*/ */
public class ScpTest extends TestCase { 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 String sshHostUri = System.getProperty("scp.host");
private int port = Integer.parseInt( System.getProperty( "scp.port", "22" ) ); private int port = Integer.parseInt( System.getProperty( "scp.port", "22" ) );
private String knownHosts = System.getProperty("scp.known.hosts"); private String knownHosts = System.getProperty("scp.known.hosts");
@@ -57,6 +58,9 @@ public class ScpTest extends TestCase {


public ScpTest(String testname) { public ScpTest(String testname) {
super(testname); super(testname);
if (System.getProperty("scp.tmp") != null) {
tempDir = new File(System.getProperty("scp.tmp"));
}
} }


protected void setUp() { protected void setUp() {
@@ -71,8 +75,10 @@ public class ScpTest extends TestCase {
} }


public void testSingleFileUploadAndDownload() throws IOException { public void testSingleFileUploadAndDownload() throws IOException {
assertNotNull("system property scp.tmp must be set", tempDir);
File uploadFile = createTemporaryFile(); File uploadFile = createTemporaryFile();


// upload
Scp scpTask = createTask(); Scp scpTask = createTask();
scpTask.setFile( uploadFile.getPath() ); scpTask.setFile( uploadFile.getPath() );
scpTask.setTodir( sshHostUri ); scpTask.setTodir( sshHostUri );
@@ -84,6 +90,8 @@ public class ScpTest extends TestCase {
assertTrue( "Assert that the testFile does not exist.", assertTrue( "Assert that the testFile does not exist.",
!testFile.exists() ); !testFile.exists() );


// download
scpTask = createTask();
scpTask.setFile( sshHostUri + "/" + uploadFile.getName() ); scpTask.setFile( sshHostUri + "/" + uploadFile.getName() );
scpTask.setTodir( testFile.getPath() ); scpTask.setTodir( testFile.getPath() );
scpTask.execute(); scpTask.execute();
@@ -93,6 +101,7 @@ public class ScpTest extends TestCase {
} }


public void testMultiUploadAndDownload() throws IOException { public void testMultiUploadAndDownload() throws IOException {
assertNotNull("system property scp.tmp must be set", tempDir);
List uploadList = new ArrayList(); List uploadList = new ArrayList();
for( int i = 0; i < 5; i++ ) { for( int i = 0; i < 5; i++ ) {
uploadList.add( createTemporaryFile() ); 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 ) { public void addCleanup( File file ) {
cleanUpList.add( file ); cleanUpList.add( file );
} }


Loading…
Cancel
Save