|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RESTAPI\Tests; |
| 4 | + |
| 5 | +require_once 'RESTAPI/autoloader.inc'; |
| 6 | + |
| 7 | +use RESTAPI\Core\TestCase; |
| 8 | +use RESTAPI\Models\HAProxyFrontend; |
| 9 | +use RESTAPI\Models\HAProxyFrontendACL; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests HAProxy frontend ACL expression compatibility with parent processing types. |
| 13 | + */ |
| 14 | +class APIModelsHAProxyFrontendACLTestCase extends TestCase { |
| 15 | + public array $required_packages = ['pfSense-pkg-haproxy']; |
| 16 | + |
| 17 | + private const SUPPORTED_EXPRESSIONS = [ |
| 18 | + 'http' => [ |
| 19 | + 'host_starts_with', |
| 20 | + 'host_ends_with', |
| 21 | + 'host_matches', |
| 22 | + 'host_regex', |
| 23 | + 'host_contains', |
| 24 | + 'path_starts_with', |
| 25 | + 'path_ends_with', |
| 26 | + 'path_matches', |
| 27 | + 'path_regex', |
| 28 | + 'path_contains', |
| 29 | + 'path_dir', |
| 30 | + 'url_parameter', |
| 31 | + 'ssl_c_verify_code', |
| 32 | + 'ssl_c_verify', |
| 33 | + 'ssl_c_ca_commonname', |
| 34 | + 'source_ip', |
| 35 | + 'backendservercount', |
| 36 | + 'custom', |
| 37 | + ], |
| 38 | + 'https' => [ |
| 39 | + 'source_ip', |
| 40 | + 'backendservercount', |
| 41 | + 'ssl_sni_matches', |
| 42 | + 'ssl_sni_contains', |
| 43 | + 'ssl_sni_starts_with', |
| 44 | + 'ssl_sni_ends_with', |
| 45 | + 'ssl_sni_regex', |
| 46 | + 'custom', |
| 47 | + ], |
| 48 | + 'tcp' => ['source_ip', 'backendservercount', 'traffic_is_http', 'traffic_is_ssl', 'custom'], |
| 49 | + ]; |
| 50 | + |
| 51 | + /** |
| 52 | + * Checks every expression against each persisted parent frontend type. |
| 53 | + */ |
| 54 | + public function test_expression_type_matrix(): void { |
| 55 | + $choices = (new HAProxyFrontendACL())->expression->choices; |
| 56 | + |
| 57 | + foreach (self::SUPPORTED_EXPRESSIONS as $type => $supported) { |
| 58 | + $frontend = new HAProxyFrontend(name: "acl_validation_$type", type: $type); |
| 59 | + $frontend->create(); |
| 60 | + |
| 61 | + foreach ($choices as $expression) { |
| 62 | + $acl = new HAProxyFrontendACL( |
| 63 | + parent_id: $frontend->id, |
| 64 | + name: 'test_acl', |
| 65 | + expression: $expression, |
| 66 | + value: '', |
| 67 | + ); |
| 68 | + |
| 69 | + if (in_array($expression, $supported, true)) { |
| 70 | + $this->assert_is_true($acl->validate()); |
| 71 | + $this->assert_equals($acl->expression->value, $expression); |
| 72 | + } else { |
| 73 | + $this->assert_throws_response( |
| 74 | + response_id: 'HAPROXY_FRONTEND_ACL_EXPRESSION_TYPE_MISMATCH', |
| 75 | + code: 400, |
| 76 | + callable: fn() => $acl->validate(), |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Checks nested ACL validation uses the unsaved parent frontend's type. |
| 85 | + */ |
| 86 | + public function test_nested_expression_type_matrix(): void { |
| 87 | + $choices = (new HAProxyFrontendACL())->expression->choices; |
| 88 | + |
| 89 | + foreach (self::SUPPORTED_EXPRESSIONS as $type => $supported) { |
| 90 | + foreach ($choices as $expression) { |
| 91 | + $frontend = new HAProxyFrontend( |
| 92 | + name: "nested_acl_validation_$type", |
| 93 | + type: $type, |
| 94 | + ha_acls: [['name' => 'test_acl', 'expression' => $expression, 'value' => '']], |
| 95 | + ); |
| 96 | + |
| 97 | + if (in_array($expression, $supported, true)) { |
| 98 | + $this->assert_is_true($frontend->validate()); |
| 99 | + $this->assert_equals($frontend->ha_acls->value[0]['expression'], $expression); |
| 100 | + } else { |
| 101 | + $this->assert_throws_response( |
| 102 | + response_id: 'HAPROXY_FRONTEND_ACL_EXPRESSION_TYPE_MISMATCH', |
| 103 | + code: 400, |
| 104 | + callable: fn() => $frontend->validate(), |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Ensures incompatible expressions cannot be created or written over an existing ACL. |
| 113 | + */ |
| 114 | + public function test_reject_incompatible_writes(): void { |
| 115 | + $frontend = new HAProxyFrontend(name: 'acl_write_validation', type: 'http'); |
| 116 | + $frontend->create(); |
| 117 | + $acl = new HAProxyFrontendACL( |
| 118 | + parent_id: $frontend->id, |
| 119 | + name: 'test_acl', |
| 120 | + expression: 'traffic_is_ssl', |
| 121 | + value: '', |
| 122 | + ); |
| 123 | + |
| 124 | + $this->assert_throws_response( |
| 125 | + response_id: 'HAPROXY_FRONTEND_ACL_EXPRESSION_TYPE_MISMATCH', |
| 126 | + code: 400, |
| 127 | + callable: fn() => $acl->create(), |
| 128 | + ); |
| 129 | + $this->assert_is_false(HAProxyFrontendACL::query(parent_id: $frontend->id)->exists()); |
| 130 | + |
| 131 | + $acl = new HAProxyFrontendACL( |
| 132 | + parent_id: $frontend->id, |
| 133 | + name: 'test_acl', |
| 134 | + expression: 'host_matches', |
| 135 | + value: 'example.com', |
| 136 | + ); |
| 137 | + $acl->create(); |
| 138 | + $acl->expression->value = 'ssl_sni_matches'; |
| 139 | + |
| 140 | + $this->assert_throws_response( |
| 141 | + response_id: 'HAPROXY_FRONTEND_ACL_EXPRESSION_TYPE_MISMATCH', |
| 142 | + code: 400, |
| 143 | + callable: fn() => $acl->update(), |
| 144 | + ); |
| 145 | + $this->assert_equals( |
| 146 | + HAProxyFrontendACL::query(parent_id: $frontend->id, id: $acl->id)->first()->expression->value, |
| 147 | + 'host_matches', |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Ensures unknown expressions still fail the Field's choice validation. |
| 153 | + */ |
| 154 | + public function test_reject_unknown_expression(): void { |
| 155 | + $frontend = new HAProxyFrontend(name: 'acl_unknown_validation', type: 'http'); |
| 156 | + $frontend->create(); |
| 157 | + $acl = new HAProxyFrontendACL( |
| 158 | + parent_id: $frontend->id, |
| 159 | + name: 'test_acl', |
| 160 | + expression: 'unknown_expression', |
| 161 | + value: '', |
| 162 | + ); |
| 163 | + |
| 164 | + $this->assert_throws_response( |
| 165 | + response_id: 'FIELD_INVALID_CHOICE', |
| 166 | + code: 400, |
| 167 | + callable: fn() => $acl->validate(), |
| 168 | + ); |
| 169 | + } |
| 170 | +} |
0 commit comments