-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from lguerin/bandwidth
SCP : limit the used bandwidth
- Loading branch information
Showing
7 changed files
with
228 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/net/schmizz/sshj/xfer/scp/AbstractSCPClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.schmizz.sshj.xfer.scp; | ||
|
||
abstract class AbstractSCPClient { | ||
|
||
protected final SCPEngine engine; | ||
protected int bandwidthLimit; | ||
|
||
AbstractSCPClient(SCPEngine engine) { | ||
this.engine = engine; | ||
} | ||
|
||
AbstractSCPClient(SCPEngine engine, int bandwidthLimit) { | ||
this.engine = engine; | ||
this.bandwidthLimit = bandwidthLimit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/test/java/net/schmizz/sshj/xfer/scp/SCPFileTransferTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package net.schmizz.sshj.xfer.scp; | ||
|
||
import com.hierynomus.sshj.test.SshFixture; | ||
import com.hierynomus.sshj.test.util.FileUtil; | ||
import net.schmizz.sshj.SSHClient; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static junit.framework.Assert.assertFalse; | ||
import static junit.framework.Assert.assertTrue; | ||
|
||
public class SCPFileTransferTest { | ||
|
||
public static final String DEFAULT_FILE_NAME = "my_file.txt"; | ||
File targetDir; | ||
File sourceFile; | ||
File targetFile; | ||
SSHClient sshClient; | ||
|
||
@Rule | ||
public SshFixture fixture = new SshFixture(); | ||
|
||
@Rule | ||
public TemporaryFolder tempFolder = new TemporaryFolder(); | ||
|
||
@Before | ||
public void init() throws IOException { | ||
sourceFile = tempFolder.newFile(DEFAULT_FILE_NAME); | ||
FileUtil.writeToFile(sourceFile, "This is my file"); | ||
targetDir = tempFolder.newFolder(); | ||
targetFile = new File(targetDir + File.separator + DEFAULT_FILE_NAME); | ||
sshClient = fixture.setupConnectedDefaultClient(); | ||
sshClient.authPassword("test", "test"); | ||
} | ||
|
||
@After | ||
public void cleanup() { | ||
if (targetFile.exists()) { | ||
targetFile.delete(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldSCPUploadFile() throws IOException { | ||
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); | ||
assertFalse(targetFile.exists()); | ||
scpFileTransfer.upload(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); | ||
assertTrue(targetFile.exists()); | ||
} | ||
|
||
@Test | ||
public void shouldSCPUploadFileWithBandwidthLimit() throws IOException { | ||
// Limit upload transfer at 2Mo/s | ||
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer().bandwidthLimit(16000); | ||
assertFalse(targetFile.exists()); | ||
scpFileTransfer.upload(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); | ||
assertTrue(targetFile.exists()); | ||
} | ||
|
||
@Test | ||
public void shouldSCPDownloadFile() throws IOException { | ||
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); | ||
assertFalse(targetFile.exists()); | ||
scpFileTransfer.download(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); | ||
assertTrue(targetFile.exists()); | ||
} | ||
|
||
@Test | ||
public void shouldSCPDownloadFileWithBandwidthLimit() throws IOException { | ||
// Limit download transfer at 128Ko/s | ||
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer().bandwidthLimit(1024); | ||
assertFalse(targetFile.exists()); | ||
scpFileTransfer.download(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath()); | ||
assertTrue(targetFile.exists()); | ||
} | ||
} |