aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/compiler/consts_test.go
blob: 918f61d0c90f329011875ccc89104ec91457101e (plain)
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
// 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"
	"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, "test", nil)
	if desc == nil {
		t.Fatalf("failed to parse input")
	}
	target := targets.List["linux"]["amd64"]
	info := ExtractConsts(desc, target, func(pos ast.Pos, msg string) {
		t.Fatalf("%v: %v", pos, msg)
	})
	wantConsts := []string{"CONST1", "CONST10", "CONST11", "CONST12", "CONST13",
		"CONST14", "CONST15", "CONST16",
		"CONST2", "CONST3", "CONST4", "CONST5",
		"CONST6", "CONST7", "CONST8", "CONST9", "__NR_bar", "__NR_foo"}
	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)
}