1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 package org.mozilla.gecko.tests;
6 
7 import java.io.InputStream;
8 
9 import org.mozilla.gecko.AppConstants;
10 import org.mozilla.gecko.util.GeckoJarReader;
11 
12 import android.content.Context;
13 
14 /**
15  * A basic jar reader test. Tests reading a png from fennec's apk, as well
16  * as loading some invalid jar urls.
17  */
18 public class testJarReader extends OldBaseTest {
testJarReader()19     public void testJarReader() {
20         // Invalid characters are escaped.
21         final String s = GeckoJarReader.computeJarURI("some[1].apk", "something/else");
22         mAsserter.ok(!s.contains("["), "Illegal characters are escaped away.", null);
23         mAsserter.ok(!s.toLowerCase().contains("%2f"), "Path characters aren't escaped.", null);
24 
25         final Context context = getInstrumentation().getTargetContext().getApplicationContext();
26         String appPath = getActivity().getApplication().getPackageResourcePath();
27         mAsserter.isnot(appPath, null, "getPackageResourcePath is non-null");
28 
29         // Test reading a file from a jar url that looks correct.
30         String url = "jar:file://" + appPath + "!/" + AppConstants.OMNIJAR_NAME;
31         InputStream stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
32         mAsserter.isnot(stream, null, "JarReader returned non-null for valid file in valid jar");
33 
34         // Test looking for an non-existent file in a jar.
35         url = "jar:file://" + appPath + "!/" + AppConstants.OMNIJAR_NAME;
36         stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
37         mAsserter.is(stream, null, "JarReader returned null for non-existent file in valid jar");
38 
39         // Test looking for a file that doesn't exist in the APK.
40         url = "jar:file://" + appPath + "!/" + "BAD" + AppConstants.OMNIJAR_NAME;
41         stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
42         mAsserter.is(stream, null, "JarReader returned null for valid file in invalid jar file");
43 
44         // Test looking for a file that doesn't exist in the APK.
45         // Bug 1174922, prefixed string / length error.
46         url = "jar:file://" + appPath + "!/" + AppConstants.OMNIJAR_NAME + "BAD";
47         stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
48         mAsserter.is(stream, null, "JarReader returned null for valid file in other invalid jar file");
49 
50         // Test looking for an jar with an invalid url.
51         url = "jar:file://" + appPath + "!" + "!/" + AppConstants.OMNIJAR_NAME;
52         stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
53         mAsserter.is(stream, null, "JarReader returned null for bad jar url");
54 
55         // Test looking for a file that doesn't exist on disk.
56         url = "jar:file://" + appPath + "BAD" + "!/" + AppConstants.OMNIJAR_NAME;
57         stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
58         mAsserter.is(stream, null, "JarReader returned null for a non-existent APK");
59 
60         // This test completes very quickly. If it completes too soon, the
61         // minidumps directory may not be created before the process is
62         // taken down, causing bug 722166.
63         blockForGeckoReady();
64     }
65 
getData(InputStream stream)66     private String getData(InputStream stream) {
67         return new java.util.Scanner(stream).useDelimiter("\\A").next();
68     }
69 
70 }
71