tapir/testing/tapir_integration_test.go

156 lines
4.4 KiB
Go
Raw Normal View History

package testing
2019-05-15 19:45:45 +00:00
import (
"git.openprivacy.ca/cwtch.im/tapir"
"git.openprivacy.ca/cwtch.im/tapir/applications"
"git.openprivacy.ca/cwtch.im/tapir/networks/tor"
"git.openprivacy.ca/cwtch.im/tapir/primitives"
"git.openprivacy.ca/openprivacy/connectivity"
torProvider "git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log"
2019-05-15 19:45:45 +00:00
"golang.org/x/crypto/ed25519"
2022-01-24 20:28:55 +00:00
"io/ioutil"
2020-06-29 21:32:38 +00:00
"os"
"runtime"
"runtime/pprof"
"sync"
"testing"
2019-05-15 19:45:45 +00:00
"time"
)
2019-05-21 18:28:10 +00:00
// SimpleApp is a trivial implementation of a basic p2p application
2019-05-15 19:45:45 +00:00
type SimpleApp struct {
2019-07-16 19:14:42 +00:00
applications.AuthApp
2019-05-15 19:45:45 +00:00
}
// NewInstance should always return a new instantiation of the application.
2019-09-14 23:44:19 +00:00
func (ea *SimpleApp) NewInstance() tapir.Application {
2019-05-15 19:45:45 +00:00
return new(SimpleApp)
}
// Init is run when the connection is first started.
2019-09-14 23:44:19 +00:00
func (ea *SimpleApp) Init(connection tapir.Connection) {
2019-05-15 19:45:45 +00:00
// First run the Authentication App
ea.AuthApp.Init(connection)
2019-07-16 19:14:42 +00:00
if connection.HasCapability(applications.AuthCapability) {
2019-05-15 19:45:45 +00:00
// The code for out simple application (We just send and receive "Hello"
connection.Send([]byte("Hello"))
message := connection.Expect()
log.Infof("Received: %q", message)
}
}
var AuthSuccess = false
2019-07-16 20:22:30 +00:00
// CheckConnection is a simple test that GetConnection is working.
func CheckConnection(service tapir.Service, hostname string, group *sync.WaitGroup) {
2019-07-16 20:22:30 +00:00
for {
_, err := service.GetConnection(hostname)
if err == nil {
log.Infof("Authed!")
group.Done()
2019-07-16 20:22:30 +00:00
return
}
log.Infof("Waiting for Authentication...%v", err)
time.Sleep(time.Second * 5)
2019-07-16 20:22:30 +00:00
}
}
func TestTapir(t *testing.T) {
2019-05-15 19:45:45 +00:00
numRoutinesStart := runtime.NumGoroutine()
2019-05-15 19:45:45 +00:00
log.SetLevel(log.LevelDebug)
log.Infof("Number of goroutines open at start: %d", runtime.NumGoroutine())
2019-05-15 19:45:45 +00:00
// Connect to Tor
2020-06-29 21:32:38 +00:00
os.MkdirAll("./tor/", 0700)
2021-04-09 00:14:41 +00:00
builder := new(torProvider.TorrcBuilder)
2022-01-24 20:28:55 +00:00
builder.WithSocksPort(9059).WithControlPort(9060).WithHashedPassword("tapir-integration-test").Build("./tor/torrc")
torDataDir := ""
var err error
if torDataDir, err = ioutil.TempDir("./tor/", "data-dir-"); err != nil {
t.Fatalf("could not create data dir")
}
2020-06-29 21:32:38 +00:00
// Connect to Tor
2022-01-24 20:28:55 +00:00
acn, err := torProvider.NewTorACNWithAuth("./", "", torDataDir, 9060, torProvider.HashedPasswordAuthenticator{Password: "tapir-integration-test"})
2020-06-29 21:32:38 +00:00
if err != nil {
t.Fatalf("could not launch ACN %v", err)
}
2019-05-15 19:45:45 +00:00
acn.WaitTillBootstrapped()
// Generate Server Keys
2019-08-08 19:07:13 +00:00
id, sk := primitives.InitializeEphemeralIdentity()
2019-05-15 19:45:45 +00:00
2019-06-05 18:44:26 +00:00
// Init the Server running the Simple App.
2021-06-09 17:36:34 +00:00
service := new(tor.BaseOnionService)
service.Init(acn, sk, &id)
// Goroutine Management
sg := new(sync.WaitGroup)
sg.Add(1)
go func() {
2019-09-14 23:44:19 +00:00
service.Listen(new(SimpleApp))
sg.Done()
}()
// Wait for server to come online
time.Sleep(time.Second * 30)
wg := new(sync.WaitGroup)
wg.Add(2)
// Init a Client to Connect to the Server
client, clienthostname := genclient(acn)
2022-01-24 20:28:55 +00:00
go connectclient(t, client, id.PublicKey(), wg)
CheckConnection(service, clienthostname, wg)
wg.Wait()
// Wait for Garbage Collection...
time.Sleep(time.Second * 60)
log.Infof("Closing ACN...")
client.Shutdown()
service.Shutdown()
acn.Close()
sg.Wait()
time.Sleep(time.Second * 5)
log.Infof("Number of goroutines open at close: %d", runtime.NumGoroutine())
if numRoutinesStart != runtime.NumGoroutine() {
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
t.Errorf("Potential goroutine leak: Num Start:%v NumEnd: %v", numRoutinesStart, runtime.NumGoroutine())
}
if !AuthSuccess {
t.Fatalf("Integration Test FAILED, client did not auth with server")
}
2020-06-29 21:32:38 +00:00
2019-05-15 19:45:45 +00:00
}
2019-07-16 20:22:30 +00:00
func genclient(acn connectivity.ACN) (tapir.Service, string) {
2019-08-08 19:07:13 +00:00
id, sk := primitives.InitializeEphemeralIdentity()
2021-06-09 17:36:34 +00:00
client := new(tor.BaseOnionService)
client.Init(acn, sk, &id)
return client, id.Hostname()
2019-07-16 20:22:30 +00:00
}
// Client will Connect and launch it's own Echo App goroutine.
2022-01-24 20:28:55 +00:00
func connectclient(t *testing.T, client tapir.Service, key ed25519.PublicKey, group *sync.WaitGroup) {
client.Connect(torProvider.GetTorV3Hostname(key), new(SimpleApp))
2019-05-15 19:45:45 +00:00
// Once connected, it shouldn't take long to authenticate and run the application. So for the purposes of this demo
// we will wait a little while then exit.
time.Sleep(time.Second * 5)
2019-05-21 18:28:10 +00:00
conn, _ := client.GetConnection(torProvider.GetTorV3Hostname(key))
2019-07-16 19:14:42 +00:00
log.Debugf("Client has Auth: %v", conn.HasCapability(applications.AuthCapability))
2022-01-24 20:28:55 +00:00
// attempt to send a message that is too long
var long [8195]byte
err := conn.Send(long[:])
if err == nil {
t.Errorf("should have errored on message being too long...")
}
AuthSuccess = true
group.Done()
2019-05-15 19:45:45 +00:00
}