Skip to content

Commit

Permalink
Making tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ineiti committed Mar 1, 2024
1 parent df648a3 commit 4027d1c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 80 deletions.
18 changes: 15 additions & 3 deletions internal/testing/fake/ordering.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 1 addition & 53 deletions services/dkg/pedersen/controller/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"io"
"testing"

"github.com/c4dt/d-voting/services/dkg/pedersen"
"golang.org/x/xerrors"

"github.com/c4dt/d-voting/internal/testing/fake"
"github.com/c4dt/d-voting/services/dkg"
"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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
33 changes: 18 additions & 15 deletions services/dkg/pedersen/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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: <nil>")
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"))

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
19 changes: 13 additions & 6 deletions services/dkg/pedersen/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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: <nil>")
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()
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions services/shuffle/neff/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down

0 comments on commit 4027d1c

Please sign in to comment.