From e3200896326e4ee3b4c8061fc80dcb45b142f741 Mon Sep 17 00:00:00 2001 From: josh <josh@elixxir.io> Date: Fri, 28 Jan 2022 10:21:54 -0800 Subject: [PATCH] Add storage tests and clean formatting --- auth/manager.go | 6 +- storage/auth/store.go | 9 +-- storage/auth/store_test.go | 141 +++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 6 deletions(-) diff --git a/auth/manager.go b/auth/manager.go index 4562ed804..20b341c38 100644 --- a/auth/manager.go +++ b/auth/manager.go @@ -93,9 +93,11 @@ func (m *Manager) RemoveSpecificConfirmCallback(id *id.ID) { m.confirmCallbacks.RemoveSpecific(id) } -func (m *Manager)ReplayRequests(){ +// ReplayRequests will iterate through all pending contact requests and resend them +// to the desired contact. +func (m *Manager) ReplayRequests() { cList := m.storage.Auth().GetAllReceived() - for i := range cList{ + for i := range cList { c := cList[i] cbList := m.requestCallbacks.Get(c.ID) for _, cb := range cbList { diff --git a/storage/auth/store.go b/storage/auth/store.go index acf15c3d2..2bb90faf7 100644 --- a/storage/auth/store.go +++ b/storage/auth/store.go @@ -261,14 +261,15 @@ func (s *Store) AddReceived(c contact.Contact, key *sidh.PublicKey) error { return nil } -func (s *Store)GetAllReceived()[]contact.Contact{ +// GetAllReceived returns all pending received contact requests from storage. +func (s *Store) GetAllReceived() []contact.Contact { s.mux.RLock() defer s.mux.RUnlock() cList := make([]contact.Contact, 0, len(s.requests)) - for key := range s.requests{ + for key := range s.requests { r := s.requests[key] - if r.rt == Receive{ - cList = append(cList,*r.receive) + if r.rt == Receive { + cList = append(cList, *r.receive) } } return cList diff --git a/storage/auth/store_test.go b/storage/auth/store_test.go index b1c4bb92b..6568cd755 100644 --- a/storage/auth/store_test.go +++ b/storage/auth/store_test.go @@ -8,6 +8,7 @@ package auth import ( + "bytes" "github.com/cloudflare/circl/dh/sidh" sidhinterface "gitlab.com/elixxir/client/interfaces/sidh" util "gitlab.com/elixxir/client/storage/utility" @@ -23,6 +24,7 @@ import ( "io" "math/rand" "reflect" + "sort" "sync" "testing" ) @@ -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) { kv := versioned.NewKV(make(ekv.Memstore)) grp := cyclic.NewGroup(large.NewInt(173), large.NewInt(0)) -- GitLab