2018-03-31 19:33:32 +00:00
|
|
|
package testing
|
2018-03-30 21:16:51 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-27 19:20:33 +00:00
|
|
|
cwtchserver "git.mascherari.press/cwtch/server"
|
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
2018-04-02 21:10:29 +00:00
|
|
|
"git.mascherari.press/cwtch/peer"
|
2018-03-30 21:16:51 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2018-04-27 19:20:33 +00:00
|
|
|
"log"
|
|
|
|
"git.mascherari.press/cwtch/protocol"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"io/ioutil"
|
|
|
|
"fmt"
|
|
|
|
"git.mascherari.press/cwtch/model"
|
|
|
|
"os"
|
|
|
|
"github.com/s-rah/go-ricochet"
|
2018-03-30 21:16:51 +00:00
|
|
|
)
|
|
|
|
|
2018-04-27 19:20:33 +00:00
|
|
|
const (
|
|
|
|
keyfile = "./private_key"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
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) {
|
|
|
|
fmt.Println("generating new private key...")
|
|
|
|
pk, pk_err := utils.GeneratePrivateKey()
|
|
|
|
if pk_err != nil {
|
|
|
|
log.Fatalf("error generating new private key: %v\n", err)
|
|
|
|
}
|
|
|
|
err := ioutil.WriteFile(privateKeyFile, []byte(utils.PrivateKeyToString(pk)), 0400)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error writing new private key to file %s: %v\n", privateKeyFile, err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func printAndVerifyTimeline(t *testing.T, timeline model.Timeline) error {
|
|
|
|
for _, message := range timeline.Messages {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverCheck(serverAddr string) bool {
|
|
|
|
rc, err := goricochet.Open(serverAddr)
|
|
|
|
if err == nil {
|
|
|
|
rc.Conn.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:16:51 +00:00
|
|
|
func TestCwtchPeerIntegration(t *testing.T) {
|
2018-04-27 19:20:33 +00:00
|
|
|
// Hide logging "noise"
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
|
|
|
|
generatedKey := checkAndGenPrivateKey(keyfile)
|
|
|
|
|
|
|
|
serverKey, err := utils.LoadPrivateKeyFromFile(keyfile)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Could not load server's key from %v", keyfile)
|
|
|
|
}
|
|
|
|
serverAddr, _ := utils.GetOnionAddress(serverKey)
|
|
|
|
|
|
|
|
serverOnline := false
|
|
|
|
if ! generatedKey {
|
|
|
|
fmt.Printf("Checking if test server %v is online...\n", serverAddr)
|
|
|
|
serverOnline = serverCheck(serverAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ! serverOnline {
|
|
|
|
// launch app
|
|
|
|
server := new(cwtchserver.Server)
|
|
|
|
fmt.Printf("No server found\nStarting cwtch server...\n")
|
|
|
|
go server.Run(keyfile)
|
|
|
|
|
|
|
|
// let tor get established
|
|
|
|
fmt.Printf("Establishing Tor hidden service: %v...\n", serverAddr)
|
|
|
|
time.Sleep(time.Second * 60)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Found existing cwtch server %v, using for tests...\n", serverAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// launch alice
|
2018-04-27 19:20:33 +00:00
|
|
|
fmt.Println("Creating Alice...")
|
2018-03-31 19:33:32 +00:00
|
|
|
alice := peer.NewCwtchPeer("Alice")
|
2018-04-27 19:20:33 +00:00
|
|
|
alice.Profile.AddContact(alice.Profile.Onion, &alice.Profile.PublicProfile)
|
|
|
|
fmt.Println("Alice created:", alice.Profile.Onion)
|
|
|
|
|
|
|
|
fmt.Println("Starting group on ", serverAddr, "...")
|
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-27 19:20:33 +00:00
|
|
|
gci := &protocol.CwtchPeerPacket{}
|
|
|
|
proto.Unmarshal(invite, gci)
|
2018-04-27 19:20:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
fmt.Println("Alice joining server/group...")
|
2018-04-27 19:20:33 +00:00
|
|
|
alice.JoinServer(serverAddr)
|
|
|
|
|
|
|
|
// launch bob
|
2018-04-27 19:20:33 +00:00
|
|
|
fmt.Println("Creating Bob...")
|
2018-04-27 19:20:33 +00:00
|
|
|
bob := peer.NewCwtchPeer("Bob")
|
|
|
|
bob.Profile.AddContact(bob.Profile.Onion, &bob.Profile.PublicProfile)
|
2018-04-27 19:20:33 +00:00
|
|
|
fmt.Println("Bob created:", bob.Profile.Onion)
|
2018-04-27 19:20:33 +00:00
|
|
|
|
|
|
|
// Associate peer alice & bob as peers
|
2018-04-27 19:20:33 +00:00
|
|
|
fmt.Println("Peering Alice and Bob...")
|
2018-04-27 19:20:33 +00:00
|
|
|
alice.Profile.AddContact(bob.Profile.Onion, &bob.Profile.PublicProfile)
|
|
|
|
bob.Profile.AddContact(alice.Profile.Onion, &alice.Profile.PublicProfile)
|
|
|
|
|
2018-04-27 19:20:33 +00:00
|
|
|
// TODO: have alice actually protocolly 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)
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Println("Starting conversation in group...")
|
2018-04-27 19:20:33 +00:00
|
|
|
// Conversation
|
|
|
|
fmt.Println("Alice> ", aliceLines[0])
|
|
|
|
alice.SendMessageToGroup(groupId, aliceLines[0])
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
fmt.Println("Bob> ", bobLines[0])
|
|
|
|
bob.SendMessageToGroup(groupId, bobLines[0])
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
2018-04-27 19:20:33 +00:00
|
|
|
// "instant" - could be either order?
|
2018-04-27 19:20:33 +00:00
|
|
|
fmt.Println("Alice> ", aliceLines[1])
|
|
|
|
alice.SendMessageToGroup(groupId, aliceLines[1])
|
|
|
|
fmt.Println("Bob> ", bobLines[1])
|
|
|
|
bob.SendMessageToGroup(groupId, bobLines[1])
|
2018-04-27 19:20:33 +00:00
|
|
|
time.Sleep(time.Second * 3)
|
2018-04-27 19:20:33 +00:00
|
|
|
|
|
|
|
fmt.Println("Alice> ", aliceLines[2])
|
|
|
|
alice.SendMessageToGroup(groupId, aliceLines[2])
|
|
|
|
// Todo: Alice disconnects
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
fmt.Println("Bob> ", bobLines[2])
|
|
|
|
bob.SendMessageToGroup(groupId, bobLines[2])
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
|
|
|
|
// Todo: Alice reconects, gets missed messages (from bob)
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
|
|
|
|
// final syncing time...
|
2018-04-27 19:20:33 +00:00
|
|
|
time.Sleep(time.Second * 4)
|
|
|
|
|
|
|
|
alicesGroup := alice.Profile.GetGroupByGroupID(groupId)
|
|
|
|
fmt.Printf("alice Groups:\n")
|
|
|
|
for k := range alice.Profile.Groups {
|
|
|
|
fmt.Println(" " + k)
|
|
|
|
}
|
|
|
|
if alicesGroup == nil {
|
|
|
|
t.Error("aliceGroup == nil")
|
|
|
|
return
|
|
|
|
}
|
2018-04-27 19:20:33 +00:00
|
|
|
|
|
|
|
fmt.Printf("Alice TimeLine:\n")
|
2018-04-27 19:20:33 +00:00
|
|
|
printAndVerifyTimeline(t, alicesGroup.GetTimeline())
|
2018-04-27 19:20:33 +00:00
|
|
|
|
2018-04-27 19:20:33 +00:00
|
|
|
bobsGroup := bob.Profile.GetGroupByGroupID(groupId)
|
|
|
|
fmt.Printf("bob Groups:\n")
|
|
|
|
for k := range bob.Profile.Groups {
|
|
|
|
fmt.Println(" " + k)
|
|
|
|
}
|
|
|
|
if bobsGroup == nil {
|
|
|
|
t.Error("bobGroup == nil")
|
|
|
|
return
|
|
|
|
}
|
2018-04-27 19:20:33 +00:00
|
|
|
fmt.Printf("Bob TimeLine:\n")
|
2018-04-27 19:20:33 +00:00
|
|
|
printAndVerifyTimeline(t, bobsGroup.GetTimeline())
|
2018-04-27 19:20:33 +00:00
|
|
|
|
|
|
|
if len(alicesGroup.Timeline.Messages) != 6 {
|
|
|
|
t.Errorf("Alice's timeline does not have all messages")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Todo: shutdown users and server
|
2018-03-30 21:16:51 +00:00
|
|
|
}
|