meowlib/endtoend_test.go
2022-09-06 09:30:45 +02:00

93 lines
2.7 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, MsgSignature, Servers, err := myFirstFriend.CreateMessage([]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 := meowlib.DecryptAndCheck(myFirstFriend.EncryptionKp.Private, myFirstFriend.Contact.EncryptionPublicKey, []byte(EncMsg), MsgSignature)
if err2 != nil {
fmt.Println(err2.Error())
}
fmt.Println(decMess)
//
}
}