Skip to content

Commit

Permalink
Merge pull request #263 from dedis/inmemory_store
Browse files Browse the repository at this point in the history
More realistic fake.InMemorySnapshot
  • Loading branch information
jbsv authored Aug 14, 2023
2 parents b7a74fa + bf09f67 commit 02ee68c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 30 deletions.
2 changes: 1 addition & 1 deletion contracts/value/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestCommand_Delete(t *testing.T) {

res, err := snap.Get(key)
require.Nil(t, err)
require.Nil(t, res)
require.Nil(t, res) // = "key not found"

_, found := contract.index[keyStr]
require.False(t, found)
Expand Down
8 changes: 5 additions & 3 deletions core/access/darc/darc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
package darc

import (
contract "go.dedis.ch/dela/contracts/access"
"go.dedis.ch/dela/core/access"
"go.dedis.ch/dela/core/access/darc/types"
"go.dedis.ch/dela/core/store"
"go.dedis.ch/dela/core/store/prefixed"
"go.dedis.ch/dela/serde"
"golang.org/x/xerrors"
)

// 4 bytes used to prefix DARC keys in the K/V store.
const ContractUID = "DARC"

// Service is an implementation of an access service that will allow one to
// store and verify access for a group of identities.
//
Expand All @@ -39,6 +38,7 @@ func (srvc Service) Match(
creds access.Credential,
idents ...access.Identity,
) error {
store = prefixed.NewReadable(contract.ContractUID, store)
perm, err := srvc.readPermission(store, creds.GetID())
if err != nil {
return xerrors.Errorf("store failed: %v", err)
Expand All @@ -63,6 +63,8 @@ func (srvc Service) Grant(
cred access.Credential,
idents ...access.Identity,
) error {
store = prefixed.NewSnapshot(contract.ContractUID, store)

perm, err := srvc.readPermission(store, cred.GetID())
if err != nil {
return xerrors.Errorf("store failed: %v", err)
Expand Down
41 changes: 19 additions & 22 deletions core/access/darc/darc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
contract "go.dedis.ch/dela/contracts/access"
"go.dedis.ch/dela/core/access"
"go.dedis.ch/dela/core/access/darc/types"
"go.dedis.ch/dela/core/store/prefixed"
Expand All @@ -16,22 +17,21 @@ import (
var testCtx = json.NewContext()

func TestService_Match(t *testing.T) {
store := prefixed.NewSnapshot(ContractUID, fake.NewSnapshot())
store := fake.NewSnapshot()

alice := bls.NewSigner()
bob := bls.NewSigner()

creds := access.NewContractCreds([]byte{'A'}, "test", "match")
creds := access.NewContractCreds([]byte{0xaa}, "test", "match")

perm := types.NewPermission()
perm.Allow(creds.GetRule(), alice.GetPublicKey())
data, err := perm.Serialize(testCtx)
require.NoError(t, err)

err = store.Set([]byte{'A'}, data)
require.NoError(t, err)
err = store.Set([]byte{'B'}, []byte{})
require.NoError(t, err)
prefixStore := prefixed.NewSnapshot(contract.ContractUID, store)
prefixStore.Set([]byte{0xaa}, data)
prefixStore.Set([]byte{0xbb}, []byte{})

srvc := NewService(testCtx)

Expand All @@ -47,51 +47,48 @@ func TestService_Match(t *testing.T) {
require.Regexp(t,
"^permission: rule 'test:match': unauthorized: \\[bls:[[:xdigit:]]+\\]", err.Error())

badStore := prefixed.NewSnapshot(ContractUID, fake.NewBadSnapshot())
err = srvc.Match(badStore, creds, alice.GetPublicKey())
err = srvc.Match(fake.NewBadSnapshot(), creds, alice.GetPublicKey())
require.EqualError(t, err, fake.Err("store failed: while reading"))

err = srvc.Match(store, access.NewContractCreds([]byte{'C'}, "", ""))
require.EqualError(t, err, "permission 0x43 not found")
err = srvc.Match(store, access.NewContractCreds([]byte{0xcc}, "", ""))
require.EqualError(t, err, "permission 0xcc not found")

err = srvc.Match(store, access.NewContractCreds([]byte{'B'}, "", ""), alice.GetPublicKey())
err = srvc.Match(store, access.NewContractCreds([]byte{0xbb}, "", ""), alice.GetPublicKey())
require.EqualError(t, err,
"store failed: permission malformed: JSON format: failed to unmarshal: unexpected end of JSON input")
}

func TestService_Grant(t *testing.T) {
store := prefixed.NewSnapshot(ContractUID, fake.NewSnapshot())
err := store.Set([]byte{'B'}, []byte{})
require.NoError(t, err)
store := fake.NewSnapshot()
prefixStore := prefixed.NewSnapshot(contract.ContractUID, store)
prefixStore.Set([]byte{0xbb}, []byte{})

creds := access.NewContractCreds([]byte{'A'}, "test", "grant")
creds := access.NewContractCreds([]byte{0xaa}, "test", "grant")

alice := bls.NewSigner()
bob := bls.NewSigner()

srvc := NewService(testCtx)

err = srvc.Grant(store, creds, alice.GetPublicKey())
err := srvc.Grant(store, creds, alice.GetPublicKey())
require.NoError(t, err)

err = srvc.Grant(store, creds, bob.GetPublicKey())
require.NoError(t, err)

badStore := prefixed.NewSnapshot(ContractUID, fake.NewBadSnapshot())
err = srvc.Grant(badStore, creds)
err = srvc.Grant(fake.NewBadSnapshot(), creds)
require.EqualError(t, err, fake.Err("store failed: while reading"))

err = srvc.Grant(store, access.NewContractCreds([]byte{'B'}, "", ""))
err = srvc.Grant(store, access.NewContractCreds([]byte{0xbb}, "", ""))
require.EqualError(t, err,
"store failed: permission malformed: JSON format: failed to unmarshal: unexpected end of JSON input")

srvc.fac = badFac{}
err = srvc.Grant(store, creds, alice.GetPublicKey())
require.EqualError(t, err, fake.Err("failed to serialize"))

fakeSnap := fake.NewSnapshot()
fakeSnap.ErrWrite = fake.GetError()
badStore = prefixed.NewSnapshot(ContractUID, fakeSnap)
badStore := fake.NewSnapshot()
badStore.ErrWrite = fake.GetError()
err = srvc.Grant(badStore, creds, alice.GetPublicKey())
require.EqualError(t, err, fake.Err("store failed to write"))
}
Expand Down
8 changes: 7 additions & 1 deletion core/store/prefixed/prefixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type snapshot struct {
*readable
}

// NewSnapShot creates a new prefixed Snapshot.
// NewSnapshot creates a new prefixed Snapshot.
func NewSnapshot(prefix string, snap store.Snapshot) store.Snapshot {
p := []byte(prefix)
return &snapshot{
Expand All @@ -31,6 +31,12 @@ func NewSnapshot(prefix string, snap store.Snapshot) store.Snapshot {
}
}

// NewReadable creates a new prefixed Readable.
func NewReadable(prefix string, r store.Readable) store.Readable {
p := []byte(prefix)
return &readable{r, p}
}

// Get implements store.Readable
// It takes a key as input and returns a value or error status
func (s *readable) Get(key []byte) ([]byte, error) {
Expand Down
3 changes: 3 additions & 0 deletions core/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type Readable interface {
type Writable interface {
Set(key []byte, value []byte) error

// Delete deletes a given key.
// It can return an error if deleting from a read-only store
// No error is returned if the key does not exist.
Delete(key []byte) error
}

Expand Down
22 changes: 19 additions & 3 deletions testing/fake/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,37 @@ func NewBadSnapshot() *InMemorySnapshot {

// Get implements store.Snapshot.
func (snap *InMemorySnapshot) Get(key []byte) ([]byte, error) {
return snap.values[string(key)], snap.ErrRead
if snap.ErrRead != nil {
return nil, snap.ErrRead
}

value, found := snap.values[string(key)]
if found {
return value, nil
}
return nil, nil
}

// Set implements store.Snapshot.
func (snap *InMemorySnapshot) Set(key, value []byte) error {
if snap.ErrWrite != nil {
return snap.ErrWrite
}

snap.values[string(key)] = value

return snap.ErrWrite
return nil
}

// Delete implements store.Snapshot.
func (snap *InMemorySnapshot) Delete(key []byte) error {
if snap.ErrDelete != nil {
return snap.ErrDelete
}

delete(snap.values, string(key))

return snap.ErrDelete
return nil
}

// InMemoryDB is a fake implementation of a key/value storage.
Expand Down

0 comments on commit 02ee68c

Please sign in to comment.