meowlib/endtoend_test.go

113 lines
3.3 KiB
Go
Raw Normal View History

2022-09-06 09:30:45 +02:00
package meowlib_test
2022-01-15 22:19:29 +01:00
import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"
2022-09-06 09:30:45 +02:00
"forge.redroom.link/yves/meowlib"
"forge.redroom.link/yves/meowlib/client"
2022-01-15 22:19:29 +01:00
)
func TestEndToEnd(t *testing.T) {
//
// Create my own identity
//
fmt.Println("Trying to load identity from file.")
2022-09-06 17:07:35 +02:00
Me, err := client.LoadIdentity("id.enc")
2022-01-15 22:19:29 +01:00
if err != nil {
fmt.Println("Failed : creating New identity...")
2022-09-06 17:07:35 +02:00
Me = client.CreateIdentity("myname")
2022-01-15 22:19:29 +01:00
//
// define my preferences (servers)
//
2022-09-06 17:07:35 +02:00
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"})
2022-01-15 22:19:29 +01:00
//
// create an invitation for a friend, I want him/her to know me as Bender
//
fmt.Println("Creating an invitation for the first friend...")
2022-09-06 17:07:35 +02:00
MyFirstFriend, invitation := Me.InvitePeer("Bender", "myfirstfriend", []int{1, 2})
2022-01-15 22:19:29 +01:00
// print my invitation
a, _ := json.Marshal(invitation)
fmt.Println(string(a))
// TODO : Convert invitation to QR Code
2022-03-01 10:37:11 +01:00
invitation.WritePng("invitation.png")
2022-01-15 22:19:29 +01:00
//
// Simulate peer invitation response : generate the friend's keypair
fmt.Println("Simulating first friend answer...")
2022-09-06 17:07:35 +02:00
var ReceivedContact meowlib.ContactCard
2022-09-02 12:07:21 +02:00
// Friend simulated invitation
2022-09-06 17:07:35 +02:00
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/"})
2022-09-02 12:07:21 +02:00
// end Friend simulated invitation
2022-08-29 15:40:29 +02:00
// End simulating contact invitation response
2022-01-15 22:19:29 +01:00
//
//
// Finalize the contact with the invitation response
//
2022-09-06 17:07:35 +02:00
Me.FinalizeInvitation(MyFirstFriend, &ReceivedContact)
err = Me.Save("id.enc")
2022-01-15 22:19:29 +01:00
if err != nil {
fmt.Println(err.Error())
}
2022-09-06 17:07:35 +02:00
a, _ = json.Marshal(Me)
2022-01-15 22:19:29 +01:00
ioutil.WriteFile("id.json", a, 0644)
fmt.Println(string(a))
2022-03-01 10:37:11 +01:00
2022-09-02 12:07:21 +02:00
// create message to simulated friend
sentmessage := "Hello friend!"
2022-09-06 17:07:35 +02:00
lookupK, EncMsg, Signature, Servers, err := MyFirstFriend.AsymEncryptMessage([]byte(sentmessage))
2022-09-02 12:07:21 +02:00
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"
2022-09-06 17:07:35 +02:00
decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), Signature)
2022-09-02 12:07:21 +02:00
if err2 != nil {
fmt.Println(err2.Error())
}
2022-09-06 17:07:35 +02:00
// 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))
2022-09-02 12:07:21 +02:00
}
2022-01-15 22:19:29 +01:00
}