|
11 | 11 | package ims |
12 | 12 |
|
13 | 13 | import ( |
| 14 | + "math/rand" |
14 | 15 | "testing" |
| 16 | + "time" |
15 | 17 | ) |
16 | 18 |
|
17 | 19 | func TestValidateDecodeTokenConfig(t *testing.T) { |
@@ -393,3 +395,98 @@ func searchString(s, substr string) bool { |
393 | 395 | } |
394 | 396 | return false |
395 | 397 | } |
| 398 | + |
| 399 | +// randomString generates a string of the given length with arbitrary bytes. |
| 400 | +func randomString(rng *rand.Rand, length int) string { |
| 401 | + b := make([]byte, length) |
| 402 | + for i := range b { |
| 403 | + b[i] = byte(rng.Intn(256)) |
| 404 | + } |
| 405 | + return string(b) |
| 406 | +} |
| 407 | + |
| 408 | +// TestFuzzValidateURL generates random inputs for 10 seconds to verify that |
| 409 | +// validateURL never panics regardless of input. Runs in parallel with other tests. |
| 410 | +// |
| 411 | +// For deeper exploration, use Go's built-in fuzz engine: |
| 412 | +// |
| 413 | +// go test -fuzz=FuzzValidateURL -fuzztime=60s ./ims/ |
| 414 | +func TestFuzzValidateURL(t *testing.T) { |
| 415 | + t.Parallel() |
| 416 | + |
| 417 | + rng := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 418 | + deadline := time.After(10 * time.Second) |
| 419 | + iterations := 0 |
| 420 | + |
| 421 | + for { |
| 422 | + select { |
| 423 | + case <-deadline: |
| 424 | + t.Logf("fuzz: %d iterations without panic", iterations) |
| 425 | + return |
| 426 | + default: |
| 427 | + input := randomString(rng, rng.Intn(512)) |
| 428 | + _ = validateURL(input) |
| 429 | + iterations++ |
| 430 | + } |
| 431 | + } |
| 432 | +} |
| 433 | + |
| 434 | +// FuzzValidateURL is a standard Go fuzz target for deeper exploration. |
| 435 | +// Run manually: go test -fuzz=FuzzValidateURL -fuzztime=60s ./ims/ |
| 436 | +func FuzzValidateURL(f *testing.F) { |
| 437 | + f.Add("https://example.com") |
| 438 | + f.Add("http://localhost:8080") |
| 439 | + f.Add("") |
| 440 | + f.Add("not-a-url") |
| 441 | + f.Add("://missing-scheme.com") |
| 442 | + f.Add("https://") |
| 443 | + |
| 444 | + f.Fuzz(func(t *testing.T, u string) { |
| 445 | + _ = validateURL(u) |
| 446 | + }) |
| 447 | +} |
| 448 | + |
| 449 | +// TestFuzzDecodeToken generates random inputs for 10 seconds to verify that |
| 450 | +// DecodeToken never panics regardless of input. Runs in parallel with other tests. |
| 451 | +// |
| 452 | +// For deeper exploration, use Go's built-in fuzz engine: |
| 453 | +// |
| 454 | +// go test -fuzz=FuzzDecodeToken -fuzztime=60s ./ims/ |
| 455 | +func TestFuzzDecodeToken(t *testing.T) { |
| 456 | + t.Parallel() |
| 457 | + |
| 458 | + rng := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 459 | + deadline := time.After(10 * time.Second) |
| 460 | + iterations := 0 |
| 461 | + |
| 462 | + for { |
| 463 | + select { |
| 464 | + case <-deadline: |
| 465 | + t.Logf("fuzz: %d iterations without panic", iterations) |
| 466 | + return |
| 467 | + default: |
| 468 | + // Generate random JWT-like strings (three dot-separated parts) |
| 469 | + input := randomString(rng, rng.Intn(128)) + "." + |
| 470 | + randomString(rng, rng.Intn(256)) + "." + |
| 471 | + randomString(rng, rng.Intn(128)) |
| 472 | + c := Config{Token: input} |
| 473 | + _, _ = c.DecodeToken() |
| 474 | + iterations++ |
| 475 | + } |
| 476 | + } |
| 477 | +} |
| 478 | + |
| 479 | +// FuzzDecodeToken is a standard Go fuzz target for deeper exploration. |
| 480 | +// Run manually: go test -fuzz=FuzzDecodeToken -fuzztime=60s ./ims/ |
| 481 | +func FuzzDecodeToken(f *testing.F) { |
| 482 | + f.Add("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.signature") |
| 483 | + f.Add("a.b.c") |
| 484 | + f.Add("...") |
| 485 | + f.Add("") |
| 486 | + f.Add("no-dots-at-all") |
| 487 | + |
| 488 | + f.Fuzz(func(t *testing.T, token string) { |
| 489 | + c := Config{Token: token} |
| 490 | + _, _ = c.DecodeToken() |
| 491 | + }) |
| 492 | +} |
0 commit comments