2019-08-12 20:04:39 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
2021-04-09 00:55:17 +00:00
|
|
|
"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"
|
2020-02-06 23:54:13 +00:00
|
|
|
torProvider "git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2019-08-12 20:04:39 +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"
|
2019-08-12 20:04:39 +00:00
|
|
|
"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)
|
|
|
|
}
|
2019-08-12 20:04:39 +00:00
|
|
|
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)
|
2019-08-12 20:04:39 +00:00
|
|
|
// 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))
|
2019-08-12 20:04:39 +00:00
|
|
|
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...")
|
|
|
|
acn.Close()
|
|
|
|
sg.Wait()
|
2020-07-14 21:59:08 +00:00
|
|
|
time.Sleep(time.Second * 5) // wait for goroutines to finish...
|
2019-08-12 20:04:39 +00:00
|
|
|
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) {
|
2020-02-06 23:54:13 +00:00
|
|
|
client.Connect(torProvider.GetTorV3Hostname(key), new(applications.AuthApp))
|
2019-08-12 20:04:39 +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)
|
|
|
|
|
|
|
|
log.Infof("Checking connection status...")
|
2020-02-06 23:54:13 +00:00
|
|
|
conn, err := client.GetConnection(torProvider.GetTorV3Hostname(key))
|
2019-08-12 20:04:39 +00:00
|
|
|
if err == nil {
|
|
|
|
group.Done()
|
2021-06-09 17:36:34 +00:00
|
|
|
t.Errorf("Connection should have failed! %v %v", conn, err)
|
2019-08-12 20:04:39 +00:00
|
|
|
}
|
|
|
|
log.Infof("Successfully failed to authenticate...")
|
|
|
|
group.Done()
|
|
|
|
}
|