2024-02-08 22:17:16 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2024-02-12 20:02:02 +01:00
|
|
|
"os"
|
2024-02-08 22:17:16 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
|
|
)
|
|
|
|
|
2024-02-10 11:32:44 +01:00
|
|
|
func TestGetUid(t *testing.T) {
|
|
|
|
srv := Server{
|
|
|
|
Name: "test",
|
|
|
|
Url: "http://127.0.0.1:8080",
|
|
|
|
PublicKey: meowlib.NewKeyPair().Public,
|
|
|
|
}
|
|
|
|
uid := srv.GetUid()
|
|
|
|
if uid != "http://127.0.0.1:8080" {
|
|
|
|
log.Fatal("uid not correct")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 22:17:16 +01:00
|
|
|
func TestStoreServer(t *testing.T) {
|
2024-06-05 14:45:01 +02:00
|
|
|
createId()
|
2024-02-08 22:17:16 +01:00
|
|
|
ss := ServerStorage{DbFile: "test.db"}
|
|
|
|
srv := Server{
|
|
|
|
Name: "test",
|
|
|
|
Url: "http://127.0.0.1",
|
|
|
|
PublicKey: meowlib.NewKeyPair().Public,
|
|
|
|
}
|
|
|
|
err := ss.StoreServer(&srv)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-02-10 11:32:44 +01:00
|
|
|
|
2024-02-08 22:17:16 +01:00
|
|
|
sout, err := ss.LoadServer(srv.GetServerCard().GetUid())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if sout == nil {
|
|
|
|
log.Fatal("server not found")
|
|
|
|
}
|
|
|
|
if sout.Name != srv.Name {
|
|
|
|
log.Fatal("name not found")
|
|
|
|
}
|
2024-02-12 20:02:02 +01:00
|
|
|
// Clean up
|
|
|
|
// recursively remove the test.db folder
|
|
|
|
os.RemoveAll("test.db")
|
2024-02-08 22:17:16 +01:00
|
|
|
}
|
2024-02-10 11:32:44 +01:00
|
|
|
|
|
|
|
func TestLoadServersFromUids(t *testing.T) {
|
2024-06-05 14:45:01 +02:00
|
|
|
createId()
|
2024-02-10 11:32:44 +01:00
|
|
|
GetConfig().SetMemPass("test")
|
|
|
|
ss := ServerStorage{DbFile: "test.db"}
|
|
|
|
srv := Server{
|
|
|
|
Name: "test",
|
|
|
|
Url: "http://localhost:8080",
|
|
|
|
PublicKey: meowlib.NewKeyPair().Public,
|
|
|
|
}
|
|
|
|
err := ss.StoreServer(&srv)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
sout, err := ss.LoadServersFromUids([]string{srv.GetServerCard().GetUid()})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if sout == nil {
|
|
|
|
log.Fatal("server not found")
|
|
|
|
}
|
|
|
|
if sout[0].Name != srv.Name {
|
|
|
|
log.Fatal("name not found")
|
|
|
|
}
|
2024-02-12 20:02:02 +01:00
|
|
|
// Clean up
|
|
|
|
// recursively remove the test.db folder
|
|
|
|
os.RemoveAll("test.db")
|
2024-02-10 11:32:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadServerCardsFromUids(t *testing.T) {
|
2024-06-05 14:45:01 +02:00
|
|
|
createId()
|
2024-02-10 11:32:44 +01:00
|
|
|
ss := ServerStorage{DbFile: "test.db"}
|
|
|
|
srv := Server{
|
|
|
|
Name: "test",
|
|
|
|
Url: "http://localhost:8080",
|
|
|
|
PublicKey: meowlib.NewKeyPair().Public,
|
|
|
|
}
|
|
|
|
err := ss.StoreServer(&srv)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
sout, err := ss.LoadServerCardsFromUids([]string{srv.GetServerCard().GetUid()})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if sout == nil {
|
|
|
|
log.Fatal("server not found")
|
|
|
|
}
|
|
|
|
if sout[0].Name != srv.Name {
|
|
|
|
log.Fatal("name not found")
|
|
|
|
}
|
2024-02-12 20:02:02 +01:00
|
|
|
// Clean up
|
|
|
|
// recursively remove the test.db folder
|
|
|
|
os.RemoveAll("test.db")
|
2024-02-10 11:32:44 +01:00
|
|
|
}
|
2024-06-05 14:45:01 +02:00
|
|
|
|
|
|
|
func TestServerExists(t *testing.T) {
|
|
|
|
createId()
|
|
|
|
ss := ServerStorage{DbFile: "test.db"}
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
Name: "test",
|
|
|
|
Url: "http://localhost:8080",
|
|
|
|
PublicKey: meowlib.NewKeyPair().Public,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if server exists before storing it
|
|
|
|
exists, err := ss.ServerExists(server)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to check if server exists: %v", err)
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
t.Errorf("Server exists before storing it")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the server
|
|
|
|
err = ss.StoreServer(server)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to store server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if server exists after storing it
|
|
|
|
exists, err = ss.ServerExists(server)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to check if server exists: %v", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
t.Errorf("Server does not exist after storing it")
|
|
|
|
}
|
|
|
|
// Clean up
|
|
|
|
// recursively remove the test.db folder
|
|
|
|
os.RemoveAll("test.db")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreServerIfNotExists(t *testing.T) {
|
|
|
|
createId()
|
|
|
|
ss := ServerStorage{DbFile: "test.db"}
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
Name: "test",
|
|
|
|
Url: "http://localhost:8080",
|
|
|
|
PublicKey: meowlib.NewKeyPair().Public,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if server exists before storing it
|
|
|
|
exists, err := ss.ServerExists(server)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to check if server exists: %v", err)
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
t.Errorf("Server exists before storing it")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the server if it does not exist
|
|
|
|
err = ss.StoreServerIfNotExists(server)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to store server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if server exists after storing it
|
|
|
|
exists, err = ss.ServerExists(server)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to check if server exists: %v", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
t.Errorf("Server does not exist after storing it")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
// recursively remove the test.db folder
|
|
|
|
os.RemoveAll("test.db")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreServerIfNotExists_ServerExists(t *testing.T) {
|
|
|
|
createId()
|
|
|
|
ss := ServerStorage{DbFile: "test.db"}
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
Name: "test",
|
|
|
|
Url: "http://localhost:8080",
|
|
|
|
PublicKey: meowlib.NewKeyPair().Public,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the server
|
|
|
|
err := ss.StoreServer(server)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to store server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the server again with a different public key
|
|
|
|
newServer := &Server{
|
|
|
|
Name: "test",
|
|
|
|
Url: "http://localhost:8080",
|
|
|
|
PublicKey: meowlib.NewKeyPair().Public,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ss.StoreServerIfNotExists(newServer)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to store server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the server and check if the public key has not changed
|
|
|
|
storedServer, err := ss.LoadServer(server.GetServerCard().GetUid())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to get server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if storedServer.PublicKey != server.PublicKey {
|
|
|
|
t.Errorf("Public key was modified")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
// recursively remove the test.db folder
|
|
|
|
os.RemoveAll("test.db")
|
|
|
|
}
|