|
| 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 | +package org.apache.dubbo.remoting.transport.netty4; |
| 18 | + |
| 19 | +import org.apache.dubbo.common.URL; |
| 20 | +import org.apache.dubbo.remoting.ChannelHandler; |
| 21 | +import org.apache.dubbo.remoting.api.ProtocolDetector; |
| 22 | +import org.apache.dubbo.remoting.api.WireProtocol; |
| 23 | +import org.apache.dubbo.remoting.api.pu.ChannelOperator; |
| 24 | +import org.apache.dubbo.remoting.api.ssl.ContextOperator; |
| 25 | +import org.apache.dubbo.rpc.model.ApplicationModel; |
| 26 | +import org.apache.dubbo.rpc.model.FrameworkModel; |
| 27 | + |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.concurrent.atomic.AtomicInteger; |
| 30 | + |
| 31 | +import io.netty.buffer.Unpooled; |
| 32 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | + |
| 39 | +class NettyPortUnificationServerHandlerTest { |
| 40 | + |
| 41 | + @Test |
| 42 | + void shouldDetectPlaintextProtocolWhenAuthPolicyIsNone() { |
| 43 | + AtomicInteger detectCount = new AtomicInteger(); |
| 44 | + FrameworkModel frameworkModel = new FrameworkModel(); |
| 45 | + try { |
| 46 | + ApplicationModel applicationModel = frameworkModel.newApplication(); |
| 47 | + URL url = URL.valueOf("dubbo://127.0.0.1:20880?codec=default&pu.test.cert=true") |
| 48 | + .setScopeModel(applicationModel); |
| 49 | + ChannelHandler handler = mock(ChannelHandler.class); |
| 50 | + WireProtocol protocol = new CountingWireProtocol(detectCount); |
| 51 | + NettyPortUnificationServerHandler puHandler = new NettyPortUnificationServerHandler( |
| 52 | + url, |
| 53 | + true, |
| 54 | + Collections.singletonMap("dubbo", protocol), |
| 55 | + handler, |
| 56 | + Collections.singletonMap("dubbo", url), |
| 57 | + Collections.emptyMap()); |
| 58 | + EmbeddedChannel channel = new EmbeddedChannel(puHandler); |
| 59 | + |
| 60 | + channel.writeInbound(Unpooled.wrappedBuffer(new byte[] {(byte) 0xda, (byte) 0xbb, 0, 0, 0})); |
| 61 | + |
| 62 | + assertEquals(1, detectCount.get()); |
| 63 | + assertTrue(channel.isOpen()); |
| 64 | + channel.finishAndReleaseAll(); |
| 65 | + } finally { |
| 66 | + frameworkModel.destroy(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static final class CountingWireProtocol implements WireProtocol { |
| 71 | + private final AtomicInteger detectCount; |
| 72 | + |
| 73 | + private CountingWireProtocol(AtomicInteger detectCount) { |
| 74 | + this.detectCount = detectCount; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ProtocolDetector detector() { |
| 79 | + return in -> { |
| 80 | + detectCount.incrementAndGet(); |
| 81 | + return ProtocolDetector.Result.needMoreData(); |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void configServerProtocolHandler(URL url, ChannelOperator operator) {} |
| 87 | + |
| 88 | + @Override |
| 89 | + public void configClientPipeline(URL url, ChannelOperator operator, ContextOperator contextOperator) {} |
| 90 | + |
| 91 | + @Override |
| 92 | + public void close() {} |
| 93 | + } |
| 94 | +} |
0 commit comments