|
1 | 1 | package command |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net" |
| 5 | + "os" |
4 | 6 | "reflect" |
5 | 7 | "testing" |
6 | 8 | ) |
@@ -211,3 +213,288 @@ func TestExpandList(t *testing.T) { |
211 | 213 | }) |
212 | 214 | } |
213 | 215 | } |
| 216 | + |
| 217 | +func TestParseCidr(t *testing.T) { |
| 218 | + tests := []struct { |
| 219 | + name string |
| 220 | + input []string |
| 221 | + expected []string |
| 222 | + }{ |
| 223 | + { |
| 224 | + name: "no CIDR notation", |
| 225 | + input: []string{"example.com", "8.8.8.8"}, |
| 226 | + expected: []string{"example.com", "8.8.8.8"}, |
| 227 | + }, |
| 228 | + { |
| 229 | + name: "single host /32", |
| 230 | + input: []string{"192.168.1.1/32"}, |
| 231 | + expected: []string{"192.168.1.1"}, |
| 232 | + }, |
| 233 | + { |
| 234 | + name: "small subnet /30", |
| 235 | + input: []string{"192.168.1.0/30"}, |
| 236 | + expected: []string{"192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3"}, |
| 237 | + }, |
| 238 | + { |
| 239 | + name: "IPv6 single host /128", |
| 240 | + input: []string{"2001:db8::1/128"}, |
| 241 | + expected: []string{"2001:db8::1"}, |
| 242 | + }, |
| 243 | + { |
| 244 | + name: "mixed input with and without CIDR", |
| 245 | + input: []string{"example.com", "192.168.1.0/31", "8.8.8.8"}, |
| 246 | + expected: []string{"example.com", "192.168.1.0", "192.168.1.1", "8.8.8.8"}, |
| 247 | + }, |
| 248 | + { |
| 249 | + name: "invalid CIDR notation", |
| 250 | + input: []string{"192.168.1.0/33", "invalid/cidr"}, |
| 251 | + expected: []string{"192.168.1.0/33", "invalid/cidr"}, |
| 252 | + }, |
| 253 | + } |
| 254 | + |
| 255 | + for _, tt := range tests { |
| 256 | + t.Run(tt.name, func(t *testing.T) { |
| 257 | + result := parseCidr(tt.input) |
| 258 | + if !reflect.DeepEqual(result, tt.expected) { |
| 259 | + t.Errorf("parseCidr() = %v, expected %v", result, tt.expected) |
| 260 | + } |
| 261 | + }) |
| 262 | + } |
| 263 | +} |
| 264 | + |
| 265 | +func TestIpInc(t *testing.T) { |
| 266 | + tests := []struct { |
| 267 | + name string |
| 268 | + input string |
| 269 | + expected string |
| 270 | + }{ |
| 271 | + { |
| 272 | + name: "IPv4 increment", |
| 273 | + input: "192.168.1.1", |
| 274 | + expected: "192.168.1.2", |
| 275 | + }, |
| 276 | + { |
| 277 | + name: "IPv4 overflow to next octet", |
| 278 | + input: "192.168.1.255", |
| 279 | + expected: "192.168.2.0", |
| 280 | + }, |
| 281 | + { |
| 282 | + name: "IPv6 increment", |
| 283 | + input: "2001:db8::1", |
| 284 | + expected: "2001:db8::2", |
| 285 | + }, |
| 286 | + } |
| 287 | + |
| 288 | + for _, tt := range tests { |
| 289 | + t.Run(tt.name, func(t *testing.T) { |
| 290 | + ip := net.ParseIP(tt.input) |
| 291 | + if ip == nil { |
| 292 | + t.Fatalf("Invalid IP address: %s", tt.input) |
| 293 | + } |
| 294 | + ipInc(ip) |
| 295 | + if ip.String() != tt.expected { |
| 296 | + t.Errorf("ipInc() = %v, expected %v", ip.String(), tt.expected) |
| 297 | + } |
| 298 | + }) |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | +func TestFile2hostnames(t *testing.T) { |
| 303 | + tests := []struct { |
| 304 | + name string |
| 305 | + content string |
| 306 | + expected []string |
| 307 | + }{ |
| 308 | + { |
| 309 | + name: "simple hostnames", |
| 310 | + content: "example.com\ngoogle.com\n8.8.8.8", |
| 311 | + expected: []string{"example.com", "google.com", "8.8.8.8"}, |
| 312 | + }, |
| 313 | + { |
| 314 | + name: "with comments", |
| 315 | + content: "# This is a comment\nexample.com # inline comment\n; semicolon comment\ngoogle.com", |
| 316 | + expected: []string{"example.com", "google.com"}, |
| 317 | + }, |
| 318 | + { |
| 319 | + name: "with empty lines and whitespace", |
| 320 | + content: "\n example.com \n\n\tgoogle.com\t\n\n", |
| 321 | + expected: []string{"example.com", "google.com"}, |
| 322 | + }, |
| 323 | + { |
| 324 | + name: "URLs with # in them", |
| 325 | + content: "http://example.com#anchor\nhttps://test.com/path#hash", |
| 326 | + expected: []string{"http://example.com#anchor", "https://test.com/path#hash"}, |
| 327 | + }, |
| 328 | + { |
| 329 | + name: "mixed comments and URLs", |
| 330 | + content: "# Configuration file\nhttp://api.example.com # API endpoint\n; Another comment\nhttps://web.example.com#main", |
| 331 | + expected: []string{"http://api.example.com", "https://web.example.com#main"}, |
| 332 | + }, |
| 333 | + { |
| 334 | + name: "protocol prefixes", |
| 335 | + content: "icmpv4://example.com\nhttps://api.example.com\ntcp://db.example.com:5432", |
| 336 | + expected: []string{"icmpv4://example.com", "https://api.example.com", "tcp://db.example.com:5432"}, |
| 337 | + }, |
| 338 | + { |
| 339 | + name: "CIDR notation", |
| 340 | + content: "192.168.1.0/24\n10.0.0.0/16 # Internal network", |
| 341 | + expected: []string{"192.168.1.0/24", "10.0.0.0/16"}, |
| 342 | + }, |
| 343 | + } |
| 344 | + |
| 345 | + for _, tt := range tests { |
| 346 | + t.Run(tt.name, func(t *testing.T) { |
| 347 | + // Create a temporary file with test content |
| 348 | + tmpfile, err := os.CreateTemp("", "test_hostnames_*.txt") |
| 349 | + if err != nil { |
| 350 | + t.Fatalf("Failed to create temp file: %v", err) |
| 351 | + } |
| 352 | + defer os.Remove(tmpfile.Name()) |
| 353 | + defer tmpfile.Close() |
| 354 | + |
| 355 | + if _, err := tmpfile.WriteString(tt.content); err != nil { |
| 356 | + t.Fatalf("Failed to write to temp file: %v", err) |
| 357 | + } |
| 358 | + |
| 359 | + // Reset file pointer to beginning |
| 360 | + if _, err := tmpfile.Seek(0, 0); err != nil { |
| 361 | + t.Fatalf("Failed to seek temp file: %v", err) |
| 362 | + } |
| 363 | + |
| 364 | + result := file2hostnames(tmpfile) |
| 365 | + if !reflect.DeepEqual(result, tt.expected) { |
| 366 | + t.Errorf("file2hostnames() = %v, expected %v", result, tt.expected) |
| 367 | + } |
| 368 | + }) |
| 369 | + } |
| 370 | +} |
| 371 | + |
| 372 | +func TestParseHostnames(t *testing.T) { |
| 373 | + tests := []struct { |
| 374 | + name string |
| 375 | + args []string |
| 376 | + fileContent string |
| 377 | + expected []string |
| 378 | + }{ |
| 379 | + { |
| 380 | + name: "args only", |
| 381 | + args: []string{"example.com", "google.com"}, |
| 382 | + expected: []string{"example.com", "google.com"}, |
| 383 | + }, |
| 384 | + { |
| 385 | + name: "file only", |
| 386 | + args: []string{}, |
| 387 | + fileContent: "example.com\ngoogle.com", |
| 388 | + expected: []string{"example.com", "google.com"}, |
| 389 | + }, |
| 390 | + { |
| 391 | + name: "args and file combined", |
| 392 | + args: []string{"cli-host.com"}, |
| 393 | + fileContent: "file-host.com", |
| 394 | + expected: []string{"file-host.com", "cli-host.com"}, |
| 395 | + }, |
| 396 | + { |
| 397 | + name: "with bracket expansion", |
| 398 | + args: []string{"server[1-2].example.com"}, |
| 399 | + fileContent: "web[01-02].example.com", |
| 400 | + expected: []string{"web01.example.com", "web02.example.com", "server1.example.com", "server2.example.com"}, |
| 401 | + }, |
| 402 | + { |
| 403 | + name: "with CIDR expansion", |
| 404 | + args: []string{"192.168.1.0/30"}, |
| 405 | + fileContent: "10.0.0.0/31", |
| 406 | + expected: []string{"10.0.0.0", "10.0.0.1", "192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3"}, |
| 407 | + }, |
| 408 | + { |
| 409 | + name: "combined bracket and CIDR expansion", |
| 410 | + args: []string{"server[1-2].example.com", "192.168.1.0/30"}, |
| 411 | + fileContent: "web[a-b].example.com", |
| 412 | + expected: []string{"weba.example.com", "webb.example.com", "server1.example.com", "server2.example.com", "192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3"}, |
| 413 | + }, |
| 414 | + } |
| 415 | + |
| 416 | + for _, tt := range tests { |
| 417 | + t.Run(tt.name, func(t *testing.T) { |
| 418 | + var fpath string |
| 419 | + if tt.fileContent != "" { |
| 420 | + // Create temporary file |
| 421 | + tmpfile, err := os.CreateTemp("", "test_parse_*.txt") |
| 422 | + if err != nil { |
| 423 | + t.Fatalf("Failed to create temp file: %v", err) |
| 424 | + } |
| 425 | + defer os.Remove(tmpfile.Name()) |
| 426 | + |
| 427 | + if _, err := tmpfile.WriteString(tt.fileContent); err != nil { |
| 428 | + t.Fatalf("Failed to write to temp file: %v", err) |
| 429 | + } |
| 430 | + tmpfile.Close() |
| 431 | + fpath = tmpfile.Name() |
| 432 | + } |
| 433 | + |
| 434 | + result := parseHostnames(tt.args, fpath) |
| 435 | + if !reflect.DeepEqual(result, tt.expected) { |
| 436 | + t.Errorf("parseHostnames() = %v, expected %v", result, tt.expected) |
| 437 | + } |
| 438 | + }) |
| 439 | + } |
| 440 | +} |
| 441 | + |
| 442 | +func TestParseHostnamesIntegration(t *testing.T) { |
| 443 | + // Test the complete flow: file reading -> bracket expansion -> CIDR expansion |
| 444 | + fileContent := `# Test configuration |
| 445 | +# Web servers with bracket expansion |
| 446 | +web[01-02].prod.example.com |
| 447 | +
|
| 448 | +# Database subnet |
| 449 | +10.0.1.0/30 # DB cluster IPs |
| 450 | +
|
| 451 | +# Mixed protocols |
| 452 | +https://api[1-2].example.com |
| 453 | +icmpv4://monitor.example.com |
| 454 | +` |
| 455 | + |
| 456 | + tmpfile, err := os.CreateTemp("", "test_integration_*.txt") |
| 457 | + if err != nil { |
| 458 | + t.Fatalf("Failed to create temp file: %v", err) |
| 459 | + } |
| 460 | + defer os.Remove(tmpfile.Name()) |
| 461 | + |
| 462 | + if _, err := tmpfile.WriteString(fileContent); err != nil { |
| 463 | + t.Fatalf("Failed to write to temp file: %v", err) |
| 464 | + } |
| 465 | + tmpfile.Close() |
| 466 | + |
| 467 | + args := []string{"server[a-b].test.com", "192.168.1.0/31"} |
| 468 | + result := parseHostnames(args, tmpfile.Name()) |
| 469 | + |
| 470 | + expected := []string{ |
| 471 | + // From file: bracket expansion |
| 472 | + "web01.prod.example.com", "web02.prod.example.com", |
| 473 | + // From file: CIDR expansion |
| 474 | + "10.0.1.0", "10.0.1.1", "10.0.1.2", "10.0.1.3", |
| 475 | + // From file: protocols with bracket expansion |
| 476 | + "https://api1.example.com", "https://api2.example.com", |
| 477 | + // From file: plain hostname |
| 478 | + "icmpv4://monitor.example.com", |
| 479 | + // From args: bracket expansion |
| 480 | + "servera.test.com", "serverb.test.com", |
| 481 | + // From args: CIDR expansion |
| 482 | + "192.168.1.0", "192.168.1.1", |
| 483 | + } |
| 484 | + |
| 485 | + if !reflect.DeepEqual(result, expected) { |
| 486 | + t.Errorf("Integration test failed.\nGot: %v\nExpected: %v", result, expected) |
| 487 | + |
| 488 | + // Helper debug output |
| 489 | + t.Logf("Result length: %d, Expected length: %d", len(result), len(expected)) |
| 490 | + for i, item := range result { |
| 491 | + if i < len(expected) { |
| 492 | + if item != expected[i] { |
| 493 | + t.Logf(" [%d] Got: %q, Expected: %q", i, item, expected[i]) |
| 494 | + } |
| 495 | + } else { |
| 496 | + t.Logf(" [%d] Extra item: %q", i, item) |
| 497 | + } |
| 498 | + } |
| 499 | + } |
| 500 | +} |
0 commit comments