Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {

testCompile 'junit:junit:4.13'
testCompile 'org.mockito:mockito-core:3.0.0'
implementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.12'
}

task copyNecessaryFiles {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.telerik.metadata.security.filtering.input.user

class UserLineCommentFilter {
class UserLineFilter {
fun isCommentLine(line: String) = line.startsWith("//") || line.startsWith("#")
fun isEmptyLine(line: String) = line.isBlank()
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ object UserPatternsCollection : PatternsCollection {
private fun parseFile(path: Path): Collection<PatternEntry> {
val fileLines = Files.readAllLines(path)
val lineSplitter = UserLineSplitter()
val commentFilter = UserLineCommentFilter()
val lineFilter = UserLineFilter()

return fileLines
.filter { !commentFilter.isCommentLine(it) }
.asSequence()
.filter { !lineFilter.isCommentLine(it) }
.filter { !lineFilter.isEmptyLine(it) }
.map { lineSplitter.splitLine(it) }
.toList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class PatternMatcherImpl : PatternMatcher {
Algorithm is borrowed from: https://www.geeksforgeeks.org/wildcard-character-matching/
*/


// Short circuit if pattern is '*'
if (pattern == "*" && input.isNotEmpty()) {
return true
}

// If we reach at the end of both strings,
// we are done
if (pattern.isEmpty() && input.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import org.junit.Test

import org.junit.Assert.*

class UserLineCommentFilterTest {
class UserLineFilterTest {

companion object {
private const val UNEXPECTED_LINE_FILTERING_RESULT_MESSAGE = "Unexpected line filtering result"
}

private val filter = UserLineCommentFilter()
private val filter = UserLineFilter()

@Test
fun `Is regular commented line`() {
Expand Down Expand Up @@ -38,4 +38,22 @@ class UserLineCommentFilterTest {

assertFalse(UNEXPECTED_LINE_FILTERING_RESULT_MESSAGE, isComment)
}

@Test
fun `Is empty line`(){
val line = System.lineSeparator()

val isEmptyLine = filter.isEmptyLine(line)

assertTrue(UNEXPECTED_LINE_FILTERING_RESULT_MESSAGE, isEmptyLine)
}

@Test
fun `Is whitespace line`(){
val line = " "

val isEmptyLine = filter.isEmptyLine(line)

assertTrue(UNEXPECTED_LINE_FILTERING_RESULT_MESSAGE, isEmptyLine)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.telerik.metadata.security.filtering.matching.impl

import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized


@RunWith(Parameterized::class)
class PatternMatcherImplTest(private var pattern: String, private var input: String, private var shouldMatch: Boolean) {

private val matcher = PatternMatcherImpl()

companion object {
@JvmStatic
@Parameterized.Parameters(name = "Case {index}: matcher.match({0}, {1}) should be {2}")
fun data(): Collection<Array<Any>> {
return listOf(
arrayOf("g*ks", "geeks", true),
arrayOf("ge?ks*", "geeksforgeeks", true),
arrayOf("g*k", "gee", false),
arrayOf("*pqrs", "pqrst", false),
arrayOf("abc*bcd", "abcdhghgbcd", true),
arrayOf("abc*c?d", "abcd", false),
arrayOf("*c*d", "abcd", true),
arrayOf("*?c*d", "abcd", true)
)
}
}

@Test
fun `Test match`() {
assertEquals("Wrong pattern matcher result!", shouldMatch, matcher.match(pattern, input))
}
}