-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add fuzz testing for CLI #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
282ec11
feat: add fuzz testing for CLI
swarna1101 02c7fad
fix
swarna1101 f7333fc
Merge main into feature/add-fuzz-tests
swarna1101 2947c75
fix: address fuzz test review comments
swarna1101 9c3d517
fix: make token setup optional for fuzz tests
swarna1101 46164e1
fix
swarna1101 47a1b91
fix
swarna1101 4361ae8
Improve fuzz test setup with timeouts and better error handling
swarna1101 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // FuzzPublishTopicName tests the publish command with a topic name | ||
| func FuzzPublishTopicName(f *testing.F) { | ||
| require.NotEmpty(f, cliBinaryPath, "CLI binary path must be set by TestMain") | ||
|
|
||
| f.Add("") | ||
| f.Add("\x00") | ||
| f.Add("../../../etc/passwd") | ||
| f.Add(strings.Repeat("a", 1000)) | ||
| f.Add("test\n\r\t") | ||
| f.Add("test'; DROP TABLE") | ||
| f.Add("${HOME}") | ||
|
|
||
| f.Fuzz(func(t *testing.T, topic string) { | ||
| if len(topic) > 5000 { | ||
| t.Skip() | ||
| } | ||
|
|
||
| // For obviously invalid input, verify graceful error handling | ||
| if strings.Contains(topic, "\x00") { | ||
| args := []string{"publish", "--topic=" + topic, "--message=test"} | ||
| out, err := RunCommand(cliBinaryPath, args...) | ||
| require.Error(t, err, "CLI should reject null bytes in topic name") | ||
| // Verify error is handled gracefully (not a panic) | ||
| if strings.Contains(out, "panic") || strings.Contains(out, "fatal") { | ||
| t.Fatalf("CLI panicked or crashed on topic with null byte: %v\nOutput: %s", err, out) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| args := []string{"publish", "--topic=" + topic, "--message=test"} | ||
| out, err := RunCommand(cliBinaryPath, args...) | ||
| // For fuzzing, we need to detect failures - invalid topics should fail gracefully | ||
| // Valid topics might succeed (if subscribed) or fail (if not subscribed) | ||
| // The key is that the CLI should handle the input without panicking | ||
| if err != nil { | ||
| // Any error should be handled gracefully (not a panic or crash) | ||
| if strings.Contains(out, "panic") || strings.Contains(out, "fatal") { | ||
| t.Fatalf("CLI panicked or crashed on topic %q: %v\nOutput: %s", topic, err, out) | ||
| } | ||
swarna1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // For invalid topics, errors are expected and acceptable | ||
| t.Logf("Command failed (expected for invalid input): %v\nOutput: %s", err, out) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // FuzzPublishMessage tests the publish command with a message. | ||
| // This is distinct from FuzzPublishTopicName which tests topic name validation; | ||
| // this test focuses on message content validation and handling. | ||
| func FuzzPublishMessage(f *testing.F) { | ||
swarna1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| require.NotEmpty(f, cliBinaryPath, "CLI binary path must be set by TestMain") | ||
|
|
||
| f.Add("") | ||
| f.Add("\x00") | ||
| f.Add(strings.Repeat("a", 10000)) | ||
| f.Add("{\"test\": \"value\"}") | ||
| f.Add("test<script>alert(1)</script>") | ||
|
|
||
| f.Fuzz(func(t *testing.T, message string) { | ||
| if len(message) > 50000 { | ||
| t.Skip() | ||
| } | ||
|
|
||
| // For obviously invalid input, verify graceful error handling | ||
| if strings.Contains(message, "\x00") { | ||
| args := []string{"publish", "--topic=fuzz-test", "--message=" + message} | ||
| out, err := RunCommand(cliBinaryPath, args...) | ||
| require.Error(t, err, "CLI should reject null bytes in message") | ||
| // Verify error is handled gracefully (not a panic) | ||
| if strings.Contains(out, "panic") || strings.Contains(out, "fatal") { | ||
| t.Fatalf("CLI panicked or crashed on message with null byte: %v\nOutput: %s", err, out) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| args := []string{"publish", "--topic=fuzz-test", "--message=" + message} | ||
| out, err := RunCommand(cliBinaryPath, args...) | ||
| // For fuzzing, we need to detect failures - invalid messages should fail gracefully | ||
| // Valid messages might succeed (if topic is subscribed) or fail (if not subscribed) | ||
| // The key is that the CLI should handle the input without panicking | ||
| if err != nil { | ||
| // Any error should be handled gracefully (not a panic or crash) | ||
| if strings.Contains(out, "panic") || strings.Contains(out, "fatal") { | ||
| t.Fatalf("CLI panicked or crashed on message %q: %v\nOutput: %s", message, err, out) | ||
| } | ||
| // For invalid messages, errors are expected and acceptable | ||
| t.Logf("Command failed (expected for invalid input): %v\nOutput: %s", err, out) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // FuzzServiceURL tests the health command with a service URL | ||
| func FuzzServiceURL(f *testing.F) { | ||
| require.NotEmpty(f, cliBinaryPath, "CLI binary path must be set by TestMain") | ||
|
|
||
| f.Add("not-a-url") | ||
| f.Add("://broken") | ||
| f.Add("http://") | ||
| f.Add("http://localhost:-8080") | ||
swarna1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| f.Add("http://localhost:99999") | ||
| f.Add("javascript:alert(1)") | ||
| f.Add("\x00") | ||
|
|
||
| f.Fuzz(func(t *testing.T, url string) { | ||
| if len(url) > 1000 { | ||
| t.Skip() | ||
| } | ||
|
|
||
| // For obviously invalid input, verify graceful error handling | ||
| if strings.Contains(url, "\x00") { | ||
| args := []string{"health", "--service-url=" + url} | ||
| out, err := RunCommand(cliBinaryPath, args...) | ||
| require.Error(t, err, "CLI should reject null bytes in service URL") | ||
| // Verify error is handled gracefully (not a panic) | ||
| if strings.Contains(out, "panic") || strings.Contains(out, "fatal") { | ||
| t.Fatalf("CLI panicked or crashed on service URL with null byte: %v\nOutput: %s", err, out) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| args := []string{"health", "--service-url=" + url} | ||
| out, err := RunCommand(cliBinaryPath, args...) | ||
| // For fuzzing, we need to detect failures - invalid URLs should fail gracefully | ||
| // Valid URLs might succeed (if proxy is reachable) or fail (if not) | ||
| // The key is that the CLI should handle the input without panicking | ||
| if err != nil { | ||
| // Any error should be handled gracefully (not a panic or crash) | ||
| if strings.Contains(out, "panic") || strings.Contains(out, "fatal") { | ||
| t.Fatalf("CLI panicked or crashed on URL %q: %v\nOutput: %s", url, err, out) | ||
| } | ||
| // For invalid URLs, errors are expected and acceptable | ||
| t.Logf("Command failed (expected for invalid URL): %v\nOutput: %s", err, out) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // FuzzFilePath tests the publish command with a file path | ||
| func FuzzFilePath(f *testing.F) { | ||
| require.NotEmpty(f, cliBinaryPath, "CLI binary path must be set by TestMain") | ||
|
|
||
| f.Add("") | ||
| f.Add("nonexistent.txt") | ||
| f.Add("../../../etc/passwd") | ||
| f.Add("/dev/null") | ||
| f.Add("test\x00file.txt") | ||
| f.Add(strings.Repeat("a", 500) + ".txt") | ||
|
|
||
| f.Fuzz(func(t *testing.T, filepath string) { | ||
| if len(filepath) > 2000 { | ||
| t.Skip() | ||
| } | ||
|
|
||
| // For obviously invalid input, verify graceful error handling | ||
| if strings.Contains(filepath, "\x00") { | ||
| args := []string{"publish", "--topic=fuzz-test", "--file=" + filepath} | ||
| out, err := RunCommand(cliBinaryPath, args...) | ||
| require.Error(t, err, "CLI should reject null bytes in file path") | ||
| // Verify error is handled gracefully (not a panic) | ||
| if strings.Contains(out, "panic") || strings.Contains(out, "fatal") { | ||
| t.Fatalf("CLI panicked or crashed on file path with null byte: %v\nOutput: %s", err, out) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| args := []string{"publish", "--topic=fuzz-test", "--file=" + filepath} | ||
| out, err := RunCommand(cliBinaryPath, args...) | ||
| // For fuzzing, we need to detect failures - invalid file paths should fail gracefully | ||
| // Valid file paths might succeed (if file exists and topic is subscribed) or fail (if not) | ||
| // The key is that the CLI should handle the input without panicking | ||
| if err != nil { | ||
| // Any error should be handled gracefully (not a panic or crash) | ||
| if strings.Contains(out, "panic") || strings.Contains(out, "fatal") { | ||
| t.Fatalf("CLI panicked or crashed on file path %q: %v\nOutput: %s", filepath, err, out) | ||
| } | ||
| // For invalid file paths, errors are expected and acceptable | ||
| t.Logf("Command failed (expected for invalid file path): %v\nOutput: %s", err, out) | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.