mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lgoldst...@apache.org
Subject [1/2] mina-sshd git commit: [SSHD-455] Allow users to register "extensions" to some of the builtin factories
Date Thu, 07 May 2015 11:59:00 GMT
Repository: mina-sshd
Updated Branches:
  refs/heads/master cca7cc743 -> 7ccbd0663


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
index 345916a..ebc600a 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
@@ -22,12 +22,15 @@ package org.apache.sshd.common.util;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.EnumSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -220,4 +223,41 @@ public class GenericUtils {
 
         return result;
     }
+
+    @SuppressWarnings("rawtypes")
+    private static final Comparator<Comparable> naturalOrderComparator=new Comparator<Comparable>()
{
+        // TODO for JDK-8 use Comparator.naturalOrder() 
+        @Override
+        @SuppressWarnings("unchecked")
+        public int compare(Comparable c1, Comparable c2) {
+            return c1.compareTo(c2);
+        }
+    };
+
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    public static final <V extends Comparable<V>> Comparator<V> naturalComparator()
{
+        // TODO for JDK-8 use Comparator.naturalOrder() 
+        return (Comparator) naturalOrderComparator;
+    }
+
+    public static final <V extends Comparable<V>> SortedSet<V> asSortedSet(Collection<?
extends V> values) {
+        // TODO for JDK-8 use Comparator.naturalOrder() 
+        return asSortedSet(GenericUtils.<V>naturalComparator(), values);
+    }
+
+    /**
+     * @param comp The (non-{@code null}) {@link Comparator} to use
+     * @param values The values to be added (ignored if {@code null))
+     * @return A {@link SortedSet} containing the values (if any) sorted
+     * using the provided comparator
+     */
+    public static final <V> SortedSet<V> asSortedSet(Comparator<? super V>
comp, Collection<? extends V> values) {
+        // TODO for JDK-8 return Collections.emptySortedSet()
+        SortedSet<V>    set=new TreeSet<V>(ValidateUtils.checkNotNull(comp, "No
comparator", EMPTY_OBJECT_ARRAY));
+        if (size(values) > 0) {
+            set.addAll(values);
+        }
+        
+        return set;
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/LoadTest.java b/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
index ca07844..cc156b7 100644
--- a/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
@@ -90,6 +90,7 @@ public class LoadTest extends BaseTest {
         final CountDownLatch latch = new CountDownLatch(nbThreads);
         for (int i = 0; i < nbThreads; i++) {
             Runnable r = new Runnable() {
+                @Override
                 public void run() {
                     try {
                         for (int i = 0; i < nbSessionsPerThread; i++) {
@@ -116,7 +117,7 @@ public class LoadTest extends BaseTest {
             FactoryManagerUtils.updateProperty(props, FactoryManager.MAX_PACKET_SIZE, 1024
* 16);
             FactoryManagerUtils.updateProperty(props, FactoryManager.WINDOW_SIZE, 1024 *
8);
             client.setKeyExchangeFactories(Arrays.asList(
-                    SshBuilder.ClientBuilder.getKeyExchangeFactory(BuiltinDHFactories.dhg1)));
+                    SshBuilder.ClientBuilder.DH2KEX.transform(BuiltinDHFactories.dhg1)));
             client.setCipherFactories(Arrays.<NamedFactory<Cipher>>asList(BuiltinCiphers.blowfishcbc));
             client.start();
             try {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java b/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java
index 848dd20..e9ab522 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java
@@ -93,7 +93,7 @@ public class KexTest extends BaseTest {
     }
 
     private void testClient(DHFactory factory) throws Exception {
-        testClient(SshBuilder.ClientBuilder.getKeyExchangeFactory(factory));
+        testClient(SshBuilder.ClientBuilder.DH2KEX.transform(factory));
     }
 
     private void testClient(NamedFactory<KeyExchange> kex) throws Exception {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/test/java/org/apache/sshd/common/cipher/BaseCipherTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/cipher/BaseCipherTest.java b/sshd-core/src/test/java/org/apache/sshd/common/cipher/BaseCipherTest.java
index 9abcbf7..b0814fb 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/cipher/BaseCipherTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/cipher/BaseCipherTest.java
@@ -42,7 +42,8 @@ public abstract class BaseCipherTest extends BaseTest {
 		super();
 	}
 
-	protected void ensureKeySizeSupported(int bsize, String algorithm, String transformation)
throws GeneralSecurityException {
+	@SuppressWarnings("deprecation")
+    protected void ensureKeySizeSupported(int bsize, String algorithm, String transformation)
throws GeneralSecurityException {
 		try {
 	        javax.crypto.Cipher	cipher=SecurityUtils.getCipher(transformation);
 	        byte[]				key=new byte[bsize];

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
b/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
index 4a70140..1b46453 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/cipher/BuiltinCiphersTest.java
@@ -36,6 +36,7 @@ import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.util.BaseTest;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -150,4 +151,72 @@ public class BuiltinCiphersTest extends BaseTest {
             assertListEquals(fullList + "[unsupported]", unknown, missing);
         }
     }
+
+    @Test
+    public void testResolveFactoryOnBuiltinValues() {
+        for (NamedFactory<Cipher> expected : BuiltinCiphers.VALUES) {
+            String                  name=expected.getName();
+            NamedFactory<Cipher>    actual=BuiltinCiphers.resolveFactory(name);
+            Assert.assertSame(name, expected, actual);
+        }
+    }
+
+    @Test
+    public void testNotAllowedToRegisterBuiltinFactories() {
+        for (CipherFactory expected : BuiltinCiphers.VALUES) {
+            try {
+                BuiltinCiphers.registerExtension(expected);
+                Assert.fail("Unexpected sucess for " + expected.getName());
+            } catch(IllegalArgumentException e) {
+                // expected - ignored
+            }
+        }
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testNotAllowedToOverrideRegisteredFactories() {
+        CipherFactory    expected=Mockito.mock(CipherFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            for (int index=1; index <= Byte.SIZE; index++) {
+                BuiltinCiphers.registerExtension(expected);
+                Assert.assertEquals("Unexpected success at attempt #" + index, 1, index);
+            }
+        } finally {
+            BuiltinCiphers.unregisterExtension(name);
+        }
+    }
+
+    @Test
+    public void testResolveFactoryOnRegisteredExtension() {
+        CipherFactory    expected=Mockito.mock(CipherFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            Assert.assertNull("Extension already registered", BuiltinCiphers.resolveFactory(name));
+            BuiltinCiphers.registerExtension(expected);
+
+            NamedFactory<Cipher>    actual=BuiltinCiphers.resolveFactory(name);
+            Assert.assertSame("Mismatched resolved instance", expected, actual);
+        } finally {
+            NamedFactory<Cipher>    actual=BuiltinCiphers.unregisterExtension(name);
+            Assert.assertSame("Mismatched unregistered instance", expected, actual);
+            Assert.assertNull("Extension not un-registered", BuiltinCiphers.resolveFactory(name));
+        }
+    }
+
+    @Test
+    public void testFac2NamedTransformer() {
+        Assert.assertNull("Invalid null transformation", CipherFactory.FAC2NAMED.transform(null));
+        for (CipherFactory expected : BuiltinCiphers.VALUES) {
+            NamedFactory<Cipher>   actual=CipherFactory.FAC2NAMED.transform(expected);
+            Assert.assertSame("Mismatched transformed instance for " + expected.getName(),
expected, actual);
+        }
+        
+        CipherFactory   mock=Mockito.mock(CipherFactory.class);
+        Assert.assertSame("Mismatched transformed mocked instance", mock, CipherFactory.FAC2NAMED.transform(mock));
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/test/java/org/apache/sshd/common/compression/BuiltinCompressionsTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/compression/BuiltinCompressionsTest.java
b/sshd-core/src/test/java/org/apache/sshd/common/compression/BuiltinCompressionsTest.java
index 7f9528c..601b28f 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/compression/BuiltinCompressionsTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/compression/BuiltinCompressionsTest.java
@@ -20,12 +20,22 @@
 package org.apache.sshd.common.compression;
 
 import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.EnumSet;
+import java.util.List;
+import java.util.Random;
 import java.util.Set;
 
+import org.apache.sshd.common.NamedFactory;
+import org.apache.sshd.common.NamedResource;
+import org.apache.sshd.common.compression.BuiltinCompressions.ParseResult;
+import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.util.BaseTest;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -60,4 +70,108 @@ public class BuiltinCompressionsTest extends BaseTest {
         
         Assert.assertEquals("Incomplete coverage", BuiltinCompressions.VALUES, avail);
     }
+
+    @Test
+    public void testParseCompressionsList() {
+        List<String>    builtin=NamedResource.Utils.getNameList(BuiltinCompressions.VALUES);
+        List<String>    unknown=Arrays.asList(getClass().getPackage().getName(), getClass().getSimpleName(),
getCurrentTestName());
+        Random          rnd=new Random();
+        for (int index=0; index < (builtin.size() + unknown.size()); index++) {
+            Collections.shuffle(builtin, rnd);
+            Collections.shuffle(unknown, rnd);
+            
+            List<String>    weavedList=new ArrayList<String>(builtin.size() +
unknown.size());
+            for (int bIndex=0, uIndex=0; (bIndex < builtin.size()) || (uIndex < unknown.size());
) {
+                boolean useBuiltin=false;
+                if (bIndex < builtin.size()) {
+                    useBuiltin = (uIndex < unknown.size()) ? rnd.nextBoolean() : true;
+                }
+
+                if (useBuiltin) {
+                    weavedList.add(builtin.get(bIndex));
+                    bIndex++;
+                } else if (uIndex < unknown.size()){
+                    weavedList.add(unknown.get(uIndex));
+                    uIndex++;
+                }
+            }
+
+            String          fullList=GenericUtils.join(weavedList, ',');
+            ParseResult     result=BuiltinCompressions.parseCompressionsList(fullList);
+            List<String>    parsed=NamedResource.Utils.getNameList(result.getParsedFactories());
+            List<String>    missing=result.getUnsupportedFactories();
+            
+            // makes sure not only that the contents are the same but also the order
+            assertListEquals(fullList + "[parsed]", builtin, parsed);
+            assertListEquals(fullList + "[unsupported]", unknown, missing);
+        }
+    }
+
+    @Test
+    public void testResolveFactoryOnBuiltinValues() {
+        for (NamedFactory<Compression> expected : BuiltinCompressions.VALUES) {
+            String                  name=expected.getName();
+            NamedFactory<Compression>    actual=BuiltinCompressions.resolveFactory(name);
+            Assert.assertSame(name, expected, actual);
+        }
+    }
+
+    @Test
+    public void testNotAllowedToRegisterBuiltinFactories() {
+        for (CompressionFactory expected : BuiltinCompressions.VALUES) {
+            try {
+                BuiltinCompressions.registerExtension(expected);
+                Assert.fail("Unexpected sucess for " + expected.getName());
+            } catch(IllegalArgumentException e) {
+                // expected - ignored
+            }
+        }
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testNotAllowedToOverrideRegisteredFactories() {
+        CompressionFactory    expected=Mockito.mock(CompressionFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            for (int index=1; index <= Byte.SIZE; index++) {
+                BuiltinCompressions.registerExtension(expected);
+                Assert.assertEquals("Unexpected success at attempt #" + index, 1, index);
+            }
+        } finally {
+            BuiltinCompressions.unregisterExtension(name);
+        }
+    }
+
+    @Test
+    public void testResolveFactoryOnRegisteredExtension() {
+        CompressionFactory    expected=Mockito.mock(CompressionFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            Assert.assertNull("Extension already registered", BuiltinCompressions.resolveFactory(name));
+            BuiltinCompressions.registerExtension(expected);
+
+            NamedFactory<Compression>    actual=BuiltinCompressions.resolveFactory(name);
+            Assert.assertSame("Mismatched resolved instance", expected, actual);
+        } finally {
+            NamedFactory<Compression>    actual=BuiltinCompressions.unregisterExtension(name);
+            Assert.assertSame("Mismatched unregistered instance", expected, actual);
+            Assert.assertNull("Extension not un-registered", BuiltinCompressions.resolveFactory(name));
+        }
+    }
+
+    @Test
+    public void testFac2NamedTransformer() {
+        Assert.assertNull("Invalid null transformation", CompressionFactory.FAC2NAMED.transform(null));
+        for (CompressionFactory expected : BuiltinCompressions.VALUES) {
+            NamedFactory<Compression>   actual=CompressionFactory.FAC2NAMED.transform(expected);
+            Assert.assertSame("Mismatched transformed instance for " + expected.getName(),
expected, actual);
+        }
+        
+        CompressionFactory   mock=Mockito.mock(CompressionFactory.class);
+        Assert.assertSame("Mismatched transformed mocked instance", mock, CompressionFactory.FAC2NAMED.transform(mock));
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java
b/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java
index e3cfcb3..4c61be3 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java
@@ -35,8 +35,11 @@ import org.apache.sshd.common.Mac;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.NamedResource;
 import org.apache.sshd.common.Signature;
+import org.apache.sshd.common.Transformer;
 import org.apache.sshd.common.cipher.BuiltinCiphers;
+import org.apache.sshd.common.compression.BuiltinCompressions;
 import org.apache.sshd.common.compression.Compression;
+import org.apache.sshd.common.compression.CompressionFactory;
 import org.apache.sshd.common.kex.BuiltinDHFactories;
 import org.apache.sshd.common.mac.BuiltinMacs;
 import org.apache.sshd.common.signature.BuiltinSignatures;
@@ -124,7 +127,7 @@ public class SshConfigFileReaderTest extends BaseTest {
                 }
             };
         // must be lenient since we do not cover the full default spectrum
-        AbstractFactoryManager  actual=SshConfigFileReader.configure(expected, props, true);
+        AbstractFactoryManager  actual=SshConfigFileReader.configure(expected, props, true,
true);
         Assert.assertSame("Mismatched configured result", expected, actual);
         validateAbstractFactoryManagerConfiguration(expected, props, true);
     }
@@ -139,7 +142,8 @@ public class SshConfigFileReaderTest extends BaseTest {
                     }
                 },
                 getCurrentTestName(),
-                false);
+                false,
+                true);
         Assert.fail("Unexpected success: " + NamedResource.Utils.getNames(manager.getCipherFactories()));
     }
 
@@ -153,7 +157,8 @@ public class SshConfigFileReaderTest extends BaseTest {
                     }
                 },
                 getCurrentTestName(),
-                false);
+                false,
+                true);
         Assert.fail("Unexpected success: " + NamedResource.Utils.getNames(manager.getSignatureFactories()));
     }
 
@@ -167,10 +172,38 @@ public class SshConfigFileReaderTest extends BaseTest {
                     }
                 },
                 getCurrentTestName(),
-                false);
+                false,
+                true);
         Assert.fail("Unexpected success: " + NamedResource.Utils.getNames(manager.getMacFactories()));
     }
-    
+
+    @Test
+    public void testConfigureCompressionFromStringAcceptsCombinedValues() {
+        testConfigureCompressionFromStringAcceptsCombinedValues(CompressionConfigValue.class,
Transformer.ENUM_NAME_EXTRACTOR);
+        testConfigureCompressionFromStringAcceptsCombinedValues(BuiltinCompressions.class,
NamedResource.NAME_EXTRACTOR);
+    }
+
+    private static <E extends Enum<E> & CompressionFactory> void testConfigureCompressionFromStringAcceptsCombinedValues(
+            Class<E> facs, Transformer<? super E,String> configValueXformer)
{
+        for (E expected : facs.getEnumConstants()) {
+            String          value=configValueXformer.transform(expected);
+            String          prefix=facs.getSimpleName() + "[" + expected.name() + "][" +
value + "]";
+            FactoryManager  manager=SshConfigFileReader.configureCompression(
+                    new AbstractFactoryManager() {
+                        @Override
+                        protected Closeable getInnerCloseable() {
+                            return null;
+                        }
+                    },
+                    value,
+                    false,
+                    true);
+            List<NamedFactory<Compression>> compressions=manager.getCompressionFactories();
+            Assert.assertEquals(prefix + "(size)", 1, GenericUtils.size(compressions));
+            Assert.assertSame(prefix + "[instance]", expected, compressions.get(0));
+        }
+    }
+
     private static <M extends FactoryManager> M validateAbstractFactoryManagerConfiguration(M
manager, Properties props, boolean lenient) {
         validateFactoryManagerCiphers(manager, props);
         validateFactoryManagerSignatures(manager, props);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/test/java/org/apache/sshd/common/kex/BuiltinDHFactoriesTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/kex/BuiltinDHFactoriesTest.java
b/sshd-core/src/test/java/org/apache/sshd/common/kex/BuiltinDHFactoriesTest.java
index 1dd77ce..04d17a5 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/kex/BuiltinDHFactoriesTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/kex/BuiltinDHFactoriesTest.java
@@ -34,6 +34,7 @@ import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.util.BaseTest;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -101,4 +102,60 @@ public class BuiltinDHFactoriesTest extends BaseTest {
             assertListEquals(fullList + "[unsupported]", unknown, missing);
         }
     }
+
+    @Test
+    public void testResolveFactoryOnBuiltinValues() {
+        for (DHFactory expected : BuiltinDHFactories.VALUES) {
+            String              name=expected.getName();
+            DHFactory   actual=BuiltinDHFactories.resolveFactory(name);
+            Assert.assertSame(name, expected, actual);
+        }
+    }
+
+    @Test
+    public void testNotAllowedToRegisterBuiltinFactories() {
+        for (DHFactory expected : BuiltinDHFactories.VALUES) {
+            try {
+                BuiltinDHFactories.registerExtension(expected);
+                Assert.fail("Unexpected sucess for " + expected.getName());
+            } catch(IllegalArgumentException e) {
+                // expected - ignored
+            }
+        }
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testNotAllowedToOverrideRegisteredFactories() {
+        DHFactory    expected=Mockito.mock(DHFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            for (int index=1; index <= Byte.SIZE; index++) {
+                BuiltinDHFactories.registerExtension(expected);
+                Assert.assertEquals("Unexpected success at attempt #" + index, 1, index);
+            }
+        } finally {
+            BuiltinDHFactories.unregisterExtension(name);
+        }
+    }
+
+    @Test
+    public void testResolveFactoryOnRegisteredExtension() {
+        DHFactory    expected=Mockito.mock(DHFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            Assert.assertNull("Extension already registered", BuiltinDHFactories.resolveFactory(name));
+            BuiltinDHFactories.registerExtension(expected);
+
+            DHFactory    actual=BuiltinDHFactories.resolveFactory(name);
+            Assert.assertSame("Mismatched resolved instance", expected, actual);
+        } finally {
+            DHFactory    actual=BuiltinDHFactories.unregisterExtension(name);
+            Assert.assertSame("Mismatched unregistered instance", expected, actual);
+            Assert.assertNull("Extension not un-registered", BuiltinDHFactories.resolveFactory(name));
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/test/java/org/apache/sshd/common/mac/BuiltinMacsTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/mac/BuiltinMacsTest.java b/sshd-core/src/test/java/org/apache/sshd/common/mac/BuiltinMacsTest.java
index ead553d..a93f73e 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/mac/BuiltinMacsTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/mac/BuiltinMacsTest.java
@@ -28,6 +28,7 @@ import java.util.List;
 import java.util.Random;
 import java.util.Set;
 
+import org.apache.sshd.common.Mac;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.NamedResource;
 import org.apache.sshd.common.mac.BuiltinMacs.ParseResult;
@@ -35,6 +36,7 @@ import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.util.BaseTest;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -102,4 +104,72 @@ public class BuiltinMacsTest extends BaseTest {
             assertListEquals(fullList + "[unsupported]", unknown, missing);
         }
     }
+
+    @Test
+    public void testResolveFactoryOnBuiltinValues() {
+        for (MacFactory expected : BuiltinMacs.VALUES) {
+            String       name=expected.getName();
+            MacFactory   actual=BuiltinMacs.resolveFactory(name);
+            Assert.assertSame(name, expected, actual);
+        }
+    }
+
+    @Test
+    public void testNotAllowedToRegisterBuiltinFactories() {
+        for (MacFactory expected : BuiltinMacs.VALUES) {
+            try {
+                BuiltinMacs.registerExtension(expected);
+                Assert.fail("Unexpected sucess for " + expected.getName());
+            } catch(IllegalArgumentException e) {
+                // expected - ignored
+            }
+        }
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testNotAllowedToOverrideRegisteredFactories() {
+        MacFactory    expected=Mockito.mock(MacFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            for (int index=1; index <= Byte.SIZE; index++) {
+                BuiltinMacs.registerExtension(expected);
+                Assert.assertEquals("Unexpected success at attempt #" + index, 1, index);
+            }
+        } finally {
+            BuiltinMacs.unregisterExtension(name);
+        }
+    }
+
+    @Test
+    public void testResolveFactoryOnRegisteredExtension() {
+        MacFactory  expected=Mockito.mock(MacFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            Assert.assertNull("Extension already registered", BuiltinMacs.resolveFactory(name));
+            BuiltinMacs.registerExtension(expected);
+
+            MacFactory  actual=BuiltinMacs.resolveFactory(name);
+            Assert.assertSame("Mismatched resolved instance", expected, actual);
+        } finally {
+            MacFactory  actual=BuiltinMacs.unregisterExtension(name);
+            Assert.assertSame("Mismatched unregistered instance", expected, actual);
+            Assert.assertNull("Extension not un-registered", BuiltinMacs.resolveFactory(name));
+        }
+    }
+
+    @Test
+    public void testFac2NamedTransformer() {
+        Assert.assertNull("Invalid null transformation", MacFactory.FAC2NAMED.transform(null));
+        for (MacFactory expected : BuiltinMacs.VALUES) {
+            NamedFactory<Mac>   actual=MacFactory.FAC2NAMED.transform(expected);
+            Assert.assertSame("Mismatched transformed instance for " + expected.getName(),
expected, actual);
+        }
+        
+        MacFactory   mock=Mockito.mock(MacFactory.class);
+        Assert.assertSame("Mismatched transformed mocked instance", mock, MacFactory.FAC2NAMED.transform(mock));
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/7ccbd066/sshd-core/src/test/java/org/apache/sshd/common/signature/BuiltinSignaturesTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/signature/BuiltinSignaturesTest.java
b/sshd-core/src/test/java/org/apache/sshd/common/signature/BuiltinSignaturesTest.java
index f61ad18..155c7c8 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/signature/BuiltinSignaturesTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/signature/BuiltinSignaturesTest.java
@@ -27,11 +27,13 @@ import java.util.Random;
 
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.NamedResource;
+import org.apache.sshd.common.Signature;
 import org.apache.sshd.common.signature.BuiltinSignatures.ParseResult;
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.util.BaseTest;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -85,4 +87,72 @@ public class BuiltinSignaturesTest extends BaseTest {
             assertListEquals(fullList + "[unsupported]", unknown, missing);
         }
     }
+
+    @Test
+    public void testResolveFactoryOnBuiltinValues() {
+        for (SignatureFactory expected : BuiltinSignatures.VALUES) {
+            String              name=expected.getName();
+            SignatureFactory    actual=BuiltinSignatures.resolveFactory(name);
+            Assert.assertSame(name, expected, actual);
+        }
+    }
+
+    @Test
+    public void testNotAllowedToRegisterBuiltinFactories() {
+        for (SignatureFactory expected : BuiltinSignatures.VALUES) {
+            try {
+                BuiltinSignatures.registerExtension(expected);
+                Assert.fail("Unexpected sucess for " + expected.getName());
+            } catch(IllegalArgumentException e) {
+                // expected - ignored
+            }
+        }
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testNotAllowedToOverrideRegisteredFactories() {
+        SignatureFactory    expected=Mockito.mock(SignatureFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            for (int index=1; index <= Byte.SIZE; index++) {
+                BuiltinSignatures.registerExtension(expected);
+                Assert.assertEquals("Unexpected success at attempt #" + index, 1, index);
+            }
+        } finally {
+            BuiltinSignatures.unregisterExtension(name);
+        }
+    }
+
+    @Test
+    public void testResolveFactoryOnRegisteredExtension() {
+        SignatureFactory    expected=Mockito.mock(SignatureFactory.class);
+        Mockito.when(expected.getName()).thenReturn(getCurrentTestName());
+
+        String  name=expected.getName();
+        try {
+            Assert.assertNull("Extension already registered", BuiltinSignatures.resolveFactory(name));
+            BuiltinSignatures.registerExtension(expected);
+
+            SignatureFactory    actual=BuiltinSignatures.resolveFactory(name);
+            Assert.assertSame("Mismatched resolved instance", expected, actual);
+        } finally {
+            SignatureFactory    actual=BuiltinSignatures.unregisterExtension(name);
+            Assert.assertSame("Mismatched unregistered instance", expected, actual);
+            Assert.assertNull("Extension not un-registered", BuiltinSignatures.resolveFactory(name));
+        }
+    }
+
+    @Test
+    public void testFac2NamedTransformer() {
+        Assert.assertNull("Invalid null transformation", SignatureFactory.FAC2NAMED.transform(null));
+        for (SignatureFactory expected : BuiltinSignatures.VALUES) {
+            NamedFactory<Signature>   actual=SignatureFactory.FAC2NAMED.transform(expected);
+            Assert.assertSame("Mismatched transformed instance for " + expected.getName(),
expected, actual);
+        }
+        
+        SignatureFactory   mock=Mockito.mock(SignatureFactory.class);
+        Assert.assertSame("Mismatched transformed mocked instance", mock, SignatureFactory.FAC2NAMED.transform(mock));
+    }
 }


Mime
View raw message