From 582887e103867670be854b7d60f36799bb9868b7 Mon Sep 17 00:00:00 2001 From: Linus Gasser Date: Wed, 28 Feb 2024 17:18:38 +0100 Subject: [PATCH] Making tests pass --- internal/testing/fake/ordering.go | 18 +++++-- .../dkg/pedersen/controller/action_test.go | 54 +------------------ services/dkg/pedersen/handler_test.go | 33 ++++++------ services/dkg/pedersen/mod_test.go | 19 ++++--- services/shuffle/neff/handler_test.go | 7 +-- 5 files changed, 51 insertions(+), 80 deletions(-) diff --git a/internal/testing/fake/ordering.go b/internal/testing/fake/ordering.go index 975f7200..3d419b7d 100644 --- a/internal/testing/fake/ordering.go +++ b/internal/testing/fake/ordering.go @@ -75,16 +75,28 @@ func (f Service) GetProof(key []byte) (ordering.Proof, error) { // service. func (f Service) GetStore() store.Readable { return readable{ - snap: f.BallotSnap, + snap: f.BallotSnap, + forms: f.Forms, + context: f.Context, } } type readable struct { - snap *InMemorySnapshot + snap *InMemorySnapshot + forms map[string]formTypes.Form + context serde.Context } func (fr readable) Get(key []byte) ([]byte, error) { - return fr.snap.Get(key) + ret, err := fr.snap.Get(key) + if err != nil || ret != nil { + return ret, err + } + val, ok := fr.forms[hex.EncodeToString(key)] + if !ok { + return nil, xerrors.Errorf("this key doesn't exist") + } + return val.Serialize(fr.context) } // Watch implements ordering.Service. It returns the events that occurred within diff --git a/services/dkg/pedersen/controller/action_test.go b/services/dkg/pedersen/controller/action_test.go index dd8fff03..6df953a0 100644 --- a/services/dkg/pedersen/controller/action_test.go +++ b/services/dkg/pedersen/controller/action_test.go @@ -5,7 +5,6 @@ import ( "io" "testing" - "github.com/c4dt/d-voting/services/dkg/pedersen" "golang.org/x/xerrors" "github.com/c4dt/d-voting/internal/testing/fake" @@ -13,7 +12,6 @@ import ( "github.com/stretchr/testify/require" "go.dedis.ch/dela/cli" "go.dedis.ch/dela/cli/node" - "go.dedis.ch/dela/core/store/kv" ) func TestInitAction_Execute(t *testing.T) { @@ -60,27 +58,6 @@ func TestInitAction_Execute(t *testing.T) { ctx.Injector = node.NewInjector() ctx.Injector.Inject(&service) ctx.Injector.Inject(valService) - - // Try with a DKG but no DKGMap in the system - p := fake.Pedersen{Actors: make(map[string]dkg.Actor)} - ctx.Injector.Inject(p) - - err = action.Execute(ctx) - require.EqualError(t, err, "failed to update DKG store: "+ - "failed to resolve db: couldn't find dependency for 'kv.DB'") - - ctx.Injector = node.NewInjector() - ctx.Injector.Inject(&service) - ctx.Injector.Inject(valService) - - // Try with a DKG and a DKGMap in the system - p.Actors = make(map[string]dkg.Actor) - ctx.Injector.Inject(p) - db := fake.NewInMemoryDB() - ctx.Injector.Inject(db) - - err = action.Execute(ctx) - require.NoError(t, err) } func TestSetupAction_Execute(t *testing.T) { @@ -113,39 +90,10 @@ func TestSetupAction_Execute(t *testing.T) { formIDBuf, err := hex.DecodeString(formID) require.NoError(t, err) - a, err := p.Listen(formIDBuf, fake.Manager{}) + _, err = p.Listen(formIDBuf, fake.Manager{}) require.NoError(t, err) inj.Inject(p) - - err = action.Execute(ctx) - require.EqualError(t, err, "failed to update DKG store: failed to resolve db: "+ - "couldn't find dependency for 'kv.DB'") - - // DKG and DKGMap - db := fake.NewInMemoryDB() - ctx.Injector.Inject(db) - - err = action.Execute(ctx) - require.NoError(t, err) - - // Check that the map contains the actor - err = db.View(func(tx kv.ReadableTx) error { - bucket := tx.GetBucket([]byte(pedersen.BucketName)) - require.NotNil(t, bucket) - - pubKeyBuf := bucket.Get(formIDBuf) - pubKeyRes := suite.Point() - err = pubKeyRes.UnmarshalBinary(pubKeyBuf) - require.NoError(t, err) - - pubKey := a.(fake.DKGActor).PubKey - - require.True(t, pubKeyRes.Equal(pubKey)) - - return nil - }) - require.NoError(t, err) } func TestExportInfoAction_Execute(t *testing.T) { diff --git a/services/dkg/pedersen/handler_test.go b/services/dkg/pedersen/handler_test.go index 0e398244..3d32832f 100644 --- a/services/dkg/pedersen/handler_test.go +++ b/services/dkg/pedersen/handler_test.go @@ -24,7 +24,8 @@ import ( ) func TestHandler_Stream(t *testing.T) { - h := Handler{startRes: &state{}, service: &fake.Service{}} + h := Handler{startRes: &state{}, service: &fake.Service{Forms: make(map[string]formTypes.Form), + BallotSnap: fake.NewSnapshot()}} receiver := fake.NewBadReceiver() err := h.Stream(fake.Sender{}, receiver) require.EqualError(t, err, fake.Err("failed to receive")) @@ -44,8 +45,8 @@ func TestHandler_Stream(t *testing.T) { fake.NewRecvMsg(fake.NewAddress(0), types.DecryptRequest{}), ) err = h.Stream(fake.NewBadSender(), receiver) - require.EqualError(t, err, "could not send pubShares: failed to check"+ - " if the shuffle is over: could not get the form: form does not exist: ") + require.EqualError(t, err, "could not send pubShares: failed to check if the shuffle is over: "+ + "could not get the form: while getting data for form: this key doesn't exist") formIDHex := hex.EncodeToString([]byte("form")) @@ -78,12 +79,13 @@ func TestHandler_Stream(t *testing.T) { h.formFac = formTypes.NewFormFactory(formTypes.CiphervoteFactory{}, fake.RosterFac{}) h.service = &fake.Service{ - Err: nil, - Forms: Forms, - Pool: nil, - Status: false, - Channel: nil, - Context: json.NewContext(), + Err: nil, + Forms: Forms, + Pool: nil, + Status: false, + Channel: nil, + Context: json.NewContext(), + BallotSnap: fake.NewSnapshot(), } h.context = json.NewContext() @@ -313,12 +315,13 @@ func TestHandler_HandlerDecryptRequest(t *testing.T) { h.formFac = formTypes.NewFormFactory(formTypes.CiphervoteFactory{}, fake.RosterFac{}) service := fake.Service{ - Err: nil, - Forms: Forms, - Pool: nil, - Status: false, - Channel: nil, - Context: json.NewContext(), + Err: nil, + Forms: Forms, + Pool: nil, + Status: false, + Channel: nil, + Context: json.NewContext(), + BallotSnap: fake.NewSnapshot(), } h.context = json.NewContext() diff --git a/services/dkg/pedersen/mod_test.go b/services/dkg/pedersen/mod_test.go index 993dc92c..566198a6 100644 --- a/services/dkg/pedersen/mod_test.go +++ b/services/dkg/pedersen/mod_test.go @@ -15,6 +15,7 @@ import ( "go.dedis.ch/dela/core/ordering" "go.dedis.ch/dela/core/txn/signed" "go.dedis.ch/dela/core/validation" + "go.dedis.ch/dela/mino/minogrpc/session" "golang.org/x/xerrors" "github.com/c4dt/d-voting/contracts/evoting" @@ -96,7 +97,8 @@ func TestPedersen_InitNonEmptyMap(t *testing.T) { hd := HandlerData{ StartRes: &state{ distKey: distKey, - participants: []mino.Address{fake.NewAddress(0), fake.NewAddress(1)}, + participants: []mino.Address{session.NewAddress("grpcs://0"), session.NewAddress("grpcs://1")}, + //participants: []mino.Address{fake.NewAddress(0), fake.NewAddress(1)}, }, PrivShare: &share.PriShare{ I: 1, @@ -139,7 +141,7 @@ func TestPedersen_InitNonEmptyMap(t *testing.T) { require.NoError(t, err) // Initialize a Pedersen - p := NewPedersen(fake.Mino{}, &fake.Service{}, fake.NewInMemoryDB(), &fake.Pool{}, fake.Factory{}, fake.Signer{}) + p := NewPedersen(fake.Mino{}, &fake.Service{}, dkgMap, &fake.Pool{}, fake.Factory{}, fake.Signer{}) err = dkgMap.View(func(tx kv.ReadableTx) error { bucket := tx.GetBucket([]byte("dkgmap")) @@ -183,8 +185,8 @@ func TestPedersen_InitNonEmptyMap(t *testing.T) { require.True(t, exists) otherActor := Actor{ - handler: NewHandler(fake.NewAddress(0), &fake.Service{}, &fake.Pool{}, - fake.Manager{}, fake.Signer{}, handlerData, serdecontext, formFac, nil), + handler: NewHandler(session.NewAddress("grpcs://0"), &fake.Service{}, &fake.Pool{}, + fake.Manager{}, fake.Signer{}, handlerData, serdecontext, formFac, nil, nil), } requireActorsEqual(t, actor, &otherActor) @@ -343,12 +345,16 @@ func TestPedersen_Setup(t *testing.T) { Roster: fake.Authority{}, }, serdecontext) + privKey := suite.Scalar().Pick(suite.RandomStream()) + pubKey := suite.Point().Mul(privKey, nil) actor := Actor{ rpc: nil, factory: nil, service: &service, handler: &Handler{ startRes: &state{}, + pubKey: pubKey, + privKey: privKey, }, context: serdecontext, formFac: formFac, @@ -360,7 +366,7 @@ func TestPedersen_Setup(t *testing.T) { actor.formID = wrongFormID _, err := actor.Setup() - require.EqualError(t, err, "failed to get form: form does not exist: ") + require.EqualError(t, err, "failed to get form: while getting data for form: this key doesn't exist") require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromFormDkgStatus)) initMetrics() @@ -434,6 +440,7 @@ func TestPedersen_Setup(t *testing.T) { // This will not change startRes since the responses are all // simulated, so running setup() several times will work. // We test that particular behaviour later. + actor.db = fake.NewInMemoryDB() _, err = actor.Setup() require.NoError(t, err) require.Equal(t, float64(dkg.Setup), testutil.ToFloat64(evoting.PromFormDkgStatus)) @@ -691,7 +698,7 @@ func requireActorsEqual(t require.TestingT, actor1, actor2 dkg.Actor) { actor2Data, err := actor2.MarshalJSON() require.NoError(t, err) - require.Equal(t, actor1Data, actor2Data) + require.Equal(t, string(actor1Data), string(actor2Data)) } func fakeKCPoints(k int, msg string, pubKey kyber.Point) ([]kyber.Point, []kyber.Point, kyber.Point) { diff --git a/services/shuffle/neff/handler_test.go b/services/shuffle/neff/handler_test.go index a0967142..154b7a6f 100644 --- a/services/shuffle/neff/handler_test.go +++ b/services/shuffle/neff/handler_test.go @@ -40,6 +40,7 @@ func TestHandler_Stream(t *testing.T) { types.NewStartShuffle("dummyID", make([]mino.Address, 0)))) handler.txmngr = fake.Manager{} + handler.service = &fake.Service{Forms: make(map[string]etypes.Form), BallotSnap: fake.NewSnapshot()} err = handler.Stream(fake.Sender{}, receiver) require.EqualError(t, err, "failed to handle StartShuffle message: failed "+ @@ -70,13 +71,13 @@ func TestHandler_StartShuffle(t *testing.T) { // Service not working: badService := fake.Service{ Err: fakeErr, - BallotSnap: nil, + BallotSnap: fake.NewSnapshot(), } handler.service = &badService handler.txmngr = fake.Manager{} err := handler.handleStartShuffle(dummyID) - require.EqualError(t, err, "failed to get form: failed to get proof: fake error") + require.EqualError(t, err, "failed to get form: while getting data for form: this key doesn't exist") // Form does not exist service := fake.Service{ @@ -87,7 +88,7 @@ func TestHandler_StartShuffle(t *testing.T) { handler.service = &service err = handler.handleStartShuffle(dummyID) - require.EqualError(t, err, "failed to get form: form does not exist") + require.EqualError(t, err, "failed to get form: while getting data for form: this key doesn't exist") // Form still opened: form := etypes.Form{