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