meowlib/endtoend_test.go

130 lines
4.4 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...")
///////////////////////////
// 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"})
////////////////////////////////////////////////////////////////////////////
// Create an invitation for a friend, I want him/her to know me as Bender //
////////////////////////////////////////////////////////////////////////////
2022-01-15 22:19:29 +01:00
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")
///////////////////////////////////////
// Simulate peer invitation response //
///////////////////////////////////////
2022-01-15 22:19:29 +01:00
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
2022-09-19 13:26:27 +02:00
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)
2022-09-02 12:07:21 +02: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))
/////////////////////////////////////
// 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())
}
// Encrypting it
EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage(usermessage)
2022-09-02 12:07:21 +02:00
if err != nil {
fmt.Println(err.Error())
}
2022-09-19 13:26:27 +02:00
fmt.Println(len(FriendServers))
// Packing it
2022-09-19 13:26:27 +02:00
packedMsg := MyFirstFriend.Pack(EncMsg, EncMsgSignature)
2022-09-19 13:26:27 +02:00
srv := FriendServers[0]
intS1 := client.InternalServerFromServer(srv)
2022-09-02 12:07:21 +02:00
// Creating Server message for transporting the user message
2022-09-19 13:26:27 +02:00
toServerMessage, err := intS1.BuildMessageSendingMessage(&packedMsg)
if err != nil {
fmt.Println(err.Error())
}
2022-09-19 13:26:27 +02:00
// Encrypting it
encToServerMessage, encToServerMessageSignature, err := intS1.AsymEncryptMessage(toServerMessage)
if err != nil {
fmt.Println(err.Error())
}
2022-09-19 13:26:27 +02:00
// 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)
2022-09-19 13:26:27 +02:00
// You have to implement the netwok layer
// Just FYI printing final byte array size. Those bytes will be sent over the network.
2022-09-19 13:26:27 +02:00
println(len(protoPackedServerMsg))
///////////////////////////////////////
// Simulating server side processing //
///////////////////////////////////////
//////////////////////////////////////////////
// Back to client, decoding server response //
//////////////////////////////////////////////
2022-09-19 13:26:27 +02:00
//
decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), EncMsgSignature)
2022-09-02 12:07:21 +02:00
if err2 != nil {
fmt.Println(err2.Error())
}
fmt.Println(decMess)
2022-09-06 17:07:35 +02:00
2022-09-02 12:07:21 +02:00
}
2022-01-15 22:19:29 +01:00
}