|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 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.app.browser.santize |
| 18 | + |
| 19 | +import android.content.ClipData |
| 20 | +import android.content.Intent |
| 21 | +import android.net.Uri |
| 22 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 23 | +import com.duckduckgo.appbuildconfig.api.AppBuildConfig |
| 24 | +import org.junit.Assert.assertFalse |
| 25 | +import org.junit.Assert.assertTrue |
| 26 | +import org.junit.Before |
| 27 | +import org.junit.Test |
| 28 | +import org.junit.runner.RunWith |
| 29 | +import org.mockito.Mock |
| 30 | +import org.mockito.Mockito.mock |
| 31 | +import org.mockito.kotlin.whenever |
| 32 | + |
| 33 | +@RunWith(AndroidJUnit4::class) |
| 34 | +class RealNonHttpAppLinkCheckerTest { |
| 35 | + |
| 36 | + @Mock |
| 37 | + private val appBuildConfig: AppBuildConfig = mock() |
| 38 | + |
| 39 | + private lateinit var testee: RealNonHttpAppLinkChecker |
| 40 | + |
| 41 | + @Before |
| 42 | + fun setUp() { |
| 43 | + whenever(appBuildConfig.applicationId).thenReturn("com.duckduckgo.mobile.android") |
| 44 | + testee = RealNonHttpAppLinkChecker(appBuildConfig) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun whenIntentDataStringContainsInternalContentFileProviderThenReturnFalse() { |
| 49 | + val intent = Intent().apply { |
| 50 | + data = Uri.parse("content://com.duckduckgo.mobile.android.provider/path/to/file") |
| 51 | + } |
| 52 | + |
| 53 | + val result = testee.isPermitted(intent) |
| 54 | + |
| 55 | + assertFalse(result) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun whenIntentDataStringContainsInternalContentFileProviderWithSubpathThenReturnFalse() { |
| 60 | + val intent = Intent().apply { |
| 61 | + data = Uri.parse("content://com.duckduckgo.mobile.android.provider/external_files/documents/test.pdf") |
| 62 | + } |
| 63 | + |
| 64 | + val result = testee.isPermitted(intent) |
| 65 | + |
| 66 | + assertFalse(result) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun whenIntentDataStringContainsExternalContentProviderThenReturnTrue() { |
| 71 | + val intent = Intent().apply { |
| 72 | + data = Uri.parse("content://com.other.app.provider/path/to/file") |
| 73 | + } |
| 74 | + |
| 75 | + val result = testee.isPermitted(intent) |
| 76 | + |
| 77 | + assertTrue(result) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun whenIntentDataStringIsHttpsUrlThenReturnTrue() { |
| 82 | + val intent = Intent().apply { |
| 83 | + data = Uri.parse("https://example.com/page") |
| 84 | + } |
| 85 | + |
| 86 | + val result = testee.isPermitted(intent) |
| 87 | + |
| 88 | + assertTrue(result) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun whenIntentDataStringIsCustomSchemeThenReturnTrue() { |
| 93 | + val intent = Intent().apply { |
| 94 | + data = Uri.parse("myapp://action/data") |
| 95 | + } |
| 96 | + |
| 97 | + val result = testee.isPermitted(intent) |
| 98 | + |
| 99 | + assertTrue(result) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun whenIntentDataIsNullThenReturnTrue() { |
| 104 | + val intent = Intent().apply { |
| 105 | + data = null |
| 106 | + } |
| 107 | + |
| 108 | + val result = testee.isPermitted(intent) |
| 109 | + |
| 110 | + assertTrue(result) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun whenIntentDataStringIsNullThenReturnTrue() { |
| 115 | + val intent = Intent().apply { |
| 116 | + data = Uri.parse("") |
| 117 | + } |
| 118 | + |
| 119 | + val result = testee.isPermitted(intent) |
| 120 | + |
| 121 | + assertTrue(result) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun whenIntentDataStringContainsInternalProviderInUpperCaseThenReturnFalse() { |
| 126 | + val intent = Intent().apply { |
| 127 | + data = Uri.parse("CONTENT://COM.DUCKDUCKGO.MOBILE.ANDROID.PROVIDER/file") |
| 128 | + } |
| 129 | + |
| 130 | + assertFalse(testee.isPermitted(intent)) |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun whenIntentClipDataContainsInternalContentFileProviderThenReturnFalse() { |
| 135 | + val clipData = ClipData.newRawUri("test", Uri.parse("content://com.duckduckgo.mobile.android.provider/file")) |
| 136 | + val intent = Intent().apply { |
| 137 | + data = Uri.parse("https://example.com/safe") |
| 138 | + setClipData(clipData) |
| 139 | + } |
| 140 | + |
| 141 | + val result = testee.isPermitted(intent) |
| 142 | + |
| 143 | + assertFalse(result) |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + fun whenIntentClipDataContainsMultipleUrisWithInternalProviderThenReturnFalse() { |
| 148 | + val clipData = ClipData.newRawUri("test", Uri.parse("https://example.com/safe")) |
| 149 | + clipData.addItem(ClipData.Item(Uri.parse("content://com.duckduckgo.mobile.android.provider/file"))) |
| 150 | + val intent = Intent().apply { |
| 151 | + data = Uri.parse("https://example.com/safe") |
| 152 | + setClipData(clipData) |
| 153 | + } |
| 154 | + |
| 155 | + val result = testee.isPermitted(intent) |
| 156 | + |
| 157 | + assertFalse(result) |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + fun whenIntentClipDataContainsOnlyExternalProvidersThenReturnTrue() { |
| 162 | + val clipData = ClipData.newRawUri("test", Uri.parse("content://com.other.app.provider/file")) |
| 163 | + clipData.addItem(ClipData.Item(Uri.parse("https://example.com/safe"))) |
| 164 | + val intent = Intent().apply { |
| 165 | + data = Uri.parse("https://example.com/safe") |
| 166 | + setClipData(clipData) |
| 167 | + } |
| 168 | + |
| 169 | + val result = testee.isPermitted(intent) |
| 170 | + |
| 171 | + assertTrue(result) |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + fun whenIntentClipDataIsNullThenReturnTrue() { |
| 176 | + val intent = Intent().apply { |
| 177 | + data = Uri.parse("https://example.com/safe") |
| 178 | + clipData = null |
| 179 | + } |
| 180 | + |
| 181 | + val result = testee.isPermitted(intent) |
| 182 | + |
| 183 | + assertTrue(result) |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + fun whenIntentClipDataHasItemWithNullUriThenReturnTrue() { |
| 188 | + val clipData = ClipData.newPlainText("test", "some text") |
| 189 | + val intent = Intent().apply { |
| 190 | + data = Uri.parse("https://example.com/safe") |
| 191 | + setClipData(clipData) |
| 192 | + } |
| 193 | + |
| 194 | + val result = testee.isPermitted(intent) |
| 195 | + |
| 196 | + assertTrue(result) |
| 197 | + } |
| 198 | +} |
0 commit comments