|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.common.blobstore; |
| 10 | + |
| 11 | +import org.opensearch.common.Randomness; |
| 12 | +import org.opensearch.common.blobstore.stream.read.ReadContext; |
| 13 | +import org.opensearch.common.blobstore.stream.read.listener.ListenerTestUtils; |
| 14 | +import org.opensearch.common.crypto.CryptoHandler; |
| 15 | +import org.opensearch.common.crypto.DecryptedRangedStreamProvider; |
| 16 | +import org.opensearch.common.io.InputStreamContainer; |
| 17 | +import org.opensearch.core.action.ActionListener; |
| 18 | +import org.opensearch.test.OpenSearchTestCase; |
| 19 | + |
| 20 | +import java.io.ByteArrayInputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | +import java.util.function.UnaryOperator; |
| 24 | + |
| 25 | +import org.mockito.Mockito; |
| 26 | + |
| 27 | +import static org.mockito.ArgumentMatchers.any; |
| 28 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 29 | +import static org.mockito.ArgumentMatchers.eq; |
| 30 | +import static org.mockito.Mockito.mock; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +public class AsyncMultiStreamEncryptedBlobContainerTests extends OpenSearchTestCase { |
| 34 | + |
| 35 | + // Tests the happy path scenario for decrypting a read context |
| 36 | + @SuppressWarnings("unchecked") |
| 37 | + public void testReadBlobAsync() throws Exception { |
| 38 | + String testBlobName = "testBlobName"; |
| 39 | + int size = 100; |
| 40 | + |
| 41 | + // Mock objects needed for the test |
| 42 | + AsyncMultiStreamBlobContainer blobContainer = mock(AsyncMultiStreamBlobContainer.class); |
| 43 | + CryptoHandler<Object, Object> cryptoHandler = mock(CryptoHandler.class); |
| 44 | + Object cryptoContext = mock(Object.class); |
| 45 | + when(cryptoHandler.loadEncryptionMetadata(any())).thenReturn(cryptoContext); |
| 46 | + when(cryptoHandler.estimateDecryptedLength(any(), anyLong())).thenReturn((long) size); |
| 47 | + long[] adjustedRanges = { 0, size - 1 }; |
| 48 | + DecryptedRangedStreamProvider rangedStreamProvider = new DecryptedRangedStreamProvider(adjustedRanges, UnaryOperator.identity()); |
| 49 | + when(cryptoHandler.createDecryptingStreamOfRange(eq(cryptoContext), anyLong(), anyLong())).thenReturn(rangedStreamProvider); |
| 50 | + |
| 51 | + // Objects needed for API call |
| 52 | + final byte[] data = new byte[size]; |
| 53 | + Randomness.get().nextBytes(data); |
| 54 | + final InputStreamContainer inputStreamContainer = new InputStreamContainer(new ByteArrayInputStream(data), data.length, 0); |
| 55 | + final ListenerTestUtils.CountingCompletionListener<ReadContext> completionListener = |
| 56 | + new ListenerTestUtils.CountingCompletionListener<>(); |
| 57 | + final ReadContext readContext = new ReadContext(size, List.of(inputStreamContainer), null); |
| 58 | + |
| 59 | + Mockito.doAnswer(invocation -> { |
| 60 | + ActionListener<ReadContext> readContextActionListener = invocation.getArgument(1); |
| 61 | + readContextActionListener.onResponse(readContext); |
| 62 | + return null; |
| 63 | + }).when(blobContainer).readBlobAsync(eq(testBlobName), any()); |
| 64 | + |
| 65 | + AsyncMultiStreamEncryptedBlobContainer<Object, Object> asyncMultiStreamEncryptedBlobContainer = |
| 66 | + new AsyncMultiStreamEncryptedBlobContainer<>(blobContainer, cryptoHandler); |
| 67 | + asyncMultiStreamEncryptedBlobContainer.readBlobAsync(testBlobName, completionListener); |
| 68 | + |
| 69 | + // Assert results |
| 70 | + ReadContext response = completionListener.getResponse(); |
| 71 | + assertEquals(0, completionListener.getFailureCount()); |
| 72 | + assertEquals(1, completionListener.getResponseCount()); |
| 73 | + assertNull(completionListener.getException()); |
| 74 | + |
| 75 | + assertTrue(response instanceof AsyncMultiStreamEncryptedBlobContainer.DecryptedReadContext); |
| 76 | + assertEquals(1, response.getNumberOfParts()); |
| 77 | + assertEquals(size, response.getBlobSize()); |
| 78 | + |
| 79 | + InputStreamContainer responseContainer = response.getPartStreams().get(0); |
| 80 | + assertEquals(0, responseContainer.getOffset()); |
| 81 | + assertEquals(size, responseContainer.getContentLength()); |
| 82 | + assertEquals(100, responseContainer.getInputStream().available()); |
| 83 | + } |
| 84 | + |
| 85 | + // Tests the exception scenario for decrypting a read context |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + public void testReadBlobAsyncException() throws Exception { |
| 88 | + String testBlobName = "testBlobName"; |
| 89 | + int size = 100; |
| 90 | + |
| 91 | + // Mock objects needed for the test |
| 92 | + AsyncMultiStreamBlobContainer blobContainer = mock(AsyncMultiStreamBlobContainer.class); |
| 93 | + CryptoHandler<Object, Object> cryptoHandler = mock(CryptoHandler.class); |
| 94 | + when(cryptoHandler.loadEncryptionMetadata(any())).thenThrow(new IOException()); |
| 95 | + |
| 96 | + // Objects needed for API call |
| 97 | + final byte[] data = new byte[size]; |
| 98 | + Randomness.get().nextBytes(data); |
| 99 | + final InputStreamContainer inputStreamContainer = new InputStreamContainer(new ByteArrayInputStream(data), data.length, 0); |
| 100 | + final ListenerTestUtils.CountingCompletionListener<ReadContext> completionListener = |
| 101 | + new ListenerTestUtils.CountingCompletionListener<>(); |
| 102 | + final ReadContext readContext = new ReadContext(size, List.of(inputStreamContainer), null); |
| 103 | + |
| 104 | + Mockito.doAnswer(invocation -> { |
| 105 | + ActionListener<ReadContext> readContextActionListener = invocation.getArgument(1); |
| 106 | + readContextActionListener.onResponse(readContext); |
| 107 | + return null; |
| 108 | + }).when(blobContainer).readBlobAsync(eq(testBlobName), any()); |
| 109 | + |
| 110 | + AsyncMultiStreamEncryptedBlobContainer<Object, Object> asyncMultiStreamEncryptedBlobContainer = |
| 111 | + new AsyncMultiStreamEncryptedBlobContainer<>(blobContainer, cryptoHandler); |
| 112 | + asyncMultiStreamEncryptedBlobContainer.readBlobAsync(testBlobName, completionListener); |
| 113 | + |
| 114 | + // Assert results |
| 115 | + assertEquals(1, completionListener.getFailureCount()); |
| 116 | + assertEquals(0, completionListener.getResponseCount()); |
| 117 | + assertNull(completionListener.getResponse()); |
| 118 | + assertTrue(completionListener.getException() instanceof IOException); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments