|
39 | 39 | import java.security.NoSuchAlgorithmException; |
40 | 40 | import java.security.Provider; |
41 | 41 | import java.security.Security; |
| 42 | +import java.util.ArrayList; |
42 | 43 | import java.util.concurrent.CompletableFuture; |
43 | 44 | import java.util.concurrent.CompletionException; |
44 | 45 | import java.util.concurrent.ExecutorService; |
@@ -308,114 +309,141 @@ public void failsWhenBothBufferSizeAndDelayedAuthModeEnabled() { |
308 | 309 |
|
309 | 310 | @Test |
310 | 311 | public void customSetBufferSizeWithLargeObject() throws IOException { |
311 | | - final String objectKey = appendTestSuffix("large-object-test-custom-buffer-size"); |
312 | | - |
313 | | - Security.addProvider(new BouncyCastleProvider()); |
314 | | - Provider provider = Security.getProvider("BC"); |
315 | | - |
316 | | - // V3 Client with custom max buffer size 32 MiB. |
317 | | - S3Client v3ClientWithBuffer32MiB = S3EncryptionClient.builder() |
318 | | - .aesKey(AES_KEY) |
319 | | - .cryptoProvider(provider) |
320 | | - .setBufferSize(32 * 1024 * 1024) |
321 | | - .build(); |
322 | | - |
323 | | - // V3 Client with default buffer size (i.e. 64MiB) |
324 | | - // When enableDelayedAuthenticationMode is set to true, delayed authentication mode always takes priority over buffered mode. |
325 | | - S3Client v3ClientWithDelayedAuth = S3EncryptionClient.builder() |
326 | | - .aesKey(AES_KEY) |
327 | | - .cryptoProvider(provider) |
328 | | - .enableDelayedAuthenticationMode(true) |
329 | | - .build(); |
330 | | - |
331 | | - // Tight bound on the custom buffer size limit of 32MiB |
332 | | - final long fileSizeExceedingDefaultLimit = 1024 * 1024 * 32 + 1; |
333 | | - final InputStream largeObjectStream = new BoundedInputStream(fileSizeExceedingDefaultLimit); |
334 | | - v3ClientWithBuffer32MiB.putObject(PutObjectRequest.builder() |
335 | | - .bucket(BUCKET) |
336 | | - .key(objectKey) |
337 | | - .build(), RequestBody.fromInputStream(largeObjectStream, fileSizeExceedingDefaultLimit)); |
338 | | - |
339 | | - largeObjectStream.close(); |
340 | | - |
341 | | - // Object is larger than Buffer, so getObject fails |
342 | | - assertThrows(S3EncryptionClientException.class, () -> v3ClientWithBuffer32MiB.getObjectAsBytes(builder -> builder |
343 | | - .bucket(BUCKET) |
344 | | - .key(objectKey))); |
345 | | - |
346 | | - // You have to either enable the delayed auth mode or increase max buffer size (but in allowed bounds) |
347 | | - ResponseInputStream<GetObjectResponse> response = v3ClientWithDelayedAuth.getObject(builder -> builder |
348 | | - .bucket(BUCKET) |
349 | | - .key(objectKey)); |
350 | | - |
351 | | - |
352 | | - assertTrue(IOUtils.contentEquals(new BoundedInputStream(fileSizeExceedingDefaultLimit), response)); |
353 | | - response.close(); |
| 312 | + ArrayList<Exception> failures = new ArrayList<>(); |
| 313 | + int success=0, fails = 0; |
| 314 | + for(int i=0; i < 50; i++) { |
| 315 | + try { |
| 316 | + final String objectKey = appendTestSuffix("large-object-test-custom-buffer-size"); |
| 317 | + |
| 318 | + Security.addProvider(new BouncyCastleProvider()); |
| 319 | + Provider provider = Security.getProvider("BC"); |
| 320 | + |
| 321 | + // V3 Client with custom max buffer size 32 MiB. |
| 322 | + S3Client v3ClientWithBuffer32MiB = S3EncryptionClient.builder() |
| 323 | + .aesKey(AES_KEY) |
| 324 | + .cryptoProvider(provider) |
| 325 | + .setBufferSize(32 * 1024 * 1024) |
| 326 | + .build(); |
| 327 | + |
| 328 | + // V3 Client with default buffer size (i.e. 64MiB) |
| 329 | + // When enableDelayedAuthenticationMode is set to true, delayed authentication mode always takes priority over buffered mode. |
| 330 | + S3Client v3ClientWithDelayedAuth = S3EncryptionClient.builder() |
| 331 | + .aesKey(AES_KEY) |
| 332 | + .cryptoProvider(provider) |
| 333 | + .enableDelayedAuthenticationMode(true) |
| 334 | + .build(); |
| 335 | + |
| 336 | + // Tight bound on the custom buffer size limit of 32MiB |
| 337 | + final long fileSizeExceedingDefaultLimit = 1024 * 1024 * 32 + 1; |
| 338 | + final InputStream largeObjectStream = new BoundedInputStream(fileSizeExceedingDefaultLimit); |
| 339 | + v3ClientWithBuffer32MiB.putObject(PutObjectRequest.builder() |
| 340 | + .bucket(BUCKET) |
| 341 | + .key(objectKey) |
| 342 | + .build(), RequestBody.fromInputStream(largeObjectStream, fileSizeExceedingDefaultLimit)); |
| 343 | + |
| 344 | + largeObjectStream.close(); |
| 345 | + |
| 346 | + // Object is larger than Buffer, so getObject fails |
| 347 | + assertThrows(S3EncryptionClientException.class, () -> v3ClientWithBuffer32MiB.getObjectAsBytes(builder -> builder |
| 348 | + .bucket(BUCKET) |
| 349 | + .key(objectKey))); |
| 350 | + |
| 351 | + // You have to either enable the delayed auth mode or increase max buffer size (but in allowed bounds) |
| 352 | + ResponseInputStream<GetObjectResponse> response = v3ClientWithDelayedAuth.getObject(builder -> builder |
| 353 | + .bucket(BUCKET) |
| 354 | + .key(objectKey)); |
| 355 | + |
| 356 | + |
| 357 | + assertTrue(IOUtils.contentEquals(new BoundedInputStream(fileSizeExceedingDefaultLimit), response)); |
| 358 | + response.close(); |
| 359 | + |
| 360 | + // Cleanup |
| 361 | + deleteObject(BUCKET, objectKey, v3ClientWithBuffer32MiB); |
| 362 | + v3ClientWithBuffer32MiB.close(); |
| 363 | + v3ClientWithDelayedAuth.close(); |
| 364 | + |
| 365 | + success++; |
| 366 | + } catch(Exception e) { |
| 367 | + fails++; |
| 368 | + failures.add(e); |
| 369 | + } |
| 370 | + System.out.println("Success: " + success + " Fails: " + fails); |
| 371 | + failures.forEach(e -> e.printStackTrace()); |
354 | 372 |
|
355 | | - // Cleanup |
356 | | - deleteObject(BUCKET, objectKey, v3ClientWithBuffer32MiB); |
357 | | - v3ClientWithBuffer32MiB.close(); |
358 | | - v3ClientWithDelayedAuth.close(); |
| 373 | + } |
359 | 374 | } |
360 | 375 |
|
361 | 376 | @Test |
362 | 377 | public void customSetBufferSizeWithLargeObjectAsyncClient() throws IOException { |
363 | | - final String objectKey = appendTestSuffix("large-object-test-custom-buffer-size-async"); |
| 378 | + ArrayList<Exception> failures = new ArrayList<>(); |
| 379 | + int success=0, fails = 0; |
| 380 | + for(int i=0; i < 50; i++) { |
| 381 | + try { |
| 382 | + final String objectKey = appendTestSuffix("large-object-test-custom-buffer-size-async"); |
| 383 | + |
| 384 | + Security.addProvider(new BouncyCastleProvider()); |
| 385 | + Provider provider = Security.getProvider("BC"); |
| 386 | + |
| 387 | + // V3 Client with custom max buffer size 32 MiB. |
| 388 | + S3AsyncClient v3ClientWithBuffer32MiB = S3AsyncEncryptionClient.builder() |
| 389 | + .aesKey(AES_KEY) |
| 390 | + .cryptoProvider(provider) |
| 391 | + .setBufferSize(32 * 1024 * 1024) |
| 392 | + .build(); |
| 393 | + |
| 394 | + // V3 Client with default buffer size (i.e. 64MiB) |
| 395 | + // When enableDelayedAuthenticationMode is set to true, delayed authentication mode always takes priority over buffered mode. |
| 396 | + S3AsyncClient v3ClientWithDelayedAuth = S3AsyncEncryptionClient.builder() |
| 397 | + .aesKey(AES_KEY) |
| 398 | + .cryptoProvider(provider) |
| 399 | + .enableDelayedAuthenticationMode(true) |
| 400 | + .build(); |
| 401 | + |
| 402 | + // Tight bound on the custom buffer size limit of 32MiB |
| 403 | + final long fileSizeExceedingDefaultLimit = 1024 * 1024 * 32 + 1; |
| 404 | + final InputStream largeObjectStream = new BoundedInputStream(fileSizeExceedingDefaultLimit); |
| 405 | + ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); |
| 406 | + CompletableFuture<PutObjectResponse> futurePut = v3ClientWithBuffer32MiB.putObject(PutObjectRequest.builder() |
| 407 | + .bucket(BUCKET) |
| 408 | + .key(objectKey) |
| 409 | + .build(), AsyncRequestBody.fromInputStream(largeObjectStream, fileSizeExceedingDefaultLimit, singleThreadExecutor)); |
| 410 | + |
| 411 | + futurePut.join(); |
| 412 | + largeObjectStream.close(); |
| 413 | + singleThreadExecutor.shutdown(); |
364 | 414 |
|
365 | | - Security.addProvider(new BouncyCastleProvider()); |
366 | | - Provider provider = Security.getProvider("BC"); |
| 415 | + try { |
| 416 | + // Object is larger than Buffer, so getObject fails |
| 417 | + CompletableFuture<ResponseInputStream<GetObjectResponse>> futureResponse = v3ClientWithBuffer32MiB.getObject(builder -> builder |
| 418 | + .bucket(BUCKET) |
| 419 | + .key(objectKey), AsyncResponseTransformer.toBlockingInputStream()); |
| 420 | + futureResponse.join(); |
| 421 | + } catch (CompletionException e) { |
| 422 | + assertEquals(S3EncryptionClientException.class, e.getCause().getClass()); |
| 423 | + } |
367 | 424 |
|
368 | | - // V3 Client with custom max buffer size 32 MiB. |
369 | | - S3AsyncClient v3ClientWithBuffer32MiB = S3AsyncEncryptionClient.builder() |
370 | | - .aesKey(AES_KEY) |
371 | | - .cryptoProvider(provider) |
372 | | - .setBufferSize(32 * 1024 * 1024) |
373 | | - .build(); |
| 425 | + // You have to either enable the delayed auth mode or increase max buffer size (but in allowed bounds) |
| 426 | + CompletableFuture<ResponseInputStream<GetObjectResponse>> futureGet = v3ClientWithDelayedAuth.getObject(builder -> builder |
| 427 | + .bucket(BUCKET) |
| 428 | + .key(objectKey), AsyncResponseTransformer.toBlockingInputStream()); |
| 429 | + ResponseInputStream<GetObjectResponse> output = futureGet.join(); |
374 | 430 |
|
375 | | - // V3 Client with default buffer size (i.e. 64MiB) |
376 | | - // When enableDelayedAuthenticationMode is set to true, delayed authentication mode always takes priority over buffered mode. |
377 | | - S3AsyncClient v3ClientWithDelayedAuth = S3AsyncEncryptionClient.builder() |
378 | | - .aesKey(AES_KEY) |
379 | | - .cryptoProvider(provider) |
380 | | - .enableDelayedAuthenticationMode(true) |
381 | | - .build(); |
| 431 | + assertTrue(IOUtils.contentEquals(new BoundedInputStream(fileSizeExceedingDefaultLimit), output)); |
| 432 | + output.close(); |
382 | 433 |
|
383 | | - // Tight bound on the custom buffer size limit of 32MiB |
384 | | - final long fileSizeExceedingDefaultLimit = 1024 * 1024 * 32 + 1; |
385 | | - final InputStream largeObjectStream = new BoundedInputStream(fileSizeExceedingDefaultLimit); |
386 | | - ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); |
387 | | - CompletableFuture<PutObjectResponse> futurePut = v3ClientWithBuffer32MiB.putObject(PutObjectRequest.builder() |
388 | | - .bucket(BUCKET) |
389 | | - .key(objectKey) |
390 | | - .build(), AsyncRequestBody.fromInputStream(largeObjectStream, fileSizeExceedingDefaultLimit, singleThreadExecutor)); |
391 | | - |
392 | | - futurePut.join(); |
393 | | - largeObjectStream.close(); |
394 | | - singleThreadExecutor.shutdown(); |
| 434 | + // Cleanup |
| 435 | + deleteObject(BUCKET, objectKey, v3ClientWithBuffer32MiB); |
| 436 | + v3ClientWithBuffer32MiB.close(); |
| 437 | + v3ClientWithDelayedAuth.close(); |
395 | 438 |
|
396 | | - try { |
397 | | - // Object is larger than Buffer, so getObject fails |
398 | | - CompletableFuture<ResponseInputStream<GetObjectResponse>> futureResponse = v3ClientWithBuffer32MiB.getObject(builder -> builder |
399 | | - .bucket(BUCKET) |
400 | | - .key(objectKey), AsyncResponseTransformer.toBlockingInputStream()); |
401 | | - futureResponse.join(); |
402 | | - } catch (CompletionException e) { |
403 | | - assertEquals(S3EncryptionClientException.class, e.getCause().getClass()); |
| 439 | + success++; |
| 440 | + } catch (Exception e) { |
| 441 | + fails++; |
| 442 | + failures.add(e); |
| 443 | + } |
| 444 | + System.out.println("Success: " + success + " Fails: " + fails); |
| 445 | + failures.forEach(e -> e.printStackTrace()); |
404 | 446 | } |
405 | | - |
406 | | - // You have to either enable the delayed auth mode or increase max buffer size (but in allowed bounds) |
407 | | - CompletableFuture<ResponseInputStream<GetObjectResponse>> futureGet = v3ClientWithDelayedAuth.getObject(builder -> builder |
408 | | - .bucket(BUCKET) |
409 | | - .key(objectKey), AsyncResponseTransformer.toBlockingInputStream()); |
410 | | - ResponseInputStream<GetObjectResponse> output = futureGet.join(); |
411 | | - |
412 | | - assertTrue(IOUtils.contentEquals(new BoundedInputStream(fileSizeExceedingDefaultLimit), output)); |
413 | | - output.close(); |
414 | | - |
415 | | - // Cleanup |
416 | | - deleteObject(BUCKET, objectKey, v3ClientWithBuffer32MiB); |
417 | | - v3ClientWithBuffer32MiB.close(); |
418 | | - v3ClientWithDelayedAuth.close(); |
419 | 447 | } |
420 | 448 |
|
421 | 449 | @Test |
|
0 commit comments