tapir/testing/tapir_malicious_remote_inte...

102 lines
3.1 KiB
Go
Raw Normal View History

package testing
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"
torProvider "git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log"
"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"
"sync"
"testing"
"time"
)
func TestTapirMaliciousRemote(t *testing.T) {
numRoutinesStart := runtime.NumGoroutine()
log.SetLevel(log.LevelDebug)
log.Infof("Number of goroutines open at start: %d", runtime.NumGoroutine())
// 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)
builder.WithHashedPassword("tapir-integration-test").Build("./tor/torrc")
2020-06-29 21:32:38 +00:00
// Connect to Tor
2022-01-24 20:28:55 +00:00
torDataDir := ""
var err error
if torDataDir, err = ioutil.TempDir("./tor/", "data-dir-"); err != nil {
t.Fatalf("could not create data dir")
}
// Connect to Tor
acn, err := torProvider.NewTorACNWithAuth("./", "", torDataDir, 9051, 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)
}
acn.WaitTillBootstrapped()
// Generate Server Keys, not we generate two sets
id, _ := primitives.InitializeEphemeralIdentity()
id2, sk2 := primitives.InitializeEphemeralIdentity()
// Init the Server running the Simple App.
2021-06-09 17:36:34 +00:00
service := new(tor.BaseOnionService)
// Initialize an onion service with one identity, but the auth app with another, this should
// trigger a failure in authentication protocol
service.Init(acn, sk2, &id)
// Goroutine Management
sg := new(sync.WaitGroup)
sg.Add(1)
go func() {
2019-09-14 23:44:19 +00:00
service.Listen(new(applications.AuthApp))
sg.Done()
}()
// Wait for server to come online
time.Sleep(time.Second * 30)
wg := new(sync.WaitGroup)
wg.Add(1)
// Init a Client to Connect to the Server
log.Infof("initializing the client....")
client, _ := genclient(acn)
go connectclientandfail(client, id2.PublicKey(), wg, t)
wg.Wait()
// Wait for Server to Sync
time.Sleep(time.Second * 2)
log.Infof("closing ACN...")
client.Shutdown()
service.Shutdown()
acn.Close()
sg.Wait()
2020-07-14 21:59:08 +00:00
time.Sleep(time.Second * 5) // wait for goroutines to finish...
log.Infof("Number of goroutines open at close: %d", runtime.NumGoroutine())
if numRoutinesStart != runtime.NumGoroutine() {
t.Errorf("Potential goroutine leak: Num Start:%v NumEnd: %v", numRoutinesStart, runtime.NumGoroutine())
}
}
// Client will Connect and launch it's own Echo App goroutine.
func connectclientandfail(client tapir.Service, key ed25519.PublicKey, group *sync.WaitGroup, t *testing.T) {
client.Connect(torProvider.GetTorV3Hostname(key), new(applications.AuthApp))
// 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)
log.Infof("Checking connection status...")
conn, err := client.GetConnection(torProvider.GetTorV3Hostname(key))
if err == nil {
group.Done()
2021-06-09 17:36:34 +00:00
t.Errorf("Connection should have failed! %v %v", conn, err)
}
log.Infof("Successfully failed to authenticate...")
group.Done()
}