1 // Copyright 2017 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 compiler 5 6 import ( 7 "io/ioutil" 8 "path/filepath" 9 "reflect" 10 "sort" 11 "testing" 12 13 "github.com/google/syzkaller/pkg/ast" 14 "github.com/google/syzkaller/sys/targets" 15 ) 16 17 func TestExtractConsts(t *testing.T) { 18 data, err := ioutil.ReadFile(filepath.Join("testdata", "consts.txt")) 19 if err != nil { 20 t.Fatalf("failed to read input file: %v", err) 21 } 22 desc := ast.Parse(data, "consts.txt", nil) 23 if desc == nil { 24 t.Fatalf("failed to parse input") 25 } 26 target := targets.List["linux"]["amd64"] 27 fileInfo := ExtractConsts(desc, target, func(pos ast.Pos, msg string) { 28 t.Fatalf("%v: %v", pos, msg) 29 }) 30 info := fileInfo["consts.txt"] 31 if info == nil || len(fileInfo) != 1 { 32 t.Fatalf("bad file info returned: %+v", info) 33 } 34 wantConsts := []string{ 35 "__NR_bar", "__NR_foo", 36 "CONST1", "CONST2", "CONST3", "CONST4", "CONST5", 37 "CONST6", "CONST7", "CONST8", "CONST9", "CONST10", 38 "CONST11", "CONST12", "CONST13", "CONST14", "CONST15", 39 "CONST16", "CONST17", "CONST18", "CONST19", "CONST20", 40 "CONST21", "CONST22", "CONST23", "CONST24", 41 } 42 sort.Strings(wantConsts) 43 if !reflect.DeepEqual(info.Consts, wantConsts) { 44 t.Fatalf("got consts:\n%q\nwant:\n%q", info.Consts, wantConsts) 45 } 46 wantIncludes := []string{"foo/bar.h", "bar/foo.h"} 47 if !reflect.DeepEqual(info.Includes, wantIncludes) { 48 t.Fatalf("got includes:\n%q\nwant:\n%q", info.Includes, wantIncludes) 49 } 50 wantIncdirs := []string{"/foo", "/bar"} 51 if !reflect.DeepEqual(info.Incdirs, wantIncdirs) { 52 t.Fatalf("got incdirs:\n%q\nwant:\n%q", info.Incdirs, wantIncdirs) 53 } 54 wantDefines := map[string]string{ 55 "CONST1": "1", 56 "CONST2": "FOOBAR + 1", 57 } 58 if !reflect.DeepEqual(info.Defines, wantDefines) { 59 t.Fatalf("got defines:\n%q\nwant:\n%q", info.Defines, wantDefines) 60 } 61 } 62 63 func TestConstErrors(t *testing.T) { 64 name := "consts_errors.txt" 65 em := ast.NewErrorMatcher(t, filepath.Join("testdata", name)) 66 desc := ast.Parse(em.Data, name, em.ErrorHandler) 67 if desc == nil { 68 em.DumpErrors(t) 69 t.Fatalf("parsing failed") 70 } 71 target := targets.List["linux"]["amd64"] 72 ExtractConsts(desc, target, em.ErrorHandler) 73 em.Check(t) 74 } 75