diff --git a/pkg/framework/aries/api/vdri/vdri.go b/pkg/framework/aries/api/vdri/vdri.go index 334d753ce..973bbf4ac 100644 --- a/pkg/framework/aries/api/vdri/vdri.go +++ b/pkg/framework/aries/api/vdri/vdri.go @@ -21,6 +21,7 @@ type Registry interface { Resolve(did string, opts ...ResolveOpts) (*did.Doc, error) Store(doc *did.Doc) error Create(method string, opts ...DocOpts) (*did.Doc, error) + Close() error } // VDRI verifiable data registry interface @@ -29,6 +30,7 @@ type VDRI interface { Store(doc *did.Doc, by *[]ModifiedBy) error Build(pubKey *PubKey, opts ...DocOpts) (*did.Doc, error) Accept(method string) bool + Close() error } // ResultType input option can be used to request a certain type of result. diff --git a/pkg/framework/aries/framework.go b/pkg/framework/aries/framework.go index 3cf5007f1..42fd75cfd 100644 --- a/pkg/framework/aries/framework.go +++ b/pkg/framework/aries/framework.go @@ -232,6 +232,16 @@ func (a *Aries) Close() error { } } + return a.closeVDRI() +} + +func (a *Aries) closeVDRI() error { + if a.vdriRegistry != nil { + if err := a.vdriRegistry.Close(); err != nil { + return fmt.Errorf("vdri registry close failed: %w", err) + } + } + return nil } diff --git a/pkg/framework/aries/framework_test.go b/pkg/framework/aries/framework_test.go index fdf46ee7d..d48dee561 100644 --- a/pkg/framework/aries/framework_test.go +++ b/pkg/framework/aries/framework_test.go @@ -171,6 +171,21 @@ func TestFramework(t *testing.T) { require.Contains(t, err.Error(), "create new vdri peer failed") }) + t.Run("test vdri - close error", func(t *testing.T) { + path, cleanup := generateTempDir(t) + defer cleanup() + dbPath = path + + vdri := &mockvdri.MockVDRI{CloseErr: fmt.Errorf("close vdri error")} + aries, err := New(WithVDRI(vdri), WithInboundTransport(&mockInboundTransport{})) + require.NoError(t, err) + require.NotEmpty(t, aries) + + err = aries.Close() + require.Error(t, err) + require.Contains(t, err.Error(), "close vdri error") + }) + t.Run("test vdri - with default vdri", func(t *testing.T) { // store peer DID in the store dbprov, err := leveldb.NewProvider(dbPath) diff --git a/pkg/internal/mock/vdri/mock_registry.go b/pkg/internal/mock/vdri/mock_registry.go index bce7ad464..8a3713aa8 100644 --- a/pkg/internal/mock/vdri/mock_registry.go +++ b/pkg/internal/mock/vdri/mock_registry.go @@ -67,6 +67,11 @@ func (m *MockVDRIRegistry) Resolve(didID string, opts ...vdriapi.ResolveOpts) (* return m.ResolveValue, nil } +// Close frees resources being maintained by vdri. +func (m *MockVDRIRegistry) Close() error { + return nil +} + func createDefaultDID() *did.Doc { const ( didContext = "https://w3id.org/did/v1" diff --git a/pkg/internal/mock/vdri/mock_vdri.go b/pkg/internal/mock/vdri/mock_vdri.go index 4ef3776ba..3ebe5f729 100644 --- a/pkg/internal/mock/vdri/mock_vdri.go +++ b/pkg/internal/mock/vdri/mock_vdri.go @@ -18,6 +18,7 @@ type MockVDRI struct { StoreErr error ReadFunc func(didID string, opts ...vdriapi.ResolveOpts) (*did.Doc, error) BuildFunc func(pubKey *vdriapi.PubKey, opts ...vdriapi.DocOpts) (*did.Doc, error) + CloseErr error } // Read did @@ -47,3 +48,8 @@ func (m *MockVDRI) Build(pubKey *vdriapi.PubKey, opts ...vdriapi.DocOpts) (*did. func (m *MockVDRI) Accept(method string) bool { return m.AcceptValue } + +// Close frees resources being maintained by vdri. +func (m *MockVDRI) Close() error { + return m.CloseErr +} diff --git a/pkg/vdri/httpbinding/vdri.go b/pkg/vdri/httpbinding/vdri.go index ba662d217..5ddba4000 100644 --- a/pkg/vdri/httpbinding/vdri.go +++ b/pkg/vdri/httpbinding/vdri.go @@ -65,6 +65,11 @@ func (v *VDRI) Build(pubKey *vdriapi.PubKey, opts ...vdriapi.DocOpts) (*did.Doc, return nil, errors.New("not supported") } +// Close frees resources being maintained by vdri. +func (v *VDRI) Close() error { + return nil +} + // Option configures the peer vdri type Option func(opts *VDRI) diff --git a/pkg/vdri/httpbinding/vdri_test.go b/pkg/vdri/httpbinding/vdri_test.go new file mode 100644 index 000000000..1e29dc278 --- /dev/null +++ b/pkg/vdri/httpbinding/vdri_test.go @@ -0,0 +1,41 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package httpbinding + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestVDRI_Close(t *testing.T) { + t.Run("test success", func(t *testing.T) { + v, err := New("/did:example:334455") + require.NoError(t, err) + require.NoError(t, v.Close()) + }) +} + +func TestVDRI_Store(t *testing.T) { + t.Run("test success", func(t *testing.T) { + v, err := New("/did:example:334455") + require.NoError(t, err) + err = v.Store(nil, nil) + require.Error(t, err) + require.Contains(t, err.Error(), "not supported") + }) +} + +func TestVDRI_Build(t *testing.T) { + t.Run("test success", func(t *testing.T) { + v, err := New("/did:example:334455") + require.NoError(t, err) + _, err = v.Build(nil) + require.Error(t, err) + require.Contains(t, err.Error(), "not supported") + }) +} diff --git a/pkg/vdri/peer/store.go b/pkg/vdri/peer/store.go index 6ddcc562c..58521a12c 100644 --- a/pkg/vdri/peer/store.go +++ b/pkg/vdri/peer/store.go @@ -80,6 +80,11 @@ func (v *VDRI) Get(id string) (*did.Doc, error) { return document, nil } +// Close frees resources being maintained by vdri. +func (v *VDRI) Close() error { + return nil +} + func (v *VDRI) getDeltas(id string) ([]docDelta, error) { val, err := v.store.Get(id) if err != nil { diff --git a/pkg/vdri/peer/store_test.go b/pkg/vdri/peer/store_test.go index 02249ffcb..bff7dc114 100644 --- a/pkg/vdri/peer/store_test.go +++ b/pkg/vdri/peer/store_test.go @@ -69,3 +69,11 @@ func TestPeerDIDStore(t *testing.T) { require.Nil(t, v) require.Contains(t, err.Error(), "delta data fetch from store failed") } + +func TestVDRI_Close(t *testing.T) { + t.Run("test success", func(t *testing.T) { + v, err := New(&storage.MockStoreProvider{}) + require.NoError(t, err) + require.NoError(t, v.Close()) + }) +} diff --git a/pkg/vdri/registry.go b/pkg/vdri/registry.go index 73a7acfa0..464b58f5e 100644 --- a/pkg/vdri/registry.go +++ b/pkg/vdri/registry.go @@ -128,6 +128,17 @@ func (r *Registry) Store(doc *diddoc.Doc) error { return method.Store(doc, nil) } +// Close frees resources being maintained by vdri. +func (r *Registry) Close() error { + for _, v := range r.vdri { + if err := v.Close(); err != nil { + return fmt.Errorf("close vdri: %w", err) + } + } + + return nil +} + func (r *Registry) resolveVDRI(method string) (vdriapi.VDRI, error) { for _, v := range r.vdri { if v.Accept(method) { diff --git a/pkg/vdri/registry_test.go b/pkg/vdri/registry_test.go index 67bc2c25b..83bda7639 100644 --- a/pkg/vdri/registry_test.go +++ b/pkg/vdri/registry_test.go @@ -19,6 +19,20 @@ import ( mockvdri "github.com/hyperledger/aries-framework-go/pkg/internal/mock/vdri" ) +func TestRegistry_Close(t *testing.T) { + t.Run("test success", func(t *testing.T) { + registry := New(&mockprovider.Provider{}) + require.NoError(t, registry.Close()) + }) + t.Run("test error", func(t *testing.T) { + registry := New(&mockprovider.Provider{}, + WithVDRI(&mockvdri.MockVDRI{CloseErr: fmt.Errorf("close error")})) + err := registry.Close() + require.Error(t, err) + require.Contains(t, err.Error(), "close error") + }) +} + func TestRegistry_Resolve(t *testing.T) { t.Run("test invalid did input", func(t *testing.T) { registry := New(&mockprovider.Provider{})