Skip to content

Commit

Permalink
For issue mozilla-mobile#12796: Ensure Cookie purging is only active …
Browse files Browse the repository at this point in the history
…in nightly or debug.
  • Loading branch information
Amejia481 committed Aug 14, 2020
1 parent 7f9ddfe commit 45965d1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package org.mozilla.fenix.components

import androidx.annotation.VisibleForTesting
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicyForSessionTypes
import org.mozilla.fenix.Config
import org.mozilla.fenix.utils.Settings

/**
Expand Down Expand Up @@ -34,17 +37,18 @@ class TrackingProtectionPolicyFactory(private val settings: Settings) {
}

return when {
normalMode && privateMode -> trackingProtectionPolicy
normalMode && !privateMode -> trackingProtectionPolicy.forRegularSessionsOnly()
!normalMode && privateMode -> trackingProtectionPolicy.forPrivateSessionsOnly()
normalMode && privateMode -> trackingProtectionPolicy.adaptPolicyToChannel()
normalMode && !privateMode -> trackingProtectionPolicy.adaptPolicyToChannel().forRegularSessionsOnly()
!normalMode && privateMode -> trackingProtectionPolicy.adaptPolicyToChannel().forPrivateSessionsOnly()
else -> TrackingProtectionPolicy.none()
}
}

private fun createCustomTrackingProtectionPolicy(): TrackingProtectionPolicy {
return TrackingProtectionPolicy.select(
cookiePolicy = getCustomCookiePolicy(),
trackingCategories = getCustomTrackingCategories()
trackingCategories = getCustomTrackingCategories(),
cookiePurging = Config.channel.isNightlyOrDebug
).let {
if (settings.blockTrackingContentSelectionInCustomTrackingProtection == "private") {
it.forPrivateSessionsOnly()
Expand Down Expand Up @@ -91,3 +95,13 @@ class TrackingProtectionPolicyFactory(private val settings: Settings) {
return categories.toTypedArray()
}
}

@VisibleForTesting
internal fun TrackingProtectionPolicyForSessionTypes.adaptPolicyToChannel(): TrackingProtectionPolicyForSessionTypes {
return TrackingProtectionPolicy.select(
trackingCategories = trackingCategories,
cookiePolicy = cookiePolicy,
strictSocialTrackingProtection = strictSocialTrackingProtection,
cookiePurging = Config.channel.isNightlyOrDebug
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package org.mozilla.fenix.components

import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.Config
import org.mozilla.fenix.R
import org.mozilla.fenix.ReleaseChannel
import org.mozilla.fenix.utils.Settings
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner

Expand Down Expand Up @@ -87,6 +91,57 @@ class TrackingProtectionPolicyFactoryTest {
expected.assertPolicyEquals(always, checkPrivacy = false)
}

@Test
fun `cookiePurging must be available ONLY in nightly or debug`() {
mockkObject(Config)
for (channel in ReleaseChannel.values()) {
every { Config.channel } returns channel

val shouldCookiePurgingActive = channel.isNightlyOrDebug
val customSetting =
settingsForCustom(shouldBlockCookiesInCustom = true, blockCookiesSelection = "all")
val stringSetting = mockSettings(useStrict = true)
val recommendedSetting = mockSettings(useTrackingProtection = true)

for (setting in arrayOf(recommendedSetting, stringSetting, customSetting)) {
val factory = TrackingProtectionPolicyFactory(setting)

val privateOnly =
factory.createTrackingProtectionPolicy(normalMode = false, privateMode = true)
val normalOnly =
factory.createTrackingProtectionPolicy(normalMode = true, privateMode = false)
val always =
factory.createTrackingProtectionPolicy(normalMode = true, privateMode = true)

assertEquals(shouldCookiePurgingActive, privateOnly.cookiePurging)
assertEquals(shouldCookiePurgingActive, normalOnly.cookiePurging)
assertEquals(shouldCookiePurgingActive, always.cookiePurging)
}
}
}

@Test
fun `adaptPolicyToChannel MUST only update properties that have changed per give channel`() {
mockkObject(Config)

val policies = arrayOf(
TrackingProtectionPolicy.strict(), TrackingProtectionPolicy.recommended(),
TrackingProtectionPolicy.select()
)

for (channel in ReleaseChannel.values()) {
every { Config.channel } returns channel

val shouldCookiePurgingActive = channel.isNightlyOrDebug

for (policy in policies) {
val adaptedPolicy = policy.adaptPolicyToChannel()
policy.assertPolicyEquals(adaptedPolicy, checkPrivacy = false)
assertEquals(shouldCookiePurgingActive, adaptedPolicy.cookiePurging)
}
}
}

@Test
fun `GIVEN custom policy WHEN cookie policy social THEN tracking policy should have cookie policy allow non-trackers`() {
val expected = EngineSession.TrackingProtectionPolicy.select(
Expand Down

0 comments on commit 45965d1

Please sign in to comment.