|
22 | 22 | import javax.crypto.SecretKey;
|
23 | 23 | import java.security.NoSuchAlgorithmException;
|
24 | 24 | import java.util.concurrent.CompletionException;
|
25 |
| -import java.util.regex.Pattern; |
26 | 25 |
|
27 | 26 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
28 | 27 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
@@ -143,6 +142,83 @@ public void AsyncFailsOnRangeWhenLegacyModeDisabled() {
|
143 | 142 | asyncClient.close();
|
144 | 143 | }
|
145 | 144 |
|
| 145 | + @Test |
| 146 | + public void AsyncAesCbcV1toV3RangedGet() { |
| 147 | + final String objectKey = appendTestSuffix("aes-cbc-v1-to-v3-ranged-get-async"); |
| 148 | + |
| 149 | + // V1 Client |
| 150 | + EncryptionMaterialsProvider materialsProvider = |
| 151 | + new StaticEncryptionMaterialsProvider(new EncryptionMaterials(AES_KEY)); |
| 152 | + CryptoConfiguration v1CryptoConfig = |
| 153 | + new CryptoConfiguration(); |
| 154 | + AmazonS3Encryption v1Client = AmazonS3EncryptionClient.encryptionBuilder() |
| 155 | + .withCryptoConfiguration(v1CryptoConfig) |
| 156 | + .withEncryptionMaterials(materialsProvider) |
| 157 | + .build(); |
| 158 | + |
| 159 | + final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" + |
| 160 | + "1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" + |
| 161 | + "2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" + |
| 162 | + "3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" + |
| 163 | + "4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST"; |
| 164 | + |
| 165 | + v1Client.putObject(BUCKET, objectKey, input); |
| 166 | + |
| 167 | + // V3 Client |
| 168 | + S3AsyncClient v3Client = S3AsyncEncryptionClient.builder() |
| 169 | + .aesKey(AES_KEY) |
| 170 | + .enableLegacyUnauthenticatedModes(true) |
| 171 | + .build(); |
| 172 | + |
| 173 | + // Valid Range |
| 174 | + ResponseBytes<GetObjectResponse> objectResponse; |
| 175 | + |
| 176 | + objectResponse = v3Client.getObject(builder -> builder |
| 177 | + .bucket(BUCKET) |
| 178 | + .range("bytes=10-20") |
| 179 | + .key(objectKey), AsyncResponseTransformer.toBytes()).join(); |
| 180 | + String output; |
| 181 | + output = objectResponse.asUtf8String(); |
| 182 | + assertEquals("klmnopqrst0", output); |
| 183 | + |
| 184 | + // Valid start index within input and end index out of range, returns object from start index to End of Stream |
| 185 | + objectResponse = v3Client.getObject(builder -> builder |
| 186 | + .bucket(BUCKET) |
| 187 | + .range("bytes=190-300") |
| 188 | + .key(objectKey), AsyncResponseTransformer.toBytes()).join(); |
| 189 | + output = objectResponse.asUtf8String(); |
| 190 | + assertEquals("KLMNOPQRST", output); |
| 191 | + |
| 192 | + // Invalid range start index range greater than ending index, returns entire object |
| 193 | + objectResponse = v3Client.getObject(builder -> builder |
| 194 | + .bucket(BUCKET) |
| 195 | + .range("bytes=100-50") |
| 196 | + .key(objectKey), AsyncResponseTransformer.toBytes()).join(); |
| 197 | + output = objectResponse.asUtf8String(); |
| 198 | + assertEquals(input, output); |
| 199 | + |
| 200 | + // Invalid range format, returns entire object |
| 201 | + objectResponse = v3Client.getObject(builder -> builder |
| 202 | + .bucket(BUCKET) |
| 203 | + .range("10-20") |
| 204 | + .key(objectKey), AsyncResponseTransformer.toBytes()).join(); |
| 205 | + output = objectResponse.asUtf8String(); |
| 206 | + assertEquals(input, output); |
| 207 | + |
| 208 | + // Invalid range starting index and ending index greater than object length but within Cipher Block size, returns empty object |
| 209 | + objectResponse = v3Client.getObject(builder -> builder |
| 210 | + .bucket(BUCKET) |
| 211 | + .range("bytes=216-217") |
| 212 | + .key(objectKey), AsyncResponseTransformer.toBytes()).join(); |
| 213 | + output = objectResponse.asUtf8String(); |
| 214 | + assertEquals("", output); |
| 215 | + |
| 216 | + // Cleanup |
| 217 | + deleteObject(BUCKET, objectKey, v3Client); |
| 218 | + v3Client.close(); |
| 219 | + } |
| 220 | + |
| 221 | + |
146 | 222 | @Test
|
147 | 223 | public void failsOnRangeWhenLegacyModeDisabled() {
|
148 | 224 | final String objectKey = appendTestSuffix("fails-when-on-range-when-legacy-disabled");
|
@@ -314,6 +390,8 @@ public void AesCbcV1toV3RangedGet() {
|
314 | 390 | .withEncryptionMaterials(materialsProvider)
|
315 | 391 | .build();
|
316 | 392 |
|
| 393 | + // This string is 200 characters/bytes long |
| 394 | + // Due to padding, its ciphertext will be 208 bytes |
317 | 395 | final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
|
318 | 396 | "1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
|
319 | 397 | "2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
|
@@ -360,14 +438,25 @@ public void AesCbcV1toV3RangedGet() {
|
360 | 438 | output = objectResponse.asUtf8String();
|
361 | 439 | assertEquals(input, output);
|
362 | 440 |
|
363 |
| - // Invalid range starting index and ending index greater than object length but within Cipher Block size, returns empty object |
| 441 | + // Invalid range starting index and ending index greater than object length |
| 442 | + // but within Cipher Block size, returns empty object |
364 | 443 | objectResponse = v3Client.getObjectAsBytes(builder -> builder
|
365 | 444 | .bucket(BUCKET)
|
366 | 445 | .range("bytes=216-217")
|
367 | 446 | .key(objectKey));
|
368 | 447 | output = objectResponse.asUtf8String();
|
369 | 448 | assertEquals("", output);
|
370 | 449 |
|
| 450 | + // Invalid range starting index and ending index greater than object length |
| 451 | + // but within Cipher Block size, returns empty object |
| 452 | + objectResponse = v3Client.getObjectAsBytes(builder -> builder |
| 453 | + .bucket(BUCKET) |
| 454 | + .range("bytes=216-218") |
| 455 | + .key(objectKey)); |
| 456 | + output = objectResponse.asUtf8String(); |
| 457 | + assertEquals("", output); |
| 458 | + |
| 459 | + |
371 | 460 | // Cleanup
|
372 | 461 | deleteObject(BUCKET, objectKey, v3Client);
|
373 | 462 | v3Client.close();
|
@@ -411,4 +500,5 @@ public void AesCbcV1toV3FailsRangeExceededObjectLength() {
|
411 | 500 | deleteObject(BUCKET, objectKey, v3Client);
|
412 | 501 | v3Client.close();
|
413 | 502 | }
|
| 503 | + |
414 | 504 | }
|
0 commit comments