|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+package faketls_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "bytes"
|
|
|
5
|
+ "crypto/hmac"
|
|
|
6
|
+ "crypto/sha256"
|
|
|
7
|
+ "math/rand"
|
|
|
8
|
+ "testing"
|
|
|
9
|
+ "time"
|
|
|
10
|
+
|
|
|
11
|
+ "github.com/9seconds/mtg/v2/mtglib"
|
|
|
12
|
+ "github.com/9seconds/mtg/v2/mtglib/internal/faketls"
|
|
|
13
|
+ "github.com/9seconds/mtg/v2/mtglib/internal/faketls/record"
|
|
|
14
|
+ "github.com/stretchr/testify/suite"
|
|
|
15
|
+)
|
|
|
16
|
+
|
|
|
17
|
+type WelcomeTestSuite struct {
|
|
|
18
|
+ suite.Suite
|
|
|
19
|
+
|
|
|
20
|
+ h *faketls.ClientHello
|
|
|
21
|
+ buf *bytes.Buffer
|
|
|
22
|
+ secret mtglib.Secret
|
|
|
23
|
+}
|
|
|
24
|
+
|
|
|
25
|
+func (suite *WelcomeTestSuite) SetupTest() {
|
|
|
26
|
+ suite.h = &faketls.ClientHello{
|
|
|
27
|
+ Time: time.Now(),
|
|
|
28
|
+ Host: "google.com",
|
|
|
29
|
+ CipherSuite: 4867,
|
|
|
30
|
+ SessionID: make([]byte, 32),
|
|
|
31
|
+ }
|
|
|
32
|
+
|
|
|
33
|
+ _, err := rand.Read(suite.h.SessionID)
|
|
|
34
|
+ suite.NoError(err)
|
|
|
35
|
+
|
|
|
36
|
+ _, err = rand.Read(suite.h.Random[:])
|
|
|
37
|
+ suite.NoError(err)
|
|
|
38
|
+
|
|
|
39
|
+ suite.buf = &bytes.Buffer{}
|
|
|
40
|
+
|
|
|
41
|
+ suite.secret = mtglib.GenerateSecret("google.com")
|
|
|
42
|
+}
|
|
|
43
|
+
|
|
|
44
|
+func (suite *WelcomeTestSuite) TestOk() {
|
|
|
45
|
+ suite.NoError(faketls.SendWelcomePacket(suite.buf, suite.secret.Key[:], *suite.h))
|
|
|
46
|
+
|
|
|
47
|
+ welcomePacket := []byte{}
|
|
|
48
|
+ welcomePacket = append(welcomePacket, suite.buf.Bytes()...)
|
|
|
49
|
+
|
|
|
50
|
+ rec := record.AcquireRecord()
|
|
|
51
|
+ defer record.ReleaseRecord(rec)
|
|
|
52
|
+
|
|
|
53
|
+ suite.NoError(rec.Read(suite.buf))
|
|
|
54
|
+ suite.Equal(record.TypeHandshake, rec.Type)
|
|
|
55
|
+ suite.Equal(record.Version12, rec.Version)
|
|
|
56
|
+
|
|
|
57
|
+ suite.NoError(rec.Read(suite.buf))
|
|
|
58
|
+ suite.Equal(record.TypeChangeCipherSpec, rec.Type)
|
|
|
59
|
+ suite.Equal(record.Version12, rec.Version)
|
|
|
60
|
+
|
|
|
61
|
+ suite.NoError(rec.Read(suite.buf))
|
|
|
62
|
+ suite.Equal(record.TypeApplicationData, rec.Type)
|
|
|
63
|
+ suite.Equal(record.Version12, rec.Version)
|
|
|
64
|
+ suite.Empty(suite.buf.Bytes())
|
|
|
65
|
+
|
|
|
66
|
+ random := make([]byte, 32)
|
|
|
67
|
+ copy(random, welcomePacket[11:])
|
|
|
68
|
+
|
|
|
69
|
+ empty := make([]byte, 32)
|
|
|
70
|
+ copy(welcomePacket[11:], empty)
|
|
|
71
|
+
|
|
|
72
|
+ mac := hmac.New(sha256.New, suite.secret.Key[:])
|
|
|
73
|
+ mac.Write(suite.h.Random[:])
|
|
|
74
|
+ mac.Write(welcomePacket)
|
|
|
75
|
+
|
|
|
76
|
+ suite.Equal(random, mac.Sum(nil))
|
|
|
77
|
+}
|
|
|
78
|
+
|
|
|
79
|
+func TestWelcome(t *testing.T) {
|
|
|
80
|
+ t.Parallel()
|
|
|
81
|
+ suite.Run(t, &WelcomeTestSuite{})
|
|
|
82
|
+}
|