diff --git a/Core/XMPPFramework.h b/Core/XMPPFramework.h index 87f6ef49f8..794bd7e6df 100644 --- a/Core/XMPPFramework.h +++ b/Core/XMPPFramework.h @@ -34,6 +34,7 @@ #import "XMPPTimer.h" #import "XMPPCoreDataStorage.h" #import "XMPPCoreDataStorageProtected.h" +#import "XMPPDelayedDelivery.h" #import "NSXMLElement+XEP_0203.h" #import "XMPPFileTransfer.h" #import "XMPPIncomingFileTransfer.h" diff --git a/Extensions/XEP-0203/NSXMLElement+XEP_0203.h b/Extensions/XEP-0203/NSXMLElement+XEP_0203.h index 662fda8303..84fd413ba9 100644 --- a/Extensions/XEP-0203/NSXMLElement+XEP_0203.h +++ b/Extensions/XEP-0203/NSXMLElement+XEP_0203.h @@ -1,10 +1,13 @@ #import @import KissXML; +@class XMPPJID; @interface NSXMLElement (XEP_0203) @property (nonatomic, readonly) BOOL wasDelayed; @property (nonatomic, readonly, nullable) NSDate *delayedDeliveryDate; +@property (nonatomic, readonly, nullable) XMPPJID *delayedDeliveryFrom; +@property (nonatomic, readonly, nullable) NSString *delayedDeliveryReasonDescription; @end diff --git a/Extensions/XEP-0203/NSXMLElement+XEP_0203.m b/Extensions/XEP-0203/NSXMLElement+XEP_0203.m index ee404d3326..82b0b371e6 100644 --- a/Extensions/XEP-0203/NSXMLElement+XEP_0203.m +++ b/Extensions/XEP-0203/NSXMLElement+XEP_0203.m @@ -1,6 +1,7 @@ #import "NSXMLElement+XEP_0203.h" #import "XMPPDateTimeProfiles.h" #import "NSXMLElement+XMPP.h" +#import "XMPPJID.h" #if ! __has_feature(objc_arc) #warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). @@ -10,27 +11,11 @@ @implementation NSXMLElement (XEP_0203) - (BOOL)wasDelayed { - NSXMLElement *delay; - - delay = [self elementForName:@"delay" xmlns:@"urn:xmpp:delay"]; - if (delay) - { - return YES; - } - - delay = [self elementForName:@"x" xmlns:@"jabber:x:delay"]; - if (delay) - { - return YES; - } - - return NO; + return [self anyDelayedDeliveryChildElement] != nil; } - (NSDate *)delayedDeliveryDate { - NSXMLElement *delay; - // From XEP-0203 (Delayed Delivery) // // - delay = [self elementForName:@"x" xmlns:@"jabber:x:delay"]; - if (delay) + NSXMLElement *legacyDelay = [self legacyDelayedDeliveryChildElement]; + if (legacyDelay) { NSDate *stamp; - NSString *stampValue = [delay attributeStringValueForName:@"stamp"]; + NSString *stampValue = [legacyDelay attributeStringValueForName:@"stamp"]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; @@ -81,4 +66,30 @@ - (NSDate *)delayedDeliveryDate return nil; } +- (XMPPJID *)delayedDeliveryFrom +{ + NSString *delayedDeliveryFromString = [[self anyDelayedDeliveryChildElement] attributeStringValueForName:@"from"]; + return delayedDeliveryFromString ? [XMPPJID jidWithString:delayedDeliveryFromString] : nil; +} + +- (NSString *)delayedDeliveryReasonDescription +{ + return [self anyDelayedDeliveryChildElement].stringValue; +} + +- (NSXMLElement *)delayedDeliveryChildElement +{ + return [self elementForName:@"delay" xmlns:@"urn:xmpp:delay"]; +} + +- (NSXMLElement *)legacyDelayedDeliveryChildElement +{ + return [self elementForName:@"x" xmlns:@"jabber:x:delay"]; +} + +- (NSXMLElement *)anyDelayedDeliveryChildElement +{ + return [self delayedDeliveryChildElement] ?: [self legacyDelayedDeliveryChildElement]; +} + @end diff --git a/Extensions/XEP-0203/XMPPDelayedDelivery.h b/Extensions/XEP-0203/XMPPDelayedDelivery.h new file mode 100644 index 0000000000..29d5240dd0 --- /dev/null +++ b/Extensions/XEP-0203/XMPPDelayedDelivery.h @@ -0,0 +1,25 @@ +#import "XMPP.h" + +NS_ASSUME_NONNULL_BEGIN + +/// A module for processing XEP-0203 Delayed Delivery information in incoming XMPP stanzas. +@interface XMPPDelayedDelivery : XMPPModule + +@end + +/// A protocol defining @c XMPPDelayedDelivery module delegate API. +@protocol XMPPDelayedDeliveryDelegate + +@optional + +/// Notifies the delegate that a delayed delivery message has been received in the stream. +- (void)xmppDelayedDelivery:(XMPPDelayedDelivery *)xmppDelayedDelivery + didReceiveDelayedMessage:(XMPPMessage *)delayedMessage; + +/// Notifies the delegate that a delayed delivery presence has been received in the stream. +- (void)xmppDelayedDelivery:(XMPPDelayedDelivery *)xmppDelayedDelivery + didReceiveDelayedPresence:(XMPPPresence *)delayedPresence; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Extensions/XEP-0203/XMPPDelayedDelivery.m b/Extensions/XEP-0203/XMPPDelayedDelivery.m new file mode 100644 index 0000000000..7831754150 --- /dev/null +++ b/Extensions/XEP-0203/XMPPDelayedDelivery.m @@ -0,0 +1,57 @@ +#import "XMPPDelayedDelivery.h" +#import "XMPPLogging.h" +#import "NSXMLElement+XEP_0203.h" + +// Log levels: off, error, warn, info, verbose +// Log flags: trace +#if DEBUG +static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN; // | XMPP_LOG_FLAG_TRACE; +#else +static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN; +#endif + +@implementation XMPPDelayedDelivery + +- (void)didActivate +{ + XMPPLogTrace(); +} + +- (void)willDeactivate +{ + XMPPLogTrace(); +} + +- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message +{ + XMPPLogTrace(); + + if (![message wasDelayed]) { + return; + } + + XMPPLogInfo(@"Received delayed delivery message with date: %@, origin: %@, reason description: %@", + [message delayedDeliveryDate], + [message delayedDeliveryFrom] ?: @"unspecified", + [message delayedDeliveryReasonDescription] ?: @"unspecified"); + + [multicastDelegate xmppDelayedDelivery:self didReceiveDelayedMessage:message]; +} + +- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence +{ + XMPPLogTrace(); + + if (![presence wasDelayed]) { + return; + } + + XMPPLogInfo(@"Received delayed delivery presence with date: %@, origin: %@, reason description: %@", + [presence delayedDeliveryDate], + [presence delayedDeliveryFrom] ?: @"unspecified", + [presence delayedDeliveryReasonDescription] ?: @"unspecified"); + + [multicastDelegate xmppDelayedDelivery:self didReceiveDelayedPresence:presence]; +} + +@end diff --git a/XMPPFramework.xcodeproj/project.pbxproj b/XMPPFramework.xcodeproj/project.pbxproj index a48131ff9f..179acff88b 100644 --- a/XMPPFramework.xcodeproj/project.pbxproj +++ b/XMPPFramework.xcodeproj/project.pbxproj @@ -938,6 +938,12 @@ D9E6A05A1F92DEE100D8BFCB /* XMPPStanzaIdModule.m in Sources */ = {isa = PBXBuildFile; fileRef = D9E6A0561F92DEE100D8BFCB /* XMPPStanzaIdModule.m */; }; D9E6A05B1F92DEE100D8BFCB /* XMPPStanzaIdModule.m in Sources */ = {isa = PBXBuildFile; fileRef = D9E6A0561F92DEE100D8BFCB /* XMPPStanzaIdModule.m */; }; D9E6A05C1F92DEE100D8BFCB /* XMPPStanzaIdModule.m in Sources */ = {isa = PBXBuildFile; fileRef = D9E6A0561F92DEE100D8BFCB /* XMPPStanzaIdModule.m */; }; + DD1C59831F4429FD003D73DB /* XMPPDelayedDelivery.h in Headers */ = {isa = PBXBuildFile; fileRef = DD1C59811F4429FD003D73DB /* XMPPDelayedDelivery.h */; settings = {ATTRIBUTES = (Public, ); }; }; + DD1C59841F4429FD003D73DB /* XMPPDelayedDelivery.h in Headers */ = {isa = PBXBuildFile; fileRef = DD1C59811F4429FD003D73DB /* XMPPDelayedDelivery.h */; settings = {ATTRIBUTES = (Public, ); }; }; + DD1C59851F4429FD003D73DB /* XMPPDelayedDelivery.h in Headers */ = {isa = PBXBuildFile; fileRef = DD1C59811F4429FD003D73DB /* XMPPDelayedDelivery.h */; settings = {ATTRIBUTES = (Public, ); }; }; + DD1C59861F4429FD003D73DB /* XMPPDelayedDelivery.m in Sources */ = {isa = PBXBuildFile; fileRef = DD1C59821F4429FD003D73DB /* XMPPDelayedDelivery.m */; }; + DD1C59871F4429FD003D73DB /* XMPPDelayedDelivery.m in Sources */ = {isa = PBXBuildFile; fileRef = DD1C59821F4429FD003D73DB /* XMPPDelayedDelivery.m */; }; + DD1C59881F4429FD003D73DB /* XMPPDelayedDelivery.m in Sources */ = {isa = PBXBuildFile; fileRef = DD1C59821F4429FD003D73DB /* XMPPDelayedDelivery.m */; }; DD1E73331ED885FD009B529B /* XMPPRoomLightCoreDataStorage+XEP_0313.h in Headers */ = {isa = PBXBuildFile; fileRef = DD1E73311ED885FD009B529B /* XMPPRoomLightCoreDataStorage+XEP_0313.h */; settings = {ATTRIBUTES = (Public, ); }; }; DD1E73341ED885FD009B529B /* XMPPRoomLightCoreDataStorage+XEP_0313.h in Headers */ = {isa = PBXBuildFile; fileRef = DD1E73311ED885FD009B529B /* XMPPRoomLightCoreDataStorage+XEP_0313.h */; settings = {ATTRIBUTES = (Public, ); }; }; DD1E73351ED885FD009B529B /* XMPPRoomLightCoreDataStorage+XEP_0313.h in Headers */ = {isa = PBXBuildFile; fileRef = DD1E73311ED885FD009B529B /* XMPPRoomLightCoreDataStorage+XEP_0313.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -1543,6 +1549,8 @@ D9DCD6BF1E625B4D0010D1C7 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/AppKit.framework; sourceTree = DEVELOPER_DIR; }; D9E6A0551F92DEE100D8BFCB /* XMPPStanzaIdModule.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XMPPStanzaIdModule.h; sourceTree = ""; }; D9E6A0561F92DEE100D8BFCB /* XMPPStanzaIdModule.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XMPPStanzaIdModule.m; sourceTree = ""; }; + DD1C59811F4429FD003D73DB /* XMPPDelayedDelivery.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XMPPDelayedDelivery.h; sourceTree = ""; }; + DD1C59821F4429FD003D73DB /* XMPPDelayedDelivery.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPDelayedDelivery.m; sourceTree = ""; }; DD1E73311ED885FD009B529B /* XMPPRoomLightCoreDataStorage+XEP_0313.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "XMPPRoomLightCoreDataStorage+XEP_0313.h"; sourceTree = ""; }; DD1E73321ED885FD009B529B /* XMPPRoomLightCoreDataStorage+XEP_0313.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "XMPPRoomLightCoreDataStorage+XEP_0313.m"; sourceTree = ""; }; DD1E73391ED88622009B529B /* XMPPRoomLightCoreDataStorageProtected.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XMPPRoomLightCoreDataStorageProtected.h; sourceTree = ""; }; @@ -2366,6 +2374,8 @@ D9DCD2131E6250930010D1C7 /* XEP-0203 */ = { isa = PBXGroup; children = ( + DD1C59811F4429FD003D73DB /* XMPPDelayedDelivery.h */, + DD1C59821F4429FD003D73DB /* XMPPDelayedDelivery.m */, D9DCD2141E6250930010D1C7 /* NSXMLElement+XEP_0203.h */, D9DCD2151E6250930010D1C7 /* NSXMLElement+XEP_0203.m */, ); @@ -2696,6 +2706,7 @@ D9DCD26E1E6250930010D1C7 /* XMPPResourceCoreDataStorageObject.h in Headers */, D9DCD2831E6250930010D1C7 /* XMPPIQ+JabberRPC.h in Headers */, D9DCD3201E6250930010D1C7 /* XMPPMessageArchiveManagement.h in Headers */, + DD1C59831F4429FD003D73DB /* XMPPDelayedDelivery.h in Headers */, D9DCD2D61E6250930010D1C7 /* NSDate+XMPPDateTimeProfiles.h in Headers */, D9DCD2BA1E6250930010D1C7 /* XMPPvCardTempAdrTypes.h in Headers */, D9DCD2C81E6250930010D1C7 /* XMPPResultSet.h in Headers */, @@ -2861,6 +2872,7 @@ D9DCD4FB1E6256D90010D1C7 /* XMPPResourceCoreDataStorageObject.h in Headers */, D9DCD4FC1E6256D90010D1C7 /* XMPPIQ+JabberRPC.h in Headers */, D9DCD4FD1E6256D90010D1C7 /* XMPPMessageArchiveManagement.h in Headers */, + DD1C59841F4429FD003D73DB /* XMPPDelayedDelivery.h in Headers */, D9DCD4FE1E6256D90010D1C7 /* NSDate+XMPPDateTimeProfiles.h in Headers */, D9DCD4FF1E6256D90010D1C7 /* XMPPvCardTempAdrTypes.h in Headers */, D9DCD5001E6256D90010D1C7 /* XMPPResultSet.h in Headers */, @@ -3026,6 +3038,7 @@ D9DCD65E1E6258CF0010D1C7 /* XMPPResourceCoreDataStorageObject.h in Headers */, D9DCD65F1E6258CF0010D1C7 /* XMPPIQ+JabberRPC.h in Headers */, D9DCD6601E6258CF0010D1C7 /* XMPPMessageArchiveManagement.h in Headers */, + DD1C59851F4429FD003D73DB /* XMPPDelayedDelivery.h in Headers */, D9DCD6611E6258CF0010D1C7 /* NSDate+XMPPDateTimeProfiles.h in Headers */, D9DCD6621E6258CF0010D1C7 /* XMPPvCardTempAdrTypes.h in Headers */, D9DCD6631E6258CF0010D1C7 /* XMPPResultSet.h in Headers */, @@ -3519,6 +3532,7 @@ D9DCD2531E6250930010D1C7 /* XMPPIncomingFileTransfer.m in Sources */, 0D44BB531E537105000930E0 /* XMPPSCRAMSHA1Authentication.m in Sources */, D9DCD32B1E6250930010D1C7 /* XMPPIQ+XEP_0357.m in Sources */, + DD1C59861F4429FD003D73DB /* XMPPDelayedDelivery.m in Sources */, D9DCD2F71E6250930010D1C7 /* XMPPvCardAvatarModule.m in Sources */, D9DCD26B1E6250930010D1C7 /* XMPPReconnect.m in Sources */, D9DCD2B91E6250930010D1C7 /* XMPPvCardTempAdr.m in Sources */, @@ -3675,6 +3689,7 @@ D9DCD45B1E6256D90010D1C7 /* XMPPIncomingFileTransfer.m in Sources */, D9DCD45C1E6256D90010D1C7 /* XMPPSCRAMSHA1Authentication.m in Sources */, D9DCD45D1E6256D90010D1C7 /* XMPPIQ+XEP_0357.m in Sources */, + DD1C59871F4429FD003D73DB /* XMPPDelayedDelivery.m in Sources */, D9DCD45E1E6256D90010D1C7 /* XMPPvCardAvatarModule.m in Sources */, D9DCD45F1E6256D90010D1C7 /* XMPPReconnect.m in Sources */, D9DCD4601E6256D90010D1C7 /* XMPPvCardTempAdr.m in Sources */, @@ -3831,6 +3846,7 @@ D9DCD5BE1E6258CF0010D1C7 /* XMPPIncomingFileTransfer.m in Sources */, D9DCD5BF1E6258CF0010D1C7 /* XMPPSCRAMSHA1Authentication.m in Sources */, D9DCD5C01E6258CF0010D1C7 /* XMPPIQ+XEP_0357.m in Sources */, + DD1C59881F4429FD003D73DB /* XMPPDelayedDelivery.m in Sources */, D9DCD5C11E6258CF0010D1C7 /* XMPPvCardAvatarModule.m in Sources */, D9DCD5C21E6258CF0010D1C7 /* XMPPReconnect.m in Sources */, D9DCD5C31E6258CF0010D1C7 /* XMPPvCardTempAdr.m in Sources */, diff --git a/Xcode/Testing-Carthage/XMPPFrameworkTests.xcodeproj/project.pbxproj b/Xcode/Testing-Carthage/XMPPFrameworkTests.xcodeproj/project.pbxproj index c7b0708fcb..b18ddd086d 100644 --- a/Xcode/Testing-Carthage/XMPPFrameworkTests.xcodeproj/project.pbxproj +++ b/Xcode/Testing-Carthage/XMPPFrameworkTests.xcodeproj/project.pbxproj @@ -64,6 +64,9 @@ D9DCD70E1E625C560010D1C7 /* OMEMOTestStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = D99C5E0C1D99C48100FB068A /* OMEMOTestStorage.m */; }; D9DCD7191E625CAE0010D1C7 /* XMPPFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D9DCD6B01E625A9B0010D1C7 /* XMPPFramework.framework */; }; D9E35E701D90B894002E7CF7 /* OMEMOElementTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D9E35E6F1D90B894002E7CF7 /* OMEMOElementTests.m */; }; + DD4003F91F7528A90078D144 /* XMPPDelayedDeliveryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DD4003F81F7528A90078D144 /* XMPPDelayedDeliveryTests.m */; }; + DD4003FA1F7528B40078D144 /* XMPPDelayedDeliveryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DD4003F81F7528A90078D144 /* XMPPDelayedDeliveryTests.m */; }; + DD4003FB1F7528B40078D144 /* XMPPDelayedDeliveryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DD4003F81F7528A90078D144 /* XMPPDelayedDeliveryTests.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -137,6 +140,7 @@ D9DCD3EC1E6255E10010D1C7 /* XMPPFrameworkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = XMPPFrameworkTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; D9DCD7151E625C560010D1C7 /* XMPPFrameworkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = XMPPFrameworkTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; D9E35E6F1D90B894002E7CF7 /* OMEMOElementTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OMEMOElementTests.m; path = "../Testing-Shared/OMEMOElementTests.m"; sourceTree = SOURCE_ROOT; }; + DD4003F81F7528A90078D144 /* XMPPDelayedDeliveryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMPPDelayedDeliveryTests.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -207,6 +211,7 @@ D973A0791D2F18040096F3ED /* XMPPStorageHintTests.m */, D973A07A1D2F18040096F3ED /* XMPPURITests.m */, D973A07B1D2F18040096F3ED /* XMPPvCardTests.m */, + DD4003F81F7528A90078D144 /* XMPPDelayedDeliveryTests.m */, 63F50D971C60208200CA0201 /* Info.plist */, ); name = XMPPFrameworkTests; @@ -386,6 +391,7 @@ buildActionMask = 2147483647; files = ( D973A07C1D2F18040096F3ED /* CapabilitiesHashingTest.m in Sources */, + DD4003F91F7528A90078D144 /* XMPPDelayedDeliveryTests.m in Sources */, D973A0811D2F18040096F3ED /* XMPPMUCLightTests.m in Sources */, D973A07D1D2F18040096F3ED /* EncodeDecodeTest.m in Sources */, D973A0831D2F18040096F3ED /* XMPPRoomLightCoreDataStorageTests.m in Sources */, @@ -411,6 +417,7 @@ buildActionMask = 2147483647; files = ( D9DCD3D51E6255E10010D1C7 /* CapabilitiesHashingTest.m in Sources */, + DD4003FA1F7528B40078D144 /* XMPPDelayedDeliveryTests.m in Sources */, D9DCD3D61E6255E10010D1C7 /* XMPPMUCLightTests.m in Sources */, D9DCD3D71E6255E10010D1C7 /* EncodeDecodeTest.m in Sources */, D9DCD3D81E6255E10010D1C7 /* XMPPRoomLightCoreDataStorageTests.m in Sources */, @@ -436,6 +443,7 @@ buildActionMask = 2147483647; files = ( D9DCD6FE1E625C560010D1C7 /* CapabilitiesHashingTest.m in Sources */, + DD4003FB1F7528B40078D144 /* XMPPDelayedDeliveryTests.m in Sources */, D9DCD6FF1E625C560010D1C7 /* XMPPMUCLightTests.m in Sources */, D9DCD7001E625C560010D1C7 /* EncodeDecodeTest.m in Sources */, D9DCD7011E625C560010D1C7 /* XMPPRoomLightCoreDataStorageTests.m in Sources */, diff --git a/Xcode/Testing-Shared/XMPPDelayedDeliveryTests.m b/Xcode/Testing-Shared/XMPPDelayedDeliveryTests.m new file mode 100644 index 0000000000..34b165c8b4 --- /dev/null +++ b/Xcode/Testing-Shared/XMPPDelayedDeliveryTests.m @@ -0,0 +1,131 @@ +#import +#import "XMPPMockStream.h" + +@class XMPPDelayedDeliveryTestCallbackResult; + +@interface XMPPDelayedDeliveryTests : XCTestCase + +@property (nonatomic, strong) XMPPMockStream *mockStream; +@property (nonatomic, strong) XMPPDelayedDelivery *delayedDelivery; +@property (nonatomic, strong) XCTestExpectation *delegateCallbackExpectation; + +@end + +@implementation XMPPDelayedDeliveryTests + +- (void)setUp { + [super setUp]; + + self.mockStream = [[XMPPMockStream alloc] init]; + + self.delayedDelivery = [[XMPPDelayedDelivery alloc] init]; + [self.delayedDelivery addDelegate:self delegateQueue:dispatch_get_main_queue()]; + [self.delayedDelivery activate:self.mockStream]; +} + +- (void)testMessageDelegateCallback +{ + self.delegateCallbackExpectation = [self expectationWithDescription:@"Test message delegate callback expectation"]; + + [self fakeDelayedDeliveryMessage]; + + [self waitForExpectationsWithTimeout:5 handler:nil]; +} + +- (void)testPresenceDelegateCallback +{ + self.delegateCallbackExpectation = [self expectationWithDescription:@"Test presence delegate callback expectation"]; + + [self fakeDelayedDeliveryPresence]; + + [self waitForExpectationsWithTimeout:5 handler:nil]; +} + +- (void)testStanzaSkipping +{ + self.delegateCallbackExpectation = [self expectationWithDescription:@"Test skipped delegate callback expectation"]; + self.delegateCallbackExpectation.inverted = YES; + + [self fakePlainMessage]; + [self fakePlainPresence]; + + [self waitForExpectationsWithTimeout:1 handler:nil]; +} + +- (void)xmppDelayedDelivery:(XMPPDelayedDelivery *)xmppDelayedDelivery didReceiveDelayedMessage:(XMPPMessage *)delayedMessage +{ + if ([self.delegateCallbackExpectation isInverted] || + ([[delayedMessage delayedDeliveryDate] isEqualToDate:[NSDate dateWithXmppDateTimeString:@"2002-09-10T23:08:25Z"]] + && [[delayedMessage delayedDeliveryFrom] isEqualToJID:[XMPPJID jidWithString:@"capulet.com"]] + && [[delayedMessage delayedDeliveryReasonDescription] isEqualToString:@"Offline Storage"])) { + [self.delegateCallbackExpectation fulfill]; + } +} + +- (void)xmppDelayedDelivery:(XMPPDelayedDelivery *)xmppDelayedDelivery didReceiveDelayedPresence:(XMPPPresence *)delayedPresence +{ + if ([self.delegateCallbackExpectation isInverted] || + ([[delayedPresence delayedDeliveryDate] isEqualToDate:[NSDate dateWithXmppDateTimeString:@"2002-09-10T23:41:07Z"]] + && [[delayedPresence delayedDeliveryFrom] isEqualToJID:[XMPPJID jidWithString:@"juliet@capulet.com/balcony"]] + && [[delayedPresence delayedDeliveryReasonDescription] isEqualToString:@""])) { + [self.delegateCallbackExpectation fulfill]; + } +} + +- (void)fakeDelayedDeliveryMessage +{ + [self.mockStream fakeMessageResponse: + [[XMPPMessage alloc] initWithXMLString: + @"" + @"" + @"O blessed, blessed night! I am afeard." + @"Being in night, all this is but a dream," + @"Too flattering-sweet to be substantial." + @"" + @"" + @"Offline Storage" + @"" + @"" + error:nil]]; +} + +- (void)fakeDelayedDeliveryPresence +{ + [self.mockStream fakeResponse: + [[XMPPPresence alloc] initWithXMLString: + @"" + @"anon!" + @"xa" + @"1" + @"" + @"" + error:nil]]; +} + +- (void)fakePlainMessage +{ + [self.mockStream fakeMessageResponse: + [[XMPPMessage alloc] initWithXMLString: + @"" + @"" + @"O blessed, blessed night! I am afeard." + @"Being in night, all this is but a dream," + @"Too flattering-sweet to be substantial." + @"" + @"" + error:nil]]; +} + +- (void)fakePlainPresence +{ + [self.mockStream fakeResponse: + [[XMPPPresence alloc] initWithXMLString: + @"" + @"anon!" + @"xa" + @"1" + @"" + error:nil]]; +} + +@end diff --git a/Xcode/Testing-iOS/XMPPFrameworkTests.xcodeproj/project.pbxproj b/Xcode/Testing-iOS/XMPPFrameworkTests.xcodeproj/project.pbxproj index 393a79e644..9c3cff6beb 100644 --- a/Xcode/Testing-iOS/XMPPFrameworkTests.xcodeproj/project.pbxproj +++ b/Xcode/Testing-iOS/XMPPFrameworkTests.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ D99C5E0E1D99C48100FB068A /* OMEMOTestStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = D99C5E0C1D99C48100FB068A /* OMEMOTestStorage.m */; }; D9E35E701D90B894002E7CF7 /* OMEMOElementTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D9E35E6F1D90B894002E7CF7 /* OMEMOElementTests.m */; }; DD1E732C1ED86B7D009B529B /* XMPPPubSubTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DD1E732B1ED86B7D009B529B /* XMPPPubSubTests.m */; }; + DD24E0251F7119A300FA813C /* XMPPDelayedDeliveryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DD24E0241F7119A300FA813C /* XMPPDelayedDeliveryTests.m */; }; FDD2AB232C05507F2045FFFC /* Pods_XMPPFrameworkTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5CD0B17267211A912DE2098E /* Pods_XMPPFrameworkTests.framework */; }; /* End PBXBuildFile section */ @@ -55,6 +56,7 @@ D99C5E0C1D99C48100FB068A /* OMEMOTestStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OMEMOTestStorage.m; path = "../../Testing-Shared/OMEMOTestStorage.m"; sourceTree = ""; }; D9E35E6F1D90B894002E7CF7 /* OMEMOElementTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OMEMOElementTests.m; path = "../../Testing-Shared/OMEMOElementTests.m"; sourceTree = ""; }; DD1E732B1ED86B7D009B529B /* XMPPPubSubTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = XMPPPubSubTests.m; path = "../../Testing-Shared/XMPPPubSubTests.m"; sourceTree = ""; }; + DD24E0241F7119A300FA813C /* XMPPDelayedDeliveryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = XMPPDelayedDeliveryTests.m; path = "../../Testing-Shared/XMPPDelayedDeliveryTests.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -108,6 +110,7 @@ D973A07A1D2F18040096F3ED /* XMPPURITests.m */, D973A07B1D2F18040096F3ED /* XMPPvCardTests.m */, DD1E732B1ED86B7D009B529B /* XMPPPubSubTests.m */, + DD24E0241F7119A300FA813C /* XMPPDelayedDeliveryTests.m */, 63F50D971C60208200CA0201 /* Info.plist */, D973A06E1D2F18030096F3ED /* XMPPFrameworkTests-Bridging-Header.h */, ); @@ -251,6 +254,7 @@ buildActionMask = 2147483647; files = ( D973A07C1D2F18040096F3ED /* CapabilitiesHashingTest.m in Sources */, + DD24E0251F7119A300FA813C /* XMPPDelayedDeliveryTests.m in Sources */, D973A0811D2F18040096F3ED /* XMPPMUCLightTests.m in Sources */, D973A07D1D2F18040096F3ED /* EncodeDecodeTest.m in Sources */, D973A0831D2F18040096F3ED /* XMPPRoomLightCoreDataStorageTests.m in Sources */, diff --git a/Xcode/Testing-macOS/XMPPFrameworkTests.xcodeproj/project.pbxproj b/Xcode/Testing-macOS/XMPPFrameworkTests.xcodeproj/project.pbxproj index 3c8030996f..b7562e416e 100644 --- a/Xcode/Testing-macOS/XMPPFrameworkTests.xcodeproj/project.pbxproj +++ b/Xcode/Testing-macOS/XMPPFrameworkTests.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ D99C5E091D95EBA100FB068A /* OMEMOTestStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = D99C5E081D95EBA100FB068A /* OMEMOTestStorage.m */; }; D9E35E6E1D90B2C5002E7CF7 /* OMEMOElementTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D9E35E6D1D90B2C5002E7CF7 /* OMEMOElementTests.m */; }; D9F20D011D836080002A8D6F /* OMEMOModuleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D9F20D001D836080002A8D6F /* OMEMOModuleTests.m */; }; + DD24E0271F71463C00FA813C /* XMPPDelayedDeliveryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DD24E0261F71463C00FA813C /* XMPPDelayedDeliveryTests.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -52,6 +53,7 @@ D99C5E081D95EBA100FB068A /* OMEMOTestStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OMEMOTestStorage.m; path = "../../Testing-Shared/OMEMOTestStorage.m"; sourceTree = ""; }; D9E35E6D1D90B2C5002E7CF7 /* OMEMOElementTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OMEMOElementTests.m; path = "../../Testing-Shared/OMEMOElementTests.m"; sourceTree = ""; }; D9F20D001D836080002A8D6F /* OMEMOModuleTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OMEMOModuleTests.m; path = "../../Testing-Shared/OMEMOModuleTests.m"; sourceTree = ""; }; + DD24E0261F71463C00FA813C /* XMPPDelayedDeliveryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = XMPPDelayedDeliveryTests.m; path = "../../Testing-Shared/XMPPDelayedDeliveryTests.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -108,6 +110,7 @@ D973A0A11D2F1EF60096F3ED /* XMPPSwift.swift */, D973A0A21D2F1EF60096F3ED /* XMPPURITests.m */, D973A0A31D2F1EF60096F3ED /* XMPPvCardTests.m */, + DD24E0261F71463C00FA813C /* XMPPDelayedDeliveryTests.m */, D973A0921D2F1EB10096F3ED /* Info.plist */, ); path = XMPPFrameworkTests; @@ -248,6 +251,7 @@ D973A0A41D2F1EF60096F3ED /* CapabilitiesHashingTest.m in Sources */, D9F20D011D836080002A8D6F /* OMEMOModuleTests.m in Sources */, D973A0A91D2F1EF60096F3ED /* XMPPMUCLightTests.m in Sources */, + DD24E0271F71463C00FA813C /* XMPPDelayedDeliveryTests.m in Sources */, D973A0A51D2F1EF60096F3ED /* EncodeDecodeTest.m in Sources */, D973A0AB1D2F1EF60096F3ED /* XMPPRoomLightCoreDataStorageTests.m in Sources */, D973A0AE1D2F1EF60096F3ED /* XMPPSwift.swift in Sources */,