summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ContentBlockingControllerTest.kt
blob: d0ad03a439c83d5c6bfe9a050fb4ea90af903914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// For ContentBlockingException
@file:Suppress("DEPRECATION")

package org.mozilla.geckoview.test

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import org.hamcrest.Matchers.* // ktlint-disable no-wildcard-imports
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.geckoview.ContentBlocking
import org.mozilla.geckoview.ContentBlocking.CookieBannerMode
import org.mozilla.geckoview.ContentBlockingController
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.AssertCalled
@RunWith(AndroidJUnit4::class)
@MediumTest
class ContentBlockingControllerTest : BaseSessionTest() {
    // Smoke test for safe browsing settings, most testing is through platform tests
    @Test
    fun safeBrowsingSettings() {
        val contentBlocking = sessionRule.runtime.settings.contentBlocking

        val google = contentBlocking.safeBrowsingProviders.first { it.name == "google" }
        val google4 = contentBlocking.safeBrowsingProviders.first { it.name == "google4" }

        // Let's make sure the initial value of safeBrowsingProviders is correct
        assertThat(
            "Expected number of default providers",
            contentBlocking.safeBrowsingProviders.size,
            equalTo(2),
        )
        assertThat("Google legacy provider is present", google, notNullValue())
        assertThat("Google provider is present", google4, notNullValue())

        // Checks that the default provider values make sense
        assertThat(
            "Default provider values are sensible",
            google.getHashUrl,
            containsString("/safebrowsing-dummy/"),
        )
        assertThat(
            "Default provider values are sensible",
            google.advisoryUrl,
            startsWith("https://developers.google.com/"),
        )
        assertThat(
            "Default provider values are sensible",
            google4.getHashUrl,
            containsString("/safebrowsing4-dummy/"),
        )
        assertThat(
            "Default provider values are sensible",
            google4.updateUrl,
            containsString("/safebrowsing4-dummy/"),
        )
        assertThat(
            "Default provider values are sensible",
            google4.dataSharingUrl,
            startsWith("https://safebrowsing.googleapis.com/"),
        )

        // Checks that the pref value is also consistent with the runtime settings
        val originalPrefs = sessionRule.getPrefs(
            "browser.safebrowsing.provider.google4.updateURL",
            "browser.safebrowsing.provider.google4.gethashURL",
            "browser.safebrowsing.provider.google4.lists",
        )

        assertThat(
            "Initial prefs value is correct",
            originalPrefs[0] as String,
            equalTo(google4.updateUrl),
        )
        assertThat(
            "Initial prefs value is correct",
            originalPrefs[1] as String,
            equalTo(google4.getHashUrl),
        )
        assertThat(
            "Initial prefs value is correct",
            originalPrefs[2] as String,
            equalTo(google4.lists.joinToString(",")),
        )

        // Makes sure we can override a default value
        val override = ContentBlocking.SafeBrowsingProvider
            .from(ContentBlocking.GOOGLE_SAFE_BROWSING_PROVIDER)
            .updateUrl("http://test-update-url.com")
            .getHashUrl("http://test-get-hash-url.com")
            .build()

        // ... and that we can add a custom provider
        val custom = ContentBlocking.SafeBrowsingProvider
            .withName("custom-provider")
            .updateUrl("http://test-custom-update-url.com")
            .getHashUrl("http://test-custom-get-hash-url.com")
            .lists("a", "b", "c")
            .build()

        assertThat(
            "Override value is correct",
            override.updateUrl,
            equalTo("http://test-update-url.com"),
        )
        assertThat(
            "Override value is correct",
            override.getHashUrl,
            equalTo("http://test-get-hash-url.com"),
        )

        assertThat(
            "Custom provider value is correct",
            custom.updateUrl,
            equalTo("http://test-custom-update-url.com"),
        )
        assertThat(
            "Custom provider value is correct",
            custom.getHashUrl,
            equalTo("http://test-custom-get-hash-url.com"),
        )
        assertThat(
            "Custom provider value is correct",
            custom.lists,
            equalTo(arrayOf("a", "b", "c")),
        )

        contentBlocking.setSafeBrowsingProviders(override, custom)

        val prefs = sessionRule.getPrefs(
            "browser.safebrowsing.provider.google4.updateURL",
            "browser.safebrowsing.provider.google4.gethashURL",
            "browser.safebrowsing.provider.custom-provider.updateURL",
            "browser.safebrowsing.provider.custom-provider.gethashURL",
            "browser.safebrowsing.provider.custom-provider.lists",
        )

        assertThat(
            "Pref value is set correctly",
            prefs[0] as String,
            equalTo("http://test-update-url.com"),
        )
        assertThat(
            "Pref value is set correctly",
            prefs[1] as String,
            equalTo("http://test-get-hash-url.com"),
        )
        assertThat(
            "Pref value is set correctly",
            prefs[2] as String,
            equalTo("http://test-custom-update-url.com"),
        )
        assertThat(
            "Pref value is set correctly",
            prefs[3] as String,
            equalTo("http://test-custom-get-hash-url.com"),
        )
        assertThat(
            "Pref value is set correctly",
            prefs[4] as String,
            equalTo("a,b,c"),
        )

        // Restore defaults
        contentBlocking.setSafeBrowsingProviders(google, google4)

        // Checks that after restoring the providers the prefs get updated
        val restoredPrefs = sessionRule.getPrefs(
            "browser.safebrowsing.provider.google4.updateURL",
            "browser.safebrowsing.provider.google4.gethashURL",
            "browser.safebrowsing.provider.google4.lists",
        )

        assertThat(
            "Restored prefs value is correct",
            restoredPrefs[0] as String,
            equalTo(originalPrefs[0]),
        )
        assertThat(
            "Restored prefs value is correct",
            restoredPrefs[1] as String,
            equalTo(originalPrefs[1]),
        )
        assertThat(
            "Restored prefs value is correct",
            restoredPrefs[2] as String,
            equalTo(originalPrefs[2]),
        )
    }

    @Test
    fun getLog() {
        val category = ContentBlocking.AntiTracking.TEST
        sessionRule.runtime.settings.contentBlocking.setAntiTracking(category)
        mainSession.settings.useTrackingProtection = true
        mainSession.loadTestPath(TRACKERS_PATH)

        sessionRule.waitUntilCalled(object : ContentBlocking.Delegate {
            @AssertCalled(count = 1)
            override fun onContentBlocked(
                session: GeckoSession,
                event: ContentBlocking.BlockEvent,
            ) {
            }
        })

        sessionRule.waitForResult(
            sessionRule.runtime.contentBlockingController.getLog(mainSession).accept {
                assertThat("Log must not be null", it, notNullValue())
                assertThat("Log must have at least one entry", it?.size, not(0))
                it?.forEach {
                    it.blockingData.forEach {
                        assertThat(
                            "Category must match",
                            it.category,
                            equalTo(ContentBlockingController.Event.BLOCKED_TRACKING_CONTENT),
                        )
                        assertThat("Blocked must be true", it.blocked, equalTo(true))
                        assertThat("Count must be at least 1", it.count, not(0))
                    }
                }
            },
        )
    }

    @Test
    fun cookieBannerHandlingSettings() {
        // Check default value

        val contentBlocking = sessionRule.runtime.settings.contentBlocking

        assertThat(
            "Expect correct default value which is off",
            contentBlocking.cookieBannerMode,
            equalTo(CookieBannerMode.COOKIE_BANNER_MODE_DISABLED),
        )
        assertThat(
            "Expect correct default value for private browsing",
            contentBlocking.cookieBannerModePrivateBrowsing,
            equalTo(CookieBannerMode.COOKIE_BANNER_MODE_REJECT),
        )

        // Checks that the pref value is also consistent with the runtime settings
        val originalPrefs = sessionRule.getPrefs(
            "cookiebanners.service.mode",
            "cookiebanners.service.mode.privateBrowsing",
        )

        assertThat("Initial value is correct", originalPrefs[0] as Int, equalTo(contentBlocking.cookieBannerMode))
        assertThat("Initial value is correct", originalPrefs[1] as Int, equalTo(contentBlocking.cookieBannerModePrivateBrowsing))

        contentBlocking.cookieBannerMode = CookieBannerMode.COOKIE_BANNER_MODE_REJECT_OR_ACCEPT
        contentBlocking.cookieBannerModePrivateBrowsing = CookieBannerMode.COOKIE_BANNER_MODE_DISABLED

        val actualPrefs = sessionRule.getPrefs(
            "cookiebanners.service.mode",
            "cookiebanners.service.mode.privateBrowsing",
        )

        assertThat("Initial value is correct", actualPrefs[0] as Int, equalTo(contentBlocking.cookieBannerMode))
        assertThat("Initial value is correct", actualPrefs[1] as Int, equalTo(contentBlocking.cookieBannerModePrivateBrowsing))
    }

    @Test
    fun cookieBannerHandlingDetectOnlyModeSettings() {
        // Check default value
        val contentBlocking = sessionRule.runtime.settings.contentBlocking

        assertThat(
            "Expect correct default value which is off",
            contentBlocking.cookieBannerDetectOnlyMode,
            equalTo(false),
        )

        // Checks that the pref value is also consistent with the runtime settings
        val originalPrefs = sessionRule.getPrefs(
            "cookiebanners.service.detectOnly",
        )

        assertThat(
            "Initial value is correct",
            originalPrefs[0] as Boolean,
            equalTo(contentBlocking.cookieBannerDetectOnlyMode),
        )

        contentBlocking.cookieBannerDetectOnlyMode = true

        val actualPrefs = sessionRule.getPrefs(
            "cookiebanners.service.detectOnly",
        )

        assertThat(
            "Initial value is correct",
            actualPrefs[0] as Boolean,
            equalTo(contentBlocking.cookieBannerDetectOnlyMode),
        )
    }
}