|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+package record_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "bytes"
|
|
|
5
|
+ "encoding/base64"
|
|
|
6
|
+ "encoding/json"
|
|
|
7
|
+ "os"
|
|
|
8
|
+ "path/filepath"
|
|
|
9
|
+ "testing"
|
|
|
10
|
+
|
|
|
11
|
+ "github.com/9seconds/mtg/v2/mtglib/internal/faketls/record"
|
|
|
12
|
+ "github.com/stretchr/testify/assert"
|
|
|
13
|
+ "github.com/stretchr/testify/suite"
|
|
|
14
|
+)
|
|
|
15
|
+
|
|
|
16
|
+type RecordTestSnapshot struct {
|
|
|
17
|
+ Type int `json:"type"`
|
|
|
18
|
+ Version int `json:"version"`
|
|
|
19
|
+ Payload string `json:"payload"`
|
|
|
20
|
+ Record string `json:"record"`
|
|
|
21
|
+}
|
|
|
22
|
+
|
|
|
23
|
+func (r RecordTestSnapshot) RecordBytes() []byte {
|
|
|
24
|
+ data, _ := base64.StdEncoding.DecodeString(r.Record)
|
|
|
25
|
+
|
|
|
26
|
+ return data
|
|
|
27
|
+}
|
|
|
28
|
+
|
|
|
29
|
+func (r RecordTestSnapshot) PayloadBytes() []byte {
|
|
|
30
|
+ data, _ := base64.StdEncoding.DecodeString(r.Payload)
|
|
|
31
|
+
|
|
|
32
|
+ return data
|
|
|
33
|
+}
|
|
|
34
|
+
|
|
|
35
|
+type RecordTestSuite struct {
|
|
|
36
|
+ suite.Suite
|
|
|
37
|
+
|
|
|
38
|
+ r *record.Record
|
|
|
39
|
+ buf *bytes.Buffer
|
|
|
40
|
+}
|
|
|
41
|
+
|
|
|
42
|
+func (suite *RecordTestSuite) SetupTest() {
|
|
|
43
|
+ suite.r = record.AcquireRecord()
|
|
|
44
|
+ suite.buf = &bytes.Buffer{}
|
|
|
45
|
+}
|
|
|
46
|
+
|
|
|
47
|
+func (suite *RecordTestSuite) TearDownTest() {
|
|
|
48
|
+ record.ReleaseRecord(suite.r)
|
|
|
49
|
+ suite.buf.Reset()
|
|
|
50
|
+}
|
|
|
51
|
+
|
|
|
52
|
+func (suite *RecordTestSuite) TestIdempotent() {
|
|
|
53
|
+ suite.r.Type = record.TypeApplicationData
|
|
|
54
|
+ suite.r.Version = record.Version13
|
|
|
55
|
+
|
|
|
56
|
+ suite.r.Payload.Write([]byte{1, 2, 3})
|
|
|
57
|
+ suite.NoError(suite.r.Dump(suite.buf))
|
|
|
58
|
+
|
|
|
59
|
+ suite.r.Reset()
|
|
|
60
|
+ suite.NoError(suite.r.Read(suite.buf))
|
|
|
61
|
+
|
|
|
62
|
+ suite.Equal(0, suite.buf.Len())
|
|
|
63
|
+ suite.Equal(record.TypeApplicationData, suite.r.Type)
|
|
|
64
|
+ suite.Equal(record.Version13, suite.r.Version)
|
|
|
65
|
+ suite.Equal([]byte{1, 2, 3}, suite.r.Payload.Bytes())
|
|
|
66
|
+}
|
|
|
67
|
+
|
|
|
68
|
+func (suite *RecordTestSuite) TestString() {
|
|
|
69
|
+ _ = suite.r.String()
|
|
|
70
|
+}
|
|
|
71
|
+
|
|
|
72
|
+func (suite *RecordTestSuite) TestSnapshot() {
|
|
|
73
|
+ files, err := os.ReadDir("testdata")
|
|
|
74
|
+ suite.NoError(err)
|
|
|
75
|
+
|
|
|
76
|
+ testData := map[string]string{}
|
|
|
77
|
+
|
|
|
78
|
+ for _, f := range files {
|
|
|
79
|
+ testData[f.Name()] = filepath.Join("testdata", f.Name())
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ for name, pathV := range testData {
|
|
|
83
|
+ path := pathV
|
|
|
84
|
+
|
|
|
85
|
+ suite.T().Run(name, func(t *testing.T) {
|
|
|
86
|
+ data, err := os.ReadFile(path)
|
|
|
87
|
+ assert.NoError(t, err)
|
|
|
88
|
+
|
|
|
89
|
+ snapshot := &RecordTestSnapshot{}
|
|
|
90
|
+ assert.NoError(t, json.Unmarshal(data, snapshot))
|
|
|
91
|
+
|
|
|
92
|
+ rec := record.AcquireRecord()
|
|
|
93
|
+ defer record.ReleaseRecord(rec)
|
|
|
94
|
+
|
|
|
95
|
+ assert.NoError(t, rec.Read(bytes.NewReader(snapshot.RecordBytes())))
|
|
|
96
|
+ assert.Equal(t, snapshot.Type, int(rec.Type))
|
|
|
97
|
+ assert.Equal(t, snapshot.Version, int(rec.Version))
|
|
|
98
|
+ assert.Equal(t, snapshot.PayloadBytes(), rec.Payload.Bytes())
|
|
|
99
|
+
|
|
|
100
|
+ buf := &bytes.Buffer{}
|
|
|
101
|
+ assert.NoError(t, rec.Dump(buf))
|
|
|
102
|
+ assert.Equal(t, snapshot.RecordBytes(), buf.Bytes())
|
|
|
103
|
+ })
|
|
|
104
|
+ }
|
|
|
105
|
+}
|
|
|
106
|
+
|
|
|
107
|
+func TestRecord(t *testing.T) {
|
|
|
108
|
+ t.Parallel()
|
|
|
109
|
+ suite.Run(t, &RecordTestSuite{})
|
|
|
110
|
+}
|