|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.ignite.spi.discovery.tcp; |
| 19 | + |
| 20 | +import java.io.ByteArrayInputStream; |
| 21 | +import java.io.ByteArrayOutputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.OutputStream; |
| 25 | +import java.net.Socket; |
| 26 | +import org.apache.ignite.IgniteCheckedException; |
| 27 | +import org.apache.ignite.internal.IgniteEx; |
| 28 | +import org.apache.ignite.internal.util.typedef.X; |
| 29 | +import org.apache.ignite.internal.util.typedef.internal.U; |
| 30 | +import org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryHandshakeRequest; |
| 31 | +import org.apache.ignite.testframework.GridTestUtils; |
| 32 | +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +/** Tests Java serialization header detection in discovery messages. */ |
| 36 | +public class TcpDiscoveryIoSessionDifferentSerializationTest extends GridCommonAbstractTest { |
| 37 | + /** {@inheritDoc} */ |
| 38 | + @Override protected void afterTest() throws Exception { |
| 39 | + stopAllGrids(); |
| 40 | + } |
| 41 | + |
| 42 | + /** */ |
| 43 | + @Test |
| 44 | + public void testDetectJavaObjectStreamHeader() throws Exception { |
| 45 | + IgniteEx grid = startGrid(0); |
| 46 | + |
| 47 | + TcpDiscoverySpi spi = (TcpDiscoverySpi)grid.configuration().getDiscoverySpi(); |
| 48 | + |
| 49 | + TcpDiscoveryHandshakeRequest req = new TcpDiscoveryHandshakeRequest(); |
| 50 | + |
| 51 | + byte[] bytes = U.marshal(spi.marshaller(), req); |
| 52 | + |
| 53 | + TcpDiscoveryIoSession ses = new TcpDiscoveryIoSession( |
| 54 | + new TestSocket(new ByteArrayInputStream(bytes), new ByteArrayOutputStream()), |
| 55 | + spi |
| 56 | + ); |
| 57 | + |
| 58 | + Throwable e = GridTestUtils.assertThrows(log, () -> ses.readMessage(), IgniteCheckedException.class, null); |
| 59 | + |
| 60 | + assertTrue(X.hasCause(e, "Incompatible discovery protocol: received Java ObjectStream header", IOException.class)); |
| 61 | + } |
| 62 | + |
| 63 | + /** */ |
| 64 | + private static class TestSocket extends Socket { |
| 65 | + /** */ |
| 66 | + private final InputStream in; |
| 67 | + |
| 68 | + /** */ |
| 69 | + private final OutputStream out; |
| 70 | + |
| 71 | + /** |
| 72 | + * @param in Input stream. |
| 73 | + * @param out Output stream. |
| 74 | + */ |
| 75 | + private TestSocket(InputStream in, OutputStream out) { |
| 76 | + this.in = in; |
| 77 | + this.out = out; |
| 78 | + } |
| 79 | + |
| 80 | + /** {@inheritDoc} */ |
| 81 | + @Override public InputStream getInputStream() { |
| 82 | + return in; |
| 83 | + } |
| 84 | + |
| 85 | + /** {@inheritDoc} */ |
| 86 | + @Override public OutputStream getOutputStream() { |
| 87 | + return out; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments