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

View File

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

View File

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

View File

@ -1,10 +1,10 @@
package goricochet
import (
"github.com/s-rah/go-ricochet/utils"
"io"
"net"
"testing"
"github.com/s-rah/go-ricochet/utils"
)
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
File: [ricochet_test.go](./inbound_version_negotiation_test.go)
File: [inbound_version_negotiation_test.go](./inbound_version_negotiation_test.go)
### 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.
# 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.