-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing GOSSIP_MAX_SIZE to snappy uncompressor #8899
Changes from all commits
37a1b31
1ea4f0a
373b638
48fada3
9050544
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,9 @@ | |
import tech.pegasys.teku.infrastructure.ssz.sos.SszLengthBounds; | ||
|
||
public class SnappyBlockCompressorTest { | ||
|
||
private static final long GOSSIP_MAX_SIZE = Long.MAX_VALUE; | ||
|
||
private final SnappyBlockCompressor compressor = new SnappyBlockCompressor(); | ||
|
||
@Test | ||
|
@@ -29,7 +32,8 @@ public void roundTrip() throws DecodingException { | |
|
||
final Bytes compressed = compressor.compress(original); | ||
assertThat(compressed).isNotEqualTo(original); | ||
final Bytes uncompressed = compressor.uncompress(compressed, SszLengthBounds.ofBytes(0, 1000)); | ||
final Bytes uncompressed = | ||
compressor.uncompress(compressed, SszLengthBounds.ofBytes(0, 1000), GOSSIP_MAX_SIZE); | ||
|
||
assertThat(uncompressed).isEqualTo(original); | ||
} | ||
|
@@ -38,25 +42,61 @@ public void roundTrip() throws DecodingException { | |
public void uncompress_randomData() { | ||
final Bytes data = Bytes.fromHexString("0x0102"); | ||
|
||
assertThatThrownBy(() -> compressor.uncompress(data, SszLengthBounds.ofBytes(0, 1000))) | ||
assertThatThrownBy( | ||
() -> compressor.uncompress(data, SszLengthBounds.ofBytes(0, 1000), GOSSIP_MAX_SIZE)) | ||
.isInstanceOf(DecodingException.class); | ||
} | ||
|
||
@Test | ||
void uncompress_uncompressedLengthLongerThanMaxLength() { | ||
void uncompress_uncompressedLengthLongerThanSszLenghtBounds() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. - uncompress_uncompressedLengthLongerThanSszLenghtBounds()
+ uncompress_uncompressedLengthLongerThanSszLenghthBounds() |
||
final Bytes original = Bytes.fromHexString("0x010203040506"); | ||
|
||
final Bytes compressed = compressor.compress(original); | ||
assertThatThrownBy(() -> compressor.uncompress(compressed, SszLengthBounds.ofBytes(0, 4))) | ||
.isInstanceOf(DecodingException.class); | ||
assertThatThrownBy( | ||
() -> compressor.uncompress(compressed, SszLengthBounds.ofBytes(0, 4), GOSSIP_MAX_SIZE)) | ||
.isInstanceOf(DecodingException.class) | ||
.hasMessageContaining("not within expected bounds"); | ||
} | ||
|
||
@Test | ||
void uncompress_uncompressedLengthShorterThanMinLength() { | ||
void uncompress_uncompressedLengthShorterThanSszLengthBounds() { | ||
final Bytes original = Bytes.fromHexString("0x010203040506"); | ||
|
||
final Bytes compressed = compressor.compress(original); | ||
assertThatThrownBy(() -> compressor.uncompress(compressed, SszLengthBounds.ofBytes(100, 200))) | ||
.isInstanceOf(DecodingException.class); | ||
|
||
assertThatThrownBy( | ||
() -> | ||
compressor.uncompress( | ||
compressed, SszLengthBounds.ofBytes(100, 200), GOSSIP_MAX_SIZE)) | ||
.isInstanceOf(DecodingException.class) | ||
.hasMessageContaining("not within expected bounds"); | ||
} | ||
|
||
@Test | ||
void uncompress_uncompressedLengthLongerThanMaxBytesLength() { | ||
final Bytes original = Bytes.fromHexString("0x010203040506"); | ||
final long smallMaxBytesLength = 3; | ||
assertThat(smallMaxBytesLength).isLessThan(original.size()); | ||
Comment on lines
+77
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be a little more precise, I would probably do something like: assertThat(original.size()).isNotEqualTo(0);
final long smallMaxBytesLength = original.size() - 1; |
||
|
||
final Bytes compressed = compressor.compress(original); | ||
assertThatThrownBy( | ||
() -> | ||
compressor.uncompress( | ||
compressed, SszLengthBounds.ofBytes(0, 1000), smallMaxBytesLength)) | ||
.isInstanceOf(DecodingException.class) | ||
.hasMessageContaining("exceeds max length in bytes"); | ||
} | ||
|
||
@Test | ||
void uncompress_uncompressedLengthEqualThanMaxBytesLength() throws DecodingException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. - uncompress_uncompressedLengthEqualThanMaxBytesLength()
+ uncompress_uncompressedLengthEqualToMaxBytesLength() |
||
final Bytes original = Bytes.fromHexString("0x010203040506"); | ||
final long exactMaxBytesLength = original.size(); | ||
|
||
final Bytes compressed = compressor.compress(original); | ||
assertThat(compressed).isNotEqualTo(original); | ||
final Bytes uncompressed = | ||
compressor.uncompress(compressed, SszLengthBounds.ofBytes(0, 1000), exactMaxBytesLength); | ||
|
||
assertThat(uncompressed).isEqualTo(original); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also add a check to
compress
to ensure that the uncompressed size is <=maxBytesLength
.https://github.com/ethereum/consensus-specs/blob/3d180786af8f661a9dc53d1aadf0f89972a56024/specs/phase0/p2p-interface.md?plain=1#L276