summaryrefslogtreecommitdiff
path: root/src/pkg/crypto/tls/record_process_test.go
blob: 65ce3eba95ae5ee76d4cb8749b88eb67a46cada7 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tls

import (
	"encoding/hex"
	"testing"
	"testing/script"
)

func setup() (appDataChan chan []byte, requestChan chan interface{}, controlChan chan interface{}, recordChan chan *record, handshakeChan chan interface{}) {
	rp := new(recordProcessor)
	appDataChan = make(chan []byte)
	requestChan = make(chan interface{})
	controlChan = make(chan interface{})
	recordChan = make(chan *record)
	handshakeChan = make(chan interface{})

	go rp.loop(appDataChan, requestChan, controlChan, recordChan, handshakeChan)
	return
}

func fromHex(s string) []byte {
	b, _ := hex.DecodeString(s)
	return b
}

func TestNullConnectionState(t *testing.T) {
	_, requestChan, controlChan, recordChan, _ := setup()
	defer close(requestChan)
	defer close(controlChan)
	defer close(recordChan)

	// Test a simple request for the connection state.
	replyChan := make(chan ConnectionState)
	sendReq := script.NewEvent("send request", nil, script.Send{requestChan, getConnectionState{replyChan}})
	getReply := script.NewEvent("get reply", []*script.Event{sendReq}, script.Recv{replyChan, ConnectionState{false, "", 0}})

	err := script.Perform(0, []*script.Event{sendReq, getReply})
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}

func TestWaitConnectionState(t *testing.T) {
	_, requestChan, controlChan, recordChan, _ := setup()
	defer close(requestChan)
	defer close(controlChan)
	defer close(recordChan)

	// Test that waitConnectionState doesn't get a reply until the connection state changes.
	replyChan := make(chan ConnectionState)
	sendReq := script.NewEvent("send request", nil, script.Send{requestChan, waitConnectionState{replyChan}})
	replyChan2 := make(chan ConnectionState)
	sendReq2 := script.NewEvent("send request 2", []*script.Event{sendReq}, script.Send{requestChan, getConnectionState{replyChan2}})
	getReply2 := script.NewEvent("get reply 2", []*script.Event{sendReq2}, script.Recv{replyChan2, ConnectionState{false, "", 0}})
	sendState := script.NewEvent("send state", []*script.Event{getReply2}, script.Send{controlChan, ConnectionState{true, "test", 1}})
	getReply := script.NewEvent("get reply", []*script.Event{sendState}, script.Recv{replyChan, ConnectionState{true, "test", 1}})

	err := script.Perform(0, []*script.Event{sendReq, sendReq2, getReply2, sendState, getReply})
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}

func TestHandshakeAssembly(t *testing.T) {
	_, requestChan, controlChan, recordChan, handshakeChan := setup()
	defer close(requestChan)
	defer close(controlChan)
	defer close(recordChan)

	// Test the reassembly of a fragmented handshake message.
	send1 := script.NewEvent("send 1", nil, script.Send{recordChan, &record{recordTypeHandshake, 0, 0, fromHex("10000003")}})
	send2 := script.NewEvent("send 2", []*script.Event{send1}, script.Send{recordChan, &record{recordTypeHandshake, 0, 0, fromHex("0001")}})
	send3 := script.NewEvent("send 3", []*script.Event{send2}, script.Send{recordChan, &record{recordTypeHandshake, 0, 0, fromHex("42")}})
	recvMsg := script.NewEvent("recv", []*script.Event{send3}, script.Recv{handshakeChan, &clientKeyExchangeMsg{fromHex("10000003000142"), fromHex("42")}})

	err := script.Perform(0, []*script.Event{send1, send2, send3, recvMsg})
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}

func TestEarlyApplicationData(t *testing.T) {
	_, requestChan, controlChan, recordChan, handshakeChan := setup()
	defer close(requestChan)
	defer close(controlChan)
	defer close(recordChan)

	// Test that applicaton data received before the handshake has completed results in an error.
	send := script.NewEvent("send", nil, script.Send{recordChan, &record{recordTypeApplicationData, 0, 0, fromHex("")}})
	recv := script.NewEvent("recv", []*script.Event{send}, script.Closed{handshakeChan})

	err := script.Perform(0, []*script.Event{send, recv})
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}

func TestApplicationData(t *testing.T) {
	appDataChan, requestChan, controlChan, recordChan, handshakeChan := setup()
	defer close(requestChan)
	defer close(controlChan)
	defer close(recordChan)

	// Test that the application data is forwarded after a successful Finished message.
	send1 := script.NewEvent("send 1", nil, script.Send{recordChan, &record{recordTypeHandshake, 0, 0, fromHex("1400000c000000000000000000000000")}})
	recv1 := script.NewEvent("recv finished", []*script.Event{send1}, script.Recv{handshakeChan, &finishedMsg{fromHex("1400000c000000000000000000000000"), fromHex("000000000000000000000000")}})
	send2 := script.NewEvent("send connState", []*script.Event{recv1}, script.Send{controlChan, ConnectionState{true, "", 0}})
	send3 := script.NewEvent("send 2", []*script.Event{send2}, script.Send{recordChan, &record{recordTypeApplicationData, 0, 0, fromHex("0102")}})
	recv2 := script.NewEvent("recv data", []*script.Event{send3}, script.Recv{appDataChan, []byte{0x01, 0x02}})

	err := script.Perform(0, []*script.Event{send1, recv1, send2, send3, recv2})
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}

func TestInvalidChangeCipherSpec(t *testing.T) {
	appDataChan, requestChan, controlChan, recordChan, handshakeChan := setup()
	defer close(requestChan)
	defer close(controlChan)
	defer close(recordChan)

	send1 := script.NewEvent("send 1", nil, script.Send{recordChan, &record{recordTypeChangeCipherSpec, 0, 0, []byte{1}}})
	recv1 := script.NewEvent("recv 1", []*script.Event{send1}, script.Recv{handshakeChan, changeCipherSpec{}})
	send2 := script.NewEvent("send 2", []*script.Event{recv1}, script.Send{controlChan, ConnectionState{false, "", 42}})
	close := script.NewEvent("close 1", []*script.Event{send2}, script.Closed{appDataChan})
	close2 := script.NewEvent("close 2", []*script.Event{send2}, script.Closed{handshakeChan})

	err := script.Perform(0, []*script.Event{send1, recv1, send2, close, close2})
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}