|
|
@@ -2,9 +2,9 @@ package fake_test
|
|
2
|
2
|
|
|
3
|
3
|
import (
|
|
4
|
4
|
"bytes"
|
|
|
5
|
+ cryptotls "crypto/tls"
|
|
5
|
6
|
"encoding/binary"
|
|
6
|
7
|
"encoding/json"
|
|
7
|
|
- "errors"
|
|
8
|
8
|
"io"
|
|
9
|
9
|
"os"
|
|
10
|
10
|
"testing"
|
|
|
@@ -14,7 +14,6 @@ import (
|
|
14
|
14
|
"github.com/dolonet/mtg-multi/mtglib"
|
|
15
|
15
|
"github.com/dolonet/mtg-multi/mtglib/internal/tls"
|
|
16
|
16
|
"github.com/dolonet/mtg-multi/mtglib/internal/tls/fake"
|
|
17
|
|
- "github.com/stretchr/testify/mock"
|
|
18
|
17
|
"github.com/stretchr/testify/require"
|
|
19
|
18
|
"github.com/stretchr/testify/suite"
|
|
20
|
19
|
)
|
|
|
@@ -53,11 +52,6 @@ func (suite *ParseClientHelloTestSuite) SetupTest() {
|
|
53
|
52
|
suite.connMock = &parseClientHelloConnMock{
|
|
54
|
53
|
readBuf: suite.readBuf,
|
|
55
|
54
|
}
|
|
56
|
|
-
|
|
57
|
|
- suite.connMock.
|
|
58
|
|
- On("SetReadDeadline", mock.AnythingOfType("time.Time")).
|
|
59
|
|
- Twice().
|
|
60
|
|
- Return(nil)
|
|
61
|
55
|
}
|
|
62
|
56
|
|
|
63
|
57
|
func (suite *ParseClientHelloTestSuite) TearDownTest() {
|
|
|
@@ -69,23 +63,11 @@ type ParseClientHello_TLSHeaderTestSuite struct {
|
|
69
|
63
|
}
|
|
70
|
64
|
|
|
71
|
65
|
func (suite *ParseClientHello_TLSHeaderTestSuite) TestEmpty() {
|
|
72
|
|
- suite.connMock.ExpectedCalls = []*mock.Call{}
|
|
73
|
|
- suite.connMock.
|
|
74
|
|
- On("SetReadDeadline", mock.AnythingOfType("time.Time")).
|
|
75
|
|
- Once().
|
|
76
|
|
- Return(errors.New("fail"))
|
|
77
|
|
-
|
|
78
|
66
|
_, err := fake.ReadClientHello(suite.connMock, suite.secret.Key[:], suite.secret.Host, TolerateTime)
|
|
79
|
|
- suite.ErrorContains(err, "fail")
|
|
|
67
|
+ suite.ErrorContains(err, "cannot read client hello")
|
|
80
|
68
|
}
|
|
81
|
69
|
|
|
82
|
70
|
func (suite *ParseClientHello_TLSHeaderTestSuite) TestNothing() {
|
|
83
|
|
- suite.connMock.ExpectedCalls = []*mock.Call{}
|
|
84
|
|
- suite.connMock.
|
|
85
|
|
- On("SetReadDeadline", mock.AnythingOfType("time.Time")).
|
|
86
|
|
- Twice().
|
|
87
|
|
- Return(nil)
|
|
88
|
|
-
|
|
89
|
71
|
_, err := fake.ReadClientHello(suite.connMock, suite.secret.Key[:], suite.secret.Host, TolerateTime)
|
|
90
|
72
|
suite.ErrorIs(err, io.EOF)
|
|
91
|
73
|
}
|
|
|
@@ -234,12 +216,13 @@ func (suite *ParseClientHelloHandshakeBodyTestSuite) TestCannotReadCipherSuiteLe
|
|
234
|
216
|
}
|
|
235
|
217
|
|
|
236
|
218
|
func (suite *ParseClientHelloHandshakeBodyTestSuite) TestCannotReadFirstCipherSuite() {
|
|
237
|
|
- body := make([]byte, 2+fake.RandomLen+1+2)
|
|
|
219
|
+ body := make([]byte, 2+fake.RandomLen+1+2+1) // cipherSuiteLen=2 but only 1 byte available
|
|
|
220
|
+ binary.BigEndian.PutUint16(body[2+fake.RandomLen+1:], 2)
|
|
238
|
221
|
|
|
239
|
222
|
suite.writeBody(body)
|
|
240
|
223
|
|
|
241
|
224
|
_, err := fake.ReadClientHello(suite.connMock, suite.secret.Key[:], suite.secret.Host, TolerateTime)
|
|
242
|
|
- suite.ErrorContains(err, "cannot read first cipher suite")
|
|
|
225
|
+ suite.ErrorContains(err, "cannot read cipher suite")
|
|
243
|
226
|
}
|
|
244
|
227
|
|
|
245
|
228
|
func (suite *ParseClientHelloHandshakeBodyTestSuite) TestCannotSkipRemainingCipherSuites() {
|
|
|
@@ -249,12 +232,27 @@ func (suite *ParseClientHelloHandshakeBodyTestSuite) TestCannotSkipRemainingCiph
|
|
249
|
232
|
suite.writeBody(body)
|
|
250
|
233
|
|
|
251
|
234
|
_, err := fake.ReadClientHello(suite.connMock, suite.secret.Key[:], suite.secret.Host, TolerateTime)
|
|
252
|
|
- suite.ErrorContains(err, "cannot skip remaining cipher suites")
|
|
|
235
|
+ suite.ErrorContains(err, "cannot read cipher suite")
|
|
|
236
|
+}
|
|
|
237
|
+
|
|
|
238
|
+func (suite *ParseClientHelloHandshakeBodyTestSuite) TestCannotFindCipher() {
|
|
|
239
|
+ // All cipher suites are GREASE values — must return ErrCannotFindCipher.
|
|
|
240
|
+ body := make([]byte, 2+fake.RandomLen+1+2+4+1)
|
|
|
241
|
+ binary.BigEndian.PutUint16(body[2+fake.RandomLen+1:], 4)
|
|
|
242
|
+ binary.BigEndian.PutUint16(body[2+fake.RandomLen+1+2:], 0x0a0a)
|
|
|
243
|
+ binary.BigEndian.PutUint16(body[2+fake.RandomLen+1+2+2:], 0x1a1a)
|
|
|
244
|
+ body[2+fake.RandomLen+1+2+4] = 1
|
|
|
245
|
+
|
|
|
246
|
+ suite.writeBody(body)
|
|
|
247
|
+
|
|
|
248
|
+ _, err := fake.ReadClientHello(suite.connMock, suite.secret.Key[:], suite.secret.Host, TolerateTime)
|
|
|
249
|
+ suite.ErrorIs(err, fake.ErrCannotFindCipher)
|
|
253
|
250
|
}
|
|
254
|
251
|
|
|
255
|
252
|
func (suite *ParseClientHelloHandshakeBodyTestSuite) TestCannotReadCompressionMethodsLength() {
|
|
256
|
253
|
body := make([]byte, 2+fake.RandomLen+1+2+2)
|
|
257
|
254
|
binary.BigEndian.PutUint16(body[2+fake.RandomLen+1:], 2)
|
|
|
255
|
+ binary.BigEndian.PutUint16(body[2+fake.RandomLen+1+2:], cryptotls.TLS_AES_128_GCM_SHA256)
|
|
258
|
256
|
|
|
259
|
257
|
suite.writeBody(body)
|
|
260
|
258
|
|
|
|
@@ -265,6 +263,7 @@ func (suite *ParseClientHelloHandshakeBodyTestSuite) TestCannotReadCompressionMe
|
|
265
|
263
|
func (suite *ParseClientHelloHandshakeBodyTestSuite) TestCannotSkipCompressionMethods() {
|
|
266
|
264
|
body := make([]byte, 2+fake.RandomLen+1+2+2+1)
|
|
267
|
265
|
binary.BigEndian.PutUint16(body[2+fake.RandomLen+1:], 2)
|
|
|
266
|
+ binary.BigEndian.PutUint16(body[2+fake.RandomLen+1+2:], cryptotls.TLS_AES_128_GCM_SHA256)
|
|
268
|
267
|
body[2+fake.RandomLen+1+2+2] = 1
|
|
269
|
268
|
|
|
270
|
269
|
suite.writeBody(body)
|
|
|
@@ -300,6 +299,7 @@ func (suite *ParseClientHelloSNITestSuite) writeExtensions(extensions []byte) {
|
|
300
|
299
|
// cipherSuite(2) + compressionLen(1) + compression(1) = 41
|
|
301
|
300
|
body := make([]byte, 41)
|
|
302
|
301
|
binary.BigEndian.PutUint16(body[35:], 2)
|
|
|
302
|
+ binary.BigEndian.PutUint16(body[37:], cryptotls.TLS_AES_128_GCM_SHA256)
|
|
303
|
303
|
body[39] = 1
|
|
304
|
304
|
|
|
305
|
305
|
suite.readBuf.Write(body)
|
|
|
@@ -478,11 +478,6 @@ func (s *ParseClientHelloFragmentedTestSuite) makeConn(data []byte) *parseClient
|
|
478
|
478
|
readBuf: readBuf,
|
|
479
|
479
|
}
|
|
480
|
480
|
|
|
481
|
|
- connMock.
|
|
482
|
|
- On("SetReadDeadline", mock.AnythingOfType("time.Time")).
|
|
483
|
|
- Twice().
|
|
484
|
|
- Return(nil)
|
|
485
|
|
-
|
|
486
|
481
|
return connMock
|
|
487
|
482
|
}
|
|
488
|
483
|
|