Breaking out KeepAlive into a new control channel file for easier testing

This commit is contained in:
Sarah Jamie Lewis 2018-01-13 11:43:49 -05:00
parent b378c4c825
commit 88d32191f7
3 changed files with 38 additions and 9 deletions

View File

@ -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))

View File

@ -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()

View File

@ -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"}