-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz.go
More file actions
103 lines (89 loc) · 3.08 KB
/
Copy pathfuzz.go
File metadata and controls
103 lines (89 loc) · 3.08 KB
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
package test
import (
"bufio"
"bytes"
"reflect"
"testing"
"github.com/cmd-stream/cmd-stream-go/core"
"github.com/cmd-stream/cmd-stream-go/transport"
assertfatal "github.com/ymz-ncnk/assert/fatal"
)
// Codec represents a generic codec interface for testing.
type Codec[T, V any] interface {
Encode(t T, w transport.Writer) (n int, err error)
Decode(r transport.Reader) (v V, n int, err error)
}
// TransportWriter is a utility for testing codecs.
type TransportWriter struct {
*bufio.Writer
}
func (w *TransportWriter) Flush() error {
return w.Writer.Flush()
}
// VerifyRoundTripCmd verifies the round-trip encoding and decoding of a Command.
func VerifyRoundTripCmd(t *testing.T, clientCodec Codec[core.Cmd[any], core.Result],
serverCodec Codec[core.Result, core.Cmd[any]], cmd core.Cmd[any]) {
VerifyRoundTripCmdWith(t, clientCodec, serverCodec, cmd, func(a, b any) bool {
return reflect.DeepEqual(a, b)
})
}
// VerifyRoundTripCmdWith verifies the round-trip encoding and decoding of a
// Command using a custom equality check.
func VerifyRoundTripCmdWith(t *testing.T, clientCodec Codec[core.Cmd[any], core.Result],
serverCodec Codec[core.Result, core.Cmd[any]],
cmd core.Cmd[any],
equal func(expected, actual any) bool,
) {
var (
buf = &bytes.Buffer{}
bw = bufio.NewWriter(buf)
w = &TransportWriter{Writer: bw}
)
n, err := clientCodec.Encode(cmd, w)
assertfatal.EqualError(t, err, nil, "failed to encode")
w.Flush()
r := bytes.NewReader(buf.Bytes())
decoded, n2, err := serverCodec.Decode(r)
assertfatal.EqualError(t, err, nil, "failed to decode")
assertfatal.Equal(t, n, n2, "encoded and decoded bytes length mismatch")
if !equal(cmd, decoded) {
t.Fatalf("roundtrip failed: expected %v, got %v", cmd, decoded)
}
}
// VerifyRoundTripResult verifies the round-trip encoding and decoding of a Result.
func VerifyRoundTripResult(t *testing.T, clientCodec Codec[core.Cmd[any], core.Result],
server Codec[core.Result, core.Cmd[any]],
res core.Result,
) {
VerifyRoundTripResultWith(t, clientCodec, server, res, func(a, b any) bool {
return reflect.DeepEqual(a, b)
})
}
// VerifyRoundTripResultWith verifies the round-trip encoding and decoding of a
// Result using a custom equality check.
func VerifyRoundTripResultWith(t *testing.T, clientCodec Codec[core.Cmd[any], core.Result],
serverCodec Codec[core.Result, core.Cmd[any]],
res core.Result,
equal func(expected, actual any) bool,
) {
var (
buf = &bytes.Buffer{}
bw = bufio.NewWriter(buf)
w = &TransportWriter{Writer: bw}
)
n, err := serverCodec.Encode(res, w)
assertfatal.EqualError(t, err, nil, "failed to encode")
w.Flush()
r := bytes.NewReader(buf.Bytes())
decoded, n2, err := clientCodec.Decode(r)
assertfatal.EqualError(t, err, nil, "failed to decode")
assertfatal.Equal(t, n, n2, "encoded and decoded bytes length mismatch")
if !equal(res, decoded) {
t.Fatalf("roundtrip failed: expected %v, got %v", res, decoded)
}
}
// FuzzDecode ensures that the codec does not panic when decoding random data.
func FuzzDecode[T, V any](codec Codec[T, V], data []byte) {
r := bytes.NewReader(data)
_, _, _ = codec.Decode(r)
}