You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2015/05/18 15:30:06 UTC
mina-sshd git commit: [SSHD-465] Add default accept/reject-all
implementations for PasswordAuthenticator,
PublickeyAuthenticator and ServerKeyVerifier
Repository: mina-sshd
Updated Branches:
refs/heads/master 60dac4dde -> 690082009
[SSHD-465] Add default accept/reject-all implementations for PasswordAuthenticator, PublickeyAuthenticator and ServerKeyVerifier
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/69008200
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/69008200
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/69008200
Branch: refs/heads/master
Commit: 690082009105e12a595e11985de6c3fa3a3e224f
Parents: 60dac4d
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Mon May 18 16:29:53 2015 +0300
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Mon May 18 16:29:53 2015 +0300
----------------------------------------------------------------------
.../keyverifier/AcceptAllServerKeyVerifier.java | 20 +----
.../keyverifier/RejectAllServerKeyVerifier.java | 31 ++++++++
.../keyverifier/StaticServerKeyVerifier.java | 60 +++++++++++++++
.../sshd/client/session/ClientSessionImpl.java | 32 +++++++-
.../org/apache/sshd/common/util/KeyUtils.java | 10 ++-
.../sshd/server/PasswordAuthenticator.java | 46 ++++++++++++
.../sshd/server/PublickeyAuthenticator.java | 48 ++++++++++++
.../StaticServerKeyVerifierTest.java | 79 ++++++++++++++++++++
.../sshd/server/PasswordAuthenticatorTest.java | 69 +++++++++++++++++
.../sshd/server/PublickeyAuthenticatorTest.java | 77 +++++++++++++++++++
.../org/apache/sshd/util/BaseTestSupport.java | 4 +
11 files changed, 455 insertions(+), 21 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/AcceptAllServerKeyVerifier.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/AcceptAllServerKeyVerifier.java b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/AcceptAllServerKeyVerifier.java
index 5732191..d824a2a 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/AcceptAllServerKeyVerifier.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/AcceptAllServerKeyVerifier.java
@@ -18,30 +18,16 @@
*/
package org.apache.sshd.client.keyverifier;
-import java.net.SocketAddress;
-import java.security.PublicKey;
-
-import org.apache.sshd.ClientSession;
-import org.apache.sshd.client.ServerKeyVerifier;
-import org.apache.sshd.common.util.AbstractLoggingBean;
-import org.apache.sshd.common.util.KeyUtils;
/**
* A ServerKeyVerifier that accepts all server keys.
*
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
*/
-public class AcceptAllServerKeyVerifier extends AbstractLoggingBean implements ServerKeyVerifier {
- public static final ServerKeyVerifier INSTANCE = new AcceptAllServerKeyVerifier();
+public final class AcceptAllServerKeyVerifier extends StaticServerKeyVerifier {
+ public static final AcceptAllServerKeyVerifier INSTANCE = new AcceptAllServerKeyVerifier();
private AcceptAllServerKeyVerifier() {
- super();
- }
-
- @Override
- public boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) {
- log.warn("Server at {} presented unverified {} key: {}",
- new Object[] { remoteAddress, serverKey.getAlgorithm(), KeyUtils.getFingerPrint(serverKey) });
- return true;
+ super(true);
}
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/RejectAllServerKeyVerifier.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/RejectAllServerKeyVerifier.java b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/RejectAllServerKeyVerifier.java
new file mode 100644
index 0000000..9e6c2fa
--- /dev/null
+++ b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/RejectAllServerKeyVerifier.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.sshd.client.keyverifier;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public final class RejectAllServerKeyVerifier extends StaticServerKeyVerifier {
+ public static final RejectAllServerKeyVerifier INSTANCE = new RejectAllServerKeyVerifier();
+
+ private RejectAllServerKeyVerifier() {
+ super(false);
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifier.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifier.java b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifier.java
new file mode 100644
index 0000000..f0f8aa4
--- /dev/null
+++ b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifier.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.sshd.client.keyverifier;
+
+import java.net.SocketAddress;
+import java.security.PublicKey;
+
+import org.apache.sshd.ClientSession;
+import org.apache.sshd.client.ServerKeyVerifier;
+import org.apache.sshd.common.util.AbstractLoggingBean;
+import org.apache.sshd.common.util.KeyUtils;
+
+/**
+ * Returns the same constant answer {@code true/false} regardless
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public abstract class StaticServerKeyVerifier extends AbstractLoggingBean implements ServerKeyVerifier {
+ private final boolean acceptance;
+
+ protected StaticServerKeyVerifier(boolean acceptance) {
+ this.acceptance = acceptance;
+ }
+
+ public final boolean isAccepted() {
+ return acceptance;
+ }
+
+ @Override
+ public final boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) {
+ if (isAccepted()) {
+ log.warn("Server at {} presented unverified {} key: {}",
+ new Object[] { remoteAddress, (serverKey == null) ? null : serverKey.getAlgorithm(), KeyUtils.getFingerPrint(serverKey) });
+ return true;
+ } else {
+ if (log.isDebugEnabled()) {
+ log.debug("Reject server {} unverified {} key: {}",
+ new Object[] { remoteAddress, (serverKey == null) ? null : serverKey.getAlgorithm(), KeyUtils.getFingerPrint(serverKey) });
+ }
+
+ return false;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
index 195be0c..beac954 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
@@ -44,7 +44,6 @@ import org.apache.sshd.client.scp.DefaultScpClient;
import org.apache.sshd.client.sftp.DefaultSftpClient;
import org.apache.sshd.client.sftp.SftpFileSystem;
import org.apache.sshd.client.sftp.SftpFileSystemProvider;
-import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.NamedResource;
import org.apache.sshd.common.Service;
import org.apache.sshd.common.ServiceFactory;
@@ -110,6 +109,7 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
sendKexInit();
}
+ @Override
protected Service[] getServices() {
Service[] services;
if (nextService != null) {
@@ -122,26 +122,32 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
return services;
}
+ @Override
public ClientFactoryManager getFactoryManager() {
return (ClientFactoryManager) factoryManager;
}
+ @Override
public void addPasswordIdentity(String password) {
identities.add(password);
}
+ @Override
public void addPublicKeyIdentity(KeyPair key) {
identities.add(key);
}
+ @Override
public UserInteraction getUserInteraction() {
return userInteraction;
}
+ @Override
public void setUserInteraction(UserInteraction userInteraction) {
this.userInteraction = userInteraction;
}
+ @Override
public AuthFuture auth() throws IOException {
if (username == null) {
throw new IllegalStateException("No username specified when the session was created");
@@ -196,10 +202,12 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
}
}
+ @Override
public ClientChannel createChannel(String type) throws IOException {
return createChannel(type, null);
}
+ @Override
public ClientChannel createChannel(String type, String subType) throws IOException {
if (ClientChannel.CHANNEL_SHELL.equals(type)) {
return createShellChannel();
@@ -212,6 +220,7 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
}
}
+ @Override
public ChannelShell createShellChannel() throws IOException {
if (inCipher instanceof CipherNone || outCipher instanceof CipherNone) {
throw new IllegalStateException("Interactive channels are not supported with none cipher");
@@ -221,18 +230,21 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
return channel;
}
+ @Override
public ChannelExec createExecChannel(String command) throws IOException {
ChannelExec channel = new ChannelExec(command);
getConnectionService().registerChannel(channel);
return channel;
}
+ @Override
public ChannelSubsystem createSubsystemChannel(String subsystem) throws IOException {
ChannelSubsystem channel = new ChannelSubsystem(subsystem);
getConnectionService().registerChannel(channel);
return channel;
}
+ @Override
public ChannelDirectTcpip createDirectTcpipChannel(SshdSocketAddress local, SshdSocketAddress remote) throws IOException {
ChannelDirectTcpip channel = new ChannelDirectTcpip(local, remote);
getConnectionService().registerChannel(channel);
@@ -247,60 +259,74 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
return getService(ConnectionService.class);
}
+ @Override
public ScpTransferEventListener getScpTransferEventListener() {
return scpListener;
}
+ @Override
public void setScpTransferEventListener(ScpTransferEventListener listener) {
scpListener = listener;
}
+ @Override
public ScpClient createScpClient() {
return createScpClient(getScpTransferEventListener());
}
+ @Override
public ScpClient createScpClient(ScpTransferEventListener listener) {
return new DefaultScpClient(this, listener);
}
+ @Override
public SftpClient createSftpClient() throws IOException {
return new DefaultSftpClient(this);
}
+ @Override
public FileSystem createSftpFileSystem() throws IOException {
return new SftpFileSystem(new SftpFileSystemProvider((org.apache.sshd.SshClient) factoryManager), this);
}
+ @Override
public SshdSocketAddress startLocalPortForwarding(SshdSocketAddress local, SshdSocketAddress remote) throws IOException {
return getConnectionService().getTcpipForwarder().startLocalPortForwarding(local, remote);
}
+ @Override
public void stopLocalPortForwarding(SshdSocketAddress local) throws IOException {
getConnectionService().getTcpipForwarder().stopLocalPortForwarding(local);
}
+ @Override
public SshdSocketAddress startRemotePortForwarding(SshdSocketAddress remote, SshdSocketAddress local) throws IOException {
return getConnectionService().getTcpipForwarder().startRemotePortForwarding(remote, local);
}
+ @Override
public void stopRemotePortForwarding(SshdSocketAddress remote) throws IOException {
getConnectionService().getTcpipForwarder().stopRemotePortForwarding(remote);
}
+ @Override
public SshdSocketAddress startDynamicPortForwarding(SshdSocketAddress local) throws IOException {
return getConnectionService().getTcpipForwarder().startDynamicPortForwarding(local);
}
+ @Override
public void stopDynamicPortForwarding(SshdSocketAddress local) throws IOException {
getConnectionService().getTcpipForwarder().stopDynamicPortForwarding(local);
}
+ @Override
protected void handleMessage(Buffer buffer) throws Exception {
synchronized (lock) {
super.handleMessage(buffer);
}
}
+ @Override
public int waitFor(int mask, long timeout) {
long t = 0;
synchronized (lock) {
@@ -342,6 +368,7 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
}
}
+ @Override
protected boolean readIdentification(Buffer buffer) throws IOException {
serverVersion = doReadIdentification(buffer, false);
if (serverVersion == null) {
@@ -360,12 +387,14 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
sendIdentification(clientVersion);
}
+ @Override
protected void sendKexInit() throws IOException {
String algs = NamedResource.Utils.getNames(getFactoryManager().getSignatureFactories());
clientProposal = createProposal(algs);
I_C = sendKexInit(clientProposal);
}
+ @Override
protected void receiveKexInit(Buffer buffer) throws IOException {
serverProposal = new String[SshConstants.PROPOSAL_MAX];
I_S = receiveKexInit(buffer, serverProposal);
@@ -413,6 +442,7 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
throw new IllegalStateException("Starting services is not supported on the client side");
}
+ @Override
public Map<Object, Object> getMetadataMap() {
return metadataMap;
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/main/java/org/apache/sshd/common/util/KeyUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/KeyUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/KeyUtils.java
index d86c024..0b0d9ba 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/KeyUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/KeyUtils.java
@@ -44,10 +44,14 @@ public class KeyUtils {
/**
* Retrieve the public key fingerprint
*
- * @param key the public key
- * @return the fingerprint
+ * @param key the public key - ignored if {@code null}
+ * @return the fingerprint or {@code null} if no key
*/
public static String getFingerPrint(PublicKey key) {
+ if (key == null) {
+ return null;
+ }
+
try {
Buffer buffer = new ByteArrayBuffer();
buffer.putRawPublicKey(key);
@@ -56,7 +60,7 @@ public class KeyUtils {
md5.update(buffer.array(), 0, buffer.wpos());
byte[] data = md5.digest();
return BufferUtils.printHex(data, 0, data.length, ':');
- } catch (Exception e) {
+ } catch(Exception e) {
return "Unable to compute fingerprint";
}
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java b/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java
index f2b3730..f1fd0b5 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/PasswordAuthenticator.java
@@ -18,6 +18,7 @@
*/
package org.apache.sshd.server;
+import org.apache.sshd.common.util.AbstractLoggingBean;
import org.apache.sshd.server.session.ServerSession;
/**
@@ -39,4 +40,49 @@ public interface PasswordAuthenticator {
*/
boolean authenticate(String username, String password, ServerSession session);
+ /**
+ * Returns the same constant result {@code true/false} regardless
+ */
+ public static abstract class StaticPasswordAuthenticator extends AbstractLoggingBean implements PasswordAuthenticator {
+ private final boolean acceptance;
+
+ protected StaticPasswordAuthenticator(boolean acceptance) {
+ this.acceptance = acceptance;
+ }
+
+ public final boolean isAccepted() {
+ return acceptance;
+ }
+
+ @Override
+ public final boolean authenticate(String username, String password, ServerSession session) {
+ if (log.isDebugEnabled()) {
+ log.debug("authenticate({}[{}]: {}", username, session, Boolean.valueOf(isAccepted()));
+ }
+
+ return isAccepted();
+ }
+ }
+
+ /**
+ * Accepts all authentication attempts
+ */
+ public static final class AcceptAllPasswordAuthenticator extends StaticPasswordAuthenticator {
+ public static final AcceptAllPasswordAuthenticator INSTANCE = new AcceptAllPasswordAuthenticator();
+
+ private AcceptAllPasswordAuthenticator() {
+ super(true);
+ }
+ }
+
+ /**
+ * Rejects all authentication attempts
+ */
+ public static final class RejectAllPasswordAuthenticator extends StaticPasswordAuthenticator {
+ public static final RejectAllPasswordAuthenticator INSTANCE = new RejectAllPasswordAuthenticator();
+
+ private RejectAllPasswordAuthenticator() {
+ super(false);
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/main/java/org/apache/sshd/server/PublickeyAuthenticator.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/PublickeyAuthenticator.java b/sshd-core/src/main/java/org/apache/sshd/server/PublickeyAuthenticator.java
index e448c4a..276087e 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/PublickeyAuthenticator.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/PublickeyAuthenticator.java
@@ -20,6 +20,8 @@ package org.apache.sshd.server;
import java.security.PublicKey;
+import org.apache.sshd.common.util.AbstractLoggingBean;
+import org.apache.sshd.common.util.KeyUtils;
import org.apache.sshd.server.session.ServerSession;
/**
@@ -40,4 +42,50 @@ public interface PublickeyAuthenticator {
*/
boolean authenticate(String username, PublicKey key, ServerSession session);
+ /**
+ * Returns the same constant result {@code true/false} regardless
+ */
+ public static abstract class StaticPublickeyAuthenticator extends AbstractLoggingBean implements PublickeyAuthenticator {
+ private final boolean acceptance;
+
+ protected StaticPublickeyAuthenticator(boolean acceptance) {
+ this.acceptance = acceptance;
+ }
+
+ public final boolean isAccepted() {
+ return acceptance;
+ }
+
+ @Override
+ public final boolean authenticate(String username, PublicKey key, ServerSession session) {
+ if (log.isDebugEnabled()) {
+ log.debug("authenticate({}[{}][[]]: {}",
+ new Object[] { username, session, key.getAlgorithm(), KeyUtils.getFingerPrint(key), Boolean.valueOf(isAccepted()) });
+ }
+
+ return isAccepted();
+ }
+ }
+
+ /**
+ * Accepts all authentication attempts
+ */
+ public static final class AcceptAllPublickeyAuthenticator extends StaticPublickeyAuthenticator {
+ public static final AcceptAllPublickeyAuthenticator INSTANCE = new AcceptAllPublickeyAuthenticator();
+
+ private AcceptAllPublickeyAuthenticator() {
+ super(true);
+ }
+ }
+
+ /**
+ * Rejects all authentication attempts
+ */
+ public static final class RejectAllPublickeyAuthenticator extends StaticPublickeyAuthenticator {
+ public static final RejectAllPublickeyAuthenticator INSTANCE = new RejectAllPublickeyAuthenticator();
+
+ private RejectAllPublickeyAuthenticator() {
+ super(false);
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifierTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifierTest.java b/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifierTest.java
new file mode 100644
index 0000000..aadd33b
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifierTest.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.sshd.client.keyverifier;
+
+import java.lang.reflect.Method;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.security.PublicKey;
+import java.util.Arrays;
+import java.util.Random;
+
+import org.apache.sshd.ClientSession;
+import org.apache.sshd.client.ServerKeyVerifier;
+import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.util.BaseTestSupport;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class StaticServerKeyVerifierTest extends BaseTestSupport {
+ public StaticServerKeyVerifierTest() {
+ super();
+ }
+
+ @Test
+ public void testAcceptAllServerKeyVerifier() throws Exception {
+ testStaticServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE);
+ }
+
+ @Test
+ public void testRejectAllServerKeyVerifier() throws Exception {
+ testStaticServerKeyVerifier(RejectAllServerKeyVerifier.INSTANCE);
+ }
+
+ private void testStaticServerKeyVerifier(StaticServerKeyVerifier authenticator) throws Exception {
+ Method method = ServerKeyVerifier.class.getMethod("verifyServerKey", ClientSession.class, SocketAddress.class, PublicKey.class);
+ PublicKey key = Mockito.mock(PublicKey.class);
+ Mockito.when(key.getAlgorithm()).thenReturn(getCurrentTestName());
+ Mockito.when(key.getEncoded()).thenReturn(GenericUtils.EMPTY_BYTE_ARRAY);
+ Mockito.when(key.getFormat()).thenReturn(getCurrentTestName());
+
+ Object[] args = { Mockito.mock(ClientSession.class), new InetSocketAddress("localhost", 7365), key };
+ Object[] invArgs = new Object[args.length];
+ Random rnd = new Random(System.nanoTime());
+ boolean expected = authenticator.isAccepted();
+ for (int index=0; index < Long.SIZE; index++) {
+ for (int j=0; j < args.length; j++) {
+ if (rnd.nextBoolean()) {
+ invArgs[j] = args[j];
+ } else {
+ invArgs[j] = null;
+ }
+ }
+
+ Object result = method.invoke(authenticator, invArgs);
+ assertTrue("No boolean result", result instanceof Boolean);
+ assertEquals("Mismatched result for " + Arrays.toString(invArgs), expected, ((Boolean) result).booleanValue());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/test/java/org/apache/sshd/server/PasswordAuthenticatorTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/PasswordAuthenticatorTest.java b/sshd-core/src/test/java/org/apache/sshd/server/PasswordAuthenticatorTest.java
new file mode 100644
index 0000000..053d8d1
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/server/PasswordAuthenticatorTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.sshd.server;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.Random;
+
+import org.apache.sshd.server.PasswordAuthenticator.StaticPasswordAuthenticator;
+import org.apache.sshd.server.session.ServerSession;
+import org.apache.sshd.util.BaseTestSupport;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class PasswordAuthenticatorTest extends BaseTestSupport {
+ public PasswordAuthenticatorTest() {
+ super();
+ }
+
+ @Test
+ public void testAcceptAllPasswordAuthenticator() throws Exception {
+ testStaticPasswordAuthenticator(PasswordAuthenticator.AcceptAllPasswordAuthenticator.INSTANCE);
+ }
+
+ @Test
+ public void testRejectAllPasswordAuthenticator() throws Exception {
+ testStaticPasswordAuthenticator(PasswordAuthenticator.RejectAllPasswordAuthenticator.INSTANCE);
+ }
+
+ private void testStaticPasswordAuthenticator(StaticPasswordAuthenticator authenticator) throws Exception {
+ Method method = PasswordAuthenticator.class.getMethod("authenticate", String.class, String.class, ServerSession.class);
+ Object[] args = { getCurrentTestName(), getClass().getName(), null /* ServerSession */ };
+ Object[] invArgs = new Object[args.length];
+ Random rnd = new Random(System.nanoTime());
+ boolean expected = authenticator.isAccepted();
+ for (int index=0; index < Long.SIZE; index++) {
+ for (int j=0; j < args.length; j++) {
+ if (rnd.nextBoolean()) {
+ invArgs[j] = args[j];
+ } else {
+ invArgs[j] = null;
+ }
+ }
+
+ Object result = method.invoke(authenticator, invArgs);
+ assertTrue("No boolean result", result instanceof Boolean);
+ assertEquals("Mismatched result for " + Arrays.toString(invArgs), expected, ((Boolean) result).booleanValue());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/test/java/org/apache/sshd/server/PublickeyAuthenticatorTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/PublickeyAuthenticatorTest.java b/sshd-core/src/test/java/org/apache/sshd/server/PublickeyAuthenticatorTest.java
new file mode 100644
index 0000000..71eddb0
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/server/PublickeyAuthenticatorTest.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.sshd.server;
+
+import java.lang.reflect.Method;
+import java.security.PublicKey;
+import java.util.Arrays;
+import java.util.Random;
+
+import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.server.PublickeyAuthenticator.StaticPublickeyAuthenticator;
+import org.apache.sshd.server.session.ServerSession;
+import org.apache.sshd.util.BaseTestSupport;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class PublickeyAuthenticatorTest extends BaseTestSupport {
+ public PublickeyAuthenticatorTest() {
+ super();
+ }
+
+ @Test
+ public void testAcceptAllPublickeyAuthenticator() throws Exception {
+ testStaticPublickeyAuthenticator(PublickeyAuthenticator.AcceptAllPublickeyAuthenticator.INSTANCE);
+ }
+
+ @Test
+ public void testRejectAllPublickeyAuthenticator() throws Exception {
+ testStaticPublickeyAuthenticator(PublickeyAuthenticator.RejectAllPublickeyAuthenticator.INSTANCE);
+ }
+
+ private void testStaticPublickeyAuthenticator(StaticPublickeyAuthenticator authenticator) throws Exception {
+ Method method = PublickeyAuthenticator.class.getMethod("authenticate", String.class, PublicKey.class, ServerSession.class);
+ PublicKey key = Mockito.mock(PublicKey.class);
+ Mockito.when(key.getAlgorithm()).thenReturn(getCurrentTestName());
+ Mockito.when(key.getEncoded()).thenReturn(GenericUtils.EMPTY_BYTE_ARRAY);
+ Mockito.when(key.getFormat()).thenReturn(getCurrentTestName());
+
+ Object[] args = { getCurrentTestName(), key, null /* ServerSession */ };
+ Object[] invArgs = new Object[args.length];
+ Random rnd = new Random(System.nanoTime());
+ boolean expected = authenticator.isAccepted();
+ for (int index=0; index < Long.SIZE; index++) {
+ for (int j=0; j < args.length; j++) {
+ if (rnd.nextBoolean()) {
+ invArgs[j] = args[j];
+ } else {
+ invArgs[j] = null;
+ }
+ }
+
+ Object result = method.invoke(authenticator, invArgs);
+ assertTrue("No boolean result", result instanceof Boolean);
+ assertEquals("Mismatched result for " + Arrays.toString(invArgs), expected, ((Boolean) result).booleanValue());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/69008200/sshd-core/src/test/java/org/apache/sshd/util/BaseTestSupport.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/util/BaseTestSupport.java b/sshd-core/src/test/java/org/apache/sshd/util/BaseTestSupport.java
index af37b07..d81b1af 100644
--- a/sshd-core/src/test/java/org/apache/sshd/util/BaseTestSupport.java
+++ b/sshd-core/src/test/java/org/apache/sshd/util/BaseTestSupport.java
@@ -99,6 +99,10 @@ public abstract class BaseTestSupport extends Assert {
/* ----------------------- Useful extra assertions --------------------- */
+ public static void assertEquals(String message, boolean expected, boolean actual) {
+ assertEquals(message, Boolean.valueOf(expected), Boolean.valueOf(actual));
+ }
+
public static File assertHierarchyTargetFolderExists(File folder) {
if (!folder.exists()) {
assertTrue("Failed to create hierarchy of " + folder.getAbsolutePath(), folder.mkdirs());