1
2
3
4
5 package trace
6
7 import (
8 "bytes"
9 "os"
10 "path/filepath"
11 "strings"
12 "testing"
13 )
14
15 func TestCorruptedInputs(t *testing.T) {
16
17 tests := []string{
18 "gotrace\x00\x020",
19 "gotrace\x00Q00\x020",
20 "gotrace\x00T00\x020",
21 "gotrace\x00\xc3\x0200",
22 "go 1.5 trace\x00\x00\x00\x00\x020",
23 "go 1.5 trace\x00\x00\x00\x00Q00\x020",
24 "go 1.5 trace\x00\x00\x00\x00T00\x020",
25 "go 1.5 trace\x00\x00\x00\x00\xc3\x0200",
26 }
27 for _, data := range tests {
28 res, err := Parse(strings.NewReader(data), "")
29 if err == nil || res.Events != nil || res.Stacks != nil {
30 t.Fatalf("no error on input: %q", data)
31 }
32 }
33 }
34
35 func TestParseCanned(t *testing.T) {
36 files, err := os.ReadDir("./testdata")
37 if err != nil {
38 t.Fatalf("failed to read ./testdata: %v", err)
39 }
40 for _, f := range files {
41 info, err := f.Info()
42 if err != nil {
43 t.Fatal(err)
44 }
45 if testing.Short() && info.Size() > 10000 {
46 continue
47 }
48 name := filepath.Join("./testdata", f.Name())
49 data, err := os.ReadFile(name)
50 if err != nil {
51 t.Fatal(err)
52 }
53
54
55 ver, res, err := parse(bytes.NewReader(data), "")
56 switch {
57 case strings.HasSuffix(f.Name(), "_good"):
58 if err != nil {
59 t.Errorf("failed to parse good trace %v: %v", f.Name(), err)
60 }
61 checkTrace(t, ver, res)
62 case strings.HasSuffix(f.Name(), "_unordered"):
63 if err != ErrTimeOrder {
64 t.Errorf("unordered trace is not detected %v: %v", f.Name(), err)
65 }
66 default:
67 t.Errorf("unknown input file suffix: %v", f.Name())
68 }
69 }
70 }
71
72
73
74 func checkTrace(t *testing.T, ver int, res ParseResult) {
75 for _, ev := range res.Events {
76 if ver >= 1021 {
77 if ev.Type == EvSTWStart && ev.SArgs[0] == "unknown" {
78 t.Errorf("found unknown STW event; update stwReasonStrings?")
79 }
80 }
81 }
82 }
83
84 func TestParseVersion(t *testing.T) {
85 tests := map[string]int{
86 "go 1.5 trace\x00\x00\x00\x00": 1005,
87 "go 1.7 trace\x00\x00\x00\x00": 1007,
88 "go 1.10 trace\x00\x00\x00": 1010,
89 "go 1.25 trace\x00\x00\x00": 1025,
90 "go 1.234 trace\x00\x00": 1234,
91 "go 1.2345 trace\x00": -1,
92 "go 0.0 trace\x00\x00\x00\x00": -1,
93 "go a.b trace\x00\x00\x00\x00": -1,
94 }
95 for header, ver := range tests {
96 ver1, err := parseHeader([]byte(header))
97 if ver == -1 {
98 if err == nil {
99 t.Fatalf("no error on input: %q, version %v", header, ver1)
100 }
101 } else {
102 if err != nil {
103 t.Fatalf("failed to parse: %q (%v)", header, err)
104 }
105 if ver != ver1 {
106 t.Fatalf("wrong version: %v, want %v, input: %q", ver1, ver, header)
107 }
108 }
109 }
110 }
111
112 func TestTimestampOverflow(t *testing.T) {
113
114 w := NewWriter()
115 w.Emit(EvBatch, 0, 0)
116 w.Emit(EvFrequency, 1e9)
117 for ts := uint64(1); ts < 1e16; ts *= 2 {
118 w.Emit(EvGoCreate, ts, ts, 0, 0)
119 }
120 if _, err := Parse(w, ""); err != nil {
121 t.Fatalf("failed to parse: %v", err)
122 }
123 }
124
View as plain text