Browse Source

Merge branch 'test-fix-goroutine' of dan/cwtch into master

profile-password-2
Sarah Jamie Lewis Gogs 1 year ago
parent
commit
1edc952ea5
1 changed files with 50 additions and 31 deletions
  1. +50
    -31
      testing/cwtch_peer_server_intergration_test.go

+ 50
- 31
testing/cwtch_peer_server_intergration_test.go View File

@@ -7,8 +7,8 @@ import (
"cwtch.im/cwtch/peer/connections"
cwtchserver "cwtch.im/cwtch/server"
"fmt"
"github.com/s-rah/go-ricochet"
"github.com/s-rah/go-ricochet/utils"
"golang.org/x/net/proxy"
"io/ioutil"
"log"
"os"
@@ -28,26 +28,30 @@ var (
carolLines = []string{"Howdy, thanks!"}
)

func loadOrGenPrivateKey(t *testing.T) (pk *rsa.PrivateKey, generated bool) {
func loadPrivateKey(t *testing.T) *rsa.PrivateKey {
if _, err := os.Stat(serverKeyfile); os.IsNotExist(err) {
fmt.Println("generating new private key...")
pk, err := utils.GeneratePrivateKey()
if err != nil {
t.Fatalf("error generating new private key: %v\n", err)
}
err = ioutil.WriteFile(localKeyfile, []byte(utils.PrivateKeyToString(pk)), 0600)
if err != nil {
t.Fatalf("error writing new private key to file %s: %v\n", localKeyfile, err)
}

return pk, true
return nil
}

fmt.Println("Found server key " + serverKeyfile + ", loading...")
pk, err := utils.LoadPrivateKeyFromFile(serverKeyfile)
if err != nil {
t.Fatalf("Could not load server's key from %v", serverKeyfile)
}
return pk, false
return pk
}

func genPrivateKey(t *testing.T) *rsa.PrivateKey {
fmt.Println("generating new private key...")
pk, err := utils.GeneratePrivateKey()
if err != nil {
t.Fatalf("error generating new private key: %v\n", err)
}
err = ioutil.WriteFile(localKeyfile, []byte(utils.PrivateKeyToString(pk)), 0600)
if err != nil {
t.Fatalf("error writing new private key to file %s: %v\n", localKeyfile, err)
}
return pk
}

func printAndCountVerifedTimeline(t *testing.T, timeline []model.Message) int {
@@ -61,22 +65,33 @@ func printAndCountVerifedTimeline(t *testing.T, timeline []model.Message) int {
return numVerified
}

func serverCheck(serverAddr string) bool {
rc, err := goricochet.Open(serverAddr)
func serverCheck(t *testing.T, serverAddr string) bool {
torDialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct)
if err != nil {
t.Logf("Could not get SOCKS5 proxy: %v", err)
return false
}

if err == nil {
// TODO should change libricochet-go to avoid accessing member to shutdown all goroutines.
rc.Conn.Close()
return true
// Doesn't seem to be a way to turn the default timeout of 2 minutes down
conn, err := torDialer.Dial("tcp", serverAddr+".onion:9878")
if err != nil {
t.Logf("Could not dial %v: %v", serverAddr, err)
return false
}
return false

conn.Close()
return true
}

func waitForPeerConnection(t *testing.T, peer *peer.CwtchPeer, server string) {
for {
servers := peer.GetServers()
fmt.Println(servers)
state, ok := servers[server]
if ok {
if state == connections.FAILED {
t.Fatalf("%v could not connect to %v", peer.Profile.Onion, server)
}
if state != connections.AUTHENTICATED {
time.Sleep(time.Second * 10)
continue
@@ -96,19 +111,23 @@ func TestCwtchPeerIntegration(t *testing.T) {

// ***** Cwtch Server managment *****
var server *cwtchserver.Server = nil
serverKey, generatedKey := loadOrGenPrivateKey(t)
serverAddr, _ := utils.GetOnionAddress(serverKey)

serverKey := loadPrivateKey(t)
serverOnline := false
if !generatedKey {
var serverAddr string

if serverKey != nil {
serverAddr, _ = utils.GetOnionAddress(serverKey)
fmt.Printf("Checking if test server %v is online...\n", serverAddr)
serverOnline = serverCheck(serverAddr)
serverOnline = serverCheck(t, serverAddr)
}

if !serverOnline {
// launch app
// launch app with new key
fmt.Println("No server found!")
serverKey = genPrivateKey(t)
serverAddr, _ = utils.GetOnionAddress(serverKey)
server = new(cwtchserver.Server)
fmt.Printf("No server found\nStarting cwtch server...\n")
fmt.Println("Starting cwtch server...")
go server.Run(localKeyfile, 100)

// let tor get established
@@ -117,7 +136,6 @@ func TestCwtchPeerIntegration(t *testing.T) {
fmt.Printf("Found existing cwtch server %v, using for tests...\n", serverAddr)
}

time.Sleep(time.Second * 2)
numGoRoutinesPostServer := runtime.NumGoroutine()

// ***** Peer setup *****
@@ -366,7 +384,8 @@ func TestCwtchPeerIntegration(t *testing.T) {
numGoRoutinesStart, numGoRoutinesPostServer, numGoRoutinesPostPeerStart, numGoRoutinesPostServerConnect,
numGoRoutinesPostAlice, numGoRotinesPostCarolConnect, numGoRoutinesPostBob, numGoRoutinesPostServerShutdown, numGoRoutinesPostCarol)

if numGoRoutinesPostServer != numGoRoutinesPostCarol {
t.Errorf("Number of GoRoutines once server checks were completed (%v) does not match number of goRoutines after cleanup of peers and servers (%v), clean up failed, leak detected!", numGoRoutinesPostServer, numGoRoutinesPostCarol)
if numGoRoutinesStart != numGoRoutinesPostCarol {
t.Errorf("Number of GoRoutines at start (%v) does not match number of goRoutines after cleanup of peers and servers (%v), clean up failed, leak detected!", numGoRoutinesStart, numGoRoutinesPostCarol)
}

}

Loading…
Cancel
Save