|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.repositories.encrypted; |
| 8 | + |
| 9 | +import org.elasticsearch.common.Randomness; |
| 10 | +import org.elasticsearch.common.collect.Tuple; |
| 11 | +import org.elasticsearch.test.ESTestCase; |
| 12 | +import org.hamcrest.Matchers; |
| 13 | +import org.mockito.invocation.InvocationOnMock; |
| 14 | +import org.mockito.stubbing.Answer; |
| 15 | + |
| 16 | +import java.io.EOFException; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 20 | +import java.util.concurrent.atomic.AtomicInteger; |
| 21 | + |
| 22 | +import static org.mockito.Mockito.doAnswer; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +public class PrefixInputStreamTests extends ESTestCase { |
| 27 | + |
| 28 | + public void testZeroLength() throws Exception { |
| 29 | + Tuple<AtomicInteger, InputStream> mockTuple = getMockBoundedInputStream(0); |
| 30 | + PrefixInputStream test = new PrefixInputStream(mockTuple.v2(), 1 + Randomness.get().nextInt(32), randomBoolean()); |
| 31 | + assertThat(test.available(), Matchers.is(0)); |
| 32 | + assertThat(test.read(), Matchers.is(-1)); |
| 33 | + assertThat(test.skip(1 + Randomness.get().nextInt(32)), Matchers.is(0L)); |
| 34 | + } |
| 35 | + |
| 36 | + public void testClose() throws Exception { |
| 37 | + int boundedLength = 1 + Randomness.get().nextInt(256); |
| 38 | + Tuple<AtomicInteger, InputStream> mockTuple = getMockBoundedInputStream(boundedLength); |
| 39 | + int prefixLength = Randomness.get().nextInt(boundedLength); |
| 40 | + PrefixInputStream test = new PrefixInputStream(mockTuple.v2(), prefixLength, randomBoolean()); |
| 41 | + test.close(); |
| 42 | + int byteCountBefore = mockTuple.v1().get(); |
| 43 | + IOException e = expectThrows(IOException.class, () -> { |
| 44 | + test.read(); |
| 45 | + }); |
| 46 | + assertThat(e.getMessage(), Matchers.is("Stream has been closed")); |
| 47 | + e = expectThrows(IOException.class, () -> { |
| 48 | + byte[] b = new byte[1 + Randomness.get().nextInt(32)]; |
| 49 | + test.read(b, 0, 1 + Randomness.get().nextInt(b.length)); |
| 50 | + }); |
| 51 | + assertThat(e.getMessage(), Matchers.is("Stream has been closed")); |
| 52 | + e = expectThrows(IOException.class, () -> { |
| 53 | + test.skip(1 + Randomness.get().nextInt(32)); |
| 54 | + }); |
| 55 | + assertThat(e.getMessage(), Matchers.is("Stream has been closed")); |
| 56 | + e = expectThrows(IOException.class, () -> { |
| 57 | + test.available(); |
| 58 | + }); |
| 59 | + assertThat(e.getMessage(), Matchers.is("Stream has been closed")); |
| 60 | + int byteCountAfter = mockTuple.v1().get(); |
| 61 | + assertThat(byteCountBefore - byteCountAfter, Matchers.is(0)); |
| 62 | + // test closeSource parameter |
| 63 | + AtomicBoolean isClosed = new AtomicBoolean(false); |
| 64 | + InputStream mockIn = mock(InputStream.class); |
| 65 | + doAnswer(new Answer<Void>() { |
| 66 | + public Void answer(InvocationOnMock invocation) { |
| 67 | + isClosed.set(true); |
| 68 | + return null; |
| 69 | + } |
| 70 | + }).when(mockIn).close(); |
| 71 | + new PrefixInputStream(mockIn, 1 + Randomness.get().nextInt(32), true).close(); |
| 72 | + assertThat(isClosed.get(), Matchers.is(true)); |
| 73 | + isClosed.set(false); |
| 74 | + new PrefixInputStream(mockIn, 1 + Randomness.get().nextInt(32), false).close(); |
| 75 | + assertThat(isClosed.get(), Matchers.is(false)); |
| 76 | + } |
| 77 | + |
| 78 | + public void testAvailable() throws Exception { |
| 79 | + AtomicInteger available = new AtomicInteger(0); |
| 80 | + int boundedLength = 1 + Randomness.get().nextInt(256); |
| 81 | + InputStream mockIn = mock(InputStream.class); |
| 82 | + when(mockIn.available()).thenAnswer(invocationOnMock -> { |
| 83 | + return available.get(); |
| 84 | + }); |
| 85 | + PrefixInputStream test = new PrefixInputStream(mockIn, boundedLength, randomBoolean()); |
| 86 | + assertThat(test.available(), Matchers.is(0)); |
| 87 | + available.set(Randomness.get().nextInt(boundedLength)); |
| 88 | + assertThat(test.available(), Matchers.is(available.get())); |
| 89 | + available.set(boundedLength + 1 + Randomness.get().nextInt(boundedLength)); |
| 90 | + assertThat(test.available(), Matchers.is(boundedLength)); |
| 91 | + } |
| 92 | + |
| 93 | + public void testReadPrefixLength() throws Exception { |
| 94 | + int boundedLength = 1 + Randomness.get().nextInt(256); |
| 95 | + Tuple<AtomicInteger, InputStream> mockTuple = getMockBoundedInputStream(boundedLength); |
| 96 | + int prefixLength = Randomness.get().nextInt(boundedLength); |
| 97 | + PrefixInputStream test = new PrefixInputStream(mockTuple.v2(), prefixLength, randomBoolean()); |
| 98 | + int byteCountBefore = mockTuple.v1().get(); |
| 99 | + byte[] b = test.readAllBytes(); |
| 100 | + int byteCountAfter = mockTuple.v1().get(); |
| 101 | + assertThat(b.length, Matchers.is(prefixLength)); |
| 102 | + assertThat(byteCountBefore - byteCountAfter, Matchers.is(prefixLength)); |
| 103 | + assertThat(test.read(), Matchers.is(-1)); |
| 104 | + assertThat(test.available(), Matchers.is(0)); |
| 105 | + assertThat(mockTuple.v2().read(), Matchers.not(-1)); |
| 106 | + } |
| 107 | + |
| 108 | + public void testSkipPrefixLength() throws Exception { |
| 109 | + int boundedLength = 1 + Randomness.get().nextInt(256); |
| 110 | + Tuple<AtomicInteger, InputStream> mockTuple = getMockBoundedInputStream(boundedLength); |
| 111 | + int prefixLength = Randomness.get().nextInt(boundedLength); |
| 112 | + PrefixInputStream test = new PrefixInputStream(mockTuple.v2(), prefixLength, randomBoolean()); |
| 113 | + int byteCountBefore = mockTuple.v1().get(); |
| 114 | + skipNBytes(test, prefixLength); |
| 115 | + int byteCountAfter = mockTuple.v1().get(); |
| 116 | + assertThat(byteCountBefore - byteCountAfter, Matchers.is(prefixLength)); |
| 117 | + assertThat(test.read(), Matchers.is(-1)); |
| 118 | + assertThat(test.available(), Matchers.is(0)); |
| 119 | + assertThat(mockTuple.v2().read(), Matchers.not(-1)); |
| 120 | + } |
| 121 | + |
| 122 | + public void testReadShorterWrapped() throws Exception { |
| 123 | + int boundedLength = 1 + Randomness.get().nextInt(256); |
| 124 | + Tuple<AtomicInteger, InputStream> mockTuple = getMockBoundedInputStream(boundedLength); |
| 125 | + int prefixLength = boundedLength; |
| 126 | + if (randomBoolean()) { |
| 127 | + prefixLength += 1 + Randomness.get().nextInt(boundedLength); |
| 128 | + } |
| 129 | + PrefixInputStream test = new PrefixInputStream(mockTuple.v2(), prefixLength, randomBoolean()); |
| 130 | + int byteCountBefore = mockTuple.v1().get(); |
| 131 | + byte[] b = test.readAllBytes(); |
| 132 | + int byteCountAfter = mockTuple.v1().get(); |
| 133 | + assertThat(b.length, Matchers.is(boundedLength)); |
| 134 | + assertThat(byteCountBefore - byteCountAfter, Matchers.is(boundedLength)); |
| 135 | + assertThat(test.read(), Matchers.is(-1)); |
| 136 | + assertThat(test.available(), Matchers.is(0)); |
| 137 | + assertThat(mockTuple.v2().read(), Matchers.is(-1)); |
| 138 | + assertThat(mockTuple.v2().available(), Matchers.is(0)); |
| 139 | + } |
| 140 | + |
| 141 | + public void testSkipShorterWrapped() throws Exception { |
| 142 | + int boundedLength = 1 + Randomness.get().nextInt(256); |
| 143 | + Tuple<AtomicInteger, InputStream> mockTuple = getMockBoundedInputStream(boundedLength); |
| 144 | + final int prefixLength; |
| 145 | + if (randomBoolean()) { |
| 146 | + prefixLength = boundedLength + 1 + Randomness.get().nextInt(boundedLength); |
| 147 | + } else { |
| 148 | + prefixLength = boundedLength; |
| 149 | + } |
| 150 | + PrefixInputStream test = new PrefixInputStream(mockTuple.v2(), prefixLength, randomBoolean()); |
| 151 | + int byteCountBefore = mockTuple.v1().get(); |
| 152 | + if (prefixLength == boundedLength) { |
| 153 | + skipNBytes(test, prefixLength); |
| 154 | + } else { |
| 155 | + expectThrows(EOFException.class, () -> { |
| 156 | + skipNBytes(test, prefixLength); |
| 157 | + }); |
| 158 | + } |
| 159 | + int byteCountAfter = mockTuple.v1().get(); |
| 160 | + assertThat(byteCountBefore - byteCountAfter, Matchers.is(boundedLength)); |
| 161 | + assertThat(test.read(), Matchers.is(-1)); |
| 162 | + assertThat(test.available(), Matchers.is(0)); |
| 163 | + assertThat(mockTuple.v2().read(), Matchers.is(-1)); |
| 164 | + assertThat(mockTuple.v2().available(), Matchers.is(0)); |
| 165 | + } |
| 166 | + |
| 167 | + private Tuple<AtomicInteger, InputStream> getMockBoundedInputStream(int bound) throws IOException { |
| 168 | + InputStream mockSource = mock(InputStream.class); |
| 169 | + AtomicInteger bytesRemaining = new AtomicInteger(bound); |
| 170 | + when(mockSource.read(org.mockito.Matchers.<byte[]>any(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyInt())). |
| 171 | + thenAnswer(invocationOnMock -> { |
| 172 | + final byte[] b = (byte[]) invocationOnMock.getArguments()[0]; |
| 173 | + final int off = (int) invocationOnMock.getArguments()[1]; |
| 174 | + final int len = (int) invocationOnMock.getArguments()[2]; |
| 175 | + if (len == 0) { |
| 176 | + return 0; |
| 177 | + } else { |
| 178 | + if (bytesRemaining.get() <= 0) { |
| 179 | + return -1; |
| 180 | + } |
| 181 | + int bytesCount = 1 + Randomness.get().nextInt(Math.min(len, bytesRemaining.get())); |
| 182 | + bytesRemaining.addAndGet(-bytesCount); |
| 183 | + return bytesCount; |
| 184 | + } |
| 185 | + }); |
| 186 | + when(mockSource.read()).thenAnswer(invocationOnMock -> { |
| 187 | + if (bytesRemaining.get() <= 0) { |
| 188 | + return -1; |
| 189 | + } |
| 190 | + bytesRemaining.decrementAndGet(); |
| 191 | + return Randomness.get().nextInt(256); |
| 192 | + }); |
| 193 | + when(mockSource.skip(org.mockito.Matchers.anyLong())).thenAnswer(invocationOnMock -> { |
| 194 | + final long n = (long) invocationOnMock.getArguments()[0]; |
| 195 | + if (n <= 0 || bytesRemaining.get() <= 0) { |
| 196 | + return 0; |
| 197 | + } |
| 198 | + int bytesSkipped = 1 + Randomness.get().nextInt(Math.min(bytesRemaining.get(), Math.toIntExact(n))); |
| 199 | + bytesRemaining.addAndGet(-bytesSkipped); |
| 200 | + return bytesSkipped; |
| 201 | + }); |
| 202 | + when(mockSource.available()).thenAnswer(invocationOnMock -> { |
| 203 | + if (bytesRemaining.get() <= 0) { |
| 204 | + return 0; |
| 205 | + } |
| 206 | + return 1 + Randomness.get().nextInt(bytesRemaining.get()); |
| 207 | + }); |
| 208 | + when(mockSource.markSupported()).thenReturn(false); |
| 209 | + return new Tuple<>(bytesRemaining, mockSource); |
| 210 | + } |
| 211 | + |
| 212 | + private static void skipNBytes(InputStream in, long n) throws IOException { |
| 213 | + if (n > 0) { |
| 214 | + long ns = in.skip(n); |
| 215 | + if (ns >= 0 && ns < n) { // skipped too few bytes |
| 216 | + // adjust number to skip |
| 217 | + n -= ns; |
| 218 | + // read until requested number skipped or EOS reached |
| 219 | + while (n > 0 && in.read() != -1) { |
| 220 | + n--; |
| 221 | + } |
| 222 | + // if not enough skipped, then EOFE |
| 223 | + if (n != 0) { |
| 224 | + throw new EOFException(); |
| 225 | + } |
| 226 | + } else if (ns != n) { // skipped negative or too many bytes |
| 227 | + throw new IOException("Unable to skip exactly"); |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments