Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
Former-commit-id: 5524b566b5662a22aa6c44ea14117e1ef6f78032
  • Loading branch information
dkocher committed Sep 23, 2013
1 parent d8ac839 commit a7efc60
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 13 deletions.
10 changes: 4 additions & 6 deletions source/ch/cyberduck/core/ftp/FTPReadFeature.java
Expand Up @@ -69,13 +69,11 @@ public void close() throws IOException {
super.close();
}
finally {
if(this.getByteCount() == status.getLength()) {
// Read 226 status
if(!session.getClient().completePendingCommand()) {
throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString());
}
// Read 226 status
if(!session.getClient().completePendingCommand()) {
throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString());
}
else {
if(this.getByteCount() != status.getLength()) {
// Interrupted transfer
if(!session.getClient().abort()) {
log.error("Error closing data socket:" + session.getClient().getReplyString());
Expand Down
10 changes: 4 additions & 6 deletions source/ch/cyberduck/core/ftp/FTPWriteFeature.java
Expand Up @@ -66,13 +66,11 @@ public void close() throws IOException {
super.close();
}
finally {
if(this.getByteCount() == status.getLength()) {
// Read 226 status
if(!session.getClient().completePendingCommand()) {
throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString());
}
// Read 226 status
if(!session.getClient().completePendingCommand()) {
throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString());
}
else {
if(this.getByteCount() != status.getLength()) {
// Interrupted transfer
if(!session.getClient().abort()) {
log.error("Error closing data socket:" + session.getClient().getReplyString());
Expand Down
99 changes: 98 additions & 1 deletion test/ch/cyberduck/core/ftp/FTPReadFeatureTest.java
Expand Up @@ -26,12 +26,23 @@
import ch.cyberduck.core.Host;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.exception.NotfoundException;
import ch.cyberduck.core.shared.DefaultHomeFinderService;
import ch.cyberduck.core.shared.DefaultTouchFeature;
import ch.cyberduck.core.transfer.TransferStatus;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.UUID;

import static org.junit.Assert.*;

/**
* @version $Id:$
* @version $Id$
*/
public class FTPReadFeatureTest extends AbstractTestCase {

Expand All @@ -46,4 +57,90 @@ public void testReadNotFound() throws Exception {
final TransferStatus status = new TransferStatus();
new FTPReadFeature(session).read(new Path(session.workdir(), "nosuchname", Path.FILE_TYPE), status);
}

@Test
public void testReadRange() throws Exception {
final Host host = new Host(new FTPTLSProtocol(), "test.cyberduck.ch", new Credentials(
properties.getProperty("ftp.user"), properties.getProperty("ftp.password")
));
final FTPSession session = new FTPSession(host);
session.open(new DefaultHostKeyController());
session.login(new DisabledPasswordStore(), new DisabledLoginController());
final Path test = new Path(new DefaultHomeFinderService(session).find(), UUID.randomUUID().toString(), Path.FILE_TYPE);
new DefaultTouchFeature(session).touch(test);
final byte[] content = RandomStringUtils.random(1000).getBytes();
final OutputStream out = new FTPWriteFeature(session).write(test, new TransferStatus().length(content.length));
assertNotNull(out);
IOUtils.write(content, out);
IOUtils.closeQuietly(out);
final TransferStatus status = new TransferStatus();
status.setLength(content.length);
status.setAppend(true);
status.setCurrent(100L);
final Path workdir = session.workdir();
final InputStream in = new FTPReadFeature(session).read(test, status);
assertNotNull(in);
final byte[] download = new byte[content.length - 100];
IOUtils.readFully(in, download);
final byte[] reference = new byte[content.length - 100];
System.arraycopy(content, 100, reference, 0, content.length - 100);
assertArrayEquals(reference, download);
in.close();
new FTPDeleteFeature(session).delete(Collections.<Path>singletonList(test), new DisabledLoginController());
session.close();
}

@Test
public void testAbortNoRead() throws Exception {
final Host host = new Host(new FTPTLSProtocol(), "test.cyberduck.ch", new Credentials(
properties.getProperty("ftp.user"), properties.getProperty("ftp.password")
));
final FTPSession session = new FTPSession(host);
session.open(new DefaultHostKeyController());
session.login(new DisabledPasswordStore(), new DisabledLoginController());
final Path test = new Path(new DefaultHomeFinderService(session).find(), UUID.randomUUID().toString(), Path.FILE_TYPE);
new DefaultTouchFeature(session).touch(test);
final TransferStatus status = new TransferStatus();
status.setLength(5L);
final Path workdir = session.workdir();
final InputStream in = new FTPReadFeature(session).read(new Path(workdir, "test", Path.FILE_TYPE), status);
assertNotNull(in);
// Send ABOR because stream was not read completly
in.close();
// Make sure next command can be sent
session.noop();
assertEquals(workdir, session.workdir());
new FTPDeleteFeature(session).delete(Collections.<Path>singletonList(test), new DisabledLoginController());
session.close();
}

@Test
public void testAbortPartialRead() throws Exception {
final Host host = new Host(new FTPTLSProtocol(), "test.cyberduck.ch", new Credentials(
properties.getProperty("ftp.user"), properties.getProperty("ftp.password")
));
final FTPSession session = new FTPSession(host);
session.open(new DefaultHostKeyController());
session.login(new DisabledPasswordStore(), new DisabledLoginController());
final Path test = new Path(new DefaultHomeFinderService(session).find(), UUID.randomUUID().toString(), Path.FILE_TYPE);
new DefaultTouchFeature(session).touch(test);
final OutputStream out = new FTPWriteFeature(session).write(test, new TransferStatus().length(20L));
assertNotNull(out);
final byte[] content = RandomStringUtils.random(1000).getBytes();
IOUtils.write(content, out);
IOUtils.closeQuietly(out);
final TransferStatus status = new TransferStatus();
status.setLength(20L);
final Path workdir = session.workdir();
final InputStream in = new FTPReadFeature(session).read(test, status);
assertNotNull(in);
assertTrue(in.read() > 0);
// Send ABOR because stream was not read completly
in.close();
// Make sure next command can be sent
session.noop();
assertEquals(workdir, session.workdir());
new FTPDeleteFeature(session).delete(Collections.<Path>singletonList(test), new DisabledLoginController());
session.close();
}
}

0 comments on commit a7efc60

Please sign in to comment.