OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.android_webview.test; | |
6 | |
7 import android.content.Context; | |
8 import android.content.SharedPreferences; | |
9 import android.graphics.Bitmap; | |
10 import android.graphics.Color; | |
11 import android.support.test.filters.SmallTest; | |
12 | |
13 import org.chromium.android_webview.AwBrowserContext; | |
14 import org.chromium.android_webview.AwContents; | |
15 import org.chromium.android_webview.test.util.GraphicsTestUtils; | |
16 import org.chromium.base.test.util.Feature; | |
17 import org.chromium.base.test.util.InMemorySharedPreferences; | |
18 import org.chromium.components.safe_browsing.SafeBrowsingApiBridge; | |
19 import org.chromium.components.safe_browsing.SafeBrowsingApiHandler; | |
20 import org.chromium.net.test.EmbeddedTestServer; | |
21 | |
22 import java.util.concurrent.Callable; | |
23 | |
24 /** | |
25 * Test suite for SafeBrowsing. | |
26 * | |
27 * Ensures that interstitials can be successfully created for malicous pages. | |
28 */ | |
29 public class SafeBrowsingTest extends AwTestBase { | |
30 private TestAwContentsClient mContentsClient; | |
31 private AwTestContainerView mContainerView; | |
32 private AwContents mAwContents; | |
33 | |
34 private EmbeddedTestServer mTestServer; | |
35 | |
36 // The background color of the safebrowsing interstitial HTML | |
37 // See components/security_interstitials/core/browser/resources/interstitial _v2.css | |
38 private static final int INTERSTITIAL_COLOR = Color.rgb(206, 52, 38); | |
39 | |
40 private static final String RESOURCE_PATH = "/android_webview/test/data"; | |
41 private static final String SAFE_HTML_PATH = RESOURCE_PATH + "/safe.html"; | |
42 private static final String MALWARE_HTML_PATH = RESOURCE_PATH + "/malware.ht ml"; | |
43 private static final String IFRAME_HTML_PATH = RESOURCE_PATH + "/iframe.html "; | |
44 | |
45 /** | |
46 * A fake SafeBrowsingApiHandler which treats URLs ending in MALWARE_HTML_PA TH as malicious URLs | |
47 * that should be blocked. | |
48 */ | |
49 public static class MockSafeBrowsingApiHandler implements SafeBrowsingApiHan dler { | |
50 private Observer mObserver; | |
51 private static final String SAFE_METADATA = "{}"; | |
52 private static final String MALWARE_METADATA = "{\"matches\":[{\"threat_ type\":\"5\"}]}"; | |
53 | |
54 @Override | |
55 public boolean init(Context context, Observer result) { | |
56 mObserver = result; | |
57 return true; | |
58 } | |
59 | |
60 @Override | |
61 public void startUriLookup(long callbackId, String uri, int[] threatsOfI nterest) { | |
62 int resultStatus = STATUS_SUCCESS; | |
63 String metadata = isMaliciousUrl(uri) ? MALWARE_METADATA : SAFE_META DATA; | |
64 | |
65 mObserver.onUrlCheckDone(callbackId, resultStatus, metadata); | |
66 } | |
67 | |
68 private static boolean isMaliciousUrl(String uri) { | |
69 return uri.endsWith(MALWARE_HTML_PATH); | |
70 } | |
71 } | |
72 | |
73 /** | |
74 * A fake AwBrowserContext which loads the MockSafeBrowsingApiHandler instea d of the real one. | |
75 */ | |
76 private static class MockAwBrowserContext extends AwBrowserContext { | |
77 public MockAwBrowserContext( | |
78 SharedPreferences sharedPreferences, Context applicationContext) { | |
79 super(sharedPreferences, applicationContext); | |
80 SafeBrowsingApiBridge.setSafeBrowsingHandlerType(MockSafeBrowsingApi Handler.class); | |
81 } | |
82 } | |
83 | |
84 @Override | |
85 public void setUp() throws Exception { | |
86 super.setUp(); | |
87 mContentsClient = new TestAwContentsClient(); | |
88 mContainerView = createAwTestContainerViewOnMainSync(mContentsClient); | |
89 mAwContents = mContainerView.getAwContents(); | |
90 | |
91 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation ().getContext()); | |
92 } | |
93 | |
94 @Override | |
95 public void tearDown() throws Exception { | |
96 mTestServer.stopAndDestroyServer(); | |
97 super.tearDown(); | |
98 } | |
99 | |
100 /** | |
101 * Creates a special BrowserContext that has a safebrowsing api handler whic h always says | |
102 * sites are malicious | |
103 */ | |
104 @Override | |
105 protected void createAwBrowserContextHelper( | |
106 InMemorySharedPreferences prefs, Context appContext) { | |
107 setBrowserContext(new MockAwBrowserContext(prefs, appContext)); | |
108 } | |
109 | |
110 private boolean isShowingInterstitial() { | |
111 Bitmap bitmap = GraphicsTestUtils.drawAwContentsOnUiThread( | |
112 mAwContents, mContainerView.getWidth(), mContainerView.getHeight ()); | |
113 return (bitmap.getPixel(0, 0) == INTERSTITIAL_COLOR); | |
114 } | |
115 | |
116 private void waitForInterstitial() throws Exception { | |
117 pollUiThread(new Callable<Boolean>() { | |
boliu
2017/02/14 18:31:40
Always prefer the CallbackHelper pattern, which ju
Nate Fischer
2017/02/14 19:42:55
Ack. If I've understood you correctly, polling is
boliu
2017/02/14 22:26:57
Yep that's all correct
| |
118 @Override | |
119 public Boolean call() throws Exception { | |
120 return isShowingInterstitial(); | |
121 } | |
122 }); | |
123 } | |
124 | |
125 @SmallTest | |
126 @Feature({"AndroidWebView"}) | |
127 public void testSafeBrowsingDoesNotBlockSafePages() throws Throwable { | |
128 final String responseUrl = mTestServer.getURL(SAFE_HTML_PATH); | |
129 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), resp onseUrl); | |
130 assertFalse("Should not create an interstitial", isShowingInterstitial() ); | |
131 } | |
132 | |
133 @SmallTest | |
134 @Feature({"AndroidWebView"}) | |
135 public void testSafeBrowsingShowsInterstitialForMalware() throws Throwable { | |
136 final String responseUrl = mTestServer.getURL(MALWARE_HTML_PATH); | |
137 loadUrlAsync(mAwContents, responseUrl); | |
138 waitForInterstitial(); | |
139 } | |
140 | |
141 @SmallTest | |
142 @Feature({"AndroidWebView"}) | |
143 public void testSafeBrowsingMaliciousSubresourceShowsInterstitial() throws T hrowable { | |
144 final String responseUrl = mTestServer.getURL(IFRAME_HTML_PATH); | |
145 loadUrlAsync(mAwContents, responseUrl); | |
146 waitForInterstitial(); | |
147 } | |
148 } | |
OLD | NEW |