Skip to content

Commit e595003

Browse files
Steve RamageSJrX
authored andcommitted
feat: add a couple more validators (Resolves #409)
1 parent 07272a9 commit e595003

5 files changed

Lines changed: 251 additions & 0 deletions

File tree

src/main/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/optionvalues/AiGenerated.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ fun getAllAIGeneratedValidators(): Map<Validator, OptionValueInformation> {
184184
Validator("config_parse_wireguard_peer_route_priority", "0") to ConfigParseWireguardPeerRoutePriorityOptionValue() as OptionValueInformation,
185185
Validator("config_parse_wireguard_route_priority", "0") to ConfigParseWireguardRoutePriorityOptionValue() as OptionValueInformation,
186186
Validator("config_parse_wlan_iftype", "0") to ConfigParseWlanIftypeOptionValue() as OptionValueInformation,
187+
Validator("config_parse_ad_actor_system", "0") to ConfigParseAdActorSystemOptionValue() as OptionValueInformation,
188+
Validator("config_parse_address_section", "ADDRESS_SCOPE") to ConfigParseAddressSectionAddressScopeOptionValue() as OptionValueInformation,
189+
187190
)
188191

189192
return allValidators
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.ai
2+
3+
import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.Validator
4+
import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.grammar.*
5+
import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.SimpleGrammarOptionValues
6+
7+
/**
8+
* Validator for Bond.AdActorSystem
9+
* C Function: config_parse_ad_actor_system(0)
10+
* Used by Options: Bond.AdActorSystem
11+
*
12+
* Validates 802.3ad system MAC addresses in the format XX:XX:XX:XX:XX:XX
13+
* where each XX is a hexadecimal byte (00-FF).
14+
*
15+
* Note: The C implementation also validates that the address is not null
16+
* and not multicast, but those semantic checks cannot be implemented in
17+
* the grammar layer.
18+
*/
19+
class ConfigParseAdActorSystemOptionValue : SimpleGrammarOptionValues(
20+
"config_parse_ad_actor_system",
21+
SequenceCombinator(
22+
MAC_ADDRESS,
23+
EOF()
24+
)
25+
) {
26+
companion object {
27+
// MAC address octet: 2 hexadecimal digits
28+
private val MAC_OCTET = RegexTerminal("[0-9a-fA-F]{2}", "[0-9a-fA-F]{2}")
29+
private val COLON = LiteralChoiceTerminal(":")
30+
31+
// MAC address format: XX:XX:XX:XX:XX:XX
32+
val MAC_ADDRESS = SequenceCombinator(
33+
MAC_OCTET, COLON,
34+
MAC_OCTET, COLON,
35+
MAC_OCTET, COLON,
36+
MAC_OCTET, COLON,
37+
MAC_OCTET, COLON,
38+
MAC_OCTET
39+
)
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.ai
2+
3+
import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.grammar.*
4+
import net.sjrx.intellij.plugins.systemdunitfiles.semanticdata.optionvalues.SimpleGrammarOptionValues
5+
6+
/**
7+
* Validator for Address.Scope
8+
* C Function: config_parse_address_section(ADDRESS_SCOPE)
9+
* Used by Options: Address.Scope
10+
*
11+
* Valid values:
12+
* - "global" - valid everywhere on the network, even through a gateway
13+
* - "link" - only valid on this device, will not traverse a gateway
14+
* - "host" - only valid within the device itself
15+
* - Integer in the range 0-255
16+
*
17+
* Note: IPv4 only - IPv6 scope is automatically assigned by the kernel
18+
*/
19+
class ConfigParseAddressSectionAddressScopeOptionValue : SimpleGrammarOptionValues(
20+
"config_parse_address_section",
21+
SequenceCombinator(
22+
AlternativeCombinator(
23+
FlexibleLiteralChoiceTerminal("global", "link", "host"),
24+
IntegerTerminal(0, 256)
25+
),
26+
EOF()
27+
)
28+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package net.sjrx.intellij.plugins.systemdunitfiles.inspections.ai
2+
3+
import net.sjrx.intellij.plugins.systemdunitfiles.AbstractUnitFileTest
4+
import net.sjrx.intellij.plugins.systemdunitfiles.inspections.InvalidValueInspection
5+
import org.junit.Test
6+
7+
class ConfigParseAdActorSystemOptionValueTest : AbstractUnitFileTest() {
8+
9+
@Test
10+
fun testValidMacAddresses() {
11+
// Fixture Setup
12+
// language="unit file (systemd)"
13+
val file = """
14+
[Bond]
15+
AdActorSystem=00:11:22:33:44:55
16+
AdActorSystem=AA:BB:CC:DD:EE:FF
17+
AdActorSystem=01:23:45:67:89:ab
18+
AdActorSystem=FE:DC:BA:98:76:54
19+
""".trimIndent()
20+
21+
// Execute SUT
22+
setupFileInEditor("file.netdev", file)
23+
enableInspection(InvalidValueInspection::class.java)
24+
val highlights = myFixture.doHighlighting()
25+
26+
// Verification
27+
assertSize(0, highlights)
28+
}
29+
30+
@Test
31+
fun testInvalidMacAddresses() {
32+
// Fixture Setup
33+
// language="unit file (systemd)"
34+
val file = """
35+
[Bond]
36+
AdActorSystem=<error descr="Invalid value">00:11:22:33:44</error>
37+
AdActorSystem=<error descr="Invalid value">00:11:22:33:44:55:66</error>
38+
AdActorSystem=<error descr="Invalid value">00-11-22-33-44-55</error>
39+
AdActorSystem=<error descr="Invalid value">GG:HH:II:JJ:KK:LL</error>
40+
AdActorSystem=<error descr="Invalid value">0:1:2:3:4:5</error>
41+
AdActorSystem=<error descr="Invalid value">001122334455</error>
42+
""".trimIndent()
43+
44+
// Execute SUT
45+
setupFileInEditor("file.netdev", file)
46+
enableInspection(InvalidValueInspection::class.java)
47+
val highlights = myFixture.doHighlighting()
48+
49+
// Verification
50+
assertSize(6, highlights)
51+
}
52+
53+
@Test
54+
fun testMixedCaseIsValid() {
55+
// Fixture Setup
56+
// language="unit file (systemd)"
57+
val file = """
58+
[Bond]
59+
AdActorSystem=aA:bB:cC:dD:eE:fF
60+
""".trimIndent()
61+
62+
// Execute SUT
63+
setupFileInEditor("file.netdev", file)
64+
enableInspection(InvalidValueInspection::class.java)
65+
val highlights = myFixture.doHighlighting()
66+
67+
// Verification
68+
assertSize(0, highlights)
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package net.sjrx.intellij.plugins.systemdunitfiles.inspections.ai
2+
3+
import net.sjrx.intellij.plugins.systemdunitfiles.AbstractUnitFileTest
4+
import net.sjrx.intellij.plugins.systemdunitfiles.inspections.InvalidValueInspection
5+
import org.junit.Test
6+
7+
class ConfigParseAddressSectionAddressScopeOptionValueTest : AbstractUnitFileTest() {
8+
9+
@Test
10+
fun testValidLiteralValues() {
11+
// Fixture Setup
12+
// language="unit file (systemd)"
13+
val file = """
14+
[Address]
15+
Scope=global
16+
Scope=link
17+
Scope=host
18+
""".trimIndent()
19+
20+
// Execute SUT
21+
setupFileInEditor("file.network", file)
22+
enableInspection(InvalidValueInspection::class.java)
23+
val highlights = myFixture.doHighlighting()
24+
25+
// Verification
26+
assertSize(0, highlights)
27+
}
28+
29+
@Test
30+
fun testValidIntegerValues() {
31+
// Fixture Setup
32+
// language="unit file (systemd)"
33+
val file = """
34+
[Address]
35+
Scope=0
36+
Scope=127
37+
Scope=255
38+
""".trimIndent()
39+
40+
// Execute SUT
41+
setupFileInEditor("file.network", file)
42+
enableInspection(InvalidValueInspection::class.java)
43+
val highlights = myFixture.doHighlighting()
44+
45+
// Verification
46+
assertSize(0, highlights)
47+
}
48+
49+
@Test
50+
fun testInvalidLiteralValues() {
51+
// Fixture Setup
52+
// language="unit file (systemd)"
53+
val file = """
54+
[Address]
55+
Scope=invalid
56+
Scope=localhost
57+
Scope=site
58+
""".trimIndent()
59+
60+
// Execute SUT
61+
setupFileInEditor("file.network", file)
62+
enableInspection(InvalidValueInspection::class.java)
63+
val highlights = myFixture.doHighlighting()
64+
65+
// Verification
66+
assertSize(3, highlights)
67+
}
68+
69+
@Test
70+
fun testInvalidIntegerValues() {
71+
// Fixture Setup
72+
// language="unit file (systemd)"
73+
val file = """
74+
[Address]
75+
Scope=256
76+
Scope=-1
77+
Scope=1000
78+
""".trimIndent()
79+
80+
// Execute SUT
81+
setupFileInEditor("file.network", file)
82+
enableInspection(InvalidValueInspection::class.java)
83+
val highlights = myFixture.doHighlighting()
84+
85+
// Verification
86+
assertSize(3, highlights)
87+
}
88+
89+
@Test
90+
fun testMixedInvalidValues() {
91+
// Fixture Setup
92+
// language="unit file (systemd)"
93+
val file = """
94+
[Address]
95+
Scope=global
96+
Scope=invalid_value
97+
Scope=100
98+
Scope=300
99+
""".trimIndent()
100+
101+
// Execute SUT
102+
setupFileInEditor("file.network", file)
103+
enableInspection(InvalidValueInspection::class.java)
104+
val highlights = myFixture.doHighlighting()
105+
106+
// Verification
107+
assertSize(2, highlights)
108+
}
109+
}

0 commit comments

Comments
 (0)