120 lines
3.8 KiB
Go
120 lines
3.8 KiB
Go
package managed_groups
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"cwtch.im/cwtch/app"
|
|
"cwtch.im/cwtch/app/utils"
|
|
"cwtch.im/cwtch/functionality/groupmanagement"
|
|
"cwtch.im/cwtch/model/constants"
|
|
"encoding/base64"
|
|
"git.openprivacy.ca/openprivacy/connectivity"
|
|
"git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
"git.openprivacy.ca/openprivacy/log"
|
|
"io/ioutil"
|
|
mrand "math/rand"
|
|
"os"
|
|
"os/user"
|
|
path "path/filepath"
|
|
"runtime"
|
|
"runtime/pprof"
|
|
"testing"
|
|
"time"
|
|
|
|
_ "github.com/mutecomm/go-sqlcipher/v4"
|
|
)
|
|
|
|
func setupACN(t *testing.T) connectivity.ACN {
|
|
os.Mkdir("tordir", 0700)
|
|
dataDir := path.Join("tordir", "tor")
|
|
os.MkdirAll(dataDir, 0700)
|
|
|
|
// we don't need real randomness for the port, just to avoid a possible conflict...
|
|
mrand.Seed(int64(time.Now().Nanosecond()))
|
|
socksPort := mrand.Intn(1000) + 9051
|
|
controlPort := mrand.Intn(1000) + 9052
|
|
|
|
// generate a random password
|
|
key := make([]byte, 64)
|
|
_, err := rand.Read(key)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
torDataDir := ""
|
|
if torDataDir, err = ioutil.TempDir(dataDir, "data-dir-"); err != nil {
|
|
t.Fatalf("could not create data dir")
|
|
}
|
|
|
|
tor.NewTorrc().WithSocksPort(socksPort).WithOnionTrafficOnly().WithHashedPassword(base64.StdEncoding.EncodeToString(key)).WithControlPort(controlPort).Build("tordir/tor/torrc")
|
|
acn, err := tor.NewTorACNWithAuth("./tordir", path.Join("..", "..", "tor"), torDataDir, controlPort, tor.HashedPasswordAuthenticator{Password: base64.StdEncoding.EncodeToString(key)})
|
|
if err != nil {
|
|
t.Fatalf("Could not start Tor: %v", err)
|
|
}
|
|
acn.WaitTillBootstrapped()
|
|
return acn
|
|
}
|
|
|
|
func TestManagedGroups(t *testing.T) {
|
|
|
|
os.RemoveAll("./storage")
|
|
os.RemoveAll("./tordir")
|
|
|
|
numGoRoutinesStart := runtime.NumGoroutine()
|
|
log.SetLevel(log.LevelDebug)
|
|
acn := setupACN(t)
|
|
defer acn.Close()
|
|
|
|
const ServerKeyBundleBase64 = "eyJLZXlzIjp7ImJ1bGxldGluX2JvYXJkX29uaW9uIjoibmZoeHp2enhpbnJpcGdkaDR0Mm00eGN5M2NyZjZwNGNiaGVjdGdja3VqM2lkc2pzYW90Z293YWQiLCJwcml2YWN5X3Bhc3NfcHVibGljX2tleSI6IjVwd2hQRGJ0c0EvdFI3ZHlUVUkzakpZZnM1L3Jaai9iQ1ZWZEpTc0Jtbk09IiwidG9rZW5fc2VydmljZV9vbmlvbiI6ImVvd25mcTRsNTZxMmU0NWs0bW03MjdsanJod3Z0aDZ5ZWN0dWV1bXB4emJ5cWxnbXVhZm1qdXFkIn0sIlNpZ25hdHVyZSI6IlY5R3NPMHNZWFJ1bGZxdzdmbGdtclVxSTBXS0JlSFIzNjIvR3hGbWZPekpEZjJaRks2ck9jNVRRR1ZxVWIrbXIwV2xId0pwdXh0UW1JRU9KNkplYkNRPT0ifQ=="
|
|
const ServerAddr = "nfhxzvzxinripgdh4t2m4xcy3crf6p4cbhectgckuj3idsjsaotgowad"
|
|
serverKeyBundle, _ := base64.StdEncoding.DecodeString(ServerKeyBundleBase64)
|
|
|
|
app := app.NewApp(acn, "./storage")
|
|
|
|
usr, _ := user.Current()
|
|
cwtchDir := path.Join(usr.HomeDir, ".cwtch")
|
|
os.Mkdir(cwtchDir, 0700)
|
|
os.RemoveAll(path.Join(cwtchDir, "testing"))
|
|
os.Mkdir(path.Join(cwtchDir, "testing"), 0700)
|
|
|
|
t.Logf("Creating Alice...")
|
|
app.CreateTaggedPeer("alice", "asdfasdf", "testing")
|
|
|
|
t.Logf("** Waiting for Alice...")
|
|
alice := utils.WaitGetPeer(app, "alice")
|
|
t.Logf("Have Alice: %v", alice.GetOnion())
|
|
|
|
// import server key bundle
|
|
_, err := alice.AddServer(string(serverKeyBundle))
|
|
if err != nil {
|
|
t.Fatalf("could not import server key bundle")
|
|
}
|
|
|
|
// establish new group management functionality
|
|
gmf, err := groupmanagement.FunctionalityGate(map[string]bool{constants.GroupManagementExperiment: true})
|
|
if err != nil {
|
|
t.Fatalf("could not instantiate group management functionality")
|
|
}
|
|
|
|
// create a new managed group bound to the test server
|
|
gid, err := gmf.CreateManagedGroup(alice, "Test Managed Group", ServerAddr)
|
|
if err != nil {
|
|
t.Fatalf("could not create managed group")
|
|
}
|
|
|
|
t.Logf("created new test group: %v", gid)
|
|
|
|
// Shutdown automatically closes Alice...
|
|
app.Shutdown()
|
|
acn.Close()
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
numGoRoutinesEnd := runtime.NumGoroutine()
|
|
if numGoRoutinesStart != numGoRoutinesEnd {
|
|
// Printing out the current goroutines
|
|
// Very useful if we are leaking any.
|
|
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
|
t.Fatalf("goroutine leak detected: %v %v", numGoRoutinesStart, numGoRoutinesEnd)
|
|
}
|
|
}
|