@@ -176,7 +176,7 @@ func TestService_Handle_Inviter(t *testing.T) {
176176 require .NoError (t , err )
177177
178178 completedFlag := make (chan struct {})
179- respondedFlag := make (chan struct {} )
179+ respondedFlag := make (chan string )
180180
181181 go msgEventListener (t , statusCh , respondedFlag , completedFlag )
182182
@@ -247,7 +247,151 @@ func TestService_Handle_Inviter(t *testing.T) {
247247 validateState (t , s , thid , findNamespace (AckMsgType ), (& completed {}).Name ())
248248}
249249
250- func msgEventListener (t * testing.T , statusCh chan service.StateMsg , respondedFlag , completedFlag chan struct {}) {
250+ func TestService_Handle_Inviter_With_ProblemReport (t * testing.T ) {
251+ mockStore := & mockstorage.MockStore {Store : make (map [string ]mockstorage.DBEntry )}
252+ storeProv := mockstorage .NewCustomMockStoreProvider (mockStore )
253+ k := newKMS (t , storeProv )
254+ prov := & protocol.MockProvider {
255+ StoreProvider : storeProv ,
256+ ServiceMap : map [string ]interface {}{
257+ mediator .Coordination : & mockroute.MockMediatorSvc {},
258+ },
259+ CustomKMS : k ,
260+ KeyTypeValue : kms .ED25519Type ,
261+ KeyAgreementTypeValue : kms .X25519ECDHKWType ,
262+ }
263+
264+ ctx := & context {
265+ outboundDispatcher : prov .OutboundDispatcher (),
266+ crypto : & tinkcrypto.Crypto {},
267+ kms : k ,
268+ keyType : kms .ED25519Type ,
269+ keyAgreementType : kms .X25519ECDHKWType ,
270+ }
271+
272+ _ , pubKey , err := ctx .kms .CreateAndExportPubKeyBytes (kms .ED25519Type )
273+ require .NoError (t , err )
274+
275+ ctx .vdRegistry = & mockvdr.MockVDRegistry {CreateValue : createDIDDocWithKey (pubKey )}
276+
277+ connRec , err := connection .NewRecorder (prov )
278+ require .NoError (t , err )
279+ require .NotNil (t , connRec )
280+
281+ ctx .connectionRecorder = connRec
282+
283+ doc , err := ctx .vdRegistry .Create (testMethod , nil )
284+ require .NoError (t , err )
285+
286+ s , err := New (prov )
287+ require .NoError (t , err )
288+
289+ actionCh := make (chan service.DIDCommAction , 10 )
290+ err = s .RegisterActionEvent (actionCh )
291+ require .NoError (t , err )
292+
293+ statusCh := make (chan service.StateMsg , 10 )
294+ err = s .RegisterMsgEvent (statusCh )
295+ require .NoError (t , err )
296+
297+ completedFlag := make (chan struct {})
298+ respondedFlag := make (chan string )
299+
300+ go msgEventListener (t , statusCh , respondedFlag , completedFlag )
301+ go func () { service .AutoExecuteActionEvent (actionCh ) }()
302+
303+ invitation := & Invitation {
304+ Type : InvitationMsgType ,
305+ ID : randomString (),
306+ Label : "Bob" ,
307+ RecipientKeys : []string {base58 .Encode (pubKey )},
308+ ServiceEndpoint : "http://alice.agent.example.com:8081" ,
309+ }
310+
311+ err = ctx .connectionRecorder .SaveInvitation (invitation .ID , invitation )
312+ require .NoError (t , err )
313+
314+ thid := randomString ()
315+
316+ // Invitation was previously sent by Alice to Bob.
317+ // Bob now sends a connection Request
318+ connRequest , err := json .Marshal (
319+ & Request {
320+ Type : RequestMsgType ,
321+ ID : thid ,
322+ Label : "Bob" ,
323+ Thread : & decorator.Thread {
324+ PID : invitation .ID ,
325+ },
326+ Connection : & Connection {
327+ DID : doc .DIDDocument .ID ,
328+ DIDDoc : doc .DIDDocument ,
329+ },
330+ })
331+ require .NoError (t , err )
332+ requestMsg , err := service .ParseDIDCommMsgMap (connRequest )
333+ require .NoError (t , err )
334+ _ , err = s .HandleInbound (requestMsg , service .NewDIDCommContext (doc .DIDDocument .ID , "" , nil ))
335+ require .NoError (t , err )
336+
337+ var connID string
338+ select {
339+ case connID = <- respondedFlag :
340+ case <- time .After (2 * time .Second ):
341+ require .Fail (t , "didn't receive connection ID" )
342+ }
343+
344+ connRecord , err := s .connectionRecorder .GetConnectionRecord (connID )
345+ // Alice automatically sends connection Response to Bob
346+ // Bob replies with Problem Report
347+ prbRpt , err := json .Marshal (
348+ & problemReport {
349+ ID : randomString (),
350+ Type : ProblemReportMsgType ,
351+ Thread : & decorator.Thread {ID : connRecord .ThreadID },
352+ })
353+ require .NoError (t , err )
354+
355+ prbRptMsg , err := service .ParseDIDCommMsgMap (prbRpt )
356+ require .NoError (t , err )
357+
358+ _ , err = s .HandleInbound (prbRptMsg , service .NewDIDCommContext (doc .DIDDocument .ID , "" , nil ))
359+ require .NoError (t , err )
360+
361+ validateState (t , s , thid , findNamespace (RequestMsgType ), (& responded {}).Name ())
362+
363+ _ , err = ctx .connectionRecorder .GetConnectionRecord (connID )
364+ require .ErrorContains (t , err , "data not found" )
365+
366+ _ , err = s .HandleInbound (requestMsg , service .NewDIDCommContext (doc .DIDDocument .ID , "" , nil ))
367+ require .NoError (t , err )
368+
369+ // Finally Bob replies with an ACK
370+ ack , err := json .Marshal (
371+ & model.Ack {
372+ Type : AckMsgType ,
373+ ID : randomString (),
374+ Status : "OK" ,
375+ Thread : & decorator.Thread {ID : connRecord .ThreadID },
376+ })
377+ require .NoError (t , err )
378+
379+ ackMsg , err := service .ParseDIDCommMsgMap (ack )
380+ require .NoError (t , err )
381+
382+ _ , err = s .HandleInbound (ackMsg , service .NewDIDCommContext (doc .DIDDocument .ID , "" , nil ))
383+ require .NoError (t , err )
384+
385+ select {
386+ case <- completedFlag :
387+ case <- time .After (4 * time .Second ):
388+ require .Fail (t , "didn't receive post event complete" )
389+ }
390+
391+ validateState (t , s , connRecord .ThreadID , findNamespace (AckMsgType ), (& completed {}).Name ())
392+ }
393+
394+ func msgEventListener (t * testing.T , statusCh chan service.StateMsg , respondedFlag chan string , completedFlag chan struct {}) {
251395 for e := range statusCh {
252396 require .Equal (t , LegacyConnection , e .ProtocolName )
253397
@@ -272,11 +416,11 @@ func msgEventListener(t *testing.T, statusCh chan service.StateMsg, respondedFla
272416 close (completedFlag )
273417 }
274418
275- if e .StateID == "responded" {
419+ if e .StateID == "responded" && e . Msg . Type () != ProblemReportMsgType {
276420 // validate connectionID received during state transition with original connectionID
277421 require .NotNil (t , prop .ConnectionID ())
278422 require .NotNil (t , prop .InvitationID ())
279- close ( respondedFlag )
423+ respondedFlag <- prop . ConnectionID ( )
280424 }
281425 }
282426 }
0 commit comments