129 lines
3.2 KiB
Go
129 lines
3.2 KiB
Go
package client
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func exists(filename string) bool {
|
|
if _, err := os.Stat(filename); err == nil {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
func createId() *Identity {
|
|
config := GetConfig()
|
|
config.IdentityFile = "test.id"
|
|
config.memoryPassword = "generalPassword"
|
|
// ! Extension to quickly open db : Debug only !
|
|
config.DbSuffix = ".sqlite"
|
|
id, err := CreateIdentity("myname")
|
|
if err != nil {
|
|
log.Fatal("CreateIdentity failed")
|
|
}
|
|
err = id.Save()
|
|
if err != nil {
|
|
log.Fatal("Save failed")
|
|
}
|
|
for i := range 10 {
|
|
var p Peer
|
|
p.Uid = uuid.New().String()
|
|
p.Name = "testName_" + strconv.Itoa(i)
|
|
p.MyEncryptionKp = meowlib.NewKeyPair()
|
|
p.MyIdentity = meowlib.NewKeyPair()
|
|
p.MyLookupKp = meowlib.NewKeyPair()
|
|
p.Name = "foo_" + strconv.Itoa(i)
|
|
p.ContactPublicKey = meowlib.NewKeyPair().Public
|
|
p.ContactEncryption = meowlib.NewKeyPair().Public
|
|
p.ContactLookupKey = meowlib.NewKeyPair().Public
|
|
p.MyPullServers = []string{"server1", "server2"}
|
|
//p.Contact.AddUrls([]string{"http:/127.0.0.1/meow", "tcp://localhost:1234"}) //todo add servers
|
|
id.Peers.StorePeer(&p)
|
|
}
|
|
return id
|
|
}
|
|
|
|
func TestLoad(t *testing.T) {
|
|
if exists("test.id") {
|
|
os.Remove("test.id")
|
|
}
|
|
id, err := LoadIdentity("test.id", "toto")
|
|
if err != nil {
|
|
id, err1 := CreateIdentity("myname")
|
|
if err1 != nil {
|
|
log.Fatal("CreateIdentity failed")
|
|
}
|
|
id.Save()
|
|
} else {
|
|
log.Println(id.Nickname)
|
|
}
|
|
id, err = LoadIdentity("test.id", "toto")
|
|
if err != nil {
|
|
log.Println(err.Error())
|
|
}
|
|
assert.Equal(t, err, nil, "2nd Load error")
|
|
assert.Equal(t, id.Nickname, "myname", "The two words should be the same.")
|
|
if exists("test.id") {
|
|
os.Remove("test.id")
|
|
}
|
|
}
|
|
|
|
func TestHidePeer(t *testing.T) {
|
|
/*
|
|
id := createId()
|
|
name := id.Peers[0].Name
|
|
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)
|
|
assert.Equal(t, id.unlockedHiddenPeers[0].Name, name)
|
|
|
|
if exists("test.id") {
|
|
os.Remove("test.id")
|
|
}
|
|
*/
|
|
}
|
|
|
|
// test GetRequestJobs
|
|
func TestGetRequestJobs(t *testing.T) {
|
|
// Create a mock Identity object
|
|
id := createId()
|
|
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")
|
|
}
|