github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/codesearch/codesearch_test.go (about) 1 // Copyright 2025 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 codesearch 5 6 import ( 7 "bytes" 8 "os" 9 "path/filepath" 10 "strings" 11 "testing" 12 13 "github.com/google/syzkaller/pkg/clangtool/tooltest" 14 "github.com/google/syzkaller/pkg/osutil" 15 ) 16 17 func TestClangTool(t *testing.T) { 18 tooltest.TestClangTool[Database](t) 19 } 20 21 func TestCommands(t *testing.T) { 22 db := tooltest.LoadOutput[Database](t) 23 index := &Index{db, []string{"testdata"}} 24 files, err := filepath.Glob(filepath.Join(osutil.Abs("testdata"), "query*")) 25 if err != nil { 26 t.Fatal(err) 27 } 28 if len(files) == 0 { 29 t.Fatal("found no qeury files") 30 } 31 covered := make(map[string]bool) 32 for _, file := range files { 33 t.Run(filepath.Base(file), func(t *testing.T) { 34 testCommand(t, index, covered, file) 35 }) 36 } 37 for _, cmd := range Commands { 38 if !covered[cmd.Name] { 39 t.Errorf("command %v is not covered, add at least one test", cmd.Name) 40 } 41 } 42 } 43 44 func testCommand(t *testing.T, index *Index, covered map[string]bool, file string) { 45 data, err := os.ReadFile(file) 46 if err != nil { 47 t.Fatal(err) 48 } 49 query, _, _ := bytes.Cut(data, []byte{'\n'}) 50 args := strings.Fields(string(query)) 51 if len(args) == 0 { 52 t.Fatal("no command found") 53 } 54 result, err := index.Command(args[0], args[1:]) 55 if err != nil { 56 t.Fatal(err) 57 } 58 got := append([]byte(strings.Join(args, " ")+"\n\n"), result...) 59 tooltest.CompareGoldenData(t, file, got) 60 covered[args[0]] = true 61 }