go-ricochet/ricochet_test.go

69 lines
1.6 KiB
Go
Raw Permalink Normal View History

2017-05-03 01:33:51 +02:00
package goricochet
import (
"github.com/s-rah/go-ricochet/utils"
"net"
"testing"
"time"
2017-05-03 01:33:51 +02:00
)
func SimpleServer() {
ln, _ := net.Listen("tcp", "127.0.0.1:11000")
conn, _ := ln.Accept()
b := make([]byte, 4)
n, err := conn.Read(b)
if n == 4 && err == nil {
conn.Write([]byte{0x01})
}
conn.Close()
2017-05-03 01:33:51 +02:00
}
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()
2017-05-03 01:33:51 +02:00
}
func NotRicochetServer() {
ln, _ := net.Listen("tcp", "127.0.0.1:11002")
conn, _ := ln.Accept()
conn.Close()
2017-05-03 01:33:51 +02:00
}
func TestRicochet(t *testing.T) {
go SimpleServer()
// Wait for Server to Initialize
time.Sleep(time.Second)
2017-05-03 01:33:51 +02:00
rc, err := Open("127.0.0.1:11000|abcdefghijklmno.onion")
if err == nil {
if rc.IsInbound {
t.Errorf("RicochetConnection declares itself as an Inbound connection after an Outbound attempt...that shouldn't happen")
}
return
}
t.Errorf("RicochetProtocol: Open Failed: %v", err)
2017-05-03 01:33:51 +02:00
}
func TestBadVersionNegotiation(t *testing.T) {
go BadVersionNegotiation()
time.Sleep(time.Second)
2017-05-03 01:33:51 +02:00
_, 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)
}
2017-05-03 01:33:51 +02:00
}
func TestNotARicochetServer(t *testing.T) {
go NotRicochetServer()
time.Sleep(time.Second)
2017-05-03 01:33:51 +02:00
_, 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)
}
2017-05-03 01:33:51 +02:00
}