changeset 1020:a0d25551282d

Empty "" codebase now behaves as "." codebase
author Jiri Vanek <jvanek@redhat.com>
date Thu, 09 Oct 2014 18:42:42 +0200
parents a54294dbc82d
children 94e33db5ad02
files ChangeLog netx/net/sourceforge/jnlp/Parser.java netx/net/sourceforge/jnlp/security/SecurityDialogs.java tests/netx/unit/net/sourceforge/jnlp/ParserTest.java
diffstat 4 files changed, 70 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Sep 02 11:46:09 2014 -0400
+++ b/ChangeLog	Thu Oct 09 18:42:42 2014 +0200
@@ -1,3 +1,15 @@
+2014-10-09  Jiri Vanek  <jvanek@redhat.com>
+
+	Empty "" codebase now behaves as "." codebase
+	* file netx/net/sourceforge/jnlp/Parser.java: introduced CODEBASE constant
+	to avoid duplicated String getAttribute  split to getCleanAttribute, which
+	get the pure attribute, and remaining getAttribute keep adding null in case
+	of empty
+	* file netx/net/sourceforge/jnlp/security/SecurityDialogs.java: added 
+	workaround about possible null codebase
+	* file tests/netx/unit/net/sourceforge/jnlp/ParserTest.java: added test for
+	empty codebase
+
 2014-09-02  Jie Kang  <jkang@redhat.com>
 
 	Fixed CacheUtils clearCache method to also clear the Least Recently Used
--- a/netx/net/sourceforge/jnlp/Parser.java	Tue Sep 02 11:46:09 2014 -0400
+++ b/netx/net/sourceforge/jnlp/Parser.java	Thu Oct 09 18:42:42 2014 +0200
@@ -39,6 +39,8 @@
  * @version $Revision: 1.13 $
  */
 class Parser {
+    
+    private static String CODEBASE = "codebase";
 
     // defines netx.jnlp.Node class if using Tiny XML or Nano XML
 
@@ -143,7 +145,7 @@
         this.spec = getVersion(root, "spec", "1.0+");
 
         try {
-            this.codebase = addSlash(getURL(root, "codebase", base));
+            this.codebase = addSlash(getURL(root, CODEBASE, base));
         } catch (ParseException e) {
             //If parsing fails, continue by overriding the codebase with the one passed in
         }
@@ -1065,10 +1067,20 @@
      * @throws ParseException if the JNLP file is invalid
      */
     public URL getURL(Node node, String name, URL base) throws ParseException {
-        String href = getAttribute(node, name, null);
-        if (href == null)
+        String href = null;
+        if (CODEBASE.equals(name)) {
+            href = getCleanAttribute(node, name);
+            //in case of null code can throw an exception later
+            //some bogus jnlps have codebase as "" and expect it behaving as "."
+            if (href != null && href.trim().isEmpty()) {
+                href = ".";
+            }
+        } else {
+            href = getAttribute(node, name, null);
+        }
+        if (href == null) {
             return null; // so that code can throw an exception if attribute was required
-
+        }
         try {
             if (base == null)
                 return new URL(href);
@@ -1254,11 +1266,17 @@
     public String getAttribute(Node node, String name, String defaultValue) {
         // SAX
         // String result = ((Element) node).getAttribute(name);
-        String result = node.getAttribute(name);
+        String result = getCleanAttribute(node, name);
+
+        if (result == null || result.length() == 0) {
+            return defaultValue;
+        }
 
-        if (result == null || result.length() == 0)
-            return defaultValue;
+        return result;
+    }
 
+    private String getCleanAttribute(Node node, String name) {
+        String result = node.getAttribute(name);
         return result;
     }
 
--- a/netx/net/sourceforge/jnlp/security/SecurityDialogs.java	Tue Sep 02 11:46:09 2014 -0400
+++ b/netx/net/sourceforge/jnlp/security/SecurityDialogs.java	Thu Oct 09 18:42:42 2014 +0200
@@ -276,7 +276,13 @@
 
         SecurityDialogMessage message = new SecurityDialogMessage();
         message.dialogType = DialogType.MISSING_ALACA;
-        message.extras = new Object[]{title, codeBase.toString(), UrlUtils.setOfUrlsToHtmlList(remoteUrls)};
+         String urlToShow = "unknown url";
+         if (codeBase != null) {
+             urlToShow = codeBase.toString();
+         } else {
+             OutputController.getLogger().log("Warning, null codebase wants to show in ALACA!");
+         }
+        message.extras = new Object[]{title, urlToShow, UrlUtils.setOfUrlsToHtmlList(remoteUrls)};
         Object selectedValue = getUserResponse(message);
         return getIntegerResponseAsBoolean(selectedValue);
     } 
--- a/tests/netx/unit/net/sourceforge/jnlp/ParserTest.java	Tue Sep 02 11:46:09 2014 -0400
+++ b/tests/netx/unit/net/sourceforge/jnlp/ParserTest.java	Thu Oct 09 18:42:42 2014 +0200
@@ -1413,4 +1413,30 @@
 
         Assert.assertEquals(overwrittenCodebase.toExternalForm(), parser.getCodeBase().toExternalForm());
     }
+    
+    
+    @Test
+    public void testEmptyCodebase() throws Exception {
+        String data = "<?xml version=\"1.0\"?>\n"
+                + "<jnlp spec=\"1.5+\"\n"
+                + "codebase=\"\"  aaa=\"\" "
+                + ">\n"
+                + "</jnlp>";
+
+        Node root = Parser.getRootNode(new ByteArrayInputStream(data.getBytes()), defaultParser);
+        Assert.assertEquals("Root name is not jnlp", "jnlp", root.getNodeName());
+        MockJNLPFile file = new MockJNLPFile(LANG_LOCALE);
+        Parser parser = new Parser(file, null, root, defaultParser, null);
+        ParseException eex = null;
+        //non codebase element is unaffected
+        URL u = parser.getURL(root, "aaa", null);
+        Assert.assertEquals(null, u);
+        try {
+            parser.getURL(root, "codebase", null);
+        } catch (ParseException ex) {
+            eex = ex;
+        }
+        Assert.assertEquals(true, eex != null);
+        Assert.assertEquals(true, eex instanceof ParseException);
+    }
 }