Stubbing OutboundVersionNegotiationTest

Actually committing enable features work!
This commit is contained in:
Sarah Jamie Lewis 2018-01-02 09:23:20 -08:00
parent 6d449e230f
commit 049a0ea15f
7 changed files with 78 additions and 33 deletions

View File

@ -8,8 +8,8 @@ import (
"github.com/s-rah/go-ricochet/identity" "github.com/s-rah/go-ricochet/identity"
"log" "log"
"net" "net"
"time"
"sync" "sync"
"time"
) )
// RicochetApplication bundles many useful constructs that are // RicochetApplication bundles many useful constructs that are
@ -19,8 +19,8 @@ type RicochetApplication struct {
privateKey *rsa.PrivateKey privateKey *rsa.PrivateKey
chatMessageHandler func(*RicochetApplicationInstance, uint32, time.Time, string) chatMessageHandler func(*RicochetApplicationInstance, uint32, time.Time, string)
chatMessageAckHandler func(*RicochetApplicationInstance, uint32) chatMessageAckHandler func(*RicochetApplicationInstance, uint32)
onConnected func(*RicochetApplicationInstance) onConnected func(*RicochetApplicationInstance)
onLeave func(*RicochetApplicationInstance) onLeave func(*RicochetApplicationInstance)
l net.Listener l net.Listener
instances []*RicochetApplicationInstance instances []*RicochetApplicationInstance
lock sync.Mutex lock sync.Mutex
@ -32,7 +32,7 @@ type RicochetApplicationInstance struct {
RemoteHostname string RemoteHostname string
ChatMessageHandler func(*RicochetApplicationInstance, uint32, time.Time, string) ChatMessageHandler func(*RicochetApplicationInstance, uint32, time.Time, string)
ChatMessageAckHandler func(*RicochetApplicationInstance, uint32) ChatMessageAckHandler func(*RicochetApplicationInstance, uint32)
OnLeave func(*RicochetApplicationInstance) OnLeave func(*RicochetApplicationInstance)
} }
func (rai *RicochetApplicationInstance) ContactRequest(name string, message string) string { func (rai *RicochetApplicationInstance) ContactRequest(name string, message string) string {
@ -113,7 +113,7 @@ func (ra *RicochetApplication) handleConnection(conn net.Conn) {
conn.Close() conn.Close()
return return
} }
rc.TraceLog(true) rc.TraceLog(true)
rai := new(RicochetApplicationInstance) rai := new(RicochetApplicationInstance)
rai.Init() rai.Init()
rai.RemoteHostname = rc.RemoteHostname rai.RemoteHostname = rc.RemoteHostname
@ -139,16 +139,16 @@ func (ra *RicochetApplication) handleConnection(conn net.Conn) {
rc.Process(rai) rc.Process(rai)
} }
func (rai *RicochetApplicationInstance) OnClosed(err error) { func (rai *RicochetApplicationInstance) OnClosed(err error) {
rai.OnLeave(rai) rai.OnLeave(rai)
} }
func (ra *RicochetApplication) Broadcast(message string) { func (ra *RicochetApplication) Broadcast(message string) {
ra.lock.Lock() ra.lock.Lock()
for _,rai := range ra.instances { for _, rai := range ra.instances {
rai.SendChatMessage(message) rai.SendChatMessage(message)
} }
ra.lock.Unlock() ra.lock.Unlock()
} }
func (ra *RicochetApplication) Shutdown() { func (ra *RicochetApplication) Shutdown() {

View File

@ -35,11 +35,11 @@ func (ach *AutoConnectionHandler) OnClosed(err error) {
} }
func (ach *AutoConnectionHandler) GetSupportedChannelTypes() []string { func (ach *AutoConnectionHandler) GetSupportedChannelTypes() []string {
supported := []string{} supported := []string{}
for k,_ := range ach.handlerMap { for k := range ach.handlerMap {
supported = append(supported, k) supported = append(supported, k)
} }
return supported return supported
} }
// RegisterChannelHandler ... // RegisterChannelHandler ...

View File

@ -40,10 +40,10 @@ type Connection struct {
// it for anything else. See those functions for an explanation. // it for anything else. See those functions for an explanation.
processBlockMutex sync.Mutex processBlockMutex sync.Mutex
Conn io.ReadWriteCloser Conn io.ReadWriteCloser
IsInbound bool IsInbound bool
Authentication map[string]bool Authentication map[string]bool
RemoteHostname string RemoteHostname string
SupportChannels []string SupportChannels []string
} }
@ -463,27 +463,27 @@ func (rc *Connection) controlPacket(handler Handler, res *Protocol_Data_Control.
featuresToEnable := res.GetEnableFeatures().GetFeature() featuresToEnable := res.GetEnableFeatures().GetFeature()
supportChannels := handler.GetSupportedChannelTypes() supportChannels := handler.GetSupportedChannelTypes()
result := []string{} result := []string{}
for _,v := range featuresToEnable { for _, v := range featuresToEnable {
for _,s := range supportChannels { for _, s := range supportChannels {
if v == s { if v == s {
result = append(result, v) result = append(result, v)
} }
} }
} }
messageBuilder := new(utils.MessageBuilder) messageBuilder := new(utils.MessageBuilder)
raw := messageBuilder.FeaturesEnabled(result) raw := messageBuilder.FeaturesEnabled(result)
rc.traceLog(fmt.Sprintf("sending featured enabled: %v", result)) rc.traceLog(fmt.Sprintf("sending featured enabled: %v", result))
rc.SendRicochetPacket(rc.Conn, 0, raw) rc.SendRicochetPacket(rc.Conn, 0, raw)
} 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))
} }
} }
func (rc *Connection) EnableFeatures(features []string) { func (rc *Connection) EnableFeatures(features []string) {
messageBuilder := new(utils.MessageBuilder) messageBuilder := new(utils.MessageBuilder)
raw := messageBuilder.EnableFeatures(features) raw := messageBuilder.EnableFeatures(features)
rc.SendRicochetPacket(rc.Conn, 0, raw) rc.SendRicochetPacket(rc.Conn, 0, raw)
} }
func (rc *Connection) traceLog(message string) { func (rc *Connection) traceLog(message string) {

View File

@ -25,6 +25,6 @@ type Handler interface {
// A non-nil return from this function does not guarantee that the channel // A non-nil return from this function does not guarantee that the channel
// will be opened. // will be opened.
OnOpenChannelRequest(ctype string) (channels.Handler, error) OnOpenChannelRequest(ctype string) (channels.Handler, error)
GetSupportedChannelTypes() []string GetSupportedChannelTypes() []string
} }

View File

@ -1,10 +1,10 @@
package goricochet package goricochet
import ( import (
"github.com/s-rah/go-ricochet/utils"
"io" "io"
"net" "net"
"testing" "testing"
"github.com/s-rah/go-ricochet/utils"
) )
func TestBadProtcolLength(t *testing.T) { func TestBadProtcolLength(t *testing.T) {

View File

@ -0,0 +1,31 @@
package goricochet
import (
"github.com/s-rah/go-ricochet/utils"
"net"
"testing"
"time"
)
func TestInvalidResponse(t *testing.T) {
go func() {
ln, _ := net.Listen("tcp", "127.0.0.1:12000")
conn, _ := ln.Accept()
b := make([]byte, 4)
n, err := conn.Read(b)
if n == 4 && err == nil {
conn.Write([]byte{0xFF})
}
conn.Close()
}()
time.Sleep(time.Second * 1)
conn, err := net.Dial("tcp", ":12000")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
_, err = NegotiateVersionOutbound(conn, "")
if err != utils.VersionNegotiationFailed {
t.Errorf("Expected VersionNegotiationFailed got %v", err)
}
}

View File

@ -10,7 +10,7 @@ other for outbound connections.
## Version Negotiation ## Version Negotiation
File: [ricochet_test.go](./inbound_version_negotiation_test.go) File: [inbound_version_negotiation_test.go](./inbound_version_negotiation_test.go)
### Invalid Protocol ### Invalid Protocol
@ -33,3 +33,17 @@ Assuming the inbound listener receives a valid protocol message, and that messag
contains a known supported version. Then the connection should remain open. contains a known supported version. Then the connection should remain open.
# Outbound # Outbound
File: [outbound_version_negotiation_test.go](./outbound_version_negotiation_test.go)
### No Compatible Version Found
If the outbound connection receives a response that does not match one of the versions
they sent out in their supporting list. Then then must close the connection `TestInvalidResponse`
### Successful Version Negotiation
Assuming the outbound connection receives a valid protocol message, and that message
contains a known supported version. Then the connection should remain open.