|
1 | 1 | package com.canhub.cropper |
2 | 2 |
|
| 3 | +import android.graphics.Bitmap |
| 4 | +import android.net.Uri |
3 | 5 | import io.mockk.mockkObject |
4 | 6 | import io.mockk.unmockkObject |
5 | 7 | import org.junit.After |
6 | 8 | import org.junit.Assert.assertEquals |
| 9 | +import org.junit.Assert.assertTrue |
7 | 10 | import org.junit.Before |
8 | 11 | import org.junit.Test |
9 | 12 |
|
@@ -131,4 +134,139 @@ class BitmapUtilsTest { |
131 | 134 | fun `WHEN low rectangle points is provided getRectBottom, THEN resultArrayOutOfIndexException`() { |
132 | 135 | BitmapUtils.getRectBottom(LOW_RECT_POINTS) |
133 | 136 | } |
| 137 | + |
| 138 | + @Test(expected = SecurityException::class) |
| 139 | + fun `WHEN file URI is provided for validation, THEN SecurityException is thrown`() { |
| 140 | + // GIVEN |
| 141 | + val fileUri = Uri.parse("file:///data/user/0/com.example/cache/image.jpg") |
| 142 | + val compressFormat = Bitmap.CompressFormat.JPEG |
| 143 | + |
| 144 | + // WHEN |
| 145 | + BitmapUtils.validateOutputUri(fileUri, compressFormat) |
| 146 | + |
| 147 | + // THEN - SecurityException expected |
| 148 | + } |
| 149 | + |
| 150 | + @Test(expected = SecurityException::class) |
| 151 | + fun `WHEN file URI with malicious path is provided, THEN SecurityException is thrown`() { |
| 152 | + // GIVEN |
| 153 | + val maliciousUri = Uri.parse("file:///data/user/0/com.example/shared_prefs/SecureStore.xml") |
| 154 | + val compressFormat = Bitmap.CompressFormat.JPEG |
| 155 | + |
| 156 | + // WHEN |
| 157 | + BitmapUtils.validateOutputUri(maliciousUri, compressFormat) |
| 158 | + |
| 159 | + // THEN - SecurityException expected |
| 160 | + } |
| 161 | + |
| 162 | + @Test(expected = SecurityException::class) |
| 163 | + fun `WHEN content URI with wrong extension for JPEG is provided, THEN SecurityException is thrown`() { |
| 164 | + // GIVEN |
| 165 | + val contentUri = Uri.parse("content://com.example.provider/images/image.png") |
| 166 | + val compressFormat = Bitmap.CompressFormat.JPEG |
| 167 | + |
| 168 | + // WHEN |
| 169 | + BitmapUtils.validateOutputUri(contentUri, compressFormat) |
| 170 | + |
| 171 | + // THEN - SecurityException expected |
| 172 | + } |
| 173 | + |
| 174 | + @Test(expected = SecurityException::class) |
| 175 | + fun `WHEN content URI with wrong extension for PNG is provided, THEN SecurityException is thrown`() { |
| 176 | + // GIVEN |
| 177 | + val contentUri = Uri.parse("content://com.example.provider/images/image.jpg") |
| 178 | + val compressFormat = Bitmap.CompressFormat.PNG |
| 179 | + |
| 180 | + // WHEN |
| 181 | + BitmapUtils.validateOutputUri(contentUri, compressFormat) |
| 182 | + |
| 183 | + // THEN - SecurityException expected |
| 184 | + } |
| 185 | + |
| 186 | + @Test(expected = SecurityException::class) |
| 187 | + fun `WHEN content URI with XML extension is provided, THEN SecurityException is thrown`() { |
| 188 | + // GIVEN |
| 189 | + val xmlUri = Uri.parse("content://com.example.provider/prefs/SecureStore.xml") |
| 190 | + val compressFormat = Bitmap.CompressFormat.JPEG |
| 191 | + |
| 192 | + // WHEN |
| 193 | + BitmapUtils.validateOutputUri(xmlUri, compressFormat) |
| 194 | + |
| 195 | + // THEN - SecurityException expected |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + fun `WHEN valid content URI with jpg extension for JPEG is provided, THEN validation passes`() { |
| 200 | + // GIVEN |
| 201 | + val contentUri = Uri.parse("content://com.example.provider/images/image.jpg") |
| 202 | + val compressFormat = Bitmap.CompressFormat.JPEG |
| 203 | + |
| 204 | + // WHEN & THEN - No exception should be thrown |
| 205 | + BitmapUtils.validateOutputUri(contentUri, compressFormat) |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + fun `WHEN valid content URI with jpeg extension for JPEG is provided, THEN validation passes`() { |
| 210 | + // GIVEN |
| 211 | + val contentUri = Uri.parse("content://com.example.provider/images/image.jpeg") |
| 212 | + val compressFormat = Bitmap.CompressFormat.JPEG |
| 213 | + |
| 214 | + // WHEN & THEN - No exception should be thrown |
| 215 | + BitmapUtils.validateOutputUri(contentUri, compressFormat) |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + fun `WHEN valid content URI with png extension for PNG is provided, THEN validation passes`() { |
| 220 | + // GIVEN |
| 221 | + val contentUri = Uri.parse("content://com.example.provider/images/image.png") |
| 222 | + val compressFormat = Bitmap.CompressFormat.PNG |
| 223 | + |
| 224 | + // WHEN & THEN - No exception should be thrown |
| 225 | + BitmapUtils.validateOutputUri(contentUri, compressFormat) |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + fun `WHEN valid content URI with webp extension for WEBP is provided, THEN validation passes`() { |
| 230 | + // GIVEN |
| 231 | + val contentUri = Uri.parse("content://com.example.provider/images/image.webp") |
| 232 | + val compressFormat = Bitmap.CompressFormat.WEBP |
| 233 | + |
| 234 | + // WHEN & THEN - No exception should be thrown |
| 235 | + BitmapUtils.validateOutputUri(contentUri, compressFormat) |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + fun `WHEN file URI validation fails, THEN exception message contains scheme information`() { |
| 240 | + // GIVEN |
| 241 | + val fileUri = Uri.parse("file:///path/to/image.jpg") |
| 242 | + val compressFormat = Bitmap.CompressFormat.JPEG |
| 243 | + |
| 244 | + // WHEN |
| 245 | + try { |
| 246 | + BitmapUtils.validateOutputUri(fileUri, compressFormat) |
| 247 | + throw AssertionError("Expected SecurityException to be thrown") |
| 248 | + } catch (e: SecurityException) { |
| 249 | + // THEN |
| 250 | + assertTrue(e.message?.contains("content://") == true) |
| 251 | + assertTrue(e.message?.contains("file://") == true) |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + @Test |
| 256 | + fun `WHEN extension mismatch occurs, THEN exception message contains expected extensions`() { |
| 257 | + // GIVEN |
| 258 | + val contentUri = Uri.parse("content://com.example.provider/images/image.txt") |
| 259 | + val compressFormat = Bitmap.CompressFormat.JPEG |
| 260 | + |
| 261 | + // WHEN |
| 262 | + try { |
| 263 | + BitmapUtils.validateOutputUri(contentUri, compressFormat) |
| 264 | + throw AssertionError("Expected SecurityException to be thrown") |
| 265 | + } catch (e: SecurityException) { |
| 266 | + // THEN |
| 267 | + assertTrue(e.message?.contains(".jpg") == true) |
| 268 | + assertTrue(e.message?.contains(".jpeg") == true) |
| 269 | + assertTrue(e.message?.contains("JPEG") == true) |
| 270 | + } |
| 271 | + } |
134 | 272 | } |
0 commit comments