github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/transport/internal/helpers_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package internal 8 9 import ( 10 "encoding/base64" 11 "errors" 12 "fmt" 13 "strings" 14 "testing" 15 16 "github.com/stretchr/testify/require" 17 18 "github.com/hyperledger/aries-framework-go/pkg/didcomm/transport" 19 mockpackager "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/packager" 20 ) 21 22 func TestUnpackMessage(t *testing.T) { 23 msgB64 := base64.StdEncoding.EncodeToString([]byte("msg")) 24 msgB64Raw := base64.RawURLEncoding.EncodeToString([]byte("msgraw1")) 25 26 tests := []struct { 27 name string 28 packer transport.Packager 29 message []byte 30 source string 31 }{ 32 { 33 name: "success unpack", 34 packer: &mockpackager.Packager{UnpackValue: &transport.Envelope{}}, 35 message: []byte(""), 36 source: "http", 37 }, 38 { 39 name: "error unpack not double quoted", 40 packer: &mockpackager.Packager{UnpackErr: errors.New("unpack error")}, 41 message: []byte("not double quote"), 42 source: "http", 43 }, 44 { 45 name: "success unpack double quoted message", 46 packer: &mockpackager.Packager{UnpackValue: &transport.Envelope{}}, 47 message: []byte(fmt.Sprintf("\"%v\"", msgB64)), 48 source: "ws", 49 }, 50 { 51 name: "success unpack double quoted message RawBase64URL encoded", 52 packer: &mockpackager.Packager{UnpackValue: &transport.Envelope{}}, 53 message: []byte(fmt.Sprintf("\"%v\"", msgB64Raw)), 54 source: "ws", 55 }, 56 { 57 name: "error unpack double quoted invalid base64 encoded message", 58 packer: &mockpackager.Packager{UnpackErr: fmt.Errorf("failed to unpack")}, 59 message: []byte(fmt.Sprintf("\"%v\"", "!!!!!!!")), 60 source: "ws", 61 }, 62 { 63 name: "error in unpack call", 64 packer: &mockpackager.Packager{UnpackErr: fmt.Errorf("failed to unpack")}, 65 message: []byte(fmt.Sprintf("\"%v\"", msgB64)), 66 source: "ws", 67 }, 68 } 69 70 for _, tt := range tests { 71 tc := tt 72 t.Run(tc.name, func(t *testing.T) { 73 e, err := UnpackMessage(tc.message, tc.packer, tc.source) 74 if strings.HasPrefix(tc.name, "success") { 75 require.NoError(t, err) 76 require.NotNil(t, e) 77 78 return 79 } 80 81 switch tc.name { 82 case "error unpack not double quoted": 83 require.EqualError(t, err, "failed to unpack msg from http: unpack error") 84 case "error unpack double quoted invalid base64 encoded message": 85 require.EqualError(t, err, "not base64 encoded message error from ws: URLEncoding error: illegal"+ 86 " base64 data at input byte 0, RawURLEncoding error: illegal base64 data at input byte 0") 87 case "error in unpack call": 88 require.EqualError(t, err, "failed to unpack msg from ws: failed to unpack") 89 } 90 91 require.Empty(t, e) 92 }) 93 } 94 }