Skip to content

Commit 9f89d56

Browse files
committed
Verify uploaded file content in test03AddVault. Create directory as well.
1 parent cba38fb commit 9f89d56

File tree

1 file changed

+58
-19
lines changed

1 file changed

+58
-19
lines changed

hub/src/test/java/ch/iterate/hub/core/AbstractHubSynchronizeTest.java

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
package ch.iterate.hub.core;
66

77
import ch.cyberduck.core.*;
8+
import ch.cyberduck.core.exception.BackgroundException;
89
import ch.cyberduck.core.features.Bulk;
10+
import ch.cyberduck.core.features.Directory;
911
import ch.cyberduck.core.features.Home;
10-
import ch.cyberduck.core.features.Touch;
12+
import ch.cyberduck.core.features.Read;
1113
import ch.cyberduck.core.features.Vault;
1214
import ch.cyberduck.core.features.Write;
13-
import ch.cyberduck.core.io.ChecksumComputeFactory;
14-
import ch.cyberduck.core.io.HashAlgorithm;
1515
import ch.cyberduck.core.io.StatusOutputStream;
1616
import ch.cyberduck.core.preferences.PreferencesFactory;
1717
import ch.cyberduck.core.proxy.DisabledProxyFinder;
@@ -27,13 +27,16 @@
2727
import org.apache.commons.lang3.StringUtils;
2828
import org.apache.logging.log4j.LogManager;
2929
import org.apache.logging.log4j.Logger;
30+
import org.jetbrains.annotations.NotNull;
3031
import org.junit.jupiter.api.Assertions;
3132
import org.junit.jupiter.api.Disabled;
3233
import org.junit.jupiter.api.Test;
3334
import org.junit.jupiter.params.ParameterizedTest;
3435
import org.openapitools.jackson.nullable.JsonNullableModule;
3536

3637
import java.io.ByteArrayInputStream;
38+
import java.io.IOException;
39+
import java.util.Arrays;
3740
import java.util.Collections;
3841
import java.util.EnumSet;
3942
import java.util.List;
@@ -60,16 +63,15 @@
6063
import com.fasterxml.jackson.annotation.JsonInclude;
6164
import com.fasterxml.jackson.databind.ObjectMapper;
6265

63-
import org.testcontainers.shaded.org.apache.commons.io.input.NullInputStream;
64-
6566
import static ch.iterate.hub.testsetup.HubTestUtilities.getAdminApiClient;
6667
import static org.junit.jupiter.api.Assertions.*;
6768

6869
public abstract class AbstractHubSynchronizeTest extends AbstractHubTest {
6970
private static final Logger log = LogManager.getLogger(AbstractHubSynchronizeTest.class.getName());
7071

7172
/**
72-
* Use to start unattended setup and then run
73+
* Use to start unattended setup and then run tests with PUnattendedMinio.
74+
*
7375
* @throws InterruptedException
7476
*/
7577
@Test
@@ -259,29 +261,55 @@ public void test03AddVault(final HubTestConfig config) throws Exception {
259261

260262
final Path bucket = new Path(vaultBookmark.getDefaultPath(), EnumSet.of(Path.Type.directory, Path.Type.volume, Path.Type.vault));
261263
assertNotSame(Vault.DISABLED, vaultRegistry.find(session, bucket));
264+
262265
{
266+
// encrypted file listing
263267
final AttributedList<Path> list = session.getFeature(ListService.class).list(bucket, new DisabledListProgressListener());
264268
assertTrue(list.isEmpty());
265269
}
266270
{
271+
// encrypted file upload
267272
final Path home = vaultRegistry.find(session, bucket).getHome();
268-
Path file = new Path(home, "gugus.txt", EnumSet.of(AbstractPath.Type.file));
269-
byte[] content = RandomUtils.nextBytes(234);
270-
TransferStatus transferStatus = new TransferStatus().withLength(content.length);
271-
transferStatus.setChecksum(session.getFeature(Write.class).checksum(file, transferStatus).compute(new ByteArrayInputStream(content), transferStatus));
272-
session.getFeature(Bulk.class).pre(Transfer.Type.upload, Collections.singletonMap(new TransferItem(file), transferStatus), new DisabledConnectionCallback());
273-
StatusOutputStream<?> out = session.getFeature(Write.class).write(file, transferStatus, new DisabledConnectionCallback());
274-
IOUtils.copyLarge(new ByteArrayInputStream(content), out);
275-
out.close();
273+
final Path file = new Path(home, new AlphanumericRandomStringService(25).random(), EnumSet.of(AbstractPath.Type.file));
274+
byte[] content = writeRandomFile(session, file, 234);
276275
final AttributedList<Path> list = session.getFeature(ListService.class).list(bucket, new DisabledListProgressListener());
277-
assertFalse(list.isEmpty());
276+
assertEquals(1, list.size());
277+
assertEquals(file.getName(), list.get(0).getName());
278+
279+
byte[] actual = new byte[300];
280+
int l = session.getFeature(Read.class).read(file, new TransferStatus(), new DisabledConnectionCallback()).read(actual);
281+
assert l == 234;
282+
assertArrayEquals(content, Arrays.copyOfRange(actual, 0, l));
278283
}
284+
{
285+
// encrypted directory creation and listing
286+
final Path home = vaultRegistry.find(session, bucket).getHome();
287+
final Path folder = new Path(home, new AlphanumericRandomStringService(25).random(), EnumSet.of(AbstractPath.Type.directory));
279288

280-
// raw listing encrypted file names
281-
vaultRegistry.close(bucket);
282-
assertSame(Vault.DISABLED, vaultRegistry.find(session, bucket));
283-
assertTrue(vaultRegistry.isEmpty());
289+
session.getFeature(Directory.class).mkdir(folder, new TransferStatus());
290+
final AttributedList<Path> list = session.getFeature(ListService.class).list(bucket, new DisabledListProgressListener());
291+
assertEquals(2, list.size());
292+
293+
{
294+
// encrypted file upload in subfolder
295+
final Path file = new Path(folder, new AlphanumericRandomStringService(25).random(), EnumSet.of(AbstractPath.Type.file));
296+
final byte[] content = writeRandomFile(session, file, 555);
297+
final AttributedList<Path> sublist = session.getFeature(ListService.class).list(folder, new DisabledListProgressListener());
298+
assertEquals(1, sublist.size());
299+
assertEquals(file.getName(), sublist.get(0).getName());
300+
301+
byte[] actual = new byte[600];
302+
int l = session.getFeature(Read.class).read(file, new TransferStatus(), new DisabledConnectionCallback()).read(actual);
303+
assert l == 555;
304+
assertArrayEquals(content, Arrays.copyOfRange(actual, 0, l));
305+
}
306+
}
284307
{
308+
// raw listing encrypted file names
309+
vaultRegistry.close(bucket);
310+
assertSame(Vault.DISABLED, vaultRegistry.find(session, bucket));
311+
assertTrue(vaultRegistry.isEmpty());
312+
285313
final AttributedList<Path> list = session.getFeature(ListService.class).list(bucket, new DisabledListProgressListener());
286314
assertFalse(list.isEmpty());
287315
assertEquals(2, list.size());
@@ -293,4 +321,15 @@ public void test03AddVault(final HubTestConfig config) throws Exception {
293321
hubSession.close();
294322
}
295323
}
324+
325+
private static byte @NotNull [] writeRandomFile(final Session<?> session, final Path file, int size) throws BackgroundException, IOException {
326+
final byte[] content = RandomUtils.nextBytes(size);
327+
final TransferStatus transferStatus = new TransferStatus().withLength(content.length);
328+
transferStatus.setChecksum(session.getFeature(Write.class).checksum(file, transferStatus).compute(new ByteArrayInputStream(content), transferStatus));
329+
session.getFeature(Bulk.class).pre(Transfer.Type.upload, Collections.singletonMap(new TransferItem(file), transferStatus), new DisabledConnectionCallback());
330+
final StatusOutputStream<?> out = session.getFeature(Write.class).write(file, transferStatus, new DisabledConnectionCallback());
331+
IOUtils.copyLarge(new ByteArrayInputStream(content), out);
332+
out.close();
333+
return content;
334+
}
296335
}

0 commit comments

Comments
 (0)