Skip to content
Snippets Groups Projects
Commit 788208be authored by Jono Wenger's avatar Jono Wenger
Browse files

Add tests for edge.go

parent 4f52fdef
No related branches found
No related tags found
2 merge requests!67Release,!55fully implemented trial hashing of identity fingerprints. Needs tests.
......@@ -24,7 +24,7 @@ type Store struct {
kv *versioned.KV
edge map[id.ID]Preimages
callbacks map[id.ID][]ListUpdateCallBack
mux *sync.RWMutex
mux sync.RWMutex
}
// NewStore creates a new edge store object and inserts the default Preimages
......@@ -49,42 +49,6 @@ func NewStore(kv *versioned.KV, baseIdentity *id.ID) (*Store, error) {
return s, s.save()
}
func LoadStore(kv *versioned.KV) (*Store, error) {
kv = kv.Prefix(edgeStorePrefix)
// Load the list of identities with preimage lists
obj, err := kv.Get(edgeStoreKey, preimageStoreVersion)
if err != nil {
return nil, errors.WithMessagef(err, "failed to load edge store")
}
identities := make([]id.ID, 0)
err = json.Unmarshal(obj.Data, &identities)
if err != nil {
return nil, errors.WithMessagef(err, "failed to unmarshal edge store")
}
s := &Store{
kv: kv,
edge: make(map[id.ID]Preimages),
}
// Load the preimage lists for all identities
for i := range identities {
eid := &identities[i]
preimages, err := loadPreimages(kv, eid)
if err != nil {
return nil, err
}
s.edge[*eid] = preimages
}
return s, nil
}
func (s *Store) Add(preimage Preimage, identity *id.ID) {
s.mux.Lock()
defer s.mux.Unlock()
......@@ -193,7 +157,47 @@ func (s *Store) AddUpdateCallback(identity *id.ID, lucb ListUpdateCallBack) {
s.callbacks[*identity] = append(list, lucb)
}
func (s Store) save() error {
////////////////////////////////////////////////////////////////////////////////
// Storage Functions //
////////////////////////////////////////////////////////////////////////////////
func LoadStore(kv *versioned.KV) (*Store, error) {
kv = kv.Prefix(edgeStorePrefix)
// Load the list of identities with preimage lists
obj, err := kv.Get(edgeStoreKey, preimageStoreVersion)
if err != nil {
return nil, errors.WithMessagef(err, "failed to load edge store")
}
identities := make([]id.ID, 0)
err = json.Unmarshal(obj.Data, &identities)
if err != nil {
return nil, errors.WithMessagef(err, "failed to unmarshal edge store")
}
s := &Store{
kv: kv,
edge: make(map[id.ID]Preimages),
}
// Load the preimage lists for all identities
for i := range identities {
eid := &identities[i]
preimages, err := loadPreimages(kv, eid)
if err != nil {
return nil, err
}
s.edge[*eid] = preimages
}
return s, nil
}
func (s *Store) save() error {
identities := make([]id.ID, 0, len(s.edge))
for eid := range s.edge {
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
////////////////////////////////////////////////////////////////////////////////
package edge
import (
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/elixxir/ekv"
"gitlab.com/xx_network/primitives/id"
"math/rand"
"reflect"
"testing"
"time"
)
// Tests that NewStore returns the expected new Store and that it can be loaded
// from storage.
func TestNewStore(t *testing.T) {
kv := versioned.NewKV(make(ekv.Memstore))
baseIdentity := id.NewIdFromString("baseIdentity", id.User, t)
expected := &Store{
kv: kv.Prefix(edgeStorePrefix),
edge: map[id.ID]Preimages{*baseIdentity: newPreimages(baseIdentity)},
}
received, err := NewStore(kv, baseIdentity)
if err != nil {
t.Errorf("NewStore returned an error: %+v", err)
}
if !reflect.DeepEqual(expected, received) {
t.Errorf("New Store does not match expected."+
"\nexpected: %+v\nreceived: %+v", expected, received)
}
_, err = expected.kv.Get(preimagesKey(baseIdentity), preimageStoreVersion)
if err != nil {
t.Errorf("Failed to load Store from storage: %+v", err)
}
}
func TestStore_Add(t *testing.T) {
s, _, _ := newTestStore(t)
identities := []*id.ID{
id.NewIdFromString("identity0", id.User, t),
id.NewIdFromString("identity1", id.User, t),
}
preimages := []Preimage{
{[]byte("ID0"), "default0", []byte("ID0")},
{[]byte("ID1"), "default1", []byte("ID1")},
{[]byte("ID2"), "default2", []byte("ID2")},
}
// id0Chan := make(chan bool, 2)
// s.callbacks[*identities[0]] = []ListUpdateCallBack{func(identity *id.ID, deleted bool) {
// id0Chan
// }}
s.Add(preimages[0], identities[0])
s.Add(preimages[1], identities[1])
s.Add(preimages[2], identities[0])
if len(s.edge) != 3 {
t.Errorf("Length of edge incorrect.\nexpected: %d\nreceived: %d",
3, len(s.edge))
}
pis := s.edge[*identities[0]]
if len(pis) != 3 {
t.Errorf("Length of preimages for identity %s inocrrect."+
"\nexpected: %d\nreceived: %d", identities[0], 3, len(pis))
}
expected := Preimage{identities[0].Bytes(), "default", identities[0].Bytes()}
if !reflect.DeepEqual(pis[0], expected) {
t.Errorf("First Preimage of first Preimages does not match expected."+
"\nexpected: %+v\nreceived: %+v", expected, pis[0])
}
if !reflect.DeepEqual(pis[1], preimages[0]) {
t.Errorf("Second Preimage of first Preimages does not match expected."+
"\nexpected: %+v\nreceived: %+v", preimages[0], pis[1])
}
if !reflect.DeepEqual(pis[2], preimages[2]) {
t.Errorf("Third Preimage of first Preimages does not match expected."+
"\nexpected: %+v\nreceived: %+v", preimages[2], pis[2])
}
pis = s.edge[*identities[1]]
if len(pis) != 2 {
t.Errorf("Length of preimages for identity %s inocrrect."+
"\nexpected: %d\nreceived: %d", identities[1], 2, len(pis))
}
expected = Preimage{identities[1].Bytes(), "default", identities[1].Bytes()}
if !reflect.DeepEqual(pis[0], expected) {
t.Errorf("First Preimage of second Preimages does not match expected."+
"\nexpected: %+v\nreceived: %+v", expected, pis[0])
}
if !reflect.DeepEqual(pis[1], preimages[1]) {
t.Errorf("Second Preimage of second Preimages does not match expected."+
"\nexpected: %+v\nreceived: %+v", preimages[1], pis[1])
}
}
func TestStore_Remove(t *testing.T) {
}
func TestStore_Get(t *testing.T) {
}
func TestStore_AddUpdateCallback(t *testing.T) {
}
func TestLoadStore(t *testing.T) {
}
func TestStore_save(t *testing.T) {
}
// newTestStore creates a new Store with a random base identity. Returns the
// Store, KV, and base identity.
func newTestStore(t *testing.T) (*Store, *versioned.KV, *id.ID) {
kv := versioned.NewKV(make(ekv.Memstore))
baseIdentity, err := id.NewRandomID(
rand.New(rand.NewSource(time.Now().Unix())), id.User)
if err != nil {
t.Fatalf("Failed to generate random base identity: %+v", err)
}
s, err := NewStore(kv, baseIdentity)
if err != nil {
t.Fatalf("Failed to create new test Store: %+v", err)
}
return s, kv, baseIdentity
}
......@@ -36,6 +36,26 @@ func newPreimages(identity *id.ID) Preimages {
return pis
}
// add adds the preimage to the list.
func (pis Preimages) add(pimg Preimage) Preimages {
return append(pis, pimg)
}
func (pis Preimages) remove(data []byte) Preimages {
for i, preimage := range pis {
if bytes.Equal(preimage.Data, data) {
pis[i] = pis[len(pis)-1]
return pis[:len(pis)-1]
}
}
return pis
}
////////////////////////////////////////////////////////////////////////////////
// Storage Functions //
////////////////////////////////////////////////////////////////////////////////
// loadPreimages loads a Preimages object for the given identity.
func loadPreimages(kv *versioned.KV, identity *id.ID) (Preimages, error) {
......@@ -82,22 +102,6 @@ func (pis Preimages) save(kv *versioned.KV, identity *id.ID) error {
return nil
}
// add adds the preimage to the list.
func (pis Preimages) add(pimg Preimage) Preimages {
return append(pis, pimg)
}
func (pis Preimages) remove(data []byte) Preimages {
for i, preimage := range pis {
if bytes.Equal(preimage.Data, data) {
pis[i] = pis[len(pis)-1]
return pis[:len(pis)-1]
}
}
return pis
}
// delete removes the Preimages from storage.
func (pis Preimages) delete(kv *versioned.KV, identity *id.ID) error {
err := kv.Delete(preimagesKey(identity), preimageStoreVersion)
......
......@@ -34,6 +34,67 @@ func Test_newPreimages(t *testing.T) {
}
}
// Tests that Preimages.add adds the expected Preimage to the list.
func TestPreimages_add(t *testing.T) {
identity0 := id.NewIdFromString("identity0", id.User, t)
identity1 := id.NewIdFromString("identity1", id.User, t)
expected := Preimages{
{identity0.Bytes(), "default", identity0.Bytes()},
{identity0.Bytes(), "group", identity0.Bytes()},
{identity1.Bytes(), "default", identity1.Bytes()},
}
pis := newPreimages(identity0)
pis = pis.add(Preimage{identity0.Bytes(), "group", identity0.Bytes()})
pis = pis.add(Preimage{identity1.Bytes(), "default", identity1.Bytes()})
if !reflect.DeepEqual(expected, pis) {
t.Errorf("Failed to add expected Preimages."+
"\nexpected: %+v\nreceived: %+v", expected, pis)
}
}
// Tests that Preimages.remove removes all the correct Preimage from the list.
func TestPreimages_remove(t *testing.T) {
var pis Preimages
var identities [][]byte
// Add 10 Preimage to the list
for i := 0; i < 10; i++ {
identity := id.NewIdFromUInt(uint64(i), id.User, t)
pisType := "default"
if i%2 == 0 {
pisType = "group"
}
pis = pis.add(Preimage{identity.Bytes(), pisType, identity.Bytes()})
identities = append(identities, identity.Bytes())
}
// Remove each Preimage, check if the length of the list has changed, and
// check that the correct Preimage was removed
for i, identity := range identities {
pis = pis.remove(identity)
if len(pis) != len(identities)-(i+1) {
t.Errorf("Length of Preimages incorrect after removing %d Premiages."+
"\nexpected: %d\nreceived: %d", i, len(identities)-(i+1),
len(pis))
}
// Check if the correct Preimage was deleted
for _, pimg := range pis {
if bytes.Equal(pimg.Data, identity) {
t.Errorf("Failed to delete Preimage #%d: %+v", i, pimg)
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Storage Functions //
////////////////////////////////////////////////////////////////////////////////
// Tests that the Preimages loaded via loadPreimages matches the original saved
// to storage.
func Test_loadPreimages(t *testing.T) {
......@@ -94,63 +155,6 @@ func TestPreimages_save(t *testing.T) {
}
}
// Tests that Preimages.add adds the expected Preimage to the list.
func TestPreimages_add(t *testing.T) {
identity0 := id.NewIdFromString("identity0", id.User, t)
identity1 := id.NewIdFromString("identity1", id.User, t)
expected := Preimages{
{identity0.Bytes(), "default", identity0.Bytes()},
{identity0.Bytes(), "group", identity0.Bytes()},
{identity1.Bytes(), "default", identity1.Bytes()},
}
pis := newPreimages(identity0)
pis = pis.add(Preimage{identity0.Bytes(), "group", identity0.Bytes()})
pis = pis.add(Preimage{identity1.Bytes(), "default", identity1.Bytes()})
if !reflect.DeepEqual(expected, pis) {
t.Errorf("Failed to add expected Preimages."+
"\nexpected: %+v\nreceived: %+v", expected, pis)
}
}
// Tests that Preimages.remove removes all the correct Preimage from the list.
func TestPreimages_remove(t *testing.T) {
var pis Preimages
var identities [][]byte
// Add 10 Preimage to the list
for i := 0; i < 10; i++ {
identity := id.NewIdFromUInt(uint64(i), id.User, t)
pisType := "default"
if i%2 == 0 {
pisType = "group"
}
pis = pis.add(Preimage{identity.Bytes(), pisType, identity.Bytes()})
identities = append(identities, identity.Bytes())
}
// Remove each Preimage, check if the length of the list has changed, and
// check that the correct Preimage was removed
for i, identity := range identities {
pis = pis.remove(identity)
if len(pis) != len(identities)-(i+1) {
t.Errorf("Length of Preimages incorrect after removing %d Premiages."+
"\nexpected: %d\nreceived: %d", i, len(identities)-(i+1),
len(pis))
}
// Check if the correct Preimage was deleted
for _, pimg := range pis {
if bytes.Equal(pimg.Data, identity) {
t.Errorf("Failed to delete Preimage #%d: %+v", i, pimg)
}
}
}
}
// Tests that Preimages.delete deletes the Preimages saved to storage by
// attempting to load them.
func TestPreimages_delete(t *testing.T) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment