|
3 | 3 | namespace RESTAPI\Tests; |
4 | 4 |
|
5 | 5 | use RESTAPI\Core\TestCase; |
| 6 | +use RESTAPI\Models\FirewallAlias; |
6 | 7 | use RESTAPI\Models\FirewallRule; |
7 | 8 | use RESTAPI\Models\PortForward; |
| 9 | +use RESTAPI\Responses\ValidationError; |
8 | 10 |
|
9 | 11 | class APIModelsPortForwardTestCase extends TestCase { |
10 | 12 | /** |
@@ -227,4 +229,58 @@ class APIModelsPortForwardTestCase extends TestCase { |
227 | 229 | $rule_q = FirewallRule::query(associated_rule_id: $port_forward->associated_rule_id->value); |
228 | 230 | $this->assert_is_false($rule_q->exists()); |
229 | 231 | } |
| 232 | + |
| 233 | + /** |
| 234 | + * Ensures the target field accepts IP addresses, aliases and interface IPs |
| 235 | + */ |
| 236 | + public function test_target_validation(): void { |
| 237 | + # Create an alias to test with |
| 238 | + $alias = new FirewallAlias(name: "testalias", type:"host"); |
| 239 | + $alias->create(); |
| 240 | + |
| 241 | + # Set values we expect to be allowed vs disallowed |
| 242 | + $allowed_values = ["1.2.3.4", "wan:ip", "testalias"]; |
| 243 | + $disallowed_values = ["example.com", "wan", "self", "l2tp", "1.2.3.4/24"]; |
| 244 | + |
| 245 | + # Check each allowed value and ensure it does not throw an exception during validation |
| 246 | + foreach ($allowed_values as $value) { |
| 247 | + $this->assert_does_not_throw( |
| 248 | + callable: function () use ($value) { |
| 249 | + $port_forward = new PortForward( |
| 250 | + data: [ |
| 251 | + 'interface' => 'wan', |
| 252 | + 'protocol' => 'tcp', |
| 253 | + 'source' => 'any', |
| 254 | + 'destination' => 'wan:ip', |
| 255 | + 'destination_port' => '8443', |
| 256 | + 'target' => $value, |
| 257 | + 'local_port' => '4443', |
| 258 | + ], |
| 259 | + ); |
| 260 | + $port_forward->validate(); |
| 261 | + }, |
| 262 | + ); |
| 263 | + } |
| 264 | + |
| 265 | + # Check each disallowed value and ensure it throws an exception during validation |
| 266 | + foreach ($disallowed_values as $value) { |
| 267 | + $this->assert_throws( |
| 268 | + exceptions: ["ValidationError"], |
| 269 | + callable: function () use ($value) { |
| 270 | + $port_forward = new PortForward( |
| 271 | + data: [ |
| 272 | + 'interface' => 'wan', |
| 273 | + 'protocol' => 'tcp', |
| 274 | + 'source' => 'any', |
| 275 | + 'destination' => 'wan:ip', |
| 276 | + 'destination_port' => '8443', |
| 277 | + 'target' => $value, |
| 278 | + 'local_port' => '4443', |
| 279 | + ], |
| 280 | + ); |
| 281 | + $port_forward->validate(); |
| 282 | + } |
| 283 | + ); |
| 284 | + } |
| 285 | + } |
230 | 286 | } |
0 commit comments