meowlib/client/identity_test.go
ycc 6f2a65dac9
Some checks failed
continuous-integration/drone/push Build is failing
storage get messages + config improve + tests
2023-02-15 22:08:17 +01:00

82 lines
1.9 KiB
Go

package client
import (
"log"
"os"
"testing"
"forge.redroom.link/yves/meowlib"
"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 := 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()
p.Contact.Name = "foo"
p.Contact.ContactPublicKey = p.MyLookupKp.Public
p.Contact.EncryptionPublicKey = p.MyIdentity.Public
p.Contact.LookupPublicKey = p.MyEncryptionKp.Public
p.Contact.AddUrls([]string{"http:/127.0.0.1/meow", "tcp://localhost:1234"})
id.Peers = append(id.Peers, 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 := CreateIdentity("myname")
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")
}
}