From a4532910d01ad3d34bb00d89f10f1ba897698c36 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Sun, 20 May 2018 12:58:16 -0700 Subject: [PATCH] Fixing up integration test for commit --- model/message.go | 7 +- model/message_test.go | 2 +- model/profile.go | 2 +- model/profile_test | 2 +- server/app/main.go | 4 +- .../cwtch_peer_server_intergration_test.go | 82 ++++++++++--------- 6 files changed, 52 insertions(+), 47 deletions(-) diff --git a/model/message.go b/model/message.go index d21c31e..7d5c4da 100644 --- a/model/message.go +++ b/model/message.go @@ -36,11 +36,12 @@ func compareSignatures(a []byte, b []byte) bool { return true } -func (t *Timeline) GetMessages() (messages []Message) { +func (t *Timeline) GetMessages() []Message { t.lock.Lock() - copy(messages[:], t.Messages) + messages := make([]Message, len(t.Messages)) + copy(messages[:], t.Messages[:]) t.lock.Unlock() - return + return messages } // Len gets the length of the timeline diff --git a/model/message_test.go b/model/message_test.go index 7b48085..c976c1e 100644 --- a/model/message_test.go +++ b/model/message_test.go @@ -54,7 +54,7 @@ func TestTranscriptConsistency(t *testing.T) { timeline.Insert(m3) timeline.Insert(m2) - for i, m := range timeline.Messages { + for i, m := range timeline.GetMessages() { t.Logf("Messages %v: %v %x %x", i, m.Message, m.Signature, m.PreviousMessageSig) } } diff --git a/model/profile.go b/model/profile.go index 6762a68..65a020e 100644 --- a/model/profile.go +++ b/model/profile.go @@ -156,7 +156,7 @@ func (p *Profile) AttemptDecryption(ciphertext []byte) (bool, *Message) { success, dgm := group.DecryptMessage(ciphertext) if success { - // Asser that we know the owner of the group + // Assert that we know the owner of the group owner, ok := p.Contacts[group.Owner] if ok { valid := ed25519.Verify(owner.Ed25519PublicKey, []byte(group.GroupID+group.GroupServer), dgm.SignedGroupId) diff --git a/model/profile_test b/model/profile_test index 1b91fa7..f188365 100644 --- a/model/profile_test +++ b/model/profile_test @@ -1 +1 @@ -{"Name":"Sarah","Ed25519PublicKey":"W9PpKZvtZgVlHZ6wT2oTaStaUXHFYO553TyM/kcx+j0=","Trusted":false,"Blocked":false,"Onion":"b56kmtfigbjj4oed","Contacts":{"b56kmtfigbjj4oed":{"Name":"Sarah","Ed25519PublicKey":"W9PpKZvtZgVlHZ6wT2oTaStaUXHFYO553TyM/kcx+j0=","Trusted":false,"Blocked":false,"Onion":"b56kmtfigbjj4oed"}},"Ed25519PrivateKey":"D9VtCjO9WRscq1J1hBHj5KNTWhJqCzeZHTv76iuD5Alb0+kpm+1mBWUdnrBPahNpK1pRccVg7nndPIz+RzH6PQ==","OnionPrivateKey":{"N":131431908350807887726680282316542867215751372260346817354651281962467765863817142968881902964613976074829288548976846611912590630516744102978481639231882423095250930957765239944153566210775495249916207632888810237972001419936958220195829843082407234641091082084103419783680884292376945307398827177826045917213,"E":65537,"D":6527776091091744732751641346725468556498935207706011726038572452016915298025921240882411372962120513962636895599731994472976829917939515925278205223000399956250499414440500849418156829119221518850817208974047327356576252006802914743934160449516927022744695396017974696922331017645717939347896803326305764673,"Primes":[13025701017471365410286943463589035042960291474418354951794759000498929713389834945845345637495625867751746594507969387541024007744235240081409789677106193,10090198460299245573384301425475815284645715053514216764835542359499265357695622569689478708035086000871913905222809009531316465232841423534462476890808141],"Precomputed":{"Dp":5662484123285460129988785255316105533880688833882828517885052473018516372956900645545781729591686848226913964287838134962597829937794863816155223887281313,"Dq":5664872700127415683157635940750372271896095940903643615749255314942947793934002728674713209079496380915836859007400929195649145090211137792804253699748153,"Qinv":7915626106616008088617615457915827664685512673801991601529702673140410770136418670435683522388873693637240686678200328224101409032168571783863608820704490,"CRTValues":[]}},"Groups":{}} \ No newline at end of file +{"Name":"Sarah","Ed25519PublicKey":"/2xUC3+EcM69CnYrYSKW499gXtH65DQ65R6UGRmbpHM=","Trusted":false,"Blocked":false,"Onion":"4g3rdfpq4fryjyg2","Contacts":{"4g3rdfpq4fryjyg2":{"Name":"Sarah","Ed25519PublicKey":"/2xUC3+EcM69CnYrYSKW499gXtH65DQ65R6UGRmbpHM=","Trusted":false,"Blocked":false,"Onion":"4g3rdfpq4fryjyg2"}},"Ed25519PrivateKey":"RJ8K69ZZG5B4ufBDVUvXprYpGsDM8iFiQMEdc1MCDh7/bFQLf4Rwzr0KdithIpbj32Be0frkNDrlHpQZGZukcw==","OnionPrivateKey":{"N":137900602813611213189041103405418371044707088095490122917581331230896621428290390270991677482016645829512106617167800422226984565513726430820993290694895580565153249656392070078179688609933325529529966437840233160619179283162861419376169773461376606874165643290168384848468920199602266115187846439397770310477,"E":65537,"D":32650314385138245495740586257257379243796937393803809104965279410254098825133618960815689755229142214879218737180413493930087118773768909578548802839811012970470413974184929457770929480883399498950171683167037292603270431438053986057858898333372137069087346537018540254886839274994781698804912229336225943149,"Primes":[13089226577015106801916990672183383260292068983897361545720483543242420102790612098089034534068816127751017739419172149354346726789445970796490590388209763,10535427895775514969658003258523047110222786921729773130757443070837085109335455191190769936580263568766152909466244437192612859171443040757316597016718479],"Precomputed":{"Dp":12060255227628015079020362264671767065516221151298383767006902951900354576910922250684443144174213984073808782746032168679711111237632100134978474763752483,"Dq":8251736413294062268756808600687345029751832941381372119183214218977652558818655880174762525208683785469816973554050280078947932833809034974347593595452587,"Qinv":11288899754516920495714102707684244008755697053785965771187317487773161282452886307082370168337103639984386063085142036280484558810305271619464191907337467,"CRTValues":[]}},"Groups":{}} \ No newline at end of file diff --git a/server/app/main.go b/server/app/main.go index 0e32724..f84d50a 100644 --- a/server/app/main.go +++ b/server/app/main.go @@ -11,11 +11,11 @@ import ( const privateKeyFile = "./private_key" func checkAndGenPrivateKey(privateKeyFile string) { - if _, err := os.Stat(privateKeyFile); os.IsNotExist(err) { + if _, err := os.Stat(privateKeyFile); os.IsNotExist(err) { log.Printf("no private key found!") log.Printf("generating new private key...") pk, pk_err := utils.GeneratePrivateKey() - if pk_err != nil { + if pk_err != nil { log.Fatalf("error generating new private key: %v\n", err) } err := ioutil.WriteFile(privateKeyFile, []byte(utils.PrivateKeyToString(pk)), 0400) diff --git a/testing/cwtch_peer_server_intergration_test.go b/testing/cwtch_peer_server_intergration_test.go index 0b66536..bb5290e 100644 --- a/testing/cwtch_peer_server_intergration_test.go +++ b/testing/cwtch_peer_server_intergration_test.go @@ -1,19 +1,19 @@ package testing import ( - cwtchserver "git.mascherari.press/cwtch/server" - "github.com/s-rah/go-ricochet/utils" - "git.mascherari.press/cwtch/peer" - "testing" - "time" - "log" - "git.mascherari.press/cwtch/protocol" - "github.com/golang/protobuf/proto" - "io/ioutil" "fmt" "git.mascherari.press/cwtch/model" - "os" + "git.mascherari.press/cwtch/peer" + "git.mascherari.press/cwtch/protocol" + cwtchserver "git.mascherari.press/cwtch/server" + "github.com/golang/protobuf/proto" "github.com/s-rah/go-ricochet" + "github.com/s-rah/go-ricochet/utils" + "io/ioutil" + "log" + "os" + "testing" + "time" ) const ( @@ -21,16 +21,15 @@ const ( ) var ( - aliceLines = []string {"Hello", "My name is Alice", "bye"} - bobLines = []string {"Hi", "My name is Bob.", "toodles", "hello?"} - + aliceLines = []string{"Hello", "My name is Alice", "bye"} + bobLines = []string{"Hi", "My name is Bob.", "toodles", "hello?"} ) -func checkAndGenPrivateKey(privateKeyFile string) (generated bool){ - if _, err := os.Stat(privateKeyFile); os.IsNotExist(err) { +func checkAndGenPrivateKey(privateKeyFile string) (generated bool) { + if _, err := os.Stat(privateKeyFile); os.IsNotExist(err) { fmt.Println("generating new private key...") pk, pk_err := utils.GeneratePrivateKey() - if pk_err != nil { + if pk_err != nil { log.Fatalf("error generating new private key: %v\n", err) } err := ioutil.WriteFile(privateKeyFile, []byte(utils.PrivateKeyToString(pk)), 0400) @@ -42,8 +41,8 @@ func checkAndGenPrivateKey(privateKeyFile string) (generated bool){ return false } -func printAndVerifyTimeline(t *testing.T, timeline model.Timeline) error { - for _, message := range timeline.Messages { +func printAndVerifyTimeline(t *testing.T, timeline []model.Message) error { + for _, message := range timeline { fmt.Printf("%v %v> %s [%t]\n", message.Timestamp, message.PeerID, message.Message, message.Verified) if !message.Verified { t.Errorf("Message '%s' from '%s' not verified!", message.Message, message.PeerID) @@ -69,17 +68,17 @@ func TestCwtchPeerIntegration(t *testing.T) { serverKey, err := utils.LoadPrivateKeyFromFile(keyfile) if err != nil { - t.Error("Could not load server's key from %v", keyfile) + t.Errorf("Could not load server's key from %v", keyfile) } serverAddr, _ := utils.GetOnionAddress(serverKey) serverOnline := false - if ! generatedKey { + if !generatedKey { fmt.Printf("Checking if test server %v is online...\n", serverAddr) serverOnline = serverCheck(serverAddr) } - if ! serverOnline { + if !serverOnline { // launch app server := new(cwtchserver.Server) fmt.Printf("No server found\nStarting cwtch server...\n") @@ -87,7 +86,7 @@ func TestCwtchPeerIntegration(t *testing.T) { // let tor get established fmt.Printf("Establishing Tor hidden service: %v...\n", serverAddr) - time.Sleep(time.Second * 60) + time.Sleep(time.Second * 120) } else { fmt.Printf("Found existing cwtch server %v, using for tests...\n", serverAddr) } @@ -102,14 +101,13 @@ func TestCwtchPeerIntegration(t *testing.T) { groupId, invite, err := alice.Profile.StartGroup(serverAddr) fmt.Printf("Started group: %v!\n", groupId) if err != nil { - t.Error("Failed to start group: %v", err) + t.Errorf("Failed to start group: %v", err) return } gci := &protocol.CwtchPeerPacket{} proto.Unmarshal(invite, gci) - fmt.Println("Alice joining server/group...") alice.JoinServer(serverAddr) @@ -124,49 +122,54 @@ func TestCwtchPeerIntegration(t *testing.T) { alice.Profile.AddContact(bob.Profile.Onion, &bob.Profile.PublicProfile) bob.Profile.AddContact(alice.Profile.Onion, &alice.Profile.PublicProfile) - // TODO: have alice actually protocolly send the invite + // TODO: have alice actually send the invite fmt.Printf("Bob processing invite to group %v\n", gci.GetGroupChatInvite().GroupName) bob.Profile.ProcessInvite(gci.GetGroupChatInvite(), alice.Profile.Onion) fmt.Println("Bob joining server/group...") bob.JoinServer(serverAddr) - time.Sleep(time.Second * 10) - + // Wait for them to join the server + time.Sleep(time.Second * 60) fmt.Println("Starting conversation in group...") // Conversation fmt.Println("Alice> ", aliceLines[0]) - alice.SendMessageToGroup(groupId, aliceLines[0]) - time.Sleep(time.Second * 2) + err = alice.SendMessageToGroup(groupId, aliceLines[0]) + if err != nil { + t.Errorf("Alice failed to send a message to the group: %v", err) + } + time.Sleep(time.Second * 10) fmt.Println("Bob> ", bobLines[0]) - bob.SendMessageToGroup(groupId, bobLines[0]) - time.Sleep(time.Second * 2) + err = bob.SendMessageToGroup(groupId, bobLines[0]) + if err != nil { + t.Errorf("Bob failed to send a message to the group: %v", err) + } + time.Sleep(time.Second * 10) // "instant" - could be either order? fmt.Println("Alice> ", aliceLines[1]) alice.SendMessageToGroup(groupId, aliceLines[1]) fmt.Println("Bob> ", bobLines[1]) bob.SendMessageToGroup(groupId, bobLines[1]) - time.Sleep(time.Second * 3) + time.Sleep(time.Second * 10) fmt.Println("Alice> ", aliceLines[2]) alice.SendMessageToGroup(groupId, aliceLines[2]) // Todo: Alice disconnects - time.Sleep(time.Second * 2) + time.Sleep(time.Second * 10) fmt.Println("Bob> ", bobLines[2]) bob.SendMessageToGroup(groupId, bobLines[2]) - time.Sleep(time.Second * 2) - + time.Sleep(time.Second * 10) // Todo: Alice reconects, gets missed messages (from bob) // Verify // final syncing time... - time.Sleep(time.Second * 4) + time.Sleep(time.Second * 10) alicesGroup := alice.Profile.GetGroupByGroupID(groupId) fmt.Printf("alice Groups:\n") @@ -199,9 +202,10 @@ func TestCwtchPeerIntegration(t *testing.T) { } // check message 0,1 and 4,5 for content (2,3 could be out of order) - if alicesGroup.Timeline.Messages[0].Message != aliceLines[0] || alicesGroup.Timeline.Messages[1].Message != bobLines[0] || - alicesGroup.Timeline.Messages[4].Message != aliceLines[2] || alicesGroup.Timeline.Messages[5].Message != bobLines[2] { - t.Errorf("Some of the messages did not have the expected content!") + aliceGroupTimeline := alicesGroup.GetTimeline() + if aliceGroupTimeline[0].Message != aliceLines[0] || aliceGroupTimeline[1].Message != bobLines[0] || + aliceGroupTimeline[4].Message != aliceLines[2] || aliceGroupTimeline[5].Message != bobLines[2] { + t.Errorf("Some of the messages did not have the expected content!") } // Todo: shutdown users and server