|
| 1 | +package chaintracks |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// FuzzLoadHeadersFromFile tests loadHeadersFromFile with random binary data |
| 12 | +// to ensure it handles malformed header files gracefully without panicking. |
| 13 | +func FuzzLoadHeadersFromFile(f *testing.F) { |
| 14 | + // Seed corpus with interesting binary patterns |
| 15 | + |
| 16 | + // Empty file |
| 17 | + f.Add([]byte{}) |
| 18 | + |
| 19 | + // Single valid-looking header (80 bytes of zeros) |
| 20 | + validHeader := make([]byte, 80) |
| 21 | + f.Add(validHeader) |
| 22 | + |
| 23 | + // Two headers (160 bytes) |
| 24 | + twoHeaders := make([]byte, 160) |
| 25 | + f.Add(twoHeaders) |
| 26 | + |
| 27 | + // Not a multiple of 80 (79 bytes) |
| 28 | + f.Add(make([]byte, 79)) |
| 29 | + |
| 30 | + // Not a multiple of 80 (81 bytes) |
| 31 | + f.Add(make([]byte, 81)) |
| 32 | + |
| 33 | + // Not a multiple of 80 (159 bytes) |
| 34 | + f.Add(make([]byte, 159)) |
| 35 | + |
| 36 | + // Large file (1000 headers = 80,000 bytes) |
| 37 | + largeFile := make([]byte, 80*1000) |
| 38 | + f.Add(largeFile) |
| 39 | + |
| 40 | + // Random pattern (240 bytes = 3 headers worth) |
| 41 | + randomPattern := []byte{ |
| 42 | + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 43 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 44 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 45 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 46 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 47 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 48 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 49 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 50 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 51 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 52 | + } |
| 53 | + f.Add(randomPattern) |
| 54 | + |
| 55 | + // All 0xFF bytes (80 bytes) |
| 56 | + allOnes := make([]byte, 80) |
| 57 | + for i := range allOnes { |
| 58 | + allOnes[i] = 0xFF |
| 59 | + } |
| 60 | + f.Add(allOnes) |
| 61 | + |
| 62 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 63 | + // Create a temporary file with the fuzzed data |
| 64 | + tmpDir := t.TempDir() |
| 65 | + tmpFile := filepath.Join(tmpDir, "fuzz.headers") |
| 66 | + |
| 67 | + err := os.WriteFile(tmpFile, data, 0o600) |
| 68 | + require.NoError(t, err, "Failed to write temp file") |
| 69 | + |
| 70 | + // Should never panic |
| 71 | + headers, err := loadHeadersFromFile(tmpFile) |
| 72 | + |
| 73 | + // Validate invariants based on input |
| 74 | + dataLen := len(data) |
| 75 | + |
| 76 | + // If data length is not a multiple of 80, should return error |
| 77 | + if dataLen%80 != 0 { |
| 78 | + require.Error(t, err, "Expected error for non-80-byte-aligned data") |
| 79 | + require.Nil(t, headers, "Headers should be nil on error") |
| 80 | + require.ErrorIs(t, err, ErrInvalidFileSize, "Should return ErrInvalidFileSize") |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + // If data length is a multiple of 80, it may succeed or fail depending on header validity |
| 85 | + expectedHeaderCount := dataLen / 80 |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + // Error is acceptable if header parsing fails |
| 89 | + require.Nil(t, headers, "Headers should be nil when error is returned") |
| 90 | + } else { |
| 91 | + // Success: validate the returned headers |
| 92 | + require.NotNil(t, headers, "Headers should not be nil on success") |
| 93 | + require.Len(t, headers, expectedHeaderCount, "Header count mismatch") |
| 94 | + |
| 95 | + // Verify each header is not nil |
| 96 | + for i, header := range headers { |
| 97 | + require.NotNil(t, header, "Header at index %d should not be nil", i) |
| 98 | + } |
| 99 | + } |
| 100 | + }) |
| 101 | +} |
0 commit comments