Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
firmianay committed Dec 14, 2023
1 parent 9c5a0c2 commit ebb78da
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
53 changes: 26 additions & 27 deletions src/main/kotlin/net/bytedance/security/app/rules/Rules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,40 +92,39 @@ class Rules(val rulePaths: List<String>, val factory: IRuleFactory) : IRulesForC
throw Exception("read config file $path failed")
}
}

return jsonStr
}
}

private fun parseSdkVersion(input: String): List<Int> {
val MIN_SDK_VERSION = 9 // Android 2.3
val MAX_SDK_VERSION = 50 // for future
fun parseSdkVersion(input: String): List<Int> {
val MIN_SDK_VERSION = 9 // Android 2.3
val MAX_SDK_VERSION = 50 // for future

if (input.isBlank() || input.trim() == ":") {
return (MIN_SDK_VERSION..MAX_SDK_VERSION).toList()
}
return input.split(Regex("[,\\s]+")).flatMap { part ->
when {
part.contains(":") -> {
val splitPart = part.split(":")
val hasStart = splitPart[0].isNotEmpty()
val hasEnd = splitPart[1].isNotEmpty()
when {
!hasStart && !hasEnd -> listOf()
!hasEnd -> {
(splitPart[0].toIntOrNull() ?: return@flatMap listOf())..MAX_SDK_VERSION
}
!hasStart -> {
(MIN_SDK_VERSION..(splitPart[1].toIntOrNull() ?: return@flatMap listOf())).toList()
}
else -> {
val start = splitPart[0].toIntOrNull() ?: return@flatMap listOf()
val end = splitPart[1].toIntOrNull() ?: return@flatMap listOf()
(start..end).toList()
if (input.isBlank() || input.trim() == ":") {
return (MIN_SDK_VERSION..MAX_SDK_VERSION).toList()
}
return input.split(Regex("[,\\s]+")).flatMap { part ->
when {
part.contains(":") -> {
val splitPart = part.split(":")
val hasStart = splitPart[0].isNotEmpty()
val hasEnd = splitPart[1].isNotEmpty()
when {
!hasStart && !hasEnd -> listOf()
!hasEnd -> {
(splitPart[0].toIntOrNull() ?: return@flatMap listOf())..MAX_SDK_VERSION
}
!hasStart -> {
(MIN_SDK_VERSION..(splitPart[1].toIntOrNull() ?: return@flatMap listOf())).toList()
}
else -> {
val start = splitPart[0].toIntOrNull() ?: return@flatMap listOf()
val end = splitPart[1].toIntOrNull() ?: return@flatMap listOf()
(start..end).toList()
}
}
}
else -> listOf(part.toIntOrNull() ?: return@flatMap listOf())
}
else -> listOf(part.toIntOrNull() ?: return@flatMap listOf())
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/test/kotlin/net/bytedance/security/app/rules/RulesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ package net.bytedance.security.app.rules
import kotlinx.coroutines.runBlocking
import net.bytedance.security.app.getConfig
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
import java.io.File

internal class RulesTest {


fun createDefaultRules(): Rules {
val rules = Rules(
listOf(
Expand Down Expand Up @@ -69,7 +69,22 @@ internal class RulesTest {
println("const strings=${rules.constStringPatterns().toSortedSet().toList()}")
println("fields=${rules.fields().toSortedSet().toList()}")
println("new instances=${rules.newInstances().toSortedSet().toList()}")
}

@Test
fun testParseSdkVersion() {
assertEquals(
(9..50).toList(),
Rules.parseSdkVersion("")
)
assertEquals(
(9..50).toList(),
Rules.parseSdkVersion(":")
)
assertEquals(
(9..10).toList() + listOf(15) + (25..30).toList() + (45..50).toList(),
Rules.parseSdkVersion(":10, 15, 25:30, 45:")
)
}

companion object {
Expand Down

0 comments on commit ebb78da

Please sign in to comment.