2020-02-07 22:15:37 +00:00
|
|
|
package tor
|
2020-02-06 00:42:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-15 22:35:41 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2022-01-17 23:54:42 +00:00
|
|
|
"io/ioutil"
|
2022-04-18 21:49:24 +00:00
|
|
|
"os"
|
2022-01-10 20:51:21 +00:00
|
|
|
path "path/filepath"
|
2022-04-18 21:49:24 +00:00
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
2020-02-06 00:42:32 +00:00
|
|
|
"testing"
|
2022-04-18 21:49:24 +00:00
|
|
|
"time"
|
2020-02-06 00:42:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getStatusCallback(progChan chan int) func(int, string) {
|
|
|
|
return func(prog int, status string) {
|
|
|
|
fmt.Printf("%v %v\n", prog, status)
|
|
|
|
progChan <- prog
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTorProvider(t *testing.T) {
|
2022-04-18 21:49:24 +00:00
|
|
|
|
|
|
|
goRoutineStart := runtime.NumGoroutine()
|
|
|
|
|
|
|
|
progChan := make(chan int, 10)
|
2020-10-15 22:35:41 +00:00
|
|
|
log.SetLevel(log.LevelDebug)
|
|
|
|
torpath := path.Join("..", "tmp/tor")
|
2022-01-10 20:51:21 +00:00
|
|
|
|
|
|
|
NewTorrc().WithControlPort(9051).WithHashedPassword("examplehashedpassword").Build(path.Join("..", "testing", "tor", "torrc"))
|
|
|
|
|
2020-10-15 22:35:41 +00:00
|
|
|
log.Debugf("setting tor path %v", torpath)
|
2022-01-17 23:54:42 +00:00
|
|
|
|
|
|
|
dataDir := ""
|
|
|
|
var err error
|
2022-01-18 00:03:06 +00:00
|
|
|
if dataDir, err = ioutil.TempDir(path.Join("..", "testing"), "data-dir-"); err != nil {
|
2022-01-17 23:54:42 +00:00
|
|
|
t.Fatalf("could not create data dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
acn, err := NewTorACNWithAuth(path.Join("../testing/"), torpath, dataDir, 9051, HashedPasswordAuthenticator{"examplehashedpassword"})
|
2020-02-06 00:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
acn.SetStatusCallback(getStatusCallback(progChan))
|
|
|
|
|
|
|
|
progress := 0
|
|
|
|
for progress < 100 {
|
|
|
|
progress = <-progChan
|
2020-10-15 22:35:41 +00:00
|
|
|
t.Logf("progress: %v", progress)
|
2020-02-06 00:42:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 18:53:44 +00:00
|
|
|
acn.Restart()
|
|
|
|
|
|
|
|
progress = 0
|
|
|
|
for progress < 100 {
|
|
|
|
progress = <-progChan
|
|
|
|
t.Logf("progress: %v", progress)
|
|
|
|
}
|
|
|
|
|
2022-01-13 21:43:27 +00:00
|
|
|
// Test opening the OP Server
|
|
|
|
_, _, err = acn.Open("isbr2t6bflul2zyi6hjtnuezb2xvfr42svzjg2q3gyqfgg3wmnrbkkqd")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
info, err := acn.GetInfo("isbr2t6bflul2zyi6hjtnuezb2xvfr42svzjg2q3gyqfgg3wmnrbkkqd")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not find info for OP server %v", err)
|
|
|
|
}
|
|
|
|
cinfo, exists := info["circuit"]
|
2022-01-13 21:49:54 +00:00
|
|
|
if !exists || len(cinfo) == 0 {
|
2022-01-13 21:43:27 +00:00
|
|
|
t.Fatalf("could not find circuit info for OP server %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = acn.GetInfo("not_a_real_onion")
|
|
|
|
if err == nil {
|
2022-01-17 23:54:42 +00:00
|
|
|
t.Fatalf("GetInfo for non existent onion should have errored")
|
2022-01-13 21:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
t.Fatalf("could not connect to OP server %v", err)
|
|
|
|
}
|
|
|
|
|
2021-04-13 20:39:33 +00:00
|
|
|
// Should skip without blocking...
|
|
|
|
acn.Restart()
|
|
|
|
acn.Restart()
|
|
|
|
acn.Restart()
|
|
|
|
|
2020-02-06 00:42:32 +00:00
|
|
|
acn.Close()
|
2022-04-18 21:49:24 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
|
|
|
|
goRoutineEnd := runtime.NumGoroutine()
|
|
|
|
|
|
|
|
if goRoutineEnd != goRoutineStart {
|
|
|
|
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
|
|
|
|
|
|
|
t.Fatalf("goroutine leak in ACN: %v %v", goRoutineStart, goRoutineEnd)
|
|
|
|
}
|
|
|
|
|
2020-02-06 00:42:32 +00:00
|
|
|
}
|