github.com/moov-io/imagecashletter@v0.10.1/imagecashletter_test.go (about) 1 // Copyright 2020 The Moov Authors 2 // Use of this source code is governed by an Apache License 3 // license that can be found in the LICENSE file. 4 5 package imagecashletter 6 7 import ( 8 "errors" 9 "fmt" 10 "os" 11 "path/filepath" 12 "strings" 13 "testing" 14 15 "github.com/stretchr/testify/require" 16 ) 17 18 // TestImageCashLetter_ReadCrashers will attempt to parse files which have previously been reported 19 // as crashing. These files are typically generated via fuzzing, but might also be reported by users. 20 func TestImageCashLetter_ReadCrashers(t *testing.T) { 21 root := filepath.Join("test", "testdata", "crashers") 22 err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { 23 if (err != nil && !errors.Is(err, filepath.SkipDir)) || info == nil || info.IsDir() { 24 return nil // Ignore SkipDir and directories 25 } 26 if strings.HasSuffix(path, ".output") { 27 return nil // go-fuzz makes these which contain the panic's trace 28 } 29 30 fd, err := os.Open(path) 31 if err != nil { 32 return fmt.Errorf("problem opening %s: %v", path, err) 33 } 34 35 // Read out test file with multiple option patterns and ensure we don't panic 36 require.NotPanics(t, func() { 37 _, _ = NewReader(fd).Read() 38 _, _ = NewReader(fd, ReadVariableLineLengthOption()).Read() 39 }) 40 41 if testing.Verbose() { 42 t.Logf("read and parsed %s", fd.Name()) 43 } 44 45 return nil 46 }) 47 require.NoError(t, err) 48 }