55package ch .iterate .hub .core ;
66
77import ch .cyberduck .core .*;
8+ import ch .cyberduck .core .exception .BackgroundException ;
89import ch .cyberduck .core .features .Bulk ;
10+ import ch .cyberduck .core .features .Directory ;
911import ch .cyberduck .core .features .Home ;
12+ import ch .cyberduck .core .features .Read ;
1013import ch .cyberduck .core .features .Vault ;
1114import ch .cyberduck .core .features .Write ;
1215import ch .cyberduck .core .io .StatusOutputStream ;
2427import org .apache .commons .lang3 .StringUtils ;
2528import org .apache .logging .log4j .LogManager ;
2629import org .apache .logging .log4j .Logger ;
30+ import org .jetbrains .annotations .NotNull ;
2731import org .junit .jupiter .api .Disabled ;
2832import org .junit .jupiter .api .Test ;
2933import org .junit .jupiter .params .ParameterizedTest ;
3034import org .openapitools .jackson .nullable .JsonNullableModule ;
3135
3236import java .io .ByteArrayInputStream ;
37+ import java .io .IOException ;
38+ import java .util .Arrays ;
3339import java .util .Collections ;
3440import java .util .EnumSet ;
3541import java .util .List ;
@@ -67,7 +73,7 @@ public abstract class AbstractHubSynchronizeTest extends AbstractHubTest {
6773 private static final Logger log = LogManager .getLogger (AbstractHubSynchronizeTest .class .getName ());
6874
6975 /**
70- * Use to start unattended setup and then run
76+ * Use to start unattended setup and then run tests with PUnattendedMinio.
7177 *
7278 * @throws InterruptedException
7379 */
@@ -261,29 +267,55 @@ public void test03AddVault(final HubTestConfig config) throws Exception {
261267
262268 final Path bucket = new Path (vaultBookmark .getDefaultPath (), EnumSet .of (Path .Type .directory , Path .Type .volume , Path .Type .vault ));
263269 assertNotSame (Vault .DISABLED , vaultRegistry .find (session , bucket ));
270+
264271 {
272+ // encrypted file listing
265273 final AttributedList <Path > list = session .getFeature (ListService .class ).list (bucket , new DisabledListProgressListener ());
266274 assertTrue (list .isEmpty ());
267275 }
268276 {
277+ // encrypted file upload
269278 final Path home = vaultRegistry .find (session , bucket ).getHome ();
270- Path file = new Path (home , "gugus.txt" , EnumSet .of (AbstractPath .Type .file ));
271- byte [] content = RandomUtils .nextBytes (234 );
272- TransferStatus transferStatus = new TransferStatus ().withLength (content .length );
273- transferStatus .setChecksum (session .getFeature (Write .class ).checksum (file , transferStatus ).compute (new ByteArrayInputStream (content ), transferStatus ));
274- session .getFeature (Bulk .class ).pre (Transfer .Type .upload , Collections .singletonMap (new TransferItem (file ), transferStatus ), new DisabledConnectionCallback ());
275- StatusOutputStream <?> out = session .getFeature (Write .class ).write (file , transferStatus , new DisabledConnectionCallback ());
276- IOUtils .copyLarge (new ByteArrayInputStream (content ), out );
277- out .close ();
279+ final Path file = new Path (home , new AlphanumericRandomStringService (25 ).random (), EnumSet .of (AbstractPath .Type .file ));
280+ byte [] content = writeRandomFile (session , file , 234 );
278281 final AttributedList <Path > list = session .getFeature (ListService .class ).list (bucket , new DisabledListProgressListener ());
279- assertFalse (list .isEmpty ());
282+ assertEquals (1 , list .size ());
283+ assertEquals (file .getName (), list .get (0 ).getName ());
284+
285+ byte [] actual = new byte [300 ];
286+ int l = session .getFeature (Read .class ).read (file , new TransferStatus (), new DisabledConnectionCallback ()).read (actual );
287+ assert l == 234 ;
288+ assertArrayEquals (content , Arrays .copyOfRange (actual , 0 , l ));
280289 }
290+ {
291+ // encrypted directory creation and listing
292+ final Path home = vaultRegistry .find (session , bucket ).getHome ();
293+ final Path folder = new Path (home , new AlphanumericRandomStringService (25 ).random (), EnumSet .of (AbstractPath .Type .directory ));
294+
295+ session .getFeature (Directory .class ).mkdir (folder , new TransferStatus ());
296+ final AttributedList <Path > list = session .getFeature (ListService .class ).list (bucket , new DisabledListProgressListener ());
297+ assertEquals (2 , list .size ());
298+
299+ {
300+ // encrypted file upload in subfolder
301+ final Path file = new Path (folder , new AlphanumericRandomStringService (25 ).random (), EnumSet .of (AbstractPath .Type .file ));
302+ final byte [] content = writeRandomFile (session , file , 555 );
303+ final AttributedList <Path > sublist = session .getFeature (ListService .class ).list (folder , new DisabledListProgressListener ());
304+ assertEquals (1 , sublist .size ());
305+ assertEquals (file .getName (), sublist .get (0 ).getName ());
281306
282- // raw listing encrypted file names
283- vaultRegistry .close (bucket );
284- assertSame (Vault .DISABLED , vaultRegistry .find (session , bucket ));
285- assertTrue (vaultRegistry .isEmpty ());
307+ byte [] actual = new byte [600 ];
308+ int l = session .getFeature (Read .class ).read (file , new TransferStatus (), new DisabledConnectionCallback ()).read (actual );
309+ assert l == 555 ;
310+ assertArrayEquals (content , Arrays .copyOfRange (actual , 0 , l ));
311+ }
312+ }
286313 {
314+ // raw listing encrypted file names
315+ vaultRegistry .close (bucket );
316+ assertSame (Vault .DISABLED , vaultRegistry .find (session , bucket ));
317+ assertTrue (vaultRegistry .isEmpty ());
318+
287319 final AttributedList <Path > list = session .getFeature (ListService .class ).list (bucket , new DisabledListProgressListener ());
288320 assertFalse (list .isEmpty ());
289321 assertEquals (2 , list .size ());
@@ -295,4 +327,15 @@ public void test03AddVault(final HubTestConfig config) throws Exception {
295327 hubSession .close ();
296328 }
297329 }
330+
331+ private static byte @ NotNull [] writeRandomFile (final Session <?> session , final Path file , int size ) throws BackgroundException , IOException {
332+ final byte [] content = RandomUtils .nextBytes (size );
333+ final TransferStatus transferStatus = new TransferStatus ().withLength (content .length );
334+ transferStatus .setChecksum (session .getFeature (Write .class ).checksum (file , transferStatus ).compute (new ByteArrayInputStream (content ), transferStatus ));
335+ session .getFeature (Bulk .class ).pre (Transfer .Type .upload , Collections .singletonMap (new TransferItem (file ), transferStatus ), new DisabledConnectionCallback ());
336+ final StatusOutputStream <?> out = session .getFeature (Write .class ).write (file , transferStatus , new DisabledConnectionCallback ());
337+ IOUtils .copyLarge (new ByteArrayInputStream (content ), out );
338+ out .close ();
339+ return content ;
340+ }
298341}
0 commit comments