Skip to content

Go Fuzz Testing: A Powerful Tool for Automated Vulnerability Discovery

About 2583 words

GotestingFuzzsecurity

What Is Fuzz Testing?

Fuzz testing is an automated software testing technique that discovers vulnerabilities and bugs by feeding programs large amounts of random, invalid, or unexpected data. Go has included built-in fuzz testing since version 1.18, making fuzzing accessible to developers.

Benefits of Fuzz Testing

  1. Discover boundary conditions automatically: No need to write large numbers of test cases manually
  2. Find hidden bugs: Uncover edge cases that developers may overlook
  3. Improve code quality: Identify potential crashes, panics, and security vulnerabilities
  4. Work with continuous integration: Integrate fuzzing into CI/CD pipelines

Go Fuzz Testing Basics

Basic Syntax

Go fuzz tests use f.Fuzz(). The basic structure is:

func FuzzFunctionName(f *testing.F) {
    f.Add(seed1, seed2, ...) // Add seed data
    f.Fuzz(func(t *testing.T, param1 type1, param2 type2, ...) {
        // Test logic
    })
}

Understanding fuzz.NewConsumer

fuzz.NewConsumer is a powerful feature introduced in Go 1.18+ that lets us create custom fuzz consumers to generate more complex, structured test data.

Basic Usage

import "testing/fuzz"

func FuzzWithConsumer(f *testing.F) {
    f.Add([]byte("seed data"))

    f.Fuzz(func(t *testing.T, data []byte) {
        // Create a fuzz consumer
        fc := fuzz.NewConsumer(data)

        // Use the consumer to generate different data types
        var str string
        fc.Fuzz(&str)

        var num int
        fc.Fuzz(&num)

        var flag bool
        fc.Fuzz(&flag)

        // Test logic...
    })
}

Advanced Usage Example

// advanced_consumer_test.go
package main

import (
    "testing"
    "testing/fuzz"
)

// UserProfile represents a user profile
type UserProfile struct {
    Username string
    Age      int
    Email    string
    IsActive bool
    Tags     []string
}

// CreateProfileFromFuzz creates a user profile from fuzz data
func CreateProfileFromFuzz(data []byte) (*UserProfile, error) {
    fc := fuzz.NewConsumer(data)

    var profile UserProfile

    // Generate a username with a length limit
    fc.Fuzz(&profile.Username)
    if len(profile.Username) > 50 {
        profile.Username = profile.Username[:50]
    }

    // Generate an age within a bounded range
    fc.Fuzz(&profile.Age)
    if profile.Age < 0 {
        profile.Age = 0
    }
    if profile.Age > 120 {
        profile.Age = 120
    }

    // Generate an email address
    fc.Fuzz(&profile.Email)

    // Generate a boolean value
    fc.Fuzz(&profile.IsActive)

    // Generate an array of tags
    fc.Fuzz(&profile.Tags)

    return &profile, nil
}

// FuzzUserProfile tests user profile creation
func FuzzUserProfile(f *testing.F) {
    // Add seed data
    seedData := []byte("test seed data")
    f.Add(seedData)

    f.Fuzz(func(t *testing.T, data []byte) {
        profile, err := CreateProfileFromFuzz(data)

        if err != nil {
            t.Errorf("Failed to create profile: %v", err)
            return
        }

        // Validate the generated profile
        if profile == nil {
            t.Error("Profile should not be nil")
            return
        }

        // Validate the username length
        if len(profile.Username) > 50 {
            t.Errorf("Username too long: %d characters", len(profile.Username))
        }

        // Validate the age range
        if profile.Age < 0 || profile.Age > 120 {
            t.Errorf("Invalid age: %d", profile.Age)
        }

        // Validate the tag array
        if profile.Tags == nil {
            t.Error("Tags should not be nil")
        }
    })
}

Custom Data Generators

// custom_fuzz_test.go
package main

import (
    "testing"
    "testing/fuzz"
    "math/rand"
)

// CustomDataGenerator is a custom data generator
type CustomDataGenerator struct {
    fc *fuzz.Consumer
}

func NewCustomDataGenerator(data []byte) *CustomDataGenerator {
    return &CustomDataGenerator{
        fc: fuzz.NewConsumer(data),
    }
}

// GenerateRandomString generates a random string of the specified length
func (cdg *CustomDataGenerator) GenerateRandomString(maxLen int) string {
    var length int
    cdg.fc.Fuzz(&length)

    if length < 0 {
        length = 0
    }
    if length > maxLen {
        length = maxLen
    }

    chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    result := make([]byte, length)

    for i := 0; i < length; i++ {
        var charIndex int
        cdg.fc.Fuzz(&charIndex)
        charIndex = charIndex % len(chars)
        result[i] = chars[charIndex]
    }

    return string(result)
}

// GenerateRandomEmail generates a random email address
func (cdg *CustomDataGenerator) GenerateRandomEmail() string {
    username := cdg.GenerateRandomString(20)
    domain := cdg.GenerateRandomString(10)
    return username + "@" + domain + ".com"
}

// GenerateRandomSlice generates a random slice
func (cdg *CustomDataGenerator) GenerateRandomSlice(maxLen int) []int {
    var length int
    cdg.fc.Fuzz(&length)

    if length < 0 {
        length = 0
    }
    if length > maxLen {
        length = maxLen
    }

    result := make([]int, length)
    for i := 0; i < length; i++ {
        cdg.fc.Fuzz(&result[i])
    }

    return result
}

// FuzzCustomGenerator tests the custom generator
func FuzzCustomGenerator(f *testing.F) {
    seedData := []byte("custom generator seed")
    f.Add(seedData)

    f.Fuzz(func(t *testing.T, data []byte) {
        generator := NewCustomDataGenerator(data)

        // Generate a random string
        randomStr := generator.GenerateRandomString(100)
        if len(randomStr) > 100 {
            t.Errorf("String too long: %d", len(randomStr))
        }

        // Generate a random email address
        email := generator.GenerateRandomEmail()
        if email == "" {
            t.Error("Email should not be empty")
        }

        // Generate a random slice
        slice := generator.GenerateRandomSlice(50)
        if len(slice) > 50 {
            t.Errorf("Slice too long: %d", len(slice))
        }

        // Validate the elements in the slice
        for i, val := range slice {
            if val < 0 {
                t.Errorf("Negative value at index %d: %d", i, val)
            }
        }
    })
}

Generating Complex Structs

// complex_struct_test.go
package main

import (
    "testing"
    "testing/fuzz"
)

// Address represents an address
type Address struct {
    Street  string
    City    string
    Country string
    ZipCode string
}

// Company represents a company
type Company struct {
    Name        string
    Address     Address
    EmployeeCount int
    IsPublic    bool
    Departments []string
}

// GenerateCompany generates company data using fuzz.NewConsumer
func GenerateCompany(data []byte) *Company {
    fc := fuzz.NewConsumer(data)

    company := &Company{}

    // Generate a company name
    fc.Fuzz(&company.Name)

    // Generate an address
    fc.Fuzz(&company.Address.Street)
    fc.Fuzz(&company.Address.City)
    fc.Fuzz(&company.Address.Country)
    fc.Fuzz(&company.Address.ZipCode)

    // Generate an employee count
    fc.Fuzz(&company.EmployeeCount)
    if company.EmployeeCount < 0 {
        company.EmployeeCount = 0
    }

    // Generate whether the company is public
    fc.Fuzz(&company.IsPublic)

    // Generate a list of departments
    fc.Fuzz(&company.Departments)

    return company
}

// FuzzCompanyGeneration tests company data generation
func FuzzCompanyGeneration(f *testing.F) {
    seedData := []byte("company generation seed")
    f.Add(seedData)

    f.Fuzz(func(t *testing.T, data []byte) {
        company := GenerateCompany(data)

        if company == nil {
            t.Error("Company should not be nil")
            return
        }

        // Validate the employee count
        if company.EmployeeCount < 0 {
            t.Errorf("Invalid employee count: %d", company.EmployeeCount)
        }

        // Verify that the address is not empty
        if company.Address.Street == "" && company.Address.City == "" {
            t.Error("Address should have some content")
        }

        // Validate the department list
        if company.Departments == nil {
            t.Error("Departments should not be nil")
        }

        // Validate department name lengths
        for i, dept := range company.Departments {
            if len(dept) > 100 {
                t.Errorf("Department name too long at index %d: %d", i, len(dept))
            }
        }
    })
}

Error Handling and Edge Cases

// error_handling_test.go
package main

import (
    "testing"
    "testing/fuzz"
)

// SafeDataProcessor is a safe data processor
func SafeDataProcessor(data []byte) (map[string]interface{}, error) {
    if len(data) == 0 {
        return nil, nil // Return nil for empty data
    }

    fc := fuzz.NewConsumer(data)
    result := make(map[string]interface{})

    // Generate key-value pairs
    var keyCount int
    fc.Fuzz(&keyCount)

    // Limit the number of keys
    if keyCount < 0 {
        keyCount = 0
    }
    if keyCount > 10 {
        keyCount = 10
    }

    for i := 0; i < keyCount; i++ {
        var key string
        fc.Fuzz(&key)

        // Limit key length
        if len(key) > 50 {
            key = key[:50]
        }

        var value interface{}
        fc.Fuzz(&value)

        result[key] = value
    }

    return result, nil
}

// FuzzSafeDataProcessor tests the safe data processor
func FuzzSafeDataProcessor(f *testing.F) {
    seedData := []byte("safe processor seed")
    f.Add(seedData)

    f.Fuzz(func(t *testing.T, data []byte) {
        result, err := SafeDataProcessor(data)

        // Empty data should return nil
        if len(data) == 0 {
            if result != nil {
                t.Error("Empty data should return nil result")
            }
            return
        }

        if err != nil {
            t.Errorf("Unexpected error: %v", err)
            return
        }

        if result == nil {
            t.Error("Result should not be nil for non-empty data")
            return
        }

        // Validate the number of keys
        if len(result) > 10 {
            t.Errorf("Too many keys: %d", len(result))
        }

        // Validate key lengths
        for key := range result {
            if len(key) > 50 {
                t.Errorf("Key too long: %d", len(key))
            }
        }
    })
}

Complete Example: String Processing Functions

Let's learn Go fuzz testing through a complete example:

1. Create the Functions Under Test

First, create string processing functions that may contain bugs:

// stringutils.go
package main

import (
    "strings"
    "unicode"
)

// ReverseString reverses a string
func ReverseString(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}

// CountWords counts words
func CountWords(s string) int {
    if s == "" {
        return 0
    }

    words := strings.Fields(s)
    return len(words)
}

// ValidateEmail performs simple email validation (with intentional bugs)
func ValidateEmail(email string) bool {
    if email == "" {
        return false
    }

    // This deliberately simple validation logic contains bugs
    parts := strings.Split(email, "@")
    if len(parts) != 2 {
        return false
    }

    // Check the username part
    username := parts[0]
    if len(username) == 0 {
        return false
    }

    // Check the domain part
    domain := parts[1]
    if len(domain) == 0 {
        return false
    }

    return true
}

// ParseNumber parses a numeric string
func ParseNumber(s string) (int, error) {
    if s == "" {
        return 0, nil // Possible bug: an empty string should return an error
    }

    result := 0
    for _, char := range s {
        if !unicode.IsDigit(char) {
            return 0, nil // Another bug: non-digit characters should return an error
        }
        result = result*10 + int(char-'0')
    }

    return result, nil
}

2. Write Fuzz Tests

// stringutils_test.go
package main

import (
    "strings"
    "testing"
)

// FuzzReverseString tests string reversal
func FuzzReverseString(f *testing.F) {
    // Add seed data
    testcases := []string{"Hello", "世界", "123", "", "a", "ab"}
    for _, tc := range testcases {
        f.Add(tc)
    }

    f.Fuzz(func(t *testing.T, orig string) {
        rev := ReverseString(orig)
        doubleRev := ReverseString(rev)

        // Reversing twice should produce the original string
        if orig != doubleRev {
            t.Errorf("Before: %q, after: %q", orig, doubleRev)
        }

        // The reversed string should have the same length
        if len(orig) != len(rev) {
            t.Errorf("Length mismatch: orig=%d, rev=%d", len(orig), len(rev))
        }
    })
}

// FuzzCountWords tests word counting
func FuzzCountWords(f *testing.F) {
    // Add seed data
    testcases := []string{
        "hello world",
        "one two three",
        "",
        "single",
        "multiple   spaces   here",
    }
    for _, tc := range testcases {
        f.Add(tc)
    }

    f.Fuzz(func(t *testing.T, s string) {
        count := CountWords(s)

        // The word count should be nonnegative
        if count < 0 {
            t.Errorf("Negative word count: %d", count)
        }

        // An empty string should have zero words
        if s == "" && count != 0 {
            t.Errorf("Empty string should have 0 words, got %d", count)
        }

        // The word count should not exceed the character count
        if count > len(s) {
            t.Errorf("Word count %d exceeds string length %d", count, len(s))
        }
    })
}

// FuzzValidateEmail tests email validation
func FuzzValidateEmail(f *testing.F) {
    // Add seed data
    testcases := []string{
        "[email protected]",
        "[email protected]",
        "invalid-email",
        "",
        "@domain.com",
        "user@",
    }
    for _, tc := range testcases {
        f.Add(tc)
    }

    f.Fuzz(func(t *testing.T, email string) {
        isValid := ValidateEmail(email)

        // An empty string should be invalid
        if email == "" && isValid {
            t.Errorf("Empty email should be invalid")
        }

        // A string without @ should be invalid
        if !strings.Contains(email, "@") && isValid {
            t.Errorf("Email without @ should be invalid: %q", email)
        }

        // A string with multiple @ signs should be invalid
        atCount := strings.Count(email, "@")
        if atCount > 1 && isValid {
            t.Errorf("Email with multiple @ should be invalid: %q", email)
        }
    })
}

// FuzzParseNumber tests numeric parsing
func FuzzParseNumber(f *testing.F) {
    // Add seed data
    testcases := []string{
        "123",
        "0",
        "999",
        "",
        "abc",
        "12a3",
    }
    for _, tc := range testcases {
        f.Add(tc)
    }

    f.Fuzz(func(t *testing.T, s string) {
        result, err := ParseNumber(s)

        // An empty string should return an error (but our function has a bug)
        if s == "" {
            // This test will discover the bug
            if err == nil {
                t.Errorf("Empty string should return error, got result: %d", result)
            }
        }

        // A string containing non-digit characters should return an error
        hasNonDigit := false
        for _, char := range s {
            if char < '0' || char > '9' {
                hasNonDigit = true
                break
            }
        }

        if hasNonDigit && err == nil {
            t.Errorf("String with non-digit characters should return error: %q", s)
        }

        // If parsing succeeds, the result should be nonnegative
        if err == nil && result < 0 {
            t.Errorf("Parsed number should be non-negative, got: %d", result)
        }
    })
}

3. Run Fuzz Tests

# Run all fuzz tests
go test -fuzz=.

# Run a specific fuzz test
go test -fuzz=FuzzReverseString

# Run fuzz tests and generate a corpus
go test -fuzz=FuzzReverseString -fuzztime=10s

# Run fuzz tests and save failing cases
go test -fuzz=FuzzReverseString -fuzztime=30s -fuzzminimizetime=5s

4. Advanced Fuzz Testing Example

// advanced_fuzz_test.go
package main

import (
    "encoding/json"
    "testing"
)

// User represents a user
type User struct {
    ID       int    `json:"id"`
    Name     string `json:"name"`
    Email    string `json:"email"`
    Age      int    `json:"age"`
    IsActive bool   `json:"is_active"`
}

// CreateUser creates a user (with intentional bugs)
func CreateUser(data []byte) (*User, error) {
    var user User
    err := json.Unmarshal(data, &user)
    if err != nil {
        return nil, err
    }

    // Required fields are deliberately not validated here
    return &user, nil
}

// FuzzCreateUser tests user creation
func FuzzCreateUser(f *testing.F) {
    // Add valid JSON seed data
    validUsers := []string{
        `{"id":1,"name":"Alice","email":"[email protected]","age":25,"is_active":true}`,
        `{"id":2,"name":"Bob","email":"[email protected]","age":30,"is_active":false}`,
        `{"name":"Charlie","email":"[email protected]","age":35}`,
    }

    for _, user := range validUsers {
        f.Add([]byte(user))
    }

    f.Fuzz(func(t *testing.T, data []byte) {
        user, err := CreateUser(data)

        // If JSON parsing fails, an error should be returned
        if !json.Valid(data) {
            if err == nil {
                t.Errorf("Invalid JSON should return error: %q", string(data))
            }
            return
        }

        // If parsing succeeds, the user should not be nil
        if err == nil && user == nil {
            t.Errorf("Successful parsing should return non-nil user")
        }

        // Perform basic validation after successful user creation
        if user != nil {
            // The age should be reasonable
            if user.Age < 0 || user.Age > 150 {
                t.Errorf("Invalid age: %d", user.Age)
            }

            // The ID should be nonnegative
            if user.ID < 0 {
                t.Errorf("Invalid ID: %d", user.ID)
            }
        }
    })
}

Fuzz Testing Best Practices

1. Choosing Seed Data

  • Choose representative input data
  • Include boundary values and special cases
  • Use data known to cause problems

2. Designing Test Logic

  • Test invariants
  • Validate boundary conditions
  • Check error handling

3. Tips for Using fuzz.NewConsumer

  • Use Consumer appropriately: Use it when you need complex structured data
  • Bound the data range: Avoid performance problems caused by oversized data
  • Validate generated data: Ensure it conforms to business logic
  • Handle errors: Deal appropriately with exceptional cases the Consumer may produce
// Best practice example
func BestPracticeExample(f *testing.F) {
    f.Add([]byte("seed"))

    f.Fuzz(func(t *testing.T, data []byte) {
        fc := fuzz.NewConsumer(data)

        // Limit data size
        if len(data) > 10000 {
            t.Skip("Data too large")
        }

        var user User
        fc.Fuzz(&user)

        // Validate generated data
        if user.Age < 0 || user.Age > 150 {
            t.Skip("Invalid age range")
        }

        // Test logic...
    })
}

4. Performance Considerations

  • Set reasonable fuzzing time limits
  • Avoid time-consuming operations inside fuzz tests
  • Use f.Skip() to skip irrelevant tests
  • Limit the size of data generated by fuzz.NewConsumer

5. Debugging Tips

func FuzzDebugExample(f *testing.F) {
    f.Add("test input")

    f.Fuzz(func(t *testing.T, input string) {
        // Use t.Logf for debugging
        t.Logf("Testing input: %q", input)

        // Use t.Skip to skip certain cases
        if len(input) > 1000 {
            t.Skip("Skipping very long input")
        }

        // Test logic...
    })
}

Common Problems and Solutions

1. Problems Found by Fuzzing

// Problem: panic on nil pointer
func ProblematicFunction(s *string) int {
    return len(*s) // Panics if s is nil
}

// Solution: add a nil check
func SafeFunction(s *string) int {
    if s == nil {
        return 0
    }
    return len(*s)
}

2. Performance Optimization

func FuzzPerformanceTest(f *testing.F) {
    f.Add("small input")

    f.Fuzz(func(t *testing.T, input string) {
        // Skip oversized inputs to improve performance
        if len(input) > 10000 {
            t.Skip("Input too large for performance test")
        }

        // Test logic...
    })
}

Summary

Go fuzz testing is a powerful tool that helps developers:

  1. Find bugs automatically: Avoid writing large numbers of test cases manually
  2. Improve code quality: Discover boundary conditions and exceptional cases
  3. Strengthen security: Identify potential security vulnerabilities
  4. Improve continuously: Integrate into CI/CD pipelines
  5. Generate complex data: Produce structured test data with fuzz.NewConsumer

The Core Value of fuzz.NewConsumer

fuzz.NewConsumer opens up new possibilities for Go fuzz testing:

  • Structured data generation: Generate complex structs, slices, maps, and other data types
  • Controlled data ranges: Use limits and validation to ensure generated data meets business requirements
  • Custom data generators: Build specialized generators for particular scenarios
  • Better test coverage: Test more complex business logic and data flows

By using fuzz testing and fuzz.NewConsumer appropriately, we can build more robust and secure Go applications. Remember that fuzzing is not a universal solution: combine it with unit tests, integration tests, and other testing methods for the best results.

References