diff --git a/inbound_version_negotiation_test.go b/inbound_version_negotiation_test.go index ccede67..8040d22 100644 --- a/inbound_version_negotiation_test.go +++ b/inbound_version_negotiation_test.go @@ -7,6 +7,32 @@ import ( "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) { connect := func() { diff --git a/outbound_version_negotiation_test.go b/outbound_version_negotiation_test.go index 16d123c..8ed1357 100644 --- a/outbound_version_negotiation_test.go +++ b/outbound_version_negotiation_test.go @@ -7,9 +7,53 @@ import ( "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) { go func() { - ln, _ := net.Listen("tcp", "127.0.0.1:12000") + ln, _ := net.Listen("tcp", "127.0.0.1:12003") conn, _ := ln.Accept() b := make([]byte, 4) n, err := conn.Read(b) @@ -19,7 +63,7 @@ func TestInvalidResponse(t *testing.T) { conn.Close() }() time.Sleep(time.Second * 1) - conn, err := net.Dial("tcp", ":12000") + conn, err := net.Dial("tcp", ":12003") if err != nil { t.Fatal(err) } diff --git a/ricochet_test.go b/ricochet_test.go index aa638b8..dcce6dc 100644 --- a/ricochet_test.go +++ b/ricochet_test.go @@ -1,7 +1,6 @@ package goricochet import ( - "github.com/s-rah/go-ricochet/utils" "net" "testing" "time" @@ -18,29 +17,7 @@ func SimpleServer() { conn.Close() } -func BadVersionNegotiation() { - 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) { +func TestRicochetOpen(t *testing.T) { go SimpleServer() // Wait for Server to Initialize time.Sleep(time.Second) @@ -55,37 +32,23 @@ func TestRicochet(t *testing.T) { t.Errorf("RicochetProtocol: Open Failed: %v", err) } -func TestNegotiateInbound(t *testing.T) { - go func() { - err := RicochetServer() - if err != nil { - t.Errorf("RicochetProtocol: Inbound Negotiation Test Should have Succeed: %v", err) - } - }() - - 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) +func BadServer() { + ln, _ := net.Listen("tcp", "127.0.0.1:11001") + conn, _ := ln.Accept() + b := make([]byte, 4) + n, err := conn.Read(b) + if n == 4 && err == nil { + conn.Write([]byte{0xFF}) } + conn.Close() } -func TestBadVersionNegotiation(t *testing.T) { - go BadVersionNegotiation() +func TestRicochetOpenWithError(t *testing.T) { + go BadServer() + // Wait for Server to Initialize time.Sleep(time.Second) - _, err := Open("127.0.0.1:11001|abcdefghijklmno.onion") - if err != utils.VersionNegotiationFailed { - t.Errorf("RicochetProtocol: Server Had No Correct Version - Should Have Failed: err = %v", err) - } -} - -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) + if err == nil { + t.Errorf("Open should have failed because of bad version negotiation.") } } diff --git a/testing.md b/testing.md index 1c3e550..db403eb 100644 --- a/testing.md +++ b/testing.md @@ -3,12 +3,16 @@ This documents outlines each scenario this library must correctly handle and 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) @@ -30,20 +34,19 @@ supported versions. Then is must close the connection. `TestNoCompatibleVersions ### Successful Version Negotiation 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) ### 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` - +they sent out in their supporting list. Then then must close the connection `TestInvalidResponse` , `TestInvalidServer` ### 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. +contains a known supported version. Then the connection should remain open. `TestOutboundVersionNegotiation`