meowlib/endtoend_test.go
2022-09-19 13:26:27 +02:00

105 lines
3.3 KiB
Go

package meowlib_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"
"forge.redroom.link/yves/meowlib"
"forge.redroom.link/yves/meowlib/client"
)
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...")
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 : generate the friend's keypair
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)
// end Friend simulated invitation
// 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 message to simulated friend
sentmessage := "Hello friend!"
EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage([]byte(sentmessage))
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(len(FriendServers))
packedMsg := MyFirstFriend.Pack(EncMsg, EncMsgSignature)
srv := FriendServers[0]
intS1 := client.InternalServerFromServer(srv)
// Creating Server message
toServerMessage, err := intS1.BuildMessageSendingMessage(&packedMsg)
// Encrypting it
encToServerMessage, encToServerMessageSignature, err := intS1.AsymEncryptMessage(toServerMessage)
// Packing it
protoPackedServerMsg, err := intS1.PackServerMessage(encToServerMessage, encToServerMessageSignature)
// Sending to server => fake action (network protocol trnasfer are not part of meowlib)
// You have to implement the netwok layer
println(len(protoPackedServerMsg))
//
// Simulating server side processing
//
//
// Back to client, decoding server response
//
decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), EncMsgSignature)
if err2 != nil {
fmt.Println(err2.Error())
}
fmt.Println(decMess)
}
}