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"
|
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"
|
|
|
|
id := CreateIdentity("myname")
|
|
|
|
err := id.Save()
|
|
|
|
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
|
2023-02-15 22:08:17 +01:00
|
|
|
id.Peers = append(id.Peers, p)
|
|
|
|
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 {
|
|
|
|
id := CreateIdentity("myname")
|
|
|
|
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
|
|
|
}
|