diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index ac1c43bb..54b1d2b7 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -168,13 +168,12 @@ dict[name] = encDict; } else if ([name isEqualToString:SURSSElementPubDate]) { - // pubDate is expected to be an NSDate by SUAppcastItem, but the RSS class was returning an NSString - NSString *string = node.stringValue; - if (string) { - NSDate *date = [NSDate dateWithNaturalLanguageString:string]; - if (date) { - dict[name] = date; - } + // We don't want to parse and create a NSDate instance - + // that's a risk we can avoid. We don't use the date anywhere other + // than it being accessible from SUAppcastItem + NSString *dateString = node.stringValue; + if (dateString) { + dict[name] = dateString; } } else if ([name isEqualToString:SUAppcastElementDeltas]) { @@ -220,11 +219,8 @@ return nil; } } - - if ([appcastItems count]) { - NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]; - [appcastItems sortUsingDescriptors:@[sort]]; - } + + self.items = appcastItems; return appcastItems; } diff --git a/Sparkle/SUAppcastItem.h b/Sparkle/SUAppcastItem.h index 86843bfb..47b6ee48 100644 --- a/Sparkle/SUAppcastItem.h +++ b/Sparkle/SUAppcastItem.h @@ -14,7 +14,7 @@ SU_EXPORT @interface SUAppcastItem : NSObject @property (copy, readonly) NSString *title; -@property (copy, readonly) NSDate *date; +@property (copy, readonly) NSString *dateString; @property (copy, readonly) NSString *itemDescription; @property (strong, readonly) NSURL *releaseNotesURL; @property (copy, readonly) NSString *DSASignature; diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index a98d7afa..972758ba 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -16,7 +16,7 @@ @interface SUAppcastItem () @property (copy, readwrite) NSString *title; -@property (copy, readwrite) NSDate *date; +@property (copy, readwrite) NSString *dateString; @property (copy, readwrite) NSString *itemDescription; @property (strong, readwrite) NSURL *releaseNotesURL; @property (copy, readwrite) NSString *DSASignature; @@ -31,7 +31,7 @@ @end @implementation SUAppcastItem -@synthesize date; +@synthesize dateString; @synthesize deltaUpdates; @synthesize displayVersionString; @synthesize DSASignature; @@ -104,7 +104,7 @@ propertiesDictionary = [[NSMutableDictionary alloc] initWithDictionary:dict]; self.title = dict[SURSSElementTitle]; - self.date = dict[SURSSElementPubDate]; + self.dateString = dict[SURSSElementPubDate]; self.itemDescription = dict[SURSSElementDescription]; NSString *theInfoURL = dict[SURSSElementLink]; diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index 8c6960d8..301b4bc5 100644 --- a/Sparkle/SUBasicUpdateDriver.h +++ b/Sparkle/SUBasicUpdateDriver.h @@ -24,7 +24,7 @@ - (void)checkForUpdatesAtURL:(NSURL *)URL host:(SUHost *)host; - (BOOL)isItemNewer:(SUAppcastItem *)ui; -- (BOOL)hostSupportsItem:(SUAppcastItem *)ui; ++ (BOOL)hostSupportsItem:(SUAppcastItem *)ui; - (BOOL)itemContainsSkippedVersion:(SUAppcastItem *)ui; - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui; - (void)appcastDidFinishLoading:(SUAppcast *)ac; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 0243eec5..fefc6a71 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -82,12 +82,28 @@ return comparator; } -- (BOOL)isItemNewer:(SUAppcastItem *)ui ++ (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator { - return [[self versionComparator] compareVersion:[self.host version] toVersion:[ui versionString]] == NSOrderedAscending; + SUAppcastItem *item = nil; + for(SUAppcastItem *candidate in appcastItems) { + if ([[self class] hostSupportsItem:candidate]) { + if (!item || [comparator compareVersion:item.versionString toVersion:candidate.versionString] == NSOrderedAscending) { + item = candidate; + } + } + } + + if (item && deltaItem) { + SUAppcastItem *deltaUpdateItem = [item deltaUpdates][hostVersion]; + if (deltaUpdateItem && [[self class] hostSupportsItem:deltaUpdateItem]) { + *deltaItem = deltaUpdateItem; + } + } + + return item; } -- (BOOL)hostSupportsItem:(SUAppcastItem *)ui ++ (BOOL)hostSupportsItem:(SUAppcastItem *)ui { if (([ui minimumSystemVersion] == nil || [[ui minimumSystemVersion] isEqualToString:@""]) && ([ui maximumSystemVersion] == nil || [[ui maximumSystemVersion] isEqualToString:@""])) { return YES; } @@ -106,6 +122,11 @@ return minimumVersionOK && maximumVersionOK; } +- (BOOL)isItemNewer:(SUAppcastItem *)ui +{ + return [[self versionComparator] compareVersion:[self.host version] toVersion:[ui versionString]] == NSOrderedAscending; +} + - (BOOL)itemContainsSkippedVersion:(SUAppcastItem *)ui { NSString *skippedVersion = [self.host objectForUserDefaultsKey:SUSkippedVersionKey]; @@ -115,7 +136,7 @@ - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui { - return ui && [self hostSupportsItem:ui] && [self isItemNewer:ui] && ![self itemContainsSkippedVersion:ui]; + return ui && [[self class] hostSupportsItem:ui] && [self isItemNewer:ui] && ![self itemContainsSkippedVersion:ui]; } - (void)appcastDidFinishLoading:(SUAppcast *)ac @@ -139,20 +160,13 @@ } else // If not, we'll take care of it ourselves. { - // Find the first update we can actually use. - for(SUAppcastItem *i in ac.items) { - if ([self hostSupportsItem:i]) { - item = i; - break; - } - } - - if (item) { - SUAppcastItem *deltaUpdateItem = [item deltaUpdates][[self.host version]]; - if (deltaUpdateItem && [self hostSupportsItem:deltaUpdateItem]) { - self.nonDeltaUpdateItem = item; - item = deltaUpdateItem; - } + // Find the best supported update + SUAppcastItem *deltaUpdateItem = nil; + item = [[self class] bestItemFromAppcastItems:ac.items getDeltaItem:&deltaUpdateItem withHostVersion:self.host.version comparator:[self versionComparator]]; + + if (item && deltaUpdateItem) { + self.nonDeltaUpdateItem = item; + item = deltaUpdateItem; } } diff --git a/Sparkle/SUUserInitiatedUpdateDriver.m b/Sparkle/SUUserInitiatedUpdateDriver.m index d1e0c9a6..04e96b23 100644 --- a/Sparkle/SUUserInitiatedUpdateDriver.m +++ b/Sparkle/SUUserInitiatedUpdateDriver.m @@ -81,7 +81,7 @@ - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui { // We don't check to see if this update's been skipped, because the user explicitly *asked* if he had the latest version. - return [self hostSupportsItem:ui] && [self isItemNewer:ui]; + return [[self class] hostSupportsItem:ui] && [self isItemNewer:ui]; } @end diff --git a/Tests/Resources/testappcast.xml b/Tests/Resources/testappcast.xml index 0023508e..e3894b41 100644 --- a/Tests/Resources/testappcast.xml +++ b/Tests/Resources/testappcast.xml @@ -8,10 +8,38 @@ Sat, 26 Jul 2014 15:20:11 +0000 + Version 3.0 - Sat, 26 Jul 2014 15:20:12 +0000 + + + + + + + + + Version 4.0 + Sat, 26 Jul 2014 15:20:13 +0000 + + 17.0.0 + + + + Version 5.0 + + 2.0.0 diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index 5f39f795..14fa2978 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -20,12 +20,43 @@ class SUAppcastTest: XCTestCase { do { let items = try appcast.parseAppcastItemsFromXMLFile(testFileUrl) as! [SUAppcastItem]; - XCTAssertEqual(2, items.count); - XCTAssertEqual("Version 3.0", items[0].title); - XCTAssertNil(items[0].itemDescription); + XCTAssertEqual(4, items.count); - XCTAssertEqual("Version 2.0", items[1].title); - XCTAssertEqual("desc", items[1].itemDescription); + XCTAssertEqual("Version 2.0", items[0].title); + XCTAssertEqual("desc", items[0].itemDescription); + XCTAssertEqual("Sat, 26 Jul 2014 15:20:11 +0000", items[0].dateString) + + // This is the best release matching our system version + XCTAssertEqual("Version 3.0", items[1].title); + XCTAssertNil(items[1].itemDescription); + XCTAssertNil(items[1].dateString) + + XCTAssertEqual("Version 4.0", items[2].title); + XCTAssertNil(items[2].itemDescription); + XCTAssertEqual("Sat, 26 Jul 2014 15:20:13 +0000", items[2].dateString) + + XCTAssertEqual("Version 5.0", items[3].title); + XCTAssertNil(items[3].itemDescription); + XCTAssertNil(items[3].dateString) + + // Test best appcast item & a delta update item + var deltaItem: SUAppcastItem? = nil + let bestAppcastItem = SUBasicUpdateDriver.bestItemFromAppcastItems(items, getDeltaItem: &deltaItem, withHostVersion: "1.0", comparator: SUStandardVersionComparator.defaultComparator()) + + XCTAssertEqual(bestAppcastItem, items[1]) + XCTAssertEqual(deltaItem!.fileURL.lastPathComponent, "3.0_from_1.0.patch") + + // Test latest delta update item available + var latestDeltaItem: SUAppcastItem? = nil + SUBasicUpdateDriver.bestItemFromAppcastItems(items, getDeltaItem: &latestDeltaItem, withHostVersion: "2.0", comparator: SUStandardVersionComparator.defaultComparator()) + + XCTAssertEqual(latestDeltaItem!.fileURL.lastPathComponent, "3.0_from_2.0.patch") + + // Test a delta item that does not exist + var nonexistantDeltaItem: SUAppcastItem? = nil + SUBasicUpdateDriver.bestItemFromAppcastItems(items, getDeltaItem: &nonexistantDeltaItem, withHostVersion: "2.1", comparator: SUStandardVersionComparator.defaultComparator()) + + XCTAssertNil(nonexistantDeltaItem) } catch let err as NSError { NSLog("%@", err); XCTFail(err.localizedDescription); diff --git a/Tests/Sparkle Unit Tests-Bridging-Header.h b/Tests/Sparkle Unit Tests-Bridging-Header.h index 512f4a58..d08dafb8 100644 --- a/Tests/Sparkle Unit Tests-Bridging-Header.h +++ b/Tests/Sparkle Unit Tests-Bridging-Header.h @@ -8,6 +8,9 @@ #import "SUFileManager.h" #import "SUAppcast.h" #import "SUAppcastItem.h" +#import "SUBasicUpdateDriver.h" +#import "SUVersionComparisonProtocol.h" +#import "SUStandardVersionComparator.h" // Duplicated to avoid exporting a private symbol from SUFileManager static const char *SUAppleQuarantineIdentifier = "com.apple.quarantine"; @@ -23,6 +26,12 @@ static const char *SUAppleQuarantineIdentifier = "com.apple.quarantine"; @end +@interface SUBasicUpdateDriver (Private) + ++ (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator; + +@end + @interface SUAppcast (Private) - (NSArray *)parseAppcastItemsFromXMLFile:(NSURL *)appcastFile error:(NSError *__autoreleasing*)errorp;