Skip to content

Commit ddadf28

Browse files
chenkinsdkocher
authored andcommitted
Add test write and read file UVF.
1 parent 7db42d7 commit ddadf28

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

s3/src/test/java/ch/cyberduck/core/cryptomator/UVFIntegrationTest.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import ch.cyberduck.test.TestcontainerTest;
3939

4040
import org.apache.commons.io.IOUtils;
41+
import org.apache.commons.lang3.RandomUtils;
4142
import org.cryptomator.cryptolib.api.UVFMasterkey;
4243
import org.jetbrains.annotations.NotNull;
4344
import org.junit.AfterClass;
@@ -157,29 +158,25 @@ public Credentials prompt(final Host bookmark, final String title, final String
157158
final Path foo = new Path("/cyberduckbucket/foo.txt", EnumSet.of(AbstractPath.Type.file, AbstractPath.Type.decrypted));
158159
assertTrue(Arrays.toString(list.toArray()), list.contains(foo));
159160
assertTrue(Arrays.toString(list.toArray()), list.contains(new Path("/cyberduckbucket/subdir", EnumSet.of(AbstractPath.Type.directory, AbstractPath.Type.placeholder, AbstractPath.Type.decrypted))));
161+
assertEquals("Hello Foo", readFile(storage, foo));
162+
}
163+
{
164+
final byte[] expected = writeRandomFile(storage, new Path("/cyberduckbucket/alice.txt", EnumSet.of(AbstractPath.Type.file, AbstractPath.Type.decrypted)), 55);
165+
final AttributedList<Path> list = storage.getFeature(ListService.class).list(home, new DisabledListProgressListener());
166+
assertEquals(3, list.size());
167+
assertTrue(Arrays.toString(list.toArray()), list.contains(new Path("/cyberduckbucket/foo.txt", EnumSet.of(AbstractPath.Type.file, AbstractPath.Type.decrypted))));
168+
assertTrue(Arrays.toString(list.toArray()), list.contains(new Path("/cyberduckbucket/alice.txt", EnumSet.of(AbstractPath.Type.file, AbstractPath.Type.decrypted))));
169+
assertTrue(Arrays.toString(list.toArray()), list.contains(new Path("/cyberduckbucket/subdir", EnumSet.of(AbstractPath.Type.directory, AbstractPath.Type.placeholder, AbstractPath.Type.decrypted))));
160170

161-
final byte[] buf = new byte[300];
162-
final TransferStatus status = new TransferStatus();
163-
try(final InputStream inputStream = storage.getFeature(Read.class).read(foo, status, new DisabledConnectionCallback())) {
164-
int l = inputStream.read(buf);
165-
assertEquals(9, l);
166-
assertEquals("Hello Foo", new String(Arrays.copyOfRange(buf, 0, l)));
167-
}
171+
assertEquals(new String(expected), readFile(storage, new Path("/cyberduckbucket/alice.txt", EnumSet.of(AbstractPath.Type.file, AbstractPath.Type.decrypted))));
168172
}
169173
{
170174
final PathAttributes subdir = storage.getFeature(AttributesFinder.class).find(new Path("/cyberduckbucket/subdir", EnumSet.of(AbstractPath.Type.directory, AbstractPath.Type.placeholder, AbstractPath.Type.decrypted)));
171175
final AttributedList<Path> list = storage.getFeature(ListService.class).list(new Path("/cyberduckbucket/subdir", EnumSet.of(AbstractPath.Type.directory, AbstractPath.Type.placeholder, AbstractPath.Type.decrypted)).withAttributes(subdir), new DisabledListProgressListener());
172176
assertEquals(1, list.size());
173177
final Path bar = new Path("/cyberduckbucket/subdir/bar.txt", EnumSet.of(AbstractPath.Type.file, AbstractPath.Type.decrypted));
174178
assertTrue(Arrays.toString(list.toArray()), list.contains(bar));
175-
176-
final byte[] buf = new byte[300];
177-
final TransferStatus status = new TransferStatus();
178-
try(final InputStream inputStream = storage.getFeature(Read.class).read(bar, status, new DisabledConnectionCallback())) {
179-
int l = inputStream.read(buf);
180-
assertEquals(9, l);
181-
assertEquals("Hello Bar", new String(Arrays.copyOfRange(buf, 0, l)));
182-
}
179+
assertEquals("Hello Bar", readFile(storage, bar));
183180
}
184181
}
185182
finally {
@@ -217,4 +214,24 @@ public Credentials prompt(final Host bookmark, final String username, final Stri
217214
new DisabledCancelCallback());
218215
return storage;
219216
}
217+
218+
private static byte @NotNull [] writeRandomFile(final Session<?> session, final Path file, int size) throws BackgroundException, IOException {
219+
final byte[] content = RandomUtils.nextBytes(size);
220+
final TransferStatus transferStatus = new TransferStatus().withLength(content.length);
221+
transferStatus.setChecksum(session.getFeature(Write.class).checksum(file, transferStatus).compute(new ByteArrayInputStream(content), transferStatus));
222+
session.getFeature(Bulk.class).pre(Transfer.Type.upload, Collections.singletonMap(new TransferItem(file), transferStatus), new DisabledConnectionCallback());
223+
final StatusOutputStream<?> out = session.getFeature(Write.class).write(file, transferStatus, new DisabledConnectionCallback());
224+
IOUtils.copyLarge(new ByteArrayInputStream(content), out);
225+
out.close();
226+
return content;
227+
}
228+
229+
private static String readFile(final S3Session storage, final Path foo) throws IOException, BackgroundException {
230+
final byte[] buf = new byte[300];
231+
final TransferStatus status = new TransferStatus();
232+
try(final InputStream inputStream = storage.getFeature(Read.class).read(foo, status, new DisabledConnectionCallback())) {
233+
int l = inputStream.read(buf);
234+
return new String(Arrays.copyOfRange(buf, 0, l));
235+
}
236+
}
220237
}

0 commit comments

Comments
 (0)