meowlib/client/identity_test.go
2023-01-11 21:42:14 +01:00

65 lines
1.5 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 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 := CreateIdentity("myname")
id.Save()
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)
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, p.Name)
}