2015-08-30 12:11:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2015 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.channel.epoll;
|
|
|
|
|
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
2016-03-04 10:40:46 +01:00
|
|
|
import io.netty.channel.ChannelOption;
|
2015-08-30 12:11:41 +02:00
|
|
|
import io.netty.channel.ConnectTimeoutException;
|
|
|
|
import io.netty.channel.EventLoopGroup;
|
|
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
2015-09-24 01:44:26 +02:00
|
|
|
import io.netty.util.NetUtil;
|
2015-08-30 12:11:41 +02:00
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2015-09-24 01:44:26 +02:00
|
|
|
public class EpollSocketTcpMd5Test {
|
2015-08-30 12:11:41 +02:00
|
|
|
private static final byte[] SERVER_KEY = "abc".getBytes(CharsetUtil.US_ASCII);
|
|
|
|
private static final byte[] BAD_KEY = "def".getBytes(CharsetUtil.US_ASCII);
|
|
|
|
private static EventLoopGroup GROUP;
|
|
|
|
private EpollServerSocketChannel server;
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
public static void beforeClass() {
|
|
|
|
GROUP = new EpollEventLoopGroup(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
public static void afterClass() {
|
|
|
|
GROUP.shutdownGracefully();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
Bootstrap bootstrap = new Bootstrap();
|
|
|
|
server = (EpollServerSocketChannel) bootstrap.group(GROUP)
|
|
|
|
.channel(EpollServerSocketChannel.class)
|
|
|
|
.handler(new ChannelInboundHandlerAdapter())
|
2016-01-26 21:39:20 +01:00
|
|
|
.bind(new InetSocketAddress(NetUtil.LOCALHOST4, 0)).syncUninterruptibly().channel();
|
2015-08-30 12:11:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void teardown() {
|
|
|
|
server.close().syncUninterruptibly();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testServerSocketChannelOption() throws Exception {
|
2015-09-24 01:44:26 +02:00
|
|
|
server.config().setOption(EpollChannelOption.TCP_MD5SIG,
|
|
|
|
Collections.<InetAddress, byte[]>singletonMap(NetUtil.LOCALHOST4, SERVER_KEY));
|
2015-08-30 12:11:41 +02:00
|
|
|
server.config().setOption(EpollChannelOption.TCP_MD5SIG, Collections.<InetAddress, byte[]>emptyMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testServerOption() throws Exception {
|
|
|
|
Bootstrap bootstrap = new Bootstrap();
|
|
|
|
EpollServerSocketChannel ch = (EpollServerSocketChannel) bootstrap.group(GROUP)
|
|
|
|
.channel(EpollServerSocketChannel.class)
|
|
|
|
.handler(new ChannelInboundHandlerAdapter())
|
|
|
|
.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
|
|
|
|
|
2015-09-24 01:44:26 +02:00
|
|
|
ch.config().setOption(EpollChannelOption.TCP_MD5SIG,
|
|
|
|
Collections.<InetAddress, byte[]>singletonMap(NetUtil.LOCALHOST4, SERVER_KEY));
|
2015-08-30 12:11:41 +02:00
|
|
|
ch.config().setOption(EpollChannelOption.TCP_MD5SIG, Collections.<InetAddress, byte[]>emptyMap());
|
|
|
|
|
|
|
|
ch.close().syncUninterruptibly();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = ConnectTimeoutException.class)
|
|
|
|
public void testKeyMismatch() throws Exception {
|
2015-09-24 01:44:26 +02:00
|
|
|
server.config().setOption(EpollChannelOption.TCP_MD5SIG,
|
|
|
|
Collections.<InetAddress, byte[]>singletonMap(NetUtil.LOCALHOST4, SERVER_KEY));
|
2015-08-30 12:11:41 +02:00
|
|
|
|
|
|
|
EpollSocketChannel client = (EpollSocketChannel) new Bootstrap().group(GROUP)
|
|
|
|
.channel(EpollSocketChannel.class)
|
|
|
|
.handler(new ChannelInboundHandlerAdapter())
|
|
|
|
.option(EpollChannelOption.TCP_MD5SIG,
|
2015-09-24 01:44:26 +02:00
|
|
|
Collections.<InetAddress, byte[]>singletonMap(NetUtil.LOCALHOST4, BAD_KEY))
|
2016-03-04 10:40:46 +01:00
|
|
|
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
|
2015-08-30 12:11:41 +02:00
|
|
|
.connect(server.localAddress()).syncUninterruptibly().channel();
|
|
|
|
client.close().syncUninterruptibly();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testKeyMatch() throws Exception {
|
2015-09-24 01:44:26 +02:00
|
|
|
server.config().setOption(EpollChannelOption.TCP_MD5SIG,
|
|
|
|
Collections.<InetAddress, byte[]>singletonMap(NetUtil.LOCALHOST4, SERVER_KEY));
|
2015-08-30 12:11:41 +02:00
|
|
|
|
|
|
|
EpollSocketChannel client = (EpollSocketChannel) new Bootstrap().group(GROUP)
|
|
|
|
.channel(EpollSocketChannel.class)
|
|
|
|
.handler(new ChannelInboundHandlerAdapter())
|
|
|
|
.option(EpollChannelOption.TCP_MD5SIG,
|
2015-09-24 01:44:26 +02:00
|
|
|
Collections.<InetAddress, byte[]>singletonMap(NetUtil.LOCALHOST4, SERVER_KEY))
|
2015-08-30 12:11:41 +02:00
|
|
|
.connect(server.localAddress()).syncUninterruptibly().channel();
|
|
|
|
client.close().syncUninterruptibly();
|
|
|
|
}
|
|
|
|
}
|