2017-05-02 23:33:51 +00:00
|
|
|
package goricochet
|
|
|
|
|
|
|
|
import (
|
2020-02-10 19:30:32 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/connectivity"
|
2017-07-04 18:29:11 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2017-05-02 23:33:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func SimpleServer() {
|
2017-07-04 18:29:11 +00:00
|
|
|
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 {
|
2019-01-23 19:57:43 +00:00
|
|
|
conn.Write([]byte{0x03})
|
2017-07-04 18:29:11 +00:00
|
|
|
}
|
|
|
|
conn.Close()
|
2017-05-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 18:11:56 +00:00
|
|
|
func TestRicochetOpen(t *testing.T) {
|
2020-02-10 19:30:32 +00:00
|
|
|
acn := connectivity.NewLocalACN()
|
2017-07-04 18:29:11 +00:00
|
|
|
go SimpleServer()
|
|
|
|
// Wait for Server to Initialize
|
|
|
|
time.Sleep(time.Second)
|
2017-05-02 23:33:51 +00:00
|
|
|
|
2018-11-22 06:15:35 +00:00
|
|
|
rc, err := Open(acn, "127.0.0.1:11000|abcdefghijklmno.onion")
|
2017-07-04 18:29:11 +00:00
|
|
|
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-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 18:11:56 +00:00
|
|
|
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})
|
2017-12-13 19:33:35 +00:00
|
|
|
}
|
2018-01-03 18:11:56 +00:00
|
|
|
conn.Close()
|
2017-12-13 19:33:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 18:11:56 +00:00
|
|
|
func TestRicochetOpenWithError(t *testing.T) {
|
2020-02-10 19:30:32 +00:00
|
|
|
acn := connectivity.NewLocalACN()
|
2018-01-03 18:11:56 +00:00
|
|
|
go BadServer()
|
|
|
|
// Wait for Server to Initialize
|
2017-07-04 18:29:11 +00:00
|
|
|
time.Sleep(time.Second)
|
2018-11-22 06:15:35 +00:00
|
|
|
_, err := Open(acn, "127.0.0.1:11001|abcdefghijklmno.onion")
|
2018-01-03 18:11:56 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Open should have failed because of bad version negotiation.")
|
2017-07-04 18:29:11 +00:00
|
|
|
}
|
2017-05-02 23:33:51 +00:00
|
|
|
}
|
2018-01-03 18:20:53 +00:00
|
|
|
|
|
|
|
func TestRicochetOpenWithNoServer(t *testing.T) {
|
2020-02-10 19:30:32 +00:00
|
|
|
acn := connectivity.NewLocalACN()
|
2018-11-22 06:15:35 +00:00
|
|
|
_, err := Open(acn, "127.0.0.1:11002|abcdefghijklmno.onion")
|
2018-01-03 18:20:53 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Open should have failed because of bad version negotiation.")
|
|
|
|
}
|
|
|
|
}
|