github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/messaging/service/basic/basicmsgsvc_test.go (about) 1 /* 2 * 3 * Copyright SecureKey Technologies Inc. All Rights Reserved. 4 * 5 * SPDX-License-Identifier: Apache-2.0 6 * / 7 * 8 */ 9 10 package basic 11 12 import ( 13 "fmt" 14 "testing" 15 "time" 16 17 "github.com/stretchr/testify/require" 18 19 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service" 20 ) 21 22 func TestNewMessageService(t *testing.T) { 23 t.Run("test create new MessageService success", func(t *testing.T) { 24 svc, err := NewMessageService("sample-name", getMockMessageHandle()) 25 require.NoError(t, err) 26 require.NotNil(t, svc) 27 }) 28 29 t.Run("test create new MessageService error", func(t *testing.T) { 30 svc, err := NewMessageService("", getMockMessageHandle()) 31 require.Nil(t, svc) 32 require.Error(t, err) 33 require.Contains(t, err.Error(), errNameAndHandleMandatory) 34 35 svc, err = NewMessageService("sample-name", nil) 36 require.Nil(t, svc) 37 require.Error(t, err) 38 require.Contains(t, err.Error(), errNameAndHandleMandatory) 39 }) 40 } 41 42 func TestMessageService_Name(t *testing.T) { 43 t.Run("test MessageService.Name()", func(t *testing.T) { 44 const sampleName = "sample-name" 45 svc, err := NewMessageService("sample-name", getMockMessageHandle()) 46 require.NoError(t, err) 47 require.NotNil(t, svc) 48 require.Equal(t, svc.Name(), sampleName) 49 }) 50 } 51 52 func TestMessageService_Accept(t *testing.T) { 53 t.Run("test MessageService.Accept()", func(t *testing.T) { 54 svc, err := NewMessageService("sample-name", getMockMessageHandle()) 55 require.NoError(t, err) 56 require.NotNil(t, svc) 57 58 require.True(t, svc.Accept(MessageRequestType, nil)) 59 require.True(t, svc.Accept(MessageRequestType, []string{"sample-purpose001", "sample-purpose-02"})) 60 require.False(t, svc.Accept("random-msg-type", nil)) 61 require.False(t, svc.Accept("random-msg-type", []string{"sample-purpose001", "sample-purpose-02"})) 62 }) 63 } 64 65 func TestMessageService_HandleInbound(t *testing.T) { 66 const myDID = "sample-my-did" 67 68 const theirDID = "sample-their-did" 69 70 t.Run("test MessageService.HandleInbound()", func(t *testing.T) { 71 const jsonStr = `{ 72 "@id": "123456780", 73 "@type": "https://didcomm.org/basicmessage/1.0/message", 74 "~l10n": { "locale": "en" }, 75 "content": "Your hovercraft is full of eels." 76 }` 77 78 testCh := make(chan struct { 79 message Message 80 myDID string 81 theirDID string 82 }) 83 handleFn := func(message Message, ctx service.DIDCommContext) error { 84 testCh <- struct { 85 message Message 86 myDID string 87 theirDID string 88 }{ 89 message: message, myDID: ctx.MyDID(), theirDID: ctx.TheirDID(), 90 } 91 return nil 92 } 93 94 svc, err := NewMessageService("sample-name", handleFn) 95 require.NoError(t, err) 96 require.NotNil(t, svc) 97 98 go func() { 99 msg, err := service.ParseDIDCommMsgMap([]byte(jsonStr)) 100 require.NoError(t, err) 101 102 _, err = svc.HandleInbound(msg, service.NewDIDCommContext(myDID, theirDID, nil)) 103 require.NoError(t, err) 104 }() 105 106 select { 107 case x := <-testCh: 108 require.NotNil(t, x) 109 require.Equal(t, x.myDID, myDID) 110 require.Equal(t, x.theirDID, theirDID) 111 require.Equal(t, x.message.I10n.Locale, "en") 112 require.Equal(t, x.message.Content, "Your hovercraft is full of eels.") 113 require.Equal(t, x.message.ID, "123456780") 114 case <-time.After(2 * time.Second): 115 require.Fail(t, "didn't receive basic message to handle") 116 } 117 }) 118 119 t.Run("test MessageService.HandleInbound() error", func(t *testing.T) { 120 const sampleErr = "sample-error" 121 svc, err := NewMessageService("sample-name", getMockMessageHandle()) 122 require.NoError(t, err) 123 require.NotNil(t, svc) 124 125 _, err = svc.HandleInbound(&mockMsg{err: fmt.Errorf(sampleErr)}, service.NewDIDCommContext(myDID, theirDID, nil)) 126 require.Error(t, err) 127 require.Contains(t, err.Error(), "unable to decode incoming DID comm message") 128 }) 129 } 130 131 func getMockMessageHandle() MessageHandle { 132 return func(Message, service.DIDCommContext) error { 133 return nil 134 } 135 } 136 137 type mockMsg struct { 138 *service.DIDCommMsgMap 139 err error 140 } 141 142 func (m *mockMsg) Decode(v interface{}) error { 143 return m.err 144 }