From af55df1ff56c578b4b6c48c402a0797b892ab4f2 Mon Sep 17 00:00:00 2001 From: ycc Date: Wed, 5 Jun 2024 14:45:01 +0200 Subject: [PATCH] store unknown servers in the invitation process answer and finalize + server storage fixes and testing --- client/identity.go | 5 +- client/peerstorage_test.go | 13 +++- client/serverstorage.go | 33 +++++++++ client/serverstorage_test.go | 125 ++++++++++++++++++++++++++++++++++- 4 files changed, 172 insertions(+), 4 deletions(-) diff --git a/client/identity.go b/client/identity.go index c45195f..85e20ab 100644 --- a/client/identity.go +++ b/client/identity.go @@ -135,6 +135,7 @@ func (id *Identity) AnswerInvitation(MyName string, ContactName string, MessageS peer.InvitationMessage = ReceivedContact.InvitationMessage for srv := range ReceivedContact.PullServers { peer.ContactPullServers = append(peer.ContactPullServers, ReceivedContact.PullServers[srv].GetUid()) + id.MessageServers.StoreServerIfNotExists(CreateServerFromUid(ReceivedContact.PullServers[srv].GetUid())) } /* for _, i := range MessageServerIdxs { srv := id.MessageServers.Servers[i].GetServerCard() @@ -169,7 +170,9 @@ func (id *Identity) FinalizeInvitation(ReceivedContact *meowlib.ContactCard) err } } return errors.New("no matching contact found for invitationId " + ReceivedContact.InvitationId)*/ - + for srv := range ReceivedContact.PullServers { + id.MessageServers.StoreServerIfNotExists(CreateServerFromUid(ReceivedContact.PullServers[srv].GetUid())) + } return id.Peers.FinalizeInvitation(ReceivedContact) } diff --git a/client/peerstorage_test.go b/client/peerstorage_test.go index e89aa64..7e79f84 100644 --- a/client/peerstorage_test.go +++ b/client/peerstorage_test.go @@ -30,7 +30,11 @@ func TestStorePeer(t *testing.T) { } peer := &Peer{ - Uid: uuid.New().String(), + Uid: uuid.New().String(), + Name: "testName", + InvitationId: "testInvitationId", + MyName: "testMyName", + ContactPublicKey: "testContactPublicKey", } err := ps.StorePeer(peer) @@ -46,4 +50,11 @@ func TestStorePeer(t *testing.T) { if len(peers) != 1 { t.Errorf("Expected 1 peer, got %d", len(peers)) } + if peers[0].Uid != peer.Uid { + t.Errorf("Expected peer to have uid %s, got %s", peer.Uid, peers[0].Uid) + } + if peers[0].MyName != peer.MyName { + t.Errorf("Expected peer to have MyName %s, got %s", peer.MyName, peers[0].MyName) + } + } diff --git a/client/serverstorage.go b/client/serverstorage.go index 934b0c9..072b05e 100644 --- a/client/serverstorage.go +++ b/client/serverstorage.go @@ -55,6 +55,39 @@ func (ss *ServerStorage) StoreServer(sc *Server) error { } +// Check if a server exists in a badger database with Server.GetUid() as key +func (ss *ServerStorage) ServerExists(sc *Server) (bool, error) { + err := ss.open() + if err != nil { + return false, err + } + defer ss.close() + + shakey := sha256.Sum256([]byte(sc.GetServerCard().GetUid())) + key := shakey[:] + // check if key exists in badger database + err = ss.db.View(func(txn *badger.Txn) error { + _, err := txn.Get(key) + return err + }) // Add a comma here + if err != nil { // key does not exist + return false, nil + } + return true, nil +} + +// Store a server in a badger database with Server.GetUid() as key if it is not already there +func (ss *ServerStorage) StoreServerIfNotExists(sc *Server) error { + exists, err := ss.ServerExists(sc) + if err != nil { + return err + } + if !exists { + return ss.StoreServer(sc) + } + return nil +} + // LoadServer function loads a Server from a badger database with Server.GetUid() as key func (ss *ServerStorage) LoadServer(uid string) (*Server, error) { var sc Server diff --git a/client/serverstorage_test.go b/client/serverstorage_test.go index c310549..a1cefa6 100644 --- a/client/serverstorage_test.go +++ b/client/serverstorage_test.go @@ -21,7 +21,7 @@ func TestGetUid(t *testing.T) { } func TestStoreServer(t *testing.T) { - GetConfig().SetMemPass("test") + createId() ss := ServerStorage{DbFile: "test.db"} srv := Server{ Name: "test", @@ -49,6 +49,7 @@ func TestStoreServer(t *testing.T) { } func TestLoadServersFromUids(t *testing.T) { + createId() GetConfig().SetMemPass("test") ss := ServerStorage{DbFile: "test.db"} srv := Server{ @@ -76,7 +77,7 @@ func TestLoadServersFromUids(t *testing.T) { } func TestLoadServerCardsFromUids(t *testing.T) { - GetConfig().SetMemPass("test") + createId() ss := ServerStorage{DbFile: "test.db"} srv := Server{ Name: "test", @@ -101,3 +102,123 @@ func TestLoadServerCardsFromUids(t *testing.T) { // recursively remove the test.db folder os.RemoveAll("test.db") } + +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") +}