1 package ch
.cyberduck
.core
.ftp
;
4 * Copyright (c) 2013 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * feedback@cyberduck.ch
21 import ch
.cyberduck
.core
.AbstractTestCase
;
22 import ch
.cyberduck
.core
.Credentials
;
23 import ch
.cyberduck
.core
.DisabledCancelCallback
;
24 import ch
.cyberduck
.core
.DisabledHostKeyCallback
;
25 import ch
.cyberduck
.core
.DisabledLoginCallback
;
26 import ch
.cyberduck
.core
.DisabledPasswordStore
;
27 import ch
.cyberduck
.core
.DisabledTranscriptListener
;
28 import ch
.cyberduck
.core
.Host
;
29 import ch
.cyberduck
.core
.Path
;
30 import ch
.cyberduck
.core
.exception
.NotfoundException
;
31 import ch
.cyberduck
.core
.features
.Delete
;
32 import ch
.cyberduck
.core
.io
.StreamCopier
;
33 import ch
.cyberduck
.core
.shared
.DefaultHomeFinderService
;
34 import ch
.cyberduck
.core
.shared
.DefaultTouchFeature
;
35 import ch
.cyberduck
.core
.transfer
.TransferStatus
;
37 import org
.apache
.commons
.io
.IOUtils
;
38 import org
.apache
.commons
.lang3
.RandomStringUtils
;
39 import org
.junit
.Test
;
41 import java
.io
.ByteArrayInputStream
;
42 import java
.io
.ByteArrayOutputStream
;
43 import java
.io
.InputStream
;
44 import java
.io
.OutputStream
;
45 import java
.util
.Collections
;
46 import java
.util
.EnumSet
;
47 import java
.util
.Random
;
48 import java
.util
.UUID
;
50 import static org
.junit
.Assert
.*;
55 public class FTPReadFeatureTest
extends AbstractTestCase
{
57 @Test(expected
= NotfoundException
.class)
58 public void testReadNotFound() throws Exception
{
59 final Host host
= new Host(new FTPTLSProtocol(), "test.cyberduck.ch", new Credentials(
60 properties
.getProperty("ftp.user"), properties
.getProperty("ftp.password")
62 final FTPSession session
= new FTPSession(host
);
63 session
.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
64 session
.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
65 final TransferStatus status
= new TransferStatus();
66 new FTPReadFeature(session
).read(new Path(session
.workdir(), "nosuchname", EnumSet
.of(Path
.Type
.file
)), status
);
70 public void testRead() throws Exception
{
71 final Host host
= new Host(new FTPTLSProtocol(), "test.cyberduck.ch", new Credentials(
72 properties
.getProperty("ftp.user"), properties
.getProperty("ftp.password")
74 final FTPSession session
= new FTPSession(host
);
75 session
.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
76 session
.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
77 final Path home
= new DefaultHomeFinderService(session
).find();
78 final Path test
= new Path(home
, UUID
.randomUUID().toString(), EnumSet
.of(Path
.Type
.file
));
79 new DefaultTouchFeature(session
).touch(test
);
80 final byte[] content
= new byte[39865];
81 new Random().nextBytes(content
);
83 final TransferStatus status
= new TransferStatus().length(content
.length
);
84 final OutputStream out
= new FTPWriteFeature(session
).write(test
, status
);
86 new StreamCopier(status
, status
).withLimit(new Long(content
.length
)).transfer(new ByteArrayInputStream(content
), out
);
90 final TransferStatus status
= new TransferStatus();
91 status
.setLength(content
.length
);
92 final InputStream in
= new FTPReadFeature(session
).read(test
, status
);
94 final ByteArrayOutputStream buffer
= new ByteArrayOutputStream(content
.length
);
95 new StreamCopier(status
, status
).withLimit(new Long(content
.length
)).transfer(in
, buffer
);
97 assertArrayEquals(content
, buffer
.toByteArray());
99 new FTPDeleteFeature(session
).delete(Collections
.singletonList(test
), new DisabledLoginCallback(), new Delete
.Callback() {
101 public void delete(final Path file
) {
108 public void testReadRange() throws Exception
{
109 final Host host
= new Host(new FTPTLSProtocol(), "test.cyberduck.ch", new Credentials(
110 properties
.getProperty("ftp.user"), properties
.getProperty("ftp.password")
112 final FTPSession session
= new FTPSession(host
);
113 session
.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
114 session
.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
115 final Path test
= new Path(new DefaultHomeFinderService(session
).find(), UUID
.randomUUID().toString(), EnumSet
.of(Path
.Type
.file
));
116 new DefaultTouchFeature(session
).touch(test
);
117 final byte[] content
= RandomStringUtils
.random(1000).getBytes();
118 final OutputStream out
= new FTPWriteFeature(session
).write(test
, new TransferStatus().length(content
.length
));
120 new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content
), out
);
121 IOUtils
.closeQuietly(out
);
122 final TransferStatus status
= new TransferStatus();
123 // Partial read with offset and not full content length
124 status
.setLength(content
.length
- 100 - 1);
125 status
.setAppend(true);
126 status
.setOffset(100L);
127 final InputStream in
= new FTPReadFeature(session
).read(test
, status
);
129 final ByteArrayOutputStream download
= new ByteArrayOutputStream();
130 new StreamCopier(status
, status
).withLimit(status
.getLength()).transfer(in
, download
);
131 final byte[] reference
= new byte[content
.length
- 100 - 1];
132 System
.arraycopy(content
, 100, reference
, 0, content
.length
- 100 - 1);
133 assertArrayEquals(reference
, download
.toByteArray());
135 new FTPDeleteFeature(session
).delete(Collections
.singletonList(test
), new DisabledLoginCallback(), new Delete
.Callback() {
137 public void delete(final Path file
) {
144 public void testAbortNoRead() throws Exception
{
145 final Host host
= new Host(new FTPTLSProtocol(), "test.cyberduck.ch", new Credentials(
146 properties
.getProperty("ftp.user"), properties
.getProperty("ftp.password")
148 final FTPSession session
= new FTPSession(host
);
149 session
.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
150 session
.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
151 final Path test
= new Path(new DefaultHomeFinderService(session
).find(), UUID
.randomUUID().toString(), EnumSet
.of(Path
.Type
.file
));
152 new DefaultTouchFeature(session
).touch(test
);
153 final TransferStatus status
= new TransferStatus();
154 status
.setLength(5L);
155 final Path workdir
= session
.workdir();
156 final InputStream in
= new FTPReadFeature(session
).read(new Path(workdir
, "test", EnumSet
.of(Path
.Type
.file
)), status
);
158 // Send ABOR because stream was not read completly
160 // Make sure subsequent PWD command works
161 assertEquals(workdir
, session
.workdir());
162 new FTPDeleteFeature(session
).delete(Collections
.singletonList(test
), new DisabledLoginCallback(), new Delete
.Callback() {
164 public void delete(final Path file
) {
171 public void testAbortPartialRead() throws Exception
{
172 final Host host
= new Host(new FTPTLSProtocol(), "test.cyberduck.ch", new Credentials(
173 properties
.getProperty("ftp.user"), properties
.getProperty("ftp.password")
175 final FTPSession session
= new FTPSession(host
);
176 session
.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
177 session
.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
178 final Path test
= new Path(new DefaultHomeFinderService(session
).find(), UUID
.randomUUID().toString(), EnumSet
.of(Path
.Type
.file
));
179 new DefaultTouchFeature(session
).touch(test
);
180 final OutputStream out
= new FTPWriteFeature(session
).write(test
, new TransferStatus().length(20L));
182 final byte[] content
= RandomStringUtils
.random(1000).getBytes();
183 new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content
), out
);
184 IOUtils
.closeQuietly(out
);
185 final TransferStatus status
= new TransferStatus();
186 status
.setLength(20L);
187 final Path workdir
= session
.workdir();
188 final InputStream in
= new FTPReadFeature(session
).read(test
, status
);
190 assertTrue(in
.read() > 0);
191 // Send ABOR because stream was not read completly
193 // Make sure subsequent PWD command works
194 assertEquals(workdir
, session
.workdir());
195 new FTPDeleteFeature(session
).delete(Collections
.singletonList(test
), new DisabledLoginCallback(), new Delete
.Callback() {
197 public void delete(final Path file
) {