@ -2,6 +2,7 @@ package filesharing
import (
"crypto/rand"
utils2 "cwtch.im/cwtch/utils"
"encoding/base64"
"encoding/hex"
"encoding/json"
@ -9,7 +10,6 @@ import (
"io/ioutil"
app2 "cwtch.im/cwtch/app"
"cwtch.im/cwtch/app/utils"
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/functionality/filesharing"
"cwtch.im/cwtch/model"
@ -58,7 +58,7 @@ func TestFileSharing(t *testing.T) {
os . RemoveAll ( "cwtch.out.png" )
os . RemoveAll ( "cwtch.out.png.manifest" )
log . SetLevel ( log . Level Debug )
log . SetLevel ( log . Level Info )
os . Mkdir ( "tordir" , 0700 )
dataDir := path . Join ( "tordir" , "tor" )
@ -97,15 +97,15 @@ func TestFileSharing(t *testing.T) {
os . RemoveAll ( path . Join ( cwtchDir , "testing" ) )
os . Mkdir ( path . Join ( cwtchDir , "testing" ) , 0700 )
fmt. Println ( "Creating Alice..." )
t. Logf ( "Creating Alice..." )
app . CreateTaggedPeer ( "alice" , "asdfasdf" , "testing" )
fmt. Println ( "Creating Bob..." )
t. Logf ( "Creating Bob..." )
app . CreateTaggedPeer ( "bob" , "asdfasdf" , "testing" )
t . Logf ( "** Waiting for Alice, Bob..." )
alice := utils . WaitGetPeer ( app , "alice" )
bob := utils . WaitGetPeer ( app , "bob" )
alice := app2 . WaitGetPeer ( app , "alice" )
bob := app2 . WaitGetPeer ( app , "bob" )
alice . AutoHandleEvents ( [ ] event . Type { event . PeerStateChange , event . NewRetValMessageFromPeer } )
bob . AutoHandleEvents ( [ ] event . Type { event . PeerStateChange , event . NewRetValMessageFromPeer , event . ManifestReceived } )
@ -124,14 +124,14 @@ func TestFileSharing(t *testing.T) {
alice . NewContactConversation ( bob . GetOnion ( ) , model . DefaultP2PAccessControl ( ) , true )
alice . PeerWithOnion ( bob . GetOnion ( ) )
fmt. Println ( "Waiting for alice and Bob to peer..." )
t. Logf ( "Waiting for alice and Bob to peer..." )
waitForPeerPeerConnection ( t , alice , bob )
fmt. Println ( "Alice and Bob are Connected!!" )
t. Logf ( "Alice and Bob are Connected!!" )
filesharingFunctionality , _ := filesharing . FunctionalityGate ( map [ string ] bool { constants . FileSharingExperiment : true } )
_ , fileSharingMessage , err := filesharingFunctionality . ShareFile ( "cwtch.png" , alice )
filekey , fileSharingMessage , err := filesharingFunctionality . ShareFile ( "cwtch.png" , alice )
alice . SendMessage ( 1 , fileSharingMessage )
if err != nil {
@ -141,6 +141,44 @@ func TestFileSharing(t *testing.T) {
// Wait for the messages to arrive...
time . Sleep ( time . Second * 10 )
// test that bob can download and verify the file
testBobDownloadFile ( t , bob , filesharingFunctionality , queueOracle )
// Test stopping and restarting file shares
t . Logf ( "Stopping File Share" )
alice . StopFileShare ( filekey )
// Allow time for the stop request to filter through Engine
time . Sleep ( time . Second * 5 )
// Restart
t . Logf ( "Restarting File Share" )
filesharingFunctionality . ReShareFiles ( alice )
// run the same download test again...to check that we can actually download the file
testBobDownloadFile ( t , bob , filesharingFunctionality , queueOracle )
queueOracle . Shutdown ( )
app . Shutdown ( )
acn . Close ( )
time . Sleep ( 5 * time . Second )
numGoRoutinesPostACN := runtime . NumGoroutine ( )
// Printing out the current goroutines
// Very useful if we are leaking any.
pprof . Lookup ( "goroutine" ) . WriteTo ( os . Stdout , 1 )
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 )
}
}
func testBobDownloadFile ( t * testing . T , bob peer . CwtchPeer , filesharingFunctionality * filesharing . Functionality , queueOracle event . Queue ) {
os . RemoveAll ( "cwtch.out.png" )
os . RemoveAll ( "cwtch.out.png.manifest" )
message , _ , err := bob . GetChannelMessage ( 1 , 0 , 1 )
if err != nil {
t . Fatalf ( "could not find file sharing message: %v" , err )
@ -159,28 +197,21 @@ func TestFileSharing(t *testing.T) {
}
// Wait for the file downloaded event
ev := queueOracle . Next ( )
if ev . EventType != event . FileDownloaded {
t . Fatalf ( "Expected file download event" )
}
manifest , _ := files . CreateManifest ( "cwtch.out.png" )
if hex . EncodeToString ( manifest . RootHash ) != "8f0ed73bbb30db45b6a740b1251cae02945f48e4f991464d5f3607685c45dcd136a325dab2e5f6429ce2b715e602b20b5b16bf7438fb6235fefe912adcedb5fd" {
t . Fatalf ( "file hash does not match expected %x: " , manifest . RootHash )
}
queueOracle . Shutdown ( )
app . Shutdown ( )
acn . Close ( )
time . Sleep ( 5 * time . Second )
numGoRoutinesPostACN := runtime . NumGoroutine ( )
ClientTimeout := utils2 . TimeoutPolicy ( time . Second * 60 )
err = ClientTimeout . ExecuteAction ( func ( ) error {
ev := queueOracle . Next ( )
if ev . EventType != event . FileDownloaded {
t . Fatalf ( "Expected file download event" )
}
// Printing out the current goroutines
// Very useful if we are leaking any.
pprof . Lookup ( "goroutine" ) . WriteTo ( os . Stdout , 1 )
manifest , _ := files . CreateManifest ( "cwtch.out.png" )
if hex . EncodeToString ( manifest . RootHash ) != "8f0ed73bbb30db45b6a740b1251cae02945f48e4f991464d5f3607685c45dcd136a325dab2e5f6429ce2b715e602b20b5b16bf7438fb6235fefe912adcedb5fd" {
t . Fatalf ( "file hash does not match expected %x: " , manifest . RootHash )
}
return nil
} )
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 )
if err != nil {
t . Fatalf( "timeout when attempting to download a file" )
}
}