Index: components/installedapp/android/junit/src/org/chromium/device/installedapp/InstalledAppProviderTest.java |
diff --git a/components/installedapp/android/junit/src/org/chromium/device/installedapp/InstalledAppProviderTest.java b/components/installedapp/android/junit/src/org/chromium/device/installedapp/InstalledAppProviderTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8344127759c99489a52a8104503e42b0add8f622 |
--- /dev/null |
+++ b/components/installedapp/android/junit/src/org/chromium/device/installedapp/InstalledAppProviderTest.java |
@@ -0,0 +1,156 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.device.installedapp; |
+ |
+import android.content.pm.ApplicationInfo; |
+import android.content.pm.PackageManager; |
+import android.content.pm.PackageManager.NameNotFoundException; |
+import android.content.pm.PackageInfo; |
+import android.content.pm.Signature; |
+import android.content.res.AssetManager; |
+ |
+import org.chromium.base.test.util.Feature; |
+import org.chromium.testing.local.LocalRobolectricTestRunner; |
+ |
+import org.junit.Assert; |
+import org.junit.Before; |
+import org.junit.Test; |
+import org.junit.runner.RunWith; |
+ |
+import org.robolectric.annotation.Config; |
+import org.robolectric.res.builder.RobolectricPackageManager; |
+ |
+import java.security.MessageDigest; |
+import java.security.NoSuchAlgorithmException; |
+import java.util.HashSet; |
+import java.util.List; |
+import java.util.Arrays; |
+import java.util.Collections; |
+ |
+import android.content.res.Resources.NotFoundException; |
+import android.content.res.Resources; |
+ |
+import org.json.JSONArray; |
+import org.json.JSONException; |
+import org.json.JSONObject; |
+ |
+import org.chromium.components.installedapp.AssociationVerifier; |
+ |
+/** |
+ * Verify no regressions in gamepad mappings. |
+ */ |
+@RunWith(LocalRobolectricTestRunner.class) |
+@Config(manifest = Config.NONE) |
+public class InstalledAppProviderTest { |
+ private TestPackageManager pm; |
+ private final String sha256signature = |
+ "08:61:64:04:3C:E2:02:F2:4C:F6:02:F9:21:9F:2D:88:D0:D7:B6:58:8B:FF:FA:9E:CC:C9:A5:92:08:D1:E9:F3"; |
+ private final String sha256signaturebytes = |
+ "1a2fc26dc7ea5a2a4748b7cb2b1ef193d96ab2c99f93092f69e63075b28d1278"; |
+ private final String packageName = "com.real.package"; |
+ |
+ public class TestPackageManager extends RobolectricPackageManager { |
+ Resources testResources; |
+ |
+ @Override |
+ public Resources getResourcesForApplication(String appPackageName) |
+ throws NameNotFoundException { |
+ return testResources; |
+ } |
+ |
+ public void setResourcesForTest(Resources resources) { |
+ testResources = resources; |
+ } |
+ } |
+ |
+ public class FakeResources extends Resources { |
+ String[] testArray; |
+ public FakeResources(String[] array) { |
+ super(new AssetManager(), null, null); |
+ testArray = array; |
+ } |
+ |
+ @Override |
+ public int getIdentifier(String name, String defType, String defPackage) { |
+ if (testArray == null) { |
+ return 0; |
+ } |
+ return 1; |
+ } |
+ |
+ @Override |
+ public String[] getStringArray(int identifier) { |
+ return testArray; |
+ } |
+ } |
+ |
+ @Before |
+ public void setUp() { |
+ pm = new TestPackageManager(); |
+ PackageInfo info = new PackageInfo(); |
+ info.packageName = packageName; |
+ info.signatures = new Signature[] {new Signature(sha256signaturebytes)}; |
+ pm.addPackage(info); |
+ } |
+ |
+ @Test |
+ @Feature({"InstalledApp"}) |
+ public void testVerifyRealSignature() throws Exception { |
+ // PackageInfo stuff = pm.getPackageInfo("com.not.a.real.package", |
+ // PackageManager.GET_SIGNATURES); |
+ // System.out.println(computeNormalizedSha256Fingerprint(stuff.signatures[0].toByteArray())); |
+ Assert.assertTrue( |
+ AssociationVerifier.verifyCertFingerprints(packageName, sha256signature, pm)); |
+ } |
+ |
+ @Test |
+ @Feature({"InstalledApp"}) |
+ public void testVerifyFakeApplication() throws Exception { |
+ Assert.assertFalse(AssociationVerifier.verifyCertFingerprints( |
+ "com.fake.package", sha256signature, pm)); |
+ } |
+ |
+ @Test |
+ @Feature({"InstalledApp"}) |
+ public void testVerifyRealApplicationWrongSignature() throws Exception { |
+ Assert.assertFalse(AssociationVerifier.verifyCertFingerprints( |
+ packageName, "no:pe:th:is:is:no:tr:ea:l0", pm)); |
+ } |
+ |
+ @Test |
+ @Feature({"InstalledApp"}) |
+ public void testVerifyRealApplicationEmptySignature() throws Exception { |
+ Assert.assertFalse(AssociationVerifier.verifyCertFingerprints(packageName, "", pm)); |
+ } |
+ |
+ @Test |
+ @Feature({"InstalledApp"}) |
+ public void testVerifyApplicationWithNoAssociations() throws Exception { |
+ Assert.assertFalse(AssociationVerifier.isOriginAssociatedWithPackage( |
+ packageName, "localhost:8000", pm)); |
+ } |
+ |
+ @Test |
+ @Feature({"InstalledApp"}) |
+ public void testVerifyOriginInValidAssociations() throws Exception { |
+ String yeah = |
+ "{\"relation\": [\"delegate_permission/common.handle_all_urls\"],\"target\": {\"namespace\": \"web\", \"site\": \"http://localhost:8000/\"}}"; |
+ FakeResources fr = new FakeResources(new String[] {yeah}); |
+ pm.setResourcesForTest(fr); |
+ Assert.assertTrue(AssociationVerifier.isOriginAssociatedWithPackage( |
+ packageName, "http://localhost:8000/", pm)); |
+ } |
+ |
+ @Test |
+ @Feature({"InstalledApp"}) |
+ public void testVerifyOriginNotInAssociations() throws Exception { |
+ String yeah = |
+ "{\"relation\": [\"delegate_permission/common.handle_all_urls\"],\"target\": {\"namespace\": \"web\", \"site\": \"http://localhost:8000/\"}}"; |
+ FakeResources fr = new FakeResources(new String[] {yeah}); |
+ pm.setResourcesForTest(fr); |
+ Assert.assertFalse(AssociationVerifier.isOriginAssociatedWithPackage( |
+ packageName, "http://localhost:8001/", pm)); |
+ } |
+} |