-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assert that enough bytes are read from stream
- Loading branch information
Showing
3 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/hooks/StreamReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/hooks/StreamReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |