2022-09-06 09:30:45 +02:00
|
|
|
package meowlib_test
|
2022-01-15 22:19:29 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-12-19 12:15:10 +01:00
|
|
|
"os"
|
2022-01-15 22:19:29 +01:00
|
|
|
"testing"
|
2022-09-06 09:30:45 +02:00
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
|
|
"forge.redroom.link/yves/meowlib/client"
|
2022-09-25 10:16:39 +02:00
|
|
|
"forge.redroom.link/yves/meowlib/server"
|
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-11-28 20:48:42 +01:00
|
|
|
Me, err := client.LoadIdentity("id.enc", "Test")
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed : creating New identity...")
|
2022-09-19 14:09:32 +02:00
|
|
|
///////////////////////////
|
|
|
|
// Creating New Identity //
|
|
|
|
///////////////////////////
|
2022-09-06 17:07:35 +02:00
|
|
|
Me = client.CreateIdentity("myname")
|
2024-02-08 22:17:16 +01:00
|
|
|
|
2022-01-15 22:19:29 +01:00
|
|
|
// define my preferences (servers)
|
2024-02-08 22:17:16 +01:00
|
|
|
srv := client.Server{Name: "MyServer", Url: "http://127.0.0.1/meow/"}
|
|
|
|
Me.MessageServers.StoreServer(&srv)
|
|
|
|
srv = client.Server{Name: "MyServer", Url: "mqtt://127.0.0.1"}
|
|
|
|
Me.MessageServers.StoreServer(&srv)
|
|
|
|
srv = client.Server{Name: "MyServer", Url: "meow://127.0.0.1"}
|
|
|
|
Me.MessageServers.StoreServer(&srv)
|
2022-09-19 14:09:32 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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...")
|
2024-02-08 22:17:16 +01:00
|
|
|
peer, err := Me.InvitePeer("Bender", "myfirstfriend", []string{"http://127.0.0.1/meow/", "mqtt://127.0.0.1"}, "welcome, it's me!")
|
2022-11-30 21:31:01 +01:00
|
|
|
if err != nil {
|
|
|
|
println(err)
|
|
|
|
}
|
2023-11-14 16:32:50 +01:00
|
|
|
println(peer.Name)
|
2022-01-15 22:19:29 +01:00
|
|
|
// print my invitation
|
2023-12-31 18:43:55 +01:00
|
|
|
a, _ := json.Marshal(peer.MyContact)
|
2022-01-15 22:19:29 +01:00
|
|
|
fmt.Println(string(a))
|
|
|
|
// TODO : Convert invitation to QR Code
|
2023-12-31 18:43:55 +01:00
|
|
|
peer.MyContact.WritePng("invitation.png")
|
|
|
|
data, err := peer.MyContact.Compress()
|
2022-11-27 21:08:34 +01:00
|
|
|
if err != nil {
|
|
|
|
println(err)
|
|
|
|
}
|
2023-12-31 18:43:55 +01:00
|
|
|
peer.MyContact.WriteQr("qrcode.png")
|
2022-11-27 21:08:34 +01:00
|
|
|
println("Compressed contact card :", len(data))
|
2022-09-19 14:09:32 +02:00
|
|
|
///////////////////////////////////////
|
|
|
|
// 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
|
2023-12-31 18:43:55 +01:00
|
|
|
ReceivedContact.InvitationId = peer.MyContact.InvitationId
|
2022-09-19 13:26:27 +02:00
|
|
|
FriendServer1KP := meowlib.NewKeyPair()
|
2023-11-27 00:31:43 +01:00
|
|
|
FriendServer1 := meowlib.ServerCard{Name: "FriendServer1", Url: "http://myfriend.org/meow/", PublicKey: FriendServer1KP.Public, Description: "Fancy description"}
|
2022-09-19 13:26:27 +02:00
|
|
|
ReceivedContact.PullServers = append(ReceivedContact.PullServers, &FriendServer1)
|
2022-09-02 12:07:21 +02:00
|
|
|
|
2022-09-19 14:09:32 +02:00
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
// Finalize the contact with the invitation response //
|
|
|
|
///////////////////////////////////////////////////////
|
2022-11-27 21:08:34 +01:00
|
|
|
Me.FinalizeInvitation(&ReceivedContact)
|
2022-11-28 20:48:42 +01:00
|
|
|
err = Me.Save()
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2022-11-28 20:48:42 +01:00
|
|
|
}
|
2022-01-15 22:19:29 +01:00
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
a, _ = json.Marshal(Me)
|
2022-12-19 12:15:10 +01:00
|
|
|
os.WriteFile("id.json", a, 0644)
|
2022-01-15 22:19:29 +01:00
|
|
|
fmt.Println(string(a))
|
2022-09-19 14:09:32 +02:00
|
|
|
/////////////////////////////////////
|
|
|
|
// Create a message to that friend //
|
|
|
|
/////////////////////////////////////
|
2022-11-27 21:08:34 +01:00
|
|
|
MyFirstFriend := Me.Peers[0]
|
2022-09-19 14:09:32 +02:00
|
|
|
textmessage := "Hello friend!"
|
|
|
|
// Creating User message
|
2022-11-30 21:31:01 +01:00
|
|
|
usermessage, err := MyFirstFriend.BuildSimpleUserMessage([]byte(textmessage))
|
2022-09-19 14:09:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-10-22 14:41:48 +02:00
|
|
|
serializedMessage, err := MyFirstFriend.SerializeUserMessage(usermessage)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-09-19 14:09:32 +02:00
|
|
|
// Encrypting it
|
2022-12-19 20:15:53 +01:00
|
|
|
enc, err := MyFirstFriend.AsymEncryptMessage(serializedMessage)
|
2022-09-02 12:07:21 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-12-19 20:15:53 +01:00
|
|
|
|
2022-09-19 14:09:32 +02:00
|
|
|
// Packing it
|
2022-12-19 20:15:53 +01:00
|
|
|
packedMsg := MyFirstFriend.PackUserMessage(enc.Data, enc.Signature)
|
2022-09-18 18:09:27 +02:00
|
|
|
|
2024-02-08 22:17:16 +01:00
|
|
|
intS1, err := Me.MessageServers.LoadServer("http://127.0.0.1/meow/")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-09-19 14:09:32 +02:00
|
|
|
// Creating Server message for transporting the user message
|
2022-12-03 00:05:28 +01:00
|
|
|
toServerMessage, err := intS1.BuildMessageSendingMessage(packedMsg)
|
2022-09-19 14:09:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-09-19 13:26:27 +02:00
|
|
|
// Encrypting it
|
2022-12-19 20:15:53 +01:00
|
|
|
encToServer, err := intS1.AsymEncryptMessage(toServerMessage)
|
2022-09-19 14:09:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-09-19 13:26:27 +02:00
|
|
|
// Packing it
|
2022-12-19 20:15:53 +01:00
|
|
|
protoPackedServerMsg, err := intS1.PackServerMessage(encToServer.Data, encToServer.Signature)
|
2022-09-19 14:09:32 +02:00
|
|
|
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
|
2022-09-19 14:09:32 +02:00
|
|
|
// 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))
|
|
|
|
|
2022-09-19 14:09:32 +02:00
|
|
|
///////////////////////////////////////
|
|
|
|
// Simulating server side processing //
|
|
|
|
///////////////////////////////////////
|
2022-09-25 10:16:39 +02:00
|
|
|
var server1 server.Identity
|
2024-02-07 16:08:24 +01:00
|
|
|
server1.ServerName = intS1.Name
|
2022-09-25 10:16:39 +02:00
|
|
|
server1.ServerKp = FriendServer1KP
|
2024-02-07 16:08:24 +01:00
|
|
|
server1.ServerDesc = intS1.Description
|
2022-09-25 10:16:39 +02:00
|
|
|
// Unpack
|
|
|
|
srv_from, srv_encmsg, srv_signature, err := server1.UnpackReceived(protoPackedServerMsg)
|
2022-10-22 14:41:48 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-09-25 10:16:39 +02:00
|
|
|
// Decrypt
|
|
|
|
srv_clear, err := server1.AsymDecryptMessage(srv_from, srv_encmsg, srv_signature)
|
2022-10-22 14:41:48 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-09-25 10:16:39 +02:00
|
|
|
// Decode msg
|
2022-10-22 14:41:48 +02:00
|
|
|
srv_msg, err := server1.DeserializeToServerMessage(srv_clear)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-09-25 10:16:39 +02:00
|
|
|
// Response : Ack received message
|
|
|
|
srv_fromServerMessage, err := server1.BuildSimpleAckResponseMessage(srv_msg.Uuid)
|
2022-10-22 14:41:48 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
encoded_srv_fromServerMessage, err := server1.SerializeFromServerMessage(srv_fromServerMessage)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-12-19 20:15:53 +01:00
|
|
|
srv_resp, err := server1.AsymEncryptMessage(srv_from, encoded_srv_fromServerMessage)
|
2022-10-22 14:41:48 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-12-19 20:15:53 +01:00
|
|
|
resp, err := server1.PackForSending(srv_resp.Data, srv_resp.Signature)
|
2022-10-22 14:41:48 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2022-10-15 09:14:13 +02:00
|
|
|
print(resp)
|
2022-09-19 14:09:32 +02:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Back to client, decoding server response //
|
|
|
|
//////////////////////////////////////////////
|
2022-09-19 13:26:27 +02:00
|
|
|
|
2022-09-25 10:16:39 +02:00
|
|
|
// server unpack
|
|
|
|
|
|
|
|
// server decrypt
|
|
|
|
|
|
|
|
// server decode protobuf
|
|
|
|
|
|
|
|
// user unpack
|
|
|
|
|
|
|
|
// user decrypt
|
2023-01-11 21:42:14 +01:00
|
|
|
/*decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(enc.Data), enc.Signature)
|
2022-09-02 12:07:21 +02:00
|
|
|
if err2 != nil {
|
|
|
|
fmt.Println(err2.Error())
|
|
|
|
}
|
2023-01-11 21:42:14 +01:00
|
|
|
fmt.Println(decMess)*/
|
2022-09-25 10:16:39 +02:00
|
|
|
// user decode protobuf
|
2022-09-06 17:07:35 +02:00
|
|
|
|
2022-09-02 12:07:21 +02:00
|
|
|
}
|
2022-01-15 22:19:29 +01:00
|
|
|
|
|
|
|
}
|