Skip to content
Snippets Groups Projects
Commit e3200896 authored by Josh Brooks's avatar Josh Brooks
Browse files

Add storage tests and clean formatting

parent 0e08489b
No related branches found
No related tags found
2 merge requests!170Release,!143added mechanisim to replay all requests. they will replay on recept if the...
...@@ -93,6 +93,8 @@ func (m *Manager) RemoveSpecificConfirmCallback(id *id.ID) { ...@@ -93,6 +93,8 @@ func (m *Manager) RemoveSpecificConfirmCallback(id *id.ID) {
m.confirmCallbacks.RemoveSpecific(id) m.confirmCallbacks.RemoveSpecific(id)
} }
// ReplayRequests will iterate through all pending contact requests and resend them
// to the desired contact.
func (m *Manager) ReplayRequests() { func (m *Manager) ReplayRequests() {
cList := m.storage.Auth().GetAllReceived() cList := m.storage.Auth().GetAllReceived()
for i := range cList { for i := range cList {
......
...@@ -261,6 +261,7 @@ func (s *Store) AddReceived(c contact.Contact, key *sidh.PublicKey) error { ...@@ -261,6 +261,7 @@ func (s *Store) AddReceived(c contact.Contact, key *sidh.PublicKey) error {
return nil return nil
} }
// GetAllReceived returns all pending received contact requests from storage.
func (s *Store) GetAllReceived() []contact.Contact { func (s *Store) GetAllReceived() []contact.Contact {
s.mux.RLock() s.mux.RLock()
defer s.mux.RUnlock() defer s.mux.RUnlock()
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
package auth package auth
import ( import (
"bytes"
"github.com/cloudflare/circl/dh/sidh" "github.com/cloudflare/circl/dh/sidh"
sidhinterface "gitlab.com/elixxir/client/interfaces/sidh" sidhinterface "gitlab.com/elixxir/client/interfaces/sidh"
util "gitlab.com/elixxir/client/storage/utility" util "gitlab.com/elixxir/client/storage/utility"
...@@ -23,6 +24,7 @@ import ( ...@@ -23,6 +24,7 @@ import (
"io" "io"
"math/rand" "math/rand"
"reflect" "reflect"
"sort"
"sync" "sync"
"testing" "testing"
) )
...@@ -764,6 +766,145 @@ func TestStore_Delete_RequestNotInMap(t *testing.T) { ...@@ -764,6 +766,145 @@ func TestStore_Delete_RequestNotInMap(t *testing.T) {
} }
} }
// Unit test of Store.GetAllReceived.
func TestStore_GetAllReceived(t *testing.T) {
s, _, _ := makeTestStore(t)
numReceived := 10
expectContactList := make([]contact.Contact, 0, numReceived)
// Add multiple received contact requests
for i := 0; i < numReceived; i++ {
c := contact.Contact{ID: id.NewIdFromUInt(rand.Uint64(), id.User, t)}
rng := csprng.NewSystemRNG()
_, sidhPubKey := genSidhAKeys(rng)
if err := s.AddReceived(c, sidhPubKey); err != nil {
t.Fatalf("AddReceived() returned an error: %+v", err)
}
expectContactList = append(expectContactList, c)
}
// Check that GetAllReceived returns all contacts
receivedContactList := s.GetAllReceived()
if len(receivedContactList) != numReceived {
t.Errorf("GetAllReceived did not return expected amount of contacts."+
"\nExpected: %d"+
"\nReceived: %d", numReceived, len(receivedContactList))
}
// Sort expected and received lists so that they are in the same order
// since extraction from a map does not maintain order
sort.Slice(expectContactList, func(i, j int) bool {
return bytes.Compare(expectContactList[i].ID.Bytes(), expectContactList[j].ID.Bytes()) == -1
})
sort.Slice(receivedContactList, func(i, j int) bool {
return bytes.Compare(receivedContactList[i].ID.Bytes(), receivedContactList[j].ID.Bytes()) == -1
})
// Check validity of contacts
if !reflect.DeepEqual(expectContactList, receivedContactList) {
t.Errorf("GetAllReceived did not return expected contact list."+
"\nExpected: %+v"+
"\nReceived: %+v", expectContactList, receivedContactList)
}
}
// Tests that Store.GetAllReceived returns an empty list when there are no
// received requests.
func TestStore_GetAllReceived_EmptyList(t *testing.T) {
s, _, _ := makeTestStore(t)
// Check that GetAllReceived returns all contacts
receivedContactList := s.GetAllReceived()
if len(receivedContactList) != 0 {
t.Errorf("GetAllReceived did not return expected amount of contacts."+
"\nExpected: %d"+
"\nReceived: %d", 0, len(receivedContactList))
}
// Add Sent and Receive requests
for i := 0; i < 10; i++ {
partnerID := id.NewIdFromUInt(rand.Uint64(), id.User, t)
rng := csprng.NewSystemRNG()
sidhPrivKey, sidhPubKey := genSidhAKeys(rng)
sr := &SentRequest{
kv: s.kv,
partner: partnerID,
partnerHistoricalPubKey: s.grp.NewInt(1),
myPrivKey: s.grp.NewInt(2),
myPubKey: s.grp.NewInt(3),
mySidHPrivKeyA: sidhPrivKey,
mySidHPubKeyA: sidhPubKey,
fingerprint: format.Fingerprint{5},
}
if err := s.AddSent(sr.partner, sr.partnerHistoricalPubKey,
sr.myPrivKey, sr.myPubKey, sr.mySidHPrivKeyA,
sr.mySidHPubKeyA, sr.fingerprint); err != nil {
t.Fatalf("AddSent() returned an error: %+v", err)
}
}
// Check that GetAllReceived returns all contacts
receivedContactList = s.GetAllReceived()
if len(receivedContactList) != 0 {
t.Errorf("GetAllReceived did not return expected amount of contacts. "+
"It may be pulling from Sent Requests."+
"\nExpected: %d"+
"\nReceived: %d", 0, len(receivedContactList))
}
}
// Tests that Store.GetAllReceived returns only Sent requests when there
// are both Sent and Receive requests in Store.
func TestStore_GetAllReceived_MixSentReceived(t *testing.T) {
s, _, _ := makeTestStore(t)
numReceived := 10
// Add multiple received contact requests
for i := 0; i < numReceived; i++ {
// Add received request
c := contact.Contact{ID: id.NewIdFromUInt(rand.Uint64(), id.User, t)}
rng := csprng.NewSystemRNG()
_, sidhPubKey := genSidhAKeys(rng)
if err := s.AddReceived(c, sidhPubKey); err != nil {
t.Fatalf("AddReceived() returned an error: %+v", err)
}
// Add sent request
partnerID := id.NewIdFromUInt(rand.Uint64(), id.User, t)
sidhPrivKey, sidhPubKey := genSidhAKeys(rng)
sr := &SentRequest{
kv: s.kv,
partner: partnerID,
partnerHistoricalPubKey: s.grp.NewInt(1),
myPrivKey: s.grp.NewInt(2),
myPubKey: s.grp.NewInt(3),
mySidHPrivKeyA: sidhPrivKey,
mySidHPubKeyA: sidhPubKey,
fingerprint: format.Fingerprint{5},
}
if err := s.AddSent(sr.partner, sr.partnerHistoricalPubKey,
sr.myPrivKey, sr.myPubKey, sr.mySidHPrivKeyA,
sr.mySidHPubKeyA, sr.fingerprint); err != nil {
t.Fatalf("AddSent() returned an error: %+v", err)
}
}
// Check that GetAllReceived returns all contacts
receivedContactList := s.GetAllReceived()
if len(receivedContactList) != numReceived {
t.Errorf("GetAllReceived did not return expected amount of contacts. "+
"It may be pulling from Sent Requests."+
"\nExpected: %d"+
"\nReceived: %d", numReceived, len(receivedContactList))
}
}
func makeTestStore(t *testing.T) (*Store, *versioned.KV, []*cyclic.Int) { func makeTestStore(t *testing.T) (*Store, *versioned.KV, []*cyclic.Int) {
kv := versioned.NewKV(make(ekv.Memstore)) kv := versioned.NewKV(make(ekv.Memstore))
grp := cyclic.NewGroup(large.NewInt(173), large.NewInt(0)) grp := cyclic.NewGroup(large.NewInt(173), large.NewInt(0))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment