aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/compiler/const_file_test.go
blob: 957cb64219603e3ddeeaa8a5b8754ba657c22870 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2020 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 (
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestConstFile(t *testing.T) {
	type arch struct {
		consts    map[string]uint64
		undefined map[string]bool
		oldFormat string
	}
	arches := map[string]arch{
		"arch1": {
			consts: map[string]uint64{
				"CONST1_ALL_DIFFERENT":   11,
				"CONST2_ALL_THE_SAME":    3,
				"CONST3_SOME_UNDEFINED":  100,
				"CONST5_SOME_UNDEFINED2": 100,
			},
			undefined: map[string]bool{
				"CONST4_ALL_UNDEFINED": true,
			},
			oldFormat: `
CONST1_ALL_DIFFERENT = 11
CONST2_ALL_THE_SAME = 3
CONST3_SOME_UNDEFINED = 100
CONST5_SOME_UNDEFINED2 = 100
# CONST4_ALL_UNDEFINED is not set
`,
		},
		"arch2": {
			consts: map[string]uint64{
				"CONST1_ALL_DIFFERENT":   22,
				"CONST2_ALL_THE_SAME":    3,
				"CONST5_SOME_UNDEFINED2": 100,
			},
			undefined: map[string]bool{
				"CONST4_ALL_UNDEFINED":  true,
				"CONST3_SOME_UNDEFINED": true,
			},
			oldFormat: `
CONST1_ALL_DIFFERENT = 22
CONST2_ALL_THE_SAME = 3
# CONST3_SOME_UNDEFINED is not set
CONST5_SOME_UNDEFINED2 = 100
# CONST4_ALL_UNDEFINED is not set
`,
		},
		"arch3": {
			consts: map[string]uint64{
				"CONST1_ALL_DIFFERENT": 33,
				"CONST2_ALL_THE_SAME":  3,
			},
			undefined: map[string]bool{
				"CONST4_ALL_UNDEFINED":   true,
				"CONST3_SOME_UNDEFINED":  true,
				"CONST5_SOME_UNDEFINED2": true,
			},
			oldFormat: `
CONST1_ALL_DIFFERENT = 33
CONST2_ALL_THE_SAME = 3
# CONST3_SOME_UNDEFINED is not set
# CONST5_SOME_UNDEFINED2 is not set
# CONST4_ALL_UNDEFINED is not set
`,
		},
	}
	const serialized = `# Code generated by syz-sysgen. DO NOT EDIT.
arches = arch1, arch2, arch3
CONST1_ALL_DIFFERENT = arch1:11, arch2:22, arch3:33
CONST2_ALL_THE_SAME = 3
CONST3_SOME_UNDEFINED = arch1:100, arch2:arch3:???
CONST4_ALL_UNDEFINED = ???
CONST5_SOME_UNDEFINED2 = 100, arch3:???
`
	cf := NewConstFile()
	for name, arch := range arches {
		cf.AddArch(name, arch.consts, arch.undefined)
	}
	data := cf.Serialize()
	assert.Equal(t, serialized, string(data))
	assert.True(t, cf.ExistsAny("CONST3_SOME_UNDEFINED"))
	assert.False(t, cf.ExistsAny("CONST4_ALL_UNDEFINED"))
	{
		file, err := os.CreateTemp("", "syz-const")
		if err != nil {
			t.Fatal(err)
		}
		defer file.Close()
		defer os.Remove(file.Name())
		if _, err := file.Write(data); err != nil {
			t.Fatal(err)
		}
		file.Close()
		cf1 := DeserializeConstFile(file.Name(), nil)
		for name, arch := range arches {
			assert.Equal(t, cf1.Arch(name), arch.consts)
		}
	}
	{
		dir := t.TempDir()
		for name, arch := range arches {
			file := filepath.Join(dir, "consts_"+name+".const")
			if err := os.WriteFile(file, []byte(arch.oldFormat), 0600); err != nil {
				t.Fatal(err)
			}
		}
		cf1 := DeserializeConstFile(filepath.Join(dir, "*"), nil)
		for name, arch := range arches {
			assert.Equal(t, cf1.Arch(name), arch.consts)
		}
	}
}