From 59233334144dca83fb017795d54d99636cccee81 Mon Sep 17 00:00:00 2001 From: Jiri Vanek Date: Wed, 27 Nov 2013 14:11:13 +0100 Subject: Added null check when getting manifest attributes for case of jar without manifest * netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java: (getManifestAttribute) added check fo null manifest to prevent npe. * /tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java: added test for npe from getManifestAttribute * tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java: (createJarWithContents) enhanced to be able to create jar without manifest. --- .../jnlp/runtime/JNLPClassLoaderTest.java | 35 ++++++++++++++++++++++ .../net/sourceforge/jnlp/util/FileTestUtils.java | 16 ++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java index e713512..26de7b6 100644 --- a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java +++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java @@ -51,6 +51,7 @@ import net.sourceforge.jnlp.cache.UpdatePolicy; import net.sourceforge.jnlp.mock.DummyJNLPFileWithJar; import net.sourceforge.jnlp.util.FileTestUtils; import net.sourceforge.jnlp.util.logging.NoStdOutErrTest; +import org.junit.Assert; import org.junit.Test; @@ -259,4 +260,38 @@ public class JNLPClassLoaderTest extends NoStdOutErrTest{ } }); } + + + @Test + public void tryNullManifest() throws Exception { + File tempDirectory = FileTestUtils.createTempDirectory(); + File jarLocation = new File(tempDirectory, "test-npe.jar"); + File dummyContent = File.createTempFile("dummy", "context", tempDirectory); + jarLocation.deleteOnExit(); + + /* Test with-out any attribute specified specified */ + FileTestUtils.createJarWithoutManifestContents(jarLocation, dummyContent); + + final Exception[] exs = new Exception[2]; + final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation); + try { + final JNLPClassLoader classLoader = new JNLPClassLoader(jnlpFile, UpdatePolicy.ALWAYS); + assertNoFileLeak(new Runnable() { + @Override + public void run() { + try { + assertEquals(null, classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.MAIN_CLASS)); + assertEquals(null, classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.IMPLEMENTATION_TITLE)); + } catch (Exception e) { + exs[0] = e; + } + } + }); + } catch (Exception e) { + exs[1] = e; + } + Assert.assertNotNull(exs); + Assert.assertNull(exs[0]); + Assert.assertNull(exs[1]); + } } diff --git a/tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java b/tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java index 828d6a5..1ce4935 100644 --- a/tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java +++ b/tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java @@ -88,14 +88,26 @@ public class FileTestUtils { out.close(); } + /* Creates a jar in a temporary directory, with the given name & file contents */ + static public void createJarWithoutManifestContents(File jarFile, File... fileContents) throws Exception{ + createJarWithContents(jarFile, null, fileContents); + } + /* Creates a jar in a temporary directory, with the given name & file contents */ static public void createJarWithContents(File jarFile, Manifest manifestContents, File... fileContents) throws Exception { /* Manifest quite evilly ignores all attributes if we don't specify a version! * Make sure it's set here. */ - manifestContents.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); + if (manifestContents != null){ + manifestContents.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); + } - JarOutputStream jarWriter = new JarOutputStream(new FileOutputStream(jarFile), manifestContents); + JarOutputStream jarWriter; + if (manifestContents == null){ + jarWriter = new JarOutputStream(new FileOutputStream(jarFile)); + } else { + jarWriter = new JarOutputStream(new FileOutputStream(jarFile), manifestContents); + } for (File file : fileContents) { jarWriter.putNextEntry(new JarEntry(file.getName())); FileInputStream fileReader = new FileInputStream(file); -- cgit v1.2.3