store unknown servers in the invitation process answer and finalize + server storage fixes and testing
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
c0fd74f1e6
commit
af55df1ff5
@ -135,6 +135,7 @@ func (id *Identity) AnswerInvitation(MyName string, ContactName string, MessageS
|
|||||||
peer.InvitationMessage = ReceivedContact.InvitationMessage
|
peer.InvitationMessage = ReceivedContact.InvitationMessage
|
||||||
for srv := range ReceivedContact.PullServers {
|
for srv := range ReceivedContact.PullServers {
|
||||||
peer.ContactPullServers = append(peer.ContactPullServers, ReceivedContact.PullServers[srv].GetUid())
|
peer.ContactPullServers = append(peer.ContactPullServers, ReceivedContact.PullServers[srv].GetUid())
|
||||||
|
id.MessageServers.StoreServerIfNotExists(CreateServerFromUid(ReceivedContact.PullServers[srv].GetUid()))
|
||||||
}
|
}
|
||||||
/* for _, i := range MessageServerIdxs {
|
/* for _, i := range MessageServerIdxs {
|
||||||
srv := id.MessageServers.Servers[i].GetServerCard()
|
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)*/
|
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)
|
return id.Peers.FinalizeInvitation(ReceivedContact)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,10 @@ func TestStorePeer(t *testing.T) {
|
|||||||
|
|
||||||
peer := &Peer{
|
peer := &Peer{
|
||||||
Uid: uuid.New().String(),
|
Uid: uuid.New().String(),
|
||||||
|
Name: "testName",
|
||||||
|
InvitationId: "testInvitationId",
|
||||||
|
MyName: "testMyName",
|
||||||
|
ContactPublicKey: "testContactPublicKey",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := ps.StorePeer(peer)
|
err := ps.StorePeer(peer)
|
||||||
@ -46,4 +50,11 @@ func TestStorePeer(t *testing.T) {
|
|||||||
if len(peers) != 1 {
|
if len(peers) != 1 {
|
||||||
t.Errorf("Expected 1 peer, got %d", len(peers))
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
// LoadServer function loads a Server from a badger database with Server.GetUid() as key
|
||||||
func (ss *ServerStorage) LoadServer(uid string) (*Server, error) {
|
func (ss *ServerStorage) LoadServer(uid string) (*Server, error) {
|
||||||
var sc Server
|
var sc Server
|
||||||
|
@ -21,7 +21,7 @@ func TestGetUid(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStoreServer(t *testing.T) {
|
func TestStoreServer(t *testing.T) {
|
||||||
GetConfig().SetMemPass("test")
|
createId()
|
||||||
ss := ServerStorage{DbFile: "test.db"}
|
ss := ServerStorage{DbFile: "test.db"}
|
||||||
srv := Server{
|
srv := Server{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
@ -49,6 +49,7 @@ func TestStoreServer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadServersFromUids(t *testing.T) {
|
func TestLoadServersFromUids(t *testing.T) {
|
||||||
|
createId()
|
||||||
GetConfig().SetMemPass("test")
|
GetConfig().SetMemPass("test")
|
||||||
ss := ServerStorage{DbFile: "test.db"}
|
ss := ServerStorage{DbFile: "test.db"}
|
||||||
srv := Server{
|
srv := Server{
|
||||||
@ -76,7 +77,7 @@ func TestLoadServersFromUids(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadServerCardsFromUids(t *testing.T) {
|
func TestLoadServerCardsFromUids(t *testing.T) {
|
||||||
GetConfig().SetMemPass("test")
|
createId()
|
||||||
ss := ServerStorage{DbFile: "test.db"}
|
ss := ServerStorage{DbFile: "test.db"}
|
||||||
srv := Server{
|
srv := Server{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
@ -101,3 +102,123 @@ func TestLoadServerCardsFromUids(t *testing.T) {
|
|||||||
// recursively remove the test.db folder
|
// recursively remove the test.db folder
|
||||||
os.RemoveAll("test.db")
|
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")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user