diff -r 2a7aa789f554 Makefile.am --- a/Makefile.am Mon Oct 03 15:28:10 2011 +0200 +++ b/Makefile.am Fri Oct 07 17:17:39 2011 +0200 @@ -36,7 +36,7 @@ RHINO_RUNTIME= endif -RUNTIME = $(BOOT_DIR)/jre/lib/rt.jar:$(BOOT_DIR)/jre/lib/jsse.jar$(RHINO_RUNTIME) +RUNTIME = $(BOOT_DIR)/jre/lib/rt.jar:$(BOOT_DIR)/jre/lib/jsse.jar$(RHINO_RUNTIME):$(BASE64_JAR) # Flags IT_CFLAGS=$(CFLAGS) $(ARCHFLAG) @@ -52,8 +52,8 @@ # IllegalAccessException # - we want full privileges # -LAUNCHER_BOOTCLASSPATH="-Xbootclasspath/a:$(datadir)/$(PACKAGE_NAME)/netx.jar$(RHINO_RUNTIME)" -PLUGIN_BOOTCLASSPATH='"-Xbootclasspath/a:$(datadir)/$(PACKAGE_NAME)/netx.jar:$(datadir)/$(PACKAGE_NAME)/plugin.jar$(RHINO_RUNTIME)"' +LAUNCHER_BOOTCLASSPATH="-Xbootclasspath/a:$(datadir)/$(PACKAGE_NAME)/netx.jar$(RHINO_RUNTIME):$(BASE64_JAR)" +PLUGIN_BOOTCLASSPATH='"-Xbootclasspath/a:$(datadir)/$(PACKAGE_NAME)/netx.jar:$(datadir)/$(PACKAGE_NAME)/plugin.jar$(RHINO_RUNTIME):$(BASE64_JAR)"' # Fake update version to work with the Deployment Toolkit script used by Oracle # http://download.oracle.com/javase/tutorial/deployment/deploymentInDepth/depltoolkit_index.html diff -r 2a7aa789f554 acinclude.m4 --- a/acinclude.m4 Mon Oct 03 15:28:10 2011 +0200 +++ b/acinclude.m4 Fri Oct 07 17:17:39 2011 +0200 @@ -409,6 +409,8 @@ dnl replaced by '_' and all letters capitalised. dnl e.g. IT_CHECK_FOR_CLASS([JAVA_UTIL_SCANNER],[java.util.Scanner]) dnl Test class has to be in sun.applet for some internal classes +dnl third parameter is optional. Should be missing or be "w" +dnl if "w" is declared, then not found class is just warning, not error AC_DEFUN([IT_CHECK_FOR_CLASS],[ AC_REQUIRE([IT_FIND_JAVAC]) AC_REQUIRE([IT_FIND_JAVA]) @@ -448,7 +450,11 @@ # should be rmdir but has to be rm -rf due to sun.applet usage rm -rf tmp.$$ if test x"${it_cv_$1}" = "xno"; then - AC_MSG_ERROR([$2 not found.]) + if test "$3" != "w"; then + AC_MSG_ERROR([$2 not found.]) + else + AC_MSG_WARN([$2 not found.]) + fi fi AC_PROVIDE([$0])dnl ]) diff -r 2a7aa789f554 configure.ac --- a/configure.ac Mon Oct 03 15:28:10 2011 +0200 +++ b/configure.ac Fri Oct 07 17:17:39 2011 +0200 @@ -70,7 +70,7 @@ IT_CHECK_FOR_CLASS(SUN_SECURITY_UTIL_SECURITYCONSTANTS, [sun.security.util.SecurityConstants]) IT_CHECK_FOR_CLASS(SUN_SECURITY_UTIL_HOSTNAMECHECKER, [sun.security.util.HostnameChecker]) IT_CHECK_FOR_CLASS(SUN_SECURITY_X509_X500NAME, [sun.security.x509.X500Name]) -IT_CHECK_FOR_CLASS(SUN_MISC_BASE64ENCODER, [sun.misc.BASE64Encoder]) +IT_CHECK_FOR_CLASS(SUN_MISC_BASE64ENCODER, [sun.misc.BASE64Encoder],[w]) IT_CHECK_FOR_CLASS(SUN_MISC_HEXDUMPENCODER, [sun.misc.HexDumpEncoder]) IT_CHECK_FOR_CLASS(SUN_SECURITY_VALIDATOR_VALIDATOREXCEPTION, [sun.security.validator.ValidatorException]) IT_CHECK_FOR_CLASS(COM_SUN_NET_SSL_INTERNAL_SSL_X509EXTENDEDTRUSTMANAGER, @@ -92,6 +92,14 @@ [/usr/share/java/js.jar /usr/share/rhino-1.6/lib/js.jar]) IT_FIND_OPTIONAL_JAR([junit], JUNIT, [/usr/share/java/junit4.jar /usr/share/junit-4/lib/junit.jar]) +IT_FIND_OPTIONAL_JAR([base64provider], BASE64, + [/usr/share/java/apache-commons-codec.jar]) + +if test "$BASE64_AVAILABLE" = "false" ; then + if test "$it_cv_SUN_MISC_BASE64ENCODER" = "no" ; then + AC_MSG_ERROR([Sorry, sun or at least one alternative base64 provider must be avaiable]) + fi +fi AC_CONFIG_FILES([jrunscript], [chmod u+x jrunscript]) AC_CONFIG_FILES([build.properties]) diff -r 2a7aa789f554 netx/net/sourceforge/jnlp/security/CertificateUtils.java --- a/netx/net/sourceforge/jnlp/security/CertificateUtils.java Mon Oct 03 15:28:10 2011 +0200 +++ b/netx/net/sourceforge/jnlp/security/CertificateUtils.java Fri Oct 07 17:17:39 2011 +0200 @@ -60,7 +60,8 @@ import java.util.Random; import net.sourceforge.jnlp.runtime.JNLPRuntime; -import sun.misc.BASE64Encoder; +import net.sourceforge.jnlp.util.BASE64Encoder; +import net.sourceforge.jnlp.util.BASE64EncoderFactory; import sun.security.provider.X509Factory; /** @@ -210,7 +211,7 @@ public static void dump(Certificate cert, PrintStream out) throws IOException, CertificateException { - BASE64Encoder encoder = new BASE64Encoder(); + BASE64Encoder encoder = BASE64EncoderFactory.getInstance(); out.println(X509Factory.BEGIN_CERT); encoder.encodeBuffer(cert.getEncoded(), out); out.println(X509Factory.END_CERT); diff -r 2a7aa789f554 netx/net/sourceforge/jnlp/util/BASE64Encoder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/netx/net/sourceforge/jnlp/util/BASE64Encoder.java Fri Oct 07 17:17:39 2011 +0200 @@ -0,0 +1,50 @@ +/* BASE64Encoder.java -- Traverse the given directory and return the leafs. + Copyright (C) 2010 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. +*/ + +package net.sourceforge.jnlp.util; + +import java.io.IOException; +import java.io.OutputStream; + + +public interface BASE64Encoder { + + public void encodeBuffer(byte[] encoded, OutputStream out) throws IOException; + public byte[] decodeBytes(String coded) throws IOException; + + +} diff -r 2a7aa789f554 netx/net/sourceforge/jnlp/util/BASE64EncoderFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/netx/net/sourceforge/jnlp/util/BASE64EncoderFactory.java Fri Oct 07 17:17:39 2011 +0200 @@ -0,0 +1,218 @@ +/* BASE64EncoderFactory.java -- Traverse the given directory and return the leafs. + Copyright (C) 2010 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. +*/ +package net.sourceforge.jnlp.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.reflect.Method; + +public class BASE64EncoderFactory { + + public static final String apacheClass = "org.apache.commons.codec.binary.Base64"; + public static final String sunClassE = "sun.misc.BASE64Encoder"; + public static final String sunClassD = "sun.misc.BASE64Decoder"; + private static BASE64EncoderFactory factory; + + public static BASE64EncoderFactory getFactory() { + if (factory == null) { + factory = new BASE64EncoderFactory(); + } + return factory; + } + + public static BASE64Encoder getInstance() { + + return getFactory().getBASE64EncoderInstance(); + } + + private BASE64Encoder getBASE64EncoderInstance() { + if (isApacheBase64Avaiable()) { + return getApacheCommonsCodecEncoderInstance(); + } + if (isSunMiscBASE64Avaiable()) { + return getSunMiscBasedInstance(); + } + return getDummyInstance(); + } + + public boolean isApacheBase64Avaiable() { + try { + Class.forName(apacheClass); + } catch (Exception ex) { + return false; + } + return true; + } + + public boolean isSunMiscBASE64Avaiable() { + try { + Class.forName(sunClassE); + //Class.forName(sunClassD); + } catch (Exception ex) { + return false; + } + return true; + } + + private static class BASE64SunMiscEncoder implements BASE64Encoder { + + //private final sun.misc.BASE64Encoder provider; + private Object provider; + //private final sun.misc.BASE64Decoder deprovider; + private Object deprovider; + + public Object getDeprovider() { + if (deprovider == null) { + deprovider = BASE64EncoderFactory.createSafeInsatnce(sunClassD); + } + return deprovider; + } + + public Object getProvider() { + if (provider == null) { + provider = BASE64EncoderFactory.createSafeInsatnce(sunClassE); + } + return provider; + } + + @Override + public void encodeBuffer(byte[] encoded, OutputStream out) throws IOException { + //provider.encode(encoded, out); + BASE64EncoderFactory.getAndInvokeMethod(getProvider(), "encode", ((Object) (encoded)), out); + } + + @Override + public byte[] decodeBytes(String coded) throws IOException { + //return deprovider.decodeBuffer(coded); + return (byte[]) (BASE64EncoderFactory.getAndInvokeMethod(getDeprovider(), "decodeBuffer", coded)); + } + } + + private static class BASE64ApacheCommonsCodecEncoder implements BASE64Encoder { + + // private final org.apache.commons.codec.binary.Base64 provider; + private Object provider; + + public Object getProvider() { + if (provider == null) { + provider = BASE64EncoderFactory.createSafeInsatnce(apacheClass); + } + return provider; + } + + public BASE64ApacheCommonsCodecEncoder() { + } + + @Override + public void encodeBuffer(byte[] encoded, OutputStream out) throws IOException { + //byte[] output = provider.encode(encoded); + byte[] output = (byte[]) (BASE64EncoderFactory.getAndInvokeMethod(getProvider(), "encode", encoded)); + out.write(output); + } + + @Override + public byte[] decodeBytes(String coded) throws IOException { + //return provider.decode(coded); + return (byte[]) (BASE64EncoderFactory.getAndInvokeMethod(getProvider(), "decode", coded)); + } + } + + public static class DummyProvider implements BASE64Encoder { + + @Override + public void encodeBuffer(byte[] encoded, OutputStream out) throws IOException { + throw new UnsupportedOperationException("Sorry, your system have no libraries providing base64 encoding. Try to Install apache-commons-codec"); + } + + @Override + public byte[] decodeBytes(String coded) throws IOException { + throw new UnsupportedOperationException("Sorry, your system have no libraries providing base64 encoding. Try to Install apache-commons-codec"); + } + } + + protected BASE64Encoder getDummyInstance() { + return new DummyProvider(); + } + + protected BASE64Encoder getSunMiscBasedInstance() { + return new BASE64SunMiscEncoder(); + } + + protected BASE64Encoder getApacheCommonsCodecEncoderInstance() { + return new BASE64ApacheCommonsCodecEncoder(); + } + + //those four methods exists just to wrap exceptions to runtime exceptions + private static Object createSafeInsatnce(String ofCalss) { + try { + Class classDefinition = Class.forName(ofCalss); + return classDefinition.newInstance(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + private static Method getSafeMethod(Class c, String methodName, Class... params) { + try { + return c.getMethod(methodName, params); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + private static Object invokeSafeMethod(Object instance, Method m, Object... params) { + try { + return m.invoke(instance, params); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + private static Object getAndInvokeMethod(Object instance, String methodName, Object... params) { + Class[] cs = new Class[params.length]; + for (int i = 0; i < params.length; i++) { + Object object = params[i]; + cs[i] = object.getClass(); + if (object instanceof OutputStream) { + cs[i] = OutputStream.class; + } + } + Method m = getSafeMethod(instance.getClass(), methodName, cs); + return invokeSafeMethod(instance, m, params); + + } +} diff -r 2a7aa789f554 tests/jnlp_tests/signed/CheckBase64Providers/resources/CheckBase64Providers.jnlp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/jnlp_tests/signed/CheckBase64Providers/resources/CheckBase64Providers.jnlp Fri Oct 07 17:17:39 2011 +0200 @@ -0,0 +1,16 @@ + + + + read properties using System.getenv() + + + + + + + + + + diff -r 2a7aa789f554 tests/jnlp_tests/signed/CheckBase64Providers/srcs/CheckBase64Providers.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/jnlp_tests/signed/CheckBase64Providers/srcs/CheckBase64Providers.java Fri Oct 07 17:17:39 2011 +0200 @@ -0,0 +1,82 @@ +/* CheckBase64Providers.java +Copyright (C) 2011 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. + */ +import java.lang.reflect.*; + +public class CheckBase64Providers { + + /** + *some system property is expected as arg[0], eg user.name or user.home + */ + public static void main(String[] args) throws Throwable { + //security manager is not protecting us from accessing classes from + //net.sourceforge.jnlp.runtime via reflection + Class factoryClass= Class.forName("net.sourceforge.jnlp.util.BASE64EncoderFactory"); + //public static methods + Method getFactoryMethod=factoryClass.getDeclaredMethod("getFactory"); + Method getInstanceMethod=factoryClass.getDeclaredMethod("getInstance"); + Object factory = getFactoryMethod.invoke((Object) null); + System.out.println("factory class: "+factory.getClass().toString()); + Object encoderDefaultInstatnce = getInstanceMethod.invoke((Object) null); + System.out.println("default encoder insatnce: "+encoderDefaultInstatnce.getClass().toString()); + //public discovery methods + //isApacheBase64Avaiable + //isSunMiscBASE64Avaiable + Method isApacheBase64Avaiable=factoryClass.getDeclaredMethod("isApacheBase64Avaiable"); + Method isSunMiscBASE64Avaiable=factoryClass.getDeclaredMethod("isSunMiscBASE64Avaiable"); + boolean apache=(Boolean) isApacheBase64Avaiable.invoke(factory); + boolean sun=(Boolean) isSunMiscBASE64Avaiable.invoke(factory); + //protected encoders getters + //getSunMiscBasedInstance + //getApacheCommonsCodecEncoderInstance + Method getSunMiscBasedInstance=factoryClass.getDeclaredMethod("getSunMiscBasedInstance"); + getSunMiscBasedInstance.setAccessible(true); + Method getApacheCommonsCodecEncoderInstance=factoryClass.getDeclaredMethod("getApacheCommonsCodecEncoderInstance"); + getApacheCommonsCodecEncoderInstance.setAccessible(true); + + if (apache){ + Object apacheInsatnce=getApacheCommonsCodecEncoderInstance.invoke(factory); + System.out.println("apache instance: "+apacheInsatnce.getClass().toString()); + } + if (sun){ + Object sunInsatnce=getSunMiscBasedInstance.invoke(factory); + System.out.println("sun instance: "+sunInsatnce.getClass().toString()); + } + } + + + +} diff -r 2a7aa789f554 tests/jnlp_tests/signed/CheckBase64Providers/testcases/CheckBase64ProvidersTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/jnlp_tests/signed/CheckBase64Providers/testcases/CheckBase64ProvidersTest.java Fri Oct 07 17:17:39 2011 +0200 @@ -0,0 +1,78 @@ +/* CheckBase64ProvidersTest.java +Copyright (C) 2011 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. + */ + + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import net.sourceforge.jnlp.ServerAccess; +import org.junit.Assert; +import org.junit.Test; + +public class CheckBase64ProvidersTest { + + private static ServerAccess server = new ServerAccess(); + private final List l=Collections.unmodifiableList(Arrays.asList(new String[] {"-Xtrustall"})); + + + @Test + public void CheckBase64ProvidersByReflexion() throws Exception { + //no request for permissions + System.out.println("connecting CheckBase64Providers request"); + System.err.println("connecting CheckBase64Providers request"); + ServerAccess.ProcessResult pr=server.executeJavawsHeadless(l,"/CheckBase64Providers.jnlp"); + System.out.println(pr.stdout); + System.err.println(pr.stderr); + String factoryLine="factory class: class net.sourceforge.jnlp.util.BASE64EncoderFactory"; + String defaultEncoderHeader="default encoder insatnce: "; + String apacheEncoderHeader="apache instance: "; + String apacheClass="class net.sourceforge.jnlp.util.BASE64EncoderFactory$BASE64ApacheCommonsCodecEncoder"; + String sunEncoderHeader="sun instance: "; + String sunClass="class net.sourceforge.jnlp.util.BASE64EncoderFactory$BASE64SunMiscEncoder"; + Assert.assertTrue("stdout must factory class", pr.stdout.contains(factoryLine)); + Assert.assertTrue("stdout must contains apache or sun base64 provider", pr.stdout.contains(apacheClass) || pr.stdout.contains(sunClass)); + if (pr.stdout.contains(apacheEncoderHeader)){ + Assert.assertTrue("when apache base64 provider is found, then must be used in prior of sun, isnt", pr.stdout.contains(defaultEncoderHeader+apacheClass)); + } + String ss="ClassNotFoundException"; + Assert.assertFalse("Stderr should not contains "+ss+" but did",pr.stderr.contains(ss)); + Assert.assertTrue("stdout lenght should be >2 but was "+pr.stdout.length(),pr.stdout.length()>2); // some.class....apache or sun + Assert.assertFalse("should not be terminated but was",pr.wasTerminated); + Assert.assertEquals((Integer)0, pr.returnValue); + } + + } diff -r 2a7aa789f554 tests/netx/unit/net/sourceforge/jnlp/Base64EncodersTests.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/netx/unit/net/sourceforge/jnlp/Base64EncodersTests.java Fri Oct 07 17:17:39 2011 +0200 @@ -0,0 +1,213 @@ +/* Base64EncodersTests.java +Copyright (C) 2011 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. + */ +package net.sourceforge.jnlp; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import net.sourceforge.jnlp.util.BASE64Encoder; +import net.sourceforge.jnlp.util.BASE64EncoderFactory; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** Test that the parser works with basic jnlp files */ +public class Base64EncodersTests { + + private static final TestingBASE64EncoderFactory provider = new TestingBASE64EncoderFactory(); + private static final String sSrc = "abcdefgHIJKLMNOPQrstuvwxyz1234567890" + + "-=+_))(**&&&^^%%$$##@@!!~{}][\":'/\\.,><\n" + + "+ěšěčřžýáíé=ů/úěřťšďňéíáč"; + + private static class TestingBASE64EncoderFactory extends BASE64EncoderFactory { + + public BASE64Encoder getDummy() { + return getDummyInstance(); + } + + protected BASE64Encoder getSunMiscBased() { + return getSunMiscBasedInstance(); + } + + protected BASE64Encoder getApacheCommonsCodecEncoder() { + + + + + return getApacheCommonsCodecEncoderInstance(); + } + }; + + @BeforeClass + public static void setUp() { + Assert.assertNotNull("provider must not be null, was", provider); + } + + @Test + public void testTestProvider() { + Assert.assertNotNull("provided dummy instance must not be null", provider.getDummy()); + if (provider.isSunMiscBASE64Avaiable()) { + Assert.assertNotNull("provided sun based instance must not be null", provider.getSunMiscBased()); + } else { + System.err.println("sun provider dont exists. Some of test will be skipped"); + } + if (provider.isApacheBase64Avaiable()) { + Assert.assertNotNull("provided apache based instance must not be null", provider.getApacheCommonsCodecEncoder()); + } else { + System.err.println("apache provider dont exists. Some of test will be skipped"); + } + Assert.assertTrue("at least one of sun or apache base64 provider should exists", provider.isApacheBase64Avaiable() || provider.isSunMiscBASE64Avaiable()); + } + + @Test + public void testProvider() { + Assert.assertEquals(BASE64EncoderFactory.getFactory(), BASE64EncoderFactory.getFactory()); + Assert.assertEquals(BASE64EncoderFactory.getInstance().getClass(), BASE64EncoderFactory.getInstance().getClass()); + Assert.assertTrue("at least one of sun or apache base64 provider should exists", BASE64EncoderFactory.getFactory().isApacheBase64Avaiable() || BASE64EncoderFactory.getFactory().isSunMiscBASE64Avaiable()); + if (BASE64EncoderFactory.getFactory().isSunMiscBASE64Avaiable()) { + Assert.assertNotNull("provided sun based instance must not be null", BASE64EncoderFactory.getInstance()); + Assert.assertFalse("sun provider exists, so it should not be dummy class", BASE64EncoderFactory.getInstance() instanceof BASE64EncoderFactory.DummyProvider); + } + if (BASE64EncoderFactory.getFactory().isApacheBase64Avaiable()) { + Assert.assertNotNull("provided apache based instance must not be null", provider.getApacheCommonsCodecEncoder()); + Assert.assertFalse("apache provider exists, so it should not be dummy class", BASE64EncoderFactory.getInstance() instanceof BASE64EncoderFactory.DummyProvider); + } + } + + @Test + public void testDummyInstance() { + Exception ex = null; + try { + provider.getDummy().encodeBuffer(null, null); + } catch (Exception exx) { + ex = exx; + } + Assert.assertNotNull("dummy instance must throw exception", ex); + Assert.assertTrue("dummy instance thrown exception should be UnsupportedOperationException", ex instanceof UnsupportedOperationException); + + + } + + @Test + public void testSunInstance() throws UnsupportedEncodingException, IOException { + if (provider.isSunMiscBASE64Avaiable()) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + provider.getSunMiscBased().encodeBuffer(sSrc.getBytes("utf-8"), bos); + byte[] result = bos.toByteArray(); + Assert.assertNotNull("encoded result cant be null. is", result); + Assert.assertTrue("length of encoded string must be equal or longer then source's length. Wasnt - " + result.length + " is not >= " + sSrc.length(), result.length >= sSrc.length()); + //System.out.println(new String(result, "utf-8")); + byte[] decodedSource = provider.getSunMiscBased().decodeBytes(new String(result, "utf-8")); + Assert.assertArrayEquals("decoded and original string must be same, arnt", sSrc.getBytes("utf-8"), decodedSource); + //System.out.println(new String(decodedSource,"utf-8")); + } else { + System.err.println("Sun instance of base64 encoder can not be tested, becasue it does not exists"); + } + + + + } + + @Test + public void testApacheInstance() throws UnsupportedEncodingException, IOException { + if (provider.isApacheBase64Avaiable()) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + provider.getApacheCommonsCodecEncoder().encodeBuffer(sSrc.getBytes("utf-8"), bos); + byte[] result = bos.toByteArray(); + Assert.assertNotNull("encoded result cant be null. is", result); + Assert.assertTrue("length of encoded string must be equal or longer then source's length. Wasnt - " + result.length + " is not >= " + sSrc.length(), result.length >= sSrc.length()); + //System.out.println(new String(result, "utf-8")); + byte[] decodedSource = provider.getSunMiscBased().decodeBytes(new String(result, "utf-8")); + Assert.assertArrayEquals("decoded and original string must be same, arnt", sSrc.getBytes("utf-8"), decodedSource); + //System.out.println(new String(decodedSource,"utf-8")); + } else { + System.err.println("Apache instance of base64 encoder can not be tested, becasue it does not exists"); + } + + } + + @Test + public void testApacheXsunInstance() throws UnsupportedEncodingException, IOException { + if (provider.isApacheBase64Avaiable() && provider.isSunMiscBASE64Avaiable()) { + ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); + provider.getSunMiscBased().encodeBuffer(sSrc.getBytes("utf-8"), bos1); + byte[] result1 = bos1.toByteArray(); + + ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); + provider.getApacheCommonsCodecEncoder().encodeBuffer(sSrc.getBytes("utf-8"), bos2); + byte[] result2 = bos2.toByteArray(); + + //System.out.println("\""+new String(result1, "utf-8")+"\""); + //System.out.println("\""+new String(result2, "utf-8")+"\""); + + //Assert.assertArrayEquals(result1, result2); - FAIL - apache is aading + //end of line to end of EACH (even last) line. Sun is adding end of line + //to end of eeach line EXCEPT last - see testApacheXsunInstancesCodingXdecoding + //and as testApacheXsunInstancesCodingXdecoding is passing, then this minor divergencee is unimportant + Assert.assertTrue(Math.abs(result2.length - result1.length) <= 5); + } else { + System.err.println("Sun instance of base64 encoder can not be comapred with apache one by result, becasue one of them do not exists"); + } + } + + @Test + public void testApacheXsunInstancesCodingXdecoding() throws UnsupportedEncodingException, IOException { + if (provider.isApacheBase64Avaiable() && provider.isSunMiscBASE64Avaiable()) { + ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); + provider.getSunMiscBased().encodeBuffer(sSrc.getBytes("utf-8"), bos1); + byte[] result1 = bos1.toByteArray(); + + ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); + provider.getApacheCommonsCodecEncoder().encodeBuffer(sSrc.getBytes("utf-8"), bos2); + byte[] result2 = bos2.toByteArray(); + + //System.out.println("\""+new String(result1, "utf-8")+"\""); + //System.out.println("\""+new String(result2, "utf-8")+"\""); + + byte[] decodedSource12 = provider.getApacheCommonsCodecEncoder().decodeBytes(new String(result1, "utf-8")); + Assert.assertArrayEquals("decoded and original string must be same, arent", sSrc.getBytes("utf-8"), decodedSource12); + //System.out.println(new String(decodedSource12,"utf-8")); + + byte[] decodedSource21 = provider.getSunMiscBased().decodeBytes(new String(result2, "utf-8")); + Assert.assertArrayEquals("decoded and original string must be same, arnet", sSrc.getBytes("utf-8"), decodedSource21); + //System.out.println(new String(decodedSource21,"utf-8")); + } else { + System.err.println("Sun instance of base64 encoder can not be comapred with apache one by decoding, becasue one of them do not exists"); + } + } +}