2018-03-31 19:33:32 +00:00
package testing
2018-03-30 21:16:51 +00:00
import (
2020-10-13 18:53:18 +00:00
"crypto/rand"
2019-05-15 20:12:11 +00:00
app2 "cwtch.im/cwtch/app"
2019-06-21 21:50:43 +00:00
"cwtch.im/cwtch/app/utils"
2019-09-19 23:14:35 +00:00
"cwtch.im/cwtch/event"
2019-06-21 21:50:43 +00:00
"cwtch.im/cwtch/event/bridge"
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/model"
2020-03-07 07:41:00 +00:00
"cwtch.im/cwtch/model/attr"
2021-10-15 19:38:22 +00:00
"cwtch.im/cwtch/model/constants"
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/peer"
2019-01-04 21:44:21 +00:00
"cwtch.im/cwtch/protocol/connections"
2020-10-13 18:53:18 +00:00
"encoding/base64"
2018-05-20 19:58:16 +00:00
"fmt"
2020-02-10 22:09:24 +00:00
"git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log"
2020-10-13 18:53:18 +00:00
mrand "math/rand"
2018-11-10 22:14:12 +00:00
"os"
2020-02-03 18:46:15 +00:00
"os/user"
"path"
2018-05-30 18:42:17 +00:00
"runtime"
2019-01-04 21:44:21 +00:00
"runtime/pprof"
2021-05-03 23:32:48 +00:00
"strings"
2018-05-20 19:58:16 +00:00
"testing"
"time"
2018-03-30 21:16:51 +00:00
)
2018-04-27 19:20:33 +00:00
var (
2018-06-05 22:06:38 +00:00
aliceLines = [ ] string { "Hello, I'm Alice" , "bye" }
bobLines = [ ] string { "Hi, my name is Bob." , "toodles" , "welcome" }
carolLines = [ ] string { "Howdy, thanks!" }
2018-04-27 19:20:33 +00:00
)
2018-06-05 22:06:38 +00:00
func printAndCountVerifedTimeline ( t * testing . T , timeline [ ] model . Message ) int {
numVerified := 0
2018-05-20 19:58:16 +00:00
for _ , message := range timeline {
2018-10-05 03:18:34 +00:00
fmt . Printf ( "%v %v> %s\n" , message . Timestamp , message . PeerID , message . Message )
numVerified ++
2018-04-27 19:20:33 +00:00
}
2018-06-05 22:06:38 +00:00
return numVerified
2018-04-27 19:20:33 +00:00
}
2019-05-15 20:12:11 +00:00
func waitForPeerGroupConnection ( t * testing . T , peer peer . CwtchPeer , groupID string ) {
2021-10-15 19:38:22 +00:00
peerName , _ := peer . GetScopedZonedAttribute ( attr . LocalScope , attr . ProfileZone , constants . Name )
2018-06-03 19:36:20 +00:00
for {
2021-05-03 23:32:48 +00:00
fmt . Printf ( "%v checking group connection...\n" , peerName )
2020-02-03 18:46:15 +00:00
state , ok := peer . GetGroupState ( groupID )
2018-06-03 19:36:20 +00:00
if ok {
2021-05-03 23:32:48 +00:00
fmt . Printf ( "Waiting for Peer %v to join group %v - state: %v\n" , peerName , groupID , state )
2018-06-14 00:42:08 +00:00
if state == connections . FAILED {
2020-02-03 18:46:15 +00:00
t . Fatalf ( "%v could not connect to %v" , peer . GetOnion ( ) , groupID )
2018-06-14 00:42:08 +00:00
}
2019-07-19 17:27:50 +00:00
if state != connections . SYNCED {
2021-05-03 23:32:48 +00:00
fmt . Printf ( "peer %v %v waiting connect to group %v, currently: %v\n" , peerName , peer . GetOnion ( ) , groupID , connections . ConnectionStateName [ state ] )
2019-01-04 21:44:21 +00:00
time . Sleep ( time . Second * 5 )
2018-06-03 19:36:20 +00:00
continue
2019-01-04 21:44:21 +00:00
} else {
2021-05-03 23:32:48 +00:00
fmt . Printf ( "peer %v %v CONNECTED to group %v\n" , peerName , peer . GetOnion ( ) , groupID )
2019-01-04 21:44:21 +00:00
break
2018-06-03 19:36:20 +00:00
}
2020-02-03 18:46:15 +00:00
}
2021-05-03 23:32:48 +00:00
time . Sleep ( time . Second * 2 )
2018-06-03 19:36:20 +00:00
}
return
}
2018-11-10 22:14:12 +00:00
func waitForPeerPeerConnection ( t * testing . T , peera peer . CwtchPeer , peerb peer . CwtchPeer ) {
for {
2020-02-03 18:46:15 +00:00
state , ok := peera . GetPeerState ( peerb . GetOnion ( ) )
2018-11-10 22:14:12 +00:00
if ok {
2019-05-15 20:12:11 +00:00
//log.Infof("Waiting for Peer %v to peer with peer: %v - state: %v\n", peera.GetProfile().Name, peerb.GetProfile().Name, state)
2018-11-10 22:14:12 +00:00
if state == connections . FAILED {
2020-02-03 18:46:15 +00:00
t . Fatalf ( "%v could not connect to %v" , peera . GetOnion ( ) , peerb . GetOnion ( ) )
2018-11-10 22:14:12 +00:00
}
if state != connections . AUTHENTICATED {
2020-02-03 18:46:15 +00:00
fmt . Printf ( "peer %v waiting connect to peer %v, currently: %v\n" , peera . GetOnion ( ) , peerb . GetOnion ( ) , connections . ConnectionStateName [ state ] )
2019-01-04 21:44:21 +00:00
time . Sleep ( time . Second * 5 )
2018-11-10 22:14:12 +00:00
continue
2019-01-04 21:44:21 +00:00
} else {
2021-10-15 19:38:22 +00:00
peerAName , _ := peera . GetScopedZonedAttribute ( attr . LocalScope , attr . ProfileZone , constants . Name )
peerBName , _ := peerb . GetScopedZonedAttribute ( attr . LocalScope , attr . ProfileZone , constants . Name )
2021-05-03 23:32:48 +00:00
fmt . Printf ( "%v CONNECTED and AUTHED to %v\n" , peerAName , peerBName )
2019-01-04 21:44:21 +00:00
break
2018-11-10 22:14:12 +00:00
}
2020-02-03 18:46:15 +00:00
}
2018-11-10 22:14:12 +00:00
}
return
}
2018-03-30 21:16:51 +00:00
func TestCwtchPeerIntegration ( t * testing . T ) {
2018-05-30 18:42:17 +00:00
numGoRoutinesStart := runtime . NumGoroutine ( )
2018-04-27 19:20:33 +00:00
2019-02-04 22:18:24 +00:00
log . AddEverythingFromPattern ( "connectivity" )
2019-06-21 21:50:43 +00:00
log . SetLevel ( log . LevelDebug )
log . ExcludeFromPattern ( "connection/connection" )
log . ExcludeFromPattern ( "outbound/3dhauthchannel" )
log . ExcludeFromPattern ( "event/eventmanager" )
2019-07-19 17:27:50 +00:00
log . ExcludeFromPattern ( "pipeBridge" )
2020-02-03 18:46:15 +00:00
log . ExcludeFromPattern ( "tapir" )
2020-10-29 21:22:33 +00:00
os . Mkdir ( "tordir" , 0700 )
2020-10-13 18:53:18 +00:00
dataDir := path . Join ( "tordir" , "tor" )
2020-09-21 22:39:15 +00:00
os . MkdirAll ( dataDir , 0700 )
2020-10-13 18:53:18 +00:00
// we don't need real randomness for the port, just to avoid a possible conflict...
mrand . Seed ( int64 ( time . Now ( ) . Nanosecond ( ) ) )
2020-10-29 21:22:33 +00:00
socksPort := mrand . Intn ( 1000 ) + 9051
controlPort := mrand . Intn ( 1000 ) + 9052
2020-10-13 18:53:18 +00:00
// generate a random password
key := make ( [ ] byte , 64 )
_ , err := rand . Read ( key )
if err != nil {
panic ( err )
}
tor . NewTorrc ( ) . WithSocksPort ( socksPort ) . WithOnionTrafficOnly ( ) . WithHashedPassword ( base64 . StdEncoding . EncodeToString ( key ) ) . WithControlPort ( controlPort ) . Build ( "tordir/tor/torrc" )
acn , err := tor . NewTorACNWithAuth ( "./tordir" , path . Join ( ".." , "tor" ) , controlPort , tor . HashedPasswordAuthenticator { Password : base64 . StdEncoding . EncodeToString ( key ) } )
2018-11-10 22:14:12 +00:00
if err != nil {
t . Fatalf ( "Could not start Tor: %v" , err )
}
2020-11-02 23:53:13 +00:00
pid , _ := acn . GetPID ( )
2020-10-16 17:52:16 +00:00
t . Logf ( "Tor pid: %v" , pid )
2018-11-10 22:14:12 +00:00
2019-01-28 20:09:25 +00:00
// ***** Cwtch Server management *****
2021-06-29 21:38:53 +00:00
const ServerKeyBundleBase64 = "eyJLZXlzIjp7ImJ1bGxldGluX2JvYXJkX29uaW9uIjoibmZoeHp2enhpbnJpcGdkaDR0Mm00eGN5M2NyZjZwNGNiaGVjdGdja3VqM2lkc2pzYW90Z293YWQiLCJwcml2YWN5X3Bhc3NfcHVibGljX2tleSI6IjVwd2hQRGJ0c0EvdFI3ZHlUVUkzakpZZnM1L3Jaai9iQ1ZWZEpTc0Jtbk09IiwidG9rZW5fc2VydmljZV9vbmlvbiI6ImVvd25mcTRsNTZxMmU0NWs0bW03MjdsanJod3Z0aDZ5ZWN0dWV1bXB4emJ5cWxnbXVhZm1qdXFkIn0sIlNpZ25hdHVyZSI6IlY5R3NPMHNZWFJ1bGZxdzdmbGdtclVxSTBXS0JlSFIzNjIvR3hGbWZPekpEZjJaRks2ck9jNVRRR1ZxVWIrbXIwV2xId0pwdXh0UW1JRU9KNkplYkNRPT0ifQ=="
const ServerAddr = "nfhxzvzxinripgdh4t2m4xcy3crf6p4cbhectgckuj3idsjsaotgowad"
serverKeyBundle , _ := base64 . StdEncoding . DecodeString ( ServerKeyBundleBase64 )
2018-04-27 19:20:33 +00:00
2019-05-15 20:12:11 +00:00
app := app2 . NewApp ( acn , "./storage" )
2020-02-03 18:46:15 +00:00
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 )
bridgeClient := bridge . NewPipeBridgeClient ( path . Join ( cwtchDir , "testing/clientPipe" ) , path . Join ( cwtchDir , "testing/servicePipe" ) )
bridgeService := bridge . NewPipeBridgeService ( path . Join ( cwtchDir , "testing/servicePipe" ) , path . Join ( cwtchDir , "testing/clientPipe" ) )
2019-07-19 17:27:50 +00:00
appClient := app2 . NewAppClient ( "./storage" , bridgeClient )
appService := app2 . NewAppService ( acn , "./storage" , bridgeService )
2019-06-05 20:40:55 +00:00
numGoRoutinesPostAppStart := runtime . NumGoroutine ( )
2018-05-28 18:36:04 +00:00
2019-06-05 20:40:55 +00:00
// ***** cwtchPeer setup *****
2019-01-04 21:44:21 +00:00
2018-04-27 19:20:33 +00:00
fmt . Println ( "Creating Alice..." )
2019-06-05 20:40:55 +00:00
app . CreatePeer ( "alice" , "asdfasdf" )
2018-04-27 19:20:33 +00:00
2018-05-28 18:36:04 +00:00
fmt . Println ( "Creating Bob..." )
2019-06-05 20:40:55 +00:00
app . CreatePeer ( "bob" , "asdfasdf" )
2018-05-28 18:36:04 +00:00
2018-06-05 22:06:38 +00:00
fmt . Println ( "Creating Carol..." )
2019-06-05 20:40:55 +00:00
appClient . CreatePeer ( "carol" , "asdfasdf" )
2019-06-21 21:50:43 +00:00
alice := utils . WaitGetPeer ( app , "alice" )
2020-02-03 18:46:15 +00:00
fmt . Println ( "Alice created:" , alice . GetOnion ( ) )
2021-10-15 19:38:22 +00:00
alice . SetScopedZonedAttribute ( attr . PublicScope , attr . ProfileZone , constants . Name , "Alice" )
2020-04-17 00:00:17 +00:00
alice . AutoHandleEvents ( [ ] event . Type { event . PeerStateChange , event . ServerStateChange , event . NewGroupInvite , event . NewRetValMessageFromPeer } )
2019-06-05 20:40:55 +00:00
2019-06-21 21:50:43 +00:00
bob := utils . WaitGetPeer ( app , "bob" )
2020-02-03 18:46:15 +00:00
fmt . Println ( "Bob created:" , bob . GetOnion ( ) )
2021-10-15 19:38:22 +00:00
bob . SetScopedZonedAttribute ( attr . PublicScope , attr . ProfileZone , constants . Name , "Bob" )
2020-04-17 00:00:17 +00:00
bob . AutoHandleEvents ( [ ] event . Type { event . PeerStateChange , event . ServerStateChange , event . NewGroupInvite , event . NewRetValMessageFromPeer } )
2019-06-05 20:40:55 +00:00
2019-06-21 21:50:43 +00:00
carol := utils . WaitGetPeer ( appClient , "carol" )
2020-02-03 18:46:15 +00:00
fmt . Println ( "Carol created:" , carol . GetOnion ( ) )
2021-10-15 19:38:22 +00:00
carol . SetScopedZonedAttribute ( attr . PublicScope , attr . ProfileZone , constants . Name , "Carol" )
2020-04-17 00:00:17 +00:00
carol . AutoHandleEvents ( [ ] event . Type { event . PeerStateChange , event . ServerStateChange , event . NewGroupInvite , event . NewRetValMessageFromPeer } )
2018-06-05 22:06:38 +00:00
2019-05-15 20:12:11 +00:00
app . LaunchPeers ( )
2019-06-05 20:40:55 +00:00
appClient . LaunchPeers ( )
2019-05-15 20:12:11 +00:00
2020-06-16 00:16:04 +00:00
waitTime := time . Duration ( 60 ) * time . Second
t . Logf ( "** Waiting for Alice, Bob, and Carol to connect with onion network... (%v)\n" , waitTime )
time . Sleep ( waitTime )
2018-05-30 18:42:17 +00:00
numGoRoutinesPostPeerStart := runtime . NumGoroutine ( )
2020-06-16 00:16:04 +00:00
fmt . Println ( "** Wait Done!" )
2018-05-28 18:36:04 +00:00
2018-06-05 22:06:38 +00:00
// ***** Peering, server joining, group creation / invite *****
2018-05-28 18:36:04 +00:00
2020-02-03 18:46:15 +00:00
fmt . Println ( "Alice joining server..." )
2021-11-05 22:16:47 +00:00
if _ , err := alice . AddServer ( string ( serverKeyBundle ) ) ; err != nil {
2020-10-01 17:13:45 +00:00
t . Fatalf ( "Failed to Add Server Bundle %v" , err )
}
2021-06-29 21:38:53 +00:00
alice . JoinServer ( ServerAddr )
2020-02-03 18:46:15 +00:00
fmt . Println ( "Alice peering with Bob..." )
alice . PeerWithOnion ( bob . GetOnion ( ) )
fmt . Println ( "Alice peering with Carol..." )
alice . PeerWithOnion ( carol . GetOnion ( ) )
2021-06-29 21:38:53 +00:00
fmt . Println ( "Creating group on " , ServerAddr , "..." )
groupID , _ , err := alice . StartGroup ( ServerAddr )
2018-06-15 16:21:07 +00:00
fmt . Printf ( "Created group: %v!\n" , groupID )
2018-04-27 19:20:33 +00:00
if err != nil {
2018-06-05 22:06:38 +00:00
t . Errorf ( "Failed to init group: %v" , err )
2018-04-27 19:20:33 +00:00
return
}
2018-11-10 22:14:12 +00:00
fmt . Println ( "Waiting for alice to join server..." )
2019-05-15 20:12:11 +00:00
waitForPeerGroupConnection ( t , alice , groupID )
2018-11-10 22:14:12 +00:00
fmt . Println ( "Waiting for alice and Bob to peer..." )
waitForPeerPeerConnection ( t , alice , bob )
2020-06-16 00:16:04 +00:00
// Need to add contact else SetContactAuth fails on peer peer doesnt exist
// Normal flow would be Bob app monitors for the new connection (a new connection state change to Auth
// and the adds the user to peer, and then approves or blocks it
bob . AddContact ( "alice?" , alice . GetOnion ( ) , model . AuthApproved )
2020-07-14 00:46:05 +00:00
bob . AddServer ( string ( serverKeyBundle ) )
2020-06-16 00:16:04 +00:00
bob . SetContactAuthorization ( alice . GetOnion ( ) , model . AuthApproved )
2020-03-07 07:41:00 +00:00
waitForPeerPeerConnection ( t , alice , carol )
2020-06-16 00:16:04 +00:00
carol . AddContact ( "alice?" , alice . GetOnion ( ) , model . AuthApproved )
2020-07-14 00:46:05 +00:00
carol . AddServer ( string ( serverKeyBundle ) )
2020-06-16 00:16:04 +00:00
carol . SetContactAuthorization ( alice . GetOnion ( ) , model . AuthApproved )
2020-03-07 07:41:00 +00:00
fmt . Println ( "Alice and Bob getVal public.name..." )
2021-10-15 19:38:22 +00:00
alice . SendScopedZonedGetValToContact ( bob . GetOnion ( ) , attr . PublicScope , attr . ProfileZone , constants . Name )
bob . SendScopedZonedGetValToContact ( alice . GetOnion ( ) , attr . PublicScope , attr . ProfileZone , constants . Name )
2020-03-07 07:41:00 +00:00
2021-10-15 19:38:22 +00:00
alice . SendScopedZonedGetValToContact ( carol . GetOnion ( ) , attr . PublicScope , attr . ProfileZone , constants . Name )
carol . SendScopedZonedGetValToContact ( alice . GetOnion ( ) , attr . PublicScope , attr . ProfileZone , constants . Name )
2020-03-07 07:41:00 +00:00
2021-10-30 00:44:16 +00:00
// This used to be 10, but increasing it to 30 because this is now causing frequent issues
// Probably related to latency/throughput problems in the underlying tor network.
time . Sleep ( 30 * time . Second )
2020-03-07 07:41:00 +00:00
2021-10-15 19:38:22 +00:00
aliceName , exists := bob . GetContactAttribute ( alice . GetOnion ( ) , attr . GetPeerScope ( constants . Name ) )
2020-03-07 07:41:00 +00:00
if ! exists || aliceName != "Alice" {
2020-06-16 00:16:04 +00:00
t . Fatalf ( "Bob: alice GetKeyVal error on alice peer.name %v\n" , exists )
2020-03-07 07:41:00 +00:00
}
fmt . Printf ( "Bob has alice's name as '%v'\n" , aliceName )
2021-10-15 19:38:22 +00:00
bobName , exists := alice . GetContactAttribute ( bob . GetOnion ( ) , attr . GetPeerScope ( constants . Name ) )
2020-03-07 07:41:00 +00:00
if ! exists || bobName != "Bob" {
2020-06-16 00:16:04 +00:00
t . Fatalf ( "Alice: bob GetKeyVal error on bob peer.name\n" )
2020-03-07 07:41:00 +00:00
}
fmt . Printf ( "Alice has bob's name as '%v'\n" , bobName )
2021-10-15 19:38:22 +00:00
aliceName , exists = carol . GetContactAttribute ( alice . GetOnion ( ) , attr . GetPeerScope ( constants . Name ) )
2020-03-07 07:41:00 +00:00
if ! exists || aliceName != "Alice" {
t . Fatalf ( "carol GetKeyVal error for alice peer.name %v\n" , exists )
}
2021-10-15 19:38:22 +00:00
carolName , exists := alice . GetContactAttribute ( carol . GetOnion ( ) , attr . GetPeerScope ( constants . Name ) )
2020-03-07 07:41:00 +00:00
if ! exists || carolName != "Carol" {
t . Fatalf ( "alice GetKeyVal error, carol peer.name\n" )
}
fmt . Printf ( "Alice has carol's name as '%v'\n" , carolName )
2018-11-10 22:14:12 +00:00
2018-05-28 18:36:04 +00:00
fmt . Println ( "Alice inviting Bob to group..." )
2020-02-03 18:46:15 +00:00
err = alice . InviteOnionToGroup ( bob . GetOnion ( ) , groupID )
2018-05-28 18:36:04 +00:00
if err != nil {
t . Fatalf ( "Error for Alice inviting Bob to group: %v" , err )
}
2020-02-03 18:46:15 +00:00
time . Sleep ( time . Second * 5 )
2018-04-27 19:20:33 +00:00
2018-05-28 18:36:04 +00:00
fmt . Println ( "Bob examining groups and accepting invites..." )
2021-05-03 23:32:48 +00:00
for _ , message := range bob . GetContact ( alice . GetOnion ( ) ) . Timeline . GetMessages ( ) {
fmt . Printf ( "Found message from Alice: %v" , message . Message )
if strings . HasPrefix ( message . Message , "torv3" ) {
gid , err := bob . ImportGroup ( message . Message )
if err == nil {
fmt . Printf ( "Bob found invite...now accepting %v..." , gid )
bob . AcceptInvite ( gid )
} else {
t . Fatalf ( "Bob could not accept invite...%v" , gid )
}
2018-05-28 18:36:04 +00:00
}
}
2018-04-27 19:20:33 +00:00
2020-02-10 22:09:24 +00:00
fmt . Println ( "Waiting for Bob to join connect to group server..." )
waitForPeerGroupConnection ( t , bob , groupID )
2018-06-05 22:06:38 +00:00
numGoRoutinesPostServerConnect := runtime . NumGoroutine ( )
2018-05-30 18:42:17 +00:00
2018-05-28 18:36:04 +00:00
// ***** Conversation *****
2018-04-27 19:20:33 +00:00
fmt . Println ( "Starting conversation in group..." )
2018-04-27 19:20:33 +00:00
// Conversation
2020-03-07 07:41:00 +00:00
fmt . Printf ( "%v> %v\n" , aliceName , aliceLines [ 0 ] )
2021-10-30 00:44:16 +00:00
err = alice . SendMessage ( groupID , aliceLines [ 0 ] )
2018-05-20 19:58:16 +00:00
if err != nil {
2018-05-28 18:36:04 +00:00
t . Fatalf ( "Alice failed to send a message to the group: %v" , err )
2018-05-20 19:58:16 +00:00
}
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
2020-03-07 07:41:00 +00:00
fmt . Printf ( "%v> %v\n" , bobName , bobLines [ 0 ] )
2021-10-30 00:44:16 +00:00
err = bob . SendMessage ( groupID , bobLines [ 0 ] )
2018-05-20 19:58:16 +00:00
if err != nil {
2018-05-28 18:36:04 +00:00
t . Fatalf ( "Bob failed to send a message to the group: %v" , err )
2018-05-20 19:58:16 +00:00
}
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
2020-03-07 07:41:00 +00:00
fmt . Printf ( "%v> %v\n" , aliceName , aliceLines [ 1 ] )
2021-10-30 00:44:16 +00:00
alice . SendMessage ( groupID , aliceLines [ 1 ] )
2018-06-05 22:06:38 +00:00
time . Sleep ( time . Second * 10 )
2020-03-07 07:41:00 +00:00
fmt . Printf ( "%v> %v\n" , bobName , bobLines [ 1 ] )
2021-10-30 00:44:16 +00:00
bob . SendMessage ( groupID , bobLines [ 1 ] )
2018-05-20 19:58:16 +00:00
time . Sleep ( time . Second * 10 )
2018-04-27 19:20:33 +00:00
2018-06-05 22:06:38 +00:00
fmt . Println ( "Alice inviting Carol to group..." )
2020-02-03 18:46:15 +00:00
err = alice . InviteOnionToGroup ( carol . GetOnion ( ) , groupID )
2018-06-05 22:06:38 +00:00
if err != nil {
t . Fatalf ( "Error for Alice inviting Carol to group: %v" , err )
}
2020-07-14 00:46:05 +00:00
time . Sleep ( time . Second * 60 ) // Account for some token acquisition in Alice and Bob flows.
2018-06-05 22:06:38 +00:00
fmt . Println ( "Carol examining groups and accepting invites..." )
2021-05-03 23:32:48 +00:00
for _ , message := range carol . GetContact ( alice . GetOnion ( ) ) . Timeline . GetMessages ( ) {
fmt . Printf ( "Found message from Alice: %v" , message . Message )
if strings . HasPrefix ( message . Message , "torv3" ) {
gid , err := carol . ImportGroup ( message . Message )
if err == nil {
fmt . Printf ( "Carol found invite...now accepting %v..." , gid )
carol . AcceptInvite ( gid )
} else {
t . Fatalf ( "Carol could not accept invite...%v" , gid )
}
2018-06-05 22:06:38 +00:00
}
}
fmt . Println ( "Shutting down Alice..." )
2020-02-03 18:46:15 +00:00
app . ShutdownPeer ( alice . GetOnion ( ) )
2018-06-05 22:06:38 +00:00
time . Sleep ( time . Second * 5 )
numGoRoutinesPostAlice := runtime . NumGoroutine ( )
fmt . Println ( "Carol joining server..." )
2021-06-29 21:38:53 +00:00
carol . JoinServer ( ServerAddr )
2019-05-15 20:12:11 +00:00
waitForPeerGroupConnection ( t , carol , groupID )
2018-06-05 22:06:38 +00:00
numGoRotinesPostCarolConnect := runtime . NumGoroutine ( )
2018-04-27 19:20:33 +00:00
2020-03-07 07:41:00 +00:00
fmt . Printf ( "%v> %v" , bobName , bobLines [ 2 ] )
2021-10-30 00:44:16 +00:00
bob . SendMessage ( groupID , bobLines [ 2 ] )
2020-09-28 17:40:41 +00:00
// Bob should have enough tokens so we don't need to account for
// token acquisition here...
2018-04-27 19:20:33 +00:00
2020-03-07 07:41:00 +00:00
fmt . Printf ( "%v> %v" , carolName , carolLines [ 0 ] )
2021-10-30 00:44:16 +00:00
carol . SendMessage ( groupID , carolLines [ 0 ] )
2020-09-28 17:40:41 +00:00
time . Sleep ( time . Second * 30 ) // we need to account for spam-based token acquisition, but everything should
// be warmed-up and delays should be pretty small.
2018-04-27 19:20:33 +00:00
2018-05-28 18:36:04 +00:00
// ***** Verify Test *****
2018-04-27 19:20:33 +00:00
2018-06-27 15:14:59 +00:00
fmt . Println ( "Final syncing time..." )
2020-09-28 17:40:41 +00:00
time . Sleep ( time . Second * 30 )
2018-04-27 19:20:33 +00:00
2018-06-19 22:28:44 +00:00
alicesGroup := alice . GetGroup ( groupID )
2018-04-27 19:20:33 +00:00
if alicesGroup == nil {
t . Error ( "aliceGroup == nil" )
return
}
2018-04-27 19:20:33 +00:00
2018-06-05 22:06:38 +00:00
fmt . Printf ( "Alice's TimeLine:\n" )
aliceVerified := printAndCountVerifedTimeline ( t , alicesGroup . GetTimeline ( ) )
if aliceVerified != 4 {
t . Errorf ( "Alice did not have 4 verified messages" )
}
2018-04-27 19:20:33 +00:00
2018-06-19 22:28:44 +00:00
bobsGroup := bob . GetGroup ( groupID )
2018-04-27 19:20:33 +00:00
if bobsGroup == nil {
t . Error ( "bobGroup == nil" )
return
}
2018-06-05 22:06:38 +00:00
fmt . Printf ( "Bob's TimeLine:\n" )
bobVerified := printAndCountVerifedTimeline ( t , bobsGroup . GetTimeline ( ) )
2018-10-05 03:18:34 +00:00
if bobVerified != 6 {
2020-07-14 00:46:05 +00:00
t . Errorf ( "Bob did not have 6 verified messages" )
2018-06-05 22:06:38 +00:00
}
2018-06-19 22:28:44 +00:00
carolsGroup := carol . GetGroup ( groupID )
2018-06-05 22:06:38 +00:00
fmt . Printf ( "Carol's TimeLine:\n" )
carolVerified := printAndCountVerifedTimeline ( t , carolsGroup . GetTimeline ( ) )
2018-10-05 03:18:34 +00:00
if carolVerified != 6 {
2020-07-14 00:46:05 +00:00
t . Errorf ( "Carol did not have 6 verified messages" )
2018-06-05 22:06:38 +00:00
}
2018-04-27 19:20:33 +00:00
2018-06-05 22:06:38 +00:00
if len ( alicesGroup . GetTimeline ( ) ) != 4 {
2018-04-27 19:20:33 +00:00
t . Errorf ( "Alice's timeline does not have all messages" )
2018-06-27 15:14:59 +00:00
} else {
// check message 0,1,2,3
2021-11-02 20:41:53 +00:00
alicesGroup . Timeline . Sort ( )
2018-06-27 15:14:59 +00:00
aliceGroupTimeline := alicesGroup . GetTimeline ( )
if aliceGroupTimeline [ 0 ] . Message != aliceLines [ 0 ] || aliceGroupTimeline [ 1 ] . Message != bobLines [ 0 ] ||
aliceGroupTimeline [ 2 ] . Message != aliceLines [ 1 ] || aliceGroupTimeline [ 3 ] . Message != bobLines [ 1 ] {
t . Errorf ( "Some of Alice's timeline messages did not have the expected content!" )
}
2018-04-27 19:20:33 +00:00
}
2018-06-05 22:06:38 +00:00
if len ( bobsGroup . GetTimeline ( ) ) != 6 {
t . Errorf ( "Bob's timeline does not have all messages" )
2018-06-27 15:14:59 +00:00
} else {
// check message 0,1,2,3,4,5
2021-11-02 20:41:53 +00:00
bobsGroup . Timeline . Sort ( )
2018-06-27 15:14:59 +00:00
bobGroupTimeline := bobsGroup . GetTimeline ( )
if bobGroupTimeline [ 0 ] . Message != aliceLines [ 0 ] || bobGroupTimeline [ 1 ] . Message != bobLines [ 0 ] ||
bobGroupTimeline [ 2 ] . Message != aliceLines [ 1 ] || bobGroupTimeline [ 3 ] . Message != bobLines [ 1 ] ||
bobGroupTimeline [ 4 ] . Message != bobLines [ 2 ] || bobGroupTimeline [ 5 ] . Message != carolLines [ 0 ] {
t . Errorf ( "Some of Bob's timeline messages did not have the expected content!" )
}
2018-06-05 22:06:38 +00:00
}
if len ( carolsGroup . GetTimeline ( ) ) != 6 {
t . Errorf ( "Carol's timeline does not have all messages" )
2018-06-27 15:14:59 +00:00
} else {
// check message 0,1,2,3,4,5
2021-11-02 20:41:53 +00:00
carolsGroup . Timeline . Sort ( )
2018-06-27 15:14:59 +00:00
carolGroupTimeline := carolsGroup . GetTimeline ( )
if carolGroupTimeline [ 0 ] . Message != aliceLines [ 0 ] || carolGroupTimeline [ 1 ] . Message != bobLines [ 0 ] ||
carolGroupTimeline [ 2 ] . Message != aliceLines [ 1 ] || carolGroupTimeline [ 3 ] . Message != bobLines [ 1 ] ||
2021-10-30 00:44:16 +00:00
carolGroupTimeline [ 4 ] . Message != carolLines [ 0 ] || carolGroupTimeline [ 5 ] . Message != bobLines [ 2 ] {
2018-06-27 15:14:59 +00:00
t . Errorf ( "Some of Carol's timeline messages did not have the expected content!" )
}
2018-04-27 19:20:33 +00:00
}
2018-05-30 18:42:17 +00:00
fmt . Println ( "Shutting down Bob..." )
2020-02-03 18:46:15 +00:00
app . ShutdownPeer ( bob . GetOnion ( ) )
2018-05-30 18:42:17 +00:00
time . Sleep ( time . Second * 3 )
numGoRoutinesPostBob := runtime . NumGoroutine ( )
2018-06-05 22:06:38 +00:00
2019-01-04 21:44:21 +00:00
fmt . Println ( "Shutting down Carol..." )
2020-02-03 18:46:15 +00:00
appClient . ShutdownPeer ( carol . GetOnion ( ) )
2018-05-30 18:42:17 +00:00
time . Sleep ( time . Second * 3 )
2018-06-05 22:06:38 +00:00
numGoRoutinesPostCarol := runtime . NumGoroutine ( )
2018-05-30 18:42:17 +00:00
2019-06-05 20:40:55 +00:00
fmt . Println ( "Shutting down apps..." )
2019-07-19 17:27:50 +00:00
fmt . Printf ( "app Shutdown: %v\n" , runtime . NumGoroutine ( ) )
2019-06-05 20:40:55 +00:00
app . Shutdown ( )
2019-07-19 17:27:50 +00:00
fmt . Printf ( "appClientShutdown: %v\n" , runtime . NumGoroutine ( ) )
2019-06-05 20:40:55 +00:00
appClient . Shutdown ( )
2019-07-19 17:27:50 +00:00
fmt . Printf ( "appServiceShutdown: %v\n" , runtime . NumGoroutine ( ) )
2019-06-05 20:40:55 +00:00
appService . Shutdown ( )
2019-07-19 17:27:50 +00:00
fmt . Printf ( "bridgeClientShutdown: %v\n" , runtime . NumGoroutine ( ) )
bridgeClient . Shutdown ( )
time . Sleep ( 2 * time . Second )
fmt . Printf ( "brideServiceShutdown: %v\n" , runtime . NumGoroutine ( ) )
bridgeService . Shutdown ( )
time . Sleep ( 2 * time . Second )
fmt . Printf ( "Done shutdown: %v\n" , runtime . NumGoroutine ( ) )
2019-06-05 20:40:55 +00:00
numGoRoutinesPostAppShutdown := runtime . NumGoroutine ( )
2019-02-04 22:18:24 +00:00
fmt . Println ( "Shutting down ACN..." )
acn . Close ( )
2019-11-04 20:18:59 +00:00
time . Sleep ( time . Second * 2 ) // Server ^^ has a 5 second loop attempting reconnect before exiting
2019-10-31 23:05:01 +00:00
time . Sleep ( time . Second * 30 ) // the network status plugin might keep goroutines alive for a minute before killing them
2019-02-04 22:18:24 +00:00
numGoRoutinesPostACN := runtime . NumGoroutine ( )
2019-01-04 21:44:21 +00:00
// Printing out the current goroutines
// Very useful if we are leaking any.
pprof . Lookup ( "goroutine" ) . WriteTo ( os . Stdout , 1 )
2021-06-29 23:27:57 +00:00
fmt . Printf ( "numGoRoutinesStart: %v\nnumGoRoutinesPostAppStart: %v\nnumGoRoutinesPostPeerStart: %v\nnumGoRoutinesPostPeerAndServerConnect: %v\n" +
2021-06-29 23:12:58 +00:00
"numGoRoutinesPostAlice: %v\nnumGoRotinesPostCarolConnect: %v\nnumGoRoutinesPostBob: %v\nnumGoRoutinesPostCarol: %v\nnumGoRoutinesPostAppShutdown: %v\nnumGoRoutinesPostACN: %v\n" ,
numGoRoutinesStart , numGoRoutinesPostAppStart , numGoRoutinesPostPeerStart , numGoRoutinesPostServerConnect ,
2021-06-29 23:27:57 +00:00
numGoRoutinesPostAlice , numGoRotinesPostCarolConnect , numGoRoutinesPostBob , numGoRoutinesPostCarol , numGoRoutinesPostAppShutdown , numGoRoutinesPostACN )
2018-05-30 18:42:17 +00:00
2019-02-04 22:18:24 +00:00
if numGoRoutinesStart != numGoRoutinesPostACN {
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 , numGoRoutinesPostACN )
2018-05-30 18:42:17 +00:00
}
2018-03-30 21:16:51 +00:00
}