|
17 | 17 | import org.elasticsearch.common.blobstore.fs.FsBlobStore;
|
18 | 18 | import org.elasticsearch.common.bytes.BytesArray;
|
19 | 19 | import org.elasticsearch.common.io.Streams;
|
20 |
| -import org.elasticsearch.common.io.stream.BytesStreamOutput; |
21 | 20 | import org.elasticsearch.common.util.MockBigArrays;
|
22 | 21 | import org.elasticsearch.common.xcontent.ToXContent;
|
23 | 22 | import org.elasticsearch.common.xcontent.ToXContentFragment;
|
24 | 23 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
25 | 24 | import org.elasticsearch.common.xcontent.XContentParser;
|
26 | 25 | import org.elasticsearch.common.xcontent.XContentParserUtils;
|
27 |
| -import org.elasticsearch.index.translog.BufferedChecksumStreamOutput; |
28 | 26 | import org.elasticsearch.repositories.blobstore.ChecksumBlobStoreFormat;
|
29 | 27 | import org.elasticsearch.test.ESTestCase;
|
30 | 28 |
|
@@ -134,24 +132,24 @@ protected BlobStore createTestBlobStore() throws IOException {
|
134 | 132 | }
|
135 | 133 |
|
136 | 134 | protected void randomCorruption(BlobContainer blobContainer, String blobName) throws IOException {
|
137 |
| - byte[] buffer = new byte[(int) blobContainer.listBlobsByPrefix(blobName).get(blobName).length()]; |
138 |
| - long originalChecksum = checksum(buffer); |
| 135 | + final byte[] buffer = new byte[(int) blobContainer.listBlobsByPrefix(blobName).get(blobName).length()]; |
139 | 136 | try (InputStream inputStream = blobContainer.readBlob(blobName)) {
|
140 | 137 | Streams.readFully(inputStream, buffer);
|
141 | 138 | }
|
142 |
| - do { |
143 |
| - int location = randomIntBetween(0, buffer.length - 1); |
144 |
| - buffer[location] = (byte) (buffer[location] ^ 42); |
145 |
| - } while (originalChecksum == checksum(buffer)); |
146 |
| - blobContainer.writeBlob(blobName, new BytesArray(buffer), false); |
147 |
| - } |
148 |
| - |
149 |
| - private long checksum(byte[] buffer) throws IOException { |
150 |
| - try (BytesStreamOutput streamOutput = new BytesStreamOutput()) { |
151 |
| - try (BufferedChecksumStreamOutput checksumOutput = new BufferedChecksumStreamOutput(streamOutput)) { |
152 |
| - checksumOutput.write(buffer); |
153 |
| - return checksumOutput.getChecksum(); |
154 |
| - } |
| 139 | + final BytesArray corruptedBytes; |
| 140 | + final int location = randomIntBetween(0, buffer.length - 1); |
| 141 | + if (randomBoolean()) { |
| 142 | + // flipping bits in a single byte will always invalidate the file: CRC-32 certainly detects all eight-bit-burst errors; we don't |
| 143 | + // checksum the last 8 bytes but we do verify that they contain the checksum preceded by 4 zero bytes so in any case this will |
| 144 | + // be a detectable error: |
| 145 | + buffer[location] = (byte) (buffer[location] ^ between(1, 255)); |
| 146 | + corruptedBytes = new BytesArray(buffer); |
| 147 | + } else { |
| 148 | + // truncation will invalidate the file: the last 12 bytes should start with 8 zero bytes but by construction we won't have |
| 149 | + // another sequence of 8 zero bytes anywhere in the file, let alone such a sequence followed by a correct checksum. |
| 150 | + corruptedBytes = new BytesArray(buffer, 0, location); |
155 | 151 | }
|
| 152 | + blobContainer.writeBlob(blobName, corruptedBytes, false); |
156 | 153 | }
|
| 154 | + |
157 | 155 | }
|
0 commit comments