You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

163 lines
2.9 KiB

package goricochet
import (
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
"io"
"net"
"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, 0x03})
}
l, err := net.Listen("tcp", ":4000")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go connect()
conn, err := l.Accept()
if err != nil {
t.Errorf("Error setting up test: %v", err)
}
_, err = NegotiateVersionInbound(conn)
if err != nil {
t.Errorf("Expected Success Got %v", err)
}
}
func TestBadProtcolLength(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})
}
l, err := net.Listen("tcp", ":4000")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go connect()
conn, err := l.Accept()
if err != nil {
t.Errorf("Error setting up test: %v", err)
}
_, err = NegotiateVersionInbound(conn)
if err != io.ErrUnexpectedEOF {
t.Errorf("Invalid Error Received. Expected ErrUnexpectedEOF. Got %v", err)
}
}
func TestNoSupportedVersions(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, 0x00, 0xFF})
}
l, err := net.Listen("tcp", ":4000")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go connect()
conn, err := l.Accept()
if err != nil {
t.Errorf("Error setting up test: %v", err)
}
_, err = NegotiateVersionInbound(conn)
if err != utils.VersionNegotiationError {
t.Errorf("Invalid Error Received. Expected VersionNegotiationError. Got %v", err)
}
}
func TestInvalidVersionList(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})
}
l, err := net.Listen("tcp", ":4000")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go connect()
conn, err := l.Accept()
if err != nil {
t.Errorf("Error setting up test: %v", err)
}
_, err = NegotiateVersionInbound(conn)
if err != utils.VersionNegotiationError {
t.Errorf("Invalid Error Received. Expected VersionNegotiationError. Got %v", err)
}
}
func TestNoCompatibleVersions(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, 0xFF})
}
l, err := net.Listen("tcp", ":4000")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go connect()
conn, err := l.Accept()
if err != nil {
t.Errorf("Error setting up test: %v", err)
}
_, err = NegotiateVersionInbound(conn)
if err != utils.VersionNegotiationFailed {
t.Errorf("Invalid Error Received. Expected VersionNegotiationFailed. Got %v", err)
}
}