|
19 | 19 | import io.netty.buffer.Unpooled; |
20 | 20 | import org.junit.jupiter.api.Test; |
21 | 21 |
|
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
22 | 23 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
23 | 25 |
|
24 | 26 | public class Http2DefaultFramesTest { |
25 | 27 |
|
@@ -41,4 +43,48 @@ public void testEqualOperation() { |
41 | 43 | dflt.release(); |
42 | 44 | } |
43 | 45 | } |
| 46 | + |
| 47 | + // Reproduces https://github.com/netty/netty/issues/13659 |
| 48 | + // AbstractHttp2StreamFrame.equals() treats two frames with a null stream as equal, but |
| 49 | + // AbstractHttp2StreamFrame.hashCode() previously returned super.hashCode() (identity hash) |
| 50 | + // in that case, producing different hashes for equal instances and violating the |
| 51 | + // Object.hashCode contract. |
| 52 | + @Test |
| 53 | + public void testAbstractHttp2StreamFrameEqualInstancesHaveEqualHashCodes() { |
| 54 | + AbstractHttp2StreamFrame a = new TestStreamFrame(); |
| 55 | + AbstractHttp2StreamFrame b = new TestStreamFrame(); |
| 56 | + assertEquals(a, b); |
| 57 | + assertEquals(a.hashCode(), b.hashCode()); |
| 58 | + } |
| 59 | + |
| 60 | + // Reproduces https://github.com/netty/netty/issues/13659 for DefaultHttp2PingFrame. |
| 61 | + // equals() compares ack and content; before the fix hashCode() folded in the identity |
| 62 | + // hash of Object, so two equal pings had different hash codes. |
| 63 | + @Test |
| 64 | + public void testDefaultHttp2PingFrameEqualInstancesHaveEqualHashCodes() { |
| 65 | + DefaultHttp2PingFrame a = new DefaultHttp2PingFrame(42L, true); |
| 66 | + DefaultHttp2PingFrame b = new DefaultHttp2PingFrame(42L, true); |
| 67 | + assertEquals(a, b); |
| 68 | + assertEquals(a.hashCode(), b.hashCode()); |
| 69 | + } |
| 70 | + |
| 71 | + // Smoke test that the hash actually folds in content and ack. This is a regression guard |
| 72 | + // against the pre-fix implementation, which seeded the hash with Object identity and never |
| 73 | + // folded content into the result at all. Specific hashCode values are an implementation |
| 74 | + // detail; we only assert that the chosen widely-spread inputs do not collide. |
| 75 | + @Test |
| 76 | + public void testDefaultHttp2PingFrameHashCodeDistinguishesDifferentValues() { |
| 77 | + DefaultHttp2PingFrame a = new DefaultHttp2PingFrame(0L, false); |
| 78 | + DefaultHttp2PingFrame differentContent = new DefaultHttp2PingFrame(Long.MAX_VALUE, false); |
| 79 | + DefaultHttp2PingFrame differentAck = new DefaultHttp2PingFrame(0L, true); |
| 80 | + assertNotEquals(a.hashCode(), differentContent.hashCode()); |
| 81 | + assertNotEquals(a.hashCode(), differentAck.hashCode()); |
| 82 | + } |
| 83 | + |
| 84 | + private static final class TestStreamFrame extends AbstractHttp2StreamFrame { |
| 85 | + @Override |
| 86 | + public String name() { |
| 87 | + return "TEST"; |
| 88 | + } |
| 89 | + } |
44 | 90 | } |