-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
β¦2956) * Commit with unresolved merge conflicts * Fixed cherry-pick issues --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: boris <[email protected]> Co-authored-by: Boris Safonov <[email protected]> Co-authored-by: Mohamad Jaara <[email protected]>
- Loading branch information
1 parent
147eb18
commit 49582cd
Showing
9 changed files
with
252 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...src/commonMain/kotlin/com/wire/kalium/logic/feature/asset/ValidateAssetFileTypeUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.asset | ||
|
||
/** | ||
* Returns true if the file extension is present in file name and is allowed and false otherwise. | ||
* @param fileName the file name (with extension) to validate. | ||
* @param allowedExtension the list of allowed extension. | ||
*/ | ||
interface ValidateAssetFileTypeUseCase { | ||
operator fun invoke(fileName: String?, allowedExtension: List<String>): Boolean | ||
} | ||
|
||
internal class ValidateAssetFileTypeUseCaseImpl : ValidateAssetFileTypeUseCase { | ||
override operator fun invoke(fileName: String?, allowedExtension: List<String>): Boolean { | ||
if (fileName == null) return false | ||
|
||
val split = fileName.split(".") | ||
return if (split.size < 2) { | ||
false | ||
} else { | ||
val allowedExtensionLowerCase = allowedExtension.map { it.lowercase() } | ||
val extensions = split.subList(1, split.size).map { it.lowercase() } | ||
extensions.all { it.isNotEmpty() && allowedExtensionLowerCase.contains(it) } | ||
} | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
...src/commonMain/kotlin/com/wire/kalium/logic/feature/asset/ValidateAssetMimeTypeUseCase.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...commonTest/kotlin/com/wire/kalium/logic/feature/asset/ValidateAssetFileTypeUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.asset | ||
|
||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class ValidateAssetFileTypeUseCaseTest { | ||
|
||
@Test | ||
fun givenRegularFileNameWithAllowedExtension_whenInvoke_thenBeApproved() = runTest { | ||
val (_, validate) = arrange {} | ||
|
||
val result = validate("name.txt", listOf("txt", "jpg")) | ||
|
||
assertTrue(result) | ||
} | ||
|
||
@Test | ||
fun givenRegularFileNameWithNOTAllowedExtension_whenInvoke_thenBeRestricted() = runTest { | ||
val (_, validate) = arrange {} | ||
|
||
val result = validate("name.php", listOf("txt", "jpg")) | ||
|
||
assertFalse(result) | ||
} | ||
|
||
@Test | ||
fun givenRegularFileNameWithoutExtension_whenInvoke_thenBeRestricted() = runTest { | ||
val (_, validate) = arrange {} | ||
|
||
val result = validate("name", listOf("txt", "jpg")) | ||
|
||
assertFalse(result) | ||
} | ||
|
||
@Test | ||
fun givenNullFileName_whenInvoke_thenBeRestricted() = runTest { | ||
val (_, validate) = arrange {} | ||
|
||
val result = validate(null, listOf("txt", "jpg")) | ||
|
||
assertFalse(result) | ||
} | ||
|
||
@Test | ||
fun givenRegularFileNameWithFewExtensions_whenInvoke_thenEachExtensionIsChecked() = runTest { | ||
val (_, validate) = arrange {} | ||
|
||
val result1 = validate("name.php.txt", listOf("txt", "jpg")) | ||
val result2 = validate("name.txt.php", listOf("txt", "jpg")) | ||
val result3 = validate("name..txt.jpg", listOf("txt", "jpg")) | ||
val result4 = validate("name.txt.php.txt.jpg", listOf("txt", "jpg")) | ||
|
||
assertFalse(result1) | ||
assertFalse(result2) | ||
assertFalse(result3) | ||
assertFalse(result4) | ||
} | ||
|
||
private fun arrange(block: Arrangement.() -> Unit) = Arrangement(block).arrange() | ||
|
||
private class Arrangement( | ||
private val block: Arrangement.() -> Unit | ||
) { | ||
fun arrange() = block().run { | ||
this@Arrangement to ValidateAssetFileTypeUseCaseImpl() | ||
} | ||
} | ||
} |
Oops, something went wrong.