|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.adblocking.impl |
| 18 | + |
| 19 | +import com.duckduckgo.adblocking.impl.domain.RealScriptletUpdater |
| 20 | +import com.duckduckgo.adblocking.impl.domain.ScriptletSignatureValidator |
| 21 | +import com.duckduckgo.adblocking.impl.domain.ScriptletUpdateResult |
| 22 | +import com.duckduckgo.adblocking.impl.domain.ScriptletValidationResult |
| 23 | +import com.duckduckgo.adblocking.impl.remoteconfig.ScriptletEntry |
| 24 | +import com.duckduckgo.adblocking.impl.remoteconfig.ScriptletsSettings |
| 25 | +import kotlinx.coroutines.CompletableDeferred |
| 26 | +import kotlinx.coroutines.awaitCancellation |
| 27 | +import kotlinx.coroutines.test.runTest |
| 28 | +import org.junit.Assert.assertEquals |
| 29 | +import org.junit.Assert.assertTrue |
| 30 | +import org.junit.Test |
| 31 | +import org.mockito.kotlin.any |
| 32 | +import org.mockito.kotlin.check |
| 33 | +import org.mockito.kotlin.eq |
| 34 | +import org.mockito.kotlin.mock |
| 35 | +import org.mockito.kotlin.never |
| 36 | +import org.mockito.kotlin.verify |
| 37 | +import org.mockito.kotlin.whenever |
| 38 | +import java.io.IOException |
| 39 | + |
| 40 | +class RealScriptletUpdaterTest { |
| 41 | + |
| 42 | + private val repository: AdBlockingExtensionRepository = mock() |
| 43 | + private val downloader: ScriptletDownloader = mock() |
| 44 | + private val validator: ScriptletSignatureValidator = mock() |
| 45 | + |
| 46 | + private val updater = RealScriptletUpdater(repository, downloader, validator) |
| 47 | + |
| 48 | + private val isolatedPath = "scriptlets/isolated/ublock-filters.js" |
| 49 | + private val mainPath = "scriptlets/main/ublock-filters.js" |
| 50 | + private val isolatedEntry = ScriptletEntry(url = "https://cdn.example/isolated.js", signature = "iso-sig") |
| 51 | + private val mainEntry = ScriptletEntry(url = "https://cdn.example/main.js", signature = "main-sig") |
| 52 | + private val isolatedBytes = "isolated-bytes".toByteArray() |
| 53 | + private val mainBytes = "main-bytes".toByteArray() |
| 54 | + |
| 55 | + private val validSettings = ScriptletsSettings( |
| 56 | + version = "2026.3.9", |
| 57 | + scriptlets = mapOf(isolatedPath to isolatedEntry, mainPath to mainEntry), |
| 58 | + ) |
| 59 | + |
| 60 | + @Test |
| 61 | + fun whenStoredVersionMatchesRemoteVersionThenUpdateReturnsSuccess() = runTest { |
| 62 | + whenever(repository.getStoredVersion()).thenReturn("2026.3.9") |
| 63 | + |
| 64 | + assertEquals(ScriptletUpdateResult.Success, updater.update(validSettings)) |
| 65 | + verify(downloader, never()).download(any()) |
| 66 | + verify(repository, never()).storeScriptlets(any(), any()) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun whenSingleDownloadFailsThenUpdateReturnsRetryAndDoesNotPersist() = runTest { |
| 71 | + whenever(repository.getStoredVersion()).thenReturn("0.0.0") |
| 72 | + whenever(downloader.download(isolatedEntry.url)).thenReturn(kotlin.Result.success(isolatedBytes)) |
| 73 | + whenever(downloader.download(mainEntry.url)).thenReturn(kotlin.Result.failure(IOException("boom"))) |
| 74 | + whenever(validator.validate(isolatedBytes, isolatedEntry.signature)).thenReturn(ScriptletValidationResult.Valid) |
| 75 | + |
| 76 | + assertEquals(ScriptletUpdateResult.Retry, updater.update(validSettings)) |
| 77 | + verify(repository, never()).storeScriptlets(any(), any()) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun whenValidationFailsForAnyScriptletThenUpdateReturnsRetryAndDoesNotPersist() = runTest { |
| 82 | + whenever(repository.getStoredVersion()).thenReturn("0.0.0") |
| 83 | + whenever(downloader.download(isolatedEntry.url)).thenReturn(kotlin.Result.success(isolatedBytes)) |
| 84 | + whenever(downloader.download(mainEntry.url)).thenReturn(kotlin.Result.success(mainBytes)) |
| 85 | + whenever(validator.validate(isolatedBytes, isolatedEntry.signature)).thenReturn(ScriptletValidationResult.Valid) |
| 86 | + whenever(validator.validate(mainBytes, mainEntry.signature)) |
| 87 | + .thenReturn(ScriptletValidationResult.Invalid.SignatureVerificationFailed) |
| 88 | + |
| 89 | + assertEquals(ScriptletUpdateResult.Retry, updater.update(validSettings)) |
| 90 | + verify(repository, never()).storeScriptlets(any(), any()) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun whenAllScriptletsValidThenUpdateReturnsSuccessAndStoresAllScriptlets() = runTest { |
| 95 | + whenever(repository.getStoredVersion()).thenReturn("0.0.0") |
| 96 | + whenever(downloader.download(isolatedEntry.url)).thenReturn(kotlin.Result.success(isolatedBytes)) |
| 97 | + whenever(downloader.download(mainEntry.url)).thenReturn(kotlin.Result.success(mainBytes)) |
| 98 | + whenever(validator.validate(isolatedBytes, isolatedEntry.signature)).thenReturn(ScriptletValidationResult.Valid) |
| 99 | + whenever(validator.validate(mainBytes, mainEntry.signature)).thenReturn(ScriptletValidationResult.Valid) |
| 100 | + |
| 101 | + assertEquals(ScriptletUpdateResult.Success, updater.update(validSettings)) |
| 102 | + verify(repository).storeScriptlets( |
| 103 | + eq("2026.3.9"), |
| 104 | + check { stored -> |
| 105 | + assertEquals(setOf(isolatedPath, mainPath), stored.keys) |
| 106 | + assertEquals(String(isolatedBytes), String(stored.getValue(isolatedPath))) |
| 107 | + assertEquals(String(mainBytes), String(stored.getValue(mainPath))) |
| 108 | + }, |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun whenSettingsHasNoScriptletsThenUpdateReturnsSuccessAndStoresEmptyMap() = runTest { |
| 114 | + whenever(repository.getStoredVersion()).thenReturn("0.0.0") |
| 115 | + |
| 116 | + assertEquals(ScriptletUpdateResult.Success, updater.update(validSettings.copy(scriptlets = emptyMap()))) |
| 117 | + verify(downloader, never()).download(any()) |
| 118 | + verify(repository).storeScriptlets(eq("2026.3.9"), eq(emptyMap())) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun whenOneDownloadFailsThenUpdateShortCircuitsAndCancelsInFlightDownloads() = runTest { |
| 123 | + whenever(repository.getStoredVersion()).thenReturn("0.0.0") |
| 124 | + val isolatedDownloadStarted = CompletableDeferred<Unit>() |
| 125 | + val shortCircuitingDownloader = object : ScriptletDownloader { |
| 126 | + override suspend fun download(url: String): kotlin.Result<ByteArray> = when (url) { |
| 127 | + isolatedEntry.url -> { |
| 128 | + isolatedDownloadStarted.complete(Unit) |
| 129 | + awaitCancellation() |
| 130 | + } |
| 131 | + mainEntry.url -> kotlin.Result.failure(IOException("boom")) |
| 132 | + else -> error("unexpected url: $url") |
| 133 | + } |
| 134 | + } |
| 135 | + val shortCircuitingUpdater = RealScriptletUpdater(repository, shortCircuitingDownloader, validator) |
| 136 | + |
| 137 | + assertEquals(ScriptletUpdateResult.Retry, shortCircuitingUpdater.update(validSettings)) |
| 138 | + assertTrue("isolated download should have started in parallel", isolatedDownloadStarted.isCompleted) |
| 139 | + verify(validator, never()).validate(any(), any()) |
| 140 | + verify(repository, never()).storeScriptlets(any(), any()) |
| 141 | + } |
| 142 | +} |
0 commit comments