Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(292)

Side by Side Diff: content/public/android/junit/src/org/chromium/content/browser/installedapp/InstalledAppProviderTest.java

Issue 2802603002: getInstalledRelatedApps: Introduce random delay to stop timing attacks. (Closed)
Patch Set: Rebase. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.content.browser.installedapp; 5 package org.chromium.content.browser.installedapp;
6 6
7 import android.content.Context;
7 import android.content.pm.ApplicationInfo; 8 import android.content.pm.ApplicationInfo;
8 import android.content.pm.PackageManager; 9 import android.content.pm.PackageManager;
9 import android.content.pm.PackageManager.NameNotFoundException; 10 import android.content.pm.PackageManager.NameNotFoundException;
10 import android.content.res.AssetManager; 11 import android.content.res.AssetManager;
11 import android.content.res.Resources; 12 import android.content.res.Resources;
12 import android.os.Bundle; 13 import android.os.Bundle;
13 14
14 import org.junit.Assert; 15 import org.junit.Assert;
15 import org.junit.Before; 16 import org.junit.Before;
16 import org.junit.Test; 17 import org.junit.Test;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 private static final String ORIGIN_SYNTAX_ERROR = "https:{"; 58 private static final String ORIGIN_SYNTAX_ERROR = "https:{";
58 private static final String ORIGIN_MISSING_SCHEME = "path/only"; 59 private static final String ORIGIN_MISSING_SCHEME = "path/only";
59 private static final String ORIGIN_MISSING_HOST = "file:///path/piece"; 60 private static final String ORIGIN_MISSING_HOST = "file:///path/piece";
60 private static final String ORIGIN_MISSING_PORT = "http://example.com"; 61 private static final String ORIGIN_MISSING_PORT = "http://example.com";
61 private static final String ORIGIN_DIFFERENT_SCHEME = "http://example.com:80 00"; 62 private static final String ORIGIN_DIFFERENT_SCHEME = "http://example.com:80 00";
62 private static final String ORIGIN_DIFFERENT_HOST = "https://example.org:800 0"; 63 private static final String ORIGIN_DIFFERENT_HOST = "https://example.org:800 0";
63 private static final String ORIGIN_DIFFERENT_PORT = "https://example.com:800 1"; 64 private static final String ORIGIN_DIFFERENT_PORT = "https://example.com:800 1";
64 65
65 private FakePackageManager mPackageManager; 66 private FakePackageManager mPackageManager;
66 private FakeFrameUrlDelegate mFrameUrlDelegate; 67 private FakeFrameUrlDelegate mFrameUrlDelegate;
67 private InstalledAppProviderImpl mInstalledAppProvider; 68 private InstalledAppProviderTestImpl mInstalledAppProvider;
69
70 private static class InstalledAppProviderTestImpl extends InstalledAppProvid erImpl {
71 private long mLastDelayMillis;
72
73 public InstalledAppProviderTestImpl(FrameUrlDelegate frameUrlDelegate, C ontext context) {
74 super(frameUrlDelegate, context);
75 }
76
77 public long getLastDelayMillis() {
78 return mLastDelayMillis;
79 }
80
81 @Override
82 protected void delayThenRun(Runnable r, long delayMillis) {
83 mLastDelayMillis = delayMillis;
84 r.run();
85 }
86 }
68 87
69 /** 88 /**
70 * FakePackageManager allows for the "installation" of Android package names and setting up 89 * FakePackageManager allows for the "installation" of Android package names and setting up
71 * Resources for installed packages. 90 * Resources for installed packages.
72 */ 91 */
73 private static class FakePackageManager extends DefaultPackageManager { 92 private static class FakePackageManager extends DefaultPackageManager {
74 private final HashMap<String, Bundle> mMetaDataMap; 93 private final HashMap<String, Bundle> mMetaDataMap;
75 private final HashMap<String, Resources> mResourceMap; 94 private final HashMap<String, Resources> mResourceMap;
76 95
77 public FakePackageManager() { 96 public FakePackageManager() {
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 } 291 }
273 }); 292 });
274 } 293 }
275 294
276 @Before 295 @Before
277 public void setUp() { 296 public void setUp() {
278 mPackageManager = new FakePackageManager(); 297 mPackageManager = new FakePackageManager();
279 RuntimeEnvironment.setRobolectricPackageManager(mPackageManager); 298 RuntimeEnvironment.setRobolectricPackageManager(mPackageManager);
280 mFrameUrlDelegate = new FakeFrameUrlDelegate(URL_ON_ORIGIN); 299 mFrameUrlDelegate = new FakeFrameUrlDelegate(URL_ON_ORIGIN);
281 mInstalledAppProvider = 300 mInstalledAppProvider =
282 new InstalledAppProviderImpl(mFrameUrlDelegate, RuntimeEnvironme nt.application); 301 new InstalledAppProviderTestImpl(mFrameUrlDelegate, RuntimeEnvir onment.application);
283 } 302 }
284 303
285 /** 304 /**
286 * Origin of the page using the API is missing certain parts of the URI. 305 * Origin of the page using the API is missing certain parts of the URI.
287 */ 306 */
288 @Test 307 @Test
289 @Feature({"InstalledApp"}) 308 @Feature({"InstalledApp"})
290 public void testOriginMissingParts() { 309 public void testOriginMissingParts() {
291 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] { 310 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
292 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) }; 311 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 createRelatedApplication(PLATFORM_OTHER, PACKAGE_NAME_2, null), 802 createRelatedApplication(PLATFORM_OTHER, PACKAGE_NAME_2, null),
784 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_3, null) }; 803 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_3, null) };
785 804
786 setAssetStatement(PACKAGE_NAME_2, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN); 805 setAssetStatement(PACKAGE_NAME_2, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
787 setAssetStatement(PACKAGE_NAME_3, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN); 806 setAssetStatement(PACKAGE_NAME_3, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
788 807
789 RelatedApplication[] expectedInstalledRelatedApps = 808 RelatedApplication[] expectedInstalledRelatedApps =
790 new RelatedApplication[] {manifestRelatedApps[1], manifestRelate dApps[3]}; 809 new RelatedApplication[] {manifestRelatedApps[1], manifestRelate dApps[3]};
791 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps); 810 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
792 } 811 }
812
813 /**
814 * Tests the pseudo-random artificial delay to counter a timing attack.
815 */
816 @Test
817 @Feature({"InstalledApp"})
818 public void testArtificialDelay() {
819 byte[] salt = {0x64, 0x09, -0x68, -0x25, 0x70, 0x11, 0x25, 0x24, 0x68, - 0x1a, 0x08, 0x79,
820 -0x12, -0x50, 0x3b, -0x57, -0x17, -0x4d, 0x46, 0x02};
821 PackageHash.setGlobalSaltForTesting(salt);
822 setAssetStatement(PACKAGE_NAME_1, NAMESPACE_WEB, RELATION_HANDLE_ALL_URL S, ORIGIN);
823
824 // Installed app.
825 RelatedApplication manifestRelatedApps[] = new RelatedApplication[] {
826 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_1, null) };
827 RelatedApplication[] expectedInstalledRelatedApps = manifestRelatedApps;
828 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
829 // This expectation is based on HMAC_SHA256(salt, packageName encoded in UTF-8), taking the
830 // low 10 bits of the first two bytes of the result / 100.
831 Assert.assertEquals(2, mInstalledAppProvider.getLastDelayMillis());
832
833 // Non-installed app.
834 manifestRelatedApps = new RelatedApplication[] {
835 createRelatedApplication(PLATFORM_ANDROID, PACKAGE_NAME_2, null) };
836 expectedInstalledRelatedApps = new RelatedApplication[] {};
837 verifyInstalledApps(manifestRelatedApps, expectedInstalledRelatedApps);
838 // This expectation is based on HMAC_SHA256(salt, packageName encoded in UTF-8), taking the
839 // low 10 bits of the first two bytes of the result / 100.
840 Assert.assertEquals(5, mInstalledAppProvider.getLastDelayMillis());
841 }
793 } 842 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698