diff --git a/wrappers/ios/libindy-pod/libindy-demo.xcodeproj/project.pbxproj b/wrappers/ios/libindy-pod/libindy-demo.xcodeproj/project.pbxproj index 3eb26b1711..8dfda9f97f 100644 --- a/wrappers/ios/libindy-pod/libindy-demo.xcodeproj/project.pbxproj +++ b/wrappers/ios/libindy-pod/libindy-demo.xcodeproj/project.pbxproj @@ -7,6 +7,13 @@ objects = { /* Begin PBXBuildFile section */ + 5D0481B71F46E1BD00ED79A2 /* AgentMediumConnect.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D0481B61F46E1BD00ED79A2 /* AgentMediumConnect.m */; }; + 5D0481B91F46FEC800ED79A2 /* AgentMediumListen.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D0481B81F46FEC800ED79A2 /* AgentMediumListen.m */; }; + 5D0481BB1F46FEE400ED79A2 /* AgentMediumAddIdentity.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D0481BA1F46FEE400ED79A2 /* AgentMediumAddIdentity.m */; }; + 5D0481BD1F46FEFA00ED79A2 /* AgentMediumRemoveIdentity.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D0481BC1F46FEFA00ED79A2 /* AgentMediumRemoveIdentity.m */; }; + 5D0481BF1F46FF0D00ED79A2 /* AgentMediumSend.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D0481BE1F46FF0D00ED79A2 /* AgentMediumSend.m */; }; + 5D0481C11F46FF2700ED79A2 /* AgentMediumCloseConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D0481C01F46FF2700ED79A2 /* AgentMediumCloseConnection.m */; }; + 5D0481C31F46FF4000ED79A2 /* AgentMediumCloseListener.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D0481C21F46FF4000ED79A2 /* AgentMediumCloseListener.m */; }; 5D13F6C61F161E800024DFB3 /* SequenceUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D13F6C51F161E800024DFB3 /* SequenceUtils.m */; }; 5D13F6C81F161E9C0024DFB3 /* libindy.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D13F6C71F161E9C0024DFB3 /* libindy.framework */; }; 5D13F6C91F161E9C0024DFB3 /* libindy.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 5D13F6C71F161E9C0024DFB3 /* libindy.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -18,7 +25,6 @@ 5DF013E21F1604C200076592 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DF013DB1F1604C200076592 /* ViewController.m */; }; 5DF0149D1F1605EB00076592 /* Base58Test.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DF014661F1605EB00076592 /* Base58Test.m */; }; 5DF0149E1F1605EB00076592 /* AgentHighCases.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DF014691F1605EB00076592 /* AgentHighCases.m */; }; - 5DF0149F1F1605EB00076592 /* AgentMediumCases.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DF0146A1F1605EB00076592 /* AgentMediumCases.m */; }; 5DF014A01F1605EB00076592 /* AnoncredsHighCases.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DF0146C1F1605EB00076592 /* AnoncredsHighCases.m */; }; 5DF014A11F1605EB00076592 /* AnoncredsMediumCases.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DF0146D1F1605EB00076592 /* AnoncredsMediumCases.m */; }; 5DF014A21F1605EB00076592 /* AnoncredsMediumCasesDemos.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DF0146E1F1605EB00076592 /* AnoncredsMediumCasesDemos.m */; }; @@ -90,6 +96,13 @@ 2EF9598E5D9881BADD8E4EB7 /* Pods-libindy-demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-libindy-demo.release.xcconfig"; path = "Pods/Target Support Files/Pods-libindy-demo/Pods-libindy-demo.release.xcconfig"; sourceTree = ""; }; 3E2A7F851EC36186006194EC /* libindy-demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "libindy-demo.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 3E2A7F9E1EC36187006194EC /* libindy-demoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "libindy-demoTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; + 5D0481B61F46E1BD00ED79A2 /* AgentMediumConnect.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AgentMediumConnect.m; sourceTree = ""; }; + 5D0481B81F46FEC800ED79A2 /* AgentMediumListen.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AgentMediumListen.m; sourceTree = ""; }; + 5D0481BA1F46FEE400ED79A2 /* AgentMediumAddIdentity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AgentMediumAddIdentity.m; sourceTree = ""; }; + 5D0481BC1F46FEFA00ED79A2 /* AgentMediumRemoveIdentity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AgentMediumRemoveIdentity.m; sourceTree = ""; }; + 5D0481BE1F46FF0D00ED79A2 /* AgentMediumSend.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AgentMediumSend.m; sourceTree = ""; }; + 5D0481C01F46FF2700ED79A2 /* AgentMediumCloseConnection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AgentMediumCloseConnection.m; sourceTree = ""; }; + 5D0481C21F46FF4000ED79A2 /* AgentMediumCloseListener.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AgentMediumCloseListener.m; sourceTree = ""; }; 5D13F6C41F161E800024DFB3 /* SequenceUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SequenceUtils.h; sourceTree = ""; }; 5D13F6C51F161E800024DFB3 /* SequenceUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SequenceUtils.m; sourceTree = ""; }; 5D13F6C71F161E9C0024DFB3 /* libindy.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = libindy.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -104,7 +117,6 @@ 5DF013DB1F1604C200076592 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 5DF014661F1605EB00076592 /* Base58Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Base58Test.m; sourceTree = ""; }; 5DF014691F1605EB00076592 /* AgentHighCases.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AgentHighCases.m; sourceTree = ""; }; - 5DF0146A1F1605EB00076592 /* AgentMediumCases.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AgentMediumCases.m; sourceTree = ""; }; 5DF0146C1F1605EB00076592 /* AnoncredsHighCases.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AnoncredsHighCases.m; sourceTree = ""; }; 5DF0146D1F1605EB00076592 /* AnoncredsMediumCases.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AnoncredsMediumCases.m; sourceTree = ""; }; 5DF0146E1F1605EB00076592 /* AnoncredsMediumCasesDemos.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AnoncredsMediumCasesDemos.m; sourceTree = ""; }; @@ -196,6 +208,20 @@ name = Products; sourceTree = ""; }; + 5D0481B41F46E16B00ED79A2 /* Medium Cases */ = { + isa = PBXGroup; + children = ( + 5D0481B61F46E1BD00ED79A2 /* AgentMediumConnect.m */, + 5D0481B81F46FEC800ED79A2 /* AgentMediumListen.m */, + 5D0481BA1F46FEE400ED79A2 /* AgentMediumAddIdentity.m */, + 5D0481BC1F46FEFA00ED79A2 /* AgentMediumRemoveIdentity.m */, + 5D0481BE1F46FF0D00ED79A2 /* AgentMediumSend.m */, + 5D0481C01F46FF2700ED79A2 /* AgentMediumCloseConnection.m */, + 5D0481C21F46FF4000ED79A2 /* AgentMediumCloseListener.m */, + ); + path = "Medium Cases"; + sourceTree = ""; + }; 5DF013D01F1604C100076592 /* libindy-demo */ = { isa = PBXGroup; children = ( @@ -241,8 +267,8 @@ 5DF014681F1605EB00076592 /* Agent */ = { isa = PBXGroup; children = ( + 5D0481B41F46E16B00ED79A2 /* Medium Cases */, 5DF014691F1605EB00076592 /* AgentHighCases.m */, - 5DF0146A1F1605EB00076592 /* AgentMediumCases.m */, ); path = Agent; sourceTree = ""; @@ -589,10 +615,12 @@ 5DF014A41F1605EB00076592 /* LedgerHignCases.mm in Sources */, 5DF014AC1F1605EB00076592 /* WalletHighCases.m in Sources */, 5DF014B41F1605EB00076592 /* ClosureUtils.m in Sources */, - 5DF0149F1F1605EB00076592 /* AgentMediumCases.m in Sources */, 5DF014B91F1605EB00076592 /* TestUtils.m in Sources */, + 5D0481C31F46FF4000ED79A2 /* AgentMediumCloseListener.m in Sources */, 5DF014A31F1605EB00076592 /* LedgerAttribRequest.mm in Sources */, 5DF014A21F1605EB00076592 /* AnoncredsMediumCasesDemos.m in Sources */, + 5D0481C11F46FF2700ED79A2 /* AgentMediumCloseConnection.m in Sources */, + 5D0481B71F46E1BD00ED79A2 /* AgentMediumConnect.m in Sources */, 5DF014B61F1605EB00076592 /* LedgerUtils.mm in Sources */, 5DF014AD1F1605EB00076592 /* WalletMediumCases.m in Sources */, 5DF014A81F1605EB00076592 /* PoolHighCases.m in Sources */, @@ -610,8 +638,12 @@ 5DF014AF1F1605EB00076592 /* LedgerDemo.mm in Sources */, 5DF014B21F1605EB00076592 /* AgentUtils.m in Sources */, 5DF014AB1F1605EB00076592 /* SignusMediumCases.m in Sources */, + 5D0481BD1F46FEFA00ED79A2 /* AgentMediumRemoveIdentity.m in Sources */, 5DF014BC1F1605EB00076592 /* NSDictionary+JSON.m in Sources */, + 5D0481BB1F46FEE400ED79A2 /* AgentMediumAddIdentity.m in Sources */, 5DF014A11F1605EB00076592 /* AnoncredsMediumCases.m in Sources */, + 5D0481B91F46FEC800ED79A2 /* AgentMediumListen.m in Sources */, + 5D0481BF1F46FF0D00ED79A2 /* AgentMediumSend.m in Sources */, 5DF014A61F1605EB00076592 /* LedgerNymRequest.mm in Sources */, 5DF014BD1F1605EB00076592 /* NSString+Validation.m in Sources */, 5DF014BA1F1605EB00076592 /* WalletUtils.m in Sources */, diff --git a/wrappers/ios/libindy-pod/libindy-demo.xcodeproj/xcshareddata/xcschemes/libindy-demo.xcscheme b/wrappers/ios/libindy-pod/libindy-demo.xcodeproj/xcshareddata/xcschemes/libindy-demo.xcscheme index 9b00a536db..48691bc6b8 100644 --- a/wrappers/ios/libindy-pod/libindy-demo.xcodeproj/xcshareddata/xcschemes/libindy-demo.xcscheme +++ b/wrappers/ios/libindy-pod/libindy-demo.xcodeproj/xcshareddata/xcschemes/libindy-demo.xcscheme @@ -58,6 +58,9 @@ + + diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/AgentHighCases.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/AgentHighCases.m index 138214038c..43f1370f25 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/AgentHighCases.m +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/AgentHighCases.m @@ -222,7 +222,7 @@ - (void)testAgentConnectWorksForAllDataInWalletPresent // 1. obtain wallet handle IndyHandle walletHandle; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); @@ -323,6 +323,345 @@ - (void)testAgentListenWorksForAllDataInWalletPresent [TestUtils cleanupStorage]; } +- (void)testAgentListenWorksForGetSenderDataFromLedger +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. create and open pool ledger + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. create and open trustee wallet + + IndyHandle trusteeWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&trusteeWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for trusteeWallet"); + IndyHandle listenerWallet = trusteeWallet; + + // 3. create and open sender wallet + + IndyHandle senderWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&senderWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for senderWallet"); + + // 4. create and store trusteeDid + NSString *trusteeDid; + NSString *trusteeVerkey; + NSString *trusteePk; + + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:trusteeWallet + seed:[TestUtils trusteeSeed] + outMyDid:&trusteeDid + outMyVerkey:&trusteeVerkey + outMyPk:&trusteePk]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trusteeDid"); + + // 5. create and store senderDid + NSString *senderDid; + NSString *senderVerkey; + NSString *senderPk; + + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:senderWallet + seed:[TestUtils trusteeSeed] + outMyDid:&senderDid + outMyVerkey:&senderVerkey + outMyPk:&senderPk]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for senderDid"); + + // 6. create and store listenerDid + NSString *listenerDid; + NSString *listenerVerkey; + NSString *listenerPk; + + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:trusteeWallet + seed:[TestUtils trusteeSeed] + outMyDid:&listenerDid + outMyVerkey:&listenerVerkey + outMyPk:&listenerPk]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for listenerDid"); + + // 7 Build & submit nym request + + NSString *senderNymJson; + ret = [[LedgerUtils sharedInstance] buildNymRequestWithSubmitterDid:trusteeDid + targetDid:senderDid + verkey:senderVerkey + alias:nil + role:nil + outRequest:&senderNymJson]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildNymRequestWithSubmitterDid() failed"); + + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:trusteeWallet + submitterDid:trusteeDid + requestJson:senderNymJson + outResponseJson:nil]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::signAndSubmitRequestWithPoolHandle() failed for senderNymJson"); + + // 8. Build & submit attribute request + NSString *raw = [NSString stringWithFormat:@"{\"endpoint\":{\"ha\":\"%@\", \"verkey\":\"%@\"}}",[TestUtils endpoint], senderPk]; + NSString *senderAttribJson; + ret = [[LedgerUtils sharedInstance] buildAttribRequestWithSubmitterDid:senderDid + targetDid:senderDid + hash:nil + raw:raw + enc:nil + resultJson:&senderAttribJson]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildAttribRequestWithSubmitterDid() failed"); + + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:senderWallet + submitterDid:senderDid + requestJson:senderAttribJson + outResponseJson:nil]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::signAndSubmitRequestWithPoolHandle() failed for senderAttribJson"); + + // 9. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 10. add identity + + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 11. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:senderWallet + theirDid:listenerDid + theirPk:listenerPk + theirVerkey:listenerVerkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 12. connect + + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + messageCallback:nil + outConnectionHandle:nil]; + + // 13. Close + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentListenWorksForPassedOnConnectCallback +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + NSString *msg = @"New Connection"; + + // 1. create and open pool ledger + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. create and open wallet + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + //3. obtain my did + + NSString *did; + NSString *verkey; + NSString *pubKey; + + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubKey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 5. listen + + // connection callback + XCTestExpectation* listenerConnectionCompletionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"listener completion finished"]; + __block NSString *connectionCallbackIsCalled; + void (^connectionCallback)(IndyHandle, IndyHandle) = ^(IndyHandle xListenerHandle, IndyHandle xConnectionHandle) { + connectionCallbackIsCalled = msg; + NSLog(@"AgentHighCases::testAgentListenWorksForPassedOnConnectCallback:: listener's connectionCallback triggered."); + [listenerConnectionCompletionExpectation fulfill]; + }; + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:connectionCallback + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 6. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 7. connect + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:walletHandle + senderDid:did + receiverDid:did + messageCallback:nil + outConnectionHandle:nil]; + + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle() failed"); + + + [self waitForExpectations: @[listenerConnectionCompletionExpectation] timeout:[TestUtils defaultTimeout]]; + XCTAssertTrue([msg isEqualToString:connectionCallbackIsCalled], @"conenction callback is not called!"); + + // 8. close everything + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [TestUtils cleanupStorage]; +} + +- (void)testAgentListenWorksForPassedOnMessageCallback +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + NSString *msg = @"Message from client"; + + // 1. create and open pool ledger + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. create and open wallet + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + //3. obtain my did + + NSString *did; + NSString *verkey; + NSString *pubKey; + + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubKey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 5. listen + + XCTestExpectation* messageFromClientCompletionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"listener completion finished"]; + __block NSString *clientMessage; + // message from client callback + void (^messageFromClientCallback)(IndyHandle, NSString *) = ^(IndyHandle xConnectionHandle, NSString * message) { + clientMessage = message; + NSLog(@"AgentHighCases::testAgentSendWorksForAllDataInWalletPresent::messageFromClientCallback triggered with message: %@", message); + [messageFromClientCompletionExpectation fulfill]; + }; + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:messageFromClientCallback + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 6. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 7. connect + IndyHandle connectionHandle = 0; + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:walletHandle + senderDid:did + receiverDid:did + messageCallback:nil + outConnectionHandle:&connectionHandle]; + + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle() failed"); + + // 8. send message + + ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:connectionHandle + message:msg]; + XCTAssertEqual(ret.code,Success, @"AgentUtils::sendWithConnectionHandler failed"); + + [self waitForExpectations: @[messageFromClientCompletionExpectation] timeout:[TestUtils defaultTimeout]]; + XCTAssertTrue([clientMessage isEqualToString:msg], @"wrong clientMessage"); + + + // 8. close everything + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[AgentUtils sharedInstance] closeListener:connectionHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [TestUtils cleanupStorage]; +} + + // MARK: - Add identity - (void)testAgentAddIdentityWorks diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/AgentMediumCases.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/AgentMediumCases.m deleted file mode 100644 index 2b3dd37b9a..0000000000 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/AgentMediumCases.m +++ /dev/null @@ -1,333 +0,0 @@ -// -// AgentMediumCases.m -// libindy-demo -// -// Created by Anastasia Tarasova on 22.06.17. -// Copyright © 2017 Kirill Neznamov. All rights reserved. -// - -#import -#import -#import -#import "TestUtils.h" - -@interface AgentMediumCases : XCTestCase - -@end - -@implementation AgentMediumCases - -- (void)setUp -{ - [super setUp]; - // Put setup code here. This method is called before the invocation of each test method in the class. -} - -- (void)tearDown -{ - // Put teardown code here. This method is called after the invocation of each test method in the class. - [super tearDown]; -} - -// MARK: - Add identity -- (void)testAgentAddIdentityWorksForIncomingConnectionRequireLedgerRequestButPoolHandleIsInvalid -{ - [TestUtils cleanupStorage]; - NSError *ret; - NSString *endpoint = @"127.0.0.1:9812"; - NSString *poolName = @"indy_agent_add_identity_works_for_incoming_connection_require_ledger_request_but_pool_handle_is_invalid"; - - // 1. Obtain pool handle - IndyHandle poolHandle = 0; - ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName - poolHandle:&poolHandle]; - XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); - - // 2. Obtain listener's wallet - IndyHandle listenerWalletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName - xtype:nil - handle:&listenerWalletHandle]; - XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenWalletWithPoolName() failed"); - - // 3. Obtain trustee's wallet - IndyHandle trusteeWalletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName - xtype:nil - handle:&trusteeWalletHandle]; - XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenWalletWithPoolName() failed"); - - // 4. Create and store listener's did - NSString *listenerDid; - NSString *listenerVerKey; - NSString *listenerPubKey; - ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWalletHandle - seed:nil - outMyDid:&listenerDid - outMyVerkey:&listenerVerKey - outMyPk:&listenerPubKey]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for listener"); - - // 5. create trustee did - - NSString *trusteeDid; - ret =[[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:trusteeWalletHandle - seed:@"000000000000000000000000Trustee1" - outMyDid:&trusteeDid - outMyVerkey:nil - outMyPk:nil]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); - - // 6. Build nym request for listener - NSString *listenerNymRequest; - ret = [[LedgerUtils sharedInstance] buildNymRequestWithSubmitterDid:trusteeDid - targetDid:listenerDid - verkey:listenerVerKey - alias:nil - role:nil - outRequest:&listenerNymRequest]; - XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildNymRequestWithSubmitterDid() failed"); - - // 7. Sign and submit listener's nym request - NSString *listenerNymResponse; - ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle - walletHandle:trusteeWalletHandle - submitterDid:trusteeDid - requestJson:listenerNymRequest - outResponseJson:&listenerNymResponse]; - XCTAssertEqual(ret.code, Success, @"LedgerUtils::signAndSubmitRequestWithPoolHandle() failed for listenerNymRequest"); - - // 8. Build listener attribute request - NSString *rawJson =[NSString stringWithFormat:@"{\"endpoint\":{\"ha\":\"%@\", \"verkey\":\"%@\"}}", endpoint, listenerPubKey]; - NSString *listenerAttributeRequest; - ret = [[LedgerUtils sharedInstance] buildAttribRequestWithSubmitterDid:listenerDid - targetDid:listenerDid - hash:nil - raw:rawJson - enc:nil - resultJson:&listenerAttributeRequest]; - XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildAttribRequestWithSubmitterDid() failed"); - - // 9. Sign and submit listener's attribute request - NSString *listenerAttributeResponse; - ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle - walletHandle:listenerWalletHandle - submitterDid:listenerDid - requestJson:listenerAttributeRequest - outResponseJson:&listenerAttributeResponse]; - XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildAttribRequestWithSubmitterDid() failed for listenerAttributeResponse"); - - // 10. listen - IndyHandle listenerHandle = 0; - ret = [[AgentUtils sharedInstance] listenForEndpoint:endpoint - connectionCallback:nil - messageCallback:nil - outListenerHandle:&listenerHandle]; - XCTAssertEqual(ret.code, Success, @"LedgerUtils::listenWithEndpoint()"); - - // 11. Add identity - IndyHandle invalidPoolHandle = listenerHandle; - ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle - poolHandle:invalidPoolHandle - walletHandle:listenerWalletHandle - did:listenerDid]; - XCTAssertEqual(ret.code, Success, @"LedgerUtils::addIdentityForListenerHandle() failed for listenerDid"); - - /* TODO - * Currently pool_handle and wallet_handle of add_identity will be checked only at required: - * when listener will check incoming connection and go to ledger for info. - * As result, add_identity will be successful but next connect will fail. - * Possible the test should be split into two: - * - add_identity_works_for_incompatible_pool_and_wallet - * with immediately check in the library - * - connect_works_for_incorrect_connect_request - * actual info in ledger or listener_wallet, wrong public key in sender_wallet - */ - - // 12. Connect - NSString *senderDid = [NSString stringWithString:trusteeDid]; - IndyHandle senderWalletHandle = trusteeWalletHandle; - - ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle - walletHandle:senderWalletHandle - senderDid:senderDid - receiverDid:listenerDid - messageCallback:nil - outConnectionHandle:nil]; - XCTAssertEqual(ret.code, CommonInvalidState, @"AgentUtils::connectWithPoolHandle() returned wrong code"); - [TestUtils cleanupStorage]; -} - -// MARK: - Close connection - -- (void)testAgentCloseConnectionWorksForIncorrectConnectionHandle -{ - [TestUtils cleanupStorage]; - NSError *ret; - - // 1. create and open wallet handle - IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool6" - xtype:nil - handle:&walletHandle]; - XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); - - // 2. obtain did - NSString *did; - NSString *verKey; - NSString *pubKey; - ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle - seed:nil - outMyDid:&did - outMyVerkey:&verKey - outMyPk:&pubKey]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); - - // 3. listen - NSString *endpoint = @"127.0.0.1:9807"; - - XCTestExpectation* messageExpectation = [[ XCTestExpectation alloc] initWithDescription: @"message completion finished"]; - - IndyHandle listenerHandler = 0; - __block NSString* messageFromClient; - ret = [[AgentUtils sharedInstance] listenForEndpoint:endpoint - connectionCallback:nil - messageCallback:^(IndyHandle connectionHandle, NSString *message) - { - messageFromClient = message; - [messageExpectation fulfill]; - } - outListenerHandle:&listenerHandler]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::listenWithEndpoint() failed"); - - // 4. add identity - ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandler - poolHandle:-1 - walletHandle:walletHandle - did:did]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); - - // 5. store their did from parts - ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle - theirDid:did - theirPk:pubKey - theirVerkey:verKey - endpoint:endpoint]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::storeTheirDidFromPartsWithWalletHandle() failed"); - - // 6. Connect - IndyHandle connectionHandle = 0; - ret = [[AgentUtils sharedInstance] connectWithPoolHandle:0 - walletHandle:walletHandle - senderDid:did - receiverDid:did - messageCallback:nil - outConnectionHandle:&connectionHandle]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle() failed"); - - // 7. Close connection - ret = [[AgentUtils sharedInstance] closeConnection:connectionHandle + 100]; - XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::closeConnection() returned wrong code"); - - // 8. send - NSString *clientMessage = @"msg_from_cli_to_srv"; - ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:connectionHandle - message:clientMessage]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::sendWithConnectionHandler() failed"); - - // 9. wait for message callback - [self waitForExpectations: @[messageExpectation] timeout:[TestUtils defaultTimeout]]; - - XCTAssertTrue([messageFromClient isEqualToString:clientMessage], @"wrong message from client!"); - - [TestUtils cleanupStorage]; -} - -// MARK: - Close listener - -- (void)testAgentCloseListenerWorksForIncorrectHandle -{ - [TestUtils cleanupStorage]; - NSError *ret; - - // 1.Create and open wallet - IndyHandle walletHandle; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool9" - xtype:nil - handle:&walletHandle]; - XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); - - // 2. obtain did - NSString *did; - NSString *verKey; - NSString *pubKey; - ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle - seed:nil - outMyDid:&did - outMyVerkey:&verKey - outMyPk:&pubKey]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); - - // 3. listen - NSString *endpoint = @"127.0.0.1:9809"; - - XCTestExpectation* messageExpectation = [[ XCTestExpectation alloc] initWithDescription: @"message completion finished"]; - - IndyHandle listenerHandler = 0; - __block NSString* messageFromClient; - ret = [[AgentUtils sharedInstance] listenForEndpoint:endpoint - connectionCallback:nil - messageCallback:^(IndyHandle connectionHandle, NSString *message) - { - messageFromClient = message; - [messageExpectation fulfill]; - } - outListenerHandle:&listenerHandler]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::listenWithEndpoint() failed"); - - // 4. add identity - ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandler - poolHandle:-1 - walletHandle:walletHandle - did:did]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); - - // 5. store their did from parts - ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle - theirDid:did - theirPk:pubKey - theirVerkey:verKey - endpoint:endpoint]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::storeTheirDidFromPartsWithWalletHandle() failed"); - - // 6. Connect - IndyHandle connectionHandle = 0; - ret = [[AgentUtils sharedInstance] connectWithPoolHandle:0 - walletHandle:walletHandle - senderDid:did - receiverDid:did - messageCallback:nil - outConnectionHandle:&connectionHandle]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle() failed"); - - // 7. close listener - IndyHandle incorrectListenerHandle = connectionHandle; // + 1; - ret = [[AgentUtils sharedInstance] closeListener:incorrectListenerHandle]; - XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::closeListener() returned wrong code"); - - // 8. send - NSString *clientMessage = @"msg_from_cli_to_srv"; - ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:connectionHandle - message:clientMessage]; - XCTAssertEqual(ret.code, Success, @"AgentUtils::sendWithConnectionHandler() failed"); - - // 9. wait for message callback - [self waitForExpectations: @[messageExpectation] timeout:[TestUtils defaultTimeout]]; - - XCTAssertTrue([messageFromClient isEqualToString:clientMessage], @"wrong message from client!"); - - [TestUtils cleanupStorage]; -} - - -@end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumAddIdentity.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumAddIdentity.m new file mode 100644 index 0000000000..733bbf4a8f --- /dev/null +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumAddIdentity.m @@ -0,0 +1,452 @@ +// +// AgentMediumAddIdentity.m +// libindy-demo +// +// Created by Anastasia Tarasova on 18/08/2017. +// Copyright © 2017 Kirill Neznamov. All rights reserved. +// + +#import +#import +#import +#import "TestUtils.h" + +@interface AgentMediumAddIdentity : XCTestCase + +@end + +@implementation AgentMediumAddIdentity + +- (void)setUp +{ + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown +{ + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testAgentAddIdentityWorksForIncomingConnectionRequireLedgerRequestButPoolHandleIsInvalid +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = @"indy_agent_add_identity_works_for_incoming_connection_require_ledger_request_but_pool_handle_is_invalid"; + + // 1. Obtain pool handle + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. Obtain listener's wallet + IndyHandle listenerWalletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWalletHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenWalletWithPoolName() failed"); + + // 3. Obtain trustee's wallet + IndyHandle trusteeWalletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&trusteeWalletHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenWalletWithPoolName() failed"); + + // 4. Create and store listener's did + NSString *listenerDid; + NSString *listenerVerKey; + NSString *listenerPubKey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWalletHandle + seed:nil + outMyDid:&listenerDid + outMyVerkey:&listenerVerKey + outMyPk:&listenerPubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for listener"); + + // 5. create trustee did + + NSString *trusteeDid; + ret =[[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:trusteeWalletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&trusteeDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); + NSString *senderDid = [NSString stringWithString:trusteeDid]; + IndyHandle senderWallet = trusteeWalletHandle; + + // 6. Build nym request for listener + NSString *listenerNymRequest; + ret = [[LedgerUtils sharedInstance] buildNymRequestWithSubmitterDid:trusteeDid + targetDid:listenerDid + verkey:listenerVerKey + alias:nil + role:nil + outRequest:&listenerNymRequest]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildNymRequestWithSubmitterDid() failed"); + + // 7. Sign and submit listener's nym request + NSString *listenerNymResponse; + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:trusteeWalletHandle + submitterDid:trusteeDid + requestJson:listenerNymRequest + outResponseJson:&listenerNymResponse]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::signAndSubmitRequestWithPoolHandle() failed for listenerNymRequest"); + + // 8. Build listener attribute request + NSString *rawJson =[NSString stringWithFormat:@"{\"endpoint\":{\"ha\":\"%@\", \"verkey\":\"%@\"}}", [TestUtils endpoint], listenerPubKey]; + NSString *listenerAttributeRequest; + ret = [[LedgerUtils sharedInstance] buildAttribRequestWithSubmitterDid:listenerDid + targetDid:listenerDid + hash:nil + raw:rawJson + enc:nil + resultJson:&listenerAttributeRequest]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildAttribRequestWithSubmitterDid() failed"); + + // 9. Sign and submit listener's attribute request + NSString *listenerAttributeResponse; + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:listenerWalletHandle + submitterDid:listenerDid + requestJson:listenerAttributeRequest + outResponseJson:&listenerAttributeResponse]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildAttribRequestWithSubmitterDid() failed for listenerAttributeResponse"); + + // 10. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::listenWithEndpoint()"); + + // 11. Add identity + IndyHandle invalidPoolHandle = listenerHandle; + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:invalidPoolHandle + walletHandle:listenerWalletHandle + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::addIdentityForListenerHandle() failed for listenerDid"); + + /* TODO + * Currently pool_handle and wallet_handle of add_identity will be checked only at required: + * when listener will check incoming connection and go to ledger for info. + * As result, add_identity will be successful but next connect will fail. + * Possible the test should be split into two: + * - add_identity_works_for_incompatible_pool_and_wallet + * with immediately check in the library + * - connect_works_for_incorrect_connect_request + * actual info in ledger or listener_wallet, wrong public key in sender_wallet + */ + + // 12. Connect + + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, CommonInvalidState, @"AgentUtils::connectWithPoolHandle() returned wrong code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWalletHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle: poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentAddIdentityWorksForUnknownDid +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 3. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:1 + walletHandle:walletHandle + did:@"unknownDid"]; + XCTAssertEqual(ret.code, WalletNotFoundError, @"AgentUtils::addIdentityForListenerHandle() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentAddIdentityWorksForInvalidListenerHandle +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. obtain receiver's did + NSString *receiverDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&receiverDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 3. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 4. add identity + IndyHandle invalidListenerHandle = listenerHandle + 1; + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:invalidListenerHandle + poolHandle:1 + walletHandle:walletHandle + did:receiverDid]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::addIdentityForListenerHandle() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentAddIdentityWorksForTwice +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 3. obtain did + NSString *did; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. add identity 1 + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed for the first attempt"); + + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::addIdentityForListenerHandle() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentAssIdentityWorksForTwoDinOnSameListener +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 3. obtain did 1 + NSString *did; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. obtain did 2 + NSString *did2; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did2 + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndStoreMyDidWithWalletHandle() failed for did2"); + + // 5. add first identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed for did"); + + // 6. add second identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did2]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed for did2"); + + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentAddIdentityWorksForInvalidWalletHandle +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 3. obtain receiver's did + NSString *receiverDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&receiverDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 5. add identity + IndyHandle invalidWalletHandle = walletHandle + 1; + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:1 + walletHandle:invalidWalletHandle + did:receiverDid]; + XCTAssertEqual(ret.code, WalletInvalidHandle, @"AgentUtils::addIdentityForListenerHandle() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentAddIdentityWorksAfterRemove +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 3. obtain did + NSString *did; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed for the first attempt"); + + // 5. remove identity + ret = [[AgentUtils sharedInstance] removeIdentity:did + listenerHandle:listenerHandle + walletHandle:walletHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::removeIdentity() failed"); + + // 6. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed for second attempt"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +@end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumCloseConnection.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumCloseConnection.m new file mode 100644 index 0000000000..1f7585e272 --- /dev/null +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumCloseConnection.m @@ -0,0 +1,192 @@ +// +// AgentMediumCloseConnection.m +// libindy-demo +// +// Created by Anastasia Tarasova on 18/08/2017. +// Copyright © 2017 Kirill Neznamov. All rights reserved. +// + +#import +#import +#import +#import "TestUtils.h" + +@interface AgentMediumCloseConnection : XCTestCase + +@end + +@implementation AgentMediumCloseConnection +- (void)setUp +{ + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown +{ + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +// MARK: - Close connection + +- (void)testAgentCloseConnectionWorksForIncorrectConnectionHandle +{ + [TestUtils cleanupStorage]; + NSError *ret; + + // 1. create and open wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. obtain did + NSString *did; + NSString *verKey; + NSString *pubKey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verKey + outMyPk:&pubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + + // 3. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubKey + theirVerkey:verKey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 4. listen + + XCTestExpectation* messageExpectation = [[ XCTestExpectation alloc] initWithDescription: @"message completion finished"]; + + IndyHandle listenerHandler = 0; + __block NSString* messageFromClient; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:^(IndyHandle connectionHandle, NSString *message) + { + messageFromClient = message; + [messageExpectation fulfill]; + } + outListenerHandle:&listenerHandler]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenWithEndpoint() failed"); + + // 5. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandler + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 6. Connect + IndyHandle connectionHandle = 0; + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + messageCallback:nil + outConnectionHandle:&connectionHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle() failed"); + + // 7. Close connection + ret = [[AgentUtils sharedInstance] closeConnection:connectionHandle + 100]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::closeConnection() returned wrong code"); + + // 8. send + ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:connectionHandle + message:[TestUtils clientMessage]]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::sendWithConnectionHandler() failed"); + + // 9. wait for message callback + [self waitForExpectations: @[messageExpectation] timeout:[TestUtils defaultTimeout]]; + + XCTAssertTrue([messageFromClient isEqualToString:[TestUtils clientMessage]], @"wrong message from client!"); + + [[AgentUtils sharedInstance] closeListener: listenerHandler]; + [[WalletUtils sharedInstance] closeWalletWithHandle: walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentCloseConnectionWorksForTwice +{ + [TestUtils cleanupStorage]; + NSError *ret; + + // 1. create and open wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. obtain did + NSString *did; + NSString *verKey; + NSString *pubKey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verKey + outMyPk:&pubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + + // 3. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubKey + theirVerkey:verKey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 4. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenWithEndpoint() failed"); + + // 5. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 6. Connect + IndyHandle connectionHandle = 0; + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + messageCallback:nil + outConnectionHandle:&connectionHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle() failed"); + + // 7. Close connection (1) + ret = [[AgentUtils sharedInstance] closeConnection:connectionHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::closeConnection() failed for the first attempt"); + + // 8. Close connection (2) + ret = [[AgentUtils sharedInstance] closeConnection:connectionHandle]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::closeConnection() returned wrong error code"); + + + [[AgentUtils sharedInstance] closeListener: listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle: walletHandle]; + + [TestUtils cleanupStorage]; +} + + +@end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumCloseListener.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumCloseListener.m new file mode 100644 index 0000000000..06aecb4f2b --- /dev/null +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumCloseListener.m @@ -0,0 +1,183 @@ +// +// AgentMediumCloseListener.m +// libindy-demo +// +// Created by Anastasia Tarasova on 18/08/2017. +// Copyright © 2017 Kirill Neznamov. All rights reserved. +// + +#import + + +#import +#import +#import +#import "TestUtils.h" + +@interface AgentMediumCloseListener : XCTestCase + +@end + +@implementation AgentMediumCloseListener +- (void)setUp +{ + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown +{ + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testAgentCloseListenerWorksForIncorrectHandle +{ + [TestUtils cleanupStorage]; + NSError *ret; + + // 1. create and open wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. obtain did + NSString *did; + NSString *verKey; + NSString *pubKey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verKey + outMyPk:&pubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 3. listen + + XCTestExpectation* messageExpectation = [[ XCTestExpectation alloc] initWithDescription: @"message completion finished"]; + + IndyHandle listenerHandler = 0; + __block NSString* messageFromClient; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:^(IndyHandle connectionHandle, NSString *message) + { + messageFromClient = message; + [messageExpectation fulfill]; + } + outListenerHandle:&listenerHandler]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenWithEndpoint() failed"); + + // 4. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandler + poolHandle:-1 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 5. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubKey + theirVerkey:verKey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 6. Connect + IndyHandle connectionHandle = 0; + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + messageCallback:nil + outConnectionHandle:&connectionHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle() failed"); + + // 7. Close listener + IndyHandle incorrectListenerHandle = connectionHandle; + ret = [[AgentUtils sharedInstance] closeListener:incorrectListenerHandle]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::closeListener() returned wrong code"); + + // 8. send + ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:connectionHandle + message:[TestUtils clientMessage]]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::sendWithConnectionHandler() failed"); + + // 9. wait for message callback + [self waitForExpectations: @[messageExpectation] timeout:[TestUtils defaultTimeout]]; + + XCTAssertTrue([messageFromClient isEqualToString:[TestUtils clientMessage]], @"wrong message from client!"); + + [[AgentUtils sharedInstance] closeListener: listenerHandler]; + [[WalletUtils sharedInstance] closeWalletWithHandle: walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentCloseListenerWorksForTwice +{ + [TestUtils cleanupStorage]; + NSError *ret; + + // 1. create and open wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. obtain did + NSString *did; + NSString *verKey; + NSString *pubKey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verKey + outMyPk:&pubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 3. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubKey + theirVerkey:verKey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 4. listen + + IndyHandle listenerHandler = 0; + __block NSString* messageFromClient; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandler]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenWithEndpoint() failed"); + + // 5. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandler + poolHandle:-1 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 6. Close listener (1) + + IndyHandle connectionHandle = 0; + ret = [[AgentUtils sharedInstance] closeListener:listenerHandler]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::closeListener() failed"); + + // 7. Close listener (2) + + ret = [[AgentUtils sharedInstance] closeListener:listenerHandler]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::closeListener() returned wrong code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle: walletHandle]; + + [TestUtils cleanupStorage]; +} + +@end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumConnect.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumConnect.m new file mode 100644 index 0000000000..71d2ba2f87 --- /dev/null +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumConnect.m @@ -0,0 +1,1098 @@ +// +// AgentMediumConnect.m +// libindy-demo +// +// Created by Anastasia Tarasova on 18/08/2017. +// Copyright © 2017 Kirill Neznamov. All rights reserved. +// + + +#import +#import +#import +#import "TestUtils.h" + +@interface AgentMediumConnect : XCTestCase + +@end + +@implementation AgentMediumConnect +- (void)setUp +{ + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown +{ + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +// MARK: - Connect + +- (void)testAgentConnectWorksForUnknownListener +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. create and open pool ledger + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. listener wallet + IndyHandle listenerWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for listener wallet"); + + // 3. sender wallet + IndyHandle senderWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&senderWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for sender wallet"); + + // 4. obtain listener did + + NSString *listenerDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWallet + seed:nil + outMyDid:&listenerDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed listener did"); + + // 5. obtain sender did + + NSString *senderDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:senderWallet + seed:nil + outMyDid:&senderDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed sender did"); + + // 6. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 7. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 8. connect + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::connectWithPoolHandle() returned wrong error code"); + + // 9. close everything + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForInvalidRemoteData +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. create and open pool ledger + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. listener wallet + IndyHandle listenerWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for listener wallet"); + + // 3. trustee wallet + IndyHandle trusteeWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&trusteeWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for trustee wallet"); + IndyHandle senderWallet = trusteeWallet; + + // 4. obtain listener did + + NSString *listenerDid; + NSString *listenerVerKey; + NSString *listenerPubKey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWallet + seed:nil + outMyDid:&listenerDid + outMyVerkey:&listenerVerKey + outMyPk:&listenerPubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed listener did"); + + // 5. obtain trustee did + + NSString *trusteeDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:trusteeWallet + seed:[TestUtils trusteeSeed] + outMyDid:&trusteeDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed trustee did"); + + NSString *senderDid = [NSString stringWithString:trusteeDid]; + + // 6. Build & submit nym request + + NSString *listenerNymJson; + ret = [[LedgerUtils sharedInstance] buildNymRequestWithSubmitterDid:trusteeDid + targetDid:listenerDid + verkey:listenerVerKey + alias:nil + role:nil + outRequest:&listenerNymJson]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildNymRequestWithSubmitterDid() failed"); + + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:trusteeWallet + submitterDid:trusteeDid + requestJson:listenerNymJson + outResponseJson:nil]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::signAndSubmitRequestWithPoolHandle() failed for listenerNymJson"); + + // 7. Build & submit attrib request + + NSString *invalidAttribData = [NSString stringWithFormat:@"{\"endpoint\":{\"verkey\":\"%@\"}}", listenerPubKey]; + NSString *listenerAttribJson; + ret = [[LedgerUtils sharedInstance] buildAttribRequestWithSubmitterDid:listenerDid + targetDid:listenerDid + hash:nil + raw:invalidAttribData + enc:nil + resultJson:&listenerAttribJson]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildAttribRequestWithSubmitterDid() failed"); + + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:listenerWallet + submitterDid:listenerDid + requestJson:listenerAttribJson + outResponseJson:nil]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::signAndSubmitRequestWithPoolHandle() failed for listenerAttribJson"); + + // 8. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 9. add identity + + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 10. connect + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::connectWithPoolHandle() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForExpiredKeyInWallet +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. create and open pool ledger + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. listener wallet + IndyHandle listenerWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for listener wallet"); + + // 3. sender wallet + IndyHandle senderWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&senderWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for sender wallet"); + + // 4. obtain listener did + + NSString *listenerDid; + NSString *listenerVerKey; + NSString *listenerPubKey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWallet + seed:nil + outMyDid:&listenerDid + outMyVerkey:&listenerVerKey + outMyPk:&listenerPubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed listener did"); + + // 5. obtain trustee did + + NSString *senderDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:senderWallet + seed:[TestUtils trusteeSeed] + outMyDid:&senderDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed sender did"); + + // 6. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:senderWallet + theirDid:listenerDid + theirPk:listenerPubKey + theirVerkey:listenerVerKey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 7. replace keys + + NSString *listenerNewVerKey; + NSString *listenerNewPubKey; + + ret = [[SignusUtils sharedInstance] replaceKeysWithWalletHandle:listenerWallet + did:listenerDid + identityJson:@"{}" + outMyVerKey:&listenerNewVerKey + outMyPk:&listenerNewPubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::replaceKeysWithWalletHandle() failed"); + + XCTAssertFalse([listenerVerKey isEqualToString:listenerNewVerKey], @"listener's verKey is the same!"); + XCTAssertFalse([listenerPubKey isEqualToString:listenerNewPubKey], @"listener's pub key is the same!"); + + // 8. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 9. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 10. connect hang up expected + + BOOL isTimeout = NO; + ret = [[AgentUtils sharedInstance] connectHangUpExpectedForPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + isTimeout:&isTimeout]; + XCTAssertEqual(isTimeout, YES, @"AgentUtils::connectHandUpExpectedForPoolHandle() succeeded for some reason"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForExpiredKeyInLedger +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. create and open pool ledger + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. listener wallet + IndyHandle listenerWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for listener wallet"); + + // 3. trustee wallet + IndyHandle trusteeWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&trusteeWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for trustee wallet"); + IndyHandle senderWallet = trusteeWallet; + + // 4. obtain listener did + + NSString *listenerDid; + NSString *listenerVerKey; + NSString *listenerPubKey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWallet + seed:nil + outMyDid:&listenerDid + outMyVerkey:&listenerVerKey + outMyPk:&listenerPubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed listener did"); + + // 5. obtain trustee did + + NSString *trusteeDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:trusteeWallet + seed:[TestUtils trusteeSeed] + outMyDid:&trusteeDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed trustee did"); + + NSString *senderDid = [NSString stringWithString:trusteeDid]; + + // 6. Build & submit nym request + + NSString *listenerNymJson; + ret = [[LedgerUtils sharedInstance] buildNymRequestWithSubmitterDid:trusteeDid + targetDid:listenerDid + verkey:listenerVerKey + alias:nil + role:nil + outRequest:&listenerNymJson]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildNymRequestWithSubmitterDid() failed"); + + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:trusteeWallet + submitterDid:trusteeDid + requestJson:listenerNymJson + outResponseJson:nil]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::signAndSubmitRequestWithPoolHandle() failed for listenerNymJson"); + + // 7. Build & submit attrib request + + NSString *invalidAttribData = [NSString stringWithFormat:@"{\"endpoint\":{\"ha\":\"%@\", \"verkey\":\"%@\"}}", [TestUtils endpoint], listenerPubKey]; + NSString *listenerAttribJson; + ret = [[LedgerUtils sharedInstance] buildAttribRequestWithSubmitterDid:listenerDid + targetDid:listenerDid + hash:nil + raw:invalidAttribData + enc:nil + resultJson:&listenerAttribJson]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::buildAttribRequestWithSubmitterDid() failed"); + + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:listenerWallet + submitterDid:listenerDid + requestJson:listenerAttribJson + outResponseJson:nil]; + XCTAssertEqual(ret.code, Success, @"LedgerUtils::signAndSubmitRequestWithPoolHandle() failed for listenerAttribJson"); + + // 8. replace keys + + NSString *listenerNewVerKey; + NSString *listenerNewPubKey; + + ret = [[SignusUtils sharedInstance] replaceKeysWithWalletHandle:listenerWallet + did:listenerDid + identityJson:@"{}" + outMyVerKey:&listenerNewVerKey + outMyPk:&listenerNewPubKey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::replaceKeysWithWalletHandle() failed"); + + XCTAssertFalse([listenerVerKey isEqualToString:listenerNewVerKey], @"listener's verKey is the same!"); + XCTAssertFalse([listenerPubKey isEqualToString:listenerNewPubKey], @"listener's pub key is the same!"); + + // 9. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 9. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 10. connect hang up expected + + BOOL isTimeout = NO; + ret = [[AgentUtils sharedInstance] connectHangUpExpectedForPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + isTimeout:&isTimeout]; + XCTAssertEqual(isTimeout, YES, @"AgentUtils::connectHandUpExpectedForPoolHandle() succeeded for some reason"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForIncompatibleWalletAndPool +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. create and open pool ledger + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. wallet + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"other_pool" + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 3. obtain my did + + NSString *did; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 5. add identity + + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 6. connect + + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:walletHandle + senderDid:did + receiverDid:did + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, WalletIncompatiblePoolError, @"AgentUtils::connectWithPoolHandle() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForUnknownSenderDid +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. create and open pool ledger + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. wallet + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 3. obtain my did + + NSString *did; + NSString *verkey; + NSString *pubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubkey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 5. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 6. add identity + + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 7. connect + + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:walletHandle + senderDid:@"unknownDid" + receiverDid:did + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, WalletNotFoundError, @"AgentUtils::connectWithPoolHandle() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentconnectWorksForInvalidListener +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. obtain my did + + NSString *did; + NSString *verkey; + NSString *pubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 3. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubkey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 4. connect hang up expected + + BOOL isTimeout = NO; + ret = [[AgentUtils sharedInstance] connectHangUpExpectedForPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + isTimeout:&isTimeout]; + XCTAssertTrue(isTimeout, @"AgentUtils::connectHandUpExpectedForPoolHandle() succeeded for some reason"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForClosedListener +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. obtain my did + + NSString *did; + NSString *verkey; + NSString *pubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 3. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubkey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 4. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 5. add identity + + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 6. close listener + + ret = [[AgentUtils sharedInstance] closeListener:listenerHandle]; + + // 7. connect hang up expected + + BOOL isTimeout = NO; + ret = [[AgentUtils sharedInstance] connectHangUpExpectedForPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + isTimeout:&isTimeout]; + XCTAssertTrue(isTimeout, @"AgentUtils::connectHandUpExpectedForPoolHandle() succeeded for some reason"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForConnectWithoutAddIdentity +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. obtain my did + + NSString *did; + NSString *verkey; + NSString *pubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 3. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubkey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 4. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 5. connect hang up expected + + BOOL isTimeout = NO; + ret = [[AgentUtils sharedInstance] connectHangUpExpectedForPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + isTimeout:&isTimeout]; + XCTAssertTrue(isTimeout, @"AgentUtils::connectHandUpExpectedForPoolHandle() succeeded for some reason"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForConnectAfterRemoveIdentity +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. obtain my did + + NSString *did; + NSString *verkey; + NSString *pubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 3. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubkey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 4. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 5. add identity + + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 6. remove identity + + ret = [[AgentUtils sharedInstance] removeIdentity:did + listenerHandle:listenerHandle + walletHandle:walletHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::removeIdentity() failed"); + + // 7. connect hang up expected + + BOOL isTimeout = NO; + ret = [[AgentUtils sharedInstance] connectHangUpExpectedForPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + isTimeout:&isTimeout]; + XCTAssertTrue(isTimeout, @"AgentUtils::connectHandUpExpectedForPoolHandle() succeeded for some reason"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForPassedCallback +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. pool handle + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. wallet + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 3. obtain my did + + NSString *did; + NSString *verkey; + NSString *pubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubkey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 5. listen + + // connection callback + XCTestExpectation* listenerConnectionCompletionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"listener completion finished"]; + __block IndyHandle connectionHandleFromCallback; + void (^connectionCallback)(IndyHandle, IndyHandle) = ^(IndyHandle xListenerHandle, IndyHandle xConnectionHandle) { + NSLog(@"AgentHighCases::testAgentListenWorksForPassedOnConnectCallback:: listener's connectionCallback triggered."); + connectionHandleFromCallback = xConnectionHandle; + [listenerConnectionCompletionExpectation fulfill]; + }; + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:connectionCallback + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 6. add identity + + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 7. connect + + // message from server callback + + XCTestExpectation* messageFromClientCompletionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"listener completion finished"]; + __block NSString *clientMessage; + void (^messageFromClientCallback)(IndyHandle, NSString *) = ^(IndyHandle xConnectionHandle, NSString * message) { + clientMessage = message; + NSLog(@"messageFromClientCallback triggered with message: %@", message); + [messageFromClientCompletionExpectation fulfill]; + }; + + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + messageCallback:messageFromClientCallback + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle() failed"); + + [self waitForExpectations:@[listenerConnectionCompletionExpectation] timeout:[TestUtils defaultTimeout]]; + + // 8. send + + ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:connectionHandleFromCallback + message:[TestUtils clientMessage]]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::sendWithConnectionHandler() failed"); + + [self waitForExpectations: @[messageFromClientCompletionExpectation] timeout:[TestUtils defaultTimeout]]; + XCTAssertTrue([clientMessage isEqualToString:[TestUtils clientMessage]], @"wrong client message"); + + // 9. clean + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [[PoolUtils sharedInstance] closeHandle: poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentConnectWorksForTwice +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. pool handle + + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName() failed"); + + // 2. listener wallet + IndyHandle listenerWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for listener wallet"); + + // 3. sender wallet + IndyHandle senderWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&senderWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed for sender wallet"); + + // 4. obtain listener did + + NSString *listenerDid; + NSString *listenerVerkey; + NSString *listenerPubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWallet + seed:nil + outMyDid:&listenerDid + outMyVerkey:&listenerVerkey + outMyPk:&listenerPubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for listener did"); + + // 5. obtain sender did + + NSString *senderDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:senderWallet + seed:[TestUtils trusteeSeed] + outMyDid:&senderDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for sender did"); + + // 6. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:senderWallet + theirDid:listenerDid + theirPk:listenerPubkey + theirVerkey:listenerVerkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle() failed"); + + // 7. listen + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 8. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 9. connect + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle() failed"); + + // 10. connect hang up expected + BOOL isTimeout = NO; + ret = [[AgentUtils sharedInstance] connectHangUpExpectedForPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + isTimeout:&isTimeout]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectHandUpExpectedForPoolHandle() failed"); + + // 11. close + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + + + +@end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumListen.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumListen.m new file mode 100644 index 0000000000..c3478e99f4 --- /dev/null +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumListen.m @@ -0,0 +1,269 @@ +// +// AgentMediumListen.m +// libindy-demo +// +// Created by Anastasia Tarasova on 18/08/2017. +// Copyright © 2017 Kirill Neznamov. All rights reserved. +// + +#import +#import +#import +#import "TestUtils.h" + +@interface AgentMediumListen : XCTestCase + +@end + +@implementation AgentMediumListen + +- (void)setUp +{ + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown +{ + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testAgentListenWorksForEndpointAlreadyInUse +{ + [TestUtils cleanupStorage]; + NSError *ret; + + // 1. listen first + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint failed"); + + // 2. listen second + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:nil]; + XCTAssertEqual(ret.code, CommonIOError, @"AgentUtils::listenForEndpoint returned wrong error code"); + + // 3. close listener + ret = [[AgentUtils sharedInstance] closeListener:listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::closeListener failed"); + + [TestUtils cleanupStorage]; +} + +- (void)testAgentListenWorksForInvalidEndpoint +{ + [TestUtils cleanupStorage]; + NSError *ret; + + ret = [[AgentUtils sharedInstance] listenForEndpoint:@"127.0.0" + connectionCallback:nil + messageCallback:nil + outListenerHandle:nil]; + XCTAssertEqual(ret.code, CommonIOError, @"AgentUtils::listenForEndpoint returned wrong error code"); + + [TestUtils cleanupStorage]; +} + +- (void)testAgentListenWorksForRejectUnknownSender +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. pool + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName failed"); + + // 2. listener handle + + IndyHandle listenerWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed for listenerWallet"); + + // 3. sender wallet + IndyHandle senderWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&senderWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed for senderWallet"); + + // 4. obtain listener did + NSString *listenerDid; + NSString *listenerVerkey; + NSString *listenerPubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWallet + seed:nil + outMyDid:&listenerDid + outMyVerkey:&listenerVerkey + outMyPk:&listenerPubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed for listenerDid"); + + // 5. obtain sender did + NSString *senderDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:senderWallet + seed:nil + outMyDid:&senderDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed for senderDid"); + + // 6. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:senderWallet + theirDid:listenerDid + theirPk:listenerPubkey + theirVerkey:listenerVerkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle failed"); + + // 7. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint failed"); + + // 8. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle failed"); + + // 9. connect + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, CommonInvalidState, @"AgentUtils::connectWithPoolHandle returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentListenWorksForRejectExpiredSavedSenderData +{ + [TestUtils cleanupStorage]; + + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. pool + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName failed"); + + // 2. listener handle + + IndyHandle listenerWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed for listenerWallet"); + + // 3. sender wallet + IndyHandle senderWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&senderWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed for senderWallet"); + + // 4. obtain listener did + NSString *listenerDid; + NSString *listenerVerkey; + NSString *listenerPubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWallet + seed:[TestUtils trusteeSeed] + outMyDid:&listenerDid + outMyVerkey:&listenerVerkey + outMyPk:&listenerPubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed for listenerDid"); + + // 5. obtain sender did + NSString *senderDid; + NSString *senderVerkey; + NSString *senderPubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:senderWallet + seed:nil + outMyDid:&senderDid + outMyVerkey:&senderVerkey + outMyPk:&senderPubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed for senderDid"); + + // 6. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:listenerWallet + theirDid:senderDid + theirPk:senderPubkey + theirVerkey:senderVerkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle failed"); + + // 7. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint failed"); + + // 8. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle failed"); + + // 9. replace keys + ret = [[SignusUtils sharedInstance] replaceKeysWithWalletHandle:senderWallet + did:senderDid + identityJson:@"{}" + outMyVerKey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::replaceKeysWithWalletHandle failed"); + + // 10. store their did from parts + + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:senderWallet + theirDid:listenerDid + theirPk:listenerPubkey + theirVerkey:listenerVerkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle failed"); + + // 11. connect + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, CommonInvalidState, @"AgentUtils::connectWithPoolHandle returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + + [TestUtils cleanupStorage]; +} + +@end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumRemoveIdentity.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumRemoveIdentity.m new file mode 100644 index 0000000000..fbef994f3a --- /dev/null +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumRemoveIdentity.m @@ -0,0 +1,237 @@ +// +// AgentMediumRemoveIdentity.m +// libindy-demo +// +// Created by Anastasia Tarasova on 18/08/2017. +// Copyright © 2017 Kirill Neznamov. All rights reserved. +// + +#import +#import +#import +#import "TestUtils.h" + +@interface AgentMediumRemoveIdentity : XCTestCase + +@end + +@implementation AgentMediumRemoveIdentity +- (void)setUp +{ + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown +{ + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testAgentRemoveIdentityWorksForInvalidListenerHandle +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 3. obtain did + NSString *did; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed for the first attempt"); + + // 5. remove identity + IndyHandle invalidListenerHandle = 0; + ret = [[AgentUtils sharedInstance] removeIdentity:did + listenerHandle:invalidListenerHandle + walletHandle:walletHandle]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::removeIdentity() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentRemoveIdentityWorksForTwice +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 3. obtain receiver's did + NSString *receiverDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&receiverDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:receiverDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed for the first attempt"); + + // 5. remove identity (1) + ret = [[AgentUtils sharedInstance] removeIdentity:receiverDid + listenerHandle:listenerHandle + walletHandle:walletHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::removeIdentity() failed"); + + // 6. remove identity (2) + + ret = [[AgentUtils sharedInstance] removeIdentity:receiverDid + listenerHandle:listenerHandle + walletHandle:walletHandle]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::removeIdentity() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentRemoveIdentityWorksForInvalidWalletHandle +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 3. obtain did + NSString *did; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:-1 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed for the first attempt"); + + // 5. remove identity + IndyHandle invalidWalletHandle = walletHandle + 1; + ret = [[AgentUtils sharedInstance] removeIdentity:did + listenerHandle:listenerHandle + walletHandle:invalidWalletHandle]; + XCTAssertEqual(ret.code, WalletInvalidHandle, @"AgentUtils::removeIdentity() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentRemoveIdentityWorksForUnknownReceiverDid +{ + [TestUtils cleanupStorage]; + NSError *ret; + NSString *poolName = [TestUtils pool]; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed"); + + // 2. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint() failed"); + + // 3. obtain did + NSString *did; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); + + // 4. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle() failed"); + + // 5. remove identity + ret = [[AgentUtils sharedInstance] removeIdentity:@"unknownDid" + listenerHandle:listenerHandle + walletHandle:walletHandle]; + XCTAssertEqual(ret.code, WalletNotFoundError, @"AgentUtils::removeIdentity() returned wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + + +@end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumSend.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumSend.m new file mode 100644 index 0000000000..045a04b2cf --- /dev/null +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Agent/Medium Cases/AgentMediumSend.m @@ -0,0 +1,391 @@ +// +// AgentMediumSend.m +// libindy-demo +// +// Created by Anastasia Tarasova on 18/08/2017. +// Copyright © 2017 Kirill Neznamov. All rights reserved. +// + +#import +#import +#import +#import "TestUtils.h" + +@interface AgentMediumSend : XCTestCase + +@end + +@implementation AgentMediumSend +- (void)setUp +{ + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown +{ + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testAgentSendWorksForInvalidConnectionHandle +{ + [TestUtils cleanupStorage]; + NSError *ret; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed"); + + // 2. obtain did + + NSString *did; + NSString *verkey; + NSString *pubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed"); + + // 3. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubkey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle failed"); + + // 4. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint failed"); + + // 5. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle failed"); + + // 6. connect + IndyHandle connectionHandle = 0; + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + messageCallback:nil + outConnectionHandle:&connectionHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle failed"); + + // 7. send + IndyHandle invalidConnectionHandle = connectionHandle + 100; + ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:invalidConnectionHandle + message:[TestUtils clientMessage]]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::sendWithConnectionHandler returned wrong code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentSendWorksForClosedConnection +{ + [TestUtils cleanupStorage]; + NSError *ret; + + // 1. wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed"); + + // 2. obtain did + + NSString *did; + NSString *verkey; + NSString *pubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&did + outMyVerkey:&verkey + outMyPk:&pubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed"); + + // 3. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:walletHandle + theirDid:did + theirPk:pubkey + theirVerkey:verkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle failed"); + + // 4. listen + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:nil + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::listenForEndpoint failed"); + + // 5. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:0 + walletHandle:walletHandle + did:did]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle failed"); + + // 6. connect + IndyHandle connectionHandle = 0; + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:0 + walletHandle:walletHandle + senderDid:did + receiverDid:did + messageCallback:nil + outConnectionHandle:&connectionHandle]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle failed"); + + // 7. close connection + ret = [[AgentUtils sharedInstance] closeConnection:connectionHandle]; + + // 7. send + IndyHandle invalidConnectionHandle = connectionHandle + 100; + ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:invalidConnectionHandle + message:[TestUtils clientMessage]]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::sendWithConnectionHandler returned wrong code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testAgentSendWorksForClosedListenerIncomingConenction +{ + [TestUtils cleanupStorage]; + NSString *poolName = [TestUtils pool]; + NSError *ret; + + // 1. pool ledger + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName failed"); + + // 2. listener wallet + IndyHandle listenerWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed for listenerWallet"); + + // 3. sender wallet + IndyHandle senderWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&senderWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed for senderWallet"); + + // 4. obtain listener's did + NSString *listenerDid; + NSString *listenerVerkey; + NSString *listenerPubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWallet + seed:nil + outMyDid:&listenerDid + outMyVerkey:&listenerVerkey + outMyPk:&listenerPubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed for listenerDid"); + + // 5. obtain sender did + NSString *senderDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:senderWallet + seed:[TestUtils trusteeSeed] + outMyDid:&senderDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed for senderDid"); + + // 6. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:senderWallet + theirDid:listenerDid + theirPk:listenerPubkey + theirVerkey:listenerVerkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle failed"); + + // 7. listen + + // connection callback + XCTestExpectation* listenerConnectionCompletionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"listener completion finished"]; + __block IndyHandle connectionHandleFromCallback; + void (^connectionCallback)(IndyHandle, IndyHandle) = ^(IndyHandle xListenerHandle, IndyHandle xConnectionHandle) { + NSLog(@"AgentHighCases::testAgentListenWorksForPassedOnConnectCallback:: listener's connectionCallback triggered."); + connectionHandleFromCallback = xConnectionHandle; + [listenerConnectionCompletionExpectation fulfill]; + + }; + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:connectionCallback + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::listenForEndpoint failed"); + + // 8. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle failed"); + + // 9. connect + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle failed"); + + [self waitForExpectations:@[listenerConnectionCompletionExpectation] timeout:[TestUtils shortTimeout]]; + + // 10. close listener + ret = [[AgentUtils sharedInstance] closeListener:listenerHandle]; + + // 11. send + ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:connectionHandleFromCallback + message:[TestUtils serverMessage]]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::sendWithConnectionHandler return wrong error code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [TestUtils cleanupStorage]; +} + +// TODO: This test is disabled in Rust +- (void)testAgentSendWorksForRemovedIdentity +{ + [TestUtils cleanupStorage]; + NSString *poolName = [TestUtils pool]; + NSError *ret; + + // 1. pool ledger + IndyHandle poolHandle = 0; + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils::createAndOpenPoolLedgerConfigWithName failed"); + + // 2. listener wallet + IndyHandle listenerWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&listenerWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed for listenerWallet"); + + // 3. sender wallet + IndyHandle senderWallet = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&senderWallet]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed for senderWallet"); + + // 4. obtain listener's did + NSString *listenerDid; + NSString *listenerVerkey; + NSString *listenerPubkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:listenerWallet + seed:nil + outMyDid:&listenerDid + outMyVerkey:&listenerVerkey + outMyPk:&listenerPubkey]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed for listenerDid"); + + // 5. obtain sender did + NSString *senderDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:senderWallet + seed:[TestUtils trusteeSeed] + outMyDid:&senderDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle failed for senderDid"); + + // 6. store their did from parts + ret = [[SignusUtils sharedInstance] storeTheirDidFromPartsWithWalletHandle:senderWallet + theirDid:listenerDid + theirPk:listenerPubkey + theirVerkey:listenerVerkey + endpoint:[TestUtils endpoint]]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidFromPartsWithWalletHandle failed"); + + // 7. listen + + // connection callback + XCTestExpectation* listenerConnectionCompletionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"listener completion finished"]; + __block IndyHandle connectionHandleFromCallback; + void (^connectionCallback)(IndyHandle, IndyHandle) = ^(IndyHandle xListenerHandle, IndyHandle xConnectionHandle) { + NSLog(@"AgentHighCases::testAgentListenWorksForPassedOnConnectCallback:: listener's connectionCallback triggered."); + connectionHandleFromCallback = xConnectionHandle; + [listenerConnectionCompletionExpectation fulfill]; + + }; + + IndyHandle listenerHandle = 0; + ret = [[AgentUtils sharedInstance] listenForEndpoint:[TestUtils endpoint] + connectionCallback:connectionCallback + messageCallback:nil + outListenerHandle:&listenerHandle]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::listenForEndpoint failed"); + + // 8. add identity + ret = [[AgentUtils sharedInstance] addIdentityForListenerHandle:listenerHandle + poolHandle:poolHandle + walletHandle:listenerWallet + did:listenerDid]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::addIdentityForListenerHandle failed"); + + // 9. connect + ret = [[AgentUtils sharedInstance] connectWithPoolHandle:poolHandle + walletHandle:senderWallet + senderDid:senderDid + receiverDid:listenerDid + messageCallback:nil + outConnectionHandle:nil]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::connectWithPoolHandle failed"); + + [self waitForExpectations:@[listenerConnectionCompletionExpectation] timeout:[TestUtils shortTimeout]]; + + + + // 10. remove identity + ret = [[AgentUtils sharedInstance] removeIdentity:listenerDid + listenerHandle:listenerHandle + walletHandle:listenerWallet]; + XCTAssertEqual(ret.code, Success, @"AgentUtils::removeIdentity failed"); + + // 11. send + ret = [[AgentUtils sharedInstance] sendWithConnectionHandler:connectionHandleFromCallback + message:[TestUtils serverMessage]]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"AgentUtils::sendWithConnectionHandler return wrong error code"); + + [[AgentUtils sharedInstance] closeListener:listenerHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:listenerWallet]; + [[WalletUtils sharedInstance] closeWalletWithHandle:senderWallet]; + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [TestUtils cleanupStorage]; +} + + + +@end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsHighCases.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsHighCases.m index 5bd4e9fa4b..5b31cdf24a 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsHighCases.m +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsHighCases.m @@ -80,7 +80,7 @@ - (void)testIssuerCreateAndStoreClaimDefWorksForInvalidWallet // 2. Create claim definition NSString *schema = [[AnoncredsUtils sharedInstance] getGvtSchemaJson:@(1)]; - IndyHandle invalidWalletHandle = walletHandle + 1; + IndyHandle invalidWalletHandle = walletHandle + 100; ret = [[AnoncredsUtils sharedInstance] issuerCreateClaimDefinifionWithWalletHandle:invalidWalletHandle issuerDid:[AnoncredsUtils issuerDid] schemaJson:schema @@ -512,9 +512,9 @@ - (void)testIssuerCreateClaimWorks // 2. create claim NSString *claimRequest = [NSString stringWithFormat: @"{"\ "\"blinded_ms\":{"\ - "\"prover_did\":\"CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW\","\ - "\"u\":\"54172737564529332710724213139048941083013176891644677117322321823630308734620627329227591845094100636256829761959157314784293939045176621327154990908459072821826818718739696323299787928173535529024556540323709578850706993294234966440826690899266872682790228513973999212370574548239877108511283629423807338632435431097339875665075453785141722989098387895970395982432709011505864533727415552566715069675346220752584449560407261446567731711814188836703337365986725429656195275616846543535707364215498980750860746440672050640048215761507774996460985293327604627646056062013419674090094698841792968543317468164175921100038\","\ - "\"ur\":null},"\ + "\"prover_did\":\"CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW\","\ + "\"u\":\"54172737564529332710724213139048941083013176891644677117322321823630308734620627329227591845094100636256829761959157314784293939045176621327154990908459072821826818718739696323299787928173535529024556540323709578850706993294234966440826690899266872682790228513973999212370574548239877108511283629423807338632435431097339875665075453785141722989098387895970395982432709011505864533727415552566715069675346220752584449560407261446567731711814188836703337365986725429656195275616846543535707364215498980750860746440672050640048215761507774996460985293327604627646056062013419674090094698841792968543317468164175921100038\","\ + "\"ur\":null},"\ "\"issuer_did\":\"%@\","\ "\"schema_seq_no\":1}", [AnoncredsUtils issuerDid]]; @@ -533,6 +533,8 @@ - (void)testIssuerCreateClaimWorks XCTAssertTrue([claim[@"signature"][@"primary_claim"][@"m2"] length] > 0, @"wrong \"m2\" length"); XCTAssertTrue([claim[@"signature"][@"primary_claim"][@"e"] length] > 0, @"wrong \"e\" length"); XCTAssertTrue([claim[@"signature"][@"primary_claim"][@"v"] length] > 0, @"wrong \"v\" length"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; } - (void)testIssuerCreateClaimWorksForClaimDoesNotCorrespondToClaimReq @@ -593,6 +595,8 @@ - (void)testIssuerCreateClaimWorksForInvalidWalletHandle outRevocRegUpdateJSON:nil]; XCTAssertEqual(ret.code, WalletInvalidHandle, @"AnoncredsUtils::issuerCreateClaimWithWalletHandle returned wrong error code."); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; } // MARK: - Prover store claim @@ -641,6 +645,8 @@ - (void)testProverStoreClaimWorks ret = [[AnoncredsUtils sharedInstance] proverStoreClaimWithWalletHandle:walletHandle claimsJson:xClaimJson]; XCTAssertEqual(ret.code, Success, @"AnoncredsUtils::proverStoreClaimWithWalletHandle failed"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; } - (void)testProverStoreClaimWorksForInvalidWalletHandle @@ -689,6 +695,8 @@ - (void)testProverStoreClaimWorksForInvalidWalletHandle ret = [[AnoncredsUtils sharedInstance] proverStoreClaimWithWalletHandle:invalidWalletHandle claimsJson:xClaimJson]; XCTAssertEqual(ret.code, WalletInvalidHandle, @"AnoncredsUtils::proverStoreClaimWithWalletHandle failed"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; } // MARK: - Prover get claims @@ -715,6 +723,8 @@ - (void)testProverGetClaimsWorksForEmptyFilter NSArray *claims = (NSArray *)claimsDict; XCTAssertEqual([claims count], 1, @"claims count != 1"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; } - (void)testProverGetClaimsWorksForFilterByIssuerDid @@ -740,6 +750,8 @@ - (void)testProverGetClaimsWorksForFilterByIssuerDid NSArray *claims = (NSArray *)claimsDict; XCTAssertEqual([claims count], 1, @"claims count != 1"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; } - (void)testProverGetClaimsWorksForFilterByClaimDefSeqNoAndSchemaSeqNo @@ -765,6 +777,8 @@ - (void)testProverGetClaimsWorksForFilterByClaimDefSeqNoAndSchemaSeqNo NSArray *claims = (NSArray *)claimsDict; XCTAssertEqual([claims count], 1, @"claims count != 1"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; } - (void)testProverGetClaimsWorksForEmptyResult @@ -789,6 +803,8 @@ - (void)testProverGetClaimsWorksForEmptyResult NSArray *claims = (NSArray *)claimsDict; XCTAssertEqual([claims count], 0, @"claims count != 0"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; } - (void)testProverGetClaimsWorksForInvalidWalletHandle @@ -808,6 +824,8 @@ - (void)testProverGetClaimsWorksForInvalidWalletHandle filterJson:@"{}" outClaimsJson:&claimsJson]; XCTAssertEqual(ret.code, WalletInvalidHandle, @"AnoncredsUtils::proverGetClaimsForWalletHandle returned wrong code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; } // MARK: - Prover get claims for proof request diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsMediumCases.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsMediumCases.m index 80d65b1229..520f07783b 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsMediumCases.m +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsMediumCases.m @@ -162,13 +162,13 @@ - (void)testProverGetClaimOffersWorksForDifferentWallets // 2. obtain wallet handles IndyHandle walletHandle1 = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle1]; XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed"); IndyHandle walletHandle2 = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle2]; XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed"); diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsMediumCasesDemos.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsMediumCasesDemos.m index 976df0272e..3d2e9f8dfd 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsMediumCasesDemos.m +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Anoncreds/AnoncredsMediumCasesDemos.m @@ -42,7 +42,7 @@ - (void)testVerifierVerifyProofWorksForProofDoesNotCorrespondProofRequest //1. Create wallet, get wallet handle NSError *ret; IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName failed"); @@ -173,7 +173,7 @@ - (void)testAnoncredsWorksForSingleIssuerSingleProver { [TestUtils cleanupStorage]; - NSString* poolName = @"pool1"; + NSString* poolName = [TestUtils pool]; IndyHandle issuerWalletHandle = 0; IndyHandle proverWalletHandle = 0; NSError *ret = nil; @@ -356,7 +356,7 @@ - (void)testAnoncredsWorksForMultiplyIssuerSingleProver NSString *issuer2Did = @"CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW"; NSString *proverDid = @"BzfFCYk"; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; NSError *ret; //1. Issuer1 create wallet, get wallet handles @@ -703,7 +703,7 @@ - (void)testAnoncredsWorksForSingleIssuerMultiplyClaimsSingleProver NSString* issuerDid = @"CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW"; NSString* proverDid = @"BzfFCYk"; - NSString* poolName = @"pool1"; + NSString* poolName = [TestUtils pool]; NSError* ret = nil; //1. Issuer create wallet, get wallet handles diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Ledger/LedgerHignCases.mm b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Ledger/LedgerHignCases.mm index c0e1a4bfec..9eb68d5aee 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Ledger/LedgerHignCases.mm +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Ledger/LedgerHignCases.mm @@ -307,7 +307,7 @@ -(void) testSignAndSubmitRequestWorksForInvalidWalletHandle - (void) testSignAndSubmitRequestWorksForIncompatibleWalletAndPool { [TestUtils cleanupStorage]; - NSString *poolName1 = @"pool1"; + NSString *poolName1 = [TestUtils pool]; NSString *poolName2 = @"pool2"; NSError *ret; diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Signus/SignusHighCases.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Signus/SignusHighCases.m index 77284fc067..89a9f1e2ae 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Signus/SignusHighCases.m +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Signus/SignusHighCases.m @@ -40,12 +40,11 @@ - (void)tearDown - (void)testCreateMyDidWorksForEmptyJson { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; NSError *ret = nil; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -62,19 +61,18 @@ - (void)testCreateMyDidWorksForEmptyJson XCTAssertEqual([[myDid dataFromBase58] length] , 16, @"length of myDid != 16"); XCTAssertEqual([[myVerKey dataFromBase58] length], 32, @"length of myVerKey != 32"); - + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } - (void)testCreateMyDidWorksWithSeed { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; NSError *ret = nil; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -92,18 +90,18 @@ - (void)testCreateMyDidWorksWithSeed XCTAssertTrue([myDid isEqualToString:@"NcYxiDXkpYi6ov5FcYDi1e"], @"wrong myDid!"); XCTAssertTrue([myVerKey isEqualToString:@"CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW"], @"wrong myVerKey!"); + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } - (void)testCreateMyDidWorksAsCid { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; NSError *ret = nil; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -122,18 +120,19 @@ - (void)testCreateMyDidWorksAsCid XCTAssertTrue([myDid isEqualToString:@"CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW"], @"wrong myDid!"); XCTAssertTrue([myVerKey isEqualToString:@"CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW"], @"wrong myVerKey!"); + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } - (void)testCreateMyDidWorksWithPassedDid { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; NSError *ret = nil; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -154,18 +153,44 @@ - (void)testCreateMyDidWorksWithPassedDid XCTAssertTrue([myDid isEqualToString:did], @"wrong myDid!"); XCTAssertTrue([myVerKey isEqualToString:@"CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW"], @"wrong myVerKey!"); + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; +} + +- (void)testCreateMyDidWorkForExistsCryptoType +{ + [TestUtils cleanupStorage]; + NSError *ret = nil; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. create my did + ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle + myDidJson:@"{\"crypto_type\":\"ed25519\"}" + outMyDid:nil + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() returned wrong error code"); + + // 3. close wallet + [[WalletUtils sharedInstance] closeWalletWithHandle: walletHandle]; + [TestUtils cleanupStorage]; } - (void)testCreateMyDidWorksForinvalidWalletHandle { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; NSError *ret = nil; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -178,6 +203,8 @@ - (void)testCreateMyDidWorksForinvalidWalletHandle outMyVerkey:nil outMyPk:nil]; XCTAssertEqual(ret.code, WalletInvalidHandle, @"SignusUtils::createMyDidWithWalletHandle() returned wrong error code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } @@ -186,12 +213,11 @@ - (void)testCreateMyDidWorksForinvalidWalletHandle - (void)testReplaceKeysWorks { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; NSError *ret = nil; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -211,18 +237,19 @@ - (void)testReplaceKeysWorks identityJson:@"{}" outMyVerKey:nil outMyPk:nil]; + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } - (void)testReplaceKeysWorksForInvalidDid { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; NSError *ret = nil; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -234,18 +261,19 @@ - (void)testReplaceKeysWorksForInvalidDid outMyVerKey:nil outMyPk:nil]; XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils:replaceKeysWithWalletHandle failed"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } - (void)testReplaceKeysWorksForInvalidHandle { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; NSError *ret = nil; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -267,6 +295,47 @@ - (void)testReplaceKeysWorksForInvalidHandle outMyVerKey:nil outMyPk:nil]; XCTAssertEqual(ret.code, WalletInvalidHandle, @"SignusUtils:replaceKeysWithWalletHandle failed"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; +} + +- (void)testReplaceKeysWorksForSeed +{ + [TestUtils cleanupStorage]; + NSError *ret = nil; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. create my did + NSString *myDid; + NSString *myVerkey; + ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle + myDidJson:@"{}" + outMyDid:&myDid + outMyVerkey:&myVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() returned wrong error code"); + + // 3. replace keys + + NSString *newVerkey; + ret = [[SignusUtils sharedInstance] replaceKeysWithWalletHandle:walletHandle + did:myDid + identityJson:@"{\"seed\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}" + outMyVerKey:&newVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils:replaceKeysWithWalletHandle failed"); + XCTAssertTrue([newVerkey isEqualToString:@"CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW"], @"wrong newVerkey"); + XCTAssertFalse([myVerkey isEqualToString:newVerkey], @"verkey is the same!"); + + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } @@ -278,7 +347,7 @@ - (void)testStoreTheidDidWorks // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -288,6 +357,8 @@ - (void)testStoreTheidDidWorks ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle identityJson:identityJson]; XCTAssertEqual(ret.code, Success, @"SignusUtils:storeTheirDid failed"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } @@ -298,7 +369,7 @@ - (void)testStoreTheirDidWorksForInvalidJson // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -308,6 +379,8 @@ - (void)testStoreTheirDidWorksForInvalidJson ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle identityJson:identityJson]; XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils:storeTheirDid returned wrong error"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } @@ -318,7 +391,7 @@ - (void)testStoreTheirDidWorksForInvalidHandle // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -329,6 +402,8 @@ - (void)testStoreTheirDidWorksForInvalidHandle ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:invalidWalletHandle identityJson:identityJson]; XCTAssertEqual(ret.code, WalletInvalidHandle, @"SignusUtils:storeTheirDid returned wrong error"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } @@ -339,7 +414,7 @@ - (void)testStoreTheirDidWorksWithVerkey // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); @@ -350,6 +425,53 @@ - (void)testStoreTheirDidWorksWithVerkey ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle identityJson:identityJson]; XCTAssertEqual(ret.code, Success, @"SignusUtils:storeTheirDid() failed"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; +} + +- (void)testStoretheirDidWorksWithoutDid +{ + [TestUtils cleanupStorage]; + NSError *ret = nil; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. Store their did + NSString *identityJson = @"{\"verkey\":\"GjZWsBLgZCR18aL468JAT7w9CZRiBnpxUPPgyQxh4voa\"}"; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils:storeTheirDidWithWalletHandle() returned wrong code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; + +} + +- (void)testStoreTheirDidWorksForCorrectCryptoType +{ + [TestUtils cleanupStorage]; + NSError *ret = nil; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. Store their did + NSString *identityJson = @"{\"did\":\"8wZcEriaNLNKtteJvx7f8i\", \"verkey\":\"GjZWsBLgZCR18aL468JAT7w9CZRiBnpxUPPgyQxh4voa\", \"crypto_type\": \"ed25519\"}"; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils:storeTheirDidWithWalletHandle() failed"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } @@ -362,14 +484,14 @@ - (void)testSignWorks // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); // 2. create my did NSString *myDid; - NSString *myDidJson = @"{\"seed\":\"000000000000000000000000Trustee1\"}"; + NSString *myDidJson = [NSString stringWithFormat:@"{\"seed\":\"%@\"}",[TestUtils mySeed]]; ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle myDidJson:myDidJson outMyDid:&myDid @@ -378,53 +500,38 @@ - (void)testSignWorks XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() failed"); // 3. Sign - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"identifier\":\"GJ1SzoWzavQYfNL9XkaJdrQejfztN4XqdsiV4ct3LXKL\","\ - "\"operation\":{"\ - "\"type\":\"1\","\ - "\"dest\":\"VsKV7grR1BUE29mG2Fm2kX\","\ - "\"verkey\":\"GjZWsBLgZCR18aL468JAT7w9CZRiBnpxUPPgyQxh4voa\""\ - "}}"; - - NSMutableDictionary *expectedSignature = [NSMutableDictionary new]; - expectedSignature[@"signature"] = @"65hzs4nsdQsTUqLCLy2qisbKLfwYKZSWoyh1C6CU59p5pfG3EHQXGAsjW4Qw4QdwkrvjSgQuyv8qyABcXRBznFKW"; - - NSString *signatureJson; - ret = [[SignusUtils sharedInstance] signWithWalletHandle:walletHandle - theirDid:myDid message:message - outSignature:&signatureJson]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::signWithWalletHandle() failed"); - NSDictionary *signature = [NSDictionary fromString:signatureJson]; - XCTAssertTrue([signature contains:expectedSignature]); + NSData *signature; + ret = [[SignusUtils sharedInstance] signWithWalletHandle:walletHandle + theirDid:myDid + message:[TestUtils message] + outSignature:&signature]; + XCTAssertTrue([signature isEqualToData:[TestUtils signature]], @"SignusUtils::signWithWalletHandle() failed. Signature is not verified"); + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } -- (void)testSignWorksForUnknownDid +- (void)testSignWorksForUnknownSigner { [TestUtils cleanupStorage]; NSError *ret = nil; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); - // 2. Sign - NSString *message = @"{"\ - "\"reqId\":1495034346617224651}"; - - NSString *signatureJson; + NSData *signature; ret = [[SignusUtils sharedInstance] signWithWalletHandle:walletHandle - theirDid:@"some_did" - message:message - outSignature:&signatureJson]; + theirDid:@"did" + message:[TestUtils message] + outSignature:&signature]; XCTAssertEqual(ret.code, WalletNotFoundError, @"SignusUtils::signWithWalletHandle() returned wrong error"); + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } @@ -435,33 +542,31 @@ - (void)testSignWorksForInvalidWalletHandle // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:[TestUtils pool] xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); // 2. create my did NSString *myDid; - NSString *myDidJson = @"{\"seed\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}"; ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle - myDidJson:myDidJson + myDidJson:@"{}" outMyDid:&myDid outMyVerkey:nil outMyPk:nil]; XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() failed"); // 3. Sign - NSString *message = @"{"\ - "\"reqId\":1495034346617224651}"; - - NSString *signatureJson; + NSData *signature; IndyHandle invalidWalletHandle = walletHandle + 1; ret = [[SignusUtils sharedInstance] signWithWalletHandle:invalidWalletHandle theirDid:myDid - message:message - outSignature:&signatureJson]; + message:[TestUtils message] + outSignature:&signature]; XCTAssertEqual(ret.code, WalletInvalidHandle, @"SignusUtils::signWithWalletHandle() returned wrong code"); + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; } @@ -471,7 +576,7 @@ - (void)testVerifyWorksForVerkeyCachedInWallet { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_verkey_cached_in_wallet"; + NSString *poolName = [TestUtils pool]; // 1. Create and open pool ledger config, get pool handle IndyHandle poolHandle = 0; @@ -492,7 +597,7 @@ - (void)testVerifyWorksForVerkeyCachedInWallet NSString *did; NSString *verKey; ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle - seed:@"000000000000000000000000Trustee1" + seed:[TestUtils mySeed] outMyDid:&did outMyVerkey:&verKey outMyPk:nil]; @@ -507,25 +612,19 @@ - (void)testVerifyWorksForVerkeyCachedInWallet XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); // 5. Verify - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"identifier\":\"GJ1SzoWzavQYfNL9XkaJdrQejfztN4XqdsiV4ct3LXKL\","\ - "\"operation\":{"\ - "\"type\":\"1\","\ - "\"dest\":\"VsKV7grR1BUE29mG2Fm2kX\","\ - "\"verkey\":\"GjZWsBLgZCR18aL468JAT7w9CZRiBnpxUPPgyQxh4voa\""\ - "},"\ - "\"signature\":\"65hzs4nsdQsTUqLCLy2qisbKLfwYKZSWoyh1C6CU59p5pfG3EHQXGAsjW4Qw4QdwkrvjSgQuyv8qyABcXRBznFKW\"}"; - BOOL verified = NO; ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle poolHandle:poolHandle did:did - signature:message + message:[TestUtils message] + signature:[TestUtils signature] outVerified:&verified]; XCTAssertEqual(ret.code, Success, @"SignusUtils::verifyWithWalletHandle() failed"); XCTAssertTrue(verified, @"verifying failed"); + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; } @@ -533,7 +632,7 @@ - (void)testVerifyWorksForGetVerkeyFromLedger { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_get_verkey_from_ledger"; + NSString *poolName = [TestUtils pool]; // 1. Create and open pool ledger config, get pool handle IndyHandle poolHandle = 0; @@ -553,7 +652,7 @@ - (void)testVerifyWorksForGetVerkeyFromLedger // 3. trustee did NSString *trusteeDid; ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle - seed:@"000000000000000000000000Trustee1" + seed:[TestUtils trusteeSeed] outMyDid:&trusteeDid outMyVerkey:nil outMyPk:nil]; @@ -564,7 +663,7 @@ - (void)testVerifyWorksForGetVerkeyFromLedger NSString *myDid; NSString *myVerKey; ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle - seed:@"00000000000000000000000000000My1" + seed:[TestUtils mySeed] outMyDid:&myDid outMyVerkey:&myVerKey outMyPk:nil]; @@ -591,9 +690,6 @@ - (void)testVerifyWorksForGetVerkeyFromLedger XCTAssertEqual(ret.code, Success, @"LedgerUtils::signAndSubmitRequestWithPoolHandle() failed"); // 7. Store their did - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"signature\":\"tibTuE59pZn1sCeZpNL5rDzpkpqV3EkDmRpFTizys9Gr3ZieLdGEGyq4h8jsVWW9zSaXSRnfYcVb1yTjUJ7vJai\"}"; NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\"}",myDid]; ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle identityJson:identityJson]; @@ -604,11 +700,15 @@ - (void)testVerifyWorksForGetVerkeyFromLedger ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle poolHandle:poolHandle did:myDid - signature:message + message:[TestUtils message] + signature:[TestUtils signature] outVerified:&verified]; XCTAssertEqual(ret.code, Success, @"SignusUtils::verifyWithWalletHandle() failed"); XCTAssertTrue(verified, @"verifying failed"); + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; } @@ -616,7 +716,7 @@ - (void)testVerifyWorksForExpiredNym { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_expired_nym"; + NSString *poolName = [TestUtils pool]; NSString *walletName = @"wallet1"; // 1. Create and open pool ledger config, get pool handle @@ -644,7 +744,7 @@ - (void)testVerifyWorksForExpiredNym // 4. trustee did NSString *trusteeDid; ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle - seed:@"000000000000000000000000Trustee1" + seed:[TestUtils trusteeSeed] outMyDid:&trusteeDid outMyVerkey:nil outMyPk:nil]; @@ -655,7 +755,7 @@ - (void)testVerifyWorksForExpiredNym NSString *myDid; NSString *myVerKey; ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle - seed:@"00000000000000000000000000000My1" + seed:[TestUtils mySeed] outMyDid:&myDid outMyVerkey:&myVerKey outMyPk:nil]; @@ -688,19 +788,19 @@ - (void)testVerifyWorksForExpiredNym XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); // 8. Verify - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"signature\":\"tibTuE59pZn1sCeZpNL5rDzpkpqV3EkDmRpFTizys9Gr3ZieLdGEGyq4h8jsVWW9zSaXSRnfYcVb1yTjUJ7vJai\"}"; - BOOL verified = NO; ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle poolHandle:poolHandle did:myDid - signature:message + message:[TestUtils message] + signature:[TestUtils signature] outVerified:&verified]; XCTAssertEqual(ret.code, Success, @"SignusUtils::verifyWithWalletHandle() failed"); XCTAssertTrue(verified, @"verifying failed"); + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; } @@ -709,7 +809,7 @@ - (void)testVerifyWorksForInvalidWalletHandle { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_invalid_wallet_handle"; + NSString *poolName = [TestUtils pool]; // 1. Create and open pool ledger config, get pool handle IndyHandle poolHandle = 0; @@ -728,25 +828,19 @@ - (void)testVerifyWorksForInvalidWalletHandle // 3. Verify - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"identifier\":GJ1SzoWzavQYfNL9XkaJdrQejfztN4XqdsiV4ct3LXKL,"\ - "\"operation\":{"\ - "\"type\":\"1\","\ - "\"dest\":\"VsKV7grR1BUE29mG2Fm2kX\","\ - "\"verkey\":\"GjZWsBLgZCR18aL468JAT7w9CZRiBnpxUPPgyQxh4voa\"},"\ - "\"signature\":\"65hzs4nsdQsTUqLCLy2qisbKLfwYKZSWoyh1C6CU59p5pfG3EHQXGAsjW4Qw4QdwkrvjSgQuyv8qyABcXRBznFKW\"}"; - - // 4. Verify BOOL verified = NO; IndyHandle invalidWalletHandle = walletHandle + 1; ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:invalidWalletHandle poolHandle:poolHandle did:@"did" - signature:message + message:[TestUtils message] + signature:[TestUtils signature] outVerified:&verified]; XCTAssertEqual(ret.code, WalletInvalidHandle, @"SignusUtils::verifyWithWalletHandle() failed"); + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; } @@ -754,7 +848,7 @@ - (void)testVerifyWorksForInvalidPoolHandle { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_invalid_pool_handle"; + NSString *poolName = [TestUtils pool]; // 1. Create and open pool ledger config, get pool handle IndyHandle poolHandle = 0; @@ -772,28 +866,822 @@ - (void)testVerifyWorksForInvalidPoolHandle XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); // 3. Verify - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"identifier\":\"GJ1SzoWzavQYfNL9XkaJdrQejfztN4XqdsiV4ct3LXKL\","\ - "\"operation\":{"\ - "\"type\":\"1\","\ - "\"dest\":\"VsKV7grR1BUE29mG2Fm2kX\","\ - "\"verkey\":\"GjZWsBLgZCR18aL468JAT7w9CZRiBnpxUPPgyQxh4voa\"},"\ - "\"signature\":\"65hzs4nsdQsTUqLCLy2qisbKLfwYKZSWoyh1C6CU59p5pfG3EHQXGAsjW4Qw4QdwkrvjSgQuyv8qyABcXRBznFKW\"}"; - - // 4. Verify BOOL verified = NO; IndyHandle invalidPoolHandle = poolHandle + 1; ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle poolHandle:invalidPoolHandle did:@"did" - signature:message + message:[TestUtils message] + signature:[TestUtils signature] outVerified:&verified]; XCTAssertEqual(ret.code, PoolLedgerInvalidPoolHandle, @"SignusUtils::verifyWithWalletHandle() failed"); + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testVerifyWorksForOtherSigner +{ + [TestUtils cleanupStorage]; + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. my did + NSString *did; + NSString *verKey; + NSString *didJson = [NSString stringWithFormat:@"{\"seed\":\"%@\"}", [TestUtils trusteeSeed]]; + ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle + myDidJson:didJson + outMyDid:&did + outMyVerkey:&verKey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() failed for myDid"); + + // 4. other did + NSString *otherDid; + NSString *otherVerKey; + NSString *otherDidJson = @"{\"seed\":\"000000000000000000000000Steward1\"}"; + ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle + myDidJson:otherDidJson + outMyDid:&otherDid + outMyVerkey:&otherVerKey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() failed for otherDid"); + + // 5. Store my did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}",did, verKey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed for did"); + + // 6. Store my did + identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}",otherDid, otherVerKey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed for otherDid"); + + // 7. Sign + + NSData *signature; + ret = [[SignusUtils sharedInstance] signWithWalletHandle:walletHandle + theirDid:did + message:[TestUtils message] + outSignature:&signature]; + + // 8. verify + + BOOL isValid = NO; + ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle + poolHandle:poolHandle + did:otherDid + message:[TestUtils message] + signature:signature + outVerified:&isValid]; + XCTAssertTrue(isValid, @"SignusUtils::verifyWithWalletHandle failed. Signature is not valid"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +// MARK: - Encrypt + +- (void)testEncryptWorksForPubKeyCachedInWallet +{ + [TestUtils cleanupStorage]; + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. my did + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(myDid, @"invalid did"); + + + // 3. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); + + // 5. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}",theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 6. encrypt + NSData *encryptedMessage; + NSData *nonce; + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:walletHandle + poolHandle:poolHandle + myDid:myDid + did:theirDid + message:[TestUtils message] + outEncryptedMessage:&encryptedMessage + outNonce:&nonce]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::encryptWithWalletHandle() failed"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testEncryptWorksForGetPubKeyFromLedger +{ + [TestUtils cleanupStorage]; + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. trustee did + NSString *trusteeDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&trusteeDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(trusteeDid, @"invalid did"); + + + // 3. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for their did"); + + + // 4. Build & Submit nym request + + NSString *nymRequest; + ret = [[LedgerUtils sharedInstance] buildNymRequestWithSubmitterDid:trusteeDid + targetDid:theirDid + verkey:theirVerkey + alias:nil + role:nil + outRequest:&nymRequest]; + + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:walletHandle + submitterDid:trusteeDid + requestJson:nymRequest + outResponseJson:nil]; + // 5. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\"}",theirDid]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 6. encrypt + NSData *encryptedMessage; + NSData *nonce; + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:walletHandle + poolHandle:poolHandle + myDid:trusteeDid + did:theirDid + message:[TestUtils message] + outEncryptedMessage:&encryptedMessage + outNonce:&nonce]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::encryptWithWalletHandle() failed"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; } +- (void)testEncryptWorksForGetNymFromLedger +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. trustee did + NSString *trusteeDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&trusteeDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(trusteeDid, @"invalid did"); + + + // 4. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for their did"); + + // 4. Build & Submit nym request + + NSString *nymRequest; + ret = [[LedgerUtils sharedInstance] buildNymRequestWithSubmitterDid:trusteeDid + targetDid:theirDid + verkey:theirVerkey + alias:nil + role:nil + outRequest:&nymRequest]; + + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:walletHandle + submitterDid:trusteeDid + requestJson:nymRequest + outResponseJson:nil]; + + // 5. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}",theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 6. encrypt + NSData *encryptedMessage; + NSData *nonce; + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:walletHandle + poolHandle:poolHandle + myDid:trusteeDid + did:theirDid + message:[TestUtils message] + outEncryptedMessage:&encryptedMessage + outNonce:&nonce]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::encryptWithWalletHandle() failed"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testEncryptWorksForExriredNym +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + NSString *walletName = @"wallet1"; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + // 2. Create wallet + ret = [[WalletUtils sharedInstance] createWalletWithPoolName:poolName + walletName:walletName + xtype:nil + config:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createWalletWithPoolName failed"); + + // 3. Open wallet + NSString *config = @"{\"freshness_time\":1}"; + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] openWalletWithName:walletName + config:config + outHandle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:openWalletWithName failed"); + + // 4. trustee did + NSString *trusteeDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&trusteeDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(trusteeDid, @"invalid did"); + + + // 5. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for their did"); + + // 6. Build & Submit nym request + + NSString *nymRequest; + ret = [[LedgerUtils sharedInstance] buildNymRequestWithSubmitterDid:trusteeDid + targetDid:theirDid + verkey:theirVerkey + alias:nil + role:nil + outRequest:&nymRequest]; + + ret = [[LedgerUtils sharedInstance] signAndSubmitRequestWithPoolHandle:poolHandle + walletHandle:walletHandle + submitterDid:trusteeDid + requestJson:nymRequest + outResponseJson:nil]; + + // 7. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}",theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 8. encrypt + NSData *encryptedMessage; + NSData *nonce; + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:walletHandle + poolHandle:poolHandle + myDid:trusteeDid + did:theirDid + message:[TestUtils message] + outEncryptedMessage:&encryptedMessage + outNonce:&nonce]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::encryptWithWalletHandle() failed"); + + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testEncryptWorksForInvalidWalletHandle +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. my did + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(myDid, @"invalid did"); + + + // 4. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); + + // 5. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}",theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 6. encrypt + IndyHandle invalidWalletHandle = walletHandle + 1; + NSData *encryptedMessage; + NSData *nonce; + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:invalidWalletHandle + poolHandle:poolHandle + myDid:myDid + did:theirDid + message:[TestUtils message] + outEncryptedMessage:&encryptedMessage + outNonce:&nonce]; + XCTAssertEqual(ret.code, WalletInvalidHandle, @"SignusUtils::encryptWithWalletHandle() returned wrong code"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testEncryptWorksForInvalidPoolHandle +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. my did + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(myDid, @"invalid did"); + + + // 4. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); + + // 5. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}",theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 6. encrypt + IndyHandle invalidPoolHandle = poolHandle + 1; + NSData *encryptedMessage; + NSData *nonce; + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:walletHandle + poolHandle:invalidPoolHandle + myDid:myDid + did:theirDid + message:[TestUtils message] + outEncryptedMessage:&encryptedMessage + outNonce:&nonce]; + XCTAssertEqual(ret.code, PoolLedgerInvalidPoolHandle, @"SignusUtils::encryptWithWalletHandle() returned wrong code"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + + +// MARK: - Decrypt + +- (void)testDecryptWorks +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. my did + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(myDid, @"invalid did"); + + + // 3. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); + + // 4. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}",theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 5. decrypt + + NSData *decryptedMessage; + ret = [[SignusUtils sharedInstance] decryptWithWalletHandle:walletHandle + myDid:myDid + did:theirDid + encryptedMessage:[TestUtils encryptedMessage] + nonce:[TestUtils nonce] + outDecryptedMessage:&decryptedMessage]; + XCTAssertTrue([decryptedMessage isEqualToData:[TestUtils message]], @"SignusUtils::decryptWithWalletHandle() failed. Decrypted mesage doesn't match message"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testDecryptWorksForOtherCoder +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. my did + NSString *myDid; + NSString *myVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&myDid + outMyVerkey:&myVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(myDid, @"invalid did"); + + + // 4. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); + + // 5. store myDid + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}", myDid, myVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 6. store theirDid + identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}", theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 7. encrypt + NSData *encryptedMessage; + NSData *nonce; + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:walletHandle + poolHandle:poolHandle + myDid:myDid + did:myDid + message:[TestUtils message] + outEncryptedMessage:&encryptedMessage + outNonce:&nonce]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::encryptWithWalletHandle() failed"); + + // 8. decrypt + + NSData *decryptedMessage; + ret = [[SignusUtils sharedInstance] decryptWithWalletHandle:walletHandle + myDid:myDid + did:theirDid + encryptedMessage:[TestUtils encryptedMessage] + nonce:[TestUtils nonce] + outDecryptedMessage:&decryptedMessage]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils::decryptWithWalletHandle() returned wrong error code."); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testDecryptWorksForNonceNotCorrespondMessage +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. my did + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(myDid, @"invalid did"); + + + // 4. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); + + // 5. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}", theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed for theirDid"); + + // 6. decrypt + + NSString *nonce = @"acS2SQgDdfE3Goxa1AhcWCa4kEMqSelv7"; + ret = [[SignusUtils sharedInstance] decryptWithWalletHandle:walletHandle + myDid:myDid + did:theirDid + encryptedMessage:[TestUtils encryptedMessage] + nonce:[nonce dataUsingEncoding:NSUTF8StringEncoding] + outDecryptedMessage:nil]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils::decryptWithWalletHandle() returned wrong error code."); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testDecryptWorksForInvalidWalletHandle +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. my did + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + XCTAssertTrue(myDid, @"invalid did"); + + + // 4. their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); + + // 5. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}", theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed for theirDid"); + + // 6. decrypt + + IndyHandle invalidWalletHandle = walletHandle + 1; + NSData *decryptedMessage; + ret = [[SignusUtils sharedInstance] decryptWithWalletHandle:invalidWalletHandle + myDid:myDid + did:theirDid + encryptedMessage:[TestUtils encryptedMessage] + nonce:[TestUtils nonce] + outDecryptedMessage:&decryptedMessage]; + XCTAssertEqual(ret.code, WalletInvalidHandle, @"SignusUtils::decryptWithWalletHandle() returned wrong error code."); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} @end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Signus/SignusMediumCases.m b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Signus/SignusMediumCases.m index 6365adae08..112795c6a3 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Signus/SignusMediumCases.m +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Case Tests/Signus/SignusMediumCases.m @@ -40,7 +40,7 @@ - (void)testCreateMyDidWorksForInvalidCryptoType { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; @@ -65,7 +65,7 @@ - (void)testCreateMyDidWorksForInvalidSeed { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; @@ -90,7 +90,7 @@ - (void)testCreateMyDidWorksForInvalidDid { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; @@ -115,7 +115,7 @@ - (void)testCreateMyDidWorksForInvalidJson { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; @@ -142,7 +142,7 @@ -(void)testReplaceKeysWorksForNotExistingDid { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; @@ -164,14 +164,12 @@ -(void)testReplaceKeysWorksForNotExistingDid [TestUtils cleanupStorage]; } -// MARK: - Store their did - -- (void)testStoreTheirDidWorksForInvalidCryptoType +- (void)testReplaceKeysWorksForCorectCryptoType { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"pool1"; - + NSString *poolName = [TestUtils pool]; + // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName @@ -179,20 +177,38 @@ - (void)testStoreTheirDidWorksForInvalidCryptoType handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); - // 2. Store their did - NSString *identityJson = @"{\"did\":\"8wZcEriaNLNKtteJvx7f8i\", \"crypto_type\":\"type\"}"; - ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle - identityJson:identityJson]; - XCTAssertEqual(ret.code, SignusUnknownCryptoError, @"SignusUtils:storeTheirDidWithWalletHandle failed"); + // 2. create my did + + NSString *myDid; + NSString *myVerkey; + ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle + myDidJson:@"{}" + outMyDid:&myDid + outMyVerkey:&myVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils:createMyDidWithWalletHandle failed"); + + // 3. replace keys + + NSString *newVerkey; + ret = [[SignusUtils sharedInstance] replaceKeysWithWalletHandle:walletHandle + did:myDid + identityJson:@"{\"crypto_type\":\"ed25519\"}" + outMyVerKey:&newVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils:replaceKeysWithWalletHandle failed"); + XCTAssertFalse([myVerkey isEqualToString:newVerkey], @"myVerkey is equal to newVerkey"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } -- (void)testStoreTheirDidWorksForInvalidDid +- (void)testReplaceKeysWorksForInvalidCryptoType { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; @@ -201,20 +217,39 @@ - (void)testStoreTheirDidWorksForInvalidDid handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); - // 2. Store their did - NSString *identityJson = @"{\"did\":\"invalid_base58_string\"}"; - ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle - identityJson:identityJson]; - XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils:storeTheirDidWithWalletHandle returned wrong code"); + // 2. create my did + + NSString *myDid; + ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle + myDidJson:@"{}" + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils:createMyDidWithWalletHandle failed"); + + // 3. replace keys + + NSString *myVerkey; + ret = [[SignusUtils sharedInstance] replaceKeysWithWalletHandle:walletHandle + did:myDid + identityJson:@"{\"crypto_type\":\"type\"}" + outMyVerKey:&myVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, SignusUnknownCryptoError, @"SignusUtils:replaceKeysWithWalletHandle returned wrong error code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; + } -- (void)testStoreTheirDidWorksForInvalidVerkey +// MARK: - Store their did + +- (void)testStoreTheirDidWorksForInvalidCryptoType { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; @@ -224,110 +259,65 @@ - (void)testStoreTheirDidWorksForInvalidVerkey XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); // 2. Store their did - NSString *identityJson = @"{\"did\":\"did\", \"verkey\":\"verkey\"}"; + NSString *identityJson = @"{\"did\":\"8wZcEriaNLNKtteJvx7f8i\", \"crypto_type\":\"type\"}"; ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle identityJson:identityJson]; - XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils:storeTheirDidWithWalletHandle returned wrong code"); + XCTAssertEqual(ret.code, SignusUnknownCryptoError, @"SignusUtils:storeTheirDidWithWalletHandle failed"); [TestUtils cleanupStorage]; } -// MARK: - Sign - -- (void)testSignWorksForInvalidMessage +- (void)testStoreTheirDidWorksForInvalidDid { [TestUtils cleanupStorage]; NSError *ret = nil; + NSString *poolName = [TestUtils pool]; // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"pool1" + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); - // 2. create my did - NSString *myDid; - NSString *myDidJson = @"{}"; - ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle - myDidJson:myDidJson - outMyDid:&myDid - outMyVerkey:nil - outMyPk:nil]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() failed"); - - // 3. Sign - NSString *message = @"1495034346617224651"; - - ret = [[SignusUtils sharedInstance] signWithWalletHandle:walletHandle - theirDid:myDid message:message - outSignature:nil]; - XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils::signWithWalletHandle() returned wrong code"); + // 2. Store their did + NSString *identityJson = @"{\"did\":\"invalid_base58_string\"}"; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils:storeTheirDidWithWalletHandle returned wrong code"); [TestUtils cleanupStorage]; } -// MARK: - Verify - -- (void)testVerifyWorksForInvalidMessage +- (void)testStoreTheirDidWorksForInvalidVerkey { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_invalid_message"; + NSString *poolName = [TestUtils pool]; - // 1. Create and open pool ledger config, get pool handle - IndyHandle poolHandle = 0; - - ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName - poolHandle:&poolHandle]; - XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); - - - // 2. Create and open wallet, get wallet handle + // 1. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName xtype:nil handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); - // 3. create did - NSString *did; - NSString *verKey; - NSString *didJson = @"{\"seed\":\"000000000000000000000000Trustee1\"}"; - ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle - myDidJson:didJson - outMyDid:&did - outMyVerkey:&verKey - outMyPk:nil]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() failed"); - XCTAssertTrue(did, @"invalid did"); - XCTAssertTrue(verKey, @"invalid verKey"); - - // 4. Store their did - NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}", did, verKey]; + // 2. Store their did + NSString *identityJson = @"{\"did\":\"did\", \"verkey\":\"verkey\"}"; ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle identityJson:identityJson]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); - - // 5. Verify - NSString *message = @"1496822211362017764"; - - BOOL verified = NO; - ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle - poolHandle:poolHandle - did:did - signature:message - outVerified:&verified]; - XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils::verifyWithWalletHandle() returned wrong code"); + XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils:storeTheirDidWithWalletHandle returned wrong code"); [TestUtils cleanupStorage]; } -- (void)testVerifyWorksForMessageWithoutSignature +// MARK: - Verify + +- (void)testVerifyWorksForInvalidSignatureLen { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_message_without_signature"; + NSString *poolName = [TestUtils pool]; // 1. Create and open pool ledger config, get pool handle IndyHandle poolHandle = 0; @@ -347,35 +337,34 @@ - (void)testVerifyWorksForMessageWithoutSignature // 3. create did NSString *did; NSString *verKey; - NSString *didJson = @"{\"seed\":\"000000000000000000000000Trustee1\"}"; - ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle - myDidJson:didJson - outMyDid:&did - outMyVerkey:&verKey - outMyPk:nil]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() failed"); - XCTAssertTrue(did, @"invalid did"); - XCTAssertTrue(verKey, @"invalid verKey"); + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&did + outMyVerkey:&verKey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for did"); - // 4. Store their did + // 4. Store did NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}", did, verKey]; ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle identityJson:identityJson]; XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); // 5. Verify - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"identifier\":\"GJ1SzoWzavQYfNL9XkaJdrQejfztN4XqdsiV4ct3LXKL\"}"; - + const unsigned char bytes[] = {187, 227, 10, 29, 46, 178, 12, 179, 197, 69, 171, 70, 228, 204, 52, 22, 199, 54, 62, 13, 115, 5, 216, 66, 20, 131, 121, 29, 251, 224, 253, 201, 75, 73, 225, 237, 219, 133, 35, 217, 131, 135, 232, 129, 32}; + NSData *signature = [NSData dataWithBytes:bytes length:sizeof(bytes)]; BOOL verified = NO; ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle poolHandle:poolHandle did:did - signature:message + message:[TestUtils message] + signature:signature outVerified:&verified]; XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils::verifyWithWalletHandle() returned wrong code"); + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; } @@ -383,7 +372,7 @@ - (void)testVerifyWorksForGetNymFromLedgerWithIncompatibleWallet { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_get_nym_from_ledger_with_incompatible_wallet"; + NSString *poolName = [TestUtils pool]; // 1. Create and open pool ledger config, get pool handle IndyHandle poolHandle = 0; @@ -392,7 +381,6 @@ - (void)testVerifyWorksForGetNymFromLedgerWithIncompatibleWallet poolHandle:&poolHandle]; XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); - // 2. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"other_pool_name" @@ -401,43 +389,43 @@ - (void)testVerifyWorksForGetNymFromLedgerWithIncompatibleWallet XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); // 3. create did - NSString *myDid; - NSString *myDidJson = @"{\"seed\":\"00000000000000000000000000000My1\"}"; - ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle - myDidJson:myDidJson - outMyDid:&myDid - outMyVerkey:nil - outMyPk:nil]; - XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() failed"); - XCTAssertTrue(myDid, @"invalid did"); + NSString *mydid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&mydid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed"); // 4. Store their did - NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\"}", myDid]; + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\"}", mydid]; ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle identityJson:identityJson]; XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 5. verify - // 5. Verify - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"identifier\":\"tibTuE59pZn1sCeZpNL5rDzpkpqV3EkDmRpFTizys9Gr3ZieLdGEGyq4h8jsVWW9zSaXSRnfYcVb1yTjUJ7vJai\"}"; - - BOOL verified = NO; ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle poolHandle:poolHandle - did:myDid - signature:message - outVerified:&verified]; - XCTAssertEqual(ret.code, WalletIncompatiblePoolError, @"SignusUtils::verifyWithWalletHandle() returned wrong code"); + did:mydid + message:[TestUtils message] + signature:[TestUtils signature] + outVerified:nil]; + XCTAssertEqual(ret.code, WalletIncompatiblePoolError, @"SignusUtils::verifyWithWalletHandle() returned wrong error code"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } -- (void)testVerifyWorksForGetUnknownNymFromLedger + + +- (void)testVerifyWorksForGetLedgerNotFoundNym { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_get_unknow_nym_from_ledger"; + NSString *poolName = [TestUtils pool]; // 1. Create and open pool ledger config, get pool handle IndyHandle poolHandle = 0; @@ -454,7 +442,7 @@ - (void)testVerifyWorksForGetUnknownNymFromLedger handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); - // 3. create did + // 3. create did NSString *myDid; NSString *myDidJson = @"{\"seed\":\"0000000000000000000000000000Fake\"}"; ret = [[SignusUtils sharedInstance] createMyDidWithWalletHandle:walletHandle @@ -463,7 +451,6 @@ - (void)testVerifyWorksForGetUnknownNymFromLedger outMyVerkey:nil outMyPk:nil]; XCTAssertEqual(ret.code, Success, @"SignusUtils::createMyDidWithWalletHandle() failed"); - XCTAssertTrue(myDid, @"invalid did"); // 4. Store their did NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\"}", myDid]; @@ -472,26 +459,26 @@ - (void)testVerifyWorksForGetUnknownNymFromLedger XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); // 5. Verify - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"identifier\":\"tibTuE59pZn1sCeZpNL5rDzpkpqV3EkDmRpFTizys9Gr3ZieLdGEGyq4h8jsVWW9zSaXSRnfYcVb1yTjUJ7vJai\"}"; BOOL verified = NO; ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle poolHandle:poolHandle did:myDid - signature:message + message:[TestUtils message] + signature:[TestUtils signature] outVerified:&verified]; XCTAssertEqual(ret.code, CommonInvalidState, @"SignusUtils::verifyWithWalletHandle() returned wrong code"); + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; [TestUtils cleanupStorage]; } -- (void)testIndyVerifyWorksForUnknownNym +- (void)testVerifyWorksForGetNymFromLedger { [TestUtils cleanupStorage]; NSError *ret = nil; - NSString *poolName = @"indy_verify_works_for_unknown_nym"; + NSString *poolName = [TestUtils pool]; // 1. Create and open pool ledger config, get pool handle IndyHandle poolHandle = 0; @@ -500,7 +487,6 @@ - (void)testIndyVerifyWorksForUnknownNym poolHandle:&poolHandle]; XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); - // 2. Create and open wallet, get wallet handle IndyHandle walletHandle = 0; ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName @@ -508,21 +494,20 @@ - (void)testIndyVerifyWorksForUnknownNym handle:&walletHandle]; XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); - // 3. create trustee did + // 3. create & store trustee did NSString *trusteeDid; ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle - seed:@"000000000000000000000000Trustee1" + seed:[TestUtils trusteeSeed] outMyDid:&trusteeDid outMyVerkey:nil outMyPk:nil]; XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for trustee"); - XCTAssertTrue(trusteeDid, @"invalid trustee"); // 4. create my did NSString *myDid; NSString *myVerKey; ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle - seed:@"00000000000000000000000000000My1" + seed:[TestUtils mySeed] outMyDid:&myDid outMyVerkey:&myVerKey outMyPk:nil]; @@ -551,19 +536,400 @@ - (void)testIndyVerifyWorksForUnknownNym XCTAssertTrue([nymResponse isValid], @"invalid nymResponse"); // 7. Verify - NSString *message = @"{"\ - "\"reqId\":1496822211362017764,"\ - "\"signature\":\"tibTuE59pZn1sCeZpNL5rDzpkpqV3EkDmRpFTizys9Gr3ZieLdGEGyq4h8jsVWW9zSaXSRnfYcVb1yTjUJ7vJai\"}"; BOOL verified = NO; ret = [[SignusUtils sharedInstance] verifyWithWalletHandle:walletHandle poolHandle:poolHandle did:myDid - signature:message + message:[TestUtils message] + signature:[TestUtils signature] outVerified:&verified]; XCTAssertEqual(ret.code, Success, @"SignusUtils::verifyWithWalletHandle() failed"); XCTAssertTrue(verified); + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +// MARK: - Encrypt + +- (void)testEncryptWorksForUnknownmyDid +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. create and store their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for theirDid"); + + // 4. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}", theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 5. encrypt + + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:walletHandle + poolHandle:poolHandle + myDid:@"unknownDid" + did:theirDid + message:[TestUtils message] + outEncryptedMessage:nil + outNonce:nil]; + XCTAssertEqual(ret.code, WalletNotFoundError, @"SignusUtils::encryptWithWalletHandle() returned wrong error code"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testEncryptWorksForGetNymFromLedgerWithIncompatiblePool +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:@"other_pool" + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. create and store myDid + + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + + // 4. create and store their did + NSString *theirDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&theirDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for theirDid"); + + // 5. store their did + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\"}", theirDid]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 6. encrypt + + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:walletHandle + poolHandle:poolHandle + myDid:myDid + did:theirDid + message:[TestUtils message] + outEncryptedMessage:nil + outNonce:nil]; + XCTAssertEqual(ret.code, WalletIncompatiblePoolError, @"SignusUtils::encryptWithWalletHandle() returned wrong error code"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testEncryptWorksForNotFoundNym +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open pool ledger config, get pool handle + IndyHandle poolHandle = 0; + + ret = [[PoolUtils sharedInstance] createAndOpenPoolLedgerConfigWithName:poolName + poolHandle:&poolHandle]; + XCTAssertEqual(ret.code, Success, @"PoolUtils:createAndOpenPoolLedgerConfig:poolName failed"); + + // 2. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 3. create and store myDid + + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + + // 4. create and store their did + NSString *theirDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:nil + outMyDid:&theirDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for theirDid"); + + // 6. encrypt + + ret = [[SignusUtils sharedInstance] encryptWithWalletHandle:walletHandle + poolHandle:poolHandle + myDid:myDid + did:theirDid + message:[TestUtils message] + outEncryptedMessage:nil + outNonce:nil]; + XCTAssertEqual(ret.code, CommonInvalidState, @"SignusUtils::encryptWithWalletHandle() returned wrong error code"); + + [[PoolUtils sharedInstance] closeHandle:poolHandle]; + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +// MARK: - Decrypt + +- (void)testDecryptWorksForUnknownMyDid +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. create their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for theirDid"); + + // 3. store their did + + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}", theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 4. decrypt + ret = [[SignusUtils sharedInstance] decryptWithWalletHandle:walletHandle + myDid:@"unknownDid" + did:theirDid + encryptedMessage:[TestUtils encryptedMessage] + nonce:[TestUtils nonce] + outDecryptedMessage:nil]; + XCTAssertEqual(ret.code, WalletNotFoundError, @"SignusUtils::decryptWithWalletHandle() returned wrong error code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testDecryptworksForUnknownCoderDid +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. create my did + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + + // 3. create their did + NSString *theirDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for theirDid"); + + // 4. decrypt + ret = [[SignusUtils sharedInstance] decryptWithWalletHandle:walletHandle + myDid:myDid + did:theirDid + encryptedMessage:[TestUtils encryptedMessage] + nonce:[TestUtils nonce] + outDecryptedMessage:nil]; + XCTAssertEqual(ret.code, WalletNotFoundError, @"SignusUtils::decryptWithWalletHandle() returned wrong error code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +- (void)testDecryptWorksForSavedCoderNymWithoutPubKey +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. create my did + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + + // 3. create their did + NSString *theirDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for theirDid"); + + // 4. store their did + + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\"}", theirDid]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 5. decrypt + ret = [[SignusUtils sharedInstance] decryptWithWalletHandle:walletHandle + myDid:myDid + did:theirDid + encryptedMessage:[TestUtils encryptedMessage] + nonce:[TestUtils nonce] + outDecryptedMessage:nil]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils::decryptWithWalletHandle() returned wrong error code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + + [TestUtils cleanupStorage]; +} + +-(void)testDecryptWorksForInvalidNonceLen +{ + [TestUtils cleanupStorage]; + + NSError *ret = nil; + NSString *poolName = [TestUtils pool]; + + // 1. Create and open wallet, get wallet handle + IndyHandle walletHandle = 0; + ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName:poolName + xtype:nil + handle:&walletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils:createAndOpenWalletWithPoolName failed"); + + // 2. create my did + NSString *myDid; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils mySeed] + outMyDid:&myDid + outMyVerkey:nil + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for myDid"); + + // 3. create their did + NSString *theirDid; + NSString *theirVerkey; + ret = [[SignusUtils sharedInstance] createAndStoreMyDidWithWalletHandle:walletHandle + seed:[TestUtils trusteeSeed] + outMyDid:&theirDid + outMyVerkey:&theirVerkey + outMyPk:nil]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::createAndStoreMyDidWithWalletHandle() failed for theirDid"); + + // 4. store their did + + NSString *identityJson = [NSString stringWithFormat:@"{\"did\":\"%@\", \"verkey\":\"%@\"}", theirDid, theirVerkey]; + ret = [[SignusUtils sharedInstance] storeTheirDidWithWalletHandle:walletHandle + identityJson:identityJson]; + XCTAssertEqual(ret.code, Success, @"SignusUtils::storeTheirDidWithWalletHandle() failed"); + + // 5. decrypt + + const unsigned char bytes[] = {24, 99, 107, 70, 58, 6, 252, 149, 225}; + NSData *nonce = [NSData dataWithBytes:bytes length:sizeof(bytes)]; + + ret = [[SignusUtils sharedInstance] decryptWithWalletHandle:walletHandle + myDid:myDid + did:theirDid + encryptedMessage:[TestUtils encryptedMessage] + nonce:nonce + outDecryptedMessage:nil]; + XCTAssertEqual(ret.code, CommonInvalidStructure, @"SignusUtils::decryptWithWalletHandle() returned wrong error code"); + + [[WalletUtils sharedInstance] closeWalletWithHandle:walletHandle]; + [TestUtils cleanupStorage]; } diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Demo Tests/AnoncredsDemo.mm b/wrappers/ios/libindy-pod/libindy-demoTests/Demo Tests/AnoncredsDemo.mm index e538415e71..e17057e674 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Demo Tests/AnoncredsDemo.mm +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Demo Tests/AnoncredsDemo.mm @@ -32,7 +32,7 @@ - (void)tearDown - (void)testAnoncredsDemo { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; NSString *walletName = @"issuer_wallet"; NSString *xType = @"default"; XCTestExpectation *completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Demo Tests/SignusDemo.mm b/wrappers/ios/libindy-pod/libindy-demoTests/Demo Tests/SignusDemo.mm index c10f477700..cd03ea0647 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Demo Tests/SignusDemo.mm +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Demo Tests/SignusDemo.mm @@ -32,7 +32,10 @@ - (void)testSignusDemo { [TestUtils cleanupStorage]; - NSString *poolName = @"pool1"; + NSString *poolName = [TestUtils pool]; + NSString *myWalletName = @"my_wallet4"; + NSString *theirWalletName = @"their_wallet5"; + NSString *xtype = @"default"; NSError *ret = nil; XCTestExpectation* completionExpectation = nil; @@ -40,22 +43,35 @@ - (void)testSignusDemo IndyHandle theirWalletHandle = 0; //TODO CREATE ISSUER, PROVER, VERIFIER WALLETS - //1. Create and open my wallet - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName: poolName - xtype: nil - handle: &myWalletHandle]; + //1. Create my wallet + ret = [[WalletUtils sharedInstance] createWalletWithPoolName:poolName + walletName:myWalletName + xtype:xtype + config:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createWalletWithPoolName() failed for my wallet!"); - XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed!"); + // 2. Open my wallet + ret = [[WalletUtils sharedInstance] openWalletWithName:myWalletName + config:nil + outHandle:&myWalletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::openWalletWithName() failed for my wallet!"); - //2. Create and open Their Wallet - - ret = [[WalletUtils sharedInstance] createAndOpenWalletWithPoolName: poolName - xtype: nil - handle: &theirWalletHandle]; + // 3. Create Their Wallet - XCTAssertEqual(ret.code, Success, @"WalletUtils::createAndOpenWalletWithPoolName() failed!"); - - // 3. Create My DID + ret = [[WalletUtils sharedInstance] createWalletWithPoolName:poolName + walletName:theirWalletName + xtype:xtype + config:nil]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::createWalletWithPoolName() failed for their wallet!"); + + // 4. Open their wallet + + ret = [[WalletUtils sharedInstance] openWalletWithName:theirWalletName + config:nil + outHandle:&theirWalletHandle]; + XCTAssertEqual(ret.code, Success, @"WalletUtils::openWalletWithName() failed for their wallet!"); + + // 5. Create My DID NSString *myDidJson = @"{}"; completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; @@ -81,7 +97,7 @@ - (void)testSignusDemo [self waitForExpectations: @[completionExpectation] timeout:[TestUtils defaultTimeout]]; XCTAssertEqual(ret.code, Success, @"createAndStoreMyDid() failed!"); - // 4. Create Their DID + // 6. Create Their DID NSString *theirDidJson = @"{}"; completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; @@ -108,7 +124,7 @@ - (void)testSignusDemo [self waitForExpectations: @[completionExpectation] timeout:[TestUtils defaultTimeout]]; XCTAssertEqual(ret.code, Success, @"createAndStoreMyDid() failed!"); - // 5. Store Their DID + // 7. Store Their DID NSString* theirIdentityJson = [NSString stringWithFormat: @"{\"did\":\"%@\",\ \"pk\":\"%@\",\ @@ -126,48 +142,51 @@ - (void)testSignusDemo [self waitForExpectations: @[completionExpectation] timeout:[TestUtils defaultTimeout]]; XCTAssertEqual(ret.code, Success, @"createAndStoreMyDid() failed!"); - // 6. Their Sign message + // 8. Their Sign message - NSString* message = @"{"\ + NSString* messageJson = @"{"\ " \"reqId\":1495034346617224651," " \"identifier\":\"GJ1SzoWzavQYfNL9XkaJdrQejfztN4XqdsiV4ct3LXKL\"," " \"operation\":{" " \"type\":\"1\"," " \"dest\":\"4efZu2SXufS556yss7W5k6Po37jt4371RM4whbPKBKdB\"}" "}"; + NSData *message = [messageJson dataUsingEncoding:NSUTF8StringEncoding]; completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; - __block NSString *theirSignature = nil; + __block NSData *signature = nil; - ret = [IndySignus signWithWalletHandle: theirWalletHandle - did: theirDid - msg: message - completion: ^(NSError *error, NSString *signature) - { - XCTAssertEqual(error.code, Success, "sign() got error in completion"); - NSLog(@"signature: %@", signature); - theirSignature = [NSString stringWithString: signature]; - [completionExpectation fulfill]; - }]; + ret = [IndySignus signWithWalletHandle:theirWalletHandle + did:theirDid + message:message + completion:^(NSError *error, NSData *blockSignature) + { + XCTAssertEqual(error.code, Success, "sign() got error in completion"); + NSLog(@"signature: %@", signature); + signature = blockSignature; + [completionExpectation fulfill]; + }]; + [self waitForExpectations: @[completionExpectation] timeout:[TestUtils defaultTimeout]]; XCTAssertEqual(ret.code, Success, @"sign() failed!"); - // 7. I Verify message + // 9. I Verify message IndyHandle poolHandle = 1; completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; - ret = [IndySignus verifySignatureWithWalletHandle: myWalletHandle - poolHandle: poolHandle - did: theirDid - signature: theirSignature - completion: ^(NSError *error, BOOL valid) - { - XCTAssertEqual(error.code, Success, "verifySignature() got error in completion"); - XCTAssertEqual(YES, valid, "verifySignature() signature is not valid"); - [completionExpectation fulfill]; - }]; + ret = [IndySignus verifySignatureWithWalletHandle:myWalletHandle + poolHandle:poolHandle + did:theirDid + message:message + signature:signature + completion:^(NSError *error, BOOL valid) + { + XCTAssertEqual(error.code, Success, "verifySignature() got error in completion"); + XCTAssertEqual(YES, valid, "verifySignature() signature is not valid"); + [completionExpectation fulfill]; + }]; // TODO: There is some error inside closure at rust level [self waitForExpectations: @[completionExpectation] timeout:[TestUtils defaultTimeout]]; diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AgentUtils.h b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AgentUtils.h index bc21354adb..0c7a77d535 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AgentUtils.h +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AgentUtils.h @@ -38,8 +38,14 @@ walletHandle:(IndyHandle)walletHandle did:(NSString *)did; -- (NSError *)removeIdentity:(NSString *) did +- (NSError *)removeIdentity:(NSString *)did listenerHandle:(IndyHandle)listenerHandle walletHandle:(IndyHandle)walletHandle; +- (NSError *)connectHangUpExpectedForPoolHandle:(IndyHandle)poolHandle + walletHandle:(IndyHandle)walletHandle + senderDid:(NSString *)senderDid + receiverDid:(NSString *)receiverDid + isTimeout:(BOOL *)isTimeout; + @end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AgentUtils.m b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AgentUtils.m index b60efba3fd..20a4722ab9 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AgentUtils.m +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AgentUtils.m @@ -94,34 +94,6 @@ - (NSError *)listenForEndpoint:(NSString *)endpoint messageCallback:(void (^)(IndyHandle connectionHandle, NSString *message))messageCallback outListenerHandle:(IndyHandle *)listenerHandle { - - #if 0 - // connection callback - void (^onConnectCallback)(IndyHandle, NSError*, IndyHandle, NSString*, NSString* ) = ^(IndyHandle xListenerHandle, NSError *error, IndyHandle connectionHandle, NSString *senderDid, NSString *receiverDid) { - NSLog(@"AgentUtils::listen::New connection %d on listener %d, err %ld, sender DID %@, receiver DID: %@", (int)connectionHandle, (int)xListenerHandle, (long)error.code, senderDid, receiverDid); - if (connectionCallback) {connectionCallback(xListenerHandle, connectionHandle);} - }; - - - // listener callback. We need to obtain listenerHandle, so we wait for completion. Connection and message callnacks can be triggered multiple times later, so we just pass them to register. - XCTestExpectation* listenerCompletionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"listener completion finished"]; - __block IndyHandle tempListenerHandle = 0; - __block NSError *listenerErr; - - void (^onListenerCallback)(NSError*, IndyHandle) = ^(NSError *error, IndyHandle xListenerHandle) { - NSLog(@"OnListenerCallback triggered."); - listenerErr = error; - tempListenerHandle = xListenerHandle; - [listenerCompletionExpectation fulfill]; - }; - - // message callback - void (^onMessageCallback)(IndyHandle, NSError*, NSString*) = ^(IndyHandle xConnectionHandle, NSError *error, NSString *message) { - NSLog(@"AgentUtils::listen::On connection %d received (with error %ld) agent message (CLI->SRV): %@", (int)xConnectionHandle, (long)error.code, message); - if (messageCallback != nil) { messageCallback(xConnectionHandle, message);} - }; -#endif - // listener callback. We need to obtain listenerHandle, so we wait for completion. Connection and message callnacks can be triggered multiple times later, so we just pass them to register. XCTestExpectation* listenerCompletionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"listener completion finished"]; __block IndyHandle tempListenerHandle = 0; @@ -288,5 +260,51 @@ - (NSError *)removeIdentity:(NSString *) did return err; } +- (NSError *)connectHangUpExpectedForPoolHandle:(IndyHandle)poolHandle + walletHandle:(IndyHandle)walletHandle + senderDid:(NSString *)senderDid + receiverDid:(NSString *)receiverDid + isTimeout:(BOOL *)isTimeout +{ + // connection callback. waiting for completion + XCTestExpectation* connectCompletionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"listener completion finished"]; + __block NSError *connectionErr; + __block IndyHandle tempConnectionHandle; + + void (^onConnectCallback)(NSError*, IndyHandle) = ^(NSError *error, IndyHandle connectionHandle) { + NSLog(@"AgentUtils::connectWithPoolHandle::OnConnectCallback triggered with code: %ld", (long)error.code); + tempConnectionHandle = connectionHandle; + connectionErr = error; + [connectCompletionExpectation fulfill]; + }; + + __weak typeof(self)weakSelf = self; + weakSelf.connectionCallbacks[@(tempConnectionHandle)] = ^(IndyHandle xConnectionHandle, NSError *error, NSString *message) { + NSLog(@"AgentUtils::connectWithPoolHandle::OnMessageCallback triggered invoced with error code: %ld", (long)error.code); + }; + + NSError *ret = [IndyAgent connectWithPoolHandle:poolHandle + walletHandle:walletHandle + senderDId:senderDid + receiverDId:receiverDid + connectionHandler:onConnectCallback + messageHandler:(void (^)(IndyHandle, NSError*, NSString*))weakSelf.connectionCallbacks[@(tempConnectionHandle)]]; + + if (ret.code != Success) + { + return ret; + } + + // wait for connection callback + [self waitForExpectations: @[connectCompletionExpectation] timeout:[TestUtils shortTimeout]]; + + if (connectionErr) + { + return connectionErr; + } else { + if (isTimeout) { *isTimeout = YES; } + return ret; + } +} @end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AnoncredsUtils.m b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AnoncredsUtils.m index 1053ac8ecd..b2f8474f66 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AnoncredsUtils.m +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/AnoncredsUtils.m @@ -282,7 +282,7 @@ - (NSError *)proverStoreClaimOffer:(IndyHandle)walletHandle completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; NSError *ret = [IndyAnoncreds proverStoreClaimOfferWithWalletHandle:walletHandle - claimOfferJSON:str + claimOfferJSON:str completion: ^(NSError *error) { err = error; @@ -554,8 +554,6 @@ - (NSError *)initializeCommonWalletAndReturnHandle:(IndyHandle *)walletHandle [TestUtils cleanupStorage]; - XCTAssertTrue(5 == 3, @"FALSE!!!!"); - ////TODO Need clean after tests but not exists After function in Cargo NSError *ret; @@ -576,7 +574,7 @@ - (NSError *)initializeCommonWalletAndReturnHandle:(IndyHandle *)walletHandle issuerDid:[AnoncredsUtils issuerDid] schemaJson:schema signatureType:nil - createNonRevoc:false + createNonRevoc:NO claimDefJson:&tempClaimDefJson]; XCTAssertEqual(ret.code, Success, @"issuerCreateClaimDefinifionWithWalletHandle failed"); diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/SignusUtils.h b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/SignusUtils.h index 8862ace447..788ac41458 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/SignusUtils.h +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/SignusUtils.h @@ -14,10 +14,12 @@ + (SignusUtils *)sharedInstance; +// MARK: - Instance methods + - (NSError *)signWithWalletHandle:(IndyHandle)walletHandle theirDid:(NSString *)theirDid - message:(NSString *)message - outSignature:(NSString **)signature; + message:(NSData *)message + outSignature:(NSData **)signature; - (NSError *)createMyDidWithWalletHandle:(IndyHandle)walletHandle myDidJson:(NSString *)myDidJson @@ -49,6 +51,22 @@ - (NSError *)verifyWithWalletHandle:(IndyHandle)walletHandle poolHandle:(IndyHandle)poolHandle did:(NSString *)did - signature:(NSString *)signature + message:(NSData *)message + signature:(NSData *)signature outVerified:(BOOL *)verified; + +- (NSError *)encryptWithWalletHandle:(IndyHandle)walletHandle + poolHandle:(IndyHandle)poolHandle + myDid:(NSString *)myDid + did:(NSString *)did + message:(NSData *)message + outEncryptedMessage:(NSData **)encryptedMessage + outNonce:(NSData **)nonce; + +- (NSError *)decryptWithWalletHandle:(IndyHandle)walletHandle + myDid:(NSString *)myDid + did:(NSString *)did + encryptedMessage:(NSData *)encryptedMessage + nonce:(NSData *)nonce + outDecryptedMessage:(NSData **)decryptedMessage; @end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/SignusUtils.mm b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/SignusUtils.mm index 868f4f06df..962f0bd5ab 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/SignusUtils.mm +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/SignusUtils.mm @@ -24,26 +24,66 @@ + (SignusUtils *)sharedInstance return instance; } + ++ (NSString *)pool +{ + return @"pool_1"; +} + ++ (NSData *)message +{ + NSString *messageJson = @"{\"reqId\":1496822211362017764}"; + return [messageJson dataUsingEncoding:NSUTF8StringEncoding]; +} + ++ (NSData *)encryptedMessage +{ + const unsigned char bytes[] = {187, 227, 10, 29, 46, 178, 12, 179, 197, 69, 171, 70, 228, 204, 52, 22, 199, 54, 62, 13, 115, 5, 216, 66, 20, 131, 121, 29, 251, 224, 253, 201, 75, 73, 225, 237, 219, 133, 35, 217, 131, 135, 232, 129, 32}; + return [NSData dataWithBytes:bytes length:sizeof(bytes)]; +} + ++ (NSData *)nonce +{ + const unsigned char bytes[] = {242, 246, 53, 153, 106, 37, 185, 65, 212, 14, 109, 131, 200, 169, 94, 110, 51, 47, 101, 89, 0, 171, 105, 183}; + return [NSData dataWithBytes:bytes length:sizeof(bytes)]; +} + ++ (NSData *)signature +{ + const unsigned char bytes[] = {169, 215, 8, 225, 7, 107, 110, 9, 193, 162, 202, 214, 162, 66, 238, 211, 63, 209, 12, 196, 8, 211, 55, 27, 120, 94, 204, 147, 53, 104, 103, 61, 60, 249, 237, 127, 103, 46, 220, 223, 10, 95, 75, 53, 245, 210, 241, 151, 191, 41, 48, 30, 9, 16, 78, 252, 157, 206, 210, 145, 125, 133, 109, 11}; + return [NSData dataWithBytes:bytes length:sizeof(bytes)]; +} + ++ (NSString *)trusteeSeed +{ + return @"000000000000000000000000Trustee1"; +} + ++ (NSString *)mySeed +{ + return @"00000000000000000000000000000My1"; +} + - (NSError *)signWithWalletHandle:(IndyHandle)walletHandle theirDid:(NSString *)theirDid - message:(NSString *)message - outSignature:(NSString **)signature + message:(NSData *)message + outSignature:(NSData **)signature { XCTestExpectation* completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; __block NSError *err = nil; - __block NSString *signSignature = nil; NSError *ret; - + ret = [IndySignus signWithWalletHandle:walletHandle - did:theirDid - msg:message - completion:^(NSError *error, NSString *blockSignature) - { - err = error; - signSignature = blockSignature; - [completionExpectation fulfill]; - }]; + did:theirDid + message:message + completion:^(NSError *error, NSData *blockSignature) + { + err = error; + if (signature) { *signature = blockSignature; } + [completionExpectation fulfill]; + }]; + if( ret.code != Success) { @@ -52,8 +92,6 @@ - (NSError *)signWithWalletHandle:(IndyHandle)walletHandle [self waitForExpectations: @[completionExpectation] timeout:[TestUtils defaultTimeout]]; - if (signature) { *signature = signSignature; } - return err; } @@ -239,24 +277,60 @@ - (NSError *)replaceKeysWithWalletHandle:(IndyHandle)walletHandle - (NSError *)verifyWithWalletHandle:(IndyHandle)walletHandle poolHandle:(IndyHandle)poolHandle did:(NSString *)did - signature:(NSString *)signature + message:(NSData *)message + signature:(NSData *)signature outVerified:(BOOL *)verified { XCTestExpectation* completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; __block NSError *err = nil; - __block BOOL outVerified; NSError *ret; ret = [IndySignus verifySignatureWithWalletHandle:walletHandle - poolHandle:poolHandle - did:did - signature:signature - completion:^(NSError *error, BOOL valid) + poolHandle:poolHandle + did:did + message:message + signature:signature + completion:^(NSError *error, BOOL valid) + { + err = error; + if (verified) { *verified = valid; } + [completionExpectation fulfill]; + }]; + + if( ret.code != Success) { - err = error; - outVerified = valid; - [completionExpectation fulfill]; - }]; + return ret; + } + + [self waitForExpectations: @[completionExpectation] timeout:[TestUtils longTimeout]]; + + return err; +} + +- (NSError *)encryptWithWalletHandle:(IndyHandle)walletHandle + poolHandle:(IndyHandle)poolHandle + myDid:(NSString *)myDid + did:(NSString *)did + message:(NSData *)message + outEncryptedMessage:(NSData **)encryptedMessage + outNonce:(NSData **)nonce +{ + XCTestExpectation* completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; + __block NSError *err = nil; + NSError *ret; + + ret = [IndySignus encryptWithWalletHandle:walletHandle + pool:poolHandle + myDid:myDid + did:did + message:message + completion:^(NSError *error, NSData *encryptedMsg, NSData *closureNonce) + { + err = error; + if (encryptedMessage) { *encryptedMessage = encryptedMsg; } + if (nonce) { *nonce = closureNonce; } + [completionExpectation fulfill]; + }]; if( ret.code != Success) { @@ -265,9 +339,41 @@ - (NSError *)verifyWithWalletHandle:(IndyHandle)walletHandle [self waitForExpectations: @[completionExpectation] timeout:[TestUtils longTimeout]]; - if (verified) { *verified = outVerified; } + return err; +} + +- (NSError *)decryptWithWalletHandle:(IndyHandle)walletHandle + myDid:(NSString *)myDid + did:(NSString *)did + encryptedMessage:(NSData *)encryptedMessage + nonce:(NSData *)nonce + outDecryptedMessage:(NSData **)decryptedMessage +{ + XCTestExpectation* completionExpectation = [[ XCTestExpectation alloc] initWithDescription: @"completion finished"]; + __block NSError *err = nil; + NSError *ret; + + ret = [IndySignus decryptWithWalletHandle:walletHandle + myDid:myDid + did:did + encryptedMessage:encryptedMessage + nonce:nonce + completion:^(NSError *error, NSData *decryptedMsg) + { + err = error; + if (decryptedMessage) { *decryptedMessage = decryptedMsg; } + [completionExpectation fulfill]; + }]; + + if( ret.code != Success) + { + return ret; + } + + [self waitForExpectations: @[completionExpectation] timeout:[TestUtils longTimeout]]; return err; } + @end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/TestUtils.h b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/TestUtils.h index b0d9e7b7a6..9cdb02a568 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/TestUtils.h +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/TestUtils.h @@ -29,4 +29,17 @@ + (void) cleanupStorage; ++ (NSString *)pool; ++ (NSData *)message; ++ (NSData *)encryptedMessage; ++ (NSData *)nonce; ++ (NSData *)signature; ++ (NSString *)trusteeSeed; ++ (NSString *)mySeed; ++ (NSString *)endpoint; ++ (NSString *)clientMessage; ++ (NSString *)serverMessage; + + + @end diff --git a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/TestUtils.m b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/TestUtils.m index 6695147a8b..13533c2e8f 100644 --- a/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/TestUtils.m +++ b/wrappers/ios/libindy-pod/libindy-demoTests/Test Utils/TestUtils.m @@ -32,17 +32,71 @@ +(void) cleanupStorage +(NSTimeInterval) defaultTimeout { - return 1000; + return 10; } +(NSTimeInterval) shortTimeout { - return 1000; + return 5; } +(NSTimeInterval) longTimeout { - return 1000; + return 100; +} + ++ (NSString *)pool +{ + return @"pool_1"; +} + ++ (NSData *)message +{ + NSString *messageJson = @"{\"reqId\":1496822211362017764}"; + return [messageJson dataUsingEncoding:NSUTF8StringEncoding]; +} + ++ (NSData *)encryptedMessage +{ + const unsigned char bytes[] = {187, 227, 10, 29, 46, 178, 12, 179, 197, 69, 171, 70, 228, 204, 52, 22, 199, 54, 62, 13, 115, 5, 216, 66, 20, 131, 121, 29, 251, 224, 253, 201, 75, 73, 225, 237, 219, 133, 35, 217, 131, 135, 232, 129, 32}; + return [NSData dataWithBytes:bytes length:sizeof(bytes)]; +} + ++ (NSData *)nonce +{ + const unsigned char bytes[] = {242, 246, 53, 153, 106, 37, 185, 65, 212, 14, 109, 131, 200, 169, 94, 110, 51, 47, 101, 89, 0, 171, 105, 183}; + return [NSData dataWithBytes:bytes length:sizeof(bytes)]; +} + ++ (NSData *)signature +{ + const unsigned char bytes[] = {169, 215, 8, 225, 7, 107, 110, 9, 193, 162, 202, 214, 162, 66, 238, 211, 63, 209, 12, 196, 8, 211, 55, 27, 120, 94, 204, 147, 53, 104, 103, 61, 60, 249, 237, 127, 103, 46, 220, 223, 10, 95, 75, 53, 245, 210, 241, 151, 191, 41, 48, 30, 9, 16, 78, 252, 157, 206, 210, 145, 125, 133, 109, 11}; + return [NSData dataWithBytes:bytes length:sizeof(bytes)]; +} + ++ (NSString *)trusteeSeed +{ + return @"000000000000000000000000Trustee1"; +} + ++ (NSString *)mySeed +{ + return @"00000000000000000000000000000My1"; +} + ++ (NSString *)endpoint +{ + return @"127.0.0.1:9700"; +} + ++ (NSString *)clientMessage +{ + return @"msg_from_client"; +} + ++ (NSString *)serverMessage +{ + return @"msg_from_server"; } @end diff --git a/wrappers/ios/libindy-pod/libindy/Utils/IndyCallbacks.h b/wrappers/ios/libindy-pod/libindy/Utils/IndyCallbacks.h index ccfff2ce6e..c7f2a872aa 100644 --- a/wrappers/ios/libindy-pod/libindy/Utils/IndyCallbacks.h +++ b/wrappers/ios/libindy-pod/libindy/Utils/IndyCallbacks.h @@ -16,7 +16,7 @@ extern void IndyWrapperCommon3PHCallback(indy_handle_t xcommand_handle, extern void IndyWrapperCommon3PSCallback(indy_handle_t xcommand_handle, indy_error_t err, - const char* arg1); + const char *arg1); extern void IndyWrapperCommon3PBCallback(indy_handle_t xcommand_handle, indy_error_t err, @@ -24,21 +24,33 @@ extern void IndyWrapperCommon3PBCallback(indy_handle_t xcommand_handle, extern void IndyWrapperCommon4PCallback(indy_handle_t xcommand_handle, indy_error_t err, - const char* arg1, + const char *arg1, const char *arg2); +extern void IndyWrapperCommon4PDataCallback(indy_handle_t xcommand_handle, + indy_error_t err, + const uint8_t* arg1, + uint32_t arg2); + extern void IndyWrapperCommon5PCallback(indy_handle_t xcommand_handle, indy_error_t err, - const char* arg1, + const char *arg1, const char *arg2, const char *arg3); extern void IndyWrapperCommon5PSCallback(indy_handle_t xcommand_handle, indy_error_t err, indy_handle_t connection_handle, - const char* arg1, + const char *arg1, const char *arg2); +extern void IndyWrapperCommon6PDataCallback(indy_handle_t xcommand_handle, + indy_error_t err, + const uint8_t* arg1, + uint32_t arg2, + const uint8_t* arg3, + uint32_t arg4); + extern void IndyWrapperCommonAgentOutgoingConnectionCallback(indy_handle_t xcommand_handle, indy_error_t err, indy_handle_t connection_handle); diff --git a/wrappers/ios/libindy-pod/libindy/Utils/IndyCallbacks.mm b/wrappers/ios/libindy-pod/libindy/Utils/IndyCallbacks.mm index b2ebb944c6..8215a633f6 100644 --- a/wrappers/ios/libindy-pod/libindy/Utils/IndyCallbacks.mm +++ b/wrappers/ios/libindy-pod/libindy/Utils/IndyCallbacks.mm @@ -409,6 +409,29 @@ void IndyWrapperCommon4PCallback(indy_handle_t xcommand_handle, } } +/// Arguments arg1 and arg2 will be converted to nsdata +void IndyWrapperCommon4PDataCallback(indy_handle_t xcommand_handle, + indy_error_t err, + const uint8_t* arg1, + uint32_t arg2) +{ + void * block = [[IndyCallbacks sharedInstance] commandCompletionFor: xcommand_handle]; + [[IndyCallbacks sharedInstance] deleteCommandHandleFor: xcommand_handle]; + + void (^completion)(NSError*, NSData* arg) = (__bridge void (^)(NSError*, NSData* arg))block; + + NSData *sarg = [NSData dataWithBytes:arg1 length:arg2]; + + if (completion) + { + dispatch_async(dispatch_get_main_queue(), ^ + { + NSError *error = [ NSError errorFromIndyError: err ]; + completion(error, sarg); + }); + } +} + void IndyWrapperCommon5PCallback(indy_handle_t xcommand_handle, indy_error_t err, const char* arg1, @@ -458,6 +481,31 @@ void IndyWrapperCommon5PSCallback(indy_handle_t xcommand_handle, } } +void IndyWrapperCommon6PDataCallback(indy_handle_t xcommand_handle, + indy_error_t err, + const uint8_t* arg1, + uint32_t arg2, + const uint8_t* arg3, + uint32_t arg4) +{ + void * block = [[IndyCallbacks sharedInstance] commandCompletionFor: xcommand_handle]; + [[IndyCallbacks sharedInstance] deleteCommandHandleFor: xcommand_handle]; + + void (^completion)(NSError*, NSData* xArg1, NSData* xArg2) = (__bridge void (^)(NSError*, NSData* xArg1, NSData* xArg2))block; + + NSData *sarg1 = [NSData dataWithBytes:arg1 length:arg2]; + NSData *sarg2 = [NSData dataWithBytes:arg3 length:arg4]; + + if (completion) + { + dispatch_async(dispatch_get_main_queue(), ^ + { + NSError *error = [ NSError errorFromIndyError: err ]; + completion(error, sarg1, sarg2); + }); + } +} + void IndyWrapperCommonAgentOutgoingConnectionCallback(indy_handle_t xcommand_handle, indy_error_t err, indy_handle_t connection_handle) diff --git a/wrappers/ios/libindy-pod/libindy/Wrapper/IndySignus.h b/wrappers/ios/libindy-pod/libindy/Wrapper/IndySignus.h index 116872ae7a..cdffa0dd10 100644 --- a/wrappers/ios/libindy-pod/libindy/Wrapper/IndySignus.h +++ b/wrappers/ios/libindy-pod/libindy/Wrapper/IndySignus.h @@ -11,12 +11,17 @@ + (NSError *)createAndStoreMyDidWithWalletHandle:(IndyHandle)walletHandle didJSON:(NSString *)didJson - completion:(void (^)(NSError *error, NSString *did, NSString *verkey, NSString *pk)) handler; + completion:(void (^)(NSError *error, + NSString *did, + NSString *verkey, + NSString *pk)) handler; + (NSError *)replaceKeysWithWalletHandle:(IndyHandle)walletHandle did:(NSString *)did identityJSON:(NSString *)json - completion:(void (^)(NSError *error, NSString *verkey, NSString *pk)) handler; + completion:(void (^)(NSError *error, + NSString *verkey, + NSString *pk)) handler; + (NSError *)storeTheirDidWithWalletHandle:(IndyHandle)walletHandle identityJSON:(NSString *)json @@ -24,27 +29,28 @@ + (NSError *)signWithWalletHandle:(IndyHandle)walletHandle did:(NSString *)did - msg:(NSString *)msg - completion:(void (^)(NSError *error, NSString *signature)) handler; + message:(NSData*)message + completion:(void (^)(NSError *error, NSData *signature)) handler; + (NSError *)verifySignatureWithWalletHandle:(IndyHandle)walletHandle poolHandle:(IndyHandle)poolHandle did:(NSString *)did - signature:(NSString *)signature + message:(NSData *)message + signature:(NSData *)signature completion:(void (^)(NSError *error, BOOL valid)) handler; + (NSError *)encryptWithWalletHandle:(IndyHandle)walletHandle pool:(IndyHandle)poolHandle myDid:(NSString *)myDid did:(NSString *)did - msg:(NSString *)msg - completion:(void (^)(NSError *error, NSString *encryptedMsg, NSString *nonce)) handler; + message:(NSData *)message + completion:(void (^)(NSError *error, NSData *encryptedMsg, NSData *nonce)) handler; + (NSError *)decryptWithWalletHandle:(IndyHandle)walletHandle myDid:(NSString *)myDid did:(NSString *)did - encryptedMsg:(NSString *)msg - nonce:(NSString *)nonce - completion:(void (^)(NSError *error, NSString *decryptedMsg)) handler; + encryptedMessage:(NSData *)encryptedMessage + nonce:(NSData *)nonce + completion:(void (^)(NSError *error, NSData *decryptedMessage)) handler; @end diff --git a/wrappers/ios/libindy-pod/libindy/Wrapper/IndySignus.mm b/wrappers/ios/libindy-pod/libindy/Wrapper/IndySignus.mm index 47dd938d71..d203abc233 100644 --- a/wrappers/ios/libindy-pod/libindy/Wrapper/IndySignus.mm +++ b/wrappers/ios/libindy-pod/libindy/Wrapper/IndySignus.mm @@ -77,19 +77,22 @@ + (NSError *)storeTheirDidWithWalletHandle:(IndyHandle)walletHandle + (NSError *)signWithWalletHandle:(IndyHandle)walletHandle did:(NSString *)did - msg:(NSString *)msg - completion:(void (^)(NSError *error, NSString *signature)) handler + message:(NSData*)message + completion:(void (^)(NSError *error, + NSData *signature)) handler { indy_error_t ret; indy_handle_t handle = [[IndyCallbacks sharedInstance] createCommandHandleFor: (void*) handler]; - ret = indy_sign( handle, + uint32_t messageLen = (uint32_t)[message length]; + uint8_t *messageRaw = (uint8_t *)[message bytes]; + ret = indy_sign(handle, walletHandle, [did UTF8String], - [msg UTF8String], - IndyWrapperCommon3PSCallback - ); + messageRaw, + messageLen, + IndyWrapperCommon4PDataCallback); if( ret != Success ) { @@ -102,18 +105,27 @@ + (NSError *)signWithWalletHandle:(IndyHandle)walletHandle + (NSError *)verifySignatureWithWalletHandle:(IndyHandle)walletHandle poolHandle:(IndyHandle)poolHandle did:(NSString *)did - signature:(NSString *)signature + message:(NSData *)message + signature:(NSData *)signature completion:(void (^)(NSError *error, BOOL valid)) handler { indy_error_t ret; indy_handle_t handle = [[IndyCallbacks sharedInstance] createCommandHandleFor: (void*) handler]; + uint32_t messageLen = (uint32_t)[message length]; + uint8_t *messageRaw = (uint8_t *)[message bytes]; + uint32_t signatureLen = (uint32_t)[signature length]; + uint8_t *signatureRaw = (uint8_t *)[signature bytes]; + ret = indy_verify_signature(handle, walletHandle, poolHandle, [did UTF8String], - [signature UTF8String], + messageRaw, + messageLen, + signatureRaw, + signatureLen, IndyWrapperCommon3PBCallback); if( ret != Success ) { @@ -127,21 +139,24 @@ + (NSError *)encryptWithWalletHandle:(IndyHandle)walletHandle pool:(IndyHandle)poolHandle myDid:(NSString *)myDid did:(NSString *)did - msg:(NSString *)msg - completion:(void (^)(NSError *error, NSString *encryptedMsg, NSString *nonce)) handler + message:(NSData *)message + completion:(void (^)(NSError *error, NSData *encryptedMsg, NSData *nonce)) handler { indy_error_t ret; indy_handle_t handle = [[IndyCallbacks sharedInstance] createCommandHandleFor: (void*) handler]; - ret = indy_encrypt( handle, + uint32_t messageLen = (uint32_t)[message length]; + uint8_t *messageRaw = (uint8_t *)[message bytes]; + + ret = indy_encrypt(handle, walletHandle, poolHandle, [myDid UTF8String], [did UTF8String], - [msg UTF8String], - IndyWrapperCommon4PCallback - ); + messageRaw, + messageLen, + IndyWrapperCommon6PDataCallback); if( ret != Success ) { @@ -154,22 +169,28 @@ + (NSError *)encryptWithWalletHandle:(IndyHandle)walletHandle + (NSError *)decryptWithWalletHandle:(IndyHandle)walletHandle myDid:(NSString *)myDid did:(NSString *)did - encryptedMsg:(NSString *)msg - nonce:(NSString *)nonce - completion:(void (^)(NSError *error, NSString *decryptedMsg)) handler + encryptedMessage:(NSData *)encryptedMessage + nonce:(NSData *)nonce + completion:(void (^)(NSError *error, NSData *decryptedMessage)) handler { indy_error_t ret; indy_handle_t handle = [[IndyCallbacks sharedInstance] createCommandHandleFor: (void*) handler]; - ret = indy_decrypt( handle, + uint32_t messageLen = (uint32_t)[encryptedMessage length]; + uint8_t *messageRaw = (uint8_t *)[encryptedMessage bytes]; + uint32_t nonceLen = (uint32_t)[nonce length]; + uint8_t *nonceRaw = (uint8_t *)[nonce bytes]; + + ret = indy_decrypt(handle, walletHandle, [myDid UTF8String], [did UTF8String], - [msg UTF8String], - [nonce UTF8String], - IndyWrapperCommon3PSCallback - ); + messageRaw, + messageLen, + nonceRaw, + nonceLen, + IndyWrapperCommon4PDataCallback); if( ret != Success ) {