Skip to content

Commit

Permalink
Assert that enough bytes are read from stream
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeuffer committed Nov 26, 2020
1 parent c0ae910 commit 41953e1
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ private static int readInt(InputStream in) throws IOException {
}

private static byte[] read(InputStream in, int length) throws IOException {
byte[] buffer = new byte[length];
int read = in.read(buffer);
if (read < length) {
throw new EOFException("failed to read bytes from socket");
}
return buffer;
return StreamReader.read(in, length);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package sonia.scm.repository.hooks;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

final class StreamReader {

private StreamReader() {
}

static byte[] read(InputStream stream, int length) throws IOException {
byte[] result = new byte[length];
int overallBytesRead = 0;
while (overallBytesRead < length) {
int bytesRead = stream.read(result, overallBytesRead, length - overallBytesRead);
if (bytesRead < 0) {
throw new EOFException("failed to read bytes from socket");
}
overallBytesRead += bytesRead;
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package sonia.scm.repository.hooks;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class StreamReaderTest {

@Test
void shouldReadRequiredBytes() throws IOException {
byte[] input = createBytes(100);
InputStream inputStream = new ByteArrayInputStream(input) {
@Override
public synchronized int read(byte[] b, int off, int len) {
return super.read(b, off, Math.min(8, len));
}
};

byte[] read = StreamReader.read(inputStream, 50);

assertThat(read)
.hasSize(50)
.containsExactly(Arrays.copyOf(input, 50));
assertThat(inputStream.available()).isEqualTo(50);
}

@Test
void shouldFailIfNotEnoughBytes() {
byte[] input = createBytes(49);
InputStream inputStream = new ByteArrayInputStream(input);

assertThrows(EOFException.class, () -> StreamReader.read(inputStream, 50));
}

private byte[] createBytes(int length) {
byte[] input = new byte[length];
for (int i = 0; i < input.length; ++i) {
input[i] = (byte) i;
}
return input;
}

private void fill(byte[] bytes) {
for (int i = 0; i < bytes.length; ++i) {
bytes[i] = (byte) i;
}
}
}

0 comments on commit 41953e1

Please sign in to comment.