Debian Codegraph @

syntax/tls_test.go

raw symbols SCIP
1 package syntax
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "fmt"
7 "reflect"
8 "testing"
9 )
10
11 type ProtocolVersion uint16
12
13 type ExtensionType uint16
14 type Extension struct {
15 ExtensionType ExtensionType
16 ExtensionData []byte `tls:"head=2"`
17 }
18
19 type Random [32]byte
20 type CipherSuite uint16
21
22 type ClientHello struct {
23 LegacyVersion ProtocolVersion
24 Random Random
25 LegacySessionID []byte `tls:"head=1,max=32"`
26 CipherSuites []CipherSuite `tls:"head=2,min=2"`
27 LegacyCompressionMethods []byte `tls:"head=1,min=1"`
28 Extensions []Extension `tls:"head=2"`
29 }
30
31 type ServerHello struct {
32 Version ProtocolVersion
33 Random Random
34 CipherSuite CipherSuite
35 Extensions []Extension `tls:"head=2"`
36 }
37
38 var (
39 extValidIn = Extension{
40 ExtensionType: ExtensionType(0x000a),
41 ExtensionData: []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4},
42 }
43 extEmptyIn = Extension{
44 ExtensionType: ExtensionType(0x000a),
45 ExtensionData: []byte{},
46 }
47 extListValidIn = []Extension{extValidIn, extEmptyIn}
48 extListValidHex = "000d000a0005f0f1f2f3f4000a0000"
49
50 helloRandom = [32]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
51 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
52 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
53 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37}
54 chValidIn = ClientHello{
55 LegacyVersion: 0x0303,
56 Random: helloRandom,
57 LegacySessionID: []byte{},
58 CipherSuites: []CipherSuite{0x0001, 0x0002, 0x0003},
59 LegacyCompressionMethods: []byte{0},
60 Extensions: extListValidIn,
61 }
62 chValidHex = "0303" + hex.EncodeToString(helloRandom[:]) + "00" +
63 "0006000100020003" + "0100" + extListValidHex
64
65 shValidIn = ServerHello{
66 Version: 0x7f12,
67 Random: helloRandom,
68 CipherSuite: CipherSuite(0x0001),
69 Extensions: extListValidIn,
70 }
71 shValidHex = "7f12" + hex.EncodeToString(helloRandom[:]) + "0001" + extListValidHex
72 )
73
74 func TestTLSMarshal(t *testing.T) {
75 chValid, _ := hex.DecodeString(chValidHex)
76 shValid, _ := hex.DecodeString(shValidHex)
77
78 // ClientHello marshal
79 out, err := Marshal(chValidIn)
80 if err != nil {
81 t.Fatalf("Failed to marshal a valid ClientHello [%v]", err)
82 }
83 if !bytes.Equal(out, chValid) {
84 t.Fatalf("Failed to marshal a valid ClientHello [%x] != [%x]", out, chValid)
85 }
86
87 // ServerHello marshal
88 out, err = Marshal(shValidIn)
89 if err != nil {
90 t.Fatalf("Failed to marshal a valid ServerHello [%v]", err)
91 }
92 if !bytes.Equal(out, shValid) {
93 t.Fatalf("Failed to marshal a valid ServerHello [%x] != [%x]", out, shValid)
94 }
95 }
96
97 func TestTLSUnmarshal(t *testing.T) {
98 chValid, _ := hex.DecodeString(chValidHex)
99 shValid, _ := hex.DecodeString(shValidHex)
100
101 // ClientHello marshal
102 var ch ClientHello
103 read, err := Unmarshal(chValid, &ch)
104 if err != nil || read != len(chValid) {
105 t.Fatalf("Failed to unmarshal a valid ClientHello [%v]", err)
106 }
107 if !reflect.DeepEqual(ch, chValidIn) {
108 fmt.Println("LegacyVersion", reflect.DeepEqual(ch.LegacyVersion, chValidIn.LegacyVersion))
109 fmt.Println("Random", reflect.DeepEqual(ch.Random, chValidIn.Random))
110 fmt.Println("LegacySessionID")
111 fmt.Printf(" %+v\n", ch.LegacySessionID == nil)
112 fmt.Printf(" %+v\n", chValidIn.LegacySessionID == nil)
113 fmt.Println("CipherSuites", reflect.DeepEqual(ch.CipherSuites, chValidIn.CipherSuites))
114 fmt.Println("LegacyCompressionMethods", reflect.DeepEqual(ch.LegacyCompressionMethods, chValidIn.LegacyCompressionMethods))
115 fmt.Println("Extensions", reflect.DeepEqual(ch.Extensions, chValidIn.Extensions))
116 t.Errorf("Failed to unmarshal a valid ClientHello [%+v] [%+v]", ch, chValidIn)
117 }
118
119 // ServerHello marshal
120 var sh ServerHello
121 read, err = Unmarshal(shValid, &sh)
122 if err != nil || read != len(shValid) {
123 t.Fatalf("Failed to unmarshal a valid ServerHello [%v]", err)
124 }
125 if !reflect.DeepEqual(sh, shValidIn) {
126 t.Errorf("Failed to unmarshal a valid ServerHello")
127 }
128 }