Breaking out KeepAlive into a new control channel file for easier testing
This commit is contained in:
parent
b378c4c825
commit
88d32191f7
|
@ -390,7 +390,8 @@ func (rc *Connection) controlPacket(handler Handler, res *Protocol_Data_Control.
|
||||||
// Enforce Authentication Check.
|
// Enforce Authentication Check.
|
||||||
_, authed := rc.Authentication[chandler.RequiresAuthentication()]
|
_, authed := rc.Authentication[chandler.RequiresAuthentication()]
|
||||||
if !authed {
|
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()))
|
rc.traceLog(fmt.Sprintf("do not have required authorization to open channel type %v", chandler.Type()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -444,7 +445,7 @@ func (rc *Connection) controlPacket(handler Handler, res *Protocol_Data_Control.
|
||||||
channel.Handler.OpenOutboundResult(nil, cr)
|
channel.Handler.OpenOutboundResult(nil, cr)
|
||||||
} else {
|
} else {
|
||||||
rc.traceLog(fmt.Sprintf("channel of type %v rejected on %v", channel.Type, id))
|
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 {
|
} 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
|
// We should likely put these calls behind
|
||||||
// authentication.
|
// authentication.
|
||||||
rc.traceLog("received keep alive packet")
|
rc.traceLog("received keep alive packet")
|
||||||
if res.GetKeepAlive().GetResponseRequested() {
|
respond, data := ProcessKeepAlive(res.GetKeepAlive())
|
||||||
messageBuilder := new(utils.MessageBuilder)
|
if respond {
|
||||||
raw := messageBuilder.KeepAlive(true)
|
|
||||||
rc.traceLog("sending keep alive response")
|
rc.traceLog("sending keep alive response")
|
||||||
rc.SendRicochetPacket(rc.Conn, 0, raw)
|
rc.SendRicochetPacket(rc.Conn, 0, data)
|
||||||
}
|
}
|
||||||
} else if res.GetEnableFeatures() != nil {
|
} else if res.GetEnableFeatures() != nil {
|
||||||
rc.traceLog("received enable features packet")
|
rc.traceLog("received enable features packet")
|
||||||
raw := ProcessEnableFeatures(handler, res.GetEnableFeatures())
|
data := ProcessEnableFeatures(handler, res.GetEnableFeatures())
|
||||||
rc.traceLog(fmt.Sprintf("sending featured enabled: %v", raw))
|
rc.traceLog(fmt.Sprintf("sending featured enabled: %v", data))
|
||||||
rc.SendRicochetPacket(rc.Conn, 0, raw)
|
rc.SendRicochetPacket(rc.Conn, 0, data)
|
||||||
} else if res.GetFeaturesEnabled() != nil {
|
} else if res.GetFeaturesEnabled() != nil {
|
||||||
rc.SupportChannels = res.GetFeaturesEnabled().GetFeature()
|
rc.SupportChannels = res.GetFeaturesEnabled().GetFeature()
|
||||||
rc.traceLog(fmt.Sprintf("connection supports: %v", rc.SupportChannels))
|
rc.traceLog(fmt.Sprintf("connection supports: %v", rc.SupportChannels))
|
||||||
|
|
|
@ -5,6 +5,15 @@ import (
|
||||||
"github.com/s-rah/go-ricochet/wire/control"
|
"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
|
// ProcessEnableFeatures correctly handles a features enabled packet
|
||||||
func ProcessEnableFeatures(handler Handler, ef *Protocol_Data_Control.EnableFeatures) []byte {
|
func ProcessEnableFeatures(handler Handler, ef *Protocol_Data_Control.EnableFeatures) []byte {
|
||||||
featuresToEnable := ef.GetFeature()
|
featuresToEnable := ef.GetFeature()
|
|
@ -14,6 +14,26 @@ func (m *MockHandler) GetSupportedChannelTypes() []string {
|
||||||
return []string{"im.ricochet.chat"}
|
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) {
|
func TestEnableFeatures(t *testing.T) {
|
||||||
handler := new(MockHandler)
|
handler := new(MockHandler)
|
||||||
features := []string{"feature1", "im.ricochet.chat"}
|
features := []string{"feature1", "im.ricochet.chat"}
|
Reference in New Issue