| 
27 | 27 | 
 
  | 
28 | 28 | import static org.elasticsearch.common.bytes.ReleasableBytesReferenceStreamInputTests.wrapAsReleasable;  | 
29 | 29 | import static org.hamcrest.Matchers.containsString;  | 
 | 30 | +import static org.hamcrest.Matchers.greaterThan;  | 
30 | 31 | import static org.hamcrest.Matchers.hasItems;  | 
31 | 32 | import static org.hamcrest.Matchers.instanceOf;  | 
32 | 33 | 
 
  | 
@@ -182,7 +183,7 @@ public void testDecodePreHeaderSizeVariableInt() throws IOException {  | 
182 | 183 |         }  | 
183 | 184 |     }  | 
184 | 185 | 
 
  | 
185 |  | -    public void testDecodeHandshakeCompatibility() throws IOException {  | 
 | 186 | +    public void testDecodeHandshakeV7Compatibility() throws IOException {  | 
186 | 187 |         String action = "test-request";  | 
187 | 188 |         long requestId = randomNonNegativeLong();  | 
188 | 189 |         final String headerKey = randomAlphaOfLength(10);  | 
@@ -223,6 +224,55 @@ public void testDecodeHandshakeCompatibility() throws IOException {  | 
223 | 224 | 
 
  | 
224 | 225 |     }  | 
225 | 226 | 
 
  | 
 | 227 | +    public void testDecodeHandshakeV8Compatibility() throws IOException {  | 
 | 228 | +        doHandshakeCompatibilityTest(TransportHandshaker.REQUEST_HANDSHAKE_VERSION, null);  | 
 | 229 | +        doHandshakeCompatibilityTest(TransportHandshaker.REQUEST_HANDSHAKE_VERSION, Compression.Scheme.DEFLATE);  | 
 | 230 | +    }  | 
 | 231 | + | 
 | 232 | +    public void testDecodeHandshakeV9Compatibility() throws IOException {  | 
 | 233 | +        doHandshakeCompatibilityTest(TransportHandshaker.V9_HANDSHAKE_VERSION, null);  | 
 | 234 | +        doHandshakeCompatibilityTest(TransportHandshaker.V9_HANDSHAKE_VERSION, Compression.Scheme.DEFLATE);  | 
 | 235 | +    }  | 
 | 236 | + | 
 | 237 | +    private void doHandshakeCompatibilityTest(TransportVersion transportVersion, Compression.Scheme compressionScheme) throws IOException {  | 
 | 238 | +        String action = "test-request";  | 
 | 239 | +        long requestId = randomNonNegativeLong();  | 
 | 240 | +        final String headerKey = randomAlphaOfLength(10);  | 
 | 241 | +        final String headerValue = randomAlphaOfLength(20);  | 
 | 242 | +        threadContext.putHeader(headerKey, headerValue);  | 
 | 243 | +        OutboundMessage message = new OutboundMessage.Request(  | 
 | 244 | +            threadContext,  | 
 | 245 | +            new TestRequest(randomAlphaOfLength(100)),  | 
 | 246 | +            transportVersion,  | 
 | 247 | +            action,  | 
 | 248 | +            requestId,  | 
 | 249 | +            true,  | 
 | 250 | +            compressionScheme  | 
 | 251 | +        );  | 
 | 252 | + | 
 | 253 | +        try (RecyclerBytesStreamOutput os = new RecyclerBytesStreamOutput(recycler)) {  | 
 | 254 | +            final BytesReference bytes = message.serialize(os);  | 
 | 255 | +            int totalHeaderSize = TcpHeader.headerSize(transportVersion);  | 
 | 256 | + | 
 | 257 | +            InboundDecoder decoder = new InboundDecoder(recycler);  | 
 | 258 | +            final ArrayList<Object> fragments = new ArrayList<>();  | 
 | 259 | +            final ReleasableBytesReference releasable1 = wrapAsReleasable(bytes);  | 
 | 260 | +            int bytesConsumed = decoder.decode(releasable1, fragments::add);  | 
 | 261 | +            assertThat(bytesConsumed, greaterThan(totalHeaderSize));  | 
 | 262 | +            assertTrue(releasable1.hasReferences());  | 
 | 263 | + | 
 | 264 | +            final Header header = (Header) fragments.get(0);  | 
 | 265 | +            assertEquals(requestId, header.getRequestId());  | 
 | 266 | +            assertEquals(transportVersion, header.getVersion());  | 
 | 267 | +            assertEquals(compressionScheme == Compression.Scheme.DEFLATE, header.isCompressed());  | 
 | 268 | +            assertTrue(header.isHandshake());  | 
 | 269 | +            assertTrue(header.isRequest());  | 
 | 270 | +            assertFalse(header.needsToReadVariableHeader());  | 
 | 271 | +            assertEquals(headerValue, header.getRequestHeaders().get(headerKey));  | 
 | 272 | +            fragments.clear();  | 
 | 273 | +        }  | 
 | 274 | +    }  | 
 | 275 | + | 
226 | 276 |     public void testClientChannelTypeFailsDecodingRequests() throws Exception {  | 
227 | 277 |         String action = "test-request";  | 
228 | 278 |         long requestId = randomNonNegativeLong();  | 
@@ -488,23 +538,16 @@ public void testCheckVersionCompatibility() {  | 
488 | 538 |     }  | 
489 | 539 | 
 
  | 
490 | 540 |     public void testCheckHandshakeCompatibility() {  | 
491 |  | -        try {  | 
492 |  | -            InboundDecoder.checkHandshakeVersionCompatibility(randomFrom(TransportHandshaker.ALLOWED_HANDSHAKE_VERSIONS));  | 
493 |  | -        } catch (IllegalStateException e) {  | 
494 |  | -            throw new AssertionError(e);  | 
495 |  | -        }  | 
 | 541 | +        for (final var allowedHandshakeVersion : TransportHandshaker.ALLOWED_HANDSHAKE_VERSIONS) {  | 
 | 542 | +            InboundDecoder.checkHandshakeVersionCompatibility(allowedHandshakeVersion); // should not throw  | 
496 | 543 | 
 
  | 
497 |  | -        var invalid = TransportVersion.fromId(TransportHandshaker.EARLIEST_HANDSHAKE_VERSION.id() - 1);  | 
498 |  | -        try {  | 
499 |  | -            InboundDecoder.checkHandshakeVersionCompatibility(invalid);  | 
500 |  | -            fail();  | 
501 |  | -        } catch (IllegalStateException expected) {  | 
 | 544 | +            var invalid = TransportVersion.fromId(allowedHandshakeVersion.id() + randomFrom(-1, +1));  | 
502 | 545 |             assertEquals(  | 
503 | 546 |                 "Received message from unsupported version: ["  | 
504 | 547 |                     + invalid  | 
505 | 548 |                     + "] allowed versions are: "  | 
506 | 549 |                     + TransportHandshaker.ALLOWED_HANDSHAKE_VERSIONS,  | 
507 |  | -                expected.getMessage()  | 
 | 550 | +                expectThrows(IllegalStateException.class, () -> InboundDecoder.checkHandshakeVersionCompatibility(invalid)).getMessage()  | 
508 | 551 |             );  | 
509 | 552 |         }  | 
510 | 553 |     }  | 
 | 
0 commit comments