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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package generate
import (
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"sort"
"strings"
"testing"
)
var bytesListItemRE = regexp.MustCompile("`([^`]+)`")
func TestSyscallTracingPlanBytesClassificationStaysInSync(t *testing.T) {
doc, err := readSyscallTracingPlan()
if err != nil {
t.Fatalf("read syscall tracing plan: %v", err)
}
documented, err := parseDocListSection(doc, "## Bytes vs Non-Bytes Classification")
if err != nil {
t.Fatalf("parse bytes classification section: %v", err)
}
expected := map[string][]string{
"ReadClassified": {},
"WriteClassified": {},
"TransferClassified": {},
}
for syscall, classification := range retClassifications {
switch classification {
case ReadClassified:
expected["ReadClassified"] = append(expected["ReadClassified"], syscall)
case WriteClassified:
expected["WriteClassified"] = append(expected["WriteClassified"], syscall)
case TransferClassified:
expected["TransferClassified"] = append(expected["TransferClassified"], syscall)
default:
t.Fatalf("unexpected classification %q for syscall %q", classification, syscall)
}
}
for key, values := range expected {
expected[key] = normalizeValues(values)
}
if !reflect.DeepEqual(mapKeys(documented), mapKeys(expected)) {
t.Fatalf("classification groups mismatch: documented=%v expected=%v", mapKeys(documented), mapKeys(expected))
}
for key, expectedSyscalls := range expected {
got := documented[key]
if !reflect.DeepEqual(got, expectedSyscalls) {
t.Fatalf("%s mismatch:\ndocumented=%v\nexpected=%v", key, got, expectedSyscalls)
}
}
}
func readSyscallTracingPlan() (string, error) {
_, filename, _, ok := runtime.Caller(0)
if !ok {
return "", fmt.Errorf("runtime.Caller failed")
}
repoRoot := filepath.Clean(filepath.Join(filepath.Dir(filename), "..", ".."))
content, err := os.ReadFile(filepath.Join(repoRoot, "docs", "syscall-tracing-plan.md"))
if err != nil {
return "", err
}
return string(content), nil
}
func parseDocListSection(doc, heading string) (map[string][]string, error) {
lines := strings.Split(doc, "\n")
start := -1
for i, line := range lines {
if strings.TrimSpace(line) == heading {
start = i + 1
break
}
}
if start == -1 {
return nil, fmt.Errorf("heading %q not found", heading)
}
out := map[string][]string{}
for i := start; i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
if strings.HasPrefix(line, "## ") {
break
}
if !strings.HasPrefix(line, "- ") {
continue
}
label, syscalls, err := parseDocBullet(line)
if err != nil {
return nil, err
}
out[label] = normalizeValues(syscalls)
}
if len(out) == 0 {
return nil, fmt.Errorf("heading %q had no bullet rows", heading)
}
return out, nil
}
func parseDocBullet(line string) (string, []string, error) {
entry := strings.TrimSpace(strings.TrimPrefix(line, "- "))
colon := strings.Index(entry, ":")
if colon <= 0 {
return "", nil, fmt.Errorf("invalid list entry %q", line)
}
label := strings.TrimSpace(entry[:colon])
if label == "" {
return "", nil, fmt.Errorf("missing label in %q", line)
}
matches := bytesListItemRE.FindAllStringSubmatch(entry[colon+1:], -1)
if len(matches) == 0 {
return "", nil, fmt.Errorf("no syscall list in %q", line)
}
items := make([]string, 0, len(matches))
for _, match := range matches {
items = append(items, match[1])
}
return label, items, nil
}
func normalizeValues(values []string) []string {
uniq := map[string]struct{}{}
for _, value := range values {
uniq[value] = struct{}{}
}
out := make([]string, 0, len(uniq))
for value := range uniq {
out = append(out, value)
}
sort.Strings(out)
return out
}
func mapKeys(m map[string][]string) []string {
out := make([]string, 0, len(m))
for key := range m {
out = append(out, key)
}
sort.Strings(out)
return out
}
|