2020-06-29 20:21:21 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2020-10-13 18:10:45 +00:00
|
|
|
"math/rand"
|
2020-06-29 20:21:21 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2020-10-15 22:35:41 +00:00
|
|
|
"time"
|
2020-06-29 20:21:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLaunchTor(t *testing.T) {
|
|
|
|
log.SetLevel(log.LevelDebug)
|
2020-10-13 18:10:45 +00:00
|
|
|
|
2020-10-15 22:35:41 +00:00
|
|
|
rand.Seed(int64(time.Now().Nanosecond()))
|
2020-10-13 18:10:45 +00:00
|
|
|
controlPort := rand.Intn(1000) + 9052
|
|
|
|
password := "examplehashedpassword"
|
|
|
|
|
2020-06-29 20:25:26 +00:00
|
|
|
// Create the tor data directory if it doesn't already exist..
|
|
|
|
os.MkdirAll("../tmp/data/tor", 0700)
|
2020-10-15 22:35:41 +00:00
|
|
|
err := tor.NewTorrc().WithControlPort(controlPort).WithHashedPassword(password).Build("../tmp/data/tor/torrc")
|
2020-06-29 20:21:21 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create torrc file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current working director, clean the paths to remove relative references
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
t.Logf("Launching bundled tor at %v", path.Clean(wd+"/../tmp/tor"))
|
2020-10-13 18:10:45 +00:00
|
|
|
acn, err := tor.NewTorACNWithAuth(path.Clean(wd+"/../tmp/data"), path.Clean(wd+"/../tmp/tor"), controlPort, tor.HashedPasswordAuthenticator{Password: password})
|
2020-06-29 20:21:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tor failed to start: %v", err)
|
2020-10-15 22:35:41 +00:00
|
|
|
} else {
|
|
|
|
acn.WaitTillBootstrapped()
|
|
|
|
if pid, err := acn.GetPID(); err == nil {
|
|
|
|
t.Logf("tor pid: %v", pid)
|
|
|
|
} else {
|
|
|
|
t.Fatalf("error fetching pid: %v", err)
|
|
|
|
}
|
|
|
|
t.Log("we have bootstrapped!")
|
|
|
|
acn.Close()
|
2020-06-29 20:21:21 +00:00
|
|
|
}
|
|
|
|
}
|