File tree 2 files changed +34
-1
lines changed
main/kotlin/com/coder/jetbrains/matcher
test/kotlin/com/coder/jetbrains/matcher
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -28,12 +28,16 @@ class PortMatcher(private val rule: String) {
28
28
return when {
29
29
// Try parsing as single port
30
30
portPart.all { it.isDigit() } -> {
31
- MatchRule .SinglePort (portPart.toInt())
31
+ val port = portPart.toInt()
32
+ validatePort(port)
33
+ MatchRule .SinglePort (port)
32
34
}
33
35
// Try parsing as port range (e.g., "40000-55000")
34
36
portPart.matches(" ^\\ d+-\\ d+$" .toRegex()) -> {
35
37
val (start, end) = portPart.split(' -' )
36
38
.map { it.trim().toInt() }
39
+ validatePort(start)
40
+ validatePort(end)
37
41
require(start <= end) { " Invalid port range: start must be less than or equal to end" }
38
42
MatchRule .PortRange (start, end)
39
43
}
@@ -47,4 +51,12 @@ class PortMatcher(private val rule: String) {
47
51
}
48
52
}
49
53
}
54
+
55
+ private fun validatePort (port : Int ) {
56
+ require(port in 0 .. 65535 ) { " Port number must be between 0 and 65535, got: $port " }
57
+ }
58
+
59
+ companion object {
60
+ const val MAX_PORT = 65535
61
+ }
50
62
}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package com.coder.jetbrains.matcher
3
3
import org.junit.Test
4
4
import org.junit.Assert.assertFalse
5
5
import org.junit.Assert.assertTrue
6
+ import org.junit.Assert.assertThrows
6
7
7
8
class PortMatcherTest {
8
9
@@ -40,4 +41,24 @@ class PortMatcherTest {
40
41
assertTrue(matcher.matches(8009 ))
41
42
assertFalse(matcher.matches(8010 ))
42
43
}
44
+
45
+ @Test
46
+ fun `test invalid port numbers` () {
47
+ assertThrows(IllegalArgumentException ::class .java) { PortMatcher (" 65536" ) }
48
+ assertThrows(IllegalArgumentException ::class .java) { PortMatcher (" 0-65536" ) }
49
+ assertThrows(IllegalArgumentException ::class .java) { PortMatcher (" 70000" ) }
50
+ }
51
+
52
+ @Test
53
+ fun `test edge case port numbers` () {
54
+ // These should work
55
+ PortMatcher (" 0" )
56
+ PortMatcher (" 65535" )
57
+ PortMatcher (" 0-65535" )
58
+
59
+ // These combinations should work
60
+ val matcher = PortMatcher (" 0-65535" )
61
+ assertTrue(matcher.matches(0 ))
62
+ assertTrue(matcher.matches(65535 ))
63
+ }
43
64
}
You can’t perform that action at this time.
0 commit comments