meowlib/endtoend_test.go
2022-09-06 17:07:35 +02:00

114 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
ReceivedContact.AddUrls([]string{"http://myfriend.org/meow/"})
// end Friend simulated invitation
// End simulating contact invitation response
//
//
// 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!"
lookupK, EncMsg, Signature, Servers, err := MyFirstFriend.AsymEncryptMessage([]byte(sentmessage))
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(lookupK)
fmt.Println(len(Servers))
// simulated friend decoding the message
//ReadMessage
// simulates if peer can decrypt my message
//Message := "toto"
//Signature := "test"
decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), Signature)
if err2 != nil {
fmt.Println(err2.Error())
}
fmt.Println("--- myFriend.decryptedMess = ", string(decMess))
// simulates a new server to send a message to
var intS1 client.InternalServer
intS1.ServerData.Name = "IntS1"
intS1.ServerData.Description = "Internal Serveur 1"
intS1.Me = meowlib.NewKeyPair()
intS1.ServerData.Url = "http://myfriend.org/meow/"
KP := meowlib.NewKeyPair()
intS1.ServerData.PublicKey = KP.Public
// sends a message to server 1
SrvEncrypted, SrvSign, err3 := intS1.AsymEncryptMessage([]byte(sentmessage))
if err3 != nil {
fmt.Println(err3.Error())
}
// tests simulated server decrypted message
SrvDecrypted, err4 := intS1.AsymDecryptMessage(SrvEncrypted, SrvSign)
if err4 != nil {
fmt.Println(err4.Error())
}
fmt.Println("--- SrvDecryptedMess = ", string(SrvDecrypted))
}
}