Moving FeaturesEnabled logic to another file, adding tests

This commit is contained in:
Sarah Jamie Lewis 2018-01-12 13:31:47 -05:00
parent 39cf4d3871
commit bf19d1b20c
3 changed files with 63 additions and 13 deletions

View File

@ -460,19 +460,8 @@ func (rc *Connection) controlPacket(handler Handler, res *Protocol_Data_Control.
}
} else if res.GetEnableFeatures() != nil {
rc.traceLog("received enable features packet")
featuresToEnable := res.GetEnableFeatures().GetFeature()
supportChannels := handler.GetSupportedChannelTypes()
result := []string{}
for _, v := range featuresToEnable {
for _, s := range supportChannels {
if v == s {
result = append(result, v)
}
}
}
messageBuilder := new(utils.MessageBuilder)
raw := messageBuilder.FeaturesEnabled(result)
rc.traceLog(fmt.Sprintf("sending featured enabled: %v", result))
raw := ProcessEnableFeatures(handler, res.GetEnableFeatures())
rc.traceLog(fmt.Sprintf("sending featured enabled: %v", raw))
rc.SendRicochetPacket(rc.Conn, 0, raw)
} else if res.GetFeaturesEnabled() != nil {
rc.SupportChannels = res.GetFeaturesEnabled().GetFeature()

23
connection/features.go Normal file
View File

@ -0,0 +1,23 @@
package connection
import (
"github.com/s-rah/go-ricochet/utils"
"github.com/s-rah/go-ricochet/wire/control"
)
// ProcessEnableFeatures correctly handles a features enabled packet
func ProcessEnableFeatures(handler Handler, ef *Protocol_Data_Control.EnableFeatures) []byte {
featuresToEnable := ef.GetFeature()
supportChannels := handler.GetSupportedChannelTypes()
result := []string{}
for _, v := range featuresToEnable {
for _, s := range supportChannels {
if v == s {
result = append(result, v)
}
}
}
messageBuilder := new(utils.MessageBuilder)
raw := messageBuilder.FeaturesEnabled(result)
return raw
}

View File

@ -0,0 +1,38 @@
package connection
import (
"github.com/golang/protobuf/proto"
"github.com/s-rah/go-ricochet/wire/control"
"testing"
)
type MockHandler struct {
AutoConnectionHandler
}
func (m *MockHandler) GetSupportedChannelTypes() []string {
return []string{"im.ricochet.chat"}
}
func TestEnableFeatures(t *testing.T) {
handler := new(MockHandler)
features := []string{"feature1", "im.ricochet.chat"}
ef := &Protocol_Data_Control.EnableFeatures{
Feature: features,
}
raw := ProcessEnableFeatures(handler, ef)
res := new(Protocol_Data_Control.Packet)
err := proto.Unmarshal(raw, res)
if err != nil || res.GetFeaturesEnabled() == nil {
t.Errorf("Decoding FeaturesEnabled Packet failed: %v %v", err, res)
}
if len(res.GetFeaturesEnabled().GetFeature()) != 1 {
t.Errorf("Decoding FeaturesEnabled Errored, unexpected length %v", res.GetFeaturesEnabled().GetFeature())
}
if res.GetFeaturesEnabled().GetFeature()[0] != "im.ricochet.chat" {
t.Errorf("Decoding FeaturesEnabled Errored, unexpected feature enabled %v ", res.GetFeaturesEnabled().GetFeature()[0])
}
}