File size: 14,856 Bytes
6a7089a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | package config
import (
"strings"
"testing"
)
func TestValidateFileConfig_Valid(t *testing.T) {
maxTabs := 20
fc := &FileConfig{
Server: ServerConfig{
Port: "9867",
Bind: "127.0.0.1",
},
InstanceDefaults: InstanceDefaultsConfig{
Mode: "headless",
MaxTabs: &maxTabs,
StealthLevel: "light",
TabEvictionPolicy: "reject",
},
MultiInstance: MultiInstanceConfig{
Strategy: "simple",
AllocationPolicy: "fcfs",
Restart: MultiInstanceRestartConfig{
MaxRestarts: intPtr(20),
InitBackoffSec: intPtr(2),
MaxBackoffSec: intPtr(60),
StableAfterSec: intPtr(300),
},
},
Timeouts: TimeoutsConfig{
ActionSec: 30,
NavigateSec: 60,
},
}
errs := ValidateFileConfig(fc)
if len(errs) > 0 {
t.Errorf("expected no errors for valid config, got: %v", errs)
}
}
func TestValidateFileConfig_RestartPolicy(t *testing.T) {
tests := []struct {
name string
restart MultiInstanceRestartConfig
wantErr bool
}{
{
name: "bounded",
restart: MultiInstanceRestartConfig{
MaxRestarts: intPtr(10),
InitBackoffSec: intPtr(2),
MaxBackoffSec: intPtr(60),
StableAfterSec: intPtr(300),
},
wantErr: false,
},
{
name: "unlimited",
restart: MultiInstanceRestartConfig{
MaxRestarts: intPtr(-1),
InitBackoffSec: intPtr(2),
MaxBackoffSec: intPtr(60),
StableAfterSec: intPtr(300),
},
wantErr: false,
},
{
name: "zero max restarts valid (no restarts)",
restart: MultiInstanceRestartConfig{
MaxRestarts: intPtr(0),
},
wantErr: false,
},
{
name: "negative max restarts invalid (except -1)",
restart: MultiInstanceRestartConfig{
MaxRestarts: intPtr(-2),
},
wantErr: true,
},
{
name: "max backoff lower than init invalid",
restart: MultiInstanceRestartConfig{
InitBackoffSec: intPtr(10),
MaxBackoffSec: intPtr(5),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fc := &FileConfig{
MultiInstance: MultiInstanceConfig{Restart: tt.restart},
}
errs := ValidateFileConfig(fc)
hasErr := len(errs) > 0
if hasErr != tt.wantErr {
t.Fatalf("got error=%v, want %v (errs: %v)", hasErr, tt.wantErr, errs)
}
})
}
}
func intPtr(v int) *int { return &v }
func TestValidateFileConfig_InvalidPort(t *testing.T) {
tests := []struct {
port string
wantErr bool
}{
{"9867", false},
{"1", false},
{"65535", false},
{"0", true},
{"65536", true},
{"-1", true},
{"abc", true},
{"", false}, // empty is ok (uses default)
}
for _, tt := range tests {
fc := &FileConfig{
Server: ServerConfig{Port: tt.port},
}
errs := ValidateFileConfig(fc)
hasErr := len(errs) > 0
if hasErr != tt.wantErr {
t.Errorf("port=%q: got error=%v, want error=%v (errs: %v)", tt.port, hasErr, tt.wantErr, errs)
}
}
}
func TestValidateFileConfig_InvalidStealthLevel(t *testing.T) {
tests := []struct {
level string
wantErr bool
}{
{"light", false},
{"full", false},
{"", false}, // empty is ok
{"medium", true}, // removed, no longer valid
{"none", true},
{"max", true},
{"LIGHT", true}, // case sensitive
}
for _, tt := range tests {
fc := &FileConfig{
InstanceDefaults: InstanceDefaultsConfig{StealthLevel: tt.level},
}
errs := ValidateFileConfig(fc)
hasErr := len(errs) > 0
if hasErr != tt.wantErr {
t.Errorf("stealthLevel=%q: got error=%v, want error=%v", tt.level, hasErr, tt.wantErr)
}
}
}
func TestValidateFileConfig_InvalidEvictionPolicy(t *testing.T) {
tests := []struct {
policy string
wantErr bool
}{
{"reject", false},
{"close_oldest", false},
{"close_lru", false},
{"", false},
{"drop", true},
{"lru", true},
}
for _, tt := range tests {
fc := &FileConfig{
InstanceDefaults: InstanceDefaultsConfig{TabEvictionPolicy: tt.policy},
}
errs := ValidateFileConfig(fc)
hasErr := len(errs) > 0
if hasErr != tt.wantErr {
t.Errorf("tabEvictionPolicy=%q: got error=%v, want error=%v", tt.policy, hasErr, tt.wantErr)
}
}
}
func TestValidateFileConfig_InvalidStrategy(t *testing.T) {
tests := []struct {
strategy string
wantErr bool
}{
{"simple", false},
{"explicit", false},
{"simple-autorestart", false},
{"always-on", false},
{"", false},
{"auto", true},
{"default", true},
}
for _, tt := range tests {
fc := &FileConfig{
MultiInstance: MultiInstanceConfig{Strategy: tt.strategy},
}
errs := ValidateFileConfig(fc)
hasErr := len(errs) > 0
if hasErr != tt.wantErr {
t.Errorf("strategy=%q: got error=%v, want error=%v", tt.strategy, hasErr, tt.wantErr)
}
}
}
func TestValidateFileConfig_InvalidAllocationPolicy(t *testing.T) {
tests := []struct {
policy string
wantErr bool
}{
{"fcfs", false},
{"round_robin", false},
{"random", false},
{"", false},
{"fifo", true},
{"roundrobin", true}, // underscore required
}
for _, tt := range tests {
fc := &FileConfig{
MultiInstance: MultiInstanceConfig{AllocationPolicy: tt.policy},
}
errs := ValidateFileConfig(fc)
hasErr := len(errs) > 0
if hasErr != tt.wantErr {
t.Errorf("allocationPolicy=%q: got error=%v, want error=%v", tt.policy, hasErr, tt.wantErr)
}
}
}
func TestValidateFileConfig_InvalidAttachScheme(t *testing.T) {
tests := []struct {
schemes []string
wantErr bool
}{
{[]string{"ws"}, false},
{[]string{"wss"}, false},
{[]string{"ws", "wss"}, false},
{[]string{"http"}, false},
{[]string{"https"}, false},
{[]string{"ws", "https"}, false},
{[]string{"ftp"}, true},
{[]string{"ws", "tcp"}, true},
}
for _, tt := range tests {
fc := &FileConfig{
Security: SecurityConfig{
Attach: AttachConfig{AllowSchemes: tt.schemes},
},
}
errs := ValidateFileConfig(fc)
hasErr := len(errs) > 0
if hasErr != tt.wantErr {
t.Errorf("allowSchemes=%v: got error=%v, want error=%v", tt.schemes, hasErr, tt.wantErr)
}
}
}
func TestValidateFileConfig_InvalidMaxTabs(t *testing.T) {
zero := 0
negative := -1
positive := 10
tests := []struct {
name string
maxTabs *int
wantErr bool
}{
{"nil", nil, false},
{"positive", &positive, false},
{"zero", &zero, true},
{"negative", &negative, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fc := &FileConfig{
InstanceDefaults: InstanceDefaultsConfig{MaxTabs: tt.maxTabs},
}
errs := ValidateFileConfig(fc)
hasErr := len(errs) > 0
if hasErr != tt.wantErr {
t.Errorf("maxTabs=%v: got error=%v, want error=%v", tt.maxTabs, hasErr, tt.wantErr)
}
})
}
}
func TestValidateFileConfig_InvalidTimeouts(t *testing.T) {
fc := &FileConfig{
Timeouts: TimeoutsConfig{
ActionSec: -1,
NavigateSec: -1,
ShutdownSec: -1,
WaitNavMs: -1,
},
}
errs := ValidateFileConfig(fc)
if len(errs) != 4 {
t.Errorf("expected 4 timeout errors, got %d: %v", len(errs), errs)
}
}
func TestValidateFileConfig_InstancePortRange(t *testing.T) {
start := 9900
end := 9800 // invalid: start > end
fc := &FileConfig{
Server: ServerConfig{},
MultiInstance: MultiInstanceConfig{
InstancePortStart: &start,
InstancePortEnd: &end,
},
}
errs := ValidateFileConfig(fc)
if len(errs) != 1 {
t.Errorf("expected 1 error for invalid port range, got %d: %v", len(errs), errs)
}
if len(errs) > 0 && !strings.Contains(errs[0].Error(), "start port") {
t.Errorf("expected port range error, got: %v", errs[0])
}
}
func TestValidateFileConfig_MultipleErrors(t *testing.T) {
zero := 0
fc := &FileConfig{
Server: ServerConfig{
Port: "99999", // invalid
},
InstanceDefaults: InstanceDefaultsConfig{
MaxTabs: &zero, // invalid
StealthLevel: "superstealth", // invalid
TabEvictionPolicy: "delete_oldest", // invalid
},
MultiInstance: MultiInstanceConfig{
Strategy: "magical", // invalid
AllocationPolicy: "balanced", // invalid
},
}
errs := ValidateFileConfig(fc)
if len(errs) < 5 {
t.Errorf("expected at least 5 errors, got %d: %v", len(errs), errs)
}
}
func TestValidationError_Error(t *testing.T) {
err := ValidationError{
Field: "server.port",
Message: "port out of range",
}
expected := "server.port: port out of range"
if err.Error() != expected {
t.Errorf("got %q, want %q", err.Error(), expected)
}
}
func TestValidEnumValues(t *testing.T) {
// Test that the valid values match the validation functions
for _, level := range ValidStealthLevels() {
if !isValidStealthLevel(level) {
t.Errorf("ValidStealthLevels contains %q but isValidStealthLevel returns false", level)
}
}
for _, policy := range ValidEvictionPolicies() {
if !isValidEvictionPolicy(policy) {
t.Errorf("ValidEvictionPolicies contains %q but isValidEvictionPolicy returns false", policy)
}
}
for _, strategy := range ValidStrategies() {
if !isValidStrategy(strategy) {
t.Errorf("ValidStrategies contains %q but isValidStrategy returns false", strategy)
}
}
for _, policy := range ValidAllocationPolicies() {
if !isValidAllocationPolicy(policy) {
t.Errorf("ValidAllocationPolicies contains %q but isValidAllocationPolicy returns false", policy)
}
}
for _, scheme := range ValidAttachSchemes() {
if !isValidAttachScheme(scheme) {
t.Errorf("ValidAttachSchemes contains %q but isValidAttachScheme returns false", scheme)
}
}
}
// --- IDPI validation tests ---
// TestValidateIDPIConfig_Disabled verifies that a disabled IDPI config produces
// no errors regardless of what fields are set.
func TestValidateIDPIConfig_Disabled(t *testing.T) {
errs := validateIDPIConfig(IDPIConfig{
Enabled: false,
AllowedDomains: []string{"", " ", "file:///etc/passwd"},
CustomPatterns: []string{"", " "},
})
if len(errs) != 0 {
t.Errorf("expected no errors when IDPI disabled, got: %v", errs)
}
}
// TestValidateIDPIConfig_ValidConfig verifies that a well-formed enabled config
// produces no errors.
func TestValidateIDPIConfig_ValidConfig(t *testing.T) {
errs := validateIDPIConfig(IDPIConfig{
Enabled: true,
AllowedDomains: []string{"example.com", "*.example.com", "*"},
CustomPatterns: []string{"exfiltrate this", "data leak"},
})
if len(errs) != 0 {
t.Errorf("expected no errors for valid IDPI config, got: %v", errs)
}
}
// TestValidateIDPIConfig_EmptyDomain verifies that an empty or whitespace-only
// domain pattern is rejected.
func TestValidateIDPIConfig_EmptyDomain(t *testing.T) {
cases := []struct {
name string
domain string
}{
{"empty string", ""},
{"spaces only", " "},
{"tab only", "\t"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
errs := validateIDPIConfig(IDPIConfig{
Enabled: true,
AllowedDomains: []string{tc.domain},
})
if len(errs) == 0 {
t.Errorf("expected error for empty domain %q, got none", tc.domain)
}
if len(errs) > 0 && !strings.Contains(errs[0].Error(), "security.idpi.allowedDomains") {
t.Errorf("expected field name in error, got: %v", errs[0])
}
})
}
}
// TestValidateIDPIConfig_DomainWithInternalWhitespace verifies that a domain
// pattern containing internal spaces is rejected.
func TestValidateIDPIConfig_DomainWithInternalWhitespace(t *testing.T) {
errs := validateIDPIConfig(IDPIConfig{
Enabled: true,
AllowedDomains: []string{"example .com"},
})
if len(errs) == 0 {
t.Error("expected error for domain with internal whitespace, got none")
}
}
// TestValidateIDPIConfig_FileSchemeBlocked verifies that file:// domain patterns
// are rejected because they cannot represent a valid network host.
func TestValidateIDPIConfig_FileSchemeBlocked(t *testing.T) {
for _, pattern := range []string{
"file:///etc/passwd",
"file://localhost/etc/passwd",
} {
errs := validateIDPIConfig(IDPIConfig{
Enabled: true,
AllowedDomains: []string{pattern},
})
if len(errs) == 0 {
t.Errorf("expected error for file:// pattern %q, got none", pattern)
}
if len(errs) > 0 && !strings.Contains(errs[0].Error(), "file://") {
t.Errorf("expected file:// mention in error, got: %v", errs[0])
}
}
}
// TestValidateIDPIConfig_EmptyCustomPattern verifies that an empty or
// whitespace-only custom pattern is rejected.
func TestValidateIDPIConfig_EmptyCustomPattern(t *testing.T) {
cases := []string{"", " ", "\t"}
for _, p := range cases {
errs := validateIDPIConfig(IDPIConfig{
Enabled: true,
CustomPatterns: []string{p},
})
if len(errs) == 0 {
t.Errorf("expected error for empty custom pattern %q, got none", p)
}
if len(errs) > 0 && !strings.Contains(errs[0].Error(), "customPatterns") {
t.Errorf("expected customPatterns field in error, got: %v", errs[0])
}
}
}
// TestValidateIDPIConfig_MultipleErrors ensures all IDPI violations are
// accumulated rather than short-circuited.
func TestValidateIDPIConfig_MultipleErrors(t *testing.T) {
errs := validateIDPIConfig(IDPIConfig{
Enabled: true,
AllowedDomains: []string{"", "file:///bad"},
CustomPatterns: []string{"", " "},
})
if len(errs) < 4 {
t.Errorf("expected at least 4 IDPI errors, got %d: %v", len(errs), errs)
}
}
// TestValidateFileConfig_IDPIPassthrough verifies that ValidateFileConfig
// surfaces IDPI errors alongside other config errors.
func TestValidateFileConfig_IDPIPassthrough(t *testing.T) {
fc := &FileConfig{
Security: SecurityConfig{
IDPI: IDPIConfig{
Enabled: true,
AllowedDomains: []string{""}, // invalid
CustomPatterns: []string{" "}, // invalid
},
},
}
errs := ValidateFileConfig(fc)
if len(errs) < 2 {
t.Errorf("expected at least 2 IDPI errors via ValidateFileConfig, got %d: %v", len(errs), errs)
}
}
// TestValidateIDPIConfig_ScanTimeoutSec verifies that negative values are rejected
// and zero/positive values are accepted (zero means use the default).
func TestValidateIDPIConfig_ScanTimeoutSec(t *testing.T) {
t.Run("NegativeIsInvalid", func(t *testing.T) {
errs := validateIDPIConfig(IDPIConfig{
Enabled: true,
ScanTimeoutSec: -1,
})
if len(errs) == 0 {
t.Error("expected error for negative scanTimeoutSec, got none")
}
if len(errs) > 0 && !strings.Contains(errs[0].Error(), "scanTimeoutSec") {
t.Errorf("expected scanTimeoutSec field in error, got: %v", errs[0])
}
})
t.Run("ZeroIsValid", func(t *testing.T) {
errs := validateIDPIConfig(IDPIConfig{
Enabled: true,
ScanTimeoutSec: 0, // zero → use built-in default of 5s
})
if len(errs) != 0 {
t.Errorf("expected no error for scanTimeoutSec=0, got: %v", errs)
}
})
t.Run("PositiveIsValid", func(t *testing.T) {
errs := validateIDPIConfig(IDPIConfig{
Enabled: true,
ScanTimeoutSec: 10,
})
if len(errs) != 0 {
t.Errorf("expected no error for scanTimeoutSec=10, got: %v", errs)
}
})
}
|