Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
/*
* Copyright (c) 2018 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.browser

import android.net.Uri
import android.support.test.InstrumentationRegistry
import android.support.test.annotation.UiThreadTest
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import com.duckduckgo.app.surrogates.ResourceSurrogates
import com.duckduckgo.app.surrogates.SurrogateResponse
import com.duckduckgo.app.httpsupgrade.HttpsUpgrader
import com.duckduckgo.app.trackerdetection.TrackerDetector
import com.duckduckgo.app.trackerdetection.model.TrackingEvent
import com.nhaarman.mockito_kotlin.*
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import org.mockito.MockitoAnnotations

class WebViewRequestInterceptorTest {

private lateinit var testee: WebViewRequestInterceptor

@Mock
private lateinit var mockTrackerDetector: TrackerDetector
@Mock
private lateinit var mockHttpsUpgrader: HttpsUpgrader
@Mock
private lateinit var mockResourceSurrogates: ResourceSurrogates
@Mock
private lateinit var mockRequest: WebResourceRequest

private lateinit var webView: WebView

@UiThreadTest
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to be mixing and matching approaches. Nothing to do here, but we should all perhaps have a chat about what the standard approach should be (I prefer just creating them directly without annotations or using initMocks personally).


testee = WebViewRequestInterceptor(
trackerDetector = mockTrackerDetector,
httpsUpgrader = mockHttpsUpgrader,
resourceSurrogates = mockResourceSurrogates
)

val context = InstrumentationRegistry.getTargetContext()

webView = WebView(context)
}

@Test
fun whenUrlShouldBeUpgradedThenUpgraderInvoked() {
configureShouldUpgrade()
testee.shouldIntercept(
request = mockRequest,
currentUrl = null,
webView = webView,
webViewClientListener = null)

verify(mockHttpsUpgrader).upgrade(any())
}

@Test
fun whenUrlShouldBeUpgradedThenCancelledResponseReturned() {
configureShouldUpgrade()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = null,
webView = webView,
webViewClientListener = null)

assertCancelledResponse(response)
}

@Test
fun whenUrlShouldBeUpgradedButNotOnMainFrameThenNotUpgraded() {
configureShouldUpgrade()
whenever(mockRequest.isForMainFrame).thenReturn(false)
testee.shouldIntercept(
request = mockRequest,
currentUrl = null,
webView = webView,
webViewClientListener = null)

verify(mockHttpsUpgrader, never()).upgrade(any())
}

@Test
fun whenUrlShouldBeUpgradedButUrlIsNullThenNotUpgraded() {
configureShouldUpgrade()
whenever(mockRequest.url).thenReturn(null)
testee.shouldIntercept(
request = mockRequest,
currentUrl = null,
webView = webView,
webViewClientListener = null)

verify(mockHttpsUpgrader, never()).upgrade(any())
}

@Test
fun whenUrlShouldNotBeUpgradedThenUpgraderNotInvoked() {
whenever(mockHttpsUpgrader.shouldUpgrade(any())).thenReturn(false)
testee.shouldIntercept(
request = mockRequest,
currentUrl = null,
webView = webView,
webViewClientListener = null)

verify(mockHttpsUpgrader, never()).upgrade(any())
}

@Test
fun whenCurrentUrlIsNullThenShouldContinueToLoad() {
configureShouldNotUpgrade()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = null,
webView = webView,
webViewClientListener = null)
assertRequestCanContinueToLoad(response)
}

@Test
fun whenIsTrustedSite_DuckDuckGo_ThenShouldContinueToLoad() {
configureShouldNotUpgrade()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "duckduckgo.com/a/b/c?q=123",
webView = webView,
webViewClientListener = null)

assertRequestCanContinueToLoad(response)
}

@Test
fun whenIsTrustedSite_DontTrack_ThenShouldContinueToLoad() {
configureShouldNotUpgrade()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "donttrack.us/a/b/c?q=123",
webView = webView,
webViewClientListener = null)

assertRequestCanContinueToLoad(response)
}

@Test
fun whenIsTrustedSite_SpreadPrivacy_ThenShouldContinueToLoad() {
configureShouldNotUpgrade()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "spreadprivacy.com/a/b/c?q=123",
webView = webView,
webViewClientListener = null)

assertRequestCanContinueToLoad(response)
}

@Test
fun whenIsTrustedSite_DuckDuckHack_ThenShouldContinueToLoad() {
configureShouldNotUpgrade()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "duckduckhack.com/a/b/c?q=123",
webView = webView,
webViewClientListener = null)

assertRequestCanContinueToLoad(response)
}

@Test
fun whenIsTrustedSite_PrivateBrowsingMyths_ThenShouldContinueToLoad() {
configureShouldNotUpgrade()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "privatebrowsingmyths.com/a/b/c?q=123",
webView = webView,
webViewClientListener = null)

assertRequestCanContinueToLoad(response)
}

@Test
fun whenIsTrustedSite_DuckDotCo_ThenShouldContinueToLoad() {
configureShouldNotUpgrade()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "duck.co/a/b/c?q=123",
webView = webView,
webViewClientListener = null)

assertRequestCanContinueToLoad(response)
}

@Test
fun whenIsHttpRequestThenHttpRequestListenerCalled() {
configureShouldNotUpgrade()
whenever(mockRequest.url).thenReturn(Uri.parse("http://example.com"))
val mockListener = mock<WebViewClientListener>()

val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "foo.com",
webView = webView,
webViewClientListener = mockListener)

verify(mockListener).pageHasHttpResources(anyString())
assertRequestCanContinueToLoad(response)
}

@Test
fun whenIsHttpsRequestThenHttpRequestListenerNotCalled() {
configureShouldNotUpgrade()
whenever(mockRequest.url).thenReturn(Uri.parse("https://example.com"))
val mockListener = mock<WebViewClientListener>()

val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "foo.com",
webView = webView,
webViewClientListener = mockListener)

verify(mockListener, never()).pageHasHttpResources(anyString())
assertRequestCanContinueToLoad(response)
}


@Test
fun whenRequestShouldBlockAndNoSurrogateThenCancellingResponseReturned() {
whenever(mockResourceSurrogates.get(any())).thenReturn(SurrogateResponse(responseAvailable = false))

configureShouldNotUpgrade()
configureShouldBlock()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "foo.com",
webView = webView,
webViewClientListener = null)

assertCancelledResponse(response)
}

@Test
fun whenRequestShouldBlockButThereIsASurrogateThen() {
val availableSurrogate = SurrogateResponse(
responseAvailable = true,
mimeType = "application/javascript",
jsFunction = "javascript replacement function goes here")
whenever(mockResourceSurrogates.get(any())).thenReturn(availableSurrogate)

configureShouldNotUpgrade()
configureShouldBlock()
val response = testee.shouldIntercept(
request = mockRequest,
currentUrl = "foo.com",
webView = webView,
webViewClientListener = null)

assertEquals(availableSurrogate.jsFunction.byteInputStream().read(), response!!.data.read())
}

private fun assertRequestCanContinueToLoad(response: WebResourceResponse?) {
assertNull(response)
}

private fun configureShouldBlock() {
val blockTrackingEvent = TrackingEvent(blocked = true,
documentUrl = "",
trackerUrl = "",
trackerNetwork = null)
whenever(mockRequest.isForMainFrame).thenReturn(false)
whenever(mockTrackerDetector.evaluate(any(), any(), any())).thenReturn(blockTrackingEvent)
}

private fun configureShouldUpgrade() {
whenever(mockHttpsUpgrader.shouldUpgrade(any())).thenReturn(true)
whenever(mockRequest.url).thenReturn(validUri())
whenever(mockRequest.isForMainFrame).thenReturn(true)
}

private fun configureShouldNotUpgrade() {
whenever(mockHttpsUpgrader.shouldUpgrade(any())).thenReturn(false)
whenever(mockRequest.url).thenReturn(validUri())
whenever(mockRequest.isForMainFrame).thenReturn(true)
}

private fun validUri() = Uri.parse("example.com")

private fun assertCancelledResponse(response: WebResourceResponse?) {
assertNotNull(response)
assertNull(response!!.data)
assertNull(response.mimeType)
assertNull(response.encoding)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HttpsUpgraderTest {
@Before
fun before() {
mockDao = mock()
testee = HttpsUpgrader(mockDao)
testee = HttpsUpgraderImpl(mockDao)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The great implementation name debate. But nothing to change here, another one to discuss probably.

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import android.net.Uri
import android.support.test.InstrumentationRegistry
import com.duckduckgo.app.global.db.AppDatabase
import com.duckduckgo.app.httpsupgrade.HttpsUpgrader
import com.duckduckgo.app.httpsupgrade.HttpsUpgraderImpl
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Before
Expand All @@ -55,7 +56,7 @@ class HttpsUpgraderPerformanceTest {
fun setup() {
db = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), AppDatabase::class.java).build()
dao = db.httpsUpgradeDomainDao()
httpsUpgrader = HttpsUpgrader(dao)
httpsUpgrader = HttpsUpgraderImpl(dao)
}

@Test
Expand Down
Loading