File size: 4,224 Bytes
d7a5f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pgo

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"reflect"
	"strings"
	"testing"
)

// equal returns an error if got and want are not equal.
func equal(got, want *Profile) error {
	if got.TotalWeight != want.TotalWeight {
		return fmt.Errorf("got.TotalWeight %d != want.TotalWeight %d", got.TotalWeight, want.TotalWeight)
	}
	if !reflect.DeepEqual(got.NamedEdgeMap.ByWeight, want.NamedEdgeMap.ByWeight) {
		return fmt.Errorf("got.NamedEdgeMap.ByWeight != want.NamedEdgeMap.ByWeight\ngot = %+v\nwant = %+v", got.NamedEdgeMap.ByWeight, want.NamedEdgeMap.ByWeight)
	}
	if !reflect.DeepEqual(got.NamedEdgeMap.Weight, want.NamedEdgeMap.Weight) {
		return fmt.Errorf("got.NamedEdgeMap.Weight != want.NamedEdgeMap.Weight\ngot = %+v\nwant = %+v", got.NamedEdgeMap.Weight, want.NamedEdgeMap.Weight)
	}

	return nil
}

func testRoundTrip(t *testing.T, d *Profile) []byte {
	var buf bytes.Buffer
	n, err := d.WriteTo(&buf)
	if err != nil {
		t.Fatalf("WriteTo got err %v want nil", err)
	}
	if n != int64(buf.Len()) {
		t.Errorf("WriteTo got n %d want %d", n, int64(buf.Len()))
	}

	b := buf.Bytes()

	got, err := FromSerialized(&buf)
	if err != nil {
		t.Fatalf("processSerialized got err %v want nil", err)
	}
	if err := equal(got, d); err != nil {
		t.Errorf("processSerialized output does not match input: %v", err)
	}

	return b
}

func TestEmpty(t *testing.T) {
	d := emptyProfile()
	b := testRoundTrip(t, d)

	// Contents should consist of only a header.
	if string(b) != serializationHeader {
		t.Errorf("WriteTo got %q want %q", string(b), serializationHeader)
	}
}

func TestRoundTrip(t *testing.T) {
	d := &Profile{
		TotalWeight: 3,
		NamedEdgeMap: NamedEdgeMap{
			ByWeight: []NamedCallEdge{
				{
					CallerName:     "a",
					CalleeName:     "b",
					CallSiteOffset: 14,
				},
				{
					CallerName:     "c",
					CalleeName:     "d",
					CallSiteOffset: 15,
				},
			},
			Weight: map[NamedCallEdge]int64{
				{
					CallerName:     "a",
					CalleeName:     "b",
					CallSiteOffset: 14,
				}: 2,
				{
					CallerName:     "c",
					CalleeName:     "d",
					CallSiteOffset: 15,
				}: 1,
			},
		},
	}

	testRoundTrip(t, d)
}

func constructFuzzProfile(t *testing.T, b []byte) *Profile {
	// The fuzzer can't construct an arbitrary structure, so instead we
	// consume bytes from b to act as our edge data.
	r := bytes.NewReader(b)
	consumeString := func() (string, bool) {
		// First byte: how many bytes to read for this string? We only
		// use a byte to avoid making humongous strings.
		length, err := r.ReadByte()
		if err != nil {
			return "", false
		}
		if length == 0 {
			return "", false
		}

		b := make([]byte, length)
		_, err = r.Read(b)
		if err != nil {
			return "", false
		}

		return string(b), true
	}
	consumeInt64 := func() (int64, bool) {
		b := make([]byte, 8)
		_, err := r.Read(b)
		if err != nil {
			return 0, false
		}

		return int64(binary.LittleEndian.Uint64(b)), true
	}

	d := emptyProfile()

	for {
		caller, ok := consumeString()
		if !ok {
			break
		}
		if strings.ContainsAny(caller, " \r\n") {
			t.Skip("caller contains space or newline")
		}

		callee, ok := consumeString()
		if !ok {
			break
		}
		if strings.ContainsAny(callee, " \r\n") {
			t.Skip("callee contains space or newline")
		}

		line, ok := consumeInt64()
		if !ok {
			break
		}
		weight, ok := consumeInt64()
		if !ok {
			break
		}

		edge := NamedCallEdge{
			CallerName:     caller,
			CalleeName:     callee,
			CallSiteOffset: int(line),
		}

		if _, ok := d.NamedEdgeMap.Weight[edge]; ok {
			t.Skip("duplicate edge")
		}

		d.NamedEdgeMap.Weight[edge] = weight
		d.TotalWeight += weight
	}

	byWeight := make([]NamedCallEdge, 0, len(d.NamedEdgeMap.Weight))
	for namedEdge := range d.NamedEdgeMap.Weight {
		byWeight = append(byWeight, namedEdge)
	}
	sortByWeight(byWeight, d.NamedEdgeMap.Weight)
	d.NamedEdgeMap.ByWeight = byWeight

	return d
}

func FuzzRoundTrip(f *testing.F) {
	f.Add([]byte("")) // empty profile

	f.Fuzz(func(t *testing.T, b []byte) {
		d := constructFuzzProfile(t, b)
		testRoundTrip(t, d)
	})
}