Bug 738890 - Fix testJarReader. r=wesj
authorNick Alexander <nalexander@mozilla.com>
Fri, 21 Jun 2013 14:00:39 -0700
changeset 147630 c4980d2512385bad101eec87dcadbe55726c4267
parent 147629 646dd2d4fec53f5033e31e115b1691c3e17c49c9
child 147631 d5da469d2c49830901de202306efcb6c48c02543
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj
bugs738890
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 738890 - Fix testJarReader. r=wesj
mobile/android/base/tests/robocop.ini
mobile/android/base/tests/testJarReader.java.in
mobile/android/base/util/GeckoJarReader.java
--- a/mobile/android/base/tests/robocop.ini
+++ b/mobile/android/base/tests/robocop.ini
@@ -30,17 +30,17 @@
 [testVkbOverlap]
 [testDoorHanger]
 [testTabHistory]
 [testShareLink]
 [testClearPrivateData]
 # [testSettingsMenuItems] # see bug 843947
 [testSystemPages]
 # [testPermissions] # see bug 757475
-# [testJarReader] # see bug 738890
+[testJarReader]
 [testDistribution]
 [testFindInPage]
 [testInputAwesomeBar]
 [testAddSearchEngine]
 [testImportFromAndroid]
 [testMasterPassword]
 [testDeviceSearchEngine]
 
--- a/mobile/android/base/tests/testJarReader.java.in
+++ b/mobile/android/base/tests/testJarReader.java.in
@@ -1,51 +1,68 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import java.lang.ClassLoader;
 import java.io.InputStream;
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 
 /**
  * A basic jar reader test. Tests reading a png from fennec's apk, as well
  * as loading some invalid jar urls.
  */
 public class testJarReader extends BaseTest {
     @Override
     protected int getTestType() {
         return TEST_MOCHITEST;
     }
 
     public void testJarReader() {
         try {
             ClassLoader classLoader = getActivity().getClassLoader();
+
+            Class appConstantsClass = classLoader.loadClass("org.mozilla.gecko.AppConstants");
+            String omniJarName = (String) appConstantsClass.getField("OMNIJAR_NAME").get(null);
+
             Class gjrClass = classLoader.loadClass("org.mozilla.gecko.util.GeckoJarReader");
             Method getStreamMethod = gjrClass.getMethod("getStream", String.class);
             String appPath = getActivity().getApplication().getPackageResourcePath();
+            mAsserter.isnot(appPath, null, "getPackageResourcePath is non-null");
 
-            // Test reading a file from a jar url
-            String url = "jar:file://" + getActivity().getApplication().getPackageResourcePath() + "!/omni.ja";
-            InputStream stream = (InputStream)getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
-            mAsserter.isnot(stream, null, "JarReader returned null for invalid jar file");
+            // Test reading a file from a jar url that looks correct.
+            String url = "jar:file://" + appPath + "!/" + omniJarName;
+            InputStream stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
+            mAsserter.isnot(stream, null, "JarReader returned non-null for valid file in valid jar");
+
+            // Test looking for an non-existent file in a jar.
+            url = "jar:file://" + appPath + "!/" + omniJarName;
+            stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
+            mAsserter.is(stream, null, "JarReader returned null for non-existent file in valid jar");
 
-            // Test looking for a jar that doesn't exist
-            url = "jar:file://" + getActivity().getApplication().getPackageResourcePath() + "2!/omni.ja";
-            stream = (InputStream)getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
-            mAsserter.is(stream, null, "JarReader returned null for invalid jar file");
+            // Test looking for a file that doesn't exist in the APK.
+            url = "jar:file://" + appPath + "!/" + "BAD" + omniJarName;
+            stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
+            mAsserter.is(stream, null, "JarReader returned null for valid file in invalid jar file");
+
+            // Test looking for an jar with an invalid url.
+            url = "jar:file://" + appPath + "!" + "!/" + omniJarName;
+            stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
+            mAsserter.is(stream, null, "JarReader returned null for bad jar url");
 
-            // Test looking for an non-existant file in a jar
-            url = "jar:file://" + getActivity().getApplication().getPackageResourcePath() + "2!/omni.ja";
-            stream = (InputStream)getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/nonexistant_file.png");
-            mAsserter.is(stream, null, "JarReader returned null for non-existant file in jar");
-
-            // Test looking for an jar with an invalid url
-            url = "jar:file://" + getActivity().getApplication().getPackageResourcePath() + "2!!/omni.ja";
-            stream = (InputStream)getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/nonexistant_file.png");
-            mAsserter.is(stream, null, "JarReader returned null for bar jar url");
+            // Test looking for a file that doesn't exist on disk.
+            url = "jar:file://" + appPath + "BAD" + "!/" + omniJarName;
+            stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
+            mAsserter.is(stream, null, "JarReader returned null for a non-existent APK");
+        } catch (java.lang.ClassCastException ex) {
+            mAsserter.is(false, true, "Error getting OMNIJAR_NAME");
+            return;
+        } catch (java.lang.NoSuchFieldException ex) {
+            mAsserter.is(false, true, "Error getting field");
+            return;
         } catch (java.lang.ClassNotFoundException ex) {
             mAsserter.is(false, true, "Error getting class");
             return;
         } catch (java.lang.NoSuchMethodException ex) {
             mAsserter.is(false, true, "Error getting method");
             return;
         } catch (java.lang.IllegalAccessException ex) {
             mAsserter.is(false, true, "Error calling method");
--- a/mobile/android/base/util/GeckoJarReader.java
+++ b/mobile/android/base/util/GeckoJarReader.java
@@ -93,16 +93,30 @@ public final class GeckoJarReader {
         return text;
     }
 
     private static NativeZip getZipFile(String url) throws IOException {
         URL fileUrl = new URL(url);
         return new NativeZip(fileUrl.getPath());
     }
 
+    // Public for testing only.
+    public static InputStream getStream(String url) {
+        Stack<String> jarUrls = parseUrl(url);
+        try {
+            NativeZip zip = getZipFile(jarUrls.pop());
+            return getStream(zip, jarUrls, url);
+        } catch (Exception ex) {
+            // Some JNI code throws IllegalArgumentException on a bad file name;
+            // swallow the error and return null.  We could also see legitimate
+            // IOExceptions here.
+            return null;
+        }
+    }
+
     private static InputStream getStream(NativeZip zip, Stack<String> jarUrls, String origUrl) {
         InputStream inputStream = null;
 
         // loop through children jar files until we reach the innermost one
         while (!jarUrls.empty()) {
             String fileName = jarUrls.pop();
 
             if (inputStream != null) {