Adding successful version negotiation tests.

This change also simplifies ricochet_test.go to only exercise the Open()
function.
This commit is contained in:
Sarah Jamie Lewis 2018-01-03 10:11:56 -08:00
parent 049a0ea15f
commit 84d7336602
4 changed files with 98 additions and 62 deletions

View File

@ -7,6 +7,32 @@ import (
"testing" "testing"
) )
func TestNegotiateInboundVersions(t *testing.T) {
connect := func() {
conn, err := net.Dial("tcp", ":4000")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
conn.Write([]byte{0x49, 0x4D, 0x01, 0x01})
}
l, err := net.Listen("tcp", ":4000")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go connect()
conn, err := l.Accept()
_, err = NegotiateVersionInbound(conn)
if err != nil {
t.Errorf("Expected Success Got %v", err)
}
}
func TestBadProtcolLength(t *testing.T) { func TestBadProtcolLength(t *testing.T) {
connect := func() { connect := func() {

View File

@ -7,9 +7,53 @@ import (
"time" "time"
) )
func TestOutboundVersionNegotiation(t *testing.T) {
go func() {
ln, _ := net.Listen("tcp", "127.0.0.1:12001")
conn, _ := ln.Accept()
b := make([]byte, 4)
n, err := conn.Read(b)
if n == 4 && err == nil {
conn.Write([]byte{0x01})
}
conn.Close()
}()
time.Sleep(time.Second * 1)
conn, err := net.Dial("tcp", ":12001")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
_, err = NegotiateVersionOutbound(conn, "")
if err != nil {
t.Errorf("Expected success got %v", err)
}
}
func TestInvalidServer(t *testing.T) {
go func() {
ln, _ := net.Listen("tcp", "127.0.0.1:12002")
conn, _ := ln.Accept()
b := make([]byte, 4)
conn.Read(b)
conn.Write([]byte{})
conn.Close()
}()
time.Sleep(time.Second * 1)
conn, err := net.Dial("tcp", ":12002")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
_, err = NegotiateVersionOutbound(conn, "")
if err != utils.VersionNegotiationError {
t.Errorf("Expected VersionNegotiationError got %v", err)
}
}
func TestInvalidResponse(t *testing.T) { func TestInvalidResponse(t *testing.T) {
go func() { go func() {
ln, _ := net.Listen("tcp", "127.0.0.1:12000") ln, _ := net.Listen("tcp", "127.0.0.1:12003")
conn, _ := ln.Accept() conn, _ := ln.Accept()
b := make([]byte, 4) b := make([]byte, 4)
n, err := conn.Read(b) n, err := conn.Read(b)
@ -19,7 +63,7 @@ func TestInvalidResponse(t *testing.T) {
conn.Close() conn.Close()
}() }()
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
conn, err := net.Dial("tcp", ":12000") conn, err := net.Dial("tcp", ":12003")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -1,7 +1,6 @@
package goricochet package goricochet
import ( import (
"github.com/s-rah/go-ricochet/utils"
"net" "net"
"testing" "testing"
"time" "time"
@ -18,29 +17,7 @@ func SimpleServer() {
conn.Close() conn.Close()
} }
func BadVersionNegotiation() { func TestRicochetOpen(t *testing.T) {
ln, _ := net.Listen("tcp", "127.0.0.1:11001")
conn, _ := ln.Accept()
// We are already testing negotiation bytes, we don't care, just send a termination.
conn.Write([]byte{0x00})
conn.Close()
}
func NotRicochetServer() {
ln, _ := net.Listen("tcp", "127.0.0.1:11002")
conn, _ := ln.Accept()
conn.Close()
}
func RicochetServer() error {
ln, _ := net.Listen("tcp", "127.0.0.1:11003")
conn, _ := ln.Accept()
_, err := NegotiateVersionInbound(conn)
conn.Close()
return err
}
func TestRicochet(t *testing.T) {
go SimpleServer() go SimpleServer()
// Wait for Server to Initialize // Wait for Server to Initialize
time.Sleep(time.Second) time.Sleep(time.Second)
@ -55,37 +32,23 @@ func TestRicochet(t *testing.T) {
t.Errorf("RicochetProtocol: Open Failed: %v", err) t.Errorf("RicochetProtocol: Open Failed: %v", err)
} }
func TestNegotiateInbound(t *testing.T) { func BadServer() {
go func() { ln, _ := net.Listen("tcp", "127.0.0.1:11001")
err := RicochetServer() conn, _ := ln.Accept()
if err != nil { b := make([]byte, 4)
t.Errorf("RicochetProtocol: Inbound Negotiation Test Should have Succeed: %v", err) n, err := conn.Read(b)
} if n == 4 && err == nil {
}() conn.Write([]byte{0xFF})
time.Sleep(time.Second)
_, err := Open("127.0.0.1:11003|abcdefghijklmno.onion")
if err != nil {
t.Errorf("RicochetProtocol: Inbound Negotiation Test Should have Succeed: %v", err)
} }
conn.Close()
} }
func TestBadVersionNegotiation(t *testing.T) { func TestRicochetOpenWithError(t *testing.T) {
go BadVersionNegotiation() go BadServer()
// Wait for Server to Initialize
time.Sleep(time.Second) time.Sleep(time.Second)
_, err := Open("127.0.0.1:11001|abcdefghijklmno.onion") _, err := Open("127.0.0.1:11001|abcdefghijklmno.onion")
if err != utils.VersionNegotiationFailed { if err == nil {
t.Errorf("RicochetProtocol: Server Had No Correct Version - Should Have Failed: err = %v", err) t.Errorf("Open should have failed because of bad version negotiation.")
}
}
func TestNotARicochetServer(t *testing.T) {
go NotRicochetServer()
time.Sleep(time.Second)
_, err := Open("127.0.0.1:11002|abcdefghijklmno.onion")
if err != utils.VersionNegotiationError {
t.Errorf("RicochetProtocol: Server Had No Correct Version - Should Have Failed: err = %v", err)
} }
} }

View File

@ -3,12 +3,16 @@
This documents outlines each scenario this library must correctly handle and This documents outlines each scenario this library must correctly handle and
links it to the automated test that exercises that functionality. links it to the automated test that exercises that functionality.
We separate this document into two sections, one for inbound connections, and the
other for outbound connections.
# Inbound # Version Negotiation
## Version Negotiation ## Open
File: [iricochet_test.go](./ricochet_test.go)
This stub test exercises the Open() function. `TestRicochetOpen`, `TestRicochetOpenWithError`.
## Inbound
File: [inbound_version_negotiation_test.go](./inbound_version_negotiation_test.go) File: [inbound_version_negotiation_test.go](./inbound_version_negotiation_test.go)
@ -30,20 +34,19 @@ supported versions. Then is must close the connection. `TestNoCompatibleVersions
### Successful Version Negotiation ### Successful Version Negotiation
Assuming the inbound listener receives a valid protocol message, and that message Assuming the inbound listener receives a valid protocol message, and that message
contains a known supported version. Then the connection should remain open. contains a known supported version. Then the connection should remain open. `TestNegotiateInboundVersions`
# Outbound ## Outbound
File: [outbound_version_negotiation_test.go](./outbound_version_negotiation_test.go) File: [outbound_version_negotiation_test.go](./outbound_version_negotiation_test.go)
### No Compatible Version Found ### No Compatible Version Found
If the outbound connection receives a response that does not match one of the versions 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` they sent out in their supporting list. Then then must close the connection `TestInvalidResponse` , `TestInvalidServer`
### Successful Version Negotiation ### Successful Version Negotiation
Assuming the outbound connection receives a valid protocol message, and that message Assuming the outbound connection receives a valid protocol message, and that message
contains a known supported version. Then the connection should remain open. contains a known supported version. Then the connection should remain open. `TestOutboundVersionNegotiation`