meowlib/endtoend_test.go

180 lines
5.9 KiB
Go

package meowlib_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"
"forge.redroom.link/yves/meowlib"
"forge.redroom.link/yves/meowlib/client"
"forge.redroom.link/yves/meowlib/server"
)
func TestEndToEnd(t *testing.T) {
//
// Create my own identity
//
fmt.Println("Trying to load identity from file.")
Me, err := client.LoadIdentity("id.enc")
if err != nil {
fmt.Println("Failed : creating New identity...")
///////////////////////////
// Creating New Identity //
///////////////////////////
Me = client.CreateIdentity("myname")
// define my preferences (servers)
Me.MessageServers.Name = "Message Servers"
Me.MessageServers.AddUrls([]string{"http://127.0.0.1/meow/", "mqtt://127.0.0.1", "meow://127.0.0.1"})
////////////////////////////////////////////////////////////////////////////
// Create an invitation for a friend, I want him/her to know me as Bender //
////////////////////////////////////////////////////////////////////////////
fmt.Println("Creating an invitation for the first friend...")
MyFirstFriend, invitation := Me.InvitePeer("Bender", "myfirstfriend", []int{1, 2})
// print my invitation
a, _ := json.Marshal(invitation)
fmt.Println(string(a))
// TODO : Convert invitation to QR Code
invitation.WritePng("invitation.png")
///////////////////////////////////////
// Simulate peer invitation response //
///////////////////////////////////////
fmt.Println("Simulating first friend answer...")
var ReceivedContact meowlib.ContactCard
// Friend simulated invitation
FirstFriendContactKp := meowlib.NewKeyPair()
FirstFriendEncryptionKp := meowlib.NewKeyPair()
FirstFriendLookupKp := meowlib.NewKeyPair()
ReceivedContact.Name = "I'm the friend"
ReceivedContact.ContactPublicKey = FirstFriendContactKp.Public
ReceivedContact.EncryptionPublicKey = FirstFriendEncryptionKp.Public
ReceivedContact.LookupPublicKey = FirstFriendLookupKp.Public
FriendServer1KP := meowlib.NewKeyPair()
FriendServer1 := meowlib.Server{Name: "FriendServer1", Url: "http://myfriend.org/meow/", PublicKey: FriendServer1KP.Public, Description: "Fancy description", ConfidenceLevel: 1}
ReceivedContact.PullServers = append(ReceivedContact.PullServers, &FriendServer1)
///////////////////////////////////////////////////////
// Finalize the contact with the invitation response //
///////////////////////////////////////////////////////
Me.FinalizeInvitation(MyFirstFriend, &ReceivedContact)
/*err = Me.Save("id.enc")
if err != nil {
fmt.Println(err.Error())
}*/
a, _ = json.Marshal(Me)
ioutil.WriteFile("id.json", a, 0644)
fmt.Println(string(a))
/////////////////////////////////////
// Create a message to that friend //
/////////////////////////////////////
textmessage := "Hello friend!"
// Creating User message
usermessage, err := MyFirstFriend.BuildSimpleUserUMessage([]byte(textmessage))
if err != nil {
fmt.Println(err.Error())
}
serializedMessage, err := MyFirstFriend.SerializeUserMessage(usermessage)
if err != nil {
fmt.Println(err.Error())
}
// Encrypting it
EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage(serializedMessage)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(len(FriendServers))
// Packing it
packedMsg := MyFirstFriend.PackUserMessage(EncMsg, EncMsgSignature)
srv := FriendServers[0]
intS1 := client.InternalServerFromServer(srv)
// Creating Server message for transporting the user message
toServerMessage, err := intS1.BuildMessageSendingMessage(&packedMsg)
if err != nil {
fmt.Println(err.Error())
}
// Encrypting it
encToServerMessage, encToServerMessageSignature, err := intS1.AsymEncryptMessage(toServerMessage)
if err != nil {
fmt.Println(err.Error())
}
// Packing it
protoPackedServerMsg, err := intS1.PackServerMessage(encToServerMessage, encToServerMessageSignature)
if err != nil {
fmt.Println(err.Error())
}
///////////////////////
// Sending to server //
///////////////////////
//=> fake action (network protocol transfer are not part of meowlib)
// You have to implement the netwok layer
// Just FYI printing final byte array size. Those bytes will be sent over the network.
println(len(protoPackedServerMsg))
///////////////////////////////////////
// Simulating server side processing //
///////////////////////////////////////
var server1 server.Identity
server1.ServerName = intS1.ServerData.Name
server1.ServerKp = FriendServer1KP
server1.ServerDesc = intS1.ServerData.Description
// Unpack
srv_from, srv_encmsg, srv_signature, err := server1.UnpackReceived(protoPackedServerMsg)
if err != nil {
fmt.Println(err.Error())
}
// Decrypt
srv_clear, err := server1.AsymDecryptMessage(srv_from, srv_encmsg, srv_signature)
if err != nil {
fmt.Println(err.Error())
}
// Decode msg
srv_msg, err := server1.DeserializeToServerMessage(srv_clear)
if err != nil {
fmt.Println(err.Error())
}
// Response : Ack received message
srv_fromServerMessage, err := server1.BuildSimpleAckResponseMessage(srv_msg.Uuid)
if err != nil {
fmt.Println(err.Error())
}
encoded_srv_fromServerMessage, err := server1.SerializeFromServerMessage(srv_fromServerMessage)
if err != nil {
fmt.Println(err.Error())
}
srv_resp, srv_respsig, err := server1.AsymEncryptMessage(srv_from, encoded_srv_fromServerMessage)
if err != nil {
fmt.Println(err.Error())
}
resp, err := server1.PackForSending(srv_resp, srv_respsig)
if err != nil {
fmt.Println(err.Error())
}
print(resp)
//////////////////////////////////////////////
// Back to client, decoding server response //
//////////////////////////////////////////////
// server unpack
// server decrypt
// server decode protobuf
// user unpack
// user decrypt
decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), EncMsgSignature)
if err2 != nil {
fmt.Println(err2.Error())
}
fmt.Println(decMess)
// user decode protobuf
}
}