forked from refraction-networking/utls
-
Notifications
You must be signed in to change notification settings - Fork 1
/
u_clienthello_json_test.go
123 lines (105 loc) · 4.83 KB
/
u_clienthello_json_test.go
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
package tls
import (
"encoding/json"
"os"
"reflect"
"testing"
)
func TestClientHelloSpecJSONUnmarshaler(t *testing.T) {
testClientHelloSpecJSONUnmarshaler(t, "testdata/ClientHello-JSON-Chrome102.json", HelloChrome_102)
testClientHelloSpecJSONUnmarshaler(t, "testdata/ClientHello-JSON-Firefox105.json", HelloFirefox_105)
testClientHelloSpecJSONUnmarshaler(t, "testdata/ClientHello-JSON-iOS14.json", HelloIOS_14)
testClientHelloSpecJSONUnmarshaler(t, "testdata/ClientHello-JSON-Edge106.json", HelloEdge_106)
}
func testClientHelloSpecJSONUnmarshaler(
t *testing.T,
jsonFilepath string,
truthClientHelloID ClientHelloID,
) {
jsonCH, err := os.ReadFile(jsonFilepath)
if err != nil {
t.Fatal(err)
}
var chsju ClientHelloSpecJSONUnmarshaler
if err := json.Unmarshal(jsonCH, &chsju); err != nil {
t.Fatal(err)
}
truthSpec, _ := utlsIdToSpec(truthClientHelloID)
jsonSpec := chsju.ClientHelloSpec()
// Compare CipherSuites
if !reflect.DeepEqual(jsonSpec.CipherSuites, truthSpec.CipherSuites) {
t.Errorf("JSONUnmarshaler %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.CipherSuites, truthSpec.CipherSuites)
}
// Compare CompressionMethods
if !reflect.DeepEqual(jsonSpec.CompressionMethods, truthSpec.CompressionMethods) {
t.Errorf("JSONUnmarshaler %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.CompressionMethods, truthSpec.CompressionMethods)
}
// Compare Extensions
if len(jsonSpec.Extensions) != len(truthSpec.Extensions) {
t.Errorf("JSONUnmarshaler %s: len(jsonExtensions) = %d != %d = len(truthExtensions)", clientHelloSpecJSONTestIdentifier(truthClientHelloID), len(jsonSpec.Extensions), len(truthSpec.Extensions))
}
for i := range jsonSpec.Extensions {
if !reflect.DeepEqual(jsonSpec.Extensions[i], truthSpec.Extensions[i]) {
if _, ok := jsonSpec.Extensions[i].(*UtlsPaddingExtension); ok {
testedPaddingExt := jsonSpec.Extensions[i].(*UtlsPaddingExtension)
savedPaddingExt := truthSpec.Extensions[i].(*UtlsPaddingExtension)
if testedPaddingExt.PaddingLen != savedPaddingExt.PaddingLen || testedPaddingExt.WillPad != savedPaddingExt.WillPad {
t.Errorf("got %#v, want %#v", testedPaddingExt, savedPaddingExt)
} else {
continue // UtlsPaddingExtension has non-nil function member
}
}
t.Errorf("JSONUnmarshaler %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.Extensions[i], truthSpec.Extensions[i])
}
}
}
func TestClientHelloSpecUnmarshalJSON(t *testing.T) {
testClientHelloSpecUnmarshalJSON(t, "testdata/ClientHello-JSON-Chrome102.json", HelloChrome_102)
testClientHelloSpecUnmarshalJSON(t, "testdata/ClientHello-JSON-Firefox105.json", HelloFirefox_105)
testClientHelloSpecUnmarshalJSON(t, "testdata/ClientHello-JSON-iOS14.json", HelloIOS_14)
testClientHelloSpecUnmarshalJSON(t, "testdata/ClientHello-JSON-Edge106.json", HelloEdge_106)
}
func testClientHelloSpecUnmarshalJSON(
t *testing.T,
jsonFilepath string,
truthClientHelloID ClientHelloID,
) {
var jsonSpec ClientHelloSpec
jsonCH, err := os.ReadFile(jsonFilepath)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(jsonCH, &jsonSpec); err != nil {
t.Fatal(err)
}
truthSpec, _ := utlsIdToSpec(truthClientHelloID)
// Compare CipherSuites
if !reflect.DeepEqual(jsonSpec.CipherSuites, truthSpec.CipherSuites) {
t.Errorf("UnmarshalJSON %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.CipherSuites, truthSpec.CipherSuites)
}
// Compare CompressionMethods
if !reflect.DeepEqual(jsonSpec.CompressionMethods, truthSpec.CompressionMethods) {
t.Errorf("UnmarshalJSON %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.CompressionMethods, truthSpec.CompressionMethods)
}
// Compare Extensions
if len(jsonSpec.Extensions) != len(truthSpec.Extensions) {
t.Errorf("UnmarshalJSON %s: len(jsonExtensions) = %d != %d = len(truthExtensions)", jsonFilepath, len(jsonSpec.Extensions), len(truthSpec.Extensions))
}
for i := range jsonSpec.Extensions {
if !reflect.DeepEqual(jsonSpec.Extensions[i], truthSpec.Extensions[i]) {
if _, ok := jsonSpec.Extensions[i].(*UtlsPaddingExtension); ok {
testedPaddingExt := jsonSpec.Extensions[i].(*UtlsPaddingExtension)
savedPaddingExt := truthSpec.Extensions[i].(*UtlsPaddingExtension)
if testedPaddingExt.PaddingLen != savedPaddingExt.PaddingLen || testedPaddingExt.WillPad != savedPaddingExt.WillPad {
t.Errorf("got %#v, want %#v", testedPaddingExt, savedPaddingExt)
} else {
continue // UtlsPaddingExtension has non-nil function member
}
}
t.Errorf("UnmarshalJSON %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.Extensions[i], truthSpec.Extensions[i])
}
}
}
func clientHelloSpecJSONTestIdentifier(id ClientHelloID) string {
return id.Client + id.Version
}