diff --git a/Example/IrohaCommunication.xcodeproj/project.pbxproj b/Example/IrohaCommunication.xcodeproj/project.pbxproj index c5b84982c..aa6966e20 100644 --- a/Example/IrohaCommunication.xcodeproj/project.pbxproj +++ b/Example/IrohaCommunication.xcodeproj/project.pbxproj @@ -50,6 +50,17 @@ 6003F5B2195388D20070C39A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F591195388D20070C39A /* UIKit.framework */; }; 6003F5BA195388D20070C39A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6003F5B8195388D20070C39A /* InfoPlist.strings */; }; 7C8964A2BAE86B246752F5D5 /* Pods_UnitTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 872B4DCAED8E7A58AFC8B200 /* Pods_UnitTests.framework */; }; + 844761D3224A4946009ADC0E /* IRInvalidSerializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761D2224A4946009ADC0E /* IRInvalidSerializationTests.m */; }; + 844761E7224A4EFF009ADC0E /* Block.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761D6224A4EFF009ADC0E /* Block.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 844761E8224A4EFF009ADC0E /* Endpoint.pbrpc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761D8224A4EFF009ADC0E /* Endpoint.pbrpc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 844761E9224A4EFF009ADC0E /* QryResponses.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761DA224A4EFF009ADC0E /* QryResponses.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 844761EA224A4EFF009ADC0E /* Queries.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761DC224A4EFF009ADC0E /* Queries.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 844761EB224A4EFF009ADC0E /* Transaction.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761DD224A4EFF009ADC0E /* Transaction.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 844761EC224A4EFF009ADC0E /* Endpoint.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761DE224A4EFF009ADC0E /* Endpoint.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 844761ED224A4EFF009ADC0E /* Primitive.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761E0224A4EFF009ADC0E /* Primitive.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 844761EE224A4EFF009ADC0E /* Proposal.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761E2224A4EFF009ADC0E /* Proposal.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 844761EF224A4EFF009ADC0E /* Commands.pbobjc.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761E5224A4EFF009ADC0E /* Commands.pbobjc.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 844761F1224A5133009ADC0E /* IRQueryResponseDeserializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 844761F0224A5133009ADC0E /* IRQueryResponseDeserializationTests.m */; }; 8CA1A478927FFA33BA30A5B4 /* Pods_IntegrationTestHost.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 175938D8DE70C85E0CEA25AF /* Pods_IntegrationTestHost.framework */; }; /* End PBXBuildFile section */ @@ -139,6 +150,26 @@ 606FC2411953D9B200FFA9A0 /* Tests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Tests-Prefix.pch"; sourceTree = ""; }; 6BF3097DA0B1239D7D3C0799 /* Pods-UnitTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-UnitTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-UnitTests/Pods-UnitTests.release.xcconfig"; sourceTree = ""; }; 74D33D817C5F7989BD3071B1 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = ""; }; + 844761D2224A4946009ADC0E /* IRInvalidSerializationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IRInvalidSerializationTests.m; sourceTree = ""; }; + 844761D5224A4EFF009ADC0E /* Primitive.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Primitive.pbobjc.h; sourceTree = ""; }; + 844761D6224A4EFF009ADC0E /* Block.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Block.pbobjc.m; sourceTree = ""; }; + 844761D7224A4EFF009ADC0E /* Endpoint.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Endpoint.pbobjc.h; sourceTree = ""; }; + 844761D8224A4EFF009ADC0E /* Endpoint.pbrpc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Endpoint.pbrpc.m; sourceTree = ""; }; + 844761D9224A4EFF009ADC0E /* Proposal.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Proposal.pbobjc.h; sourceTree = ""; }; + 844761DA224A4EFF009ADC0E /* QryResponses.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QryResponses.pbobjc.m; sourceTree = ""; }; + 844761DB224A4EFF009ADC0E /* Commands.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Commands.pbobjc.h; sourceTree = ""; }; + 844761DC224A4EFF009ADC0E /* Queries.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Queries.pbobjc.m; sourceTree = ""; }; + 844761DD224A4EFF009ADC0E /* Transaction.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Transaction.pbobjc.m; sourceTree = ""; }; + 844761DE224A4EFF009ADC0E /* Endpoint.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Endpoint.pbobjc.m; sourceTree = ""; }; + 844761DF224A4EFF009ADC0E /* Block.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Block.pbobjc.h; sourceTree = ""; }; + 844761E0224A4EFF009ADC0E /* Primitive.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Primitive.pbobjc.m; sourceTree = ""; }; + 844761E1224A4EFF009ADC0E /* QryResponses.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QryResponses.pbobjc.h; sourceTree = ""; }; + 844761E2224A4EFF009ADC0E /* Proposal.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Proposal.pbobjc.m; sourceTree = ""; }; + 844761E3224A4EFF009ADC0E /* Endpoint.pbrpc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Endpoint.pbrpc.h; sourceTree = ""; }; + 844761E4224A4EFF009ADC0E /* Queries.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Queries.pbobjc.h; sourceTree = ""; }; + 844761E5224A4EFF009ADC0E /* Commands.pbobjc.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Commands.pbobjc.m; sourceTree = ""; }; + 844761E6224A4EFF009ADC0E /* Transaction.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Transaction.pbobjc.h; sourceTree = ""; }; + 844761F0224A5133009ADC0E /* IRQueryResponseDeserializationTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IRQueryResponseDeserializationTests.m; sourceTree = ""; }; 84EA860F14455E442B5C9B29 /* IrohaCommunication.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = IrohaCommunication.podspec; path = ../IrohaCommunication.podspec; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.ruby; }; 86F19532265991B547599707 /* Pods-IrohaExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-IrohaExample.release.xcconfig"; path = "Pods/Target Support Files/Pods-IrohaExample/Pods-IrohaExample.release.xcconfig"; sourceTree = ""; }; 872B4DCAED8E7A58AFC8B200 /* Pods_UnitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_UnitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -381,6 +412,8 @@ 6003F5B5195388D20070C39A /* Tests */ = { isa = PBXGroup; children = ( + 844761D4224A4EFF009ADC0E /* ProtoGen */, + 844761D1224A4946009ADC0E /* Serialization */, 021C24E621DD3C90005D5304 /* Query */, 02E319DB21DBB18500B33B66 /* Promise */, 02E3198921D0FED500B33B66 /* Transaction */, @@ -410,6 +443,42 @@ name = "Podspec Metadata"; sourceTree = ""; }; + 844761D1224A4946009ADC0E /* Serialization */ = { + isa = PBXGroup; + children = ( + 844761D2224A4946009ADC0E /* IRInvalidSerializationTests.m */, + 844761F0224A5133009ADC0E /* IRQueryResponseDeserializationTests.m */, + ); + name = Serialization; + path = ../../Tests/Serialization; + sourceTree = ""; + }; + 844761D4224A4EFF009ADC0E /* ProtoGen */ = { + isa = PBXGroup; + children = ( + 844761D5224A4EFF009ADC0E /* Primitive.pbobjc.h */, + 844761D6224A4EFF009ADC0E /* Block.pbobjc.m */, + 844761D7224A4EFF009ADC0E /* Endpoint.pbobjc.h */, + 844761D8224A4EFF009ADC0E /* Endpoint.pbrpc.m */, + 844761D9224A4EFF009ADC0E /* Proposal.pbobjc.h */, + 844761DA224A4EFF009ADC0E /* QryResponses.pbobjc.m */, + 844761DB224A4EFF009ADC0E /* Commands.pbobjc.h */, + 844761DC224A4EFF009ADC0E /* Queries.pbobjc.m */, + 844761DD224A4EFF009ADC0E /* Transaction.pbobjc.m */, + 844761DE224A4EFF009ADC0E /* Endpoint.pbobjc.m */, + 844761DF224A4EFF009ADC0E /* Block.pbobjc.h */, + 844761E0224A4EFF009ADC0E /* Primitive.pbobjc.m */, + 844761E1224A4EFF009ADC0E /* QryResponses.pbobjc.h */, + 844761E2224A4EFF009ADC0E /* Proposal.pbobjc.m */, + 844761E3224A4EFF009ADC0E /* Endpoint.pbrpc.h */, + 844761E4224A4EFF009ADC0E /* Queries.pbobjc.h */, + 844761E5224A4EFF009ADC0E /* Commands.pbobjc.m */, + 844761E6224A4EFF009ADC0E /* Transaction.pbobjc.h */, + ); + name = ProtoGen; + path = ../../ProtoGen; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -788,16 +857,27 @@ files = ( 02E3198B21D0FEF000B33B66 /* IRTransactionTests.m in Sources */, 0200346F21C840D1001A6A53 /* IRAmountTests.m in Sources */, + 844761E7224A4EFF009ADC0E /* Block.pbobjc.m in Sources */, 02E319DD21DBB18500B33B66 /* IRPromiseTests.m in Sources */, + 844761ED224A4EFF009ADC0E /* Primitive.pbobjc.m in Sources */, 0200347521C8516F001A6A53 /* IRGrantablePermissionTests.m in Sources */, + 844761EC224A4EFF009ADC0E /* Endpoint.pbobjc.m in Sources */, 0200347121C849FF001A6A53 /* IRAssetIdTests.m in Sources */, + 844761D3224A4946009ADC0E /* IRInvalidSerializationTests.m in Sources */, + 844761EA224A4EFF009ADC0E /* Queries.pbobjc.m in Sources */, 021C24E821DD3C90005D5304 /* IRQueryBuilderTests.m in Sources */, + 844761E8224A4EFF009ADC0E /* Endpoint.pbrpc.m in Sources */, + 844761EF224A4EFF009ADC0E /* Commands.pbobjc.m in Sources */, 0200347721C853E2001A6A53 /* IRRolePermissionTests.m in Sources */, 021C24F021DDF706005D5304 /* IRAccountAssetTests.m in Sources */, 0200345E21C62D3F001A6A53 /* IRDomainTests.m in Sources */, + 844761E9224A4EFF009ADC0E /* QryResponses.pbobjc.m in Sources */, 021C843D21EA6E950037F633 /* IRBlockQueryBuilderTests.m in Sources */, + 844761EB224A4EFF009ADC0E /* Transaction.pbobjc.m in Sources */, 0200345F21C62D3F001A6A53 /* IRAccountIdTests.m in Sources */, + 844761F1224A5133009ADC0E /* IRQueryResponseDeserializationTests.m in Sources */, 0200346121C63F45001A6A53 /* IRAddressTests.m in Sources */, + 844761EE224A4EFF009ADC0E /* Proposal.pbobjc.m in Sources */, 021C24AF21DCDDE3005D5304 /* IRPaginationTests.m in Sources */, 0200347321C84E5C001A6A53 /* IRRoleNameTests.m in Sources */, ); diff --git a/IrohaCommunication.podspec b/IrohaCommunication.podspec index 2b068c7a2..1206d85a3 100755 --- a/IrohaCommunication.podspec +++ b/IrohaCommunication.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'IrohaCommunication' - s.version = '3.0.2' + s.version = '3.1.0' s.summary = 'Helper classes to use for communication with Iroha blockchain.' s.homepage = 'https://github.com/hyperledger/iroha-ios' @@ -33,7 +33,7 @@ Pod::Spec.new do |s| s.pod_target_xcconfig = { 'GCC_PREPROCESSOR_DEFINITIONS' => 'GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1' } s.test_spec do |ts| - ts.source_files = 'Tests/**/*.{h,m}' + ts.source_files = 'Tests/**/*.{h,m}', 'ProtoGen/*.{h,m}' end end diff --git a/IrohaCommunication/Classes/Private/IRTransactionImpl.m b/IrohaCommunication/Classes/Private/IRTransactionImpl.m index d0e7b87a4..2465a8f18 100755 --- a/IrohaCommunication/Classes/Private/IRTransactionImpl.m +++ b/IrohaCommunication/Classes/Private/IRTransactionImpl.m @@ -68,14 +68,6 @@ - (nullable id)transform:(NSError*_Nullable*_Nullable)error { return transaction; } -#pragma mark - Transaction data - -- (nullable NSData *)transactionData { - Transaction *transaction = [self transform:nil]; - - return transaction.data; -} - #pragma mark - Signable - (nullable NSData*)transactionHashWithError:(NSError **)error { diff --git a/IrohaCommunication/Classes/Public/Model/IRTransaction.h b/IrohaCommunication/Classes/Public/Model/IRTransaction.h index 5c4f5b113..6bcb6e7ef 100755 --- a/IrohaCommunication/Classes/Public/Model/IRTransaction.h +++ b/IrohaCommunication/Classes/Public/Model/IRTransaction.h @@ -26,7 +26,6 @@ typedef NS_ENUM(NSUInteger, IRTransactionBatchType) { @property(nonatomic, readonly)NSArray>* _Nullable signatures; @property(nonatomic, readonly)NSArray* _Nullable batchHashes; @property(nonatomic, readonly)IRTransactionBatchType batchType; -@property(nonatomic, readonly)NSData* _Nullable transactionData; - (nullable NSData*)transactionHashWithError:(NSError **)error; diff --git a/IrohaCommunication/Classes/Public/Serializers/IRSerializationFactory.h b/IrohaCommunication/Classes/Public/Serializers/IRSerializationFactory.h new file mode 100644 index 000000000..8b8027498 --- /dev/null +++ b/IrohaCommunication/Classes/Public/Serializers/IRSerializationFactory.h @@ -0,0 +1,23 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#import +#import "IRTransaction.h" +#import "IRQueryRequest.h" +#import "IRQueryResponse.h" + +@protocol IRSerializationFactoryProtocol + ++ (nullable NSData*)serializeTransaction:(nonnull id)transaction error:(NSError**)error; ++ (nullable NSData*)serializeQueryRequest:(nonnull id)queryRequest error:(NSError**)error; + ++ (nullable id)deserializeTransactionFromData:(nonnull NSData*)data error:(NSError**)error; ++ (nullable id)deserializeQueryResponseFromData:(nonnull NSData*)data error:(NSError**)error; + +@end + +@interface IRSerializationFactory : NSObject + +@end diff --git a/IrohaCommunication/Classes/Public/Serializers/IRSerializationFactory.m b/IrohaCommunication/Classes/Public/Serializers/IRSerializationFactory.m new file mode 100644 index 000000000..ee2e40951 --- /dev/null +++ b/IrohaCommunication/Classes/Public/Serializers/IRSerializationFactory.m @@ -0,0 +1,81 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#import "IRSerializationFactory.h" +#import "IRTransactionImpl.h" +#import "IRQueryRequestImpl.h" +#import "IRTransactionImpl+Proto.h" +#import "IRQueryResponse+Proto.h" +#import "Transaction.pbobjc.h" +#import "Queries.pbobjc.h" +#import "QryResponses.pbobjc.h" + +@implementation IRSerializationFactory + ++ (nullable NSData*)serializeTransaction:(nonnull id)transaction error:(NSError**)error { + if (![transaction conformsToProtocol:@protocol(IRProtobufTransformable)]) { + + if (error) { + NSString *message = @"Unsupported transaction implementation"; + *error = [NSError errorWithDomain:NSStringFromClass([IRSerializationFactory class]) + code:IRTransactionErrorSerialization + userInfo:@{NSLocalizedDescriptionKey: message}]; + } + + return nil; + } + + Transaction *pbTransaction = [(id)transaction transform:error]; + + if (!pbTransaction) { + return nil; + } + + return [pbTransaction data]; +} + ++ (nullable NSData*)serializeQueryRequest:(nonnull id)queryRequest error:(NSError**)error { + if (![queryRequest conformsToProtocol:@protocol(IRProtobufTransformable)]) { + + if (error) { + NSString *message = @"Unsupported query request implementation"; + *error = [NSError errorWithDomain:NSStringFromClass([IRSerializationFactory class]) + code:IRQueryRequestErrorSerialization + userInfo:@{NSLocalizedDescriptionKey: message}]; + } + + return nil; + } + + Query *protobufQuery = [(id)queryRequest transform:error]; + + if (!protobufQuery) { + return nil; + } + + return [protobufQuery data]; +} + ++ (nullable id)deserializeTransactionFromData:(nonnull NSData*)data error:(NSError**)error { + Transaction *transaction = [[Transaction alloc] initWithData:data error:error]; + + if (!transaction) { + return nil; + } + + return [IRTransaction transactionFromPbTransaction:transaction error:error]; +} + ++ (nullable id)deserializeQueryResponseFromData:(nonnull NSData*)data error:(NSError**)error { + QueryResponse *response = [[QueryResponse alloc] initWithData:data error:error]; + + if (!response) { + return nil; + } + + return [IRQueryResponseProtoFactory responseFromProtobuf:response error:error]; +} + +@end diff --git a/IrohaCommunication/Classes/Public/Service/IRNetworkService.m b/IrohaCommunication/Classes/Public/Service/IRNetworkService.m index c39fe39d2..30f48d92d 100755 --- a/IrohaCommunication/Classes/Public/Service/IRNetworkService.m +++ b/IrohaCommunication/Classes/Public/Service/IRNetworkService.m @@ -206,7 +206,7 @@ - (nonnull IRPromise*)executeQueryRequest:(nonnull id)queryReque if (![queryRequest conformsToProtocol:@protocol(IRProtobufTransformable)]) { NSString *message = @"Unsupported query implementation"; NSError *error = [NSError errorWithDomain:NSStringFromClass([IRNetworkService class]) - code:IRTransactionErrorSerialization + code:IRQueryRequestErrorSerialization userInfo:@{NSLocalizedDescriptionKey: message}]; [promise fulfillWithResult:error]; diff --git a/Tests/Promise/IRPromiseTests.m b/Tests/Promise/IRPromiseTests.m index e1c142e7b..ce343611f 100755 --- a/Tests/Promise/IRPromiseTests.m +++ b/Tests/Promise/IRPromiseTests.m @@ -6,7 +6,6 @@ @import XCTest; @import IrohaCommunication; - @interface IRPromiseTests : XCTestCase @end diff --git a/Tests/Query/IRQueryBuilderTests.m b/Tests/Query/IRQueryBuilderTests.m index eac2b372d..69b3b54e5 100755 --- a/Tests/Query/IRQueryBuilderTests.m +++ b/Tests/Query/IRQueryBuilderTests.m @@ -113,6 +113,13 @@ - (void)testQueries { XCTAssertNotNil(signedQueryRequest.peerSignature); XCTAssertNil(error); + + error = nil; + NSData *rawQueryRequestData = [IRSerializationFactory serializeQueryRequest:signedQueryRequest + error:&error]; + + XCTAssertNotNil(rawQueryRequestData); + XCTAssertNil(error); } } diff --git a/Tests/Serialization/IRInvalidSerializationTests.m b/Tests/Serialization/IRInvalidSerializationTests.m new file mode 100644 index 000000000..78127ca5f --- /dev/null +++ b/Tests/Serialization/IRInvalidSerializationTests.m @@ -0,0 +1,35 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +@import XCTest; +@import IrohaCommunication; + +@interface IRInvalidSerializationTests : XCTestCase + +@end + +@implementation IRInvalidSerializationTests + +- (void)testInvalidTransactionDeserialization { + NSError *error = nil; + NSData *invalidTransactionData = [[NSData alloc] init]; + id invalidTransaction = [IRSerializationFactory deserializeTransactionFromData:invalidTransactionData + error:&error]; + + XCTAssertNil(invalidTransaction); + XCTAssertNotNil(error); +} + +- (void)testInvalidQueryResponseDeserialization { + NSError *error = nil; + NSData *invalidQueryResponseData = [[NSData alloc] init]; + id invalidQueryResponse = [IRSerializationFactory deserializeQueryResponseFromData:invalidQueryResponseData + error:&error]; + + XCTAssertNil(invalidQueryResponse); + XCTAssertNotNil(error); +} + +@end diff --git a/Tests/Serialization/IRQueryResponseDeserializationTests.m b/Tests/Serialization/IRQueryResponseDeserializationTests.m new file mode 100644 index 000000000..0fab55fba --- /dev/null +++ b/Tests/Serialization/IRQueryResponseDeserializationTests.m @@ -0,0 +1,54 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +@import XCTest; +@import IrohaCommunication; +@import IrohaCrypto; +#import "QryResponses.pbobjc.h" + +static NSString * const VALID_ACCOUNT_IDENTIFIER = @"bob@gmail.com"; +static NSString * const VALID_ASSET_IDENTIFIER = @"testcoin#gmail.com"; +static NSString * const VALID_BALANCE = @"100"; + +@interface IRQueryResponseDeserializationTests : XCTestCase + +@end + +@implementation IRQueryResponseDeserializationTests + +- (void)testAssetResponseDeserialization { + // given + AccountAsset *accountAsset = [[AccountAsset alloc] init]; + accountAsset.accountId = VALID_ACCOUNT_IDENTIFIER; + accountAsset.assetId = VALID_ASSET_IDENTIFIER; + accountAsset.balance = VALID_BALANCE; + + AccountAssetResponse *assetResponse = [[AccountAssetResponse alloc] init]; + [assetResponse setAccountAssetsArray:[NSMutableArray arrayWithObject:accountAsset]]; + + NSString *queryHash = [assetResponse.data toHexString]; + + QueryResponse *response = [[QueryResponse alloc] init]; + [response setQueryHash:queryHash]; + [response setAccountAssetsResponse:assetResponse]; + + // when + + NSError *error = nil; + id irQueryResponse = [IRSerializationFactory deserializeQueryResponseFromData:response.data error:&error]; + + // then + + if (!irQueryResponse || ![irQueryResponse conformsToProtocol:@protocol(IRAccountAssetsResponse)]) { + XCTFail("Unexpected empty response"); + return; + } + + XCTAssertEqualObjects(irQueryResponse.queryHash, assetResponse.data); + + XCTAssertNil(error); +} + +@end diff --git a/Tests/Transaction/IRTransactionTests.m b/Tests/Transaction/IRTransactionTests.m index d899095c3..0aadbae09 100755 --- a/Tests/Transaction/IRTransactionTests.m +++ b/Tests/Transaction/IRTransactionTests.m @@ -60,10 +60,12 @@ - (void)testTransactionBuildWithAllCommandsAndSingleSignature { usingPublicKey:resultSignature.publicKey]; XCTAssertTrue(verified); - - NSData *transactionData = transaction.transactionData; + + error = nil; + NSData *transactionData = [IRSerializationFactory serializeTransaction:transaction error:&error]; XCTAssertNotNil(transactionData); + XCTAssertNil(error); } - (void)testInitializationFromRawTransaction { @@ -82,14 +84,14 @@ - (void)testInitializationFromRawTransaction { error:&error]; error = nil; - id rawTransaction = [(IRTransaction*)signedTransaction transform:&error]; + NSData* transactionData = [IRSerializationFactory serializeTransaction:signedTransaction error:&error]; - XCTAssertNotNil(rawTransaction); + XCTAssertNotNil(transactionData); XCTAssertNil(error); error = nil; - id restoredTransaction = [IRTransaction transactionFromPbTransaction:rawTransaction - error:&error]; + id restoredTransaction = [IRSerializationFactory deserializeTransactionFromData:transactionData + error:&error]; XCTAssertNotNil(restoredTransaction); XCTAssertNil(error); @@ -115,10 +117,6 @@ - (void)testInitializationFromRawTransaction { XCTAssertEqualObjects(originalSignature.signature.rawData, restoredSignature.signature.rawData); XCTAssertEqualObjects(originalSignature.publicKey.rawData, restoredSignature.publicKey.rawData); - - NSData *transactionData = transaction.transactionData; - - XCTAssertNotNil(transactionData); } - (void)testBatchInitialization { @@ -155,10 +153,13 @@ - (void)testBatchInitialization { XCTAssertNil(batchedTransaction.signatures); XCTAssertEqualObjects([batchedTransaction batchHashWithError:nil], batchHash); + + error = nil; + NSData *batchTransactionData = [IRSerializationFactory serializeTransaction:batchedTransaction + error:&error]; - NSData *transactionData = transaction.transactionData; - - XCTAssertNotNil(transactionData); + XCTAssertNotNil(batchTransactionData); + XCTAssertNil(error); } #pragma mark - Private