From 88d32191f7f780b2ec42cf79696bcc5e5c4a7cf9 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Sat, 13 Jan 2018 11:43:49 -0500 Subject: [PATCH] Breaking out KeepAlive into a new control channel file for easier testing --- connection/connection.go | 18 ++++++++--------- .../{features.go => control_channel.go} | 9 +++++++++ ...atures_test.go => control_channel_test.go} | 20 +++++++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) rename connection/{features.go => control_channel.go} (72%) rename connection/{features_test.go => control_channel_test.go} (65%) diff --git a/connection/connection.go b/connection/connection.go index 3a38513..d73e66e 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -390,7 +390,8 @@ func (rc *Connection) controlPacket(handler Handler, res *Protocol_Data_Control. // Enforce Authentication Check. _, authed := rc.Authentication[chandler.RequiresAuthentication()] if !authed { - rc.SendRicochetPacket(rc.Conn, 0, []byte{}) + response := rc.messageBuilder.RejectOpenChannel(opm.GetChannelIdentifier(), "UnauthorizedError") + rc.SendRicochetPacket(rc.Conn, 0, response) rc.traceLog(fmt.Sprintf("do not have required authorization to open channel type %v", chandler.Type())) return } @@ -444,7 +445,7 @@ func (rc *Connection) controlPacket(handler Handler, res *Protocol_Data_Control. channel.Handler.OpenOutboundResult(nil, cr) } else { rc.traceLog(fmt.Sprintf("channel of type %v rejected on %v", channel.Type, id)) - channel.Handler.OpenOutboundResult(errors.New(""), cr) + channel.Handler.OpenOutboundResult(errors.New(cr.GetCommonError().String()), cr) } } else if res.GetKeepAlive() != nil { @@ -452,17 +453,16 @@ func (rc *Connection) controlPacket(handler Handler, res *Protocol_Data_Control. // We should likely put these calls behind // authentication. rc.traceLog("received keep alive packet") - if res.GetKeepAlive().GetResponseRequested() { - messageBuilder := new(utils.MessageBuilder) - raw := messageBuilder.KeepAlive(true) + respond, data := ProcessKeepAlive(res.GetKeepAlive()) + if respond { rc.traceLog("sending keep alive response") - rc.SendRicochetPacket(rc.Conn, 0, raw) + rc.SendRicochetPacket(rc.Conn, 0, data) } } else if res.GetEnableFeatures() != nil { rc.traceLog("received enable features packet") - raw := ProcessEnableFeatures(handler, res.GetEnableFeatures()) - rc.traceLog(fmt.Sprintf("sending featured enabled: %v", raw)) - rc.SendRicochetPacket(rc.Conn, 0, raw) + data := ProcessEnableFeatures(handler, res.GetEnableFeatures()) + rc.traceLog(fmt.Sprintf("sending featured enabled: %v", data)) + rc.SendRicochetPacket(rc.Conn, 0, data) } else if res.GetFeaturesEnabled() != nil { rc.SupportChannels = res.GetFeaturesEnabled().GetFeature() rc.traceLog(fmt.Sprintf("connection supports: %v", rc.SupportChannels)) diff --git a/connection/features.go b/connection/control_channel.go similarity index 72% rename from connection/features.go rename to connection/control_channel.go index b4642c6..6ca2eae 100644 --- a/connection/features.go +++ b/connection/control_channel.go @@ -5,6 +5,15 @@ import ( "github.com/s-rah/go-ricochet/wire/control" ) +// ProcessKeepAlive +func ProcessKeepAlive(ka *Protocol_Data_Control.KeepAlive) (bool, []byte) { + if ka.GetResponseRequested() { + messageBuilder := new(utils.MessageBuilder) + return true, messageBuilder.KeepAlive(true) + } + return false, nil +} + // ProcessEnableFeatures correctly handles a features enabled packet func ProcessEnableFeatures(handler Handler, ef *Protocol_Data_Control.EnableFeatures) []byte { featuresToEnable := ef.GetFeature() diff --git a/connection/features_test.go b/connection/control_channel_test.go similarity index 65% rename from connection/features_test.go rename to connection/control_channel_test.go index eee77cd..b36f869 100644 --- a/connection/features_test.go +++ b/connection/control_channel_test.go @@ -14,6 +14,26 @@ func (m *MockHandler) GetSupportedChannelTypes() []string { return []string{"im.ricochet.chat"} } +func TestKeepAliveNoResponse(t *testing.T) { + ka := &Protocol_Data_Control.KeepAlive{ + ResponseRequested: proto.Bool(false), + } + respond, _ := ProcessKeepAlive(ka) + if respond == true { + t.Errorf("KeepAlive process should have not needed a response %v %v", ka, respond) + } +} + +func TestKeepAliveRequestResponse(t *testing.T) { + ka := &Protocol_Data_Control.KeepAlive{ + ResponseRequested: proto.Bool(true), + } + respond, _ := ProcessKeepAlive(ka) + if respond == false { + t.Errorf("KeepAlive process should have produced a response %v %v", ka, respond) + } +} + func TestEnableFeatures(t *testing.T) { handler := new(MockHandler) features := []string{"feature1", "im.ricochet.chat"}