func TestClose(t *testing.T) { writeChan, controlChan, msgChan := setupServerHandshake() close := script.NewEvent("close", nil, script.Close{msgChan}) closed1 := script.NewEvent("closed1", []*script.Event{close}, script.Closed{writeChan}) closed2 := script.NewEvent("closed2", []*script.Event{close}, script.Closed{controlChan}) err := script.Perform(0, []*script.Event{close, closed1, closed2}) 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 testClientHelloFailure(t *testing.T, clientHello interface{}, expectedAlert alertType) { writeChan, controlChan, msgChan := setupServerHandshake() defer close(msgChan) send := script.NewEvent("send", nil, script.Send{msgChan, clientHello}) recvAlert := script.NewEvent("recv alert", []*script.Event{send}, script.Recv{writeChan, alert{alertLevelError, expectedAlert}}) close1 := script.NewEvent("msgChan close", []*script.Event{recvAlert}, script.Closed{writeChan}) recvState := script.NewEvent("recv state", []*script.Event{send}, script.Recv{controlChan, ConnectionState{false, "", expectedAlert}}) close2 := script.NewEvent("controlChan close", []*script.Event{recvState}, script.Closed{controlChan}) err := script.Perform(0, []*script.Event{send, recvAlert, close1, recvState, close2}) if err != nil { t.Errorf("Got error: %s", err) } }
func TestAlertForwarding(t *testing.T) { writeChan, controlChan, msgChan := setupServerHandshake() defer close(msgChan) a := alert{alertLevelError, alertNoRenegotiation} sendAlert := script.NewEvent("send alert", nil, script.Send{msgChan, a}) recvAlert := script.NewEvent("recv alert", []*script.Event{sendAlert}, script.Recv{writeChan, a}) closeWriter := script.NewEvent("close writer", []*script.Event{recvAlert}, script.Closed{writeChan}) closeControl := script.NewEvent("close control", []*script.Event{recvAlert}, script.Closed{controlChan}) err := script.Perform(0, []*script.Event{sendAlert, recvAlert, closeWriter, closeControl}) if err != nil { t.Errorf("Got error: %s", err) } }
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 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 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) } }
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 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 TestFullHandshake(t *testing.T) { writeChan, controlChan, msgChan := setupServerHandshake() defer close(msgChan) // The values for this test were obtained from running `gnutls-cli --insecure --debug 9` clientHello := &clientHelloMsg{fromHex("0100007603024aef7d77e4686d5dfd9d953dfe280788759ffd440867d687670216da45516b310000340033004500390088001600320044003800870013006600900091008f008e002f004100350084000a00050004008c008d008b008a01000019000900030200010000000e000c0000093132372e302e302e31"), 3, 2, fromHex("4aef7d77e4686d5dfd9d953dfe280788759ffd440867d687670216da45516b31"), nil, []uint16{0x33, 0x45, 0x39, 0x88, 0x16, 0x32, 0x44, 0x38, 0x87, 0x13, 0x66, 0x90, 0x91, 0x8f, 0x8e, 0x2f, 0x41, 0x35, 0x84, 0xa, 0x5, 0x4, 0x8c, 0x8d, 0x8b, 0x8a}, []uint8{0x0}} sendHello := script.NewEvent("send hello", nil, script.Send{msgChan, clientHello}) setVersion := script.NewEvent("set version", []*script.Event{sendHello}, script.Recv{writeChan, writerSetVersion{3, 2}}) recvHello := script.NewEvent("recv hello", []*script.Event{setVersion}, script.RecvMatch{writeChan, matchServerHello}) recvCert := script.NewEvent("recv cert", []*script.Event{recvHello}, script.RecvMatch{writeChan, matchCertificate}) recvDone := script.NewEvent("recv done", []*script.Event{recvCert}, script.RecvMatch{writeChan, matchDone}) ckx := &clientKeyExchangeMsg{nil, fromHex("872e1fee5f37dd86f3215938ac8de20b302b90074e9fb93097e6b7d1286d0f45abf2daf179deb618bb3c70ed0afee6ee24476ee4649e5a23358143c0f1d9c251")} sendCKX := script.NewEvent("send ckx", []*script.Event{recvDone}, script.Send{msgChan, ckx}) sendCCS := script.NewEvent("send ccs", []*script.Event{sendCKX}, script.Send{msgChan, changeCipherSpec{}}) recvNCS := script.NewEvent("recv done", []*script.Event{sendCCS}, script.RecvMatch{controlChan, matchNewCipherSpec}) finished := &finishedMsg{nil, fromHex("c8faca5d242f4423325c5b1a")} sendFinished := script.NewEvent("send finished", []*script.Event{recvNCS}, script.Send{msgChan, finished}) recvFinished := script.NewEvent("recv finished", []*script.Event{sendFinished}, script.RecvMatch{writeChan, matchFinished}) setCipher := script.NewEvent("set cipher", []*script.Event{sendFinished}, script.RecvMatch{writeChan, matchSetCipher}) recvConnectionState := script.NewEvent("recv state", []*script.Event{sendFinished}, script.Recv{controlChan, ConnectionState{true, "TLS_RSA_WITH_RC4_128_SHA", 0}}) err := script.Perform(0, []*script.Event{sendHello, setVersion, recvHello, recvCert, recvDone, sendCKX, sendCCS, recvNCS, sendFinished, setCipher, recvConnectionState, recvFinished}) if err != nil { t.Errorf("Got error: %s", err) } }