github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/did/helpers_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 SPDX-License-Identifier: Apache-2.0 4 */ 5 6 package did_test 7 8 import ( 9 "reflect" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13 14 . "github.com/hyperledger/aries-framework-go/component/models/did" 15 16 mockdiddoc "github.com/hyperledger/aries-framework-go/pkg/mock/diddoc" 17 ) 18 19 func TestContextCleanup(t *testing.T) { 20 t.Run("string", func(t *testing.T) { 21 var c0 Context = "stringval" 22 var c1 Context = ContextCleanup(c0) 23 require.Equal(t, c0, c1) 24 }) 25 26 t.Run("[]string", func(t *testing.T) { 27 var c0 Context = []string{"stringval"} 28 var c1 Context = ContextCleanup(c0) 29 require.Equal(t, c0, c1) 30 }) 31 32 t.Run("[]string empty", func(t *testing.T) { 33 var c0 Context = []string{""} 34 var c1 Context = ContextCleanup(c0) 35 require.Equal(t, c0, c1) 36 }) 37 38 t.Run("[]string nil", func(t *testing.T) { 39 var c0 Context = []string{} 40 var c1 Context = ContextCleanup(c0) 41 require.Equal(t, []string{""}, c1) 42 }) 43 44 t.Run("[]interface{} nil value", func(t *testing.T) { 45 var c0 Context = []interface{}{} 46 var c1 Context = ContextCleanup(c0) 47 require.Equal(t, "", c1) 48 }) 49 50 t.Run("[]interface{} all strings", func(t *testing.T) { 51 var c0 Context = []interface{}{"alpha", "beta"} 52 var c1 Context = ContextCleanup(c0) 53 require.Equal(t, []string{"alpha", "beta"}, c1) 54 }) 55 56 t.Run("[]interface{} with map", func(t *testing.T) { 57 var c0 Context = []interface{}{map[string]interface{}{"@key": "value"}} 58 var c1 Context = ContextCleanup(c0) 59 require.Equal(t, c0, c1) 60 }) 61 } 62 63 func TestContextCopy(t *testing.T) { 64 t.Run("string", func(t *testing.T) { 65 var c0 Context = "stringval" 66 var c1 Context = ContextCopy(c0) 67 require.Equal(t, c0, c1) 68 }) 69 70 t.Run("[]string", func(t *testing.T) { 71 var c0 Context = []string{"stringval"} 72 var c1 Context = ContextCopy(c0) 73 require.Equal(t, c0, c1) 74 75 p0, p1 := reflect.ValueOf(c0).Pointer(), reflect.ValueOf(c1).Pointer() 76 require.NotEqual(t, p0, p1, "slices should not share pointers") 77 }) 78 79 t.Run("[]interface{}", func(t *testing.T) { 80 var c0 Context = []interface{}{map[string]interface{}{"@key": "value"}} 81 var c1 Context = ContextCopy(c0) 82 require.Equal(t, c0, c1) 83 84 p0, p1 := reflect.ValueOf(c0).Pointer(), reflect.ValueOf(c1).Pointer() 85 require.NotEqual(t, p0, p1, "slices should not share pointers") 86 87 a0, ok := c0.([]interface{}) 88 require.True(t, ok) 89 90 a1, ok := c1.([]interface{}) 91 require.True(t, ok) 92 93 p0, p1 = reflect.ValueOf(a0[0]).Pointer(), reflect.ValueOf(a1[0]).Pointer() 94 require.NotEqual(t, p0, p1, "maps should not share pointers") 95 }) 96 } 97 98 func TestContextPeekString(t *testing.T) { 99 const ( 100 DoNotWant = "ContextDoNotWant" 101 Want = "ContextWant" 102 ) 103 104 tests := map[string]struct { 105 schema string 106 ok bool 107 input Context 108 }{ 109 "present in 'string'": {schema: Want, ok: true, input: Want}, 110 "present in '[]string' (single)": {schema: Want, ok: true, input: []string{Want}}, 111 "present in '[]string' (multiple)": {schema: Want, ok: true, input: []string{Want, DoNotWant}}, 112 "present in '[]interface{}' (single)": {schema: Want, ok: true, input: []interface{}{Want}}, 113 "present in '[]interface{}' (multiple)": {schema: Want, ok: true, input: []interface{}{Want, DoNotWant}}, 114 "not present in 'string'": {schema: "", ok: false, input: ""}, 115 "not present in '[]string'": {schema: "", ok: false, input: []string{}}, 116 "not present in '[]interface{}'": {schema: "", ok: false, input: []interface{}{}}, 117 "context is nil": {schema: "", ok: false, input: nil}, 118 "context is invalid": {schema: "", ok: false, input: 42}, 119 } 120 121 for name, tc := range tests { 122 t.Run(name, func(t *testing.T) { 123 schema, ok := ContextPeekString(tc.input) 124 require.Equal(t, tc.ok, ok) 125 require.Equal(t, tc.schema, schema) 126 }) 127 } 128 } 129 130 func TestContextContainsString(t *testing.T) { 131 const ( 132 DoNotWant = "ContextDoNotWant" 133 Want = "ContextWant" 134 ) 135 136 tests := map[string]struct { 137 ok bool 138 input Context 139 }{ 140 "present in 'string'": {ok: true, input: Want}, 141 "present in '[]string' (first)": {ok: true, input: []string{Want}}, 142 "present in '[]string' (not first)": {ok: true, input: []string{DoNotWant, Want}}, 143 "present in '[]interface{}' (first)": {ok: true, input: []interface{}{Want}}, 144 "present in '[]interface{}' (not first)": {ok: true, input: []interface{}{DoNotWant, Want}}, 145 "present in '[]interface{}' (map)": {ok: false, input: []interface{}{map[string]interface{}{"k": Want}}}, 146 "not present in 'string'": {ok: false, input: DoNotWant}, 147 "not present in '[]string'": {ok: false, input: []string{DoNotWant}}, 148 "not present in '[]interface{}'": {ok: false, input: []interface{}{DoNotWant}}, 149 "context is nil": {ok: false, input: nil}, 150 "context is invalid": {ok: false, input: 42}, 151 } 152 153 for name, tc := range tests { 154 t.Run(name, func(t *testing.T) { 155 ok := ContextContainsString(tc.input, Want) 156 require.Equal(t, tc.ok, ok) 157 }) 158 } 159 } 160 161 func TestGetRecipientKeys(t *testing.T) { 162 t.Run("successfully getting recipient keys", func(t *testing.T) { 163 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 164 165 recipientKeys, ok := LookupDIDCommRecipientKeys(didDoc) 166 require.True(t, ok) 167 require.Equal(t, 1, len(recipientKeys)) 168 }) 169 170 t.Run("error due to missing did-communication service", func(t *testing.T) { 171 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 172 didDoc.Service = nil 173 174 recipientKeys, ok := LookupDIDCommRecipientKeys(didDoc) 175 require.False(t, ok) 176 require.Nil(t, recipientKeys) 177 }) 178 179 t.Run("error due to missing recipient keys in did-communication service", func(t *testing.T) { 180 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 181 didDoc.Service[0].RecipientKeys = []string{} 182 183 recipientKeys, ok := LookupDIDCommRecipientKeys(didDoc) 184 require.False(t, ok) 185 require.Nil(t, recipientKeys) 186 }) 187 } 188 189 func TestGetDidCommService(t *testing.T) { 190 didCommServiceType := "did-communication" 191 192 t.Run("successfully getting did-communication service", func(t *testing.T) { 193 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 194 195 s, ok := LookupService(didDoc, didCommServiceType) 196 require.True(t, ok) 197 require.Equal(t, "did-communication", s.Type) 198 require.Equal(t, 0, s.Priority) 199 }) 200 201 t.Run("successfully getting did-communication service - switch order", func(t *testing.T) { 202 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 203 service := didDoc.Service[0] 204 didDoc.Service[0] = didDoc.Service[1] 205 didDoc.Service[1] = service 206 207 s, ok := LookupService(didDoc, didCommServiceType) 208 require.True(t, ok) 209 require.Equal(t, "did-communication", s.Type) 210 require.Equal(t, 0, s.Priority) 211 }) 212 213 t.Run("successfully getting did-communication service - first priority nil", func(t *testing.T) { 214 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 215 didDoc.Service[0].Priority = nil 216 217 s, ok := LookupService(didDoc, didCommServiceType) 218 require.True(t, ok) 219 require.Equal(t, "did-communication", s.Type) 220 require.Equal(t, 1, s.Priority) 221 }) 222 223 t.Run("successfully getting did-communication service - second priority nil", func(t *testing.T) { 224 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 225 didDoc.Service[1].Priority = nil 226 227 s, ok := LookupService(didDoc, didCommServiceType) 228 require.True(t, ok) 229 require.Equal(t, "did-communication", s.Type) 230 require.Equal(t, 0, s.Priority) 231 }) 232 233 t.Run("successfully getting did-communication service - both nil", func(t *testing.T) { 234 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 235 didDoc.Service[0].Priority = nil 236 didDoc.Service[1].Priority = nil 237 238 s, ok := LookupService(didDoc, didCommServiceType) 239 require.True(t, ok) 240 require.Equal(t, "did-communication", s.Type) 241 require.Equal(t, nil, s.Priority) 242 243 uri, err := s.ServiceEndpoint.URI() 244 require.NoError(t, err) 245 require.Equal(t, "https://localhost:8090", uri) 246 }) 247 248 t.Run("error due to missing service", func(t *testing.T) { 249 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 250 didDoc.Service = nil 251 252 s, ok := LookupService(didDoc, didCommServiceType) 253 require.False(t, ok) 254 require.Nil(t, s) 255 }) 256 257 t.Run("error due to missing did-communication service", func(t *testing.T) { 258 didDoc := mockdiddoc.GetMockDIDDoc(t, false) 259 didDoc.Service[0].Type = "some-type" 260 didDoc.Service[1].Type = "other-type" 261 262 s, ok := LookupService(didDoc, didCommServiceType) 263 require.False(t, ok) 264 require.Nil(t, s) 265 }) 266 }