Skip to content

Commit

Permalink
Merge pull request hyperledger-archives#2671 from llorllale/fix_didex…
Browse files Browse the repository at this point in the history
…change_client_createconnection

fix: ineffective didexchange.Client.CreateConnection()
  • Loading branch information
George Aristy committed Mar 24, 2021
2 parents 2015d26 + dfb9610 commit 074644c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/didcomm/protocol/didexchange/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ func (s *Service) update(msgType string, record *connection.Record) error {
// CreateConnection saves the record to the connection store and maps TheirDID to their recipient keys in
// the did connection store.
func (s *Service) CreateConnection(record *connection.Record, theirDID *did.Doc) error {
logger.Debugf("creating connection using record [%+v] and theirDID [%+v]", record, theirDID)

didMethod, err := vdr.GetDidMethod(theirDID.ID)
if err != nil {
return err
Expand All @@ -624,6 +626,16 @@ func (s *Service) CreateConnection(record *connection.Record, theirDID *did.Doc)
return fmt.Errorf("vdr failed to store theirDID : %w", err)
}

err = s.connectionStore.SaveDIDFromDoc(theirDID)
if err != nil {
return fmt.Errorf("failed to save theirDID to the did.ConnectionStore: %w", err)
}

err = s.connectionStore.SaveDIDByResolving(record.MyDID)
if err != nil {
return fmt.Errorf("failed to save myDID to the did.ConnectionStore: %w", err)
}

return s.connectionRecorder.SaveConnectionRecord(record)
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/didcomm/protocol/didexchange/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ func TestCreateConnection(t *testing.T) {
ServiceMap: map[string]interface{}{
mediator.Coordination: &mockroute.MockMediatorSvc{},
},
DIDConnectionStoreValue: &mockConnectionStore{},
}
s, err := New(provider)
require.NoError(t, err)
Expand Down Expand Up @@ -715,6 +716,53 @@ func TestCreateConnection(t *testing.T) {
ServiceMap: map[string]interface{}{
mediator.Coordination: &mockroute.MockMediatorSvc{},
},
DIDConnectionStoreValue: &mockConnectionStore{},
})
require.NoError(t, err)

err = s.CreateConnection(&connection.Record{
State: StateIDCompleted,
}, newPeerDID(t))
require.Error(t, err)
require.True(t, errors.Is(err, expected))
})

t.Run("wraps did.ConnectionStore.SaveDIDFromDoc error", func(t *testing.T) {
expected := errors.New("test")
s, err := New(&mockprovider.Provider{
KMSValue: &mockkms.KeyManager{},
StorageProviderValue: mockstorage.NewMockStoreProvider(),
ProtocolStateStorageProviderValue: mockstorage.NewMockStoreProvider(),
VDRegistryValue: &mockvdr.MockVDRegistry{},
ServiceMap: map[string]interface{}{
mediator.Coordination: &mockroute.MockMediatorSvc{},
},
DIDConnectionStoreValue: &mockConnectionStore{
saveDIDFromDocErr: expected,
},
})
require.NoError(t, err)

err = s.CreateConnection(&connection.Record{
State: StateIDCompleted,
}, newPeerDID(t))
require.Error(t, err)
require.True(t, errors.Is(err, expected))
})

t.Run("wraps did.ConnectionStore.SaveDIDByResolving error", func(t *testing.T) {
expected := errors.New("test")
s, err := New(&mockprovider.Provider{
KMSValue: &mockkms.KeyManager{},
StorageProviderValue: mockstorage.NewMockStoreProvider(),
ProtocolStateStorageProviderValue: mockstorage.NewMockStoreProvider(),
VDRegistryValue: &mockvdr.MockVDRegistry{},
ServiceMap: map[string]interface{}{
mediator.Coordination: &mockroute.MockMediatorSvc{},
},
DIDConnectionStoreValue: &mockConnectionStore{
saveDIDByResolvingErr: expected,
},
})
require.NoError(t, err)

Expand Down Expand Up @@ -2152,3 +2200,28 @@ func newPeerDID(t *testing.T) *did.Doc {

return doc
}

type mockConnectionStore struct {
saveDIDByResolvingErr error
saveDIDFromDocErr error
}

// GetDID returns DID associated with key.
func (m *mockConnectionStore) GetDID(string) (string, error) {
return "", nil
}

// SaveDID saves DID to the underlying storage.
func (m *mockConnectionStore) SaveDID(string, ...string) error {
return nil
}

// SaveDIDFromDoc saves DID from did.Doc to the underlying storage.
func (m *mockConnectionStore) SaveDIDFromDoc(*did.Doc) error {
return m.saveDIDFromDocErr
}

// SaveDIDByResolving saves DID resolved by VDR to the underlying storage.
func (m *mockConnectionStore) SaveDIDByResolving(string, ...string) error {
return m.saveDIDByResolvingErr
}

0 comments on commit 074644c

Please sign in to comment.