diff --git a/connection/connection.go b/connection/connection.go index 27c5a77..3a38513 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -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() diff --git a/connection/features.go b/connection/features.go new file mode 100644 index 0000000..b4642c6 --- /dev/null +++ b/connection/features.go @@ -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 +} diff --git a/connection/features_test.go b/connection/features_test.go new file mode 100644 index 0000000..eee77cd --- /dev/null +++ b/connection/features_test.go @@ -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]) + } + +}