2019-10-30 19:08:45 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix
|
|
|
|
|
2019-12-17 07:03:11 +01:00
|
|
|
import android.net.ConnectivityManager
|
|
|
|
import androidx.core.content.getSystemService
|
2019-10-30 19:08:45 +01:00
|
|
|
import assertk.assertThat
|
|
|
|
import assertk.assertions.isEqualTo
|
2019-12-17 07:03:11 +01:00
|
|
|
import io.mockk.every
|
2019-10-30 19:08:45 +01:00
|
|
|
import io.mockk.mockk
|
2019-12-17 07:03:11 +01:00
|
|
|
import io.mockk.mockkStatic
|
2019-10-30 19:08:45 +01:00
|
|
|
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
|
|
|
import mozilla.components.browser.errorpages.ErrorPages
|
|
|
|
import mozilla.components.browser.errorpages.ErrorType
|
|
|
|
import mozilla.components.concept.engine.request.RequestInterceptor
|
|
|
|
import mozilla.components.support.test.robolectric.testContext
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.runner.RunWith
|
2020-01-22 21:53:26 +01:00
|
|
|
import org.mozilla.fenix.AppRequestInterceptor.Companion.HIGH_RISK_ERROR_PAGES
|
|
|
|
import org.mozilla.fenix.AppRequestInterceptor.Companion.LOW_AND_MEDIUM_RISK_ERROR_PAGES
|
2019-12-17 07:03:11 +01:00
|
|
|
import org.mozilla.fenix.ext.isOnline
|
2019-10-30 19:08:45 +01:00
|
|
|
import org.robolectric.RobolectricTestRunner
|
|
|
|
import org.robolectric.annotation.Config
|
|
|
|
|
|
|
|
@UseExperimental(ObsoleteCoroutinesApi::class)
|
|
|
|
@RunWith(RobolectricTestRunner::class)
|
|
|
|
@Config(application = TestApplication::class)
|
|
|
|
class AppRequestInterceptorTest {
|
|
|
|
|
|
|
|
private lateinit var interceptor: RequestInterceptor
|
|
|
|
|
|
|
|
@Before
|
|
|
|
fun setUp() {
|
2019-12-17 07:03:11 +01:00
|
|
|
|
|
|
|
mockkStatic("org.mozilla.fenix.ext.ConnectivityManagerKt")
|
|
|
|
|
|
|
|
every { testContext.getSystemService<ConnectivityManager>()!!.isOnline() } returns true
|
|
|
|
|
2019-10-30 19:08:45 +01:00
|
|
|
interceptor = AppRequestInterceptor(testContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `onErrorRequest results in correct error page for low risk level error`() {
|
|
|
|
setOf(
|
|
|
|
ErrorType.UNKNOWN,
|
|
|
|
ErrorType.ERROR_NET_INTERRUPT,
|
|
|
|
ErrorType.ERROR_NET_TIMEOUT,
|
|
|
|
ErrorType.ERROR_CONNECTION_REFUSED,
|
|
|
|
ErrorType.ERROR_UNKNOWN_SOCKET_TYPE,
|
|
|
|
ErrorType.ERROR_REDIRECT_LOOP,
|
|
|
|
ErrorType.ERROR_OFFLINE,
|
|
|
|
ErrorType.ERROR_NET_RESET,
|
|
|
|
ErrorType.ERROR_UNSAFE_CONTENT_TYPE,
|
|
|
|
ErrorType.ERROR_CORRUPTED_CONTENT,
|
|
|
|
ErrorType.ERROR_CONTENT_CRASHED,
|
|
|
|
ErrorType.ERROR_INVALID_CONTENT_ENCODING,
|
|
|
|
ErrorType.ERROR_UNKNOWN_HOST,
|
|
|
|
ErrorType.ERROR_MALFORMED_URI,
|
|
|
|
ErrorType.ERROR_FILE_NOT_FOUND,
|
|
|
|
ErrorType.ERROR_FILE_ACCESS_DENIED,
|
|
|
|
ErrorType.ERROR_PROXY_CONNECTION_REFUSED,
|
|
|
|
ErrorType.ERROR_UNKNOWN_PROXY_HOST,
|
|
|
|
ErrorType.ERROR_UNKNOWN_PROTOCOL
|
|
|
|
).forEach { error ->
|
|
|
|
val actualPage = createActualErrorPage(error)
|
|
|
|
val expectedPage = createExpectedErrorPage(
|
|
|
|
error = error,
|
2020-01-22 21:53:26 +01:00
|
|
|
html = LOW_AND_MEDIUM_RISK_ERROR_PAGES
|
2019-10-30 19:08:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(actualPage).isEqualTo(expectedPage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `onErrorRequest results in correct error page for medium risk level error`() {
|
|
|
|
setOf(
|
|
|
|
ErrorType.ERROR_SECURITY_BAD_CERT,
|
|
|
|
ErrorType.ERROR_SECURITY_SSL,
|
|
|
|
ErrorType.ERROR_PORT_BLOCKED
|
|
|
|
).forEach { error ->
|
|
|
|
val actualPage = createActualErrorPage(error)
|
|
|
|
val expectedPage = createExpectedErrorPage(
|
|
|
|
error = error,
|
2020-01-22 21:53:26 +01:00
|
|
|
html = LOW_AND_MEDIUM_RISK_ERROR_PAGES
|
2019-10-30 19:08:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(actualPage).isEqualTo(expectedPage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `onErrorRequest results in correct error page for high risk level error`() {
|
|
|
|
setOf(
|
|
|
|
ErrorType.ERROR_SAFEBROWSING_HARMFUL_URI,
|
|
|
|
ErrorType.ERROR_SAFEBROWSING_MALWARE_URI,
|
|
|
|
ErrorType.ERROR_SAFEBROWSING_PHISHING_URI,
|
|
|
|
ErrorType.ERROR_SAFEBROWSING_UNWANTED_URI
|
|
|
|
).forEach { error ->
|
|
|
|
val actualPage = createActualErrorPage(error)
|
|
|
|
val expectedPage = createExpectedErrorPage(
|
|
|
|
error = error,
|
2020-01-22 21:53:26 +01:00
|
|
|
html = HIGH_RISK_ERROR_PAGES
|
2019-10-30 19:08:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(actualPage).isEqualTo(expectedPage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createActualErrorPage(error: ErrorType): String {
|
2020-01-22 18:21:20 +01:00
|
|
|
val errorPage = interceptor.onErrorRequest(session = mockk(), errorType = error, uri = null)
|
2020-01-22 21:53:26 +01:00
|
|
|
as RequestInterceptor.ErrorResponse.Uri
|
|
|
|
return errorPage.uri
|
2019-10-30 19:08:45 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:53:26 +01:00
|
|
|
private fun createExpectedErrorPage(error: ErrorType, html: String): String {
|
|
|
|
return ErrorPages.createUrlEncodedErrorPage(
|
|
|
|
context = testContext,
|
|
|
|
errorType = error,
|
|
|
|
htmlResource = html
|
|
|
|
)
|
2019-10-30 19:08:45 +01:00
|
|
|
}
|
|
|
|
}
|