github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/prog/images_test.go (about) 1 // Copyright 2022 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 package prog_test 5 6 import ( 7 "flag" 8 "fmt" 9 "io" 10 "os" 11 "path/filepath" 12 "reflect" 13 "sort" 14 "strings" 15 "testing" 16 17 "github.com/google/go-cmp/cmp" 18 "github.com/google/syzkaller/pkg/osutil" 19 . "github.com/google/syzkaller/prog" 20 "github.com/google/syzkaller/sys/targets" 21 ) 22 23 var flagUpdate = flag.Bool("update", false, "update test files accordingly to current results") 24 25 func TestForEachAsset(t *testing.T) { 26 target, err := GetTarget(targets.Linux, targets.AMD64) 27 if err != nil { 28 t.Fatal(err) 29 } 30 files, err := filepath.Glob(filepath.Join("testdata", "fs_images", "*.in")) 31 if err != nil { 32 t.Fatalf("directory read failed: %v", err) 33 } 34 allOutFiles, err := filepath.Glob(filepath.Join("testdata", "fs_images", "*.out*")) 35 if err != nil { 36 t.Fatalf("directory read failed: %v", err) 37 } 38 testedOutFiles := []string{} 39 for _, file := range files { 40 sourceProg, err := os.ReadFile(file) 41 if err != nil { 42 t.Fatal(err) 43 } 44 p, err := target.Deserialize(sourceProg, NonStrict) 45 if err != nil { 46 t.Fatalf("failed to deserialize %s: %s", file, err) 47 } 48 base := strings.TrimSuffix(file, ".in") 49 p.ForEachAsset(func(name string, typ AssetType, r io.Reader, c *Call) { 50 if typ != MountInRepro { 51 t.Fatalf("unknown asset type %v", typ) 52 } 53 if !strings.HasPrefix(c.Meta.Name, "syz_mount_image$") { 54 t.Fatalf("unexpected syscall name %v", c.Meta.Name) 55 } 56 testResult, err := io.ReadAll(r) 57 if err != nil { 58 t.Fatal(err) 59 } 60 outFilePath := fmt.Sprintf("%v.out_%v", base, name) 61 if *flagUpdate { 62 if err := osutil.WriteFile(outFilePath, testResult); err != nil { 63 t.Fatal(err) 64 } 65 } 66 if !osutil.IsExist(outFilePath) { 67 t.Fatalf("asset %v does not exist", outFilePath) 68 } 69 testedOutFiles = append(testedOutFiles, outFilePath) 70 outFile, err := os.ReadFile(outFilePath) 71 if err != nil { 72 t.Fatal(err) 73 } 74 if !reflect.DeepEqual(testResult, outFile) { 75 t.Fatalf("output not equal:\nWant: %x\nGot: %x", outFile, testResult) 76 } 77 }) 78 } 79 sort.Strings(testedOutFiles) 80 sort.Strings(allOutFiles) 81 if diff := cmp.Diff(allOutFiles, testedOutFiles); diff != "" { 82 t.Fatalf("not all output files used: %v", diff) 83 } 84 }