diff --git a/network/historicalRoundData_test.go b/network/historicalRoundData_test.go index 49411c274c9ff1f08583ea1418082b0fd55c5bfd..be60551932f44aef6bc93ff6eed5a14ec319ab11 100644 --- a/network/historicalRoundData_test.go +++ b/network/historicalRoundData_test.go @@ -4,7 +4,12 @@ import ( jww "github.com/spf13/jwalterweatherman" pb "gitlab.com/elixxir/comms/mixmessages" ds "gitlab.com/elixxir/comms/network/dataStructures" + "gitlab.com/elixxir/comms/testkeys" + "gitlab.com/elixxir/comms/testutils" + "gitlab.com/elixxir/crypto/signature" + "gitlab.com/elixxir/crypto/signature/rsa" "gitlab.com/elixxir/primitives/id" + "gitlab.com/xx_network/comms/connect" "testing" ) @@ -164,7 +169,7 @@ func TestERSRetrieve(t *testing.T) { t.Errorf(err.Error()) } if nri == nil { - t.Fatalf("returned round info was not nil") + t.Fatalf("returned round info was nil") } if nri.ID != r.ID || nri.UpdateID != r.UpdateID { t.Errorf("Returned round or update ID did not match what we put in") @@ -249,3 +254,136 @@ func TestERSRetrieveRange(t *testing.T) { t.Errorf("Second returned round and original mismatched IDs") } } + +// Test that calling the GetHistoricalRound interface on Instance works +func TestInstance_GetHistoricalRound(t *testing.T) { + i := Instance{} + ri, err := i.GetHistoricalRound(id.Round(0)) + // This should fail since this Instance doesn't have an ERS object + if err == nil { + t.Errorf(err.Error()) + } + + var ers ds.ExternalRoundStorage = ersMemMap{rounds: make(map[id.Round]*pb.RoundInfo)} + i2 := Instance{ers: ers} + ri, err = i2.GetHistoricalRound(id.Round(0)) + // Should return no error and blank round + if err != nil { + t.Errorf(err.Error()) + } + if ri != nil { + t.Errorf("ri contains round info") + } +} + +// Test that calling the GetHistoricalRoundRange interface on Instance works +func TestInstance_GetHistoricalRoundRange(t *testing.T) { + i := Instance{} + ri, err := i.GetHistoricalRoundRange(5, 10) + // This should fail since this Instance doesn't have an ERS object + if err == nil { + t.Errorf(err.Error()) + } + + var ers ds.ExternalRoundStorage = ersMemMap{rounds: make(map[id.Round]*pb.RoundInfo)} + i2 := Instance{ers: ers} + ri, err = i2.GetHistoricalRoundRange(5, 10) + // Should return no error and blank round + if err != nil { + t.Errorf(err.Error()) + } + for _, round := range ri { + if round != nil { + t.Errorf("ri contains round info") + } + } +} + +// Test that calling the GetHistoricalRounds interface on Instance works +func TestInstance_GetHistoricalRounds(t *testing.T) { + i := Instance{} + getRounds := []id.Round{id.Round(1), id.Round(2), id.Round(3)} + ri, err := i.GetHistoricalRounds(getRounds) + // This should fail since this Instance doesn't have an ERS object + if err == nil { + t.Errorf(err.Error()) + } + + var ers ds.ExternalRoundStorage = ersMemMap{rounds: make(map[id.Round]*pb.RoundInfo)} + i2 := Instance{ers: ers} + ri, err = i2.GetHistoricalRounds(getRounds) + // Should return no error and blank round + if err != nil { + t.Errorf(err.Error()) + } + for _, round := range ri { + if round != nil { + t.Errorf("ri contains round info") + } + } +} + +// Test that a new round update is inputted into the ERS map +func TestInstance_RoundUpdateAddsToERS(t *testing.T) { + // Get signing certificates + priv := testkeys.LoadFromPath(testkeys.GetNodeKeyPath()) + privKey, err := rsa.LoadPrivateKeyFromPem(priv) + pub := testkeys.LoadFromPath(testkeys.GetNodeCertPath()) + if err != nil { + t.Errorf("Could not generate rsa key: %s", err) + } + + // Create a basic testing NDF and sign it + f := &pb.NDF{} + f.Ndf = []byte(testutils.ExampleJSON) + baseNDF := testutils.NDF + if err != nil { + t.Errorf("Could not generate serialized ndf: %s", err) + } + err = signature.Sign(f, privKey) + if err != nil { + t.Errorf("Could not generate serialized ndf: %s", err) + } + + // Build the Instance object with an ERS memory map + pc := &connect.ProtoComms{} + var ers ds.ExternalRoundStorage = ersMemMap{rounds: make(map[id.Round]*pb.RoundInfo)} + i, err := NewInstance(pc, baseNDF, baseNDF, ers) + if err != nil { + t.Error(nil) + } + + // Add a permissioning host + _, err = i.comm.AddHost(&id.Permissioning, "0.0.0.0:4200", pub, false, true) + if err != nil { + t.Errorf("Failed to add permissioning host: %+v", err) + } + + // Build a basic RoundInfo object and sign it + r := &pb.RoundInfo{ + ID: 2, + UpdateID: 4, + } + err = signature.Sign(r, privKey) + if err != nil { + t.Errorf(err.Error()) + } + + // Cause a RoundUpdate + err = i.RoundUpdate(r) + if err != nil { + t.Errorf(err.Error()) + } + + // Check that the round info was stored correctly + rr, err := ers.Retrieve(id.Round(r.ID)) + if err != nil { + t.Errorf(err.Error()) + } + if rr == nil { + t.Fatalf("returned round info was nil") + } + if rr.ID != r.ID || rr.UpdateID != r.UpdateID { + t.Errorf("Second returned round and original mismatched IDs") + } +}