diff --git a/application/application.go b/application/application.go index 2ed4dd6..2a2c778 100644 --- a/application/application.go +++ b/application/application.go @@ -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() { diff --git a/connection/autoconnectionhandler.go b/connection/autoconnectionhandler.go index e3fc887..f8a7c3e 100644 --- a/connection/autoconnectionhandler.go +++ b/connection/autoconnectionhandler.go @@ -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 ... diff --git a/connection/connection.go b/connection/connection.go index bb323ef..1c846b1 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -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) { diff --git a/connection/handler.go b/connection/handler.go index e38c7f9..85f01f2 100644 --- a/connection/handler.go +++ b/connection/handler.go @@ -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 } diff --git a/inbound_version_negotiation_test.go b/inbound_version_negotiation_test.go index 37f0ec8..ccede67 100644 --- a/inbound_version_negotiation_test.go +++ b/inbound_version_negotiation_test.go @@ -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) { diff --git a/outbound_version_negotiation_test.go b/outbound_version_negotiation_test.go new file mode 100644 index 0000000..16d123c --- /dev/null +++ b/outbound_version_negotiation_test.go @@ -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) + } +} diff --git a/testing.md b/testing.md index 109b898..1c3e550 100644 --- a/testing.md +++ b/testing.md @@ -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. +