--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.bgpcep.tcpmd5.nio;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+
+import java.io.IOException;
+import java.net.SocketOption;
+import java.net.StandardSocketOptions;
+import java.nio.channels.NetworkChannel;
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.bgpcep.tcpmd5.KeyAccess;
+import org.opendaylight.bgpcep.tcpmd5.KeyAccessFactory;
+import org.opendaylight.bgpcep.tcpmd5.MD5SocketOptions;
+
+import com.google.common.collect.ImmutableSet;
+
+public class MD5ChannelOptionsTest {
+
+ @Mock
+ private KeyAccessFactory keyAccessFactory;
+ @Mock
+ private KeyAccess keyAccess;
+ @Mock
+ private NetworkChannel channel;
+
+ @Before
+ public void setup() throws IOException {
+ MockitoAnnotations.initMocks(this);
+
+ Mockito.doReturn(keyAccess).when(keyAccessFactory).getKeyAccess(channel);
+ Mockito.doReturn(null).when(keyAccess).getKey();
+ Mockito.doNothing().when(keyAccess).setKey(any(byte[].class));
+
+ Mockito.doReturn(ImmutableSet.of(StandardSocketOptions.TCP_NODELAY)).when(channel).supportedOptions();
+ Mockito.doReturn(false).when(channel).getOption(StandardSocketOptions.TCP_NODELAY);
+ Mockito.doReturn(channel).when(channel).setOption(StandardSocketOptions.TCP_NODELAY, true);
+ }
+
+ @Test
+ public void testCreate() {
+ final MD5ChannelOptions opts = MD5ChannelOptions.create(keyAccessFactory, channel);
+
+ final Set<SocketOption<?>> so = opts.supportedOptions();
+ assertTrue(so.contains(MD5SocketOptions.TCP_MD5SIG));
+ assertTrue(so.contains(StandardSocketOptions.TCP_NODELAY));
+
+ Mockito.verifyZeroInteractions(keyAccess);
+ Mockito.verify(keyAccessFactory).getKeyAccess(channel);
+ Mockito.verify(channel).supportedOptions();
+ }
+
+ @Test
+ public void testGetOption() throws IOException {
+ final MD5ChannelOptions opts = MD5ChannelOptions.create(keyAccessFactory, channel);
+
+ assertNull(opts.getOption(MD5SocketOptions.TCP_MD5SIG));
+ assertFalse(opts.getOption(StandardSocketOptions.TCP_NODELAY));
+
+ Mockito.verify(keyAccess).getKey();
+ Mockito.verify(channel).getOption(StandardSocketOptions.TCP_NODELAY);
+ }
+
+ @Test
+ public void testSetOption() throws IOException {
+ final MD5ChannelOptions opts = MD5ChannelOptions.create(keyAccessFactory, channel);
+
+ opts.setOption(MD5SocketOptions.TCP_MD5SIG, new byte[] { 1, });
+ opts.setOption(StandardSocketOptions.TCP_NODELAY, true);
+
+ Mockito.verify(keyAccess).setKey(any(byte[].class));
+ Mockito.verify(channel).setOption(StandardSocketOptions.TCP_NODELAY, true);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.bgpcep.tcpmd5.nio;
+
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.mockito.Matchers.any;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketOption;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.bgpcep.tcpmd5.KeyAccess;
+import org.opendaylight.bgpcep.tcpmd5.KeyAccessFactory;
+import org.opendaylight.bgpcep.tcpmd5.MD5SocketOptions;
+
+public class MD5SocketChannelTest {
+ private static final byte[] KEY1 = new byte[] { 1 };
+
+ @Mock
+ private KeyAccessFactory keyAccessFactory;
+ @Mock
+ private KeyAccess keyAccess;
+
+ // FIXME: can we mock this?
+ private SocketChannel channel;
+
+ @Before
+ public void setup() throws IOException {
+ MockitoAnnotations.initMocks(this);
+
+ channel = new SocketChannel(null) {
+
+ @Override
+ public SocketAddress getLocalAddress() throws IOException {
+ return null;
+ }
+
+ @Override
+ public <T> T getOption(final SocketOption<T> name) throws IOException {
+ return null;
+ }
+
+ @Override
+ public Set<SocketOption<?>> supportedOptions() {
+ return null;
+ }
+
+ @Override
+ public SocketChannel bind(final SocketAddress local) throws IOException {
+ return null;
+ }
+
+ @Override
+ public <T> SocketChannel setOption(final SocketOption<T> name, final T value)
+ throws IOException {
+ return null;
+ }
+
+ @Override
+ public SocketChannel shutdownInput() throws IOException {
+ return null;
+ }
+
+ @Override
+ public SocketChannel shutdownOutput() throws IOException {
+ return null;
+ }
+
+ @Override
+ public Socket socket() {
+ return null;
+ }
+
+ @Override
+ public boolean isConnected() {
+ return false;
+ }
+
+ @Override
+ public boolean isConnectionPending() {
+ return false;
+ }
+
+ @Override
+ public boolean connect(final SocketAddress remote) throws IOException {
+ return false;
+ }
+
+ @Override
+ public boolean finishConnect() throws IOException {
+ return false;
+ }
+
+ @Override
+ public SocketAddress getRemoteAddress() throws IOException {
+ return null;
+ }
+
+ @Override
+ public int read(final ByteBuffer dst) throws IOException {
+ return 0;
+ }
+
+ @Override
+ public long read(final ByteBuffer[] dsts, final int offset, final int length)
+ throws IOException {
+ return 0;
+ }
+
+ @Override
+ public int write(final ByteBuffer src) throws IOException {
+ return 0;
+ }
+
+ @Override
+ public long write(final ByteBuffer[] srcs, final int offset, final int length)
+ throws IOException {
+ return 0;
+ }
+
+ @Override
+ protected void implCloseSelectableChannel() throws IOException {
+ }
+
+ @Override
+ protected void implConfigureBlocking(final boolean block)
+ throws IOException {
+ }
+ };
+
+ Mockito.doReturn(keyAccess).when(keyAccessFactory).getKeyAccess(channel);
+ Mockito.doReturn(null).when(keyAccess).getKey();
+ Mockito.doNothing().when(keyAccess).setKey(any(byte[].class));
+ }
+
+ @Test
+ public void testCreate() throws IOException {
+ try (final MD5SocketChannel sc = new MD5SocketChannel(channel, keyAccessFactory)) {
+
+ }
+
+ Mockito.verify(keyAccessFactory).getKeyAccess(channel);
+ }
+
+ @Test
+ public void testGetKey() throws IOException {
+ try (final MD5SocketChannel sc = new MD5SocketChannel(channel, keyAccessFactory)) {
+
+ assertNull(sc.getOption(MD5SocketOptions.TCP_MD5SIG));
+ }
+
+ Mockito.verify(keyAccessFactory).getKeyAccess(channel);
+ Mockito.verify(keyAccess).getKey();
+ }
+
+ @Test
+ public void testSetKey() throws IOException {
+ try (final MD5SocketChannel sc = new MD5SocketChannel(channel, keyAccessFactory)) {
+ assertSame(sc, sc.setOption(MD5SocketOptions.TCP_MD5SIG, KEY1));
+ }
+
+ Mockito.verify(keyAccessFactory).getKeyAccess(channel);
+ Mockito.verify(keyAccess).setKey(KEY1);
+ }
+}