github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/compiler/consts_test.go (about)

     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  	"os"
     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 := os.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[targets.Linux][targets.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", "CONST25",
    41  		"CONST26", "CONST27",
    42  	}
    43  	sort.Strings(wantConsts)
    44  	var constNames []string
    45  	for _, def := range info.Consts {
    46  		constNames = append(constNames, def.Name)
    47  	}
    48  	if !reflect.DeepEqual(constNames, wantConsts) {
    49  		t.Fatalf("got consts:\n%q\nwant:\n%q", constNames, wantConsts)
    50  	}
    51  	wantIncludes := []string{"foo/bar.h", "bar/foo.h"}
    52  	if !reflect.DeepEqual(info.Includes, wantIncludes) {
    53  		t.Fatalf("got includes:\n%q\nwant:\n%q", info.Includes, wantIncludes)
    54  	}
    55  	wantIncdirs := []string{"/foo", "/bar"}
    56  	if !reflect.DeepEqual(info.Incdirs, wantIncdirs) {
    57  		t.Fatalf("got incdirs:\n%q\nwant:\n%q", info.Incdirs, wantIncdirs)
    58  	}
    59  	wantDefines := map[string]string{
    60  		"CONST1": "1",
    61  		"CONST2": "FOOBAR + 1",
    62  	}
    63  	if !reflect.DeepEqual(info.Defines, wantDefines) {
    64  		t.Fatalf("got defines:\n%q\nwant:\n%q", info.Defines, wantDefines)
    65  	}
    66  }
    67  
    68  func TestConstErrors(t *testing.T) {
    69  	name := "consts_errors.txt"
    70  	em := ast.NewErrorMatcher(t, filepath.Join("testdata", name))
    71  	desc := ast.Parse(em.Data, name, em.ErrorHandler)
    72  	if desc == nil {
    73  		em.DumpErrors()
    74  		t.Fatalf("parsing failed")
    75  	}
    76  	target := targets.List[targets.Linux][targets.AMD64]
    77  	ExtractConsts(desc, target, em.ErrorHandler)
    78  	em.Check()
    79  }