2022-09-06 09:30:45 +02:00
|
|
|
package client
|
2022-01-15 22:19:29 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2022-11-28 20:48:42 +01:00
|
|
|
"os"
|
2024-02-12 20:02:02 +01:00
|
|
|
"strconv"
|
2022-01-15 22:19:29 +01:00
|
|
|
"testing"
|
2022-09-02 12:07:21 +02:00
|
|
|
|
2023-01-11 21:42:14 +01:00
|
|
|
"forge.redroom.link/yves/meowlib"
|
2022-09-02 12:07:21 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-01-15 22:19:29 +01:00
|
|
|
)
|
|
|
|
|
2022-11-28 20:48:42 +01:00
|
|
|
func exists(filename string) bool {
|
|
|
|
if _, err := os.Stat(filename); err == nil {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
2022-01-15 22:19:29 +01:00
|
|
|
}
|
|
|
|
|
2023-02-15 22:08:17 +01:00
|
|
|
func createId() *Identity {
|
|
|
|
config := GetConfig()
|
|
|
|
config.IdentityFile = "test.id"
|
|
|
|
config.memoryPassword = "generalPassword"
|
|
|
|
// ! Extension to quickly open db : Debug only !
|
|
|
|
config.DbSuffix = ".sqlite"
|
2024-03-29 18:26:41 +01:00
|
|
|
id, err := CreateIdentity("myname")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("CreateIdentity failed")
|
|
|
|
}
|
|
|
|
err = id.Save()
|
2023-02-15 22:08:17 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Save failed")
|
|
|
|
}
|
|
|
|
var p Peer
|
|
|
|
p.Name = "testName"
|
|
|
|
p.MyEncryptionKp = meowlib.NewKeyPair()
|
|
|
|
p.MyIdentity = meowlib.NewKeyPair()
|
|
|
|
p.MyLookupKp = meowlib.NewKeyPair()
|
2024-02-10 22:36:25 +01:00
|
|
|
p.Name = "foo"
|
|
|
|
p.ContactPublicKey = p.MyLookupKp.Public
|
|
|
|
p.ContactEncryption = p.MyIdentity.Public
|
|
|
|
p.ContactLookupKey = p.MyEncryptionKp.Public
|
|
|
|
//p.Contact.AddUrls([]string{"http:/127.0.0.1/meow", "tcp://localhost:1234"}) //todo add servers
|
2024-03-02 10:07:59 +01:00
|
|
|
id.Peers = append(id.Peers, &p)
|
2023-02-15 22:08:17 +01:00
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2022-01-15 22:19:29 +01:00
|
|
|
func TestLoad(t *testing.T) {
|
2022-11-28 20:48:42 +01:00
|
|
|
if exists("test.id") {
|
|
|
|
os.Remove("test.id")
|
|
|
|
}
|
|
|
|
id, err := LoadIdentity("test.id", "toto")
|
|
|
|
if err != nil {
|
2024-03-29 18:26:41 +01:00
|
|
|
id, err1 := CreateIdentity("myname")
|
|
|
|
if err1 != nil {
|
|
|
|
log.Fatal("CreateIdentity failed")
|
|
|
|
}
|
2022-11-28 20:48:42 +01:00
|
|
|
id.Save()
|
2023-01-07 00:39:05 +01:00
|
|
|
} else {
|
|
|
|
log.Println(id.Nickname)
|
2022-11-28 20:48:42 +01:00
|
|
|
}
|
|
|
|
id, err = LoadIdentity("test.id", "toto")
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
2022-09-02 12:07:21 +02:00
|
|
|
log.Println(err.Error())
|
2022-01-15 22:19:29 +01:00
|
|
|
}
|
2022-11-28 20:48:42 +01:00
|
|
|
assert.Equal(t, err, nil, "2nd Load error")
|
2022-09-02 12:07:21 +02:00
|
|
|
assert.Equal(t, id.Nickname, "myname", "The two words should be the same.")
|
2022-11-28 20:48:42 +01:00
|
|
|
if exists("test.id") {
|
|
|
|
os.Remove("test.id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:42:14 +01:00
|
|
|
func TestHidePeer(t *testing.T) {
|
2023-02-15 22:08:17 +01:00
|
|
|
id := createId()
|
|
|
|
name := id.Peers[0].Name
|
2023-01-11 21:42:14 +01:00
|
|
|
assert.Equal(t, len(id.Peers), 1)
|
|
|
|
h := len(id.HiddenPeers)
|
|
|
|
id.HidePeer(0, "mypassword")
|
|
|
|
assert.Equal(t, len(id.Peers), 0)
|
|
|
|
assert.Equal(t, len(id.HiddenPeers), h+1)
|
|
|
|
id.TryUnlockHidden("mypassword")
|
|
|
|
assert.Equal(t, len(id.unlockedHiddenPeers), 1)
|
2023-02-15 22:08:17 +01:00
|
|
|
assert.Equal(t, id.unlockedHiddenPeers[0].Name, name)
|
|
|
|
if exists("test.id") {
|
|
|
|
os.Remove("test.id")
|
|
|
|
}
|
2022-01-15 22:19:29 +01:00
|
|
|
}
|
2024-02-12 20:02:02 +01:00
|
|
|
|
|
|
|
// test GetRequestJobs
|
|
|
|
func TestGetRequestJobs(t *testing.T) {
|
|
|
|
// Create a mock Identity object
|
|
|
|
id := &Identity{
|
2024-03-02 10:07:59 +01:00
|
|
|
Peers: []*Peer{
|
2024-02-12 20:02:02 +01:00
|
|
|
{
|
|
|
|
MyPullServers: []string{"server1", "server2"},
|
|
|
|
MyLookupKp: meowlib.NewKeyPair(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MyPullServers: []string{"server3", "server4"},
|
|
|
|
MyLookupKp: meowlib.NewKeyPair(),
|
|
|
|
},
|
|
|
|
},
|
2024-03-02 10:07:59 +01:00
|
|
|
unlockedHiddenPeers: []*Peer{
|
2024-02-12 20:02:02 +01:00
|
|
|
{
|
|
|
|
MyPullServers: []string{"server5", "server6"},
|
|
|
|
MyLookupKp: meowlib.NewKeyPair(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
id.MessageServers = ServerStorage{
|
|
|
|
DbFile: "test.db",
|
|
|
|
}
|
|
|
|
GetConfig().SetMemPass("test")
|
|
|
|
GetConfig().SetIdentity(id)
|
|
|
|
for i := 1; i < 10; i++ {
|
|
|
|
// initialize a Server with name "server+i"
|
|
|
|
srv := CreateServerFromUrl("server" + strconv.Itoa(i))
|
|
|
|
id.MessageServers.StoreServer(srv)
|
|
|
|
}
|
|
|
|
// Call GetRequestJobs
|
|
|
|
jobs := id.GetRequestJobs()
|
|
|
|
|
|
|
|
// Check that the returned list is as expected
|
|
|
|
assert.Equal(t, 6, len(jobs), "Expected 6 jobs")
|
|
|
|
|
|
|
|
// Check that each job has the correct server and lookup keys
|
|
|
|
for _, job := range jobs {
|
|
|
|
//fmt.Println(job.Server.GetUid(), job.LookupKeys)
|
|
|
|
assert.Contains(t, []string{"server1", "server2", "server3", "server4", "server5", "server6"}, job.Server.GetUid(), "Unexpected server UID")
|
|
|
|
assert.Len(t, job.LookupKeys, 1, "Expected 1 lookup key per job")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
// recursively remove the test.db folder
|
|
|
|
os.RemoveAll("test.db")
|
|
|
|
}
|